1 | /* |
---|
2 | * Timer Manager - rtems_timer_initiate_server directive along with |
---|
3 | * the Timer Server Body and support routines |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2002. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.OARcorp.com/rtems/license.html. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems/system.h> |
---|
16 | #include <rtems/rtems/status.h> |
---|
17 | #include <rtems/rtems/support.h> |
---|
18 | #include <rtems/score/object.h> |
---|
19 | #include <rtems/score/thread.h> |
---|
20 | #include <rtems/rtems/timer.h> |
---|
21 | #include <rtems/score/tod.h> |
---|
22 | #include <rtems/score/watchdog.h> |
---|
23 | |
---|
24 | #include <rtems/rtems/tasks.h> |
---|
25 | #include <rtems/rtems/support.h> |
---|
26 | #include <rtems/score/thread.h> |
---|
27 | |
---|
28 | /* |
---|
29 | * The following chains contain the list of interval timers that are |
---|
30 | * executed in the context of the Timer Server. |
---|
31 | * |
---|
32 | * NOTE: These are prototyped in rtems/timer/timer.h but since we |
---|
33 | * do not actually use them until after the Timer Server is |
---|
34 | * initiated, we can actually declare them here and avoid forcing |
---|
35 | * them into the minimum footprint. |
---|
36 | */ |
---|
37 | |
---|
38 | Chain_Control _Timer_Ticks_chain; |
---|
39 | Chain_Control _Timer_Seconds_chain; |
---|
40 | |
---|
41 | /* |
---|
42 | * The timer used to control when the Timer Server wakes up to service |
---|
43 | * "when" timers. |
---|
44 | * |
---|
45 | * NOTE: This should NOT be used outside this file. |
---|
46 | */ |
---|
47 | |
---|
48 | Watchdog_Control _Timer_Seconds_timer; |
---|
49 | |
---|
50 | /*PAGE |
---|
51 | * |
---|
52 | * _Timer_Server_body |
---|
53 | * |
---|
54 | * This is the server for task based timers. This task executes whenever |
---|
55 | * a task-based timer should fire. It services both "after" and "when" |
---|
56 | * timers. It is not created automatically but must be created explicitly |
---|
57 | * by the application before task-based timers may be initiated. |
---|
58 | * |
---|
59 | * Input parameters: |
---|
60 | * Ignored - the task argument is ignored |
---|
61 | * |
---|
62 | * Output parameters: NONE |
---|
63 | */ |
---|
64 | |
---|
65 | Thread _Timer_Server_body( |
---|
66 | unsigned32 ignored |
---|
67 | ) |
---|
68 | { |
---|
69 | Watchdog_Interval snapshot; |
---|
70 | Watchdog_Interval ticks_last_time; |
---|
71 | Watchdog_Interval seconds_last_time; |
---|
72 | Watchdog_Interval ticks; |
---|
73 | |
---|
74 | /* |
---|
75 | * Initialize the "last time" markers to indicate the timer that |
---|
76 | * the server was initiated. |
---|
77 | */ |
---|
78 | |
---|
79 | ticks_last_time = _Watchdog_Ticks_since_boot; |
---|
80 | seconds_last_time = _TOD_Seconds_since_epoch; |
---|
81 | |
---|
82 | _Thread_Disable_dispatch(); |
---|
83 | while(1) { |
---|
84 | |
---|
85 | /* |
---|
86 | * Block until there is something to do. |
---|
87 | */ |
---|
88 | |
---|
89 | _Thread_Set_state( _Timer_Server, STATES_DELAYING ); |
---|
90 | _Timer_Server_reset( TIMER_SERVER_RESET_TICKS ); |
---|
91 | _Timer_Server_reset( TIMER_SERVER_RESET_SECONDS ); |
---|
92 | _Thread_Enable_dispatch(); |
---|
93 | |
---|
94 | |
---|
95 | /* |
---|
96 | * Disable dispatching while processing the timers since we want |
---|
97 | * to mimic the environment that non-task-based TSRs execute in. |
---|
98 | * This ensures that the primary difference is that _ISR_Nest_level |
---|
99 | * is 0 for task-based timers and non-zero for the others. |
---|
100 | */ |
---|
101 | |
---|
102 | _Thread_Disable_dispatch(); |
---|
103 | |
---|
104 | /* |
---|
105 | * Process the ticks chain |
---|
106 | */ |
---|
107 | |
---|
108 | snapshot = _Watchdog_Ticks_since_boot; |
---|
109 | ticks = snapshot - ticks_last_time; |
---|
110 | ticks_last_time = snapshot; |
---|
111 | _Watchdog_Adjust( &_Timer_Ticks_chain, WATCHDOG_FORWARD, ticks ); |
---|
112 | |
---|
113 | /* |
---|
114 | * Process the seconds chain. Start by checking that the Time |
---|
115 | * of Day (TOD) has not been set backwards. If it has then |
---|
116 | * we want to adjust the _Timer_Seconds_chain to indicate this. |
---|
117 | */ |
---|
118 | |
---|
119 | snapshot = _TOD_Seconds_since_epoch; |
---|
120 | if ( snapshot > seconds_last_time ) { |
---|
121 | /* |
---|
122 | * This path is for normal forward movement and cases where the |
---|
123 | * TOD has been set forward. |
---|
124 | */ |
---|
125 | |
---|
126 | ticks = snapshot - seconds_last_time; |
---|
127 | _Watchdog_Adjust( &_Timer_Seconds_chain, WATCHDOG_FORWARD, ticks ); |
---|
128 | |
---|
129 | } else if ( snapshot < seconds_last_time ) { |
---|
130 | /* |
---|
131 | * The current TOD is before the last TOD which indicates that |
---|
132 | * TOD has been set backwards. |
---|
133 | */ |
---|
134 | |
---|
135 | ticks = seconds_last_time - snapshot; |
---|
136 | _Watchdog_Adjust( &_Timer_Seconds_chain, WATCHDOG_BACKWARD, ticks ); |
---|
137 | } |
---|
138 | seconds_last_time = snapshot; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /*PAGE |
---|
143 | * |
---|
144 | * rtems_timer_initiate_server |
---|
145 | * |
---|
146 | * This directive creates and starts the server for task-based timers. |
---|
147 | * It must be invoked before any task-based timers can be initiated. |
---|
148 | * |
---|
149 | * Input parameters: |
---|
150 | * stack_size - stack size in bytes |
---|
151 | * attribute_set - thread attributes |
---|
152 | * |
---|
153 | * Output parameters: |
---|
154 | * RTEMS_SUCCESSFUL - if successful |
---|
155 | * error code - if unsuccessful |
---|
156 | */ |
---|
157 | |
---|
158 | |
---|
159 | rtems_status_code rtems_timer_initiate_server( |
---|
160 | unsigned32 stack_size, |
---|
161 | rtems_attribute attribute_set |
---|
162 | ) |
---|
163 | { |
---|
164 | rtems_id id; |
---|
165 | rtems_status_code status; |
---|
166 | |
---|
167 | /* |
---|
168 | * Just to make sure the test versus create/start operation are atomic. |
---|
169 | */ |
---|
170 | |
---|
171 | _Thread_Disable_dispatch(); |
---|
172 | |
---|
173 | if ( _Timer_Server ) { |
---|
174 | _Thread_Enable_dispatch(); |
---|
175 | return RTEMS_INCORRECT_STATE; |
---|
176 | } |
---|
177 | |
---|
178 | /* |
---|
179 | * Create the Timer Server with the name the name of "TIME". The priority |
---|
180 | * is always "0" which makes it higher than any other task in the system. |
---|
181 | * It is also always NO_PREEMPT so it looks like an interrupt to other tasks. |
---|
182 | */ |
---|
183 | |
---|
184 | status = rtems_task_create( |
---|
185 | rtems_build_name( 'T', 'I', 'M', 'E' ), |
---|
186 | 1, /* create with priority 1 since 0 is illegal */ |
---|
187 | stack_size, /* let user specify stack size */ |
---|
188 | RTEMS_NO_PREEMPT, /* no preempt is like an interrupt */ |
---|
189 | attribute_set, /* user may want floating point */ |
---|
190 | &id /* get the id back */ |
---|
191 | ); |
---|
192 | if (status) { |
---|
193 | _Thread_Enable_dispatch(); |
---|
194 | return status; |
---|
195 | } |
---|
196 | |
---|
197 | status = rtems_task_start( |
---|
198 | id, /* the id from create */ |
---|
199 | (rtems_task_entry) _Timer_Server_body, /* the timer server entry point */ |
---|
200 | 0 /* there is no argument */ |
---|
201 | ); |
---|
202 | if (status) { |
---|
203 | /* |
---|
204 | * One would expect a call to rtems_task_delete() here to clean up |
---|
205 | * but there is actually no way (in normal circumstances) that the |
---|
206 | * start can fail. The id and starting address are known to be |
---|
207 | * be good. If this service fails, something is weirdly wrong on the |
---|
208 | * target such as a stray write in an ISR or incorrect memory layout. |
---|
209 | */ |
---|
210 | _Thread_Enable_dispatch(); |
---|
211 | return status; |
---|
212 | } |
---|
213 | |
---|
214 | /* |
---|
215 | * We work with the TCB pointer, not the ID, so we need to convert |
---|
216 | * to a TCB pointer from here out. |
---|
217 | * |
---|
218 | * NOTE: Setting the pointer to the Timer Server TCB to a value other than |
---|
219 | * NULL indicates that task-based timer support is initialized. |
---|
220 | */ |
---|
221 | |
---|
222 | _Timer_Server = (Thread_Control *)_Objects_Get_local_object( |
---|
223 | &_RTEMS_tasks_Information, |
---|
224 | _Objects_Get_index(id) |
---|
225 | ); |
---|
226 | |
---|
227 | /* |
---|
228 | * A priority of 0, higher than any user task, combined with no preemption |
---|
229 | * makes this task the highest priority in any application. It can be |
---|
230 | * viewed as a low priority interrupt. The FALSE argument indicates |
---|
231 | * that the task is to be appended (not prepended) to its priority |
---|
232 | * chain at the end of this operation. |
---|
233 | */ |
---|
234 | |
---|
235 | _Thread_Change_priority( _Timer_Server, 0, FALSE ); |
---|
236 | |
---|
237 | /* |
---|
238 | * Initialize the timer lists that the server will manage. |
---|
239 | */ |
---|
240 | |
---|
241 | _Chain_Initialize_empty( &_Timer_Ticks_chain ); |
---|
242 | _Chain_Initialize_empty( &_Timer_Seconds_chain ); |
---|
243 | |
---|
244 | /* |
---|
245 | * Initialize the timers that will be used to control when the |
---|
246 | * Timer Server wakes up and services the task-based timers. |
---|
247 | */ |
---|
248 | |
---|
249 | _Watchdog_Initialize( &_Timer_Server->Timer, _Thread_Delay_ended, id, NULL ); |
---|
250 | _Watchdog_Initialize( &_Timer_Seconds_timer, _Thread_Delay_ended, id, NULL ); |
---|
251 | |
---|
252 | _Thread_Enable_dispatch(); |
---|
253 | return RTEMS_SUCCESSFUL; |
---|
254 | } |
---|
255 | |
---|
256 | /*PAGE |
---|
257 | * |
---|
258 | * _Timer_Server_reset |
---|
259 | * |
---|
260 | * This routine resets the timers which determine when the Timer Server |
---|
261 | * will wake up next to service task-based timers. |
---|
262 | * |
---|
263 | * Input parameters: |
---|
264 | * do_ticks - TRUE indicates to process the ticks list |
---|
265 | * FALSE indicates to process the seconds list |
---|
266 | * |
---|
267 | * Output parameters: NONE |
---|
268 | */ |
---|
269 | |
---|
270 | void _Timer_Server_reset( |
---|
271 | Timer_Server_reset_mode reset_mode |
---|
272 | ) |
---|
273 | { |
---|
274 | Watchdog_Interval units; |
---|
275 | |
---|
276 | switch ( reset_mode ) { |
---|
277 | case TIMER_SERVER_RESET_TICKS: |
---|
278 | _Watchdog_Remove( &_Timer_Server->Timer ); |
---|
279 | units = ((Watchdog_Control *)_Timer_Ticks_chain.first)->delta_interval; |
---|
280 | _Watchdog_Insert_ticks( &_Timer_Server->Timer, units ); |
---|
281 | break; |
---|
282 | case TIMER_SERVER_RESET_SECONDS: |
---|
283 | _Watchdog_Remove( &_Timer_Seconds_timer ); |
---|
284 | units = ((Watchdog_Control *)_Timer_Seconds_chain.first)->delta_interval; |
---|
285 | _Watchdog_Insert_seconds( &_Timer_Seconds_timer, units ); |
---|
286 | break; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|