Skip to content

Commit

Permalink
fix: RTOS independent 1ms tick count for HAL (#3178)
Browse files Browse the repository at this point in the history
* fix: use a 1ms HW timer to generate a RTOS independend 1ms tick counter for HAL

* fix comments
  • Loading branch information
gagarinlg authored Feb 13, 2023
1 parent 60cdb89 commit 070b255
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions radio/src/targets/common/arm/stm32/stm32_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
37 changes: 26 additions & 11 deletions radio/src/targets/common/arm/stm32/timers_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "opentx.h"

static volatile uint32_t msTickCount; // Used to get 1 kHz counter

// Start TIMER at 2000000Hz
void init2MhzTimer()
{
Expand All @@ -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;
Expand All @@ -44,33 +48,44 @@ 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)
{
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
Expand All @@ -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);
}
6 changes: 4 additions & 2 deletions radio/src/targets/common/arm/stm32/timers_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -48,3 +48,5 @@ static inline tmr10ms_t get_tmr10ms()
{
return g_tmr10ms;
}

uint32_t timersGetMsTick();
2 changes: 1 addition & 1 deletion radio/src/targets/horus/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void boardInit()
TRACE("adcInit failed");

init2MhzTimer();
init5msTimer();
init1msTimer();

usbInit();
hapticInit();
Expand Down
2 changes: 1 addition & 1 deletion radio/src/targets/nv14/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void boardInit()
battery_charge_init();
globalData.flyskygimbals = flysky_gimbal_init();
init2MhzTimer();
init5msTimer();
init1msTimer();
TouchInit();
usbInit();

Expand Down
2 changes: 1 addition & 1 deletion radio/src/targets/nv14/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* TIM7 = 2MHz counter
*
*
* TIM14 = 5ms counter
* TIM14 = 1ms counter
*/

/* DMA Allocation:
Expand Down
2 changes: 1 addition & 1 deletion radio/src/targets/taranis/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void boardInit()
lcdInit(); // delaysInit() must be called before
audioInit();
init2MhzTimer();
init5msTimer();
init1msTimer();
__enable_irq();
usbInit();

Expand Down

0 comments on commit 070b255

Please sign in to comment.