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: */ etisserant@38: frdupin@71: /* #define DEBUG_WAR_CONSOLE_ON */ frdupin@71: /* #define DEBUG_ERR_CONSOLE_ON */ etisserant@38: etisserant@38: #include 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@48: #define minval(a,b) ((astate == TIMER_FREE) /* and empty row */ frdupin@71: { /* 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: frdupin@71: 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: frdupin@71: /* set next wakeup alarm if new entry is sooner than others, or if it is alone */ frdupin@71: real_timer_value = minval(value, TIMEVAL_MAX); frdupin@71: elapsed_time = getElapsedTime(); etisserant@38: frdupin@71: /*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: } frdupin@71: /*printf("SetAlarm() return %d\n", row_number); */ etisserant@38: return row_number; etisserant@38: } etisserant@38: return TIMER_NONE; etisserant@38: } etisserant@38: frdupin@71: /* --------- Use this to remove an alarm --------- */ etisserant@38: TIMER_HANDLE DelAlarm(TIMER_HANDLE handle) etisserant@38: { frdupin@71: /* 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: etisserant@38: frdupin@71: /* --------- TimeDispatch is called on each timer expiration --------- */ etisserant@38: void TimeDispatch() etisserant@38: { etisserant@38: TIMER_HANDLE i; frdupin@71: TIMEVAL next_wakeup = TIMEVAL_MAX; /* used to compute when should normaly occur next wakeup */ frdupin@71: /* First run : change timer state depending on time */ frdupin@71: /* 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: frdupin@71: if (row->state & TIMER_ARMED) /* if row is active */ etisserant@38: { frdupin@71: 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); */ frdupin@71: if (!row->interval) /* if simply outdated */ etisserant@38: { frdupin@71: row->state = TIMER_TRIG; /* ask for trig */ etisserant@38: } frdupin@71: else /* or period have expired */ etisserant@38: { frdupin@71: /* 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 */ frdupin@71: /* Check if this new timer value is the soonest */ etisserant@38: next_wakeup = minval(row->val,next_wakeup); etisserant@38: } etisserant@38: } etisserant@38: else etisserant@38: { frdupin@71: /* Each armed timer value in decremented. */ etisserant@38: row->val -= real_total_sleep_time; etisserant@38: frdupin@71: /* Check if this new timer value is the soonest */ etisserant@38: next_wakeup = minval(row->val,next_wakeup); etisserant@38: } etisserant@38: } etisserant@38: } etisserant@38: frdupin@71: /* Remember how much time we should sleep. */ etisserant@38: total_sleep_time = next_wakeup; etisserant@38: frdupin@71: /* Set timer to soonest occurence */ etisserant@38: setTimer(next_wakeup); etisserant@38: frdupin@71: /* 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: { frdupin@71: row->state &= ~TIMER_TRIG; /* reset trig state (will be free if not periodic) */ frdupin@71: (*row->callback)(row->d, row->id); /* trig ! */ etisserant@38: } etisserant@38: } etisserant@38: } etisserant@38: