2010-09-22 15:10:42 +02:00
|
|
|
/******************************************************************************
|
2013-06-18 17:21:38 +02:00
|
|
|
Copyright (C) 2013, Freie Universitaet Berlin (FUB). All rights reserved.
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
|
|
|
and Telematics group (http://cst.mi.fu-berlin.de).
|
|
|
|
-------------------------------------------------------------------------------
|
2013-03-07 20:51:26 +01:00
|
|
|
This file is part of RIOT.
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-18 17:21:38 +02:00
|
|
|
This file subject to the terms and conditions of the GNU Lesser General Public
|
|
|
|
License. See the file LICENSE in the top level directory for more details.
|
2010-09-22 15:10:42 +02:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <cpu.h>
|
|
|
|
#include <hwtimer.h>
|
|
|
|
#include <hwtimer_arch.h>
|
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2010-12-09 13:24:24 +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);
|
2013-05-24 02:20:54 +02:00
|
|
|
uint16_t overflow_interrupt[ARCH_MAXTIMERS+1];
|
|
|
|
uint16_t timer_round;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
static void TA0_disable_interrupt(short timer)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
volatile unsigned int *ptr = &TA0CCTL0 + (timer);
|
|
|
|
*ptr &= ~(CCIFG);
|
|
|
|
*ptr &= ~(CCIE);
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
static void TA0_enable_interrupt(short timer)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
volatile unsigned int *ptr = &TA0CCTL0 + (timer);
|
|
|
|
*ptr |= CCIE;
|
|
|
|
*ptr &= ~(CCIFG);
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
static void TA0_set_nostart(unsigned long value, short timer)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
volatile unsigned int *ptr = &TA0CCR0 + (timer);
|
|
|
|
*ptr = value;
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
static void TA0_set(unsigned long value, short timer)
|
|
|
|
{
|
2010-12-09 13:24:24 +01:00
|
|
|
DEBUG("Setting timer %u to %lu\n", timer, value);
|
2010-09-22 15:10:42 +02:00
|
|
|
TA0_set_nostart(value, timer);
|
|
|
|
TA0_enable_interrupt(timer);
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void TA0_unset(short timer)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
volatile unsigned int *ptr = &TA0CCR0 + (timer);
|
|
|
|
TA0_disable_interrupt(timer);
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
unsigned long hwtimer_arch_now()
|
|
|
|
{
|
2013-08-23 22:03:04 +02:00
|
|
|
return ((uint32_t)timer_round << 16)+TA0R;
|
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-05-24 02:20:54 +02:00
|
|
|
TA0_enable_interrupt(0);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_enable_interrupt(void)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (int i = 0; i < ARCH_MAXTIMERS; i++) {
|
2010-09-22 15:10:42 +02:00
|
|
|
TA0_enable_interrupt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_disable_interrupt(void)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (int i = 0; i < ARCH_MAXTIMERS; i++) {
|
2010-09-22 15:10:42 +02:00
|
|
|
TA0_disable_interrupt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-06-25 15:33:40 +02:00
|
|
|
uint16_t small_value = value % 0xFFFF;
|
|
|
|
overflow_interrupt[timer] = (uint16_t)(value >> 16);
|
2013-05-24 02:20:54 +02:00
|
|
|
TA0_set(small_value,timer);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void hwtimer_arch_unset(short timer)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
TA0_unset(timer);
|
|
|
|
}
|