api-hal-os.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <api-hal-os.h>
  2. #include <api-hal-os-timer.h>
  3. #include <api-hal-power.h>
  4. #include <stm32wbxx_ll_cortex.h>
  5. #include <FreeRTOS.h>
  6. #include <cmsis_os.h>
  7. #define API_HAL_OS_CLK_FREQUENCY 32768
  8. #define API_HAL_OS_TICK_PER_SECOND 1024
  9. #define API_HAL_OS_CLK_PER_TICK (API_HAL_OS_CLK_FREQUENCY / API_HAL_OS_TICK_PER_SECOND)
  10. #define API_HAL_OS_TICK_PER_EPOCH (API_HAL_OS_TIMER_MAX / API_HAL_OS_CLK_PER_TICK)
  11. #define API_HAL_OS_MAX_SLEEP (API_HAL_OS_TICK_PER_EPOCH - 1)
  12. #ifdef API_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 api_hal_os_timer_callback() {
  21. LL_GPIO_TogglePin(LED_SECOND_PORT, LED_SECOND_PIN);
  22. }
  23. #endif
  24. volatile uint32_t api_hal_os_skew = 0;
  25. void api_hal_os_init() {
  26. LL_DBGMCU_APB1_GRP2_FreezePeriph(LL_DBGMCU_APB1_GRP2_LPTIM2_STOP);
  27. api_hal_os_timer_init();
  28. api_hal_os_timer_continuous(API_HAL_OS_CLK_PER_TICK);
  29. #ifdef API_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(api_hal_os_timer_callback, osTimerPeriodic, NULL, NULL);
  34. osTimerStart(second_timer, 1024);
  35. #endif
  36. }
  37. void LPTIM2_IRQHandler(void) {
  38. // Autoreload
  39. if(LL_LPTIM_IsActiveFlag_ARRM(API_HAL_OS_TIMER)) {
  40. LL_LPTIM_ClearFLAG_ARRM(API_HAL_OS_TIMER);
  41. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  42. #ifdef API_HAL_OS_DEBUG
  43. LL_GPIO_TogglePin(LED_TICK_PORT, LED_TICK_PIN);
  44. #endif
  45. xPortSysTickHandler();
  46. }
  47. }
  48. if(LL_LPTIM_IsActiveFlag_CMPM(API_HAL_OS_TIMER)) {
  49. LL_LPTIM_ClearFLAG_CMPM(API_HAL_OS_TIMER);
  50. }
  51. }
  52. static inline uint32_t api_hal_os_sleep(TickType_t expected_idle_ticks) {
  53. // Stop ticks
  54. api_hal_os_timer_reset();
  55. LL_SYSTICK_DisableIT();
  56. // Start wakeup timer
  57. api_hal_os_timer_single(expected_idle_ticks * API_HAL_OS_CLK_PER_TICK);
  58. #ifdef API_HAL_OS_DEBUG
  59. LL_GPIO_ResetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  60. #endif
  61. // Go to sleep mode
  62. api_hal_power_sleep();
  63. #ifdef API_HAL_OS_DEBUG
  64. LL_GPIO_SetOutputPin(LED_SLEEP_PORT, LED_SLEEP_PIN);
  65. #endif
  66. // Calculate how much time we spent in the sleep
  67. uint32_t after_cnt = api_hal_os_timer_get_cnt() + api_hal_os_skew;
  68. uint32_t after_tick = after_cnt / API_HAL_OS_CLK_PER_TICK;
  69. api_hal_os_skew = after_cnt % API_HAL_OS_CLK_PER_TICK;
  70. bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(API_HAL_OS_TIMER);
  71. bool arrm = LL_LPTIM_IsActiveFlag_ARRM(API_HAL_OS_TIMER);
  72. if (cmpm && arrm) after_tick += expected_idle_ticks;
  73. // Prepare tick timer for new round
  74. api_hal_os_timer_reset();
  75. // Resume ticks
  76. LL_SYSTICK_EnableIT();
  77. api_hal_os_timer_continuous(API_HAL_OS_CLK_PER_TICK);
  78. return after_tick;
  79. }
  80. void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
  81. if(!api_hal_power_sleep_available()) {
  82. __WFI();
  83. return;
  84. }
  85. // Limit mount of ticks to maximum that timer can count
  86. if (expected_idle_ticks > API_HAL_OS_MAX_SLEEP) {
  87. expected_idle_ticks = API_HAL_OS_MAX_SLEEP;
  88. }
  89. // Stop IRQ handling, no one should disturb us till we finish
  90. __disable_irq();
  91. // Confirm OS that sleep is still possible
  92. if (eTaskConfirmSleepModeStatus() == eAbortSleep) {
  93. __enable_irq();
  94. return;
  95. }
  96. // Sleep and track how much ticks we spent sleeping
  97. uint32_t completed_ticks = api_hal_os_sleep(expected_idle_ticks);
  98. // Reenable IRQ
  99. __enable_irq();
  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. }
  109. void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) {
  110. asm("bkpt 1");
  111. while(1) {};
  112. }