2014-08-27 18:47:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013, Freie Universitaet Berlin (FUB). All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "hwtimer.h"
|
2014-07-09 21:08:13 +02:00
|
|
|
#include "arch/hwtimer_arch.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
|
2010-12-08 12:16:49 +01:00
|
|
|
void (*int_handler)(int);
|
|
|
|
extern void timerA_init(void);
|
2014-04-01 11:04:31 +02:00
|
|
|
volatile uint16_t overflow_interrupt[HWTIMER_MAXTIMERS+1];
|
|
|
|
volatile uint16_t timer_round;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-10-01 15:21:54 +02:00
|
|
|
#ifdef CC430
|
|
|
|
/* CC430 have "TimerA0", "TimerA1" and so on... */
|
|
|
|
#define CNT_CTRL_BASE_REG (TA0CCTL0)
|
|
|
|
#define CNT_COMP_BASE_REG (TA0CCR0)
|
|
|
|
#define TIMER_VAL_REG (TA0R)
|
|
|
|
#else
|
|
|
|
/* ... while other MSP430 MCUs have "TimerA", "TimerB".
|
|
|
|
Cheers for TI and its consistency! */
|
|
|
|
#define CNT_CTRL_BASE_REG (TACCTL0)
|
|
|
|
#define CNT_COMP_BASE_REG (TACCR0)
|
|
|
|
#define TIMER_VAL_REG (TAR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void timer_disable_interrupt(short timer)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2013-10-01 15:21:54 +02:00
|
|
|
volatile unsigned int *ptr = &CNT_CTRL_BASE_REG + (timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
*ptr &= ~(CCIFG);
|
|
|
|
*ptr &= ~(CCIE);
|
|
|
|
}
|
|
|
|
|
2013-10-01 15:21:54 +02:00
|
|
|
static void timer_enable_interrupt(short timer)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2013-10-01 15:21:54 +02:00
|
|
|
volatile unsigned int *ptr = &CNT_CTRL_BASE_REG + (timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
*ptr |= CCIE;
|
|
|
|
*ptr &= ~(CCIFG);
|
|
|
|
}
|
|
|
|
|
2014-04-01 11:04:31 +02:00
|
|
|
static void timer_set_nostart(uint32_t value, short timer)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2013-10-01 15:21:54 +02:00
|
|
|
volatile unsigned int *ptr = &CNT_COMP_BASE_REG + (timer);
|
2014-04-01 11:04:31 +02:00
|
|
|
/* ensure we won't set the timer to a "past" tick */
|
|
|
|
if (value <= hwtimer_arch_now()) {
|
|
|
|
value = hwtimer_arch_now() + 2;
|
|
|
|
}
|
|
|
|
overflow_interrupt[timer] = (uint16_t)(value >> 16);
|
|
|
|
*ptr = (value & 0xFFFF);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:04:31 +02:00
|
|
|
static void timer_set(uint32_t value, short timer)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2014-04-01 11:04:31 +02:00
|
|
|
DEBUG("Setting timer %u to %lu\n", timer, value);
|
2013-10-01 15:21:54 +02:00
|
|
|
timer_set_nostart(value, timer);
|
|
|
|
timer_enable_interrupt(timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-10-01 15:21:54 +02:00
|
|
|
void timer_unset(short timer)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2013-10-01 15:21:54 +02:00
|
|
|
volatile unsigned int *ptr = &CNT_COMP_BASE_REG + (timer);
|
|
|
|
timer_disable_interrupt(timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 12:36:32 +02:00
|
|
|
unsigned long hwtimer_arch_now(void)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2014-04-01 11:04:31 +02:00
|
|
|
return ((uint32_t)timer_round << 16) + TIMER_VAL_REG;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_init(void (*handler)(int), uint32_t fcpu)
|
|
|
|
{
|
2013-08-04 04:06:31 +02:00
|
|
|
(void) fcpu;
|
2010-09-22 15:10:42 +02:00
|
|
|
timerA_init();
|
|
|
|
int_handler = handler;
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_enable_interrupt(void)
|
|
|
|
{
|
2014-03-21 17:31:55 +01:00
|
|
|
for (int i = 0; i < HWTIMER_MAXTIMERS; i++) {
|
2013-10-01 15:21:54 +02:00
|
|
|
timer_enable_interrupt(i);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_disable_interrupt(void)
|
|
|
|
{
|
2014-03-21 17:31:55 +01:00
|
|
|
for (int i = 0; i < HWTIMER_MAXTIMERS; i++) {
|
2013-10-01 15:21:54 +02:00
|
|
|
timer_disable_interrupt(i);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_set(unsigned long offset, short timer)
|
|
|
|
{
|
2013-08-23 22:03:04 +02:00
|
|
|
uint32_t value = hwtimer_arch_now() + offset;
|
2010-09-22 15:10:42 +02:00
|
|
|
hwtimer_arch_set_absolute(value, timer);
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_set_absolute(unsigned long value, short timer)
|
|
|
|
{
|
2014-04-01 11:04:31 +02:00
|
|
|
timer_set(value, timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_unset(short timer)
|
|
|
|
{
|
2013-10-01 15:21:54 +02:00
|
|
|
timer_unset(timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|