Skip to content

Commit

Permalink
feat: better microsecond timer (#4112)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic authored Sep 26, 2023
1 parent 6f40ea4 commit 6abd426
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 196 deletions.
2 changes: 1 addition & 1 deletion radio/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern safetych_t safetyCh[MAX_OUTPUT_CHANNELS];
extern uint8_t trimsCheckTimer;
extern uint8_t trimsDisplayTimer;
extern uint8_t trimsDisplayMask;
extern uint16_t maxMixerDuration;
extern uint32_t maxMixerDuration;

extern uint8_t requiredSpeakerVolume;
extern uint8_t requiredBacklightBright;
Expand Down
9 changes: 9 additions & 0 deletions radio/src/haptic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "opentx.h"
#include "haptic.h"

hapticQueue::hapticQueue()
{
Expand Down Expand Up @@ -107,3 +108,11 @@ void hapticQueue::event(uint8_t e)
}

hapticQueue haptic;

// from timers_driver.cpp
void per5ms()
{
DEBUG_TIMER_START(debugTimerHaptic);
HAPTIC_HEARTBEAT();
DEBUG_TIMER_STOP(debugTimerHaptic);
}
7 changes: 6 additions & 1 deletion radio/src/opentx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Clipboard clipboard;

GlobalData globalData;

uint16_t maxMixerDuration; // step = 0.01ms
uint32_t maxMixerDuration; // microseconds
uint8_t heartbeat;

#if defined(OVERRIDE_CHANNEL_FUNCTION)
Expand Down Expand Up @@ -138,6 +138,9 @@ void checkValidMCU(void)

void per10ms()
{
DEBUG_TIMER_START(debugTimerPer10ms);
DEBUG_TIMER_SAMPLE(debugTimerPer10msPeriod);

g_tmr10ms++;

#if defined(GUI)
Expand Down Expand Up @@ -203,6 +206,8 @@ void per10ms()
outputTelemetryBuffer.per10ms();

heartbeat |= HEART_TIMER_10MS;

DEBUG_TIMER_STOP(debugTimerPer10ms);
}

FlightModeData *flightModeAddress(uint8_t idx)
Expand Down
1 change: 0 additions & 1 deletion radio/src/opentx.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ void doMixerPeriodicUpdates();
void checkTrims();
extern uint8_t currentBacklightBright;
void perMain();
void per10ms();

getvalue_t getValue(mixsrc_t i, bool* valid = nullptr);

Expand Down
8 changes: 4 additions & 4 deletions radio/src/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "sbus.h"
#include "timers_driver.h"

#define SBUS_FRAME_GAP_DELAY 1000 // 500uS
#define SBUS_FRAME_GAP_DELAY_US 500

#define SBUS_START_BYTE 0x0F
#define SBUS_END_BYTE 0x00
Expand Down Expand Up @@ -99,7 +99,7 @@ void processSbusInput()

// TODO: place this outside of the function
static uint8_t SbusIndex = 0;
static uint16_t SbusTimer;
static uint32_t SbusTimer;
static uint8_t SbusFrame[SBUS_FRAME_SIZE];

uint32_t active = 0;
Expand All @@ -117,13 +117,13 @@ void processSbusInput()

// Data has been received
if (active) {
SbusTimer = getTmr2MHz();
SbusTimer = timersGetUsTick();
return;
}

// Check if end-of-frame is detected
if (SbusIndex) {
if ((uint16_t)(getTmr2MHz() - SbusTimer) > SBUS_FRAME_GAP_DELAY) {
if ((uint32_t)(timersGetUsTick() - SbusTimer) > SBUS_FRAME_GAP_DELAY_US) {
processSbusFrame(SbusFrame, trainerInput, SbusIndex);
SbusIndex = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
51 changes: 4 additions & 47 deletions radio/src/targets/common/arm/stm32/bootloader/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
// #include "keys.h"
#include "debug.h"

#include "timers_driver.h"
#include "watchdog_driver.h"

#include "hal/rotary_encoder.h"
Expand Down Expand Up @@ -97,7 +98,7 @@ uint32_t unlocked = 0;

volatile uint32_t timer10MsCount;

void interrupt10ms()
void per10ms()
{
timer10MsCount++;
tenms |= 1u; // 10 mS has passed
Expand All @@ -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)
{
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -322,7 +279,7 @@ void bootloaderInitApp()
eepromInit();
#endif

initTimers();
timersInit();

// SD card detect pin
sdInit();
Expand Down
1 change: 0 additions & 1 deletion radio/src/targets/common/arm/stm32/heartbeat_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ volatile HeartbeatCapture heartbeatCapture;

static void trigger_intmodule_heartbeat()
{
heartbeatCapture.timestamp = getTmr2MHz();
#if defined(DEBUG_LATENCY)
heartbeatCapture.count++;
#endif
Expand Down
1 change: 0 additions & 1 deletion radio/src/targets/common/arm/stm32/heartbeat_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#pragma once

struct HeartbeatCapture {
uint32_t timestamp;
uint8_t valid;
#if defined(DEBUG_LATENCY)
uint32_t count;
Expand Down
3 changes: 3 additions & 0 deletions radio/src/targets/common/arm/stm32/mixer_scheduler_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

#include "mixer_scheduler.h"
#include "stm32_hal_ll.h"
#include "stm32_timer.h"

#include "FreeRTOSConfig.h"
#include "hal.h"

// 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;
Expand Down
8 changes: 8 additions & 0 deletions radio/src/targets/common/arm/stm32/stm32_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
Loading

0 comments on commit 6abd426

Please sign in to comment.