diff options
Diffstat (limited to 'os/hal')
-rw-r--r-- | os/hal/ports/AVR/avr_timers.h | 7 | ||||
-rw-r--r-- | os/hal/ports/AVR/hal_lld.c | 48 | ||||
-rw-r--r-- | os/hal/ports/AVR/hal_lld.h | 45 | ||||
-rw-r--r-- | os/hal/ports/AVR/st_lld.c | 155 | ||||
-rw-r--r-- | os/hal/ports/AVR/st_lld.h | 29 |
5 files changed, 186 insertions, 98 deletions
diff --git a/os/hal/ports/AVR/avr_timers.h b/os/hal/ports/AVR/avr_timers.h index 26b891937..b0a137ddd 100644 --- a/os/hal/ports/AVR/avr_timers.h +++ b/os/hal/ports/AVR/avr_timers.h @@ -17,6 +17,13 @@ #ifndef _AVR_TIMERS_H_
#define _AVR_TIMERS_H_
+#include "mcuconf.h"
+
+#if ((NIL_CFG_ST_TIMEDELTA > 0 || CH_CFG_ST_TIMEDELTA > 0) && \
+ (AVR_GPT_USE_TIM1 || AVR_PWM_USE_TIM1 || AVR_ICU_USE_TIM1))
+ #error "Timer 1 cannot be used by drivers when running in tickless mode."
+#endif
+
#if ((AVR_GPT_USE_TIM1 && AVR_PWM_USE_TIM1) || \
(AVR_GPT_USE_TIM1 && AVR_ICU_USE_TIM1) || \
(AVR_PWM_USE_TIM1 && AVR_ICU_USE_TIM1))
diff --git a/os/hal/ports/AVR/hal_lld.c b/os/hal/ports/AVR/hal_lld.c index 894d5dcc5..c3b90dd6b 100644 --- a/os/hal/ports/AVR/hal_lld.c +++ b/os/hal/ports/AVR/hal_lld.c @@ -40,21 +40,6 @@ /* Driver interrupt handlers. */
/*===========================================================================*/
-/**
- * @brief Timer0 interrupt handler.
- */
-OSAL_IRQ_HANDLER(AVR_TIMER_VECT) {
-
- OSAL_IRQ_PROLOGUE();
-
- osalSysLockFromISR();
- osalOsTimerHandlerI();
- osalSysUnlockFromISR();
-
- OSAL_IRQ_EPILOGUE();
-}
-
-
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
@@ -65,39 +50,6 @@ OSAL_IRQ_HANDLER(AVR_TIMER_VECT) { * @notapi
*/
void hal_lld_init(void) {
-
- /*
- * Timer 0 setup.
- */
-#if defined(TCCR0B) /* Timer has multiple output comparators */
- TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
- (0 << COM0A1) | (0 << COM0A0) | /* OC0A disabled. */
- (0 << COM0B1) | (0 << COM0B0); /* OC0B disabled. */
- TCCR0B = (0 << WGM02) | AVR_TIMER_PRESCALER_BITS; /* CTC mode. */
- OCR0A = AVR_TIMER_COUNTER - 1;
- TCNT0 = 0; /* Reset counter. */
- TIFR0 = (1 << OCF0A); /* Reset pending. */
- TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
-
-#elif defined(TCCR0A) /* AT90CAN doesn't have TCCR0B and slightly different TCCR0A */
- TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
- (0 << COM0A1) | (0 << COM0A0); /* OC0A disabled. */
- OCR0A = AVR_TIMER_COUNTER - 1;
- TCNT0 = 0; /* Reset counter. */
- TIFR0 = (1 << OCF0A); /* Reset pending. */
- TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
-
-#elif defined(TCCR0) /* Timer has single output comparator */
- TCCR0 = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
- (0 << COM01) | (0 << COM00) | /* OC0A disabled. */
- AVR_TIMER_PRESCALER_BITS;
- OCR0 = AVR_TIMER_COUNTER - 1;
- TCNT0 = 0; /* Reset counter. */
- TIFR = (1 << OCF0); /* Reset pending. */
- TIMSK = (1 << OCIE0); /* IRQ on compare. */
-#else
- #error "Neither TCCR0A nor TCCR0 registers are defined"
-#endif
}
/** @} */
diff --git a/os/hal/ports/AVR/hal_lld.h b/os/hal/ports/AVR/hal_lld.h index c72063ac0..5685943f3 100644 --- a/os/hal/ports/AVR/hal_lld.h +++ b/os/hal/ports/AVR/hal_lld.h @@ -39,59 +39,14 @@ */
#define PLATFORM_NAME "AVR"
-/**
- * @brief Timer maximum value
- */
-#define AVR_TIMER_COUNTER_MAX 255
-
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
-/* Work out what the timer interrupt is called on this MCU */
-#ifdef TIMER0_COMPA_vect
- #define AVR_TIMER_VECT TIMER0_COMPA_vect
-#elif defined(TIMER_COMPA_vect)
- #define AVR_TIMER_VECT TIMER_COMPA_vect
-#elif defined(TIMER0_COMP_vect)
- #define AVR_TIMER_VECT TIMER0_COMP_vect
-#else
- #error "Cannot find interrupt vector name for timer"
-#endif
-
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
-/* 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 */
-#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 */
-#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 */
-#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 */
-#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 */
-#else
- #error "Frequency too low for timer, please set OSAL_ST_FREQUENCY to a higher value"
-#endif
-
-#define AVR_TIMER_COUNTER (F_CPU / OSAL_ST_FREQUENCY / AVR_TIMER_PRESCALER)
-
-/* Test if OSAL_ST_FREQUENCY can be matched exactly using this timer */
-#define F_CPU_ (AVR_TIMER_COUNTER * AVR_TIMER_PRESCALER * OSAL_ST_FREQUENCY)
-#if (F_CPU_ != F_CPU)
- #warning "OSAL_ST_FREQUENCY cannot be generated exactly using timer"
-#endif
-#undef F_CPU_
-
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
diff --git a/os/hal/ports/AVR/st_lld.c b/os/hal/ports/AVR/st_lld.c index 463ff8b8a..349d7307e 100644 --- a/os/hal/ports/AVR/st_lld.c +++ b/os/hal/ports/AVR/st_lld.c @@ -30,6 +30,11 @@ /* Driver local definitions. */
/*===========================================================================*/
+/**
+ * @brief Timer maximum value
+ */
+#define AVR_TIMER_COUNTER_MAX 255
+
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
@@ -42,6 +47,60 @@ /* Driver local variables and types. */
/*===========================================================================*/
+#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
+
+/* Work out what the timer interrupt is called on this MCU */
+#ifdef TIMER0_COMPA_vect
+ #define AVR_TIMER_VECT TIMER0_COMPA_vect
+#elif defined(TIMER_COMPA_vect)
+ #define AVR_TIMER_VECT TIMER_COMPA_vect
+#elif defined(TIMER0_COMP_vect)
+ #define AVR_TIMER_VECT TIMER0_COMP_vect
+#else
+ #error "Cannot find interrupt vector name for timer"
+#endif
+
+/* 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 */
+#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 */
+#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 */
+#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 */
+#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 */
+#else
+ #error "Frequency too low for timer, please set OSAL_ST_FREQUENCY to a higher value"
+#endif
+
+#define AVR_TIMER_COUNTER (F_CPU / OSAL_ST_FREQUENCY / AVR_TIMER_PRESCALER)
+
+/* Test if OSAL_ST_FREQUENCY can be matched exactly using this timer */
+#define F_CPU_ (AVR_TIMER_COUNTER * AVR_TIMER_PRESCALER * OSAL_ST_FREQUENCY)
+#if (F_CPU_ != F_CPU)
+ #warning "OSAL_ST_FREQUENCY cannot be generated exactly using timer"
+#endif
+#undef F_CPU_
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */
+
+#if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
+
+/* FIXME: Prescaler is now fixed in 1024.
+ * Should add support for calculating best value according to
+ * user requested configuration.
+ */
+#define PRESCALER (_BV(CS12) | _BV(CS10))
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
+
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
@@ -50,6 +109,44 @@ /* Driver interrupt handlers. */
/*===========================================================================*/
+#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
+
+/**
+ * @brief Timer handler for periodic mode.
+ */
+OSAL_IRQ_HANDLER(AVR_TIMER_VECT) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+
+ OSAL_IRQ_EPILOGUE();
+}
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */
+
+#if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
+
+/**
+ * @brief Timer handler for free running mode.
+ */
+OSAL_IRQ_HANDLER(TIMER1_COMPA_vect) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ // TODO: reset status if required
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+
+ OSAL_IRQ_EPILOGUE();
+}
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
+
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
@@ -60,6 +157,64 @@ * @notapi
*/
void st_lld_init(void) {
+
+#if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
+
+ /*
+ * Periodic mode uses Timer 1 (16 bit).
+ */
+
+ /* CTC mode, no clock source */
+ TCCR1A = 0;
+ TCCR1B = _BV(WGM12);
+
+ /* start disabled */
+ TCCR1C = 0;
+ OCR1A = 0;
+ TCNT1 = 0;
+ TIFR1 = _BV(OCF1A); /* Reset pending. */
+ TIMSK1 = 0;
+ TCCR1B = PRESCALER;
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
+
+#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
+
+ /*
+ * Periodic mode uses Timer 0 (8 bit).
+ */
+#if defined(TCCR0B) /* Timer has multiple output comparators */
+ TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
+ (0 << COM0A1) | (0 << COM0A0) | /* OC0A disabled. */
+ (0 << COM0B1) | (0 << COM0B0); /* OC0B disabled. */
+ TCCR0B = (0 << WGM02) | AVR_TIMER_PRESCALER_BITS; /* CTC mode. */
+ OCR0A = AVR_TIMER_COUNTER - 1;
+ TCNT0 = 0; /* Reset counter. */
+ TIFR0 = (1 << OCF0A); /* Reset pending. */
+ TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
+
+#elif defined(TCCR0A) /* AT90CAN doesn't have TCCR0B and slightly different TCCR0A */
+ TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
+ (0 << COM0A1) | (0 << COM0A0); /* OC0A disabled. */
+ OCR0A = AVR_TIMER_COUNTER - 1;
+ TCNT0 = 0; /* Reset counter. */
+ TIFR0 = (1 << OCF0A); /* Reset pending. */
+ TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
+
+#elif defined(TCCR0) /* Timer has single output comparator */
+ TCCR0 = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
+ (0 << COM01) | (0 << COM00) | /* OC0A disabled. */
+ AVR_TIMER_PRESCALER_BITS;
+ OCR0 = AVR_TIMER_COUNTER - 1;
+ TCNT0 = 0; /* Reset counter. */
+ TIFR = (1 << OCF0); /* Reset pending. */
+ TIMSK = (1 << OCIE0); /* IRQ on compare. */
+#else
+ #error "Neither TCCR0A nor TCCR0 registers are defined"
+#endif
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */
+
}
#endif /* OSAL_ST_MODE != OSAL_ST_MODE_NONE */
diff --git a/os/hal/ports/AVR/st_lld.h b/os/hal/ports/AVR/st_lld.h index 024135c8f..6b47596ba 100644 --- a/os/hal/ports/AVR/st_lld.h +++ b/os/hal/ports/AVR/st_lld.h @@ -27,6 +27,9 @@ #ifndef _ST_LLD_H_
#define _ST_LLD_H_
+#include "mcuconf.h"
+#include "avr_timers.h"
+
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
@@ -35,10 +38,19 @@ /* Driver pre-compile time settings. */
/*===========================================================================*/
+/*
+ * TODO: for models that have many timers,
+ * could add AVR_ST_USE_TIMER
+ */
+
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
+/*
+ * TODO: error checks for valid timer selected
+ */
+
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
@@ -72,7 +84,7 @@ extern "C" { */
static inline systime_t st_lld_get_counter(void) {
- return (systime_t)0;
+ return (systime_t) TCNT1;
}
/**
@@ -86,7 +98,13 @@ static inline systime_t st_lld_get_counter(void) { */
static inline void st_lld_start_alarm(systime_t time) {
- (void)time;
+ OCR1A = (uint16_t) time;
+
+ /* Reset pending. */
+ TIFR1 = _BV(OCF1A);
+
+ /* enable interrupt */
+ TIMSK1 = _BV(OCIE1A);
}
/**
@@ -96,6 +114,7 @@ static inline void st_lld_start_alarm(systime_t time) { */
static inline void st_lld_stop_alarm(void) {
+ TIMSK1 = 0;
}
/**
@@ -107,7 +126,7 @@ static inline void st_lld_stop_alarm(void) { */
static inline void st_lld_set_alarm(systime_t time) {
- (void)time;
+ OCR1A = (uint16_t) time;
}
/**
@@ -119,7 +138,7 @@ static inline void st_lld_set_alarm(systime_t time) { */
static inline systime_t st_lld_get_alarm(void) {
- return (systime_t)0;
+ return (systime_t) OCR1A;
}
/**
@@ -133,7 +152,7 @@ static inline systime_t st_lld_get_alarm(void) { */
static inline bool st_lld_is_alarm_active(void) {
- return false;
+ return (bool) ((TIMSK1 & _BV(OCIE1A)) != 0);
}
#endif /* _ST_LLD_H_ */
|