source: rtems/c/src/lib/libbsp/shared/tod.c @ 6748b76c

4.104.115
Last change on this file since 6748b76c was 6748b76c, checked in by Joel Sherrill <joel.sherrill@…>, on 02/11/09 at 23:06:55

2009-02-11 Joel Sherrill <joel.sherrill@…>

  • tod.c: Use rtems_clock_get_tod().
  • 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 size_t                     RTC_Count;
19extern rtems_device_minor_number  RTC_Minor;
20
21int RTC_Present;
22
23extern void setRealTimeToRTEMS(void);
24
25/*
26 *  rtc_initialize
27 *
28 *  Initialize the RTC driver
29 */
30
31rtems_device_driver rtc_initialize(
32  rtems_device_major_number  major,
33  rtems_device_minor_number  minor_arg,
34  void                      *arg
35)
36{
37  rtems_device_minor_number  minor;
38  rtems_status_code          status;
39
40  for (minor=0; minor < RTC_Count ; minor++) {
41    /*
42     * First perform the configuration dependent probe, then the
43     * device dependent probe
44     */
45
46    if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) {
47      /*
48       * Use this device as the primary RTC
49       */
50      RTC_Minor = minor;
51      RTC_Present = 1;
52      break;
53    }
54  }
55
56  if ( !RTC_Present ) {
57    /*
58     * Failed to find an RTC -- this is not a fatal error.
59     */
60
61    return RTEMS_INVALID_NUMBER;
62  }
63
64  /*
65   *  Register and initialize the primary RTC's
66   */
67
68  status = rtems_io_register_name( "/dev/rtc", major, RTC_Minor );
69  if (status != RTEMS_SUCCESSFUL) {
70    rtems_fatal_error_occurred(status);
71  }
72
73  RTC_Table[minor].pDeviceFns->deviceInitialize( RTC_Minor );
74
75  /*
76   *  Now initialize any secondary RTC's
77   */
78
79  for ( minor++ ; minor<RTC_Count ; minor++) {
80    /*
81     * First perform the configuration dependent probe, then the
82     * device dependent probe
83     */
84
85    if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) {
86      status = rtems_io_register_name(
87        RTC_Table[minor].sDeviceName,
88        major,
89        minor );
90      if (status != RTEMS_SUCCESSFUL) {
91        rtems_fatal_error_occurred(status);
92      }
93
94      /*
95       * Initialize the hardware device.
96       */
97
98      RTC_Table[minor].pDeviceFns->deviceInitialize(minor);
99
100    }
101  }
102
103  setRealTimeToRTEMS();
104  return RTEMS_SUCCESSFUL;
105}
106
107/*PAGE
108 *
109 *  This routine copies the time from the real time clock to RTEMS
110 *
111 *  Input parameters:  NONE
112 *
113 *  Output parameters:  NONE
114 *
115 *  Return values: NONE
116 */
117
118void setRealTimeToRTEMS()
119{
120  rtems_time_of_day rtc_tod;
121
122  if (!RTC_Present)
123    return;
124
125  RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod);
126  rtems_clock_set( &rtc_tod );
127}
128
129/*PAGE
130 *
131 *  setRealTimeFromRTEMS
132 *
133 *  This routine copies the time from RTEMS to the real time clock
134 *
135 *  Input parameters:  NONE
136 *
137 *  Output parameters:  NONE
138 *
139 *  Return values: NONE
140 */
141
142void setRealTimeFromRTEMS(void)
143{
144  rtems_time_of_day rtems_tod;
145
146  if (!RTC_Present)
147    return;
148
149  rtems_clock_get_tod( &rtems_tod );
150  RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, &rtems_tod);
151}
152
153/*PAGE
154 *
155 *  getRealTime
156 *
157 *  This routine reads the current time from the RTC.
158 *
159 *  Input parameters:  NONE
160 *
161 *  Output parameters:  NONE
162 *
163 *  Return values: NONE
164 */
165
166void getRealTime(
167  rtems_time_of_day *tod
168)
169{
170
171  if (!RTC_Present)
172    return;
173
174  RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, tod);
175}
176
177/*PAGE
178 *
179 *  setRealTime
180 *
181 *  This routine sets the RTC.
182 *
183 *  Input parameters:  NONE
184 *
185 *  Output parameters:  NONE
186 *
187 *  Return values: NONE
188 */
189
190/* XXX this routine should be part of the public RTEMS interface */
191extern bool _TOD_Validate( rtems_time_of_day *tod );
192
193int setRealTime(
194  rtems_time_of_day *tod
195)
196{
197
198  if (!RTC_Present)
199    return -1;
200
201  if ( !_TOD_Validate(tod) )
202    return -1;
203
204  RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod);
205  return 0;
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 */
224uint32_t   _TOD_To_seconds( rtems_time_of_day *tod );
225
226int checkRealTime(void)
227{
228  rtems_time_of_day rtems_tod;
229  rtems_time_of_day rtc_tod;
230  uint32_t   rtems_time;
231  uint32_t   rtc_time;
232
233  if (!RTC_Present)
234    return -1;
235
236  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}
Note: See TracBrowser for help on using the repository browser.