30 s_timer_entry timers[MAX_NB_TIMER] = {{TIMER_FREE, NULL, NULL, 0, 0, 0},}; |
30 s_timer_entry timers[MAX_NB_TIMER] = {{TIMER_FREE, NULL, NULL, 0, 0, 0},}; |
31 // |
31 // |
32 TIMEVAL total_sleep_time = TIMEVAL_MAX; |
32 TIMEVAL total_sleep_time = TIMEVAL_MAX; |
33 TIMER_HANDLE last_timer_raw = -1; |
33 TIMER_HANDLE last_timer_raw = -1; |
34 |
34 |
35 #define max(a,b) a>b?a:b |
35 #define minval(a,b) a<b?a:b |
36 #define min(a,b) a<b?a:b |
|
37 |
36 |
38 // --------- Use this to declare a new alarm --------- |
37 // --------- Use this to declare a new alarm --------- |
39 TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period) |
38 TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period) |
40 { |
39 { |
41 //printf("SetAlarm(UNS32 id=%d, TimerCallback_t callback=%x, TIMEVAL value=%d, TIMEVAL period=%d)\n", id, callback, value, period); |
40 //printf("SetAlarm(UNS32 id=%d, TimerCallback_t callback=%x, TIMEVAL value=%d, TIMEVAL period=%d)\n", id, callback, value, period); |
64 if (row_number != TIMER_NONE) // if successfull |
63 if (row_number != TIMER_NONE) // if successfull |
65 { |
64 { |
66 if (row_number == last_timer_raw + 1) last_timer_raw++; |
65 if (row_number == last_timer_raw + 1) last_timer_raw++; |
67 |
66 |
68 // set next wakeup alarm if new entry is sooner than others, or if it is alone |
67 // set next wakeup alarm if new entry is sooner than others, or if it is alone |
69 TIMEVAL real_timer_value = min(value, TIMEVAL_MAX); |
68 TIMEVAL real_timer_value = minval(value, TIMEVAL_MAX); |
70 TIMEVAL elapsed_time = getElapsedTime(); |
69 TIMEVAL elapsed_time = getElapsedTime(); |
71 |
70 |
72 //printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time); |
71 //printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time); |
73 if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value) |
72 if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value) |
74 { |
73 { |
127 { |
126 { |
128 // set val as interval, with overrun correction |
127 // set val as interval, with overrun correction |
129 row->val = row->interval - (overrun % row->interval); |
128 row->val = row->interval - (overrun % row->interval); |
130 row->state = TIMER_TRIG_PERIOD; // ask for trig, periodic |
129 row->state = TIMER_TRIG_PERIOD; // ask for trig, periodic |
131 // Check if this new timer value is the soonest |
130 // Check if this new timer value is the soonest |
132 next_wakeup = min(row->val,next_wakeup); |
131 next_wakeup = minval(row->val,next_wakeup); |
133 } |
132 } |
134 } |
133 } |
135 else |
134 else |
136 { |
135 { |
137 // Each armed timer value in decremented. |
136 // Each armed timer value in decremented. |
138 row->val -= real_total_sleep_time; |
137 row->val -= real_total_sleep_time; |
139 |
138 |
140 // Check if this new timer value is the soonest |
139 // Check if this new timer value is the soonest |
141 next_wakeup = min(row->val,next_wakeup); |
140 next_wakeup = minval(row->val,next_wakeup); |
142 } |
141 } |
143 } |
142 } |
144 } |
143 } |
145 |
144 |
146 // Remember how much time we should sleep. |
145 // Remember how much time we should sleep. |