src/timer.c
changeset 167 b2f8b91d89b5
parent 149 fe50ada8020b
child 208 05d95c45b388
equal deleted inserted replaced
166:b6fbc1c59a44 167:b2f8b91d89b5
    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 minval(a,b) ((a<b)?a:b)
    35 #define min_val(a,b) ((a<b)?a:b)
    36 
    36 
    37 /* ---------  Use this to declare a new alarm --------- */
    37 /* ---------  Use this to declare a new alarm --------- */
    38 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)
    39 {
    39 {
    40 	/*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); */
    66 		TIMEVAL elapsed_time;
    66 		TIMEVAL elapsed_time;
    67 		
    67 		
    68 		if (row_number == last_timer_raw + 1) last_timer_raw++;
    68 		if (row_number == last_timer_raw + 1) last_timer_raw++;
    69 		
    69 		
    70 		/* set next wakeup alarm if new entry is sooner than others, or if it is alone */
    70 		/* set next wakeup alarm if new entry is sooner than others, or if it is alone */
    71 		real_timer_value = minval(value, TIMEVAL_MAX);
    71 		real_timer_value = min_val(value, TIMEVAL_MAX);
    72 		elapsed_time = getElapsedTime();
    72 		elapsed_time = getElapsedTime();
    73 
    73 
    74 		/*printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time); */
    74 		/*printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time); */
    75 		if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value)
    75 		if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value)
    76 		{
    76 		{
   129 				{
   129 				{
   130 					/* set val as interval, with overrun correction */
   130 					/* set val as interval, with overrun correction */
   131 					row->val = row->interval - (overrun % row->interval);
   131 					row->val = row->interval - (overrun % row->interval);
   132 					row->state = TIMER_TRIG_PERIOD; /* ask for trig, periodic */
   132 					row->state = TIMER_TRIG_PERIOD; /* ask for trig, periodic */
   133 					/* Check if this new timer value is the soonest */
   133 					/* Check if this new timer value is the soonest */
   134 					next_wakeup = minval(row->val,next_wakeup);
   134 					next_wakeup = min_val(row->val,next_wakeup);
   135 				}
   135 				}
   136 			}
   136 			}
   137 			else
   137 			else
   138 			{
   138 			{
   139 				/* Each armed timer value in decremented. */
   139 				/* Each armed timer value in decremented. */
   140 				row->val -= real_total_sleep_time;
   140 				row->val -= real_total_sleep_time;
   141 
   141 
   142 				/* Check if this new timer value is the soonest */
   142 				/* Check if this new timer value is the soonest */
   143 				next_wakeup = minval(row->val,next_wakeup);
   143 				next_wakeup = min_val(row->val,next_wakeup);
   144 			}
   144 			}
   145 		}
   145 		}
   146 	}
   146 	}
   147 	
   147 	
   148 	/* Remember how much time we should sleep. */
   148 	/* Remember how much time we should sleep. */