etisserant@0: /* etisserant@0: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: etisserant@0: Author: Christian Fortin (canfestival@canopencanada.ca) etisserant@0: etisserant@0: See COPYING file for copyrights details. etisserant@0: etisserant@0: This library is free software; you can redistribute it and/or etisserant@0: modify it under the terms of the GNU Lesser General Public etisserant@0: License as published by the Free Software Foundation; either etisserant@0: version 2.1 of the License, or (at your option) any later version. etisserant@0: etisserant@0: This library is distributed in the hope that it will be useful, etisserant@0: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: Lesser General Public License for more details. etisserant@0: etisserant@0: You should have received a copy of the GNU Lesser General Public etisserant@0: License along with this library; if not, write to the Free Software etisserant@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: */ etisserant@0: etisserant@0: #include etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: #include "applicfg.h" etisserant@0: etisserant@0: #include etisserant@0: etisserant@0: etisserant@0: #define max(a,b) a>b?a:b etisserant@0: #define min(a,b) astate & TIMER_ARMED) // if row is active etisserant@0: { etisserant@0: if (row->val <= real_total_sleep_time) // to be trigged etisserant@0: { etisserant@0: if (!row->interval) // if simply outdated etisserant@0: { etisserant@0: row->state = TIMER_TRIG; // ask for trig etisserant@0: } etisserant@0: else // or period have expired etisserant@0: { etisserant@0: // set val as interval, with overrun correction etisserant@0: row->val = row->interval - (overrun % row->interval); etisserant@0: row->state = TIMER_TRIG_PERIOD; // ask for trig, periodic etisserant@0: // Check if this new timer value is the soonest etisserant@0: next_wakeup = min(row->val,next_wakeup); etisserant@0: } etisserant@0: } etisserant@0: else etisserant@0: { etisserant@0: // Each armed timer value in decremented. etisserant@0: row->val -= real_total_sleep_time; etisserant@0: etisserant@0: // Check if this new timer value is the soonest etisserant@0: next_wakeup = min(row->val,next_wakeup); etisserant@0: } etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: // Remember how much time we should sleep. etisserant@0: total_sleep_time = next_wakeup; etisserant@0: etisserant@0: // Set timer to soonest occurence etisserant@0: setTimer(next_wakeup); etisserant@0: etisserant@0: // Then trig them or not. etisserant@0: for(i=0; i<=last_timer_raw; i++) etisserant@0: { etisserant@0: s_timer_entry *row = (timers+i); etisserant@0: etisserant@0: if (row->state & TIMER_TRIG) etisserant@0: { etisserant@0: row->state &= ~TIMER_TRIG; // reset trig state (will be free if not periodic) etisserant@0: (*row->callback)(row->d, row->id); // trig ! etisserant@0: } etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period) etisserant@0: { etisserant@0: int i; etisserant@0: TIMER_HANDLE row_number = TIMER_NONE; etisserant@0: etisserant@0: if (callback == NULL) // nothing to store etisserant@0: return TIMER_NONE; etisserant@0: etisserant@0: // in order to decide new timer setting we have to run over all timer rows etisserant@0: etisserant@0: for(i=0; i <= last_timer_raw + 1 && i < MAX_NB_TIMER; i++) etisserant@0: { etisserant@0: s_timer_entry *row = (timers+i); etisserant@0: etisserant@0: if (row->state == TIMER_FREE) // an empty row etisserant@0: { // just store etisserant@0: row->callback = callback; etisserant@0: row->d = d; etisserant@0: row->id = id; etisserant@0: row->val = value; etisserant@0: row->interval = period; etisserant@0: row->state = TIMER_ARMED; etisserant@0: etisserant@0: row_number = i; etisserant@0: break; etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: if (row_number != TIMER_NONE) // if successfull etisserant@0: { etisserant@0: if (row_number == last_timer_raw + 1) last_timer_raw++; etisserant@0: etisserant@0: // set next wakeup alarm if new entry is sooner than others, or if it is alone etisserant@0: unsigned int real_timer_value = min(value, TIMEVAL_MAX); etisserant@0: unsigned int elapsed_time = getElapsedTime(); etisserant@0: etisserant@0: if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value) etisserant@0: { etisserant@0: total_sleep_time = elapsed_time + real_timer_value; etisserant@0: setTimer(real_timer_value); etisserant@0: } etisserant@0: etisserant@0: return row_number; etisserant@0: } etisserant@0: etisserant@0: return TIMER_NONE; etisserant@0: } etisserant@0: etisserant@0: // --------- Use this to remove an alarm --------- etisserant@0: TIMER_HANDLE DelAlarm(TIMER_HANDLE handle) etisserant@0: { etisserant@0: if (handle != TIMER_NONE) etisserant@0: { etisserant@0: if (handle == last_timer_raw) etisserant@0: last_timer_raw--; etisserant@0: timers[handle].state = TIMER_FREE; etisserant@0: } etisserant@0: else etisserant@0: { etisserant@0: } etisserant@0: etisserant@0: return TIMER_NONE; etisserant@0: }