api-hal-os.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <api-hal-os.h>
  2. #include <api-hal-os-timer.h>
  3. #include <api-hal-power.h>
  4. #include <FreeRTOS.h>
  5. #include <cmsis_os.h>
  6. #define API_HAL_OS_CLK_FREQUENCY 32768
  7. #define API_HAL_OS_TICK_PER_SECOND 1024
  8. #define API_HAL_OS_CLK_PER_TICK (API_HAL_OS_CLK_FREQUENCY / API_HAL_OS_TICK_PER_SECOND)
  9. #define API_HAL_OS_TICK_PER_EPOCH (API_HAL_OS_TIMER_MAX / API_HAL_OS_CLK_PER_TICK)
  10. #define API_HAL_OS_MAX_SLEEP (API_HAL_OS_TICK_PER_EPOCH - 1)
  11. #ifdef API_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. #endif
  18. volatile uint32_t api_hal_os_skew = 0;
  19. void api_hal_os_init() {
  20. LL_DBGMCU_APB1_GRP2_FreezePeriph(LL_DBGMCU_APB1_GRP2_LPTIM2_STOP);
  21. api_hal_os_timer_init();
  22. api_hal_os_timer_continuous(API_HAL_OS_CLK_PER_TICK);
  23. #ifdef API_HAL_OS_DEBUG
  24. LL_GPIO_SetPinMode(LED_SLEEP_PORT, LED_SLEEP_PIN, LL_GPIO_MODE_OUTPUT);
  25. LL_GPIO_SetPinMode(LED_TICK_PORT, LED_TICK_PIN, LL_GPIO_MODE_OUTPUT);
  26. #endif
  27. }
  28. void LPTIM2_IRQHandler(void) {
  29. // Autoreload
  30. if(LL_LPTIM_IsActiveFlag_ARRM(API_HAL_OS_TIMER)) {
  31. LL_LPTIM_ClearFLAG_ARRM(API_HAL_OS_TIMER);
  32. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  33. #ifdef API_HAL_OS_DEBUG
  34. LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
  35. #endif
  36. xPortSysTickHandler();
  37. }
  38. }
  39. if(LL_LPTIM_IsActiveFlag_CMPM(API_HAL_OS_TIMER)) {
  40. LL_LPTIM_ClearFLAG_CMPM(API_HAL_OS_TIMER);
  41. }
  42. }
  43. static inline uint32_t api_hal_os_sleep(TickType_t expected_idle_ticks) {
  44. // Stop ticks
  45. api_hal_os_timer_reset();
  46. HAL_SuspendTick();
  47. // Start wakeup timer
  48. api_hal_os_timer_single(expected_idle_ticks * API_HAL_OS_CLK_PER_TICK);
  49. #ifdef API_HAL_OS_DEBUG
  50. LL_GPIO_SetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  51. #endif
  52. // Go to stop2 mode
  53. api_hal_power_deep_sleep();
  54. #ifdef API_HAL_OS_DEBUG
  55. LL_GPIO_ResetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  56. #endif
  57. // Calculate how much time we spent in the sleep
  58. uint32_t after_cnt = api_hal_os_timer_get_cnt() + api_hal_os_skew;
  59. uint32_t after_tick = after_cnt / API_HAL_OS_CLK_PER_TICK;
  60. api_hal_os_skew = after_cnt % API_HAL_OS_CLK_PER_TICK;
  61. // Prepare tick timer for new round
  62. api_hal_os_timer_reset();
  63. // Resume ticks
  64. HAL_ResumeTick();
  65. api_hal_os_timer_continuous(API_HAL_OS_CLK_PER_TICK);
  66. return after_tick;
  67. }
  68. void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
  69. // Check if sleep is available now
  70. if (!api_hal_power_deep_available()) {
  71. return;
  72. }
  73. // Limit mount of ticks to maximum that timer can count
  74. if (expected_idle_ticks > API_HAL_OS_MAX_SLEEP) {
  75. expected_idle_ticks = API_HAL_OS_MAX_SLEEP;
  76. }
  77. // Stop IRQ handling, no one should disturb us till we finish
  78. __disable_irq();
  79. // Confirm OS that sleep is still possible
  80. // And check if timer is in safe zone
  81. // (8 clocks till any IRQ event or ongoing synchronization)
  82. if (eTaskConfirmSleepModeStatus() == eAbortSleep) {
  83. __enable_irq();
  84. return;
  85. }
  86. // Sleep and track how much ticks we spent sleeping
  87. uint32_t completed_ticks = api_hal_os_sleep(expected_idle_ticks);
  88. // Reenable IRQ
  89. __enable_irq();
  90. // Notify system about time spent in sleep
  91. if (completed_ticks > 0) {
  92. if (completed_ticks > expected_idle_ticks) {
  93. vTaskStepTick(expected_idle_ticks);
  94. } else {
  95. vTaskStepTick(completed_ticks);
  96. }
  97. }
  98. }
  99. void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) {
  100. asm("bkpt 1");
  101. while(1) {};
  102. }