source: rtems/c/src/lib/libbsp/shared/tod.c @ 0bd6d2b1

Last change on this file since 0bd6d2b1 was 0bd6d2b1, checked in by Eric Norum <WENorum@…>, on 10/13/04 at 14:07:22

Set time from TOD to RTEMS as part of TOD initialization.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 *  Real Time Clock Driver Wrapper for Libchip
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.com/license/LICENSE.
7 *
8 *  $Id$
9 */
10
11#include <rtems.h>
12#include <libchip/rtc.h>
13
14/*
15 *  Configuration Information
16 */
17
18extern unsigned long              RTC_Count;
19extern rtems_device_minor_number  RTC_Minor;
20
21int RTC_Present;
22
23/*
24 *  rtc_initialize
25 *
26 *  Initialize the RTC driver
27 */
28
29rtems_device_driver rtc_initialize(
30  rtems_device_major_number  major,
31  rtems_device_minor_number  minor_arg,
32  void                      *arg
33)
34{
35  rtems_device_minor_number  minor;
36  rtems_status_code          status;
37
38  for (minor=0; minor < RTC_Count ; minor++) {
39    /*
40     * First perform the configuration dependent probe, then the
41     * device dependent probe
42     */
43
44    if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) {
45      /*
46       * Use this device as the primary RTC
47       */
48      RTC_Minor = minor;
49      RTC_Present = 1;
50      break;
51    }
52  }
53
54  if ( !RTC_Present ) {
55    /*
56     * Failed to find an RTC -- this is not a fatal error.
57     */
58
59    return RTEMS_INVALID_NUMBER;
60  }
61
62  /*
63   *  Register and initialize the primary RTC's
64   */
65
66  status = rtems_io_register_name( "/dev/rtc", major, RTC_Minor );
67  if (status != RTEMS_SUCCESSFUL) {
68    rtems_fatal_error_occurred(status);
69  }
70
71  RTC_Table[minor].pDeviceFns->deviceInitialize( RTC_Minor );
72
73  /*
74   *  Now initialize any secondary RTC's
75   */
76
77  for ( minor++ ; minor<RTC_Count ; minor++) {
78    /*
79     * First perform the configuration dependent probe, then the
80     * device dependent probe
81     */
82
83    if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) {
84      status = rtems_io_register_name(
85        RTC_Table[minor].sDeviceName,
86        major,
87        minor );
88      if (status != RTEMS_SUCCESSFUL) {
89        rtems_fatal_error_occurred(status);
90      }
91
92      /*
93       * Initialize the hardware device.
94       */
95
96      RTC_Table[minor].pDeviceFns->deviceInitialize(minor);
97
98    }
99  }
100
101  setRealTimeToRTEMS();
102  return RTEMS_SUCCESSFUL;
103}
104
105
106/*PAGE
107 *
108 *  This routine copies the time from the real time clock to RTEMS
109 *
110 *  Input parameters:  NONE
111 *
112 *  Output parameters:  NONE
113 *
114 *  Return values: NONE
115 */
116
117void setRealTimeToRTEMS()
118{
119  rtems_time_of_day rtc_tod;
120
121  if (!RTC_Present)
122    return;
123
124  RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod);
125  rtems_clock_set( &rtc_tod );
126}
127
128/*PAGE
129 *
130 *  setRealTimeFromRTEMS
131 *
132 *  This routine copies the time from RTEMS to the real time clock
133 *
134 *  Input parameters:  NONE
135 *
136 *  Output parameters:  NONE
137 *
138 *  Return values: NONE
139 */
140
141void setRealTimeFromRTEMS()
142{
143  rtems_time_of_day rtems_tod;
144
145  if (!RTC_Present)
146    return;
147
148  rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod );
149  RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, &rtems_tod);
150}
151
152/*PAGE
153 *
154 *  getRealTime
155 *
156 *  This routine reads the current time from the RTC.
157 *
158 *  Input parameters:  NONE
159 *
160 *  Output parameters:  NONE
161 *
162 *  Return values: NONE
163 */
164
165void getRealTime(
166  rtems_time_of_day *tod
167)
168{
169
170  if (!RTC_Present)
171    return;
172
173  RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, tod);
174}
175
176/*PAGE
177 *
178 *  setRealTime
179 * 
180 *  This routine sets the RTC.
181 * 
182 *  Input parameters:  NONE
183 *
184 *  Output parameters:  NONE
185 *
186 *  Return values: NONE
187 */
188
189/* XXX this routine should be part of the public RTEMS interface */
190rtems_boolean _TOD_Validate( rtems_time_of_day *tod );
191
192int setRealTime(
193  rtems_time_of_day *tod
194)
195{
196 
197  if (!RTC_Present)
198    return -1;
199 
200  if ( !_TOD_Validate(tod) )
201    return -1;
202
203  RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod);
204  return 0;
205}
206
207
208/*PAGE
209 *
210 *  checkRealTime
211 *
212 *  This routine reads the returns the variance betweent the real time and
213 *  rtems time.
214 *
215 *  Input parameters: NONE
216 *
217 *  Output parameters:  NONE
218 *
219 *  Return values:
220 *    int   The differance between the real time clock and rtems time.
221 */
222
223/* XXX this routine should be part of the public RTEMS interface */
224unsigned32 _TOD_To_seconds( rtems_time_of_day *tod );
225
226int checkRealTime()
227{
228  rtems_time_of_day rtems_tod;
229  rtems_time_of_day rtc_tod;
230  unsigned32 rtems_time;
231  unsigned32 rtc_time;
232
233  if (!RTC_Present)
234    return -1;
235
236  rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod );
237  RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod);
238
239  rtems_time = _TOD_To_seconds( &rtems_tod );
240  rtc_time = _TOD_To_seconds( &rtc_tod );
241
242  return rtems_time - rtc_time;
243}
244
Note: See TracBrowser for help on using the repository browser.