Skip to content

Commit

Permalink
Merge pull request #4040 from gebart/pr/periph-timer-init-freq
Browse files Browse the repository at this point in the history
periph/timer: Change timer_init API to support arbitrary (integer) frequencies
  • Loading branch information
Joakim Nohlgård committed Feb 15, 2016
2 parents a66ce9c + f76ecd1 commit e2f7ac7
Show file tree
Hide file tree
Showing 40 changed files with 120 additions and 102 deletions.
4 changes: 2 additions & 2 deletions boards/f4vi1/include/periph_conf.h
Expand Up @@ -62,7 +62,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (83U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK / 2)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand All @@ -71,7 +71,7 @@ extern "C" {
/* Timer 1 configuration */
#define TIMER_1_DEV TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (83U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK / 2)
#define TIMER_1_MAX_VALUE (0xffffffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM5EN)
#define TIMER_1_ISR isr_tim5
Expand Down
4 changes: 2 additions & 2 deletions boards/fox/include/periph_conf.h
Expand Up @@ -53,7 +53,7 @@ extern "C" {
#define TIMER_0_DEV_0 TIM2
#define TIMER_0_DEV_1 TIM3
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (72U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN))
#define TIMER_0_ISR_0 isr_tim2
Expand All @@ -67,7 +67,7 @@ extern "C" {
#define TIMER_1_DEV_0 TIM4
#define TIMER_1_DEV_1 TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (72U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK)
#define TIMER_1_MAX_VALUE (0xffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM4EN | RCC_APB1ENR_TIM5EN))
#define TIMER_1_ISR_0 isr_tim4
Expand Down
4 changes: 2 additions & 2 deletions boards/iotlab-m3/include/periph_conf.h
Expand Up @@ -54,7 +54,7 @@ extern "C" {
#define TIMER_0_DEV_0 TIM2
#define TIMER_0_DEV_1 TIM3
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (72U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN))
#define TIMER_0_ISR_0 isr_tim2
Expand All @@ -68,7 +68,7 @@ extern "C" {
#define TIMER_1_DEV_0 TIM4
#define TIMER_1_DEV_1 TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (72U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK)
#define TIMER_1_MAX_VALUE (0xffffffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM4EN | RCC_APB1ENR_TIM5EN))
#define TIMER_1_ISR_0 isr_tim4
Expand Down
2 changes: 1 addition & 1 deletion boards/mbed_lpc1768/include/periph_conf.h
Expand Up @@ -34,7 +34,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV LPC_TIM0
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (95U)
#define TIMER_0_FREQ (96000000ul)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (LPC_SC->PCONP |= (1 << 1))
#define TIMER_0_CLKDIS() (LPC_SC->PCONP &= ~(1 << 1))
Expand Down
4 changes: 2 additions & 2 deletions boards/msbiot/include/periph_conf.h
Expand Up @@ -61,7 +61,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (83U)
#define TIMER_0_FREQ (84000000U)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand All @@ -70,7 +70,7 @@ extern "C" {
/* Timer 1 configuration */
#define TIMER_1_DEV TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (83U)
#define TIMER_1_FREQ (84000000U)
#define TIMER_1_MAX_VALUE (0xffffffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM5EN)
#define TIMER_1_ISR isr_tim5
Expand Down
2 changes: 1 addition & 1 deletion boards/nucleo-f091/include/periph_conf.h
Expand Up @@ -45,7 +45,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (47U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_IRQ_CHAN TIM2_IRQn
Expand Down
4 changes: 2 additions & 2 deletions boards/nucleo-f103/include/periph_conf.h
Expand Up @@ -54,7 +54,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV_0 TIM2
#define TIMER_0_DEV_1 TIM3
#define TIMER_0_PRESCALER (CLOCK_CORECLOCK / 1000000U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN))
#define TIMER_0_ISR_0 isr_tim2
Expand All @@ -67,7 +67,7 @@ extern "C" {
/* Timer 1 configuration */
#define TIMER_1_DEV_0 TIM4
#define TIMER_1_DEV_1 TIM5
#define TIMER_1_PRESCALER (36000U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK)
#define TIMER_1_MAX_VALUE (0xffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM4EN | RCC_APB1ENR_TIM5EN))
#define TIMER_1_ISR_0 isr_tim4
Expand Down
2 changes: 1 addition & 1 deletion boards/nucleo-f303/include/periph_conf.h
Expand Up @@ -51,7 +51,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (71U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_IRQ_CHAN TIM2_IRQn
Expand Down
2 changes: 1 addition & 1 deletion boards/nucleo-f334/include/periph_conf.h
Expand Up @@ -50,7 +50,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (71U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand Down
4 changes: 2 additions & 2 deletions boards/nucleo-f401/include/periph_conf.h
Expand Up @@ -60,7 +60,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (83U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand All @@ -69,7 +69,7 @@ extern "C" {
/* Timer 1 configuration */
#define TIMER_1_DEV TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (83U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK)
#define TIMER_1_MAX_VALUE (0xffffffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM5EN)
#define TIMER_1_ISR isr_tim5
Expand Down
2 changes: 1 addition & 1 deletion boards/spark-core/include/periph_conf.h
Expand Up @@ -52,7 +52,7 @@
#define TIMER_0_DEV_0 TIM2
#define TIMER_0_DEV_1 TIM3
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (72U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN))
#define TIMER_0_ISR_0 isr_tim2
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f0discovery/include/periph_conf.h
Expand Up @@ -45,7 +45,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (47U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f3discovery/include/periph_conf.h
Expand Up @@ -49,7 +49,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (71U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f4discovery/include/board.h
Expand Up @@ -35,7 +35,7 @@ extern "C" {
#define XTIMER TIMER_0
#define XTIMER_CHAN (0)
#define XTIMER_OVERHEAD (6)
#define XTIMER_BACKOFF (5)
#define XTIMER_BACKOFF (10)
/** @} */

/**
Expand Down
4 changes: 2 additions & 2 deletions boards/stm32f4discovery/include/periph_conf.h
Expand Up @@ -61,7 +61,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV TIM2
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (83U)
#define TIMER_0_FREQ (CLOCK_CORECLOCK / 2)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM2EN)
#define TIMER_0_ISR isr_tim2
Expand All @@ -70,7 +70,7 @@ extern "C" {
/* Timer 1 configuration */
#define TIMER_1_DEV TIM5
#define TIMER_1_CHANNELS 4
#define TIMER_1_PRESCALER (83U)
#define TIMER_1_FREQ (CLOCK_CORECLOCK / 2)
#define TIMER_1_MAX_VALUE (0xffffffff)
#define TIMER_1_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_TIM5EN)
#define TIMER_1_ISR isr_tim5
Expand Down
2 changes: 1 addition & 1 deletion boards/weio/include/periph_conf.h
Expand Up @@ -42,7 +42,7 @@ extern "C" {
/* Timer 0 configuration */
#define TIMER_0_DEV LPC_CT32B1
#define TIMER_0_CHANNELS 4
#define TIMER_0_PRESCALER (48U)
#define TIMER_0_FREQ (48000000ul)
#define TIMER_0_MAX_VALUE (0xffffffff)
#define TIMER_0_CLKEN() (LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10))
#define TIMER_0_CLKDIS() (LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 10))
Expand Down
10 changes: 6 additions & 4 deletions cpu/atmega2560/periph/timer.c
Expand Up @@ -46,10 +46,11 @@ timer_conf_t config[TIMER_NUMOF];
* @brief Setup the given timer
*
*/
int timer_init(tim_t dev, unsigned int us_per_ticks, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
/* reject impossible us_per_ticks values */
if ((us_per_ticks != 4)) {
/* reject impossible freq values
* todo: Add support for 2 MHz and 16 MHz */
if ((freq != 250000ul)) {
return -1;
}

Expand Down Expand Up @@ -279,7 +280,7 @@ int timer_clear(tim_t dev, int channel)
unsigned int timer_read(tim_t dev)
{
uint16_t a;
uint32_t b;
uint16_t b;
/*
* Disabling interrupts globally because read from 16 Bit register can
* otherwise be messed up
Expand Down Expand Up @@ -317,6 +318,7 @@ unsigned int timer_read(tim_t dev)

case TIMER_UNDEFINED:
default:
(void)b;
a = 0;
}

Expand Down
4 changes: 2 additions & 2 deletions cpu/cc2538/periph/timer.c
Expand Up @@ -44,7 +44,7 @@ timer_conf_t config[TIMER_NUMOF];
* @brief Setup the given timer
*
*/
int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
cc2538_gptimer_t *gptimer;
unsigned int gptimer_num;
Expand Down Expand Up @@ -93,7 +93,7 @@ int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
gptimer->cc2538_gptimer_tamr.TAMRbits.TACDIR = 1; /**< Count up */

/* Set the prescale register for the desired frequency: */
gptimer->TAPR = RCOSC16M_FREQ / (ticks_per_us * USEC_PER_SEC) - 1;
gptimer->TAPR = (RCOSC16M_FREQ / freq) - 1;

/* Enable interrupts for given timer: */
timer_irq_enable(dev);
Expand Down
6 changes: 3 additions & 3 deletions cpu/cc430/periph/timer.c
Expand Up @@ -35,14 +35,14 @@
static void (*isr_cb)(int chan);


int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
/* using fixed TIMER_BASE for now */
if (dev != 0) {
return -1;
}
/* TODO: configure time-base depending on us_per_tick value */
if (us_per_tick != 1) {
/* TODO: configure time-base depending on freq value */
if (freq != 1000000ul) {
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions cpu/ezr32wg/periph/timer.c
Expand Up @@ -38,7 +38,7 @@
static timer_isr_ctx_t isr_ctx[TIMER_NUMOF];


int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
TIMER_TypeDef *pre, *tim;

Expand All @@ -63,7 +63,7 @@ int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
* configure it up-counting, driven by the HFPER clock and we set the TOP
* register depending on the specified timer speed value */
pre->CTRL = 0;
pre->TOP = ((CLOCK_HFPERCLK / 1000000) - 1) * us_per_tick;
pre->TOP = ((CLOCK_HFPERCLK / freq) - 1);
pre->CNT = 0;
pre->IEN = 0;

Expand Down
12 changes: 6 additions & 6 deletions cpu/kinetis_common/periph/timer.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2015 PHYTEC Messtechnik GmbH
* Copyright (C) 2014-2015 PHYTEC Messtechnik GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
Expand Down Expand Up @@ -57,10 +57,10 @@ inline static void pit_timer_stop(uint8_t ch)
}

/** use channel n-1 as prescaler */
inline static void timer_set_prescaler(uint8_t ch, unsigned int ticks_per_us)
inline static void timer_set_prescaler(uint8_t ch, unsigned long freq)
{
TIMER_BASE->CHANNEL[ch].TCTRL = 0x0;
TIMER_BASE->CHANNEL[ch].LDVAL = (TIMER_CLOCK / 1e6) / ticks_per_us - 1;
TIMER_BASE->CHANNEL[ch].LDVAL = (TIMER_CLOCK / freq) - 1;
TIMER_BASE->CHANNEL[ch].TCTRL = (PIT_TCTRL_TEN_MASK);
}

Expand All @@ -84,7 +84,7 @@ inline static void pit_timer_set_max(uint8_t ch)
pit_timer_start(ch);
}

int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
/* enable timer peripheral clock */
TIMER_CLKEN();
Expand All @@ -97,15 +97,15 @@ int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))

case TIMER_0:
NVIC_SetPriority(TIMER_0_IRQ_CHAN, TIMER_IRQ_PRIO);
timer_set_prescaler(TIMER_0_PRESCALER_CH, ticks_per_us);
timer_set_prescaler(TIMER_0_PRESCALER_CH, freq);
timer_set_counter(TIMER_0_COUNTER_CH);
break;
#endif
#if TIMER_1_EN

case TIMER_1:
NVIC_SetPriority(TIMER_1_IRQ_CHAN, TIMER_IRQ_PRIO);
timer_set_prescaler(TIMER_1_PRESCALER_CH, ticks_per_us);
timer_set_prescaler(TIMER_1_PRESCALER_CH, freq);
timer_set_counter(TIMER_1_COUNTER_CH);
break;
#endif
Expand Down
4 changes: 2 additions & 2 deletions cpu/lm4f120/periph/timer.c
Expand Up @@ -66,14 +66,14 @@ static inline unsigned int _llvalue_to_scaled_value(unsigned long long corrected
return scaledv;
}

int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
if (dev >= TIMER_NUMOF){
return -1;
}

config[dev].cb = callback; /* User Function */
config[dev].divisor = us_per_tick * ROM_SysCtlClockGet()/1000000;
config[dev].divisor = ROM_SysCtlClockGet() / freq;

unsigned int sysctl_timer;
unsigned int timer_base;
Expand Down
4 changes: 2 additions & 2 deletions cpu/lpc11u34/periph/timer.c
Expand Up @@ -50,7 +50,7 @@ typedef struct {
*/
static timer_conf_t config[TIMER_NUMOF];

int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
if (dev == TIMER_0) {
/* save callback */
Expand All @@ -60,7 +60,7 @@ int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
/* set to timer mode */
TIMER_0_DEV->CTCR = 0;
/* configure prescaler */
TIMER_0_DEV->PR = (us_per_tick * TIMER_0_PRESCALER);
TIMER_0_DEV->PR = (TIMER_0_FREQ / freq) - 1;
/* configure and enable timer interrupts */
NVIC_SetPriority(TIMER_0_IRQ, TIMER_IRQ_PRIO);
NVIC_EnableIRQ(TIMER_0_IRQ);
Expand Down
4 changes: 2 additions & 2 deletions cpu/lpc1768/periph/timer.c
Expand Up @@ -50,7 +50,7 @@ typedef struct {
*/
static timer_conf_t config[TIMER_NUMOF];

int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
int timer_init(tim_t dev, unsigned long freq, void (*callback)(int))
{
if (dev == TIMER_0) {
/* save callback */
Expand All @@ -62,7 +62,7 @@ int timer_init(tim_t dev, unsigned int us_per_tick, void (*callback)(int))
/* set to timer mode */
TIMER_0_DEV->CTCR = 0;
/* configure prescaler */
TIMER_0_DEV->PR = (us_per_tick * TIMER_0_PRESCALER);
TIMER_0_DEV->PR = (TIMER_0_FREQ / freq) - 1;
/* configure and enable timer interrupts */
NVIC_SetPriority(TIMER_0_IRQ, TIMER_IRQ_PRIO);
NVIC_EnableIRQ(TIMER_0_IRQ);
Expand Down

0 comments on commit e2f7ac7

Please sign in to comment.