2015-07-04 09:37:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Rakendra Thapa <rakendrathapa@gmail.com
|
2015-10-05 21:12:10 +02:00
|
|
|
* 2015 Marc Poulhiès <dkm@kataplop.net>
|
2015-07-04 09:37:54 +02:00
|
|
|
*
|
|
|
|
* 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_lm4f120
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_timer
|
2015-07-04 09:37:54 +02:00
|
|
|
* @{
|
|
|
|
*
|
2015-07-16 02:56:33 +02:00
|
|
|
* @file timer.c
|
2015-07-04 09:37:54 +02:00
|
|
|
* @brief Implementation of the low-level timer driver for the LM4F120
|
|
|
|
*
|
|
|
|
* @author Rakendra Thapa <rakendrathapa@gmail.com>
|
2015-10-05 21:12:10 +02:00
|
|
|
* Marc Poulhiès <dkm@kataplop.net>
|
2015-07-04 09:37:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/timer.h"
|
2015-07-10 04:06:50 +02:00
|
|
|
#include "mutex.h"
|
2015-07-04 09:37:54 +02:00
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2015-07-10 04:06:50 +02:00
|
|
|
#include "debug.h"
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2015-07-04 09:37:54 +02:00
|
|
|
/**
|
2020-09-09 14:35:29 +02:00
|
|
|
* @brief Interrupt context for each configured timer
|
2015-07-16 02:56:33 +02:00
|
|
|
* @{
|
2015-07-04 09:37:54 +02:00
|
|
|
*/
|
2020-09-09 14:35:29 +02:00
|
|
|
static timer_isr_ctx_t isr_ctx[TIMER_NUMOF];
|
|
|
|
static uint32_t isr_divisor[TIMER_NUMOF];
|
2015-07-16 02:56:33 +02:00
|
|
|
/**@}*/
|
2015-07-04 09:37:54 +02:00
|
|
|
|
2015-10-05 21:12:10 +02:00
|
|
|
#include "hw_timer.h"
|
|
|
|
|
2017-01-14 15:34:53 +01:00
|
|
|
/* enable timer interrupts */
|
2020-09-09 14:35:29 +02:00
|
|
|
static inline void _irq_enable(tim_t tim);
|
2017-01-14 15:34:53 +01:00
|
|
|
|
2015-10-05 21:12:10 +02:00
|
|
|
/* Missing from driverlib */
|
|
|
|
static inline unsigned long
|
|
|
|
PRIV_TimerPrescaleSnapshotGet(unsigned long ulbase, unsigned long ultimer) {
|
2020-09-09 14:35:29 +02:00
|
|
|
return ((ultimer == TIMER_A) ? HWREG(ulbase + TIMER_O_TAPS) :
|
|
|
|
HWREG(ulbase + TIMER_O_TBPS));
|
2015-10-05 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
static inline uint64_t _scaled_to_ll_value(uint32_t uncorrected,
|
|
|
|
uint32_t divisor)
|
2015-10-05 21:12:10 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
const uint64_t scaledv = (uint64_t) uncorrected * divisor;
|
2015-10-05 21:12:10 +02:00
|
|
|
return scaledv;
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
static inline uint32_t _llvalue_to_scaled_value(uint64_t corrected,
|
|
|
|
uint32_t divisor)
|
2015-10-05 21:12:10 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
const uint64_t scaledv = corrected / divisor;
|
2015-10-05 21:12:10 +02:00
|
|
|
return scaledv;
|
|
|
|
}
|
|
|
|
|
2020-10-30 12:14:35 +01:00
|
|
|
int timer_init(tim_t tim, uint32_t freq, timer_cb_t cb, void *arg)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF){
|
2015-10-05 21:12:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2020-09-09 14:35:29 +02:00
|
|
|
const timer_conf_t *cfg = &timer_config[tim];
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
isr_ctx[tim].cb = cb;
|
|
|
|
isr_ctx[tim].arg = arg;
|
|
|
|
isr_divisor[tim] = ROM_SysCtlClockGet() / freq;
|
2015-10-05 21:12:10 +02:00
|
|
|
|
|
|
|
unsigned int timer_side = TIMER_A;
|
2020-09-09 14:35:29 +02:00
|
|
|
unsigned int timer_cfg = TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC_UP |
|
|
|
|
TIMER_TAMR_TAMIE;
|
2015-10-05 21:12:10 +02:00
|
|
|
unsigned int timer_intbit = TIMER_TIMA_TIMEOUT | TIMER_TIMA_MATCH;
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_SysCtlPeripheralEnable(cfg->sysctl);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerDisable(cfg->dev, timer_side);
|
|
|
|
ROM_TimerConfigure(cfg->dev, timer_cfg);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
uint64_t val_max = _scaled_to_ll_value(cfg->max, isr_divisor[tim]);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerPrescaleSet(cfg->dev, timer_side, val_max >> 32);
|
|
|
|
ROM_TimerLoadSet(cfg->dev, timer_side, val_max & 0xFFFFFFFF);
|
|
|
|
ROM_TimerIntClear(cfg->dev, timer_intbit);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerIntEnable(cfg->dev, timer_intbit);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
_irq_enable(tim);
|
|
|
|
timer_start(tim);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
int timer_set_absolute(tim_t tim, int channel, unsigned int value)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF || channel >= timer_config[tim].channels) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const timer_conf_t *cfg = &timer_config[tim];
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
|
2015-10-05 21:12:10 +02:00
|
|
|
unsigned int timer_side = TIMER_A;
|
|
|
|
unsigned long long scaledv;
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerDisable(cfg->dev, timer_side);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
scaledv = _scaled_to_ll_value(value, isr_divisor[tim]);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
|
|
|
if (scaledv>>32){
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerPrescaleMatchSet(cfg->dev, timer_side, scaledv >> 32);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2015-10-05 21:12:10 +02:00
|
|
|
else {
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerPrescaleMatchSet(cfg->dev, timer_side, 0);
|
2015-10-05 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerMatchSet(cfg->dev, timer_side, (uint32_t) (scaledv & 0xFFFFFFFF));
|
|
|
|
ROM_TimerEnable(cfg->dev, timer_side);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2019-09-11 13:46:55 +02:00
|
|
|
return 0;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
int timer_clear(tim_t tim, int channel)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF || channel >= timer_config[tim].channels) {
|
2015-10-05 21:12:10 +02:00
|
|
|
return -1;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerIntClear(timer_config[tim].dev, TIMER_TIMA_TIMEOUT);
|
2019-09-11 13:46:55 +02:00
|
|
|
return 0;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
unsigned int timer_read(tim_t tim)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2015-10-05 21:12:10 +02:00
|
|
|
unsigned int timer_side = TIMER_A;
|
|
|
|
unsigned long long high_bits, high_bits_dup;
|
|
|
|
unsigned long long low_bits;
|
|
|
|
unsigned long long total;
|
|
|
|
unsigned int scaled_value;
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF){
|
2015-10-05 21:12:10 +02:00
|
|
|
return -1;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
const timer_conf_t *cfg = &timer_config[tim];
|
2015-10-05 21:12:10 +02:00
|
|
|
|
|
|
|
/* handle overflow happening between the 2 register reads */
|
|
|
|
do {
|
2020-09-09 14:35:29 +02:00
|
|
|
high_bits = ((uint64_t)PRIV_TimerPrescaleSnapshotGet(cfg->dev,
|
|
|
|
timer_side)) << 32;
|
|
|
|
low_bits = (uint64_t)ROM_TimerValueGet(cfg->dev, timer_side);
|
|
|
|
high_bits_dup = ((uint64_t)PRIV_TimerPrescaleSnapshotGet(cfg->dev,
|
|
|
|
timer_side)) << 32;
|
2015-10-05 21:12:10 +02:00
|
|
|
} while (high_bits != high_bits_dup);
|
|
|
|
|
|
|
|
total = high_bits + low_bits;
|
2020-09-09 14:35:29 +02:00
|
|
|
DEBUG("Combined %lx:%lx\n", (uint32_t)(total>>32),
|
|
|
|
(uint32_t)(total & 0xFFFFFFFF));
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
scaled_value = _llvalue_to_scaled_value(total, isr_divisor[tim]);
|
2015-10-05 21:12:10 +02:00
|
|
|
|
|
|
|
return scaled_value;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
void timer_start(tim_t tim)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF){
|
|
|
|
return;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerEnable(timer_config[tim].dev, TIMER_A);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
void timer_stop(tim_t tim)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF){
|
2015-10-05 21:12:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerDisable(timer_config[tim].dev, TIMER_A);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
static inline void _irq_enable(tim_t tim)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
if (tim >= TIMER_NUMOF){
|
2015-10-05 21:12:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_IntPrioritySet(timer_config[tim].intbase, 32);
|
|
|
|
ROM_IntEnable(timer_config[tim].intbase);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
void _isr_timer(tim_t tim)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2015-10-05 21:12:10 +02:00
|
|
|
/* Clears both IT */
|
2020-09-09 14:35:29 +02:00
|
|
|
ROM_TimerIntClear(timer_config[tim].dev,
|
|
|
|
TIMER_TIMA_TIMEOUT | TIMER_TIMA_MATCH);
|
|
|
|
isr_ctx[tim].cb(isr_ctx[tim].arg, 0);
|
2016-11-30 18:26:05 +01:00
|
|
|
cortexm_isr_end();
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2015-10-05 21:12:10 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
#ifdef TIMER_0_ISR
|
|
|
|
void TIMER_0_ISR(void)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2020-09-09 14:35:29 +02:00
|
|
|
_isr_timer(0);
|
|
|
|
}
|
|
|
|
#endif /* TIMER_0_ISR */
|
2015-07-15 11:48:31 +02:00
|
|
|
|
2020-09-09 14:35:29 +02:00
|
|
|
#ifdef TIMER_1_ISR
|
|
|
|
void TIMER_1_ISR(void)
|
|
|
|
{
|
|
|
|
_isr_timer(1);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
2020-09-09 14:35:29 +02:00
|
|
|
#endif /* TIMER_1_ISR */
|
2015-07-16 02:56:33 +02:00
|
|
|
/** @} */
|