implemented timebase using TIM14 instead of TIM1

This commit is contained in:
2018-01-27 18:55:17 +01:00
parent 1159fef33b
commit 94e87c74d3
8 changed files with 154 additions and 36 deletions
+2 -1
View File
@@ -84,11 +84,12 @@ void irqd_init(void)
HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 3, 0);
HAL_NVIC_SetPriority(DMA1_Channel4_5_6_7_IRQn, 3, 0);
// NVIC_EnableIRQ(ADC1_COMP_IRQn); /*!< ADC1 and COMP interrupts (ADC interrupt combined with EXTI Lines 21 and 22 */
// NVIC_EnableIRQ(TIM1_IRQn); /*!< TIM1 global Interrupt */
// NVIC_EnableIRQ(TIM2_IRQn); /*!< TIM2 global Interrupt */
// NVIC_EnableIRQ(TIM3_IRQn); /*!< TIM3 global Interrupt */
// NVIC_EnableIRQ(TIM6_DAC_IRQn); /*!< TIM6 global and DAC channel underrun error Interrupt */
// NVIC_EnableIRQ(TIM7_IRQn); /*!< TIM7 global Interrupt */
// NVIC_EnableIRQ(TIM14_IRQn); /*!< TIM14 global Interrupt */
// --used internally-- NVIC_EnableIRQ(TIM14_IRQn); /*!< TIM14 global Interrupt */
// NVIC_EnableIRQ(TIM15_IRQn); /*!< TIM15 global Interrupt */
// NVIC_EnableIRQ(TIM16_IRQn); /*!< TIM16 global Interrupt */
// NVIC_EnableIRQ(TIM17_IRQn); /*!< TIM17 global Interrupt */
+2 -1
View File
@@ -14,6 +14,7 @@
#include "status_led.h"
#include "debug_uart.h"
#include "irq_dispatcher.h"
#include "timebase.h"
void plat_init(void)
{
@@ -26,7 +27,7 @@ void plat_init(void)
LockJumper_Init();
Indicator_Init();
DebugUart_Init(); // <- only the resource claim
DebugUart_Init(); // resource claim
irqd_init();
+98
View File
@@ -0,0 +1,98 @@
//
// Created by MightyPork on 2018/01/27.
//
#include "platform.h"
#include "timebase.h"
#define TIMEBASE_TIMER TIM14
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
// TIM14 is a simple 16-bit timer timer with no special features.
// This makes it a good choice for the timebase generation. We set it to generate
// an interrupt every 1 ms
// - TIM14 is always up-counting
// - using APB1 clock
__HAL_RCC_TIM14_CLK_ENABLE();
NVIC_SetPriority(TIM14_IRQn, TickPriority); // highest possible priority
NVIC_EnableIRQ(TIM14_IRQn);
/* Compute TIM1 clock */
uint32_t uwTimclock = HAL_RCC_GetPCLK1Freq();
/* Get a 1 MHz clock for the timer */
uint16_t uhPrescalerValue = (uint16_t) ((uwTimclock / 1000000) - 1);
/* Get 1 kHz interrupt */
uint16_t uhPeriod = (1000000 / 1000) - 1;
LL_TIM_SetPrescaler(TIMEBASE_TIMER, uhPrescalerValue);
LL_TIM_SetAutoReload(TIMEBASE_TIMER, uhPeriod);
LL_TIM_EnableARRPreload(TIMEBASE_TIMER);
LL_TIM_EnableIT_UPDATE(TIMEBASE_TIMER);
LL_TIM_GenerateEvent_UPDATE(TIMEBASE_TIMER);
LL_TIM_EnableCounter(TIMEBASE_TIMER);
/* Return function status */
return HAL_OK;
}
static volatile uint32_t uwUptimeMs = 0;
/* TIMEBASE TIMER ISR */
void TIM14_IRQHandler(void)
{
uwUptimeMs++;
LL_TIM_ClearFlag_UPDATE(TIMEBASE_TIMER);
}
void HAL_IncTick(void)
{
uwUptimeMs++;
}
uint32_t HAL_GetTick(void)
{
return uwUptimeMs;
}
void HAL_SuspendTick(void)
{
LL_TIM_DisableIT_UPDATE(TIMEBASE_TIMER);
}
void HAL_ResumeTick(void)
{
LL_TIM_EnableIT_UPDATE(TIMEBASE_TIMER);
}
// -------------------------------------------------------------------------------------
// Timestamping functions ...
/*
* I wanted to freeze time. I wanted to savor that moment, to live in that moment
* for a week. But I couldn't stop it, only slow it. And before I knew it, she was gone.
* -- Ben Willis, "Cashback"
*/
uint64_t PTIM_GetMicrotime(void)
{
uint32_t uwMicros;
uint32_t uwMillis;
vPortEnterCritical();
{
uwMicros = TIMEBASE_TIMER->CNT;
uwMillis = uwUptimeMs;
if (LL_TIM_IsActiveFlag_UPDATE(TIM14)) {
// This means the timer has overflown after we disabled IRQ
// Use the last CNT value before the overflow
uwMicros = TIM14->ARR; // this is 999us
}
}
vPortExitCritical();
return (uint64_t)uwMillis*1000 + uwMicros;
}
+22
View File
@@ -0,0 +1,22 @@
//
// Created by MightyPork on 2018/01/27.
//
// Configures and manages the high priority timer used for timeouts and precision delays.
//
// SysTick can't be used for this because, under FreeRTOS, the system tick interrupt
// has the lowest priority to not interfere with more important application processes
// and interrupts.
//
#ifndef GEX_F072_TIMEBASE_H
#define GEX_F072_TIMEBASE_H
/**
* Precision timer: get microtime as uint64_t
* This timestamp should be monotonously increasing with a precision of ±0.5µs
*
* @return time in microseconds
*/
uint64_t PTIM_GetMicrotime(void);
#endif //GEX_F072_TIMEBASE_H