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

5
Last change on this file since e52906b was e52906b, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:06

Simplify SPDX-License-Identifier comment

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