feat: better microsecond timer (#4112)

This commit is contained in:
Raphael Coeffic
2023-09-26 18:30:55 +02:00
committed by GitHub
parent 6f40ea44ad
commit 6abd4263a4
22 changed files with 135 additions and 195 deletions
@@ -30,6 +30,7 @@ set(BOOTLOADER_SRC
../usbd_usr.cpp
../usbd_storage_msd.cpp
../delays_driver.cpp
../timers_driver.cpp
../usbd_desc.c
../usb_bsp.c
../usb_driver.cpp
@@ -38,6 +39,7 @@ set(BOOTLOADER_SRC
../stm32_serial_driver.cpp
../stm32_usart_driver.cpp
../stm32_gpio_driver.cpp
../stm32_timer.cpp
../stm32_dma.cpp
init.c
boot.cpp
@@ -34,6 +34,7 @@
// #include "keys.h"
#include "debug.h"
#include "timers_driver.h"
#include "watchdog_driver.h"
#include "hal/rotary_encoder.h"
@@ -97,7 +98,7 @@ uint32_t unlocked = 0;
volatile uint32_t timer10MsCount;
void interrupt10ms()
void per10ms()
{
timer10MsCount++;
tenms |= 1u; // 10 mS has passed
@@ -122,48 +123,6 @@ extern "C" uint32_t HAL_GetTick(void)
return timer10MsCount * 10;
}
#if !defined(SIMU)
static volatile uint32_t _us_overflow_cnt;
void initTimers()
{
timer10MsCount = 0;
INTERRUPT_xMS_TIMER->ARR = 9999; // 10mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
INTERRUPT_xMS_TIMER->CR1 = TIM_CR1_CEN;
INTERRUPT_xMS_TIMER->DIER = TIM_DIER_UIE;
NVIC_EnableIRQ(INTERRUPT_xMS_IRQn);
_us_overflow_cnt = 0;
TIMER_2MHz_TIMER->ARR = 65535;
TIMER_2MHz_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 2000000 - 1; // 0.5 uS, 2 MHz
TIMER_2MHz_TIMER->CR1 = TIM_CR1_CEN;
TIMER_2MHz_TIMER->DIER = TIM_DIER_UIE;
NVIC_EnableIRQ(TIMER_2MHz_IRQn);
while(timer10MsCount < 10);
}
uint32_t timersGetUsTick()
{
uint32_t us;
us = TIMER_2MHz_TIMER->CNT >> 1;
us += _us_overflow_cnt << 15;
return us;
}
extern "C" void INTERRUPT_xMS_IRQHandler()
{
INTERRUPT_xMS_TIMER->SR &= ~TIM_SR_UIF;
interrupt10ms();
}
extern "C" void TIMER_2MHz_IRQHandler()
{
TIMER_2MHz_TIMER->SR &= ~TIM_SR_UIF;
_us_overflow_cnt += 1;
}
#endif
uint32_t isValidBufferStart(const uint8_t * buffer)
{
@@ -254,9 +213,7 @@ void bootloaderInitApp()
ENABLE);
RCC_APB1PeriphClockCmd(ROTARY_ENCODER_RCC_APB1Periph | LCD_RCC_APB1Periph |
BACKLIGHT_RCC_APB1Periph |
INTERRUPT_xMS_RCC_APB1Periph |
TIMER_2MHz_RCC_APB1Periph,
BACKLIGHT_RCC_APB1Periph,
ENABLE);
RCC_APB2PeriphClockCmd(
@@ -322,7 +279,7 @@ void bootloaderInitApp()
eepromInit();
#endif
initTimers();
timersInit();
// SD card detect pin
sdInit();
@@ -36,7 +36,6 @@ volatile HeartbeatCapture heartbeatCapture;
static void trigger_intmodule_heartbeat()
{
heartbeatCapture.timestamp = getTmr2MHz();
#if defined(DEBUG_LATENCY)
heartbeatCapture.count++;
#endif
@@ -22,7 +22,6 @@
#pragma once
struct HeartbeatCapture {
uint32_t timestamp;
uint8_t valid;
#if defined(DEBUG_LATENCY)
uint32_t count;
@@ -21,6 +21,7 @@
#include "mixer_scheduler.h"
#include "stm32_hal_ll.h"
#include "stm32_timer.h"
#include "FreeRTOSConfig.h"
#include "hal.h"
@@ -28,6 +29,8 @@
// Start scheduler with default period
void mixerSchedulerStart()
{
stm32_timer_enable_clock(MIXER_SCHEDULER_TIMER);
MIXER_SCHEDULER_TIMER->CR1 &= ~TIM_CR1_CEN;
MIXER_SCHEDULER_TIMER->PSC = MIXER_SCHEDULER_TIMER_FREQ / 1000000 - 1; // 1uS (1Mhz)
MIXER_SCHEDULER_TIMER->CCER = 0;
@@ -35,6 +35,10 @@ void stm32_timer_enable_clock(TIM_TypeDef *TIMx)
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM5);
} else if (TIMx == TIM8) {
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM8);
} else if (TIMx == TIM12) {
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM12);
} else if (TIMx == TIM14) {
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM14);
}
}
@@ -52,5 +56,9 @@ void stm32_timer_disable_clock(TIM_TypeDef *TIMx)
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM5);
} else if (TIMx == TIM8) {
LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_TIM8);
} else if (TIMx == TIM12) {
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM12);
} else if (TIMx == TIM14) {
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM14);
}
}
@@ -19,109 +19,95 @@
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "timers_driver.h"
#include "stm32_timer.h"
#include "stm32_hal_ll.h"
#include "hal.h"
#include "watchdog_driver.h"
static volatile uint32_t msTickCount; // Used to get 1 kHz counter
static volatile uint32_t _us_overflow_cnt;
static volatile uint32_t _ms_ticks;
// Start TIMER at 2000000Hz
void init2MhzTimer()
static void _init_1ms_timer()
{
_us_overflow_cnt = 0;
TIMER_2MHz_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 2000000 - 1; // 0.5 uS, 2 MHz
TIMER_2MHz_TIMER->ARR = 65535;
TIMER_2MHz_TIMER->CR2 = 0;
TIMER_2MHz_TIMER->CR1 = TIM_CR1_CEN;
TIMER_2MHz_TIMER->DIER = TIM_DIER_UIE;
stm32_timer_enable_clock(MS_TIMER);
NVIC_EnableIRQ(TIMER_2MHz_IRQn);
NVIC_SetPriority(TIMER_2MHz_IRQn, 4);
_ms_ticks = 0;
MS_TIMER->ARR = 999; // 1mS in uS
MS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
MS_TIMER->CCER = 0;
MS_TIMER->CCMR1 = 0;
MS_TIMER->EGR = 0;
MS_TIMER->CR1 = TIM_CR1_CEN | TIM_CR1_URS;
MS_TIMER->DIER = TIM_DIER_UIE;
NVIC_EnableIRQ(MS_TIMER_IRQn);
NVIC_SetPriority(MS_TIMER_IRQn, 4);
}
// Start TIMER at 1000Hz
void init1msTimer()
void timersInit()
{
msTickCount = 0;
INTERRUPT_xMS_TIMER->ARR = 999; // 1mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
INTERRUPT_xMS_TIMER->CCER = 0;
INTERRUPT_xMS_TIMER->CCMR1 = 0;
INTERRUPT_xMS_TIMER->EGR = 0;
INTERRUPT_xMS_TIMER->CR1 = TIM_CR1_CEN | TIM_CR1_URS;
INTERRUPT_xMS_TIMER->DIER = TIM_DIER_UIE;
NVIC_EnableIRQ(INTERRUPT_xMS_IRQn);
NVIC_SetPriority(INTERRUPT_xMS_IRQn, 4);
}
void stop1msTimer()
{
INTERRUPT_xMS_TIMER->CR1 = 0; // stop timer
NVIC_DisableIRQ(INTERRUPT_xMS_IRQn);
_init_1ms_timer();
}
uint32_t timersGetMsTick()
{
return msTickCount;
return _ms_ticks;
}
uint32_t timersGetUsTick()
{
uint32_t ms;
uint32_t us;
us = TIMER_2MHz_TIMER->CNT >> 1;
us += _us_overflow_cnt << 15;
return us;
do {
ms = _ms_ticks;
us = MS_TIMER->CNT;
asm volatile("nop");
asm volatile("nop");
} while (ms != _ms_ticks);
return ms * 1000 + us;
}
static uint32_t watchdogTimeout = 0;
static volatile uint32_t watchdogTimeout = 0;
void watchdogSuspend(uint32_t timeout)
{
watchdogTimeout = timeout;
}
static void interrupt1ms()
#define __weak __attribute__((weak))
__weak void per10ms() {}
__weak void per5ms() {}
static inline void _interrupt_1ms()
{
static uint8_t pre_scale = 0;
++pre_scale;
++msTickCount;
++_ms_ticks;
// 5ms loop
if(pre_scale == 5 || pre_scale == 10) {
#if defined(HAPTIC)
DEBUG_TIMER_START(debugTimerHaptic);
HAPTIC_HEARTBEAT();
DEBUG_TIMER_STOP(debugTimerHaptic);
#endif
per5ms();
}
// 10ms loop
if (pre_scale == 10) {
pre_scale = 0;
if (watchdogTimeout) {
watchdogTimeout -= 1;
WDG_RESET(); // Retrigger hardware watchdog
}
DEBUG_TIMER_START(debugTimerPer10ms);
DEBUG_TIMER_SAMPLE(debugTimerPer10msPeriod);
per10ms();
DEBUG_TIMER_STOP(debugTimerPer10ms);
}
}
extern "C" void INTERRUPT_xMS_IRQHandler()
extern "C" void MS_TIMER_IRQHandler()
{
INTERRUPT_xMS_TIMER->SR &= ~TIM_SR_UIF;
interrupt1ms();
DEBUG_INTERRUPT(INT_5MS);
}
extern "C" void TIMER_2MHz_IRQHandler()
{
TIMER_2MHz_TIMER->SR &= ~TIM_SR_UIF;
_us_overflow_cnt += 1;
MS_TIMER->SR &= ~TIM_SR_UIF;
_interrupt_1ms();
}
@@ -21,33 +21,18 @@
#pragma once
#if defined(SIMU)
uint16_t getTmr2MHz();
#define watchdogSuspend(timeout)
#else // SIMU
#include "hal.h"
void init2MhzTimer();
void init1msTimer();
void stop1msTimer();
#define getTmr2MHz() TIMER_2MHz_TIMER->CNT
void watchdogSuspend(uint32_t timeout);
#endif
#include "opentx_types.h"
extern "C" volatile tmr10ms_t g_tmr10ms;
static inline tmr10ms_t get_tmr10ms() { return g_tmr10ms; }
static inline tmr10ms_t get_tmr10ms()
{
return g_tmr10ms;
}
void watchdogSuspend(uint32_t timeout);
void timersInit();
uint32_t timersGetMsTick();
uint32_t timersGetUsTick();
// declared "weak", to be implemented by application
void per5ms();
void per10ms();