-
Notifications
You must be signed in to change notification settings - Fork 57
/
raw_timer.c
598 lines (427 loc) · 14.6 KB
/
raw_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to jorya.txj@gmail.com
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2012-4 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
#if (CONFIG_RAW_TIMER > 0)
static void timer_list_init(void)
{
list_init(&timer_head);
}
static void timer_list_priority_insert(LIST *head, RAW_TIMER *timer_ptr)
{
RAW_TICK_TYPE val;
LIST *q, *list_start, *list_end;
RAW_TIMER *task_iter_temp;
list_start = list_end = head;
val = timer_ptr->remain;
for (q = list_start->next; q != list_end; q = q->next) {
task_iter_temp = raw_list_entry(q, RAW_TIMER, timer_list);
/*sorted by remain time*/
if ((task_iter_temp->match - raw_timer_count) > val) {
break;
}
}
list_insert(q, &timer_ptr->timer_list);
}
static void timer_list_remove(RAW_TIMER *timer_ptr)
{
LIST *tick_head_ptr;
tick_head_ptr = timer_ptr->to_head;
if (tick_head_ptr) {
list_delete(&timer_ptr->timer_list);
timer_ptr->to_head = 0;
}
}
/*
************************************************************************************************************************
* Create a soft timer
*
* Description: This function is called to create a timer
*
* Arguments :timer_ptr is the address of this timer object
* -----
* name_ptr is the name of this timer
* -----
* expiration_function is the call back function when time expired
* -----
* initial_ticksis the first time to call the expiration_function
* -----
* reschedule_ticks is the period time after firstcall the expiration_function
* -----
* auto_activate means whether this timer start automatically.
*
* Returns RAW_SUCCESS means timer create success.
*
* Note(s) :if reschedule_ticks equals 0, the timer call back function run once.
* if auto_activate equals 0, you have to call raw_timer_activate later.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_timer_create(RAW_TIMER *timer_ptr, RAW_U8 *name_ptr,
RAW_U16 (*expiration_function)(void *expiration_input), void *expiration_input,
RAW_TICK_TYPE initial_ticks, RAW_TICK_TYPE reschedule_ticks, RAW_U8 auto_activate)
{
#if (RAW_TIMER_FUNCTION_CHECK > 0)
if (timer_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (expiration_function == 0) {
return RAW_NULL_POINTER;
}
if (initial_ticks == 0) {
return RAW_TIMER_INVALID_TICKS;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
timer_ptr->name = name_ptr;
timer_ptr->raw_timeout_function = expiration_function;
timer_ptr->raw_timeout_param = expiration_input;
timer_ptr->init_count = initial_ticks;
timer_ptr->reschedule_ticks = reschedule_ticks;
timer_ptr->remain = 0u;
timer_ptr->match = 0u;
timer_ptr->timer_state = TIMER_DEACTIVE;
timer_ptr->to_head = 0;
list_init(&timer_ptr->timer_list);
timer_ptr->object_type = RAW_TIMER_OBJ_TYPE;
if (auto_activate) {
raw_timer_activate(timer_ptr, expiration_input);
}
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Activate the specified timer
*
* Description: This function is called to activate the specified timer
*
* Arguments :timer_ptr is the address of this timer object
* -----
*
*
*
* Returns RAW_TIMER_STATE_INVALID: means timer is already active or timer is deleted..
* RAW_SUCCESS: means timer activate success
* Note(s) :RAW_STATE_UNKNOWN wrong task state, probally sysytem error.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_timer_activate(RAW_TIMER *timer_ptr, void *expiration_input)
{
RAW_OS_ERROR mutex_ret;
#if (RAW_TIMER_FUNCTION_CHECK > 0)
if (timer_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
/*Timer state TIMER_ACTIVE TIMER_DELETED is not allowed to delete*/
if (timer_ptr->timer_state == TIMER_ACTIVE) {
return RAW_TIMER_STATE_INVALID;
}
if (timer_ptr->timer_state == TIMER_DELETED) {
return RAW_TIMER_STATE_INVALID;
}
timer_ptr->raw_timeout_param = expiration_input;
mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
RAW_ASSERT(mutex_ret == RAW_SUCCESS);
timer_ptr->match = raw_timer_count + timer_ptr->init_count;
/*Sort by remain time*/
timer_ptr->remain = timer_ptr->init_count;
/*Used by timer delete*/
timer_ptr->to_head = &timer_head;
timer_list_priority_insert(&timer_head, timer_ptr);
timer_ptr->timer_state = TIMER_ACTIVE;
raw_mutex_put(&timer_mutex);
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Change the specified timer parameter
*
* Description: This function is called to change the timer para specified by timer_ptr
*
* Arguments :timer_ptr is the address of this timer object
* -----
*
*
*
* Returns RAW_TIMER_STATE_INVALID: means timer state is not DEACTIVE or timer is deleted.
* RAW_SUCCESS: means timer change success.
* Note(s) :Youm must call raw_timer_deactivate before call this function
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_TIMER_CHANGE > 0)
RAW_OS_ERROR raw_timer_change(RAW_TIMER *timer_ptr, RAW_TICK_TYPE initial_ticks, RAW_TICK_TYPE reschedule_ticks)
{
RAW_OS_ERROR mutex_ret;
#if (RAW_TIMER_FUNCTION_CHECK > 0)
if (timer_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
/*Only timer state TIMER_DEACTIVE is allowed here*/
if (timer_ptr->timer_state != TIMER_DEACTIVE) {
return RAW_TIMER_STATE_INVALID;
}
if (timer_ptr->timer_state == TIMER_DELETED) {
return RAW_TIMER_STATE_INVALID;
}
mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
RAW_ASSERT(mutex_ret == RAW_SUCCESS);
timer_ptr->init_count = initial_ticks;
timer_ptr->reschedule_ticks = reschedule_ticks;
raw_mutex_put(&timer_mutex);
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Deactivate this timer
*
* Description: This function is called to Deactivate the timer by timer_ptr
*
* Arguments :timer_ptr is the address of this timer object
* -----
*
*
*
* Returns RAW_TIMER_STATE_INVALID: means timer state is already DEACTIVE or timer is deleted.
* RAW_SUCCESS: means timer change success.
* Note(s) :
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_TIMER_DEACTIVATE > 0)
RAW_OS_ERROR raw_timer_deactivate(RAW_TIMER *timer_ptr)
{
RAW_OS_ERROR mutex_ret;
#if (RAW_TIMER_FUNCTION_CHECK > 0)
if (timer_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
/*Timer state TIMER_DEACTIVE TIMER_DELETED is not allowed to delete*/
if (timer_ptr->timer_state == TIMER_DEACTIVE) {
return RAW_TIMER_STATE_INVALID;
}
if (timer_ptr->timer_state == TIMER_DELETED) {
return RAW_TIMER_STATE_INVALID;
}
mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
RAW_ASSERT(mutex_ret == RAW_SUCCESS);
timer_list_remove(timer_ptr);
timer_ptr->timer_state = TIMER_DEACTIVE;
raw_mutex_put(&timer_mutex);
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Delete this timer
*
* Description: This function is called to Deactivate the timer by timer_ptr
*
* Arguments :timer_ptr is the address of this timer object
* -----
*
*
*
* Returns RAW_TIMER_STATE_INVALID: means timer state is already deleted.
* RAW_SUCCESS: means timer delete success.
* Note(s) :timer object is reseted.
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_TIMER_DELETE > 0)
RAW_OS_ERROR raw_timer_delete(RAW_TIMER *timer_ptr)
{
RAW_OS_ERROR mutex_ret;
#if (RAW_TIMER_FUNCTION_CHECK > 0)
if (timer_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
if (timer_ptr->timer_state == TIMER_DELETED) {
return RAW_TIMER_STATE_INVALID;
}
mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
RAW_ASSERT(mutex_ret == RAW_SUCCESS);
timer_ptr->object_type = RAW_OBJ_TYPE_NONE;
timer_list_remove(timer_ptr);
timer_ptr->timer_state = TIMER_DELETED;
raw_mutex_put(&timer_mutex);
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Timer task
*
* Description: This function is called to start a timer task.
*
* Arguments :pa is the parameters to task.
* -----
*
*
*
* Returns
*
* Note(s) :This function is called by internal, users shoud not touch this function.
*
*
************************************************************************************************************************
*/
void timer_task(void *pa)
{
LIST *timer_head_ptr;
LIST *iter;
LIST *iter_temp;
RAW_TIMER *timer_ptr;
RAW_OS_ERROR mutex_ret;
RAW_U16 callback_ret;
/*reset the timer_sem count since it may not be 0 at this point, make it start here*/
raw_semaphore_set(&timer_sem, 0);
pa = pa;
while (1) {
/*timer task will be blocked after call this function*/
raw_semaphore_get(&timer_sem, RAW_WAIT_FOREVER);
mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
RAW_ASSERT(mutex_ret == RAW_SUCCESS);
/*calculate which timer_head*/
raw_timer_count++;
timer_head_ptr = &timer_head;
iter = timer_head_ptr->next;
while (RAW_TRUE) {
/*if timer exits*/
if (iter != timer_head_ptr) {
/*Must use iter_temp because iter may be remove later.*/
iter_temp = iter->next;
timer_ptr = raw_list_entry(iter, RAW_TIMER, timer_list);
/*if timeout*/
if (raw_timer_count == timer_ptr->match) {
/*remove from timer list*/
timer_list_remove(timer_ptr);
/*if timer is reschedulable*/
if (timer_ptr->reschedule_ticks) {
/*Sort by remain time*/
timer_ptr->remain = timer_ptr->reschedule_ticks;
timer_ptr->match = raw_timer_count + timer_ptr->remain;
timer_ptr->to_head = &timer_head;
timer_list_priority_insert(&timer_head, timer_ptr);
}
else {
timer_ptr->timer_state = TIMER_DEACTIVE;
}
/*Any way both condition need to call registered timer function*/
/*the registered timer function should not touch any timer related API,otherwise system will be crashed*/
if (timer_ptr->raw_timeout_function) {
callback_ret = timer_ptr->raw_timeout_function(timer_ptr->raw_timeout_param);
if ((callback_ret == TIMER_CALLBACK_STOP) && (timer_ptr->timer_state != TIMER_DEACTIVE)) {
/*remove from timer list*/
timer_list_remove(timer_ptr);
timer_ptr->timer_state = TIMER_DEACTIVE;
}
}
iter = iter_temp;
}
else {
break;
}
}
/*exit because timer is not exit*/
else {
break;
}
}
raw_mutex_put(&timer_mutex);
}
}
/*
************************************************************************************************************************
* Called by systen timer interrupt
*
* Description: This function is called to start a timer task.
*
* Arguments :None
*
*
*
*
* Returns
*
* Note(s) :This function is called by internal, users shoud not touch this function.
*
*
************************************************************************************************************************
*/
void call_timer_task(void)
{
raw_timer_ctrl--;
if (raw_timer_ctrl == 0u) {
/*reload timer frequency ctrl.*/
raw_timer_ctrl = RAW_TIMER_RATE;
/*Release a semphore to timer task*/
raw_semaphore_put(&timer_sem);
}
}
void raw_timer_init(void)
{
raw_timer_ctrl = RAW_TIMER_RATE;
timer_list_init();
/*Create a timer task to handle soft timer*/
raw_task_create(&raw_timer_obj, (RAW_U8 *)"timer_object", 0,
TIMER_TASK_PRIORITY, 0, timer_task_stack,
TIMER_STACK_SIZE, timer_task, 1);
/*create a semaphore for timer task*/
raw_semaphore_create(&timer_sem, (RAW_U8 *)"timer_sem", 0);
}
#endif