1 | /* |
---|
2 | * Watchdog 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/isr.h> |
---|
18 | #include <rtems/watchdog.h> |
---|
19 | |
---|
20 | /*PAGE |
---|
21 | * |
---|
22 | * _Watchdog_Handler_initialization |
---|
23 | * |
---|
24 | * This routine initializes the watchdog handler. |
---|
25 | * |
---|
26 | * Input parameters: NONE |
---|
27 | * |
---|
28 | * Output parameters: NONE |
---|
29 | */ |
---|
30 | |
---|
31 | void _Watchdog_Handler_initialization( void ) |
---|
32 | { |
---|
33 | _Watchdog_Clear_sync(); |
---|
34 | _Chain_Initialize_empty( &_Watchdog_Ticks_chain ); |
---|
35 | _Chain_Initialize_empty( &_Watchdog_Seconds_chain ); |
---|
36 | } |
---|
37 | |
---|
38 | /*PAGE |
---|
39 | * |
---|
40 | * _Watchdog_Remove |
---|
41 | * |
---|
42 | * The routine removes a watchdog from a delta chain and updates |
---|
43 | * the delta counters of the remaining watchdogs. |
---|
44 | */ |
---|
45 | |
---|
46 | Watchdog_States _Watchdog_Remove( |
---|
47 | Watchdog_Control *the_watchdog |
---|
48 | ) |
---|
49 | { |
---|
50 | ISR_Level level; |
---|
51 | Watchdog_States previous_state; |
---|
52 | Watchdog_Control *next_watchdog; |
---|
53 | |
---|
54 | _ISR_Disable( level ); |
---|
55 | previous_state = the_watchdog->state; |
---|
56 | switch ( previous_state ) { |
---|
57 | case WATCHDOG_INACTIVE: |
---|
58 | break; |
---|
59 | case WATCHDOG_ACTIVE: |
---|
60 | case WATCHDOG_REINSERT: |
---|
61 | case WATCHDOG_REMOVE_IT: |
---|
62 | |
---|
63 | the_watchdog->state = WATCHDOG_INACTIVE; |
---|
64 | next_watchdog = _Watchdog_Next( the_watchdog ); |
---|
65 | |
---|
66 | if ( _Watchdog_Next(next_watchdog) ) |
---|
67 | next_watchdog->delta_interval += the_watchdog->delta_interval; |
---|
68 | |
---|
69 | if ( the_watchdog == _Watchdog_Sync ) |
---|
70 | _Watchdog_Sync = _Watchdog_Previous( the_watchdog ); |
---|
71 | |
---|
72 | _Chain_Extract_unprotected( &the_watchdog->Node ); |
---|
73 | break; |
---|
74 | } |
---|
75 | _ISR_Enable( level ); |
---|
76 | return( previous_state ); |
---|
77 | } |
---|
78 | |
---|
79 | /*PAGE |
---|
80 | * |
---|
81 | * _Watchdog_Adjust |
---|
82 | * |
---|
83 | * This routine adjusts the delta chain backward or forward in response |
---|
84 | * to a time change. |
---|
85 | * |
---|
86 | * Input parameters: |
---|
87 | * header - pointer to the delta chain to be adjusted |
---|
88 | * direction - forward or backward adjustment to delta chain |
---|
89 | * units - units to adjust |
---|
90 | * |
---|
91 | * Output parameters: |
---|
92 | */ |
---|
93 | |
---|
94 | void _Watchdog_Adjust( |
---|
95 | Chain_Control *header, |
---|
96 | Watchdog_Adjust_directions direction, |
---|
97 | rtems_interval units |
---|
98 | ) |
---|
99 | { |
---|
100 | if ( !_Chain_Is_empty( header ) ) { |
---|
101 | switch ( direction ) { |
---|
102 | case WATCHDOG_BACKWARD: |
---|
103 | _Watchdog_First( header )->delta_interval += units; |
---|
104 | break; |
---|
105 | case WATCHDOG_FORWARD: |
---|
106 | while ( units ) { |
---|
107 | if ( units < _Watchdog_First( header )->delta_interval ) { |
---|
108 | _Watchdog_First( header )->delta_interval -= units; |
---|
109 | break; |
---|
110 | } else { |
---|
111 | units -= _Watchdog_First( header )->delta_interval; |
---|
112 | _Watchdog_First( header )->delta_interval = 1; |
---|
113 | _Watchdog_Tickle( header ); |
---|
114 | if ( _Chain_Is_empty( header ) ) |
---|
115 | break; |
---|
116 | } |
---|
117 | } |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | /*PAGE |
---|
124 | * |
---|
125 | * _Watchdog_Insert |
---|
126 | * |
---|
127 | * This routine inserts a watchdog timer on to the appropriate delta |
---|
128 | * chain while updating the delta interval counters. |
---|
129 | */ |
---|
130 | |
---|
131 | void _Watchdog_Insert( |
---|
132 | Chain_Control *header, |
---|
133 | Watchdog_Control *the_watchdog, |
---|
134 | Watchdog_Insert_modes insert_mode |
---|
135 | ) |
---|
136 | { |
---|
137 | ISR_Level level; |
---|
138 | Watchdog_Control *after; |
---|
139 | |
---|
140 | the_watchdog->state = WATCHDOG_REINSERT; |
---|
141 | the_watchdog->delta_interval = the_watchdog->initial; |
---|
142 | |
---|
143 | _ISR_Disable( level ); |
---|
144 | |
---|
145 | for ( after = _Watchdog_First( header ) ; |
---|
146 | ; |
---|
147 | after = _Watchdog_Next( _Watchdog_Get_sync() ) ) { |
---|
148 | |
---|
149 | if ( the_watchdog->delta_interval == 0 || !_Watchdog_Next( after ) ) |
---|
150 | break; |
---|
151 | |
---|
152 | if ( the_watchdog->delta_interval < after->delta_interval ) { |
---|
153 | after->delta_interval -= the_watchdog->delta_interval; |
---|
154 | break; |
---|
155 | } |
---|
156 | |
---|
157 | the_watchdog->delta_interval -= after->delta_interval; |
---|
158 | _Watchdog_Set_sync( after ); |
---|
159 | |
---|
160 | /* |
---|
161 | * If you experience problems comment out the _ISR_Flash line. Under |
---|
162 | * certain circumstances, this flash allows interrupts to execute |
---|
163 | * which violate the design assumptions. The critical section |
---|
164 | * mechanism used here must be redesigned to address this. |
---|
165 | */ |
---|
166 | |
---|
167 | _ISR_Flash( level ); |
---|
168 | } |
---|
169 | |
---|
170 | if ( insert_mode == WATCHDOG_ACTIVATE_NOW ) |
---|
171 | _Watchdog_Activate( the_watchdog ); |
---|
172 | |
---|
173 | _Chain_Insert_unprotected( after->Node.previous, &the_watchdog->Node ); |
---|
174 | |
---|
175 | _Watchdog_Clear_sync(); |
---|
176 | |
---|
177 | _ISR_Enable( level ); |
---|
178 | } |
---|
179 | |
---|
180 | /*PAGE |
---|
181 | * |
---|
182 | * _Watchdog_Tickle |
---|
183 | * |
---|
184 | * This routine decrements the delta counter in response to a tick. The |
---|
185 | * delta chain is updated accordingly. |
---|
186 | * |
---|
187 | * Input parameters: |
---|
188 | * header - pointer to the delta chain to be tickled |
---|
189 | * |
---|
190 | * Output parameters: NONE |
---|
191 | */ |
---|
192 | |
---|
193 | void _Watchdog_Tickle( |
---|
194 | Chain_Control *header |
---|
195 | ) |
---|
196 | { |
---|
197 | Watchdog_Control *the_watchdog; |
---|
198 | |
---|
199 | if ( _Chain_Is_empty( header ) ) |
---|
200 | return; |
---|
201 | |
---|
202 | the_watchdog = _Watchdog_First( header ); |
---|
203 | the_watchdog->delta_interval--; |
---|
204 | if ( the_watchdog->delta_interval != 0 ) |
---|
205 | return; |
---|
206 | |
---|
207 | do { |
---|
208 | switch( _Watchdog_Remove( the_watchdog ) ) { |
---|
209 | case WATCHDOG_ACTIVE: |
---|
210 | (*the_watchdog->routine)( |
---|
211 | the_watchdog->id, |
---|
212 | the_watchdog->user_data |
---|
213 | ); |
---|
214 | break; |
---|
215 | case WATCHDOG_REINSERT: |
---|
216 | _Watchdog_Insert( header, the_watchdog, WATCHDOG_ACTIVATE_NOW ); |
---|
217 | break; |
---|
218 | case WATCHDOG_INACTIVE: |
---|
219 | case WATCHDOG_REMOVE_IT: |
---|
220 | break; |
---|
221 | } |
---|
222 | the_watchdog = _Watchdog_First( header ); |
---|
223 | } while ( !_Chain_Is_empty( header ) && |
---|
224 | (the_watchdog->delta_interval == 0) ); |
---|
225 | } |
---|