etisserant@38: /*
etisserant@38: This file is part of CanFestival, a library implementing CanOpen Stack. 
etisserant@38: 
etisserant@38: Copyright (C): Edouard TISSERANT and Francis DUPIN
etisserant@38: 
etisserant@38: See COPYING file for copyrights details.
etisserant@38: 
etisserant@38: This library is free software; you can redistribute it and/or
etisserant@38: modify it under the terms of the GNU Lesser General Public
etisserant@38: License as published by the Free Software Foundation; either
etisserant@38: version 2.1 of the License, or (at your option) any later version.
etisserant@38: 
etisserant@38: This library is distributed in the hope that it will be useful,
etisserant@38: but WITHOUT ANY WARRANTY; without even the implied warranty of
etisserant@38: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
etisserant@38: Lesser General Public License for more details.
etisserant@38: 
etisserant@38: You should have received a copy of the GNU Lesser General Public
etisserant@38: License along with this library; if not, write to the Free Software
etisserant@38: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
etisserant@38: */
nico@208: /*!
nico@208: ** @file   timer.c
nico@208: ** @author Edouard TISSERANT and Francis DUPIN
nico@208: ** @date   Tue Jun  5 09:32:32 2007
nico@208: **
nico@208: ** @brief
nico@208: **
nico@208: **
nico@208: */
etisserant@38: 
frdupin@71: /* #define DEBUG_WAR_CONSOLE_ON */
frdupin@71: /* #define DEBUG_ERR_CONSOLE_ON */
etisserant@38: 
etisserant@38: #include <applicfg.h>
etisserant@38: #include "timer.h"
etisserant@38: 
frdupin@71: /*  ---------  The timer table --------- */
etisserant@38: s_timer_entry timers[MAX_NB_TIMER] = {{TIMER_FREE, NULL, NULL, 0, 0, 0},};
frdupin@71: 
etisserant@38: TIMEVAL total_sleep_time = TIMEVAL_MAX;
etisserant@38: TIMER_HANDLE last_timer_raw = -1;
etisserant@38: 
etisserant@167: #define min_val(a,b) ((a<b)?a:b)
etisserant@38: 
nico@208: /*!                                                                                                
nico@208: ** -------  Use this to declare a new alarm ------                                                                                                
nico@208: **                                                                                                 
nico@208: ** @param d                                                                                        
nico@208: ** @param id                                                                                       
nico@208: ** @param callback                                                                                 
nico@208: ** @param value                                                                                    
nico@208: ** @param period                                                                                   
nico@208: **                                                                                                 
nico@208: ** @return                                                                                         
nico@208: **/   
etisserant@38: TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period)
etisserant@38: {
frdupin@71: 	/*printf("SetAlarm(UNS32 id=%d, TimerCallback_t callback=%x, TIMEVAL value=%d, TIMEVAL period=%d)\n", id, callback, value, period); */
etisserant@38: 	TIMER_HANDLE i;
etisserant@38: 	TIMER_HANDLE row_number = TIMER_NONE;
etisserant@38: 
nico@215: 	/* in order to decide new timer setting we have to run over all timer rows */
etisserant@38: 	for(i=0; i <= last_timer_raw + 1 && i < MAX_NB_TIMER; i++)
etisserant@38: 	{
etisserant@38: 		s_timer_entry *row = (timers+i);
etisserant@38: 
nico@215: 		if (callback && 	/* if something to store */
nico@215: 		   row->state == TIMER_FREE) /* and empty row */
nico@215: 		{	/* just store */
etisserant@38: 			row->callback = callback;
etisserant@38: 			row->d = d;
etisserant@38: 			row->id = id;
etisserant@38: 			row->val = value;
etisserant@38: 			row->interval = period;
etisserant@38: 			row->state = TIMER_ARMED;
etisserant@38: 			row_number = i;
etisserant@38: 			break;
etisserant@38: 		}
etisserant@38: 	}
etisserant@38: 	
nico@215: 	if (row_number != TIMER_NONE) /* if successfull **/
etisserant@38: 	{
frdupin@71: 		TIMEVAL real_timer_value;
frdupin@71: 		TIMEVAL elapsed_time;
frdupin@71: 		
etisserant@38: 		if (row_number == last_timer_raw + 1) last_timer_raw++;
etisserant@38: 		
nico@215: 		/* set next wakeup alarm if new entry is sooner than others, or if it is alone */
etisserant@167: 		real_timer_value = min_val(value, TIMEVAL_MAX);
frdupin@71: 		elapsed_time = getElapsedTime();
etisserant@38: 
nico@215: 		/*printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time); */
etisserant@38: 		if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value)
etisserant@38: 		{
etisserant@38: 			total_sleep_time = elapsed_time + real_timer_value;
etisserant@38: 			setTimer(real_timer_value);
etisserant@38: 		}
nico@215: 		/*printf("SetAlarm() return %d\n", row_number); */
etisserant@38: 		return row_number;
etisserant@38: 	}
etisserant@38: 	return TIMER_NONE;
etisserant@38: }
etisserant@38: 
nico@208: /*!                                                                                                
nico@208: **  -----  Use this to remove an alarm ----                                                                                             
nico@208: **                                                                                                 
nico@208: ** @param handle                                                                                   
nico@208: **                                                                                                 
nico@208: ** @return                                                                                         
nico@208: **/  
etisserant@38: TIMER_HANDLE DelAlarm(TIMER_HANDLE handle)
etisserant@38: {
nico@215: 	/* Quick and dirty. system timer will continue to be trigged, but no action will be preformed. */
etisserant@38: 	MSG_WAR(0x3320, "DelAlarm. handle = ", handle);
etisserant@38: 	if(handle != TIMER_NONE)
etisserant@38: 	{
etisserant@38: 		if(handle == last_timer_raw) 
etisserant@38: 			last_timer_raw--;
etisserant@38: 		timers[handle].state = TIMER_FREE; 		
etisserant@38: 	}
etisserant@38: 	else {
etisserant@38: 	}
etisserant@38: 	return TIMER_NONE;
etisserant@38: }
etisserant@38: 
nico@208: /*!                                                                                                
nico@208: ** ------  TimeDispatch is called on each timer expiration ----                                                                                                
nico@208: **                                                                                                 
nico@208: **/  
etisserant@38: void TimeDispatch()
etisserant@38: {
etisserant@38: 	TIMER_HANDLE i;
nico@215: 	TIMEVAL next_wakeup = TIMEVAL_MAX; /* used to compute when should normaly occur next wakeup */
nico@215: 	/* First run : change timer state depending on time */
nico@215: 	/* Get time since timer signal */
etisserant@38: 	TIMEVAL overrun = getElapsedTime();
etisserant@38: 	
etisserant@38: 	TIMEVAL real_total_sleep_time = total_sleep_time + overrun;
frdupin@71: 	/*printf("total_sleep_time %d + overrun %d\n", total_sleep_time , overrun); */
etisserant@38: 
etisserant@38: 	for(i=0; i <= last_timer_raw; i++)
etisserant@38: 	{
etisserant@38: 		s_timer_entry *row = (timers+i);
etisserant@38: 
nico@215: 		if (row->state & TIMER_ARMED) /* if row is active */
etisserant@38: 		{
nico@215: 			if (row->val <= real_total_sleep_time) /* to be trigged */
etisserant@38: 			{
frdupin@71: 				/*printf("row->val(%d) <= (%d)real_total_sleep_time\n", row->val, real_total_sleep_time); */
nico@215: 				if (!row->interval) /* if simply outdated */
etisserant@38: 				{
nico@215: 					row->state = TIMER_TRIG; /* ask for trig */
etisserant@38: 				}
nico@215: 				else /* or period have expired */
etisserant@38: 				{
nico@215: 					/* set val as interval, with overrun correction */
etisserant@38: 					row->val = row->interval - (overrun % row->interval);
frdupin@71: 					row->state = TIMER_TRIG_PERIOD; /* ask for trig, periodic */
nico@215: 					/* Check if this new timer value is the soonest */
etisserant@167: 					next_wakeup = min_val(row->val,next_wakeup);
etisserant@38: 				}
etisserant@38: 			}
etisserant@38: 			else
etisserant@38: 			{
nico@215: 				/* Each armed timer value in decremented. */
etisserant@38: 				row->val -= real_total_sleep_time;
etisserant@38: 
nico@215: 				/* Check if this new timer value is the soonest */
etisserant@167: 				next_wakeup = min_val(row->val,next_wakeup);
etisserant@38: 			}
etisserant@38: 		}
etisserant@38: 	}
etisserant@38: 	
nico@215: 	/* Remember how much time we should sleep. */
etisserant@38: 	total_sleep_time = next_wakeup;
etisserant@38: 
nico@215: 	/* Set timer to soonest occurence */
etisserant@38: 	setTimer(next_wakeup);
etisserant@38: 
nico@215: 	/* Then trig them or not. */
etisserant@38: 	for(i=0; i<=last_timer_raw; i++)
etisserant@38: 	{
etisserant@38: 		s_timer_entry *row = (timers+i);
etisserant@38: 
etisserant@38: 		if (row->state & TIMER_TRIG)
etisserant@38: 		{
nico@215: 			row->state &= ~TIMER_TRIG; /* reset trig state (will be free if not periodic) */
etisserant@149: 			if(row->callback)
nico@215: 				(*row->callback)(row->d, row->id); /* trig ! */
etisserant@38: 		}
etisserant@38: 	}
etisserant@38: }
etisserant@38: