[AVR] Fix timer usage for ATmega128

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9263 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
Fabio Utzig 2016-04-09 13:57:38 +00:00
parent aa91fa6576
commit d873e4ed7b
2 changed files with 52 additions and 17 deletions

View File

@ -62,20 +62,45 @@
/* Find the most suitable prescaler setting for the desired OSAL_ST_FREQUENCY */
#if ((F_CPU / OSAL_ST_FREQUENCY) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 1
#define AVR_TIMER_PRESCALER_BITS (0<<CS02)|(0<<CS01)|(1<<CS00); /* CLK */
#define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(0<<CS01)|(1<<CS00)) /* CLK */
#elif ((F_CPU / OSAL_ST_FREQUENCY / 8) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 8
#define AVR_TIMER_PRESCALER_BITS (0<<CS02)|(1<<CS01)|(0<<CS00); /* CLK/8 */
#define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(1<<CS01)|(0<<CS00)) /* CLK/8 */
#elif ((F_CPU / OSAL_ST_FREQUENCY / 64) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 64
#define AVR_TIMER_PRESCALER_BITS (0<<CS02)|(1<<CS01)|(1<<CS00); /* CLK/64 */
#ifdef __AVR_ATmega128__
#define AVR_TIMER_PRESCALER_BITS ((1<<CS02)|(0<<CS01)|(0<<CS00)) /* CLK/64 */
#else
#define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(1<<CS01)|(1<<CS00)) /* CLK/64 */
#endif
#elif ((F_CPU / OSAL_ST_FREQUENCY / 256) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 256
#define AVR_TIMER_PRESCALER_BITS (1<<CS02)|(0<<CS01)|(0<<CS00); /* CLK/256 */
#ifdef __AVR_ATmega128__
#define AVR_TIMER_PRESCALER_BITS ((1<<CS02)|(1<<CS01)|(0<<CS00)) /* CLK/256 */
#else
#define AVR_TIMER_PRESCALER_BITS ((1<<CS02)|(0<<CS01)|(0<<CS00)) /* CLK/256 */
#endif
#elif ((F_CPU / OSAL_ST_FREQUENCY / 1024) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 1024
#define AVR_TIMER_PRESCALER_BITS (1<<CS02)|(0<<CS01)|(1<<CS00); /* CLK/1024 */
#ifdef __AVR_ATmega128__
#define AVR_TIMER_PRESCALER_BITS (1<<CS02)|(1<<CS01)|(1<<CS00); /* CLK/1024 */
#else
#define AVR_TIMER_PRESCALER_BITS (1<<CS02)|(0<<CS01)|(1<<CS00); /* CLK/1024 */
#endif
#else
#error "Frequency too low for timer, please set OSAL_ST_FREQUENCY to a higher value"
#endif
@ -165,16 +190,16 @@ void st_lld_init(void) {
*/
/* CTC mode, no clock source */
TCCR1A = 0;
TCCR1B = _BV(WGM12);
TCCR1A = 0;
TCCR1B = _BV(WGM12);
/* start disabled */
TCCR1C = 0;
OCR1A = 0;
TCNT1 = 0;
TIFR1 = _BV(OCF1A); /* Reset pending. */
TIMSK1 = 0;
TCCR1B = PRESCALER;
TCCR1C = 0;
OCR1A = 0;
TCNT1 = 0;
TIFR_REG = _BV(OCF1A); /* Reset pending. */
TIMSK_REG = 0;
TCCR1B = PRESCALER;
#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */

View File

@ -27,6 +27,8 @@
#ifndef _ST_LLD_H_
#define _ST_LLD_H_
#include <avr/io.h>
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
@ -68,6 +70,14 @@ extern "C" {
}
#endif
#ifdef __AVR_ATmega128__
#define TIFR_REG TIFR
#define TIMSK_REG TIMSK
#else
#define TIFR_REG TIFR1
#define TIMSK_REG TIMSK1
#endif
/*===========================================================================*/
/* Driver inline functions. */
/*===========================================================================*/
@ -98,10 +108,10 @@ static inline void st_lld_start_alarm(systime_t time) {
OCR1A = (uint16_t) time;
/* Reset pending. */
TIFR1 = _BV(OCF1A);
TIFR_REG = _BV(OCF1A);
/* enable interrupt */
TIMSK1 = _BV(OCIE1A);
TIMSK_REG = _BV(OCIE1A);
}
/**
@ -111,7 +121,7 @@ static inline void st_lld_start_alarm(systime_t time) {
*/
static inline void st_lld_stop_alarm(void) {
TIMSK1 = 0;
TIMSK_REG = 0;
}
/**
@ -149,7 +159,7 @@ static inline systime_t st_lld_get_alarm(void) {
*/
static inline bool st_lld_is_alarm_active(void) {
return (bool) ((TIMSK1 & _BV(OCIE1A)) != 0);
return (bool) ((TIMSK_REG & _BV(OCIE1A)) != 0);
}
#endif /* _ST_LLD_H_ */