source: rtems-docs/bsp_howto/real_time_clock.rst @ f916fca

4.115
Last change on this file since f916fca was d389819, checked in by Amar Takhar <amar@…>, on 01/18/16 at 05:37:40

Convert all Unicode to ASCII(128)

  • Property mode set to 100644
File size: 6.1 KB
Line 
1Real-Time Clock Driver
2######################
3
4Introduction
5============
6
7The Real-Time Clock (*RTC*) driver is responsible for providing an
8interface to an *RTC* device.  \[NOTE: In this chapter, the abbreviation*TOD* is used for *Time of Day*.]  The capabilities provided by this
9driver are:
10
11- Set the RTC TOD to RTEMS TOD
12
13- Set the RTEMS TOD to the RTC TOD
14
15- Get the RTC TOD
16
17- Set the RTC TOD to the Specified TOD
18
19- Get the Difference Between the RTEMS and RTC TOD
20
21The reference implementation for a real-time clock driver can
22be found in ``c/src/lib/libbsp/shared/tod.c``.  This driver
23is based on the libchip concept and can be easily configured
24to work with any of the RTC chips supported by the RTC
25chip drivers in the directory ``c/src/lib/lib/libchip/rtc``.
26There is a README file in this directory for each supported
27RTC chip.  Each of these README explains how to configure the
28shared libchip implementation of the RTC driver for that particular
29RTC chip.
30
31The DY-4 DMV177 BSP used the shared libchip implementation of the RTC
32driver.  There were no DMV177 specific configuration routines.  A BSP
33could use configuration routines to dynamically determine what type
34of real-time clock is on a particular board.  This would be useful for
35a BSP supporting multiple board models.  The relevant ports of
36the DMV177's ``RTC_Table`` configuration table is below:
37.. code:: c
38
39    #include <bsp.h>
40    #include <libchip/rtc.h>
41    #include <libchip/icm7170.h>
42    bool dmv177_icm7170_probe(int minor);
43    rtc_tbl     RTC_Table[] = {
44    { "/dev/rtc0",                /* sDeviceName \*/
45    RTC_ICM7170,                /* deviceType \*/
46    &icm7170_fns,               /* pDeviceFns \*/
47    dmv177_icm7170_probe,       /* deviceProbe \*/
48    (void \*) ICM7170_AT_1_MHZ,  /* pDeviceParams \*/
49    DMV170_RTC_ADDRESS,         /* ulCtrlPort1 \*/
50    0,                          /* ulDataPort \*/
51    icm7170_get_register_8,     /* getRegister \*/
52    icm7170_set_register_8,     /* setRegister \*/
53    }
54    };
55    unsigned long  RTC_Count = (sizeof(RTC_Table)/sizeof(rtc_tbl));
56    rtems_device_minor_number  RTC_Minor;
57    bool dmv177_icm7170_probe(int minor)
58    {
59    volatile unsigned16 \*card_resource_reg;
60    card_resource_reg = (volatile unsigned16 \*) DMV170_CARD_RESORCE_REG;
61    if ( (\*card_resource_reg & DMV170_RTC_INST_MASK) == DMV170_RTC_INSTALLED )
62    return TRUE;
63    return FALSE;
64    }
65
66Initialization
67==============
68
69The ``rtc_initialize`` routine is responsible for initializing the
70RTC chip so it can be used.  The shared libchip implementation of this
71driver supports multiple RTCs and bases its initialization order on
72the order the chips are defined in the ``RTC_Table``.  Each chip
73defined in the table may or may not be present on this particular board.
74It is the responsibility of the ``deviceProbe`` to indicate the
75presence of a particular RTC chip.  The first RTC found to be present
76is considered the preferred RTC.
77
78In the shared libchip based implementation
79of the driver, the following actions are performed:
80.. code:: c
81
82    rtems_device_driver rtc_initialize(
83    rtems_device_major_number  major,
84    rtems_device_minor_number  minor_arg,
85    void                      \*arg
86    )
87    {
88    for each RTC configured in RTC_Table
89    if the deviceProbe for this RTC indicates it is present
90    set RTC_Minor to this device
91    set RTC_Present to TRUE
92    break out of this loop
93    if RTC_Present is not TRUE
94    return RTEMS_INVALID_NUMBER to indicate that no RTC is present
95    register this minor number as the "/dev/rtc"
96    perform the deviceInitialize routine for the preferred RTC chip
97    for RTCs past this one in the RTC_Table
98    if the deviceProbe for this RTC indicates it is present
99    perform the deviceInitialize routine for this RTC chip
100    register the configured name for this RTC
101    }
102
103The ``deviceProbe`` routine returns TRUE if the device
104configured by this entry in the ``RTC_Table`` is present.
105This configuration scheme allows one to support multiple versions
106of the same board with a single BSP.  For example, if the first
107generation of a board had Vendor A's RTC chip and the second
108generation had Vendor B's RTC chip, RTC_Table could contain
109information for both.  The ``deviceProbe`` configured
110for Vendor A's RTC chip would need to return TRUE if the
111board was a first generation one.  The ``deviceProbe``
112routines are very board dependent and must be provided by
113the BSP.
114
115setRealTimeToRTEMS
116==================
117
118The ``setRealTimeToRTEMS`` routine sets the current RTEMS TOD to that
119of the preferred RTC.
120.. code:: c
121
122    void setRealTimeToRTEMS(void)
123    {
124    if no RTCs are present
125    return
126    invoke the deviceGetTime routine for the preferred RTC
127    set the RTEMS TOD using rtems_clock_set
128    }
129
130setRealTimeFromRTEMS
131====================
132
133The ``setRealTimeFromRTEMS`` routine sets the preferred RTC TOD to the
134current RTEMS TOD.
135.. code:: c
136
137    void setRealTimeFromRTEMS(void)
138    {
139    if no RTCs are present
140    return
141    obtain the RTEMS TOD using rtems_clock_get
142    invoke the deviceSetTime routine for the preferred RTC
143    }
144
145getRealTime
146===========
147
148The ``getRealTime`` returns the preferred RTC TOD to the
149caller.
150.. code:: c
151
152    void getRealTime( rtems_time_of_day \*tod )
153    {
154    if no RTCs are present
155    return
156    invoke the deviceGetTime routine for the preferred RTC
157    }
158
159setRealTime
160===========
161
162The ``setRealTime`` routine sets the preferred RTC TOD to the
163TOD specified by the caller.
164.. code:: c
165
166    void setRealTime( rtems_time_of_day \*tod )
167    {
168    if no RTCs are present
169    return
170    invoke the deviceSetTime routine for the preferred RTC
171    }
172
173checkRealTime
174=============
175
176The ``checkRealTime`` routine returns the number of seconds
177difference between the RTC TOD and the current RTEMS TOD.
178.. code:: c
179
180    int checkRealTime( void )
181    {
182    if no RTCs are present
183    return -1
184    obtain the RTEMS TOD using rtems_clock_get
185    get the TOD from the preferred RTC using the deviceGetTime routine
186    convert the RTEMS TOD to seconds
187    convert the RTC TOD to seconds
188    return the RTEMS TOD in seconds - RTC TOD in seconds
189    }
190
191.. COMMENT: COPYRIGHT (c) 1988-2002.
192
193.. COMMENT: On-Line Applications Research Corporation (OAR).
194
195.. COMMENT: All rights reserved.
196
Note: See TracBrowser for help on using the repository browser.