diff options
Diffstat (limited to 'platforms/avr/timer.c')
| -rw-r--r-- | platforms/avr/timer.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/platforms/avr/timer.c b/platforms/avr/timer.c new file mode 100644 index 000000000..c2e6c6e08 --- /dev/null +++ b/platforms/avr/timer.c | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <avr/io.h> | ||
| 19 | #include <avr/interrupt.h> | ||
| 20 | #include <util/atomic.h> | ||
| 21 | #include <stdint.h> | ||
| 22 | #include "timer_avr.h" | ||
| 23 | #include "timer.h" | ||
| 24 | |||
| 25 | // counter resolution 1ms | ||
| 26 | // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} | ||
| 27 | volatile uint32_t timer_count; | ||
| 28 | |||
| 29 | /** \brief timer initialization | ||
| 30 | * | ||
| 31 | * FIXME: needs doc | ||
| 32 | */ | ||
| 33 | void timer_init(void) { | ||
| 34 | #if TIMER_PRESCALER == 1 | ||
| 35 | uint8_t prescaler = _BV(CS00); | ||
| 36 | #elif TIMER_PRESCALER == 8 | ||
| 37 | uint8_t prescaler = _BV(CS01); | ||
| 38 | #elif TIMER_PRESCALER == 64 | ||
| 39 | uint8_t prescaler = _BV(CS00) | _BV(CS01); | ||
| 40 | #elif TIMER_PRESCALER == 256 | ||
| 41 | uint8_t prescaler = _BV(CS02); | ||
| 42 | #elif TIMER_PRESCALER == 1024 | ||
| 43 | uint8_t prescaler = _BV(CS00) | _BV(CS02); | ||
| 44 | #else | ||
| 45 | # error "Timer prescaler value is not valid" | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #if defined(__AVR_ATmega32A__) | ||
| 49 | // Timer0 CTC mode | ||
| 50 | TCCR0 = _BV(WGM01) | prescaler; | ||
| 51 | |||
| 52 | OCR0 = TIMER_RAW_TOP; | ||
| 53 | TIMSK = _BV(OCIE0); | ||
| 54 | #elif defined(__AVR_ATtiny85__) | ||
| 55 | // Timer0 CTC mode | ||
| 56 | TCCR0A = _BV(WGM01); | ||
| 57 | TCCR0B = prescaler; | ||
| 58 | |||
| 59 | OCR0A = TIMER_RAW_TOP; | ||
| 60 | TIMSK = _BV(OCIE0A); | ||
| 61 | #else | ||
| 62 | // Timer0 CTC mode | ||
| 63 | TCCR0A = _BV(WGM01); | ||
| 64 | TCCR0B = prescaler; | ||
| 65 | |||
| 66 | OCR0A = TIMER_RAW_TOP; | ||
| 67 | TIMSK0 = _BV(OCIE0A); | ||
| 68 | #endif | ||
| 69 | } | ||
| 70 | |||
| 71 | /** \brief timer clear | ||
| 72 | * | ||
| 73 | * FIXME: needs doc | ||
| 74 | */ | ||
| 75 | inline void timer_clear(void) { | ||
| 76 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; } | ||
| 77 | } | ||
| 78 | |||
| 79 | /** \brief timer read | ||
| 80 | * | ||
| 81 | * FIXME: needs doc | ||
| 82 | */ | ||
| 83 | inline uint16_t timer_read(void) { | ||
| 84 | uint32_t t; | ||
| 85 | |||
| 86 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } | ||
| 87 | |||
| 88 | return (t & 0xFFFF); | ||
| 89 | } | ||
| 90 | |||
| 91 | /** \brief timer read32 | ||
| 92 | * | ||
| 93 | * FIXME: needs doc | ||
| 94 | */ | ||
| 95 | inline uint32_t timer_read32(void) { | ||
| 96 | uint32_t t; | ||
| 97 | |||
| 98 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } | ||
| 99 | |||
| 100 | return t; | ||
| 101 | } | ||
| 102 | |||
| 103 | /** \brief timer elapsed | ||
| 104 | * | ||
| 105 | * FIXME: needs doc | ||
| 106 | */ | ||
| 107 | inline uint16_t timer_elapsed(uint16_t last) { | ||
| 108 | uint32_t t; | ||
| 109 | |||
| 110 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } | ||
| 111 | |||
| 112 | return TIMER_DIFF_16((t & 0xFFFF), last); | ||
| 113 | } | ||
| 114 | |||
| 115 | /** \brief timer elapsed32 | ||
| 116 | * | ||
| 117 | * FIXME: needs doc | ||
| 118 | */ | ||
| 119 | inline uint32_t timer_elapsed32(uint32_t last) { | ||
| 120 | uint32_t t; | ||
| 121 | |||
| 122 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } | ||
| 123 | |||
| 124 | return TIMER_DIFF_32(t, last); | ||
| 125 | } | ||
| 126 | |||
| 127 | // excecuted once per 1ms.(excess for just timer count?) | ||
| 128 | #ifndef __AVR_ATmega32A__ | ||
| 129 | # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect | ||
| 130 | #else | ||
| 131 | # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect | ||
| 132 | #endif | ||
| 133 | ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; } | ||
