2015-04-10 00:32:04 +08:00
|
|
|
/*
|
|
|
|
Copyright 2011 Jun Wako <wakojun@gmail.com>
|
|
|
|
|
|
|
|
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, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
2016-11-28 14:41:22 +08:00
|
|
|
#include <util/atomic.h>
|
2015-04-10 00:32:04 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "timer_avr.h"
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
// counter resolution 1ms
|
|
|
|
// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
|
2016-11-28 14:41:22 +08:00
|
|
|
volatile uint32_t timer_count;
|
2015-04-10 00:32:04 +08:00
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer initialization
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
void timer_init(void) {
|
2015-04-10 00:32:04 +08:00
|
|
|
#if TIMER_PRESCALER == 1
|
2019-09-07 23:12:46 +08:00
|
|
|
uint8_t prescaler = _BV(CS00);
|
2015-04-10 00:32:04 +08:00
|
|
|
#elif TIMER_PRESCALER == 8
|
2019-09-07 23:12:46 +08:00
|
|
|
uint8_t prescaler = _BV(CS01);
|
2015-04-10 00:32:04 +08:00
|
|
|
#elif TIMER_PRESCALER == 64
|
2019-09-07 23:12:46 +08:00
|
|
|
uint8_t prescaler = _BV(CS00) | _BV(CS01);
|
2015-04-10 00:32:04 +08:00
|
|
|
#elif TIMER_PRESCALER == 256
|
2019-09-07 23:12:46 +08:00
|
|
|
uint8_t prescaler = _BV(CS02);
|
2015-04-10 00:32:04 +08:00
|
|
|
#elif TIMER_PRESCALER == 1024
|
2019-09-07 23:12:46 +08:00
|
|
|
uint8_t prescaler = _BV(CS00) | _BV(CS02);
|
2015-04-10 00:32:04 +08:00
|
|
|
#else
|
2019-09-07 23:12:46 +08:00
|
|
|
# error "Timer prescaler value is not valid"
|
2015-04-10 00:32:04 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-03 05:23:57 +08:00
|
|
|
#if defined(__AVR_ATmega32A__)
|
|
|
|
// Timer0 CTC mode
|
|
|
|
TCCR0 = _BV(WGM01) | prescaler;
|
|
|
|
|
|
|
|
OCR0 = TIMER_RAW_TOP;
|
|
|
|
TIMSK = _BV(OCIE0);
|
|
|
|
#elif defined(__AVR_ATtiny85__)
|
2017-01-22 01:30:06 +08:00
|
|
|
// Timer0 CTC mode
|
2019-09-07 23:12:46 +08:00
|
|
|
TCCR0A = _BV(WGM01);
|
2017-01-22 01:30:06 +08:00
|
|
|
TCCR0B = prescaler;
|
|
|
|
|
2020-04-03 05:23:57 +08:00
|
|
|
OCR0A = TIMER_RAW_TOP;
|
|
|
|
TIMSK = _BV(OCIE0A);
|
2017-01-22 01:30:06 +08:00
|
|
|
#else
|
|
|
|
// Timer0 CTC mode
|
2020-04-03 05:23:57 +08:00
|
|
|
TCCR0A = _BV(WGM01);
|
|
|
|
TCCR0B = prescaler;
|
2017-01-22 01:30:06 +08:00
|
|
|
|
2020-04-03 05:23:57 +08:00
|
|
|
OCR0A = TIMER_RAW_TOP;
|
|
|
|
TIMSK0 = _BV(OCIE0A);
|
2017-01-22 01:30:06 +08:00
|
|
|
#endif
|
2015-04-10 00:32:04 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer clear
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
inline void timer_clear(void) {
|
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; }
|
2015-04-10 00:32:04 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer read
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
inline uint16_t timer_read(void) {
|
2015-04-10 00:32:04 +08:00
|
|
|
uint32_t t;
|
|
|
|
|
2019-08-31 02:19:03 +08:00
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
|
2015-04-10 00:32:04 +08:00
|
|
|
|
|
|
|
return (t & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer read32
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
inline uint32_t timer_read32(void) {
|
2015-04-10 00:32:04 +08:00
|
|
|
uint32_t t;
|
|
|
|
|
2019-08-31 02:19:03 +08:00
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
|
2015-04-10 00:32:04 +08:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer elapsed
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
inline uint16_t timer_elapsed(uint16_t last) {
|
2015-04-10 00:32:04 +08:00
|
|
|
uint32_t t;
|
|
|
|
|
2019-08-31 02:19:03 +08:00
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
|
2015-04-10 00:32:04 +08:00
|
|
|
|
|
|
|
return TIMER_DIFF_16((t & 0xFFFF), last);
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:50:38 +08:00
|
|
|
/** \brief timer elapsed32
|
|
|
|
*
|
|
|
|
* FIXME: needs doc
|
|
|
|
*/
|
2019-08-31 02:19:03 +08:00
|
|
|
inline uint32_t timer_elapsed32(uint32_t last) {
|
2015-04-10 00:32:04 +08:00
|
|
|
uint32_t t;
|
|
|
|
|
2019-08-31 02:19:03 +08:00
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
|
2015-04-10 00:32:04 +08:00
|
|
|
|
|
|
|
return TIMER_DIFF_32(t, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
// excecuted once per 1ms.(excess for just timer count?)
|
2017-01-22 01:30:06 +08:00
|
|
|
#ifndef __AVR_ATmega32A__
|
2019-08-31 02:19:03 +08:00
|
|
|
# define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
|
2017-01-22 01:30:06 +08:00
|
|
|
#else
|
2019-08-31 02:19:03 +08:00
|
|
|
# define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
|
2017-01-22 01:30:06 +08:00
|
|
|
#endif
|
2019-08-31 02:19:03 +08:00
|
|
|
ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; }
|