2015-04-16 08:33:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2015 FreshTemp, LLC.
|
|
|
|
* 2014 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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_saml21
|
|
|
|
* @ingroup drivers_periph_timer
|
2015-04-16 08:33:35 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file timer.c
|
|
|
|
* @brief Low-level timer driver implementation
|
|
|
|
*
|
|
|
|
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
|
|
|
|
#include "periph/timer.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Timer state memory
|
|
|
|
*/
|
2016-02-17 12:18:24 +01:00
|
|
|
static timer_isr_ctx_t config[TIMER_NUMOF];
|
2015-04-16 08:33:35 +02:00
|
|
|
|
2017-01-14 15:34:53 +01:00
|
|
|
/* enable timer interrupts */
|
|
|
|
static inline void _irq_enable(tim_t dev);
|
|
|
|
|
2015-04-16 08:33:35 +02:00
|
|
|
/**
|
|
|
|
* @brief Setup the given timer
|
|
|
|
*/
|
2016-02-17 12:18:24 +01:00
|
|
|
int timer_init(tim_t dev, unsigned long freq, timer_cb_t cb, void *arg)
|
2015-04-16 08:33:35 +02:00
|
|
|
{
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
/* at the moment, the timer can only run at 1MHz */
|
|
|
|
if (freq != 1000000ul) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-16 08:33:35 +02:00
|
|
|
/* configure GCLK0 to feed TC0 & TC1*/;
|
|
|
|
GCLK->PCHCTRL[TC0_GCLK_ID].reg |= GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0;
|
2016-01-26 20:01:06 +01:00
|
|
|
while (!(GCLK->PCHCTRL[TC0_GCLK_ID].reg & GCLK_PCHCTRL_CHEN)) {}
|
2015-04-16 08:33:35 +02:00
|
|
|
|
|
|
|
/* select the timer and enable the timer specific peripheral clocks */
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
if (TIMER_0_DEV.CTRLA.bit.ENABLE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
MCLK->APBCMASK.reg |= MCLK_APBCMASK_TC0;
|
|
|
|
/* reset timer */
|
|
|
|
TIMER_0_DEV.CTRLA.bit.SWRST = 1;
|
2016-01-26 20:01:06 +01:00
|
|
|
while (TIMER_0_DEV.SYNCBUSY.bit.SWRST) {}
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.CTRLA.reg |= TC_CTRLA_MODE_COUNT32 | /* choosing 32 bit mode */
|
|
|
|
TC_CTRLA_PRESCALER(4) | /* sourced by 4MHz with Presc 4 results in 1MHz*/
|
|
|
|
TC_CTRLA_PRESCSYNC_RESYNC; /* initial prescaler resync */
|
2015-04-16 08:33:35 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save callback */
|
2016-02-17 12:18:24 +01:00
|
|
|
config[dev].cb = cb;
|
|
|
|
config[dev].arg = arg;
|
2015-04-16 08:33:35 +02:00
|
|
|
|
|
|
|
/* enable interrupts for given timer */
|
2017-01-14 15:34:53 +01:00
|
|
|
_irq_enable(dev);
|
2015-04-16 08:33:35 +02:00
|
|
|
|
|
|
|
timer_start(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timer_set_absolute(tim_t dev, int channel, unsigned int value)
|
|
|
|
{
|
|
|
|
DEBUG("Setting timer %i channel %i to %i\n", dev, channel, value);
|
|
|
|
|
|
|
|
/* get timer base register address */
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
/* set timeout value */
|
|
|
|
switch (channel) {
|
|
|
|
case 0:
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC0;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.CC[0].reg = value;
|
|
|
|
TIMER_0_DEV.INTENSET.bit.MC0 = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC1;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.CC[1].reg = value;
|
|
|
|
TIMER_0_DEV.INTENSET.bit.MC1 = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timer_clear(tim_t dev, int channel)
|
|
|
|
{
|
|
|
|
/* get timer base register address */
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
switch (channel) {
|
|
|
|
case 0:
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC0;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.INTENCLR.bit.MC0 = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC1;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.INTENCLR.bit.MC1 = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int timer_read(tim_t dev)
|
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
/* request syncronisation */
|
|
|
|
TIMER_0_DEV.CTRLBSET.bit.CMD = TC_CTRLBSET_CMD_READSYNC_Val;
|
2016-01-26 20:01:06 +01:00
|
|
|
while (TIMER_0_DEV.SYNCBUSY.bit.STATUS) {}
|
2015-04-16 08:33:35 +02:00
|
|
|
return TIMER_0_DEV.COUNT.reg;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer_stop(tim_t dev)
|
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
TIMER_0_DEV.CTRLA.bit.ENABLE = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer_start(tim_t dev)
|
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
TIMER_0_DEV.CTRLA.bit.ENABLE = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 15:34:53 +01:00
|
|
|
static inline void _irq_enable(tim_t dev)
|
2015-04-16 08:33:35 +02:00
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if TIMER_0_EN
|
|
|
|
case TIMER_0:
|
|
|
|
NVIC_EnableIRQ(TC0_IRQn);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TIMER_UNDEFINED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TIMER_0_EN
|
|
|
|
void TIMER_0_ISR(void)
|
|
|
|
{
|
|
|
|
if (TIMER_0_DEV.INTFLAG.bit.MC0 && TIMER_0_DEV.INTENSET.bit.MC0) {
|
|
|
|
if(config[TIMER_0].cb) {
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC0;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.INTENCLR.reg = TC_INTENCLR_MC0;
|
2016-02-17 12:18:24 +01:00
|
|
|
config[TIMER_0].cb(config[TIMER_0].arg, 0);
|
2015-04-16 08:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (TIMER_0_DEV.INTFLAG.bit.MC1 && TIMER_0_DEV.INTENSET.bit.MC1) {
|
|
|
|
if(config[TIMER_0].cb) {
|
2016-12-19 18:27:47 +01:00
|
|
|
TIMER_0_DEV.INTFLAG.reg |= TC_INTFLAG_MC1;
|
2015-04-16 08:33:35 +02:00
|
|
|
TIMER_0_DEV.INTENCLR.reg = TC_INTENCLR_MC1;
|
2016-02-17 12:18:24 +01:00
|
|
|
config[TIMER_0].cb(config[TIMER_0].arg, 1);
|
2015-04-16 08:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-30 18:26:05 +01:00
|
|
|
cortexm_isr_end();
|
2015-04-16 08:33:35 +02:00
|
|
|
}
|
|
|
|
#endif /* TIMER_0_EN */
|