furi_hal_os.c 5.9 KB

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