2014-10-13 15:29:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Loci Controls Inc.
|
|
|
|
*
|
|
|
|
* 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 details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup cpu_cc2538
|
|
|
|
* @ingroup drivers_periph_timer
|
2014-10-13 15:29:49 +02:00
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2014-10-13 15:29:49 +02:00
|
|
|
* @brief Low-level timer driver implementation for the CC2538 CPU
|
|
|
|
*
|
|
|
|
* @author Ian Martin <ian@locicontrols.com>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2016-08-16 21:07:26 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2014-10-13 15:29:49 +02:00
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph/timer.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
2016-08-16 21:07:26 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
2016-02-22 20:20:14 +01:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
#define LOAD_VALUE (0xffff)
|
2016-08-16 21:07:26 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
#define TIMER_A_IRQ_MASK (0x000000ff)
|
|
|
|
#define TIMER_B_IRQ_MASK (0x0000ff00)
|
2016-08-16 21:07:26 +02:00
|
|
|
|
|
|
|
#define BIT(n) ( 1UL << (n) )
|
|
|
|
|
|
|
|
/* GPTIMER_CTL Bits: */
|
|
|
|
#define TBEN BIT(8)
|
|
|
|
#define TAEN BIT(0)
|
|
|
|
|
|
|
|
/* GPTIMER_TnMR Bits: */
|
|
|
|
#define TnCMIE BIT(5)
|
|
|
|
#define TnCDIR BIT(4)
|
|
|
|
|
|
|
|
/* GPTIMER_IMR Bits: */
|
|
|
|
#define TBMIM BIT(11)
|
|
|
|
#define TAMIM BIT(4)
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
typedef struct {
|
|
|
|
uint16_t mask;
|
|
|
|
uint16_t flag;
|
|
|
|
} _isr_cfg_t;
|
2016-08-16 21:07:26 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
static const _isr_cfg_t chn_isr_cfg[] = {
|
|
|
|
{ .mask = TIMER_A_IRQ_MASK, .flag = TAMIM },
|
|
|
|
{ .mask = TIMER_B_IRQ_MASK, .flag = TBMIM }
|
|
|
|
};
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
static const int irqn_cfg[] = {
|
2016-08-16 21:07:26 +02:00
|
|
|
GPTIMER_0A_IRQn,
|
|
|
|
GPTIMER_1A_IRQn,
|
|
|
|
GPTIMER_2A_IRQn,
|
|
|
|
GPTIMER_3A_IRQn
|
|
|
|
};
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Timer state memory
|
|
|
|
*/
|
|
|
|
static timer_isr_ctx_t isr_ctx[TIMER_NUMOF];
|
|
|
|
|
2017-01-14 15:34:53 +01:00
|
|
|
/* enable timer interrupts */
|
2017-07-13 23:32:13 +02:00
|
|
|
static inline void _irq_enable(tim_t tim)
|
|
|
|
{
|
|
|
|
DEBUG("%s(%u)\n", __FUNCTION__, tim);
|
|
|
|
|
|
|
|
if (tim < TIMER_NUMOF) {
|
|
|
|
IRQn_Type irqn = irqn_cfg[tim];
|
|
|
|
|
|
|
|
NVIC_SetPriority(irqn, TIMER_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(irqn);
|
|
|
|
|
|
|
|
if (timer_config[tim].chn == 2) {
|
|
|
|
irqn++;
|
|
|
|
NVIC_SetPriority(irqn, TIMER_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(irqn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cc2538_gptimer_t *dev(tim_t tim)
|
|
|
|
{
|
|
|
|
assert(tim < TIMER_NUMOF);
|
|
|
|
|
|
|
|
return ((cc2538_gptimer_t *)(GPTIMER_BASE | (((uint32_t)tim) << 12)));
|
|
|
|
}
|
2017-01-14 15:34:53 +01:00
|
|
|
|
2014-10-13 15:29:49 +02:00
|
|
|
/**
|
|
|
|
* @brief Setup the given timer
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 23:32:13 +02:00
|
|
|
int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
|
2014-10-13 15:29:49 +02:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u, %lu, %p, %p)\n", __FUNCTION__, tim, freq, cb, arg);
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (tim >= TIMER_NUMOF) {
|
2016-08-16 21:07:26 +02:00
|
|
|
return -1;
|
2014-10-13 15:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the callback function: */
|
2017-07-13 23:32:13 +02:00
|
|
|
assert(tim < TIMER_NUMOF);
|
|
|
|
isr_ctx[tim].cb = cb;
|
|
|
|
isr_ctx[tim].arg = arg;
|
2014-10-13 15:29:49 +02:00
|
|
|
|
|
|
|
/* Enable the clock for this timer: */
|
2017-07-13 23:32:13 +02:00
|
|
|
SYS_CTRL_RCGCGPT |= (1 << tim);
|
2014-10-13 15:29:49 +02:00
|
|
|
|
|
|
|
/* Disable this timer before configuring it: */
|
2017-07-13 23:32:13 +02:00
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = 0;
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
uint32_t prescaler = 0;
|
|
|
|
uint32_t chan_mode = TnCMIE | GPTIMER_PERIODIC_MODE;
|
|
|
|
if (timer_config[tim].cfg == GPTMCFG_32_BIT_TIMER) {
|
2016-08-16 21:07:26 +02:00
|
|
|
/* Count up in periodic mode */
|
2017-07-13 23:32:13 +02:00
|
|
|
chan_mode |= TnCDIR ;
|
2016-08-16 21:07:26 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (timer_config[tim].chn > 1) {
|
2016-08-16 21:07:26 +02:00
|
|
|
DEBUG("Invalid timer_config. Multiple channels are available only in 16-bit mode.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freq != sys_clock_freq()) {
|
2018-01-12 16:25:32 +01:00
|
|
|
DEBUG("In 32-bit mode, the GPTimer frequency must equal the system clock frequency (%u).\n",
|
|
|
|
(unsigned)sys_clock_freq());
|
2016-08-16 21:07:26 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-13 23:32:13 +02:00
|
|
|
}
|
|
|
|
else if (timer_config[tim].cfg == GPTMCFG_16_BIT_TIMER) {
|
|
|
|
prescaler = sys_clock_freq();
|
|
|
|
prescaler += freq / 2;
|
|
|
|
prescaler /= freq;
|
|
|
|
if (prescaler > 0) prescaler--;
|
|
|
|
if (prescaler > 255) prescaler = 255;
|
|
|
|
|
|
|
|
dev(tim)->TAPR = prescaler;
|
|
|
|
dev(tim)->TAILR = LOAD_VALUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG("timer_init: invalid timer config must be 16 or 32Bit mode!\n");
|
|
|
|
return -1;
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
dev(tim)->CFG = timer_config[tim].cfg;
|
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = TAEN;
|
|
|
|
dev(tim)->cc2538_gptimer_tamr.TAMR = chan_mode;
|
2016-08-16 21:07:26 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (timer_config[tim].chn > 1) {
|
|
|
|
dev(tim)->cc2538_gptimer_tbmr.TBMR = chan_mode;
|
|
|
|
dev(tim)->TBPR = prescaler;
|
|
|
|
dev(tim)->TBILR = LOAD_VALUE;
|
|
|
|
/* Enable the timer: */
|
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = TBEN | TAEN;
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
2014-10-13 15:29:49 +02:00
|
|
|
|
|
|
|
/* Enable interrupts for given timer: */
|
2017-07-13 23:32:13 +02:00
|
|
|
_irq_enable(tim);
|
2014-10-13 15:29:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
int timer_set_absolute(tim_t tim, int channel, unsigned int value)
|
2014-10-13 15:29:49 +02:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u, %u, %u)\n", __FUNCTION__, tim, channel, value);
|
2016-08-16 21:07:26 +02:00
|
|
|
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
if ((tim >= TIMER_NUMOF) || (channel >= (int)timer_config[tim].chn) ) {
|
2016-02-22 20:20:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-13 23:32:13 +02:00
|
|
|
/* clear any pending match interrupts */
|
|
|
|
dev(tim)->ICR = chn_isr_cfg[channel].flag;
|
|
|
|
if (channel == 0) {
|
|
|
|
dev(tim)->TAMATCHR = (timer_config[tim].cfg == GPTMCFG_32_BIT_TIMER) ?
|
|
|
|
value : (LOAD_VALUE - value);
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
2017-07-13 23:32:13 +02:00
|
|
|
else {
|
|
|
|
dev(tim)->TBMATCHR = (LOAD_VALUE - value);
|
|
|
|
}
|
|
|
|
dev(tim)->cc2538_gptimer_imr.IMR |= chn_isr_cfg[channel].flag;
|
2016-02-22 20:20:14 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
int timer_clear(tim_t tim, int channel)
|
2016-02-22 20:20:14 +01:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u, %u)\n", __FUNCTION__, tim, channel);
|
2016-02-22 20:20:14 +01:00
|
|
|
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
if ( (tim >= TIMER_NUMOF) || (channel >= (int)timer_config[tim].chn) ) {
|
2016-02-22 20:20:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-13 23:32:13 +02:00
|
|
|
/* clear interupt flags */
|
|
|
|
dev(tim)->cc2538_gptimer_imr.IMR &= ~(chn_isr_cfg[channel].flag);
|
2016-02-22 20:20:14 +01:00
|
|
|
|
2014-10-13 15:29:49 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The timer channels 1 and 2 are configured to run with the same speed and
|
|
|
|
* have the same value (they run in parallel), so only on of them is returned.
|
|
|
|
*/
|
2017-07-13 23:32:13 +02:00
|
|
|
unsigned int timer_read(tim_t tim)
|
2014-10-13 15:29:49 +02:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u)\n", __FUNCTION__, tim);
|
|
|
|
|
|
|
|
if (tim >= TIMER_NUMOF) {
|
2016-08-16 21:07:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (timer_config[tim].cfg == GPTMCFG_32_BIT_TIMER) {
|
|
|
|
return dev(tim)->TAV;
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-07-13 23:32:13 +02:00
|
|
|
return LOAD_VALUE - (dev(tim)->TAV & 0xffff);
|
2014-10-13 15:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For stopping the counting of all channels.
|
|
|
|
*/
|
2017-07-13 23:32:13 +02:00
|
|
|
void timer_stop(tim_t tim)
|
2014-10-13 15:29:49 +02:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u)\n", __FUNCTION__, tim);
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (tim < TIMER_NUMOF) {
|
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = 0;
|
2014-10-13 15:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
void timer_start(tim_t tim)
|
2014-10-13 15:29:49 +02:00
|
|
|
{
|
2017-07-13 23:32:13 +02:00
|
|
|
DEBUG("%s(%u)\n", __FUNCTION__, tim);
|
2016-08-16 21:07:26 +02:00
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
if (tim < TIMER_NUMOF) {
|
|
|
|
if (timer_config[tim].chn == 1) {
|
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = TAEN;
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
2017-07-13 23:32:13 +02:00
|
|
|
else if (timer_config[tim].chn == 2) {
|
|
|
|
dev(tim)->cc2538_gptimer_ctl.CTL = TBEN | TAEN;
|
2016-08-16 21:07:26 +02:00
|
|
|
}
|
2014-10-13 15:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
/**
|
|
|
|
* @brief timer interrupt handler
|
|
|
|
*
|
|
|
|
* @param[in] num GPT instance number
|
|
|
|
* @param[in] chn channel number (0=A, 1=B)
|
|
|
|
*/
|
|
|
|
static void irq_handler(tim_t tim, int channel)
|
|
|
|
{
|
|
|
|
DEBUG("%s(%u,%d)\n", __FUNCTION__, tim, channel);
|
|
|
|
assert(tim < TIMER_NUMOF);
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
assert(channel < (int)timer_config[tim].chn);
|
2017-07-13 23:32:13 +02:00
|
|
|
|
|
|
|
uint32_t mis;
|
|
|
|
/* Latch the active interrupt flags */
|
|
|
|
mis = dev(tim)->MIS & chn_isr_cfg[channel].mask;
|
|
|
|
/* Clear the latched interrupt flags */
|
|
|
|
dev(tim)->ICR = mis;
|
|
|
|
|
|
|
|
if (mis & chn_isr_cfg[channel].flag) {
|
|
|
|
/* Disable further match interrupts for this timer/channel */
|
|
|
|
dev(tim)->cc2538_gptimer_imr.IMR &= ~chn_isr_cfg[channel].flag;
|
|
|
|
/* Invoke the callback function */
|
|
|
|
isr_ctx[tim].cb(isr_ctx[tim].arg, channel);
|
|
|
|
}
|
2014-10-13 15:29:49 +02:00
|
|
|
|
2016-11-30 18:26:05 +01:00
|
|
|
cortexm_isr_end();
|
2014-10-13 15:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 23:32:13 +02:00
|
|
|
void isr_timer0_chan0(void) {irq_handler(0, 0);}
|
|
|
|
void isr_timer0_chan1(void) {irq_handler(0, 1);}
|
|
|
|
void isr_timer1_chan0(void) {irq_handler(1, 0);}
|
|
|
|
void isr_timer1_chan1(void) {irq_handler(1, 1);}
|
|
|
|
void isr_timer2_chan0(void) {irq_handler(2, 0);}
|
|
|
|
void isr_timer2_chan1(void) {irq_handler(2, 1);}
|
|
|
|
void isr_timer3_chan0(void) {irq_handler(3, 0);}
|
|
|
|
void isr_timer3_chan1(void) {irq_handler(3, 1);}
|