furi_hal_os.c 4.2 KB

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