furi_hal_os.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <furi_hal_os.h>
  2. #include <furi_hal_clock.h>
  3. #include <furi_hal_power.h>
  4. #include <furi_hal_delay.h>
  5. #include <furi_hal_idle_timer.h>
  6. #include <stm32wbxx_ll_cortex.h>
  7. #include <furi.h>
  8. #define TAG "FuriHalOs"
  9. #define FURI_HAL_IDLE_TIMER_CLK_HZ 32768
  10. #define FURI_HAL_OS_TICK_HZ configTICK_RATE_HZ
  11. #define FURI_HAL_OS_IDLE_CNT_TO_TICKS(x) ((x * FURI_HAL_OS_TICK_HZ) / FURI_HAL_IDLE_TIMER_CLK_HZ)
  12. #define FURI_HAL_OS_TICKS_TO_IDLE_CNT(x) ((x * FURI_HAL_IDLE_TIMER_CLK_HZ) / FURI_HAL_OS_TICK_HZ)
  13. #define FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH (FURI_HAL_OS_IDLE_CNT_TO_TICKS(FURI_HAL_IDLE_TIMER_MAX))
  14. #define FURI_HAL_OS_MAX_SLEEP (FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH - 1)
  15. #ifdef FURI_HAL_OS_DEBUG
  16. #include <stm32wbxx_ll_gpio.h>
  17. #define LED_SLEEP_PORT GPIOA
  18. #define LED_SLEEP_PIN LL_GPIO_PIN_7
  19. #define LED_TICK_PORT GPIOA
  20. #define LED_TICK_PIN LL_GPIO_PIN_6
  21. #define LED_SECOND_PORT GPIOA
  22. #define LED_SECOND_PIN LL_GPIO_PIN_4
  23. void furi_hal_os_timer_callback() {
  24. LL_GPIO_TogglePin(LED_SECOND_PORT, LED_SECOND_PIN);
  25. }
  26. #endif
  27. extern void xPortSysTickHandler();
  28. static volatile uint32_t furi_hal_os_skew = 0;
  29. void furi_hal_os_init() {
  30. furi_hal_idle_timer_init();
  31. #ifdef FURI_HAL_OS_DEBUG
  32. LL_GPIO_SetPinMode(LED_SLEEP_PORT, LED_SLEEP_PIN, LL_GPIO_MODE_OUTPUT);
  33. LL_GPIO_SetPinMode(LED_TICK_PORT, LED_TICK_PIN, LL_GPIO_MODE_OUTPUT);
  34. LL_GPIO_SetPinMode(LED_SECOND_PORT, LED_SECOND_PIN, LL_GPIO_MODE_OUTPUT);
  35. osTimerId_t second_timer = osTimerNew(furi_hal_os_timer_callback, osTimerPeriodic, NULL, NULL);
  36. osTimerStart(second_timer, FURI_HAL_OS_TICK_HZ);
  37. #endif
  38. FURI_LOG_I(TAG, "Init OK");
  39. }
  40. void furi_hal_os_tick() {
  41. if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  42. #ifdef FURI_HAL_OS_DEBUG
  43. LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
  44. #endif
  45. xPortSysTickHandler();
  46. }
  47. }
  48. static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
  49. // Stop ticks
  50. furi_hal_clock_suspend_tick();
  51. // Start wakeup timer
  52. furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
  53. #ifdef FURI_HAL_OS_DEBUG
  54. LL_GPIO_ResetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  55. #endif
  56. // Go to sleep mode
  57. furi_hal_power_sleep();
  58. #ifdef FURI_HAL_OS_DEBUG
  59. LL_GPIO_SetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  60. #endif
  61. // Calculate how much time we spent in the sleep
  62. uint32_t after_cnt = furi_hal_idle_timer_get_cnt() + furi_hal_os_skew;
  63. uint32_t after_tick = FURI_HAL_OS_IDLE_CNT_TO_TICKS(after_cnt);
  64. furi_hal_os_skew = after_cnt - (after_cnt / after_tick);
  65. bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_IDLE_TIMER);
  66. bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_IDLE_TIMER);
  67. if(cmpm && arrm) after_tick += expected_idle_ticks;
  68. // Prepare tick timer for new round
  69. furi_hal_idle_timer_reset();
  70. // Resume ticks
  71. furi_hal_clock_resume_tick();
  72. return after_tick;
  73. }
  74. void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
  75. if(!furi_hal_power_sleep_available()) {
  76. __WFI();
  77. return;
  78. }
  79. // Limit amount of ticks to maximum that timer can count
  80. if(expected_idle_ticks > FURI_HAL_OS_MAX_SLEEP) {
  81. expected_idle_ticks = FURI_HAL_OS_MAX_SLEEP;
  82. }
  83. // Stop IRQ handling, no one should disturb us till we finish
  84. __disable_irq();
  85. // Confirm OS that sleep is still possible
  86. if(eTaskConfirmSleepModeStatus() == eAbortSleep) {
  87. __enable_irq();
  88. return;
  89. }
  90. // Sleep and track how much ticks we spent sleeping
  91. uint32_t completed_ticks = furi_hal_os_sleep(expected_idle_ticks);
  92. // Notify system about time spent in sleep
  93. if(completed_ticks > 0) {
  94. vTaskStepTick(MIN(completed_ticks, expected_idle_ticks));
  95. }
  96. // Reenable IRQ
  97. __enable_irq();
  98. }
  99. void vApplicationStackOverflowHook(TaskHandle_t xTask, char* pcTaskName) {
  100. furi_crash("StackOverflow");
  101. }