mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-28 07:51:19 +01:00
Merge pull request #5254 from haukepetersen/opt_atmega_timer
cpu/atmega2560: reworked timer implementation
This commit is contained in:
commit
553d18ac9e
@ -51,6 +51,7 @@ extern "C" {
|
||||
* @brief xtimer configuration values
|
||||
* @{
|
||||
*/
|
||||
#define XTIMER_MASK (0xffff0000)
|
||||
#define XTIMER_SHIFT (2)
|
||||
#define XTIMER_SHIFT_ON_COMPARE (8)
|
||||
#define XTIMER_BACKOFF (40)
|
||||
|
||||
@ -31,109 +31,28 @@ extern "C" {
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Timer peripheral configuration
|
||||
* @brief Timer configuration
|
||||
*
|
||||
* The ATmega2560 has 6 timers. Timer0 and Timer2 are 8 Bit Timers,
|
||||
* Timer0 has special uses too and therefore we'll avoid using it.
|
||||
* Timer5 has special uses with certain Arduino Shields, too.
|
||||
* Therefore we'll also avoid using Timer5.
|
||||
* This results in the following mapping to use the left over 16 Bit
|
||||
* timers:
|
||||
*
|
||||
* Timer1 -> TIMER_0
|
||||
* Timer3 -> TIMER_1
|
||||
* Timer4 -> TIMER_2
|
||||
* The timer driver only supports the four 16-bit timers (Timer1, Timer3,
|
||||
* Timer4, Timer5), so those are the only onces we can use here.
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
#define TIMER_NUMOF (3U)
|
||||
#define TIMER_0_EN 1
|
||||
#define TIMER_1_EN 0
|
||||
#define TIMER_2_EN 0
|
||||
#define TIMER_NUMOF (2U)
|
||||
|
||||
/* Timer 0 configuration */
|
||||
#define TIMER_0_CHANNELS 3
|
||||
#define TIMER_0_MAX_VALUE (0xffff)
|
||||
#define TIMER_0 MEGA_TIMER1
|
||||
#define TIMER_0_MASK &TIMSK1
|
||||
#define TIMER_0_FLAG &TIFR1
|
||||
#define TIMER_0_ISRA TIMER1_COMPA_vect
|
||||
#define TIMER_0_ISRB TIMER1_COMPB_vect
|
||||
#define TIMER_0_ISRC TIMER1_COMPC_vect
|
||||
|
||||
/* Timer 0 register and flags */
|
||||
#define TIMER0_CONTROL_A TCCR1A
|
||||
#define TIMER0_CONTROL_B TCCR1B
|
||||
#define TIMER0_CONTROL_C TCCR1C
|
||||
#define TIMER0_COUNTER TCNT1
|
||||
#define TIMER0_IRQ_FLAG_REG TIFR1
|
||||
#define TIMER0_IRQ_MASK_REG TIMSK1
|
||||
#define TIMER0_COMP_A OCR1A
|
||||
#define TIMER0_COMP_B OCR1B
|
||||
#define TIMER0_COMP_C OCR1C
|
||||
#define TIMER0_COMP_A_FLAG OCF1A
|
||||
#define TIMER0_COMP_B_FLAG OCF1B
|
||||
#define TIMER0_COMP_C_FLAG OCF1C
|
||||
#define TIMER0_COMP_A_EN OCIE1A
|
||||
#define TIMER0_COMP_B_EN OCIE1B
|
||||
#define TIMER0_COMP_C_EN OCIE1C
|
||||
#define TIMER0_FREQ_16MHZ (0 << CS12) | (0 << CS11) | (1 << CS10)
|
||||
#define TIMER0_FREQ_2MHZ (0 << CS12) | (1 << CS11) | (0 << CS10)
|
||||
#define TIMER0_FREQ_250KHZ (0 << CS12) | (1 << CS11) | (1 << CS10)
|
||||
#define TIMER0_FREQ_DISABLE (0 << CS12) | (0 << CS11) | (0 << CS10)
|
||||
#define TIMER0_COMPA_ISR TIMER1_COMPA_vect
|
||||
#define TIMER0_COMPB_ISR TIMER1_COMPB_vect
|
||||
#define TIMER0_COMPC_ISR TIMER1_COMPC_vect
|
||||
|
||||
/* Timer 1 configuration */
|
||||
#define TIMER_1_CHANNELS 3
|
||||
#define TIMER_1_MAX_VALUE (0xffff)
|
||||
|
||||
/* Timer 1 register and flags */
|
||||
#define TIMER1_CONTROL_A TCCR3A
|
||||
#define TIMER1_CONTROL_B TCCR3B
|
||||
#define TIMER1_CONTROL_C TCCR3C
|
||||
#define TIMER1_COUNTER TCNT3
|
||||
#define TIMER1_IRQ_FLAG_REG TIFR3
|
||||
#define TIMER1_IRQ_MASK_REG TIMSK3
|
||||
#define TIMER1_COMP_A OCR3A
|
||||
#define TIMER1_COMP_B OCR3B
|
||||
#define TIMER1_COMP_C OCR3C
|
||||
#define TIMER1_COMP_A_FLAG OCF3A
|
||||
#define TIMER1_COMP_B_FLAG OCF3B
|
||||
#define TIMER1_COMP_C_FLAG OCF3C
|
||||
#define TIMER1_COMP_A_EN OCIE3A
|
||||
#define TIMER1_COMP_B_EN OCIE3B
|
||||
#define TIMER1_COMP_C_EN OCIE3C
|
||||
#define TIMER1_FREQ_16MHZ (0 << CS32) | (0 << CS31) | (1 << CS30)
|
||||
#define TIMER1_FREQ_2MHZ (0 << CS32) | (1 << CS31) | (0 << CS30)
|
||||
#define TIMER1_FREQ_250KHZ (0 << CS32) | (1 << CS31) | (1 << CS30)
|
||||
#define TIMER1_FREQ_DISABLE (0 << CS32) | (0 << CS31) | (0 << CS30)
|
||||
#define TIMER1_COMPA_ISR TIMER3_COMPA_vect
|
||||
#define TIMER1_COMPB_ISR TIMER3_COMPB_vect
|
||||
#define TIMER1_COMPC_ISR TIMER3_COMPC_vect
|
||||
|
||||
/* Timer 2 configuration */
|
||||
#define TIMER_2_CHANNELS 3
|
||||
#define TIMER_2_MAX_VALUE (0xffff)
|
||||
|
||||
/* Timer 2 register and flags */
|
||||
#define TIMER2_CONTROL_A TCCR4A
|
||||
#define TIMER2_CONTROL_B TCCR4B
|
||||
#define TIMER2_CONTROL_C TCCR4C
|
||||
#define TIMER2_COUNTER TCNT4
|
||||
#define TIMER2_IRQ_FLAG_REG TIFR4
|
||||
#define TIMER2_IRQ_MASK_REG TIMSK4
|
||||
#define TIMER2_COMP_A OCR4A
|
||||
#define TIMER2_COMP_B OCR4B
|
||||
#define TIMER2_COMP_C OCR4C
|
||||
#define TIMER2_COMP_A_FLAG OCF4A
|
||||
#define TIMER2_COMP_B_FLAG OCF4B
|
||||
#define TIMER2_COMP_C_FLAG OCF4C
|
||||
#define TIMER2_COMP_A_EN OCIE4A
|
||||
#define TIMER2_COMP_B_EN OCIE4B
|
||||
#define TIMER2_COMP_C_EN OCIE4C
|
||||
#define TIMER2_FREQ_16MHZ (0 << CS42) | (0 << CS41) | (1 << CS40)
|
||||
#define TIMER2_FREQ_2MHZ (0 << CS42) | (1 << CS41) | (0 << CS40)
|
||||
#define TIMER2_FREQ_250KHZ (0 << CS42) | (1 << CS41) | (1 << CS40)
|
||||
#define TIMER2_FREQ_DISABLE (0 << CS42) | (0 << CS41) | (0 << CS40)
|
||||
#define TIMER2_COMPA_ISR TIMER4_COMPA_vect
|
||||
#define TIMER2_COMPB_ISR TIMER4_COMPB_vect
|
||||
#define TIMER2_COMPC_ISR TIMER4_COMPC_vect
|
||||
#define TIMER_1 MEGA_TIMER4
|
||||
#define TIMER_1_MASK &TIMSK4
|
||||
#define TIMER_1_FLAG &TIFR4
|
||||
#define TIMER_1_ISRA TIMER4_COMPA_vect
|
||||
#define TIMER_1_ISRB TIMER4_COMPB_vect
|
||||
#define TIMER_1_ISRC TIMER4_COMPC_vect
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
|
||||
74
cpu/atmega2560/include/atmega2560_regs.h
Normal file
74
cpu/atmega2560/include/atmega2560_regs.h
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_atmega2560
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief CMSIS style register definitions for the atmega2560
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef ATMEGA2560_REGS_H
|
||||
#define ATMEGA2560_REGS_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Register types
|
||||
* @{
|
||||
*/
|
||||
#define REG8 volatile uint8_t
|
||||
#define REG16 volatile uint16_t
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Timer register map
|
||||
*/
|
||||
typedef struct {
|
||||
REG8 CRA; /**< control A */
|
||||
REG8 CRB; /**< control B */
|
||||
REG8 CRC; /**< control C */
|
||||
REG8 reserved; /**< reserved */
|
||||
REG16 CNT; /**< counter */
|
||||
REG16 ICR; /**< input capture */
|
||||
REG16 OCR[3]; /**< output compare */
|
||||
} mega_timer_t;
|
||||
|
||||
/**
|
||||
* @brief Base register address definitions
|
||||
* @{
|
||||
*/
|
||||
#define MEGA_TIMER1_BASE (uint16_t *)(&TCCR1A)
|
||||
#define MEGA_TIMER3_BASE (uint16_t *)(&TCCR3A)
|
||||
#define MEGA_TIMER4_BASE (uint16_t *)(&TCCR4A)
|
||||
#define MEGA_TIMER5_BASE (uint16_t *)(&TCCR5A)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Peripheral instances
|
||||
* @{
|
||||
*/
|
||||
#define MEGA_TIMER1 ((mega_timer_t *)MEGA_TIMER1_BASE)
|
||||
#define MEGA_TIMER3 ((mega_timer_t *)MEGA_TIMER3_BASE)
|
||||
#define MEGA_TIMER4 ((mega_timer_t *)MEGA_TIMER4_BASE)
|
||||
#define MEGA_TIMER5 ((mega_timer_t *)MEGA_TIMER5_BASE)
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ATMEGA2560_REGS_H */
|
||||
/** @} */
|
||||
@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_atmega2560
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
@ -19,6 +20,7 @@
|
||||
#ifndef __CPU_CONF_H
|
||||
#define __CPU_CONF_H
|
||||
|
||||
#include "atmega2560_regs.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
@ -19,9 +19,6 @@
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "board.h"
|
||||
@ -31,449 +28,231 @@
|
||||
#include "periph/timer.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
#define IRQ_DISABLED 0x00
|
||||
/**
|
||||
* @brief All timers have three channels
|
||||
*/
|
||||
#define CHANNELS (3)
|
||||
|
||||
/**
|
||||
* @brief Timer state memory
|
||||
* @brief We have 5 possible prescaler values
|
||||
*/
|
||||
static timer_isr_ctx_t config[TIMER_NUMOF];
|
||||
#define PRESCALE_NUMOF (5U)
|
||||
|
||||
/**
|
||||
* @brief Possible prescaler values, encoded as 2 ^ val
|
||||
*/
|
||||
static const uint8_t prescalers[] = { 0, 3, 6, 8, 10 };
|
||||
|
||||
/**
|
||||
* @brief Timer state context
|
||||
*/
|
||||
typedef struct {
|
||||
mega_timer_t *dev; /**< timer device */
|
||||
volatile uint8_t *mask; /**< address of interrupt mask register */
|
||||
volatile uint8_t *flag; /**< address of interrupt flag register */
|
||||
timer_cb_t cb; /**< interrupt callback */
|
||||
void *arg; /**< interrupt callback argument */
|
||||
uint8_t mode; /**< remember the configured mode */
|
||||
uint8_t isrs; /**< remember the interrupt state */
|
||||
} ctx_t;
|
||||
|
||||
/**
|
||||
* @brief Allocate memory for saving the device states
|
||||
* @{
|
||||
*/
|
||||
#if TIMER_NUMOF
|
||||
static ctx_t ctx[] = {
|
||||
#ifdef TIMER_0
|
||||
{ TIMER_0, TIMER_0_MASK, TIMER_0_FLAG, NULL, NULL, 0, 0 },
|
||||
#endif
|
||||
#ifdef TIMER_1
|
||||
{ TIMER_1, TIMER_1_MASK, TIMER_1_FLAG, NULL, NULL, 0, 0 },
|
||||
#endif
|
||||
#ifdef TIMER_2
|
||||
{ TIMER_2, TIMER_2_MASK, TIMER_2_FLAG, NULL, NULL, 0, 0 },
|
||||
#endif
|
||||
#ifdef TIMER_3
|
||||
{ TIMER_3, TIMER_3_MASK, TIMER_3_FLAG, NULL, NULL, 0, 0 },
|
||||
#endif
|
||||
};
|
||||
#else
|
||||
/* fallback if no timer is configured */
|
||||
static ctx_t *ctx[] = {{ NULL }};
|
||||
#endif
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Setup the given timer
|
||||
*
|
||||
*/
|
||||
int timer_init(tim_t dev, unsigned long freq, timer_cb_t cb, void *arg)
|
||||
int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
|
||||
{
|
||||
/* reject impossible freq values
|
||||
* todo: Add support for 2 MHz and 16 MHz */
|
||||
if ((freq != 250000ul)) {
|
||||
uint8_t pre = 0;
|
||||
|
||||
/* make sure given device is valid */
|
||||
if (tim >= TIMER_NUMOF) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* select the timer and enable the timer specific peripheral clocks */
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
|
||||
case TIMER_0:
|
||||
TIMER0_COUNTER = 0;
|
||||
TIMER0_CONTROL_B |= TIMER0_FREQ_250KHZ;
|
||||
/* figure out if freq is applicable */
|
||||
for (; pre < PRESCALE_NUMOF; pre++) {
|
||||
if ((CLOCK_CORECLOCK >> prescalers[pre]) == freq) {
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
|
||||
case TIMER_1:
|
||||
TIMER1_COUNTER = 0;
|
||||
TIMER1_CONTROL_B |= TIMER1_FREQ_250KHZ;
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
|
||||
case TIMER_2:
|
||||
TIMER2_COUNTER = 0;
|
||||
TIMER2_CONTROL_B |= TIMER2_FREQ_250KHZ;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (pre == PRESCALE_NUMOF) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* save callback */
|
||||
config[dev].cb = cb;
|
||||
config[dev].arg = arg;
|
||||
/* stop and reset timer */
|
||||
ctx[tim].dev->CRA = 0;
|
||||
ctx[tim].dev->CRB = 0;
|
||||
ctx[tim].dev->CRC = 0;
|
||||
ctx[tim].dev->CNT = 0;
|
||||
|
||||
/* save interrupt context and timer mode */
|
||||
ctx[tim].cb = cb;
|
||||
ctx[tim].arg = arg;
|
||||
ctx[tim].mode = (pre + 1);
|
||||
|
||||
/* enable timer with calculated prescaler */
|
||||
ctx[tim].dev->CRB = (pre + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_set(tim_t dev, int channel, unsigned int timeout)
|
||||
int timer_set(tim_t tim, int channel, unsigned int timeout)
|
||||
{
|
||||
return timer_set_absolute(dev, channel, timer_read(dev) + timeout);
|
||||
return timer_set_absolute(tim, channel, timer_read(tim) + timeout);
|
||||
}
|
||||
|
||||
int timer_set_absolute(tim_t dev, int channel, unsigned int value)
|
||||
int timer_set_absolute(tim_t tim, int channel, unsigned int value)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER0_COMP_A = (uint16_t) value;
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_A_FLAG);
|
||||
TIMER0_IRQ_MASK_REG |= (1 << TIMER0_COMP_A_EN);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER0_COMP_B = (uint16_t) value;
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_B_FLAG);
|
||||
TIMER0_IRQ_MASK_REG |= (1 << TIMER0_COMP_B_EN);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER0_COMP_C = (uint16_t) value;
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_C_FLAG);
|
||||
TIMER0_IRQ_MASK_REG |= (1 << TIMER0_COMP_C_EN);
|
||||
break;
|
||||
|
||||
default:
|
||||
irq_restore(state);
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
case TIMER_1:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER1_COMP_A = (uint16_t) value;
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_A_FLAG);
|
||||
TIMER1_IRQ_MASK_REG |= (1 << TIMER1_COMP_A_EN);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER1_COMP_B = (uint16_t) value;
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_B_FLAG);
|
||||
TIMER1_IRQ_MASK_REG |= (1 << TIMER1_COMP_B_EN);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER1_COMP_C = (uint16_t) value;
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_C_FLAG);
|
||||
TIMER1_IRQ_MASK_REG |= (1 << TIMER1_COMP_C_EN);
|
||||
break;
|
||||
|
||||
default:
|
||||
irq_restore(state);
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
case TIMER_2:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER2_COMP_A = (uint16_t) value;
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_A_FLAG);
|
||||
TIMER2_IRQ_MASK_REG |= (1 << TIMER2_COMP_A_EN);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER2_COMP_B = (uint16_t) value;
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_B_FLAG);
|
||||
TIMER2_IRQ_MASK_REG |= (1 << TIMER2_COMP_B_EN);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER2_COMP_C = (uint16_t) value;
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_C_FLAG);
|
||||
TIMER2_IRQ_MASK_REG |= (1 << TIMER2_COMP_C_EN);
|
||||
break;
|
||||
|
||||
default:
|
||||
irq_restore(state);
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
irq_restore(state);
|
||||
return -1;
|
||||
if (channel >= CHANNELS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* enable interrupts for given timer */
|
||||
timer_irq_enable(dev);
|
||||
irq_restore(state);
|
||||
ctx[tim].dev->OCR[channel] = (uint16_t)value;
|
||||
*ctx[tim].flag &= ~(1 << (channel + OCF1A));
|
||||
*ctx[tim].mask |= (1 << (channel + OCIE1A));
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_clear(tim_t dev, int channel)
|
||||
int timer_clear(tim_t tim, int channel)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_A_FLAG);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_B_FLAG);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER0_IRQ_FLAG_REG &= ~(1 << TIMER0_COMP_C_FLAG);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
case TIMER_1:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_A_FLAG);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_B_FLAG);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER1_IRQ_FLAG_REG &= ~(1 << TIMER1_COMP_C_FLAG);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
|
||||
case TIMER_2:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_A_FLAG);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_B_FLAG);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
TIMER2_IRQ_FLAG_REG &= ~(1 << TIMER2_COMP_C_FLAG);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
if (channel >= CHANNELS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
timer_irq_disable(dev);
|
||||
return 1;
|
||||
*ctx[tim].mask &= ~(1 << (channel + OCIE1A));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int timer_read(tim_t dev)
|
||||
unsigned int timer_read(tim_t tim)
|
||||
{
|
||||
uint16_t a;
|
||||
uint16_t b;
|
||||
/*
|
||||
* Disabling interrupts globally because read from 16 Bit register can
|
||||
* otherwise be messed up
|
||||
*/
|
||||
unsigned state = irq_disable();
|
||||
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
|
||||
case TIMER_0:
|
||||
do {
|
||||
a = TIMER0_COUNTER;
|
||||
b = TIMER0_COUNTER;
|
||||
} while (a != b);
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
|
||||
case TIMER_1:
|
||||
do {
|
||||
a = TIMER1_COUNTER;
|
||||
b = TIMER1_COUNTER;
|
||||
} while (a != b);
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
|
||||
case TIMER_2:
|
||||
do {
|
||||
a = TIMER2_COUNTER;
|
||||
b = TIMER2_COUNTER;
|
||||
} while (a != b);
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
(void)b;
|
||||
a = 0;
|
||||
}
|
||||
|
||||
irq_restore(state);
|
||||
return a;
|
||||
return (unsigned int)ctx[tim].dev->CNT;
|
||||
}
|
||||
|
||||
void timer_stop(tim_t dev)
|
||||
void timer_stop(tim_t tim)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
|
||||
case TIMER_0:
|
||||
TIMER0_CONTROL_B = TIMER0_FREQ_DISABLE;
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
|
||||
case TIMER_1:
|
||||
TIMER1_CONTROL_B = TIMER1_FREQ_DISABLE;
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
|
||||
case TIMER_2:
|
||||
TIMER2_CONTROL_B = TIMER2_FREQ_DISABLE;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
ctx[tim].dev->CRB = 0;
|
||||
}
|
||||
|
||||
void timer_start(tim_t dev)
|
||||
void timer_start(tim_t tim)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
|
||||
case TIMER_0:
|
||||
TIMER0_CONTROL_B |= TIMER0_FREQ_250KHZ;
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
|
||||
case TIMER_1:
|
||||
TIMER1_CONTROL_B |= TIMER1_FREQ_250KHZ;
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
|
||||
case TIMER_2:
|
||||
TIMER1_CONTROL_B |= TIMER1_FREQ_250KHZ;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
ctx[tim].dev->CRB = ctx[tim].mode;
|
||||
}
|
||||
|
||||
void timer_irq_enable(tim_t dev)
|
||||
void timer_irq_enable(tim_t tim)
|
||||
{
|
||||
(void) dev;
|
||||
#ifdef DEVELHELP
|
||||
printf("timer_irq_enable not implemented\n");
|
||||
#endif
|
||||
*ctx[tim].mask = ctx[tim].isrs;
|
||||
}
|
||||
|
||||
void timer_irq_disable(tim_t dev)
|
||||
void timer_irq_disable(tim_t tim)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
TIMER0_IRQ_MASK_REG &= ~(1 << TIMER0_COMP_A_EN);
|
||||
TIMER0_IRQ_MASK_REG &= ~(1 << TIMER0_COMP_B_EN);
|
||||
TIMER0_IRQ_MASK_REG &= ~(1 << TIMER0_COMP_C_EN);
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_1_EN
|
||||
case TIMER_1:
|
||||
TIMER1_IRQ_MASK_REG &= ~(1 << TIMER1_COMP_A_EN);
|
||||
TIMER1_IRQ_MASK_REG &= ~(1 << TIMER1_COMP_B_EN);
|
||||
TIMER1_IRQ_MASK_REG &= ~(1 << TIMER1_COMP_C_EN);
|
||||
break;
|
||||
#endif
|
||||
#if TIMER_2_EN
|
||||
case TIMER_2:
|
||||
TIMER2_IRQ_MASK_REG &= ~(1 << TIMER2_COMP_A_EN);
|
||||
TIMER2_IRQ_MASK_REG &= ~(1 << TIMER2_COMP_B_EN);
|
||||
TIMER2_IRQ_MASK_REG &= ~(1 << TIMER2_COMP_C_EN);
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
ctx[tim].isrs = *(ctx[tim].mask);
|
||||
*ctx[tim].mask = 0;
|
||||
}
|
||||
|
||||
static inline void _isr(int timer, int chan)
|
||||
static inline void _isr(int num, int chan)
|
||||
{
|
||||
__enter_isr();
|
||||
timer_clear(timer, chan);
|
||||
|
||||
config[timer].cb(config[timer].arg, chan);
|
||||
*ctx[num].mask &= ~(1 << (chan + OCIE1A));
|
||||
ctx[num].cb(ctx[num].arg, chan);
|
||||
|
||||
if (sched_context_switch_request) {
|
||||
thread_yield();
|
||||
}
|
||||
|
||||
__exit_isr();
|
||||
}
|
||||
|
||||
#if TIMER_0_EN
|
||||
ISR(TIMER0_COMPA_ISR, ISR_BLOCK)
|
||||
#ifdef TIMER_0
|
||||
ISR(TIMER_0_ISRA, ISR_BLOCK)
|
||||
{
|
||||
_isr(0, 0);
|
||||
}
|
||||
|
||||
ISR(TIMER0_COMPB_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_0_ISRB, ISR_BLOCK)
|
||||
{
|
||||
_isr(0, 1);
|
||||
}
|
||||
|
||||
ISR(TIMER0_COMPC_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_0_ISRC, ISR_BLOCK)
|
||||
{
|
||||
_isr(0, 2);
|
||||
}
|
||||
#endif /* TIMER_0_EN */
|
||||
#endif /* TIMER_0 */
|
||||
|
||||
#if TIMER_1_EN
|
||||
ISR(TIMER1_COMPA_ISR, ISR_BLOCK)
|
||||
#ifdef TIMER_1
|
||||
ISR(TIMER_1_ISRA, ISR_BLOCK)
|
||||
{
|
||||
_isr(1, 0);
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPB_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_1_ISRB, ISR_BLOCK)
|
||||
{
|
||||
_isr(1, 1);
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPC_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_1_ISRC, ISR_BLOCK)
|
||||
{
|
||||
_isr(1, 2);
|
||||
}
|
||||
#endif /* TIMER_1_EN */
|
||||
#endif /* TIMER_1 */
|
||||
|
||||
#if TIMER_2_EN
|
||||
ISR(TIMER2_COMPA_ISR, ISR_BLOCK)
|
||||
#ifdef TIMER_2
|
||||
ISR(TIMER_2_ISRA, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 0);
|
||||
}
|
||||
|
||||
ISR(TIMER2_COMPB_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_2_ISRB, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 1);
|
||||
}
|
||||
|
||||
ISR(TIMER2_COMPC_ISR, ISR_BLOCK)
|
||||
ISR(TIMER_2_ISRC, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 2);
|
||||
}
|
||||
#endif /* TIMER_2_EN */
|
||||
#endif /* TIMER_2 */
|
||||
|
||||
#ifdef TIMER_3
|
||||
ISR(TIMER_3_ISRA, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 0);
|
||||
}
|
||||
|
||||
ISR(TIMER_3_ISRB, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 1);
|
||||
}
|
||||
|
||||
ISR(TIMER_3_ISRC, ISR_BLOCK)
|
||||
{
|
||||
_isr(2, 2);
|
||||
}
|
||||
#endif /* TIMER_3 */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user