fix: RTOS independent 1ms tick count for HAL (#3178)

* fix: use a 1ms HW timer to generate a RTOS independend 1ms tick counter for HAL

* fix comments
This commit is contained in:
Malte Langermann
2023-02-13 16:35:11 +10:00
committed by GitHub
parent 60cdb897ee
commit 070b255647
7 changed files with 36 additions and 19 deletions
@@ -20,9 +20,9 @@
*/
#include "stm32_hal.h"
#include "rtos.h"
#include "timers_driver.h"
extern "C" uint32_t HAL_GetTick(void)
{
return RTOS_GET_MS();
return timersGetMsTick();
}
@@ -21,6 +21,8 @@
#include "opentx.h"
static volatile uint32_t msTickCount; // Used to get 1 kHz counter
// Start TIMER at 2000000Hz
void init2MhzTimer()
{
@@ -30,10 +32,12 @@ void init2MhzTimer()
TIMER_2MHz_TIMER->CR1 = TIM_CR1_CEN;
}
// Start TIMER at 200Hz
void init5msTimer()
// Start TIMER at 1000Hz
void init1msTimer()
{
INTERRUPT_xMS_TIMER->ARR = 4999; // 5mS in uS
msTickCount = 0;
INTERRUPT_xMS_TIMER->ARR = 999; // 5mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
INTERRUPT_xMS_TIMER->CCER = 0;
INTERRUPT_xMS_TIMER->CCMR1 = 0;
@@ -44,12 +48,17 @@ void init5msTimer()
NVIC_SetPriority(INTERRUPT_xMS_IRQn, 4);
}
void stop5msTimer()
void stop1msTimer()
{
INTERRUPT_xMS_TIMER->CR1 = 0; // stop timer
NVIC_DisableIRQ(INTERRUPT_xMS_IRQn);
}
uint32_t timersGetMsTick()
{
return msTickCount;
}
static uint32_t watchdogTimeout = 0;
void watchdogSuspend(uint32_t timeout)
@@ -57,20 +66,26 @@ void watchdogSuspend(uint32_t timeout)
watchdogTimeout = timeout;
}
static void interrupt5ms()
static void interrupt1ms()
{
static uint8_t pre_scale; // Used to get 10 Hz counter
static uint8_t pre_scale = 0;
++pre_scale;
++msTickCount;
// 5ms loop
if(pre_scale == 5 || pre_scale == 10) {
#if defined(HAPTIC)
DEBUG_TIMER_START(debugTimerHaptic);
HAPTIC_HEARTBEAT();
DEBUG_TIMER_STOP(debugTimerHaptic);
DEBUG_TIMER_START(debugTimerHaptic);
HAPTIC_HEARTBEAT();
DEBUG_TIMER_STOP(debugTimerHaptic);
#endif
}
// 10ms loop
if (pre_scale & 1) {
if (pre_scale == 10) {
pre_scale = 0;
if (watchdogTimeout) {
watchdogTimeout -= 1;
WDG_RESET(); // Retrigger hardware watchdog
@@ -86,6 +101,6 @@ static void interrupt5ms()
extern "C" void INTERRUPT_xMS_IRQHandler()
{
INTERRUPT_xMS_TIMER->SR &= ~TIM_SR_UIF;
interrupt5ms();
interrupt1ms();
DEBUG_INTERRUPT(INT_5MS);
}
@@ -31,8 +31,8 @@ uint16_t getTmr2MHz();
#include "hal.h"
void init2MhzTimer();
void init5msTimer();
void stop5msTimer();
void init1msTimer();
void stop1msTimer();
static inline uint16_t getTmr2MHz() { return TIMER_2MHz_TIMER->CNT; }
@@ -48,3 +48,5 @@ static inline tmr10ms_t get_tmr10ms()
{
return g_tmr10ms;
}
uint32_t timersGetMsTick();
+1 -1
View File
@@ -168,7 +168,7 @@ void boardInit()
TRACE("adcInit failed");
init2MhzTimer();
init5msTimer();
init1msTimer();
usbInit();
hapticInit();
+1 -1
View File
@@ -185,7 +185,7 @@ void boardInit()
battery_charge_init();
globalData.flyskygimbals = flysky_gimbal_init();
init2MhzTimer();
init5msTimer();
init1msTimer();
TouchInit();
usbInit();
+1 -1
View File
@@ -37,7 +37,7 @@
* TIM7 = 2MHz counter
*
*
* TIM14 = 5ms counter
* TIM14 = 1ms counter
*/
/* DMA Allocation:
+1 -1
View File
@@ -218,7 +218,7 @@ void boardInit()
lcdInit(); // delaysInit() must be called before
audioInit();
init2MhzTimer();
init5msTimer();
init1msTimer();
__enable_irq();
usbInit();