2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_os.h>
|
2022-04-21 13:15:19 +00:00
|
|
|
#include <furi_hal_clock.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_power.h>
|
2022-04-21 13:15:19 +00:00
|
|
|
#include <furi_hal_delay.h>
|
|
|
|
#include <furi_hal_idle_timer.h>
|
2021-09-10 02:19:02 +00:00
|
|
|
#include <stm32wbxx_ll_cortex.h>
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "FuriHalOs"
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
#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)
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
#include <stm32wbxx_ll_gpio.h>
|
|
|
|
|
|
|
|
#define LED_SLEEP_PORT GPIOA
|
|
|
|
#define LED_SLEEP_PIN LL_GPIO_PIN_7
|
|
|
|
#define LED_TICK_PORT GPIOA
|
|
|
|
#define LED_TICK_PIN LL_GPIO_PIN_6
|
|
|
|
#define LED_SECOND_PORT GPIOA
|
|
|
|
#define LED_SECOND_PIN LL_GPIO_PIN_4
|
|
|
|
|
|
|
|
void furi_hal_os_timer_callback() {
|
|
|
|
LL_GPIO_TogglePin(LED_SECOND_PORT, LED_SECOND_PIN);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern void xPortSysTickHandler();
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
static volatile uint32_t furi_hal_os_skew = 0;
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
void furi_hal_os_init() {
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_init();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
#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);
|
2022-04-21 13:15:19 +00:00
|
|
|
osTimerStart(second_timer, FURI_HAL_OS_TICK_HZ);
|
2021-09-10 02:19:02 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
void furi_hal_os_tick() {
|
|
|
|
if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
2022-01-05 16:10:18 +00:00
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
2022-04-21 13:15:19 +00:00
|
|
|
LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
|
2022-01-05 16:10:18 +00:00
|
|
|
#endif
|
2022-04-21 13:15:19 +00:00
|
|
|
xPortSysTickHandler();
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
|
|
|
|
// Stop ticks
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_clock_suspend_tick();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Start wakeup timer
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
LL_GPIO_ResetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Go to sleep mode
|
|
|
|
furi_hal_power_sleep();
|
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
LL_GPIO_SetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Calculate how much time we spent in the sleep
|
2022-04-21 13:15:19 +00:00
|
|
|
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);
|
2021-09-10 02:19:02 +00:00
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_IDLE_TIMER);
|
|
|
|
bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_IDLE_TIMER);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(cmpm && arrm) after_tick += expected_idle_ticks;
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Prepare tick timer for new round
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_reset();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Resume ticks
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_clock_resume_tick();
|
2021-09-10 02:19:02 +00:00
|
|
|
return after_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
|
|
|
|
if(!furi_hal_power_sleep_available()) {
|
|
|
|
__WFI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
// Limit amount of ticks to maximum that timer can count
|
2022-01-05 16:10:18 +00:00
|
|
|
if(expected_idle_ticks > FURI_HAL_OS_MAX_SLEEP) {
|
2021-09-10 02:19:02 +00:00
|
|
|
expected_idle_ticks = FURI_HAL_OS_MAX_SLEEP;
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
// Stop IRQ handling, no one should disturb us till we finish
|
2022-03-29 17:37:23 +00:00
|
|
|
__disable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Confirm OS that sleep is still possible
|
2022-01-05 16:10:18 +00:00
|
|
|
if(eTaskConfirmSleepModeStatus() == eAbortSleep) {
|
2022-03-29 17:37:23 +00:00
|
|
|
__enable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-01-05 16:10:18 +00:00
|
|
|
if(completed_ticks > 0) {
|
2022-04-21 13:15:19 +00:00
|
|
|
vTaskStepTick(MIN(completed_ticks, expected_idle_ticks));
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reenable IRQ
|
2022-03-29 17:37:23 +00:00
|
|
|
__enable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
void vApplicationStackOverflowHook(TaskHandle_t xTask, char* pcTaskName) {
|
2021-12-31 17:32:49 +00:00
|
|
|
furi_crash("StackOverflow");
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|