[FL-2480] Use SysTick as the main OS timer (#1140)

* Use SysTick as OS tick timer
* Use LPTIM2 as tickless idle timer
* Remove dummy LPTIM2 IRQ handler
* Clean up clock init code
* Rename os timer to idle timer
* Advance hal ticks along with FreeRTOS's
* Improve SysTick control during tickless idle
* Improve idle timer precision
* Correct SysTick IRQ priority
* Main, FuriHal: move system startup to separate thread
* Minor code cleanup

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Georgii Surkov
2022-04-21 16:15:19 +03:00
committed by GitHub
parent ad1ee8a5c6
commit df66f4f6ba
12 changed files with 140 additions and 162 deletions

View File

@@ -1,17 +1,22 @@
#include <furi_hal_os.h>
#include <furi_hal_os_timer.h>
#include <furi_hal_clock.h>
#include <furi_hal_power.h>
#include <furi_hal_delay.h>
#include <furi_hal_idle_timer.h>
#include <stm32wbxx_ll_cortex.h>
#include <furi.h>
#define TAG "FuriHalOs"
#define FURI_HAL_OS_CLK_FREQUENCY 32768
#define FURI_HAL_OS_TICK_PER_SECOND 1024
#define FURI_HAL_OS_CLK_PER_TICK (FURI_HAL_OS_CLK_FREQUENCY / FURI_HAL_OS_TICK_PER_SECOND)
#define FURI_HAL_OS_TICK_PER_EPOCH (FURI_HAL_OS_TIMER_MAX / FURI_HAL_OS_CLK_PER_TICK)
#define FURI_HAL_OS_MAX_SLEEP (FURI_HAL_OS_TICK_PER_EPOCH - 1)
#define FURI_HAL_IDLE_TIMER_CLK_HZ 32768
#define FURI_HAL_OS_TICK_HZ configTICK_RATE_HZ
#define FURI_HAL_OS_IDLE_CNT_TO_TICKS(x) ((x * FURI_HAL_OS_TICK_HZ) / FURI_HAL_IDLE_TIMER_CLK_HZ)
#define FURI_HAL_OS_TICKS_TO_IDLE_CNT(x) ((x * FURI_HAL_IDLE_TIMER_CLK_HZ) / FURI_HAL_OS_TICK_HZ)
#define FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH (FURI_HAL_OS_IDLE_CNT_TO_TICKS(FURI_HAL_IDLE_TIMER_MAX))
#define FURI_HAL_OS_MAX_SLEEP (FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH - 1)
#ifdef FURI_HAL_OS_DEBUG
#include <stm32wbxx_ll_gpio.h>
@@ -30,48 +35,37 @@ void furi_hal_os_timer_callback() {
extern void xPortSysTickHandler();
volatile uint32_t furi_hal_os_skew = 0;
static volatile uint32_t furi_hal_os_skew = 0;
void furi_hal_os_init() {
LL_DBGMCU_APB1_GRP2_FreezePeriph(LL_DBGMCU_APB1_GRP2_LPTIM2_STOP);
furi_hal_os_timer_init();
furi_hal_os_timer_continuous(FURI_HAL_OS_CLK_PER_TICK);
furi_hal_idle_timer_init();
#ifdef FURI_HAL_OS_DEBUG
LL_GPIO_SetPinMode(LED_SLEEP_PORT, LED_SLEEP_PIN, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinMode(LED_TICK_PORT, LED_TICK_PIN, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinMode(LED_SECOND_PORT, LED_SECOND_PIN, LL_GPIO_MODE_OUTPUT);
osTimerId_t second_timer = osTimerNew(furi_hal_os_timer_callback, osTimerPeriodic, NULL, NULL);
osTimerStart(second_timer, FURI_HAL_OS_TICK_PER_SECOND);
osTimerStart(second_timer, FURI_HAL_OS_TICK_HZ);
#endif
FURI_LOG_I(TAG, "Init OK");
}
void LPTIM2_IRQHandler(void) {
// Autoreload
if(LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_OS_TIMER)) {
LL_LPTIM_ClearFLAG_ARRM(FURI_HAL_OS_TIMER);
if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
void furi_hal_os_tick() {
if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
#ifdef FURI_HAL_OS_DEBUG
LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
#endif
xPortSysTickHandler();
}
}
if(LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_OS_TIMER)) {
LL_LPTIM_ClearFLAG_CMPM(FURI_HAL_OS_TIMER);
xPortSysTickHandler();
}
}
static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
// Stop ticks
furi_hal_os_timer_reset();
LL_SYSTICK_DisableIT();
furi_hal_clock_suspend_tick();
// Start wakeup timer
furi_hal_os_timer_single(expected_idle_ticks * FURI_HAL_OS_CLK_PER_TICK);
furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
#ifdef FURI_HAL_OS_DEBUG
LL_GPIO_ResetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
@@ -85,21 +79,19 @@ static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
#endif
// Calculate how much time we spent in the sleep
uint32_t after_cnt = furi_hal_os_timer_get_cnt() + furi_hal_os_skew;
uint32_t after_tick = after_cnt / FURI_HAL_OS_CLK_PER_TICK;
furi_hal_os_skew = after_cnt % FURI_HAL_OS_CLK_PER_TICK;
uint32_t after_cnt = furi_hal_idle_timer_get_cnt() + furi_hal_os_skew;
uint32_t after_tick = FURI_HAL_OS_IDLE_CNT_TO_TICKS(after_cnt);
furi_hal_os_skew = after_cnt - (after_cnt / after_tick);
bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_OS_TIMER);
bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_OS_TIMER);
bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_IDLE_TIMER);
bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_IDLE_TIMER);
if(cmpm && arrm) after_tick += expected_idle_ticks;
// Prepare tick timer for new round
furi_hal_os_timer_reset();
furi_hal_idle_timer_reset();
// Resume ticks
LL_SYSTICK_EnableIT();
furi_hal_os_timer_continuous(FURI_HAL_OS_CLK_PER_TICK);
furi_hal_clock_resume_tick();
return after_tick;
}
@@ -109,7 +101,7 @@ void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
return;
}
// Limit mount of ticks to maximum that timer can count
// Limit amount of ticks to maximum that timer can count
if(expected_idle_ticks > FURI_HAL_OS_MAX_SLEEP) {
expected_idle_ticks = FURI_HAL_OS_MAX_SLEEP;
}
@@ -125,14 +117,9 @@ void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
// Sleep and track how much ticks we spent sleeping
uint32_t completed_ticks = furi_hal_os_sleep(expected_idle_ticks);
// Notify system about time spent in sleep
if(completed_ticks > 0) {
if(completed_ticks > expected_idle_ticks) {
vTaskStepTick(expected_idle_ticks);
} else {
vTaskStepTick(completed_ticks);
}
vTaskStepTick(MIN(completed_ticks, expected_idle_ticks));
}
// Reenable IRQ