Skip to content

Commit

Permalink
Merge pull request RIOT-OS#909 from OlegHahm/msp430_hwtimer
Browse files Browse the repository at this point in the history
msp430: hwtimer: handle overflow correctly
  • Loading branch information
rousselk committed Mar 31, 2014
2 parents 5d00dbf + c25b785 commit dea33bd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
34 changes: 19 additions & 15 deletions cpu/cc430/hwtimer_cc430.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
*
*/

#include <legacymsp430.h>
#include "board.h"
#include "cpu.h"
#include "hwtimer.h"
#include "hwtimer_arch.h"
#include "cpu.h"

#define ENABLE_DEBUG (0)
#include "debug.h"
Expand All @@ -33,15 +31,18 @@ static uint32_t ticks = 0;

extern void (*int_handler)(int);
extern void timer_unset(short timer);
extern uint16_t overflow_interrupt[HWTIMER:_MAXTIMERS+1];
extern uint16_t timer_round;

void timerA_init(void)
{
ticks = 0; // Set tick counter value to 0
TA0CTL = TASSEL_1 + TACLR; // Clear the timer counter, set ACLK
TA0CTL &= ~TAIE; // Clear the IFG

volatile unsigned int *ccr = &TA0CCR0;
volatile unsigned int *ctl = &TA0CCTL0;
ticks = 0; /* Set tick counter value to 0 */
timer_round = 0; /* Set to round 0 */
TA0CTL = TASSEL_1 + TACLR; /* Clear the timer counter, set ACLK */
TA0CTL &= ~TAIFG; /* Clear the IFG */
TA0CTL |= TAIE; /* Enable TAIE (overflow IRQ) */

for (int i = 0; i < HWTIMER_MAXTIMERS; i++) {
*(ccr + i) = 0;
Expand All @@ -55,9 +56,10 @@ void timerA_init(void)
interrupt(TIMER0_A0_VECTOR) __attribute__((naked)) timer0_a0_isr(void)
{
__enter_isr();

timer_unset(0);
int_handler(0);
if (overflow_interrupt[0] == timer_round) {
timer_unset(0);
int_handler(0);
}
__exit_isr();
}

Expand All @@ -66,13 +68,15 @@ interrupt(TIMER0_A1_VECTOR) __attribute__((naked)) timer0_a1_5_isr(void)
__enter_isr();

short taiv = TA0IV;
short timer;

if (taiv & TAIFG) {
short timer = taiv / 2;
/* TAIV = 0x0E means overflow */
if (taiv == 0x0E) {
DEBUG("Overflow\n");
timer_round += 1;
}
else {
timer = (taiv / 2);
/* check which CCR has been hit and if the overflow counter for this timer
* has been reached */
else if (overflow_interrupt[timer] == timer_round) {
timer_unset(timer);
int_handler(timer);
}
Expand Down
7 changes: 3 additions & 4 deletions cpu/msp430-common/hwtimer_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void timer_set_nostart(unsigned long value, short timer)

static void timer_set(unsigned long value, short timer)
{
DEBUG("Setting timer %u to %lu\n", timer, value);
DEBUG("Setting timer %u to %u overflows and %lu\n", timer, overflow_interrupt[timer], value);
timer_set_nostart(value, timer);
timer_enable_interrupt(timer);
}
Expand All @@ -82,7 +82,6 @@ void hwtimer_arch_init(void (*handler)(int), uint32_t fcpu)
(void) fcpu;
timerA_init();
int_handler = handler;
timer_enable_interrupt(0);
}

void hwtimer_arch_enable_interrupt(void)
Expand All @@ -107,9 +106,9 @@ void hwtimer_arch_set(unsigned long offset, short timer)

void hwtimer_arch_set_absolute(unsigned long value, short timer)
{
uint16_t small_value = value % 0xFFFF;
uint16_t small_value = value & 0xFFFF;
overflow_interrupt[timer] = (uint16_t)(value >> 16);
timer_set(small_value,timer);
timer_set(small_value, timer);
}

void hwtimer_arch_unset(short timer)
Expand Down
35 changes: 23 additions & 12 deletions cpu/msp430x16x/hwtimer_msp430.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "hwtimer.h"
#include "hwtimer_arch.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

static uint32_t ticks = 0;

extern void (*int_handler)(int);
Expand All @@ -35,11 +38,11 @@ void timerA_init(void)
{
volatile unsigned int *ccr;
volatile unsigned int *ctl;
ticks = 0; // Set tick counter value to 0
timer_round = 0; // Set to round 0
TACTL = TASSEL_1 + TACLR; // Clear the timer counter, set ACLK
TACTL &= ~TAIFG; // Clear the IFG
TACTL &= ~TAIE; // Clear the IFG
ticks = 0; /* Set tick counter value to 0 */
timer_round = 0; /* Set to round 0 */
TACTL = TASSEL_1 + TACLR; /* Clear the timer counter, set ACLK */
TACTL &= ~TAIFG; /* Clear the IFG */
TACTL |= TAIE; /* Enable TAIE (overflow IRQ) */

for (int i = 0; i < HWTIMER_MAXTIMERS; i++) {
ccr = &TACCR0 + (i);
Expand All @@ -55,7 +58,10 @@ void timerA_init(void)
interrupt(TIMERA0_VECTOR) __attribute__((naked)) timer_isr_ccr0(void)
{
__enter_isr();
timer_round += 1;
if (overflow_interrupt[0] == timer_round) {
timer_unset(0);
int_handler(0);
}
__exit_isr();
}

Expand All @@ -64,12 +70,17 @@ interrupt(TIMERA1_VECTOR) __attribute__((naked)) timer_isr(void)
__enter_isr();

short taiv = TAIV;
if (!(taiv & TAIV_TAIFG)) {
short timer = taiv / 2;
if (overflow_interrupt[timer] == timer_round) {
timer_unset(timer);
int_handler(timer);
}
short timer = taiv / 2;
/* TAIV = 0x0A means overflow */
if (taiv == 0x0A) {
DEBUG("Overflow\n");
timer_round += 1;
}
/* check which CCR has been hit and if the overflow counter for this timer
* has been reached */
else if (overflow_interrupt[timer] == timer_round) {
timer_unset(timer);
int_handler(timer);
}

__exit_isr();
Expand Down
5 changes: 0 additions & 5 deletions tests/test_hwtimer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ int main(void)
{
puts("hwtimer test application...");

puts("Initializing hwtimer...");
hwtimer_init();

puts("Initializing hwtimer [OK].");

puts("");
puts(" Timers should print \"callback x\" once when they run out.");
printf(" The order for x is 1, n-1, n-2, ..., 2 where n is the number of available hardware timers (%u on this platform).\n", HWTIMER_MAXTIMERS);
Expand Down

0 comments on commit dea33bd

Please sign in to comment.