etisserant@38: /* greg@576: 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 Edouard@801: #include "timers.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) ((astate == TIMER_FREE) /* and empty row */ nico@215: { /* just store */ greg@522: TIMEVAL real_timer_value; greg@522: TIMEVAL elapsed_time; greg@576: greg@522: if (row_number == last_timer_raw + 1) last_timer_raw++; greg@576: greg@522: elapsed_time = getElapsedTime(); greg@522: /* set next wakeup alarm if new entry is sooner than others, or if it is alone */ greg@576: real_timer_value = value; greg@522: real_timer_value = min_val(real_timer_value, TIMEVAL_MAX); greg@576: greg@522: if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value) greg@522: { greg@522: total_sleep_time = elapsed_time + real_timer_value; greg@522: setTimer(real_timer_value); greg@522: } etisserant@38: row->callback = callback; etisserant@38: row->d = d; etisserant@38: row->id = id; greg@522: row->val = value + elapsed_time; etisserant@38: row->interval = period; etisserant@38: row->state = TIMER_ARMED; greg@522: return row_number; etisserant@38: } etisserant@38: } greg@576: etisserant@38: return TIMER_NONE; etisserant@38: } etisserant@38: greg@576: /*! greg@576: ** ----- Use this to remove an alarm ---- greg@576: ** greg@576: ** @param handle greg@576: ** greg@576: ** @return greg@576: **/ 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: { greg@576: if(handle == last_timer_raw) etisserant@38: last_timer_raw--; greg@576: timers[handle].state = TIMER_FREE; etisserant@38: } etisserant@38: return TIMER_NONE; etisserant@38: } etisserant@38: greg@576: /*! greg@576: ** ------ TimeDispatch is called on each timer expiration ---- greg@576: ** greg@522: **/ greg@576: int tdcount=0; etisserant@470: void TimeDispatch(void) 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 */ edouard@629: UNS32 overrun = (UNS32)getElapsedTime(); greg@576: etisserant@38: TIMEVAL real_total_sleep_time = total_sleep_time + overrun; etisserant@38: etisserant@470: s_timer_entry *row; etisserant@470: etisserant@470: for(i=0, row = timers; i <= last_timer_raw; i++, row++) 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: { 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: { edouard@629: /* set val as interval, with 32 bit overrun correction, */ edouard@629: /* modulo for 64 bit not available on all platforms */ edouard@629: row->val = row->interval - (overrun % (UNS32)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@470: if(row->val < next_wakeup) greg@576: next_wakeup = row->val; 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@470: if(row->val < next_wakeup) greg@576: next_wakeup = row->val; etisserant@38: } etisserant@38: } etisserant@38: } greg@576: 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@470: for(i=0, row = timers; i<=last_timer_raw; i++, row++) 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: }