source: rtems-docs/bsp-howto/real_time_clock.rst @ 969e60e

5
Last change on this file since 969e60e was cb0f55a, checked in by Sebastian Huber <sebastian.huber@…>, on 04/26/18 at 07:05:20

Update due to BSP source reorganization

This patch is a part of the BSP source reorganization.

Close #3285.

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