sleep.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2018, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #if DEVICE_SLEEP
  31. #include "sleep_api.h"
  32. #include "us_ticker_api.h"
  33. #include "us_ticker_data.h"
  34. #include "mbed_critical.h"
  35. #include "mbed_error.h"
  36. extern void save_timer_ctx(void);
  37. extern void restore_timer_ctx(void);
  38. /* Wait loop - assuming tick is 1 us */
  39. static void wait_loop(uint32_t timeout)
  40. {
  41. uint32_t t1, t2, elapsed = 0;
  42. t1 = us_ticker_read();
  43. do {
  44. t2 = us_ticker_read();
  45. elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t)t2 + 0xFFFFFFFF - t1 + 1);
  46. } while (elapsed < timeout);
  47. return;
  48. }
  49. // On L4 platforms we've seen unstable PLL CLK configuraiton
  50. // when DEEP SLEEP exits just few µs after being entered
  51. // So we need to force MSI usage before setting clocks again
  52. static void ForcePeriphOutofDeepSleep(void)
  53. {
  54. uint32_t pFLatency = 0;
  55. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  56. #if (TARGET_STM32L4 || TARGET_STM32L1) /* MSI used for L4 */
  57. /* Get the Clocks configuration according to the internal RCC registers */
  58. HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
  59. // Select HSI ss system clock source as a first step
  60. #ifdef RCC_CLOCKTYPE_PCLK2
  61. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
  62. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  63. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  64. #else
  65. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
  66. | RCC_CLOCKTYPE_PCLK1);
  67. #endif
  68. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  69. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  70. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  71. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK) {
  72. error("clock issue\r\n");
  73. }
  74. #else /* HSI used on others */
  75. /* Get the Clocks configuration according to the internal RCC registers */
  76. HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
  77. /**Initializes the CPU, AHB and APB busses clocks
  78. */
  79. #ifdef RCC_CLOCKTYPE_PCLK2
  80. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  81. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  82. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  83. #else
  84. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  85. | RCC_CLOCKTYPE_PCLK1);
  86. #endif
  87. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  88. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  89. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  90. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK) {
  91. error("clock issue");
  92. }
  93. #endif // TARGET_STM32L4
  94. }
  95. static void ForceOscOutofDeepSleep(void)
  96. {
  97. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98. /* Enable Power Control clock */
  99. __HAL_RCC_PWR_CLK_ENABLE();
  100. /* Get the Oscillators configuration according to the internal RCC registers */
  101. HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
  102. #if (TARGET_STM32L4 || TARGET_STM32L1) /* MSI used for L4 */
  103. /**Initializes the CPU, AHB and APB busses clocks
  104. */
  105. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  106. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  107. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  108. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4; // Intermediate freq, 1MHz range
  109. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  110. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  111. error("clock issue\r\n");
  112. }
  113. #else /* HSI used on others */
  114. /**Initializes the CPU, AHB and APB busses clocks
  115. */
  116. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  117. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  118. RCC_OscInitStruct.HSICalibrationValue = 16;
  119. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  120. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  121. error("clock issue");
  122. }
  123. #endif // TARGET_STM32L4
  124. }
  125. /* The content of this function has been split into 2 separate functions
  126. so that the involved structures are not allocated on the stack in parallel.
  127. This will reduce the maximum stack usage in case on non-optimized / debug
  128. compilers settings */
  129. static void ForceClockOutofDeepSleep(void)
  130. {
  131. ForceOscOutofDeepSleep();
  132. ForcePeriphOutofDeepSleep();
  133. }
  134. void hal_sleep(void)
  135. {
  136. // Disable IRQs
  137. core_util_critical_section_enter();
  138. // Request to enter SLEEP mode
  139. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  140. // Enable IRQs
  141. core_util_critical_section_exit();
  142. }
  143. extern int serial_is_tx_ongoing(void);
  144. void hal_deepsleep(void)
  145. {
  146. /* WORKAROUND:
  147. * MBED serial driver does not handle deepsleep lock
  148. * to prevent entering deepsleep until HW serial FIFO is empty.
  149. * This is tracked in mbed issue 4408.
  150. * For now, we're checking all Serial HW FIFO. If any transfer is ongoing
  151. * we're not entering deep sleep and returning immediately. */
  152. if(serial_is_tx_ongoing()) {
  153. return;
  154. }
  155. // Disable IRQs
  156. core_util_critical_section_enter();
  157. save_timer_ctx();
  158. // Request to enter STOP mode with regulator in low power mode
  159. #if TARGET_STM32L4
  160. int pwrClockEnabled = __HAL_RCC_PWR_IS_CLK_ENABLED();
  161. int lowPowerModeEnabled = PWR->CR1 & PWR_CR1_LPR;
  162. if (!pwrClockEnabled) {
  163. __HAL_RCC_PWR_CLK_ENABLE();
  164. }
  165. if (lowPowerModeEnabled) {
  166. HAL_PWREx_DisableLowPowerRunMode();
  167. }
  168. HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
  169. if (lowPowerModeEnabled) {
  170. HAL_PWREx_EnableLowPowerRunMode();
  171. }
  172. if (!pwrClockEnabled) {
  173. __HAL_RCC_PWR_CLK_DISABLE();
  174. }
  175. #else /* TARGET_STM32L4 */
  176. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  177. #endif /* TARGET_STM32L4 */
  178. // Verify Clock Out of Deep Sleep
  179. ForceClockOutofDeepSleep();
  180. // After wake-up from STOP reconfigure the PLL
  181. SetSysClock();
  182. /* Wait for clock to be stabilized.
  183. * TO DO: a better way of doing this, would be to rely on
  184. * HW Flag. At least this ensures proper operation out of
  185. * deep sleep */
  186. wait_loop(500);
  187. restore_timer_ctx();
  188. // Enable IRQs
  189. core_util_critical_section_exit();
  190. }
  191. #endif