stm32f4xx_lp_modes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. ******************************************************************************
  3. * @file PWR/PWR_CurrentConsumption/stm32f4xx_lp_modes.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the STM32F4xx Low Power Modes:
  7. * - Sleep Mode
  8. * - STOP mode with RTC
  9. * - STANDBY mode without RTC and BKPSRAM
  10. * - STANDBY mode with RTC
  11. * - STANDBY mode with RTC and BKPSRAM
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "main.h"
  43. /** @addtogroup STM32F4xx_HAL_Examples
  44. * @{
  45. */
  46. /** @addtogroup PWR_CurrentConsumption
  47. * @{
  48. */
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* RTC handler declaration */
  54. RTC_HandleTypeDef RTCHandle;
  55. /* Private function prototypes -----------------------------------------------*/
  56. static void SYSCLKConfig_STOP(void);
  57. /* Private functions ---------------------------------------------------------*/
  58. /**
  59. * @brief This function configures the system to enter Sleep mode for
  60. * current consumption measurement purpose.
  61. * Sleep Mode
  62. * ==========
  63. * - System Running at PLL (168MHz)
  64. * - Flash 5 wait state
  65. * - Instruction and Data caches ON
  66. * - Prefetch ON
  67. * - Code running from Internal FLASH
  68. * - All peripherals disabled.
  69. * - Wakeup using EXTI Line (Key Button)
  70. * @param None
  71. * @retval None
  72. */
  73. void SleepMode_Measure(void)
  74. {
  75. /* Configure Key Button */
  76. //BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
  77. /* Suspend Tick increment to prevent wakeup by Systick interrupt.
  78. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base) */
  79. HAL_SuspendTick();
  80. /* Request to enter SLEEP mode */
  81. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  82. /* Resume Tick interrupt if disabled prior to sleep mode entry */
  83. HAL_ResumeTick();
  84. /* Initialize LED4 */
  85. //BSP_LED_Init(LED4);
  86. /* Turn LED4 On */
  87. //BSP_LED_On(LED4);
  88. /* Inserted Delay: 2s */
  89. //HAL_Delay(2000);
  90. }
  91. /**
  92. * @brief This function configures the system to enter Stop mode with RTC
  93. * clocked by LSE or LSI for current consumption measurement purpose.
  94. * STOP Mode with RTC clocked by LSE/LSI
  95. * =====================================
  96. * - RTC Clocked by LSE or LSI
  97. * - Regulator in LP mode
  98. * - HSI, HSE OFF and LSI OFF if not used as RTC Clock source
  99. * - No IWDG
  100. * - FLASH in deep power down mode
  101. * - Automatic Wakeup using RTC clocked by LSE/LSI (~20s)
  102. * @param None
  103. * @retval None
  104. */
  105. void StopMode_Measure(void)
  106. {
  107. RTCHandle.Instance = RTC;
  108. /* Configure RTC prescaler and RTC data registers as follow:
  109. - Hour Format = Format 24
  110. - Asynch Prediv = Value according to source clock
  111. - Synch Prediv = Value according to source clock
  112. - OutPut = Output Disable
  113. - OutPutPolarity = High Polarity
  114. - OutPutType = Open Drain */
  115. RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  116. RTCHandle.Init.AsynchPrediv = 127;
  117. RTCHandle.Init.SynchPrediv = 255;
  118. RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  119. RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  120. RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  121. if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
  122. {
  123. /* Initialization Error */
  124. Error_Handler();
  125. }
  126. /*## Configure the Wake up timer ###########################################*/
  127. /* RTC Wakeup Interrupt Generation:
  128. Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
  129. Wakeup Time = Wakeup Time Base * WakeUpCounter
  130. = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
  131. ==> WakeUpCounter = Wakeup Time / Wakeup Time Base
  132. To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017:
  133. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  134. Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms
  135. Wakeup Time = ~20s = 0,488ms * WakeUpCounter
  136. ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */
  137. /* Disable Wake-up timer */
  138. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  139. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x1000, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  140. /* FLASH Deep Power Down Mode enabled */
  141. HAL_PWREx_EnableFlashPowerDown();
  142. /* Enter Stop Mode */
  143. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  144. /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
  145. PLL as system clock source (HSE and PLL are disabled in STOP mode) */
  146. SYSCLKConfig_STOP();
  147. /* Disable Wake-up timer */
  148. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  149. /* Initialize LED4 */
  150. //BSP_LED_Init(LED4);
  151. /* Turn LED4 On */
  152. //BSP_LED_On(LED4);
  153. }
  154. /**
  155. * @brief This function configures the system to enter Standby mode for
  156. * current consumption measurement purpose.
  157. * STANDBY Mode
  158. * ============
  159. * - Backup SRAM and RTC OFF
  160. * - IWDG and LSI OFF
  161. * - Wakeup using WakeUp Pin (PA.00)
  162. * @param None
  163. * @retval None
  164. */
  165. void StandbyMode_Measure(void)
  166. {
  167. /* Enable Power Clock */
  168. __HAL_RCC_PWR_CLK_ENABLE();
  169. /* Allow access to Backup */
  170. HAL_PWR_EnableBkUpAccess();
  171. /* Reset RTC Domain */
  172. __HAL_RCC_BACKUPRESET_FORCE();
  173. __HAL_RCC_BACKUPRESET_RELEASE();
  174. /* Disable all used wakeup sources: Pin1(PA.0) */
  175. HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
  176. /* Clear all related wakeup flags */
  177. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  178. /* Re-enable all used wakeup sources: Pin1(PA.0) */
  179. HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
  180. /* Request to enter STANDBY mode */
  181. HAL_PWR_EnterSTANDBYMode();
  182. }
  183. /**
  184. * @brief This function configures the system to enter Standby mode with RTC
  185. * clocked by LSE or LSI for current consumption measurement purpose.
  186. * STANDBY Mode with RTC clocked by LSE/LSI
  187. * ========================================
  188. * - RTC Clocked by LSE/LSI
  189. * - IWDG OFF
  190. * - Backup SRAM OFF
  191. * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s)
  192. * @param None
  193. * @retval None
  194. */
  195. void StandbyRTCMode_Measure(void)
  196. {
  197. RTCHandle.Instance = RTC;
  198. /* Configure RTC prescaler and RTC data registers as follow:
  199. - Hour Format = Format 24
  200. - Asynch Prediv = Value according to source clock
  201. - Synch Prediv = Value according to source clock
  202. - OutPut = Output Disable
  203. - OutPutPolarity = High Polarity
  204. - OutPutType = Open Drain */
  205. RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  206. RTCHandle.Init.AsynchPrediv = 127;
  207. RTCHandle.Init.SynchPrediv = 255;
  208. RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  209. RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  210. RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  211. if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
  212. {
  213. /* Initialization Error */
  214. Error_Handler();
  215. }
  216. /*## Configure the Wake up timer ###########################################*/
  217. /* RTC Wakeup Interrupt Generation:
  218. Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
  219. Wakeup Time = Wakeup Time Base * WakeUpCounter
  220. = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
  221. ==> WakeUpCounter = Wakeup Time / Wakeup Time Base
  222. To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017:
  223. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  224. Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms
  225. Wakeup Time = ~20s = 0,488ms * WakeUpCounter
  226. ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */
  227. /* Disable Wake-up timer */
  228. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  229. /*## Clear all related wakeup flags ########################################*/
  230. /* Clear PWR wake up Flag */
  231. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  232. /* Clear RTC Wake Up timer Flag */
  233. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF);
  234. /*## Setting the Wake up time ##############################################*/
  235. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA017, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  236. /*## Enter the Standby mode ################################################*/
  237. /* Request to enter STANDBY mode */
  238. HAL_PWR_EnterSTANDBYMode();
  239. }
  240. /**
  241. * @brief This function configures the system to enter Standby mode with RTC
  242. * clocked by LSE or LSI and with Backup SRAM ON for current consumption
  243. * measurement purpose.
  244. * STANDBY Mode with RTC clocked by LSE/LSI and BKPSRAM
  245. * ====================================================
  246. * - RTC Clocked by LSE or LSI
  247. * - Backup SRAM ON
  248. * - IWDG OFF
  249. * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s)
  250. * @param None
  251. * @retval None
  252. */
  253. void StandbyRTCBKPSRAMMode_Measure(void)
  254. {
  255. /* Configure RTC prescaler and RTC data registers as follow:
  256. - Hour Format = Format 24
  257. - Asynch Prediv = Value according to source clock
  258. - Synch Prediv = Value according to source clock
  259. - OutPut = Output Disable
  260. - OutPutPolarity = High Polarity
  261. - OutPutType = Open Drain */
  262. RTCHandle.Instance = RTC;
  263. RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  264. RTCHandle.Init.AsynchPrediv = 127;
  265. RTCHandle.Init.SynchPrediv = 255;
  266. RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  267. RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  268. RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  269. if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
  270. {
  271. /* Initialization Error */
  272. Error_Handler();
  273. }
  274. /*## Configure the Wake up timer ###########################################*/
  275. /* RTC Wakeup Interrupt Generation:
  276. Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
  277. Wakeup Time = Wakeup Time Base * WakeUpCounter
  278. = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
  279. ==> WakeUpCounter = Wakeup Time / Wakeup Time Base
  280. To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017:
  281. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  282. Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms
  283. Wakeup Time = ~20s = 0,488ms * WakeUpCounter
  284. ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */
  285. /* Disable Wake-up timer */
  286. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  287. /*## Clear all related wakeup flags ########################################*/
  288. /* Clear PWR wake up Flag */
  289. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  290. /* Clear RTC Wake Up timer Flag */
  291. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF);
  292. /*## Setting the Wake up time ##############################################*/
  293. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA017, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  294. /* Enable BKPRAM Clock */
  295. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  296. /* Enable the Backup SRAM low power Regulator */
  297. HAL_PWREx_EnableBkUpReg();
  298. /*## Enter the Standby mode ################################################*/
  299. /* Request to enter STANDBY mode */
  300. HAL_PWR_EnterSTANDBYMode();
  301. }
  302. /**
  303. * @brief Configures system clock after wake-up from STOP: enable HSE, PLL
  304. * and select PLL as system clock source.
  305. * @param None
  306. * @retval None
  307. */
  308. static void SYSCLKConfig_STOP(void)
  309. {
  310. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  311. RCC_OscInitTypeDef RCC_OscInitStruct;
  312. uint32_t pFLatency = 0;
  313. /* Get the Oscillators configuration according to the internal RCC registers */
  314. HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
  315. /* After wake-up from STOP reconfigure the system clock: Enable HSE and PLL */
  316. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  317. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  318. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  319. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  320. {
  321. Error_Handler();
  322. }
  323. /* Get the Clocks configuration according to the internal RCC registers */
  324. HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
  325. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  326. clocks dividers */
  327. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  328. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  329. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK)
  330. {
  331. Error_Handler();
  332. }
  333. }
  334. /**
  335. * @}
  336. */
  337. /**
  338. * @}
  339. */
  340. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/