/*************************************************************************** * Copyright (C) 11/2014 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License, * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include "event.h" #include "target.h" #include "timer.h" /* *********************************************************************** */ static uint16_t timers[TIMER_COUNT]; volatile static uint8_t timer_ticked; /* *********************************************************************** */ ISR(TIMER_TIM_VECT) { /* 1ms interrupt */ TIMER_TIM_RELOAD(TIMER_RELOAD); timer_ticked = 1; } /* TIM1_OVF_vect */ uint8_t timer_check(uint8_t timer_needed) { if (timer_ticked) { uint8_t i; timer_ticked = 0; for (i = 0; i < TIMER_COUNT; i++) { if (timers[i] > 0) { timers[i]--; if (timers[i] == 0) { event_queue(EVENT_TYPE_TIMER_ELAPSED, i, 0); } else { timer_needed = 1; } } } /* stop timer */ if (timer_needed == 0) { TIMER_TIM_DISABLE(); } return 1; } return 0; } /* timer_check */ uint8_t timer_get_sleep_mode(void) { if (TIMER_TIM_RUNNING()) { return SLEEP_MODE_IDLE; } else { return SLEEP_MODE_PWR_DOWN; } } /* timer_get_sleep_mode */ void timer_event_handler(struct event_entry *event) { if ((event->type == EVENT_TYPE_TIMER_SET) && (event->num < TIMER_COUNT) ) { timers[event->num] = event->value; /* start timer if needed */ if (!TIMER_TIM_RUNNING()) { TIMER_TIM_RELOAD(TIMER_RELOAD); TIMER_TIM_ENABLE(); } } } /* timer_event_handler */ void timer_init(void) { TIMER_TIM_INIT(); TIMER_TIM_RELOAD(TIMER_RELOAD); TIMER_TIM_ENABLE(); } /* timer_init */