furi-hal-os.c 4.2 KB

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