]> git.zerfleddert.de Git - fnordlicht-mini/blame_incremental - firmware/fnordlicht-firmware/timer.c
very crude tool to fix eeprom checksum in binary
[fnordlicht-mini] / firmware / fnordlicht-firmware / timer.c
... / ...
CommitLineData
1/*
2 * unzap firmware
3 *
4 * (c) by Alexander Neumann <alexander@lochraster.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * For more information on the GPL, please go to:
20 * http://www.gnu.org/copyleft/gpl.html
21 */
22
23#include <stdint.h>
24#include <avr/interrupt.h>
25#include "../common/io.h"
26#include "timer.h"
27
28static volatile uint8_t internal_counter;
29
30void timer_init(void)
31{
32#if defined(__AVR_ATmega8__)
33 /* initialize timer2, CTC at 10ms, prescaler 1024 */
34 OCR2 = F_CPU/1024/100;
35 TCCR2 = _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20);
36 TIMSK |= _BV(OCIE2);
37#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
38 /* initialize timer2, CTC at 10ms, prescaler 1024 */
39 OCR2A = F_CPU/1024/100;
40 TCCR2A = _BV(WGM21);
41 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
42 TIMSK2 = _BV(OCIE2A);
43#else
44#error "unknown controller, unable to initialize timer2"
45#endif
46}
47
48void timer_set(timer_t *t, uint8_t timeout)
49{
50 t->current = internal_counter;
51 t->timeout = timeout;
52}
53
54bool timer_expired(timer_t *t)
55{
56 if (t->timeout == 0)
57 return true;
58
59 /* attention: this is not correct, if internal_counter is incremented by more than one
60 * between two calls of timer_expired()! */
61 if (t->current != internal_counter) {
62 t->timeout--;
63 t->current = internal_counter;
64 }
65
66 return false;
67}
68
69/* timer interrupt function */
70#if defined(__AVR_ATmega8__)
71ISR(TIMER2_COMP_vect, ISR_NOBLOCK) {
72 internal_counter++;
73}
74#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
75ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) {
76 internal_counter++;
77}
78#endif
Impressum, Datenschutz