FreeCalypso > hg > freecalypso-sw
comparison gsm-fw/gpf/osl/contrib/os_tim_ir.c @ 476:536c53d3356c
gsm-fw/gpf/osl/contrib/os_tim_ir.c: version contributed by Das Signal
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Fri, 27 Jun 2014 03:57:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
475:dcb543907cbb | 476:536c53d3356c |
---|---|
1 /* | |
2 * This C module is a reconstruction based on the disassembly of | |
3 * os_tim.obj in frame_na7_db_ir.lib from the Leonardo package. | |
4 */ | |
5 | |
6 /* set of included headers from COFF symtab: */ | |
7 #include <stdio.h> | |
8 #include "gpfconf.h" /* FreeCalypso addition */ | |
9 #include "../../nucleus/nucleus.h" | |
10 #include "typedefs.h" | |
11 #include "os.h" | |
12 #include "gdi.h" | |
13 #include "os_types.h" | |
14 #include "os_glob.h" | |
15 | |
16 extern unsigned os_time_to_tick_multiplier; | |
17 extern unsigned os_tick_to_time_multiplier; | |
18 | |
19 extern T_OS_TIMER_ENTRY TimerTable[]; | |
20 extern T_OS_TIMER_TABLE_ENTRY *p_list[]; | |
21 | |
22 extern int volatile t_list_access; | |
23 extern unsigned t_start_ticks; | |
24 extern T_OS_TIMER_TABLE_ENTRY * volatile t_running; | |
25 extern int used_timers; | |
26 extern int next_t_handle; | |
27 extern NU_SEMAPHORE TimSemCB; | |
28 extern NU_TIMER os_timer_cb; | |
29 extern int max_used_timers; | |
30 | |
31 void os_Timeout(UNSIGNED t_handle); | |
32 | |
33 void timer_error(int err) | |
34 { | |
35 err = 0; | |
36 } | |
37 | |
38 static int os_remove_timer_from_list(T_OS_TIMER_TABLE_ENTRY *timer) | |
39 { | |
40 OS_TICK c_ticks; | |
41 | |
42 if (timer != t_running) { | |
43 if (timer->next != t_running) | |
44 timer->next->r_ticks += timer->r_ticks; | |
45 } | |
46 else { | |
47 c_ticks = NU_Retrieve_Clock(); | |
48 if (timer->next == timer) { | |
49 t_running = 0; | |
50 } else { | |
51 timer->next->r_ticks = | |
52 t_start_ticks + timer->r_ticks + | |
53 timer->next->r_ticks - c_ticks; | |
54 t_running = timer->next; | |
55 } | |
56 NU_Control_Timer(&os_timer_cb, NU_DISABLE_TIMER); | |
57 if (t_running != NULL) { | |
58 t_start_ticks = c_ticks; | |
59 if (t_running->r_ticks != 0) | |
60 NU_Reset_Timer(&os_timer_cb, os_Timeout, | |
61 t_running->r_ticks, 0, | |
62 NU_ENABLE_TIMER); | |
63 } | |
64 } | |
65 if (timer->next != timer) { | |
66 timer->prev->next = timer->next; | |
67 timer->next->prev = timer->prev; | |
68 } | |
69 timer->next = NULL; | |
70 timer->prev = NULL; | |
71 timer->status = 1; | |
72 return 1; | |
73 } | |
74 | |
75 static unsigned os_add_timer_to_list(T_OS_TIMER_TABLE_ENTRY *timer, OS_TICK ticks) | |
76 { | |
77 T_OS_TIMER_TABLE_ENTRY *t_list; | |
78 OS_TICK c_ticks, r1_ticks, return_ticks; | |
79 | |
80 if (ticks == 0) | |
81 ticks = 1; | |
82 | |
83 c_ticks = NU_Retrieve_Clock(); | |
84 t_list = t_running; | |
85 if (t_list != NULL) { | |
86 if (t_running->r_ticks >= c_ticks - t_start_ticks) { | |
87 r1_ticks = | |
88 t_running->r_ticks - c_ticks + t_start_ticks; | |
89 t_running->r_ticks = r1_ticks; | |
90 } else { | |
91 r1_ticks = 0; | |
92 t_running->r_ticks = 0; | |
93 } | |
94 t_start_ticks = c_ticks; | |
95 return_ticks = 0; | |
96 while (ticks >= r1_ticks) { | |
97 ticks -= r1_ticks; | |
98 t_list = t_list->next; | |
99 if (t_list == t_running) | |
100 goto out; | |
101 r1_ticks = t_list->r_ticks; | |
102 } | |
103 t_list->r_ticks -= ticks; | |
104 if (t_list == t_running) { | |
105 t_running = timer; | |
106 t_start_ticks = c_ticks; | |
107 NU_Control_Timer(&os_timer_cb, NU_DISABLE_TIMER); | |
108 return_ticks = ticks; | |
109 } | |
110 out: | |
111 timer->next = t_list; | |
112 timer->prev = t_list->prev; | |
113 t_list->prev->next = timer; | |
114 t_list->prev = timer; | |
115 timer->r_ticks = ticks; | |
116 } else { | |
117 timer->next = timer; | |
118 timer->prev = timer; | |
119 timer->r_ticks = ticks; | |
120 t_start_ticks = c_ticks; | |
121 t_running = timer; | |
122 return_ticks = ticks; | |
123 } | |
124 timer->status = 2; | |
125 return return_ticks; | |
126 } | |
127 | |
128 GLOBAL LONG os_StartTimer(OS_HANDLE TaskHandle, OS_HANDLE TimerHandle, | |
129 USHORT Index, OS_TIME InitialTime, | |
130 OS_TIME RescheduleTime) | |
131 { | |
132 T_OS_TIMER_TABLE_ENTRY *timer; | |
133 OS_TICK ticks; | |
134 USHORT status; | |
135 STATUS sts; | |
136 | |
137 t_list_access = 1; | |
138 timer = &TimerTable[TimerHandle].entry; | |
139 if (TimerHandle > MaxSimultaneousTimer || timer->status == 0) { | |
140 t_list_access = 0; | |
141 return OS_ERROR; | |
142 } | |
143 | |
144 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND); | |
145 /* FIXME: not sure about this | |
146 if (sts != NU_SUCCESS) | |
147 os_MyHandle(sts); | |
148 */ | |
149 status = timer->status; | |
150 if (status == 2) | |
151 status = os_remove_timer_from_list(timer); | |
152 timer->t_handle = TimerHandle; | |
153 timer->task_handle = os_MyHandle(); | |
154 timer->entity_handle = TaskHandle; | |
155 timer->t_index = Index; | |
156 timer->p_ticks = TIME_TO_SYSTEM_TICKS(RescheduleTime); | |
157 ticks = os_add_timer_to_list(timer, TIME_TO_SYSTEM_TICKS(InitialTime)); | |
158 if (ticks) | |
159 NU_Reset_Timer(&os_timer_cb, os_Timeout, ticks, 0, NU_ENABLE_TIMER); | |
160 if (sts == NU_SUCCESS) | |
161 NU_Release_Semaphore(&TimSemCB); | |
162 t_list_access = 0; | |
163 return OS_OK; | |
164 } | |
165 | |
166 /* FIXME: t_handle is unused?! */ | |
167 void os_Timeout(UNSIGNED t_handle) | |
168 { | |
169 UNSIGNED s_ticks; | |
170 OS_HANDLE task_handle; | |
171 OS_HANDLE e_handle; | |
172 int t_index, i, done; | |
173 T_OS_TIMER_TABLE_ENTRY **t_r4; | |
174 T_OS_TIMER_TABLE_ENTRY *timer; | |
175 void (*timeout_func) (OS_HANDLE, OS_HANDLE, USHORT); | |
176 OS_TIME InitialTime; | |
177 | |
178 if (t_list_access) { | |
179 t_start_ticks++; | |
180 NU_Reset_Timer(&os_timer_cb, os_Timeout, 1, 0, | |
181 NU_ENABLE_TIMER); | |
182 return; | |
183 } | |
184 | |
185 t_list_access = 1; | |
186 timer = t_running; | |
187 if (t_running) { | |
188 s_ticks = 0; | |
189 done = 0; | |
190 i = 0; | |
191 do { | |
192 timeout_func = timer->TimeoutProc; | |
193 if (timer->p_ticks) | |
194 p_list[i++] = timer; | |
195 task_handle = timer->task_handle; | |
196 e_handle = timer->entity_handle; | |
197 t_index = timer->t_index; | |
198 timer->status = 1; | |
199 if (timer->next == timer) { | |
200 t_running = NULL; | |
201 done = 1; | |
202 } else { | |
203 timer->prev->next = timer->next; | |
204 timer->next->prev = timer->prev; | |
205 if (timer->next->r_ticks) { | |
206 t_running = timer->next; | |
207 s_ticks = timer->r_ticks; | |
208 done = 1; | |
209 } else | |
210 timer = timer->next; | |
211 } | |
212 /* FIXME: IND$CALL() ? */ | |
213 } | |
214 while (!done); | |
215 | |
216 if (s_ticks) { | |
217 t_start_ticks = NU_Retrieve_Clock(); | |
218 NU_Reset_Timer(&os_timer_cb, os_Timeout, s_ticks, 0, | |
219 NU_ENABLE_TIMER); | |
220 } | |
221 } | |
222 for (t_r4 = p_list; *t_r4; t_r4++) { | |
223 timer = *t_r4; | |
224 InitialTime = SYSTEM_TICKS_TO_TIME(timer->p_ticks); | |
225 os_StartTimer(timer->entity_handle, timer->t_handle, | |
226 timer->t_index, InitialTime, InitialTime); | |
227 *t_r4 = NULL; | |
228 } | |
229 | |
230 t_list_access = 0; | |
231 return; | |
232 } | |
233 | |
234 /* FIXME: TaskHandle is unused?! */ | |
235 GLOBAL LONG os_StopTimer(OS_HANDLE TaskHandle, OS_HANDLE TimerHandle) | |
236 { | |
237 T_OS_TIMER_ENTRY *timer_e; | |
238 STATUS sts; | |
239 | |
240 t_list_access = 1; | |
241 timer_e = &TimerTable[TimerHandle]; | |
242 if (TimerHandle > MaxSimultaneousTimer || timer_e->entry.status == 0) { | |
243 t_list_access = 0; | |
244 return OS_ERROR; | |
245 } | |
246 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND); | |
247 if (timer_e->entry.status == 2) | |
248 os_remove_timer_from_list(&timer_e->entry); | |
249 if (sts == NU_SUCCESS) | |
250 NU_Release_Semaphore(&TimSemCB); | |
251 t_list_access = 0; | |
252 return OS_OK; | |
253 } | |
254 | |
255 GLOBAL LONG os_IncrementTick(OS_TICK ticks) | |
256 { | |
257 return OS_OK; | |
258 } | |
259 | |
260 /* FIXME: TaskHandle is unused?! */ | |
261 GLOBAL LONG os_DestroyTimer(OS_HANDLE TaskHandle, OS_HANDLE TimerHandle) | |
262 { | |
263 STATUS sts; | |
264 T_OS_TIMER_ENTRY *timer_e; | |
265 | |
266 t_list_access = 1; | |
267 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND); | |
268 timer_e = &TimerTable[TimerHandle]; | |
269 if (TimerHandle > MaxSimultaneousTimer || timer_e->entry.status == 0) { | |
270 if (sts == NU_SUCCESS) | |
271 NU_Release_Semaphore(&TimSemCB); | |
272 t_list_access = 0; | |
273 return OS_ERROR; | |
274 } | |
275 timer_e->next_t_handle = next_t_handle; | |
276 timer_e->entry.status = 0; | |
277 used_timers--; | |
278 t_list_access = 0; | |
279 if (sts == NU_SUCCESS) | |
280 NU_Release_Semaphore(&TimSemCB); | |
281 return OS_OK; | |
282 } | |
283 | |
284 /* FIXME: TaskHandle and MemPoolHandle are unused?! */ | |
285 GLOBAL LONG os_CreateTimer(OS_HANDLE TaskHandle, | |
286 void (*TimeoutProc) (OS_HANDLE, OS_HANDLE, USHORT), | |
287 OS_HANDLE *TimerHandle, OS_HANDLE MemPoolHandle) | |
288 { | |
289 STATUS sts; | |
290 OS_HANDLE orig_next_t_handle; | |
291 T_OS_TIMER_ENTRY *timer_e; | |
292 | |
293 t_list_access = 1; | |
294 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND); | |
295 orig_next_t_handle = next_t_handle; | |
296 if (next_t_handle == 0) { /* INVALID_HANDLE */ | |
297 if (sts == NU_SUCCESS) | |
298 NU_Release_Semaphore(&TimSemCB); | |
299 t_list_access = 0; | |
300 return OS_ERROR; | |
301 } | |
302 | |
303 timer_e = &TimerTable[next_t_handle]; | |
304 timer_e->entry.status = 1; | |
305 timer_e->entry.TimeoutProc = TimeoutProc; | |
306 *TimerHandle = orig_next_t_handle; | |
307 next_t_handle = timer_e->next_t_handle; | |
308 used_timers++; | |
309 if (max_used_timers < used_timers) | |
310 max_used_timers = used_timers; | |
311 if (sts == NU_SUCCESS) | |
312 NU_Release_Semaphore(&TimSemCB); | |
313 t_list_access = 0; | |
314 return OS_OK; | |
315 } |