source: rtems/c/src/exec/score/src/coretod.c @ 63edbb3f

4.104.114.84.95
Last change on this file since 63edbb3f was ac7d5ef0, checked in by Joel Sherrill <joel.sherrill@…>, on 05/11/95 at 17:39:37

Initial revision

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 *  Time of Day (TOD) Handler
3 *
4 *
5 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
6 *  On-Line Applications Research Corporation (OAR).
7 *  All rights assigned to U.S. Government, 1994.
8 *
9 *  This material may be reproduced by or for the U.S. Government pursuant
10 *  to the copyright license under the clause at DFARS 252.227-7013.  This
11 *  notice must appear in all copies of this file and its derivatives.
12 *
13 *  $Id$
14 */
15
16#include <rtems/system.h>
17#include <rtems/object.h>
18#include <rtems/thread.h>
19#include <rtems/tod.h>
20#include <rtems/watchdog.h>
21
22/*PAGE
23 *
24 *  _TOD_Handler_initialization
25 *
26 *  This routine initializes the time of day handler.
27 *
28 *  Input parameters:
29 *    microseconds_per_tick - microseconds between clock ticks
30 *
31 *  Output parameters: NONE
32 */
33
34void _TOD_Handler_initialization(
35  unsigned32 microseconds_per_tick
36)
37{
38  _TOD_Microseconds_per_tick = microseconds_per_tick;
39
40  _TOD_Ticks_since_boot = 0;
41  _TOD_Seconds_since_epoch = 0;
42
43  _TOD_Current.year   = TOD_BASE_YEAR;
44  _TOD_Current.month  = 1;
45  _TOD_Current.day    = 1;
46  _TOD_Current.hour   = 0;
47  _TOD_Current.minute = 0;
48  _TOD_Current.second = 0;
49  _TOD_Current.ticks  = 0;
50
51  if ( microseconds_per_tick == 0 )
52    _TOD_Ticks_per_second = 0;
53  else
54    _TOD_Ticks_per_second =
55       TOD_MICROSECONDS_PER_SECOND / microseconds_per_tick;
56
57  _Watchdog_Initialize( &_TOD_Seconds_watchdog, _TOD_Tickle, 0, NULL );
58}
59
60/*PAGE
61 *
62 *  _TOD_Set
63 *
64 *  This rountine sets the current date and time with the specified
65 *  new date and time structure.
66 *
67 *  Input parameters:
68 *    the_tod             - pointer to the time and date structure
69 *    seconds_since_epoch - seconds since system epoch
70 *
71 *  Output parameters: NONE
72 */
73
74void _TOD_Set(
75  rtems_time_of_day *the_tod,
76  rtems_interval   seconds_since_epoch
77)
78{
79  rtems_interval ticks_until_next_second;
80
81  _Thread_Disable_dispatch();
82  _TOD_Deactivate();
83
84  if ( seconds_since_epoch < _TOD_Seconds_since_epoch )
85    _Watchdog_Adjust_seconds( WATCHDOG_BACKWARD,
86       _TOD_Seconds_since_epoch - seconds_since_epoch );
87  else
88    _Watchdog_Adjust_seconds( WATCHDOG_FORWARD,
89       seconds_since_epoch - _TOD_Seconds_since_epoch );
90
91  ticks_until_next_second = _TOD_Ticks_per_second;
92  if ( ticks_until_next_second > _TOD_Current.ticks )
93    ticks_until_next_second -= _TOD_Current.ticks;
94
95  _TOD_Current             = *the_tod;
96  _TOD_Seconds_since_epoch = seconds_since_epoch;
97  _TOD_Activate( ticks_until_next_second );
98
99  _Thread_Enable_dispatch();
100}
101
102/*PAGE
103 *
104 *  _TOD_Validate
105 *
106 *  This kernel routine checks the validity of a date and time structure.
107 *
108 *  Input parameters:
109 *    the_tod - pointer to a time and date structure
110 *
111 *  Output parameters:
112 *    RTEMS_SUCCESSFUL    - if the date, time, and tick are valid
113 *    RTEMS_INVALID_CLOCK - if the the_tod is invalid
114 *
115 *  NOTE: This routine only works for leap-years through 2099.
116 */
117
118rtems_status_code _TOD_Validate(
119  rtems_time_of_day *the_tod
120)
121{
122  unsigned32 days_in_month;
123
124  if ((the_tod->ticks  >= _TOD_Ticks_per_second)  ||
125      (the_tod->second >= TOD_SECONDS_PER_MINUTE) ||
126      (the_tod->minute >= TOD_MINUTES_PER_HOUR)   ||
127      (the_tod->hour   >= TOD_HOURS_PER_DAY)      ||
128      (the_tod->month  == 0)                      ||
129      (the_tod->month  >  TOD_MONTHS_PER_YEAR)    ||
130      (the_tod->year   <  TOD_BASE_YEAR)          ||
131      (the_tod->day    == 0) )
132     return RTEMS_INVALID_CLOCK;
133
134  if ( (the_tod->year % 4) == 0 )
135    days_in_month = _TOD_Days_per_month[ 1 ][ the_tod->month ];
136  else
137    days_in_month = _TOD_Days_per_month[ 0 ][ the_tod->month ];
138
139  if ( the_tod->day > days_in_month )
140    return RTEMS_INVALID_CLOCK;
141
142  return RTEMS_SUCCESSFUL;
143}
144
145/*PAGE
146 *
147 *  _TOD_To_seconds
148 *
149 *  This routine returns the seconds from the epoch until the
150 *  current date and time.
151 *
152 *  Input parameters:
153 *    the_tod - pointer to the time and date structure
154 *
155 *  Output parameters:
156 *    returns    - seconds since epoch until the_tod
157 */
158
159unsigned32 _TOD_To_seconds(
160  rtems_time_of_day *the_tod
161)
162{
163  unsigned32 time;
164  unsigned32 year_mod_4;
165
166  time = the_tod->day - 1;
167  year_mod_4 = the_tod->year & 3;
168
169  if ( year_mod_4 == 0 )
170    time += _TOD_Days_to_date[ 1 ][ the_tod->month ];
171  else
172    time += _TOD_Days_to_date[ 0 ][ the_tod->month ];
173
174  time += ( (the_tod->year - TOD_BASE_YEAR) / 4 ) *
175            ( (TOD_DAYS_PER_YEAR * 4) + 1);
176
177  time += _TOD_Days_since_last_leap_year[ year_mod_4 ];
178
179  time *= TOD_SECONDS_PER_DAY;
180
181  time += ((the_tod->hour * TOD_MINUTES_PER_HOUR) + the_tod->minute)
182             * TOD_SECONDS_PER_MINUTE;
183
184  time += the_tod->second;
185
186  return( time );
187}
188
189/*PAGE
190 *
191 *  _TOD_Tickle
192 *
193 *  This routine updates the calendar time and tickles the
194 *  per second watchdog timer chain.
195 *
196 *  Input parameters:
197 *    ignored - this parameter is ignored
198 *
199 *  Output parameters: NONE
200 *
201 *  NOTE: This routine only works for leap-years through 2099.
202 */
203
204void _TOD_Tickle(
205  Objects_Id  id,
206  void       *ignored
207)
208{
209  unsigned32 leap;
210
211  _TOD_Current.ticks = 0;
212  ++_TOD_Seconds_since_epoch;
213  if ( ++_TOD_Current.second >= TOD_SECONDS_PER_MINUTE ) {
214    _TOD_Current.second = 0;
215    if ( ++_TOD_Current.minute >= TOD_MINUTES_PER_HOUR ) {
216      _TOD_Current.minute = 0;
217      if ( ++_TOD_Current.hour >= TOD_HOURS_PER_DAY ) {
218        _TOD_Current.hour = 0;
219        if ( _TOD_Current.year & 0x3 ) leap = 0;
220        else                           leap = 1;
221        if ( ++_TOD_Current.day >
222               _TOD_Days_per_month[ leap ][ _TOD_Current.month ]) {
223          _TOD_Current.day = 1;
224          if ( ++_TOD_Current.month > TOD_MONTHS_PER_YEAR ) {
225            _TOD_Current.month = 1;
226            _TOD_Current.year++;
227          }
228        }
229      }
230    }
231  }
232
233  _Watchdog_Tickle_seconds();
234  _Watchdog_Insert_ticks( &_TOD_Seconds_watchdog, _TOD_Ticks_per_second,
235                          WATCHDOG_ACTIVATE_NOW );
236}
Note: See TracBrowser for help on using the repository browser.