furi_hal_os.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <furi_hal_os.h>
  2. #include <furi_hal_clock.h>
  3. #include <furi_hal_power.h>
  4. #include <furi_hal_gpio.h>
  5. #include <furi_hal_resources.h>
  6. #include <furi_hal_idle_timer.h>
  7. #include <stm32wbxx_ll_cortex.h>
  8. #include <furi.h>
  9. #define TAG "FuriHalOs"
  10. #define FURI_HAL_IDLE_TIMER_CLK_HZ 32768
  11. #define FURI_HAL_OS_TICK_HZ configTICK_RATE_HZ
  12. #define FURI_HAL_OS_IDLE_CNT_TO_TICKS(x) ((x * FURI_HAL_OS_TICK_HZ) / FURI_HAL_IDLE_TIMER_CLK_HZ)
  13. #define FURI_HAL_OS_TICKS_TO_IDLE_CNT(x) ((x * FURI_HAL_IDLE_TIMER_CLK_HZ) / FURI_HAL_OS_TICK_HZ)
  14. #define FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH (FURI_HAL_OS_IDLE_CNT_TO_TICKS(FURI_HAL_IDLE_TIMER_MAX))
  15. #define FURI_HAL_OS_MAX_SLEEP (FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH - 1)
  16. #define FURI_HAL_OS_NVIC_IS_PENDING() (NVIC->ISPR[0] || NVIC->ISPR[1])
  17. #define FURI_HAL_OS_EXTI_LINE_0_31 0
  18. #define FURI_HAL_OS_EXTI_LINE_32_63 1
  19. // Arbitrary (but small) number for better tick consistency
  20. #define FURI_HAL_OS_EXTRA_CNT 3
  21. #ifdef FURI_HAL_OS_DEBUG
  22. #include <stm32wbxx_ll_gpio.h>
  23. void furi_hal_os_timer_callback() {
  24. furi_hal_gpio_write(&gpio_ext_pa4, !furi_hal_gpio_read(&gpio_ext_pa4));
  25. }
  26. #endif
  27. extern void xPortSysTickHandler();
  28. static volatile uint32_t furi_hal_os_skew;
  29. void furi_hal_os_init() {
  30. furi_hal_idle_timer_init();
  31. #ifdef FURI_HAL_OS_DEBUG
  32. furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeOutputPushPull);
  33. furi_hal_gpio_init_simple(&gpio_ext_pa6, GpioModeOutputPushPull);
  34. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  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. furi_hal_gpio_write(&gpio_ext_pa6, !furi_hal_gpio_read(&gpio_ext_pa6));
  44. #endif
  45. xPortSysTickHandler();
  46. }
  47. }
  48. #ifdef FURI_HAL_OS_DEBUG
  49. // Find out the IRQ number while debugging
  50. static void furi_hal_os_nvic_dbg_trap() {
  51. for(int32_t i = WWDG_IRQn; i <= DMAMUX1_OVR_IRQn; i++) {
  52. if(NVIC_GetPendingIRQ(i)) {
  53. (void)i;
  54. // Break here
  55. __NOP();
  56. }
  57. }
  58. }
  59. // Find out the EXTI line number while debugging
  60. static void furi_hal_os_exti_dbg_trap(uint32_t exti, uint32_t val) {
  61. for(uint32_t i = 0; val; val >>= 1U, ++i) {
  62. if(val & 1U) {
  63. (void)exti;
  64. (void)i;
  65. // Break here
  66. __NOP();
  67. }
  68. }
  69. }
  70. #endif
  71. static inline bool furi_hal_os_is_pending_irq() {
  72. if(FURI_HAL_OS_NVIC_IS_PENDING()) {
  73. #ifdef FURI_HAL_OS_DEBUG
  74. furi_hal_os_nvic_dbg_trap();
  75. #endif
  76. return true;
  77. }
  78. uint32_t exti_lines_active;
  79. if((exti_lines_active = LL_EXTI_ReadFlag_0_31(LL_EXTI_LINE_ALL_0_31))) {
  80. #ifdef FURI_HAL_OS_DEBUG
  81. furi_hal_os_exti_dbg_trap(FURI_HAL_OS_EXTI_LINE_0_31, exti_lines_active);
  82. #endif
  83. return true;
  84. } else if((exti_lines_active = LL_EXTI_ReadFlag_32_63(LL_EXTI_LINE_ALL_32_63))) {
  85. #ifdef FURI_HAL_OS_DEBUG
  86. furi_hal_os_exti_dbg_trap(FURI_HAL_OS_EXTI_LINE_32_63, exti_lines_active);
  87. #endif
  88. return true;
  89. }
  90. return false;
  91. }
  92. static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
  93. // Stop ticks
  94. furi_hal_clock_suspend_tick();
  95. // Start wakeup timer
  96. furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
  97. #ifdef FURI_HAL_OS_DEBUG
  98. furi_hal_gpio_write(&gpio_ext_pa7, 0);
  99. #endif
  100. // Go to sleep mode
  101. furi_hal_power_sleep();
  102. #ifdef FURI_HAL_OS_DEBUG
  103. furi_hal_gpio_write(&gpio_ext_pa7, 1);
  104. #endif
  105. // Calculate how much time we spent in the sleep
  106. uint32_t after_cnt = furi_hal_idle_timer_get_cnt() + furi_hal_os_skew + FURI_HAL_OS_EXTRA_CNT;
  107. uint32_t after_tick = FURI_HAL_OS_IDLE_CNT_TO_TICKS(after_cnt);
  108. furi_hal_os_skew = after_cnt - FURI_HAL_OS_TICKS_TO_IDLE_CNT(after_tick);
  109. bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_IDLE_TIMER);
  110. bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_IDLE_TIMER);
  111. if(cmpm && arrm) after_tick += expected_idle_ticks;
  112. // Prepare tick timer for new round
  113. furi_hal_idle_timer_reset();
  114. // Resume ticks
  115. furi_hal_clock_resume_tick();
  116. return after_tick;
  117. }
  118. void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
  119. if(!furi_hal_power_sleep_available()) {
  120. __WFI();
  121. return;
  122. }
  123. // Limit amount of ticks to maximum that timer can count
  124. if(expected_idle_ticks > FURI_HAL_OS_MAX_SLEEP) {
  125. expected_idle_ticks = FURI_HAL_OS_MAX_SLEEP;
  126. }
  127. // Stop IRQ handling, no one should disturb us till we finish
  128. __disable_irq();
  129. // Confirm OS that sleep is still possible
  130. if(eTaskConfirmSleepModeStatus() == eAbortSleep || furi_hal_os_is_pending_irq()) {
  131. __enable_irq();
  132. return;
  133. }
  134. // Sleep and track how much ticks we spent sleeping
  135. uint32_t completed_ticks = furi_hal_os_sleep(expected_idle_ticks);
  136. // Notify system about time spent in sleep
  137. if(completed_ticks > 0) {
  138. vTaskStepTick(MIN(completed_ticks, expected_idle_ticks));
  139. }
  140. // Reenable IRQ
  141. __enable_irq();
  142. }
  143. void vApplicationStackOverflowHook(TaskHandle_t xTask, char* pcTaskName) {
  144. furi_crash("StackOverflow");
  145. }