1. /*
    2. *********************************************************
    3. ** Filename: stop_mode.c
    4. ** Abstract: 使用STM32L151C8T6MCU,使用RTC唤醒STOP和STANDBY模式下的低功耗,低功耗时长可以人为的进行设置
    5. ** 设置低功耗时长时请参考头文件中关于时长的宏定义
    6. ** 使用注意事项:使用CubeMX生成函数时,在main()函数后会自动生成SystemClock_Config()函数,此程序中调用了该函数。
    7. ** 如果该函数在其他文件中,请将该.h文件加入,以免发生错误;
    8. ** Date : 2018-01-04
    9. ** Author:王翔武
    10. *********************************************************
    11. */
    12. /* Includes ------------------------------------------------------------------*/
    13. #include "pwr_mode_rtc.h"
    14. #include "main.h"
    15. RTC_HandleTypeDef RTCHandle; //RTC结构体变量
    16. //进入STOP模式低功耗,使用RTC功能唤醒,其中stoptime单位为S,如设置1,低功耗1秒后唤醒
    17. void enter_stop_rtc(float stoptime)
    18. {
    19. uint32_t i; //局部变量,用于计算低功耗时长
    20. system_power_config();
    21. /* Disable Wakeup Counter */
    22. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
    23. /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
    24. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
    25. Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
    26. Wakeup Time = ~5s = 0,432ms * WakeUpCounter
    27. ==> WakeUpCounter = ~5s/0,432ms = 11562 */
    28. i = stoptime*2396;
    29. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
    30. /* Enter Stop Mode */
    31. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
    32. SystemClock_Config();
    33. }
    34. //进入STANDBY模式低功耗,使用RTC功能唤醒,其中standbytime单位为S,如设置1,低功耗1秒后唤醒
    35. void enter_standby_rtc(float standbytime)
    36. {
    37. uint32_t i; //局部变量,用于计算低功耗时长
    38. system_power_config();
    39. if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
    40. {
    41. /* Clear Standby flag */
    42. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
    43. }
    44. /* Disable Wakeup Counter */
    45. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
    46. /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
    47. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
    48. Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
    49. Wakeup Time = ~5s = 0,432ms * WakeUpCounter
    50. ==> WakeUpCounter = ~5s/0,432ms = 11562 */
    51. i = standbytime*2396;
    52. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
    53. /* Clear all related wakeup flags */
    54. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
    55. /* Enter the Standby mode */
    56. HAL_PWR_EnterSTANDBYMode();
    57. }
    58. //进入SLEEP模式低功耗,使用RTC功能唤醒,其中sleeptime单位为S,如设置1,低功耗1秒后唤醒
    59. void enter_sleep_rtc(float sleeptime)
    60. {
    61. uint32_t i; //局部变量,用于计算低功耗时长
    62. system_power_config();
    63. /* Disable Wakeup Counter */
    64. HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
    65. /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
    66. RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
    67. Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
    68. Wakeup Time = ~5s = 0,432ms * WakeUpCounter
    69. ==> WakeUpCounter = ~5s/0,432ms = 11562 */
    70. i = sleeptime*2396;
    71. HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
    72. /*Suspend Tick increment to prevent wakeup by Systick interrupt.
    73. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
    74. HAL_SuspendTick();
    75. /* Enter Sleep Mode , wake up is done once Key push button is pressed */
    76. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
    77. /* Resume Tick interrupt if disabled prior to sleep mode entry*/
    78. HAL_ResumeTick();
    79. }
    80. //低功耗关闭项
    81. void system_power_config(void)
    82. {
    83. GPIO_InitTypeDef GPIO_InitStructure = {0};
    84. /* Enable Power Control clock */
    85. __HAL_RCC_PWR_CLK_ENABLE();
    86. /* Enable Ultra low power mode */
    87. HAL_PWREx_EnableUltraLowPower();
    88. /* Enable the fast wake up from Ultra low power mode */
    89. HAL_PWREx_EnableFastWakeUp();
    90. /* Enable GPIOs clock */
    91. __HAL_RCC_GPIOA_CLK_ENABLE();
    92. __HAL_RCC_GPIOB_CLK_ENABLE();
    93. __HAL_RCC_GPIOC_CLK_ENABLE();
    94. __HAL_RCC_GPIOD_CLK_ENABLE();
    95. __HAL_RCC_GPIOH_CLK_ENABLE();
    96. __HAL_RCC_GPIOE_CLK_ENABLE();
    97. /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
    98. /* Note: Debug using ST-Link is not possible during the execution of this */
    99. /* example because communication between ST-link and the device */
    100. /* under test is done through UART. All GPIO pins are disabled (set */
    101. /* to analog input mode) including UART I/O pins. */
    102. GPIO_InitStructure.Pin = GPIO_PIN_All;
    103. GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
    104. GPIO_InitStructure.Pull = GPIO_NOPULL;
    105. HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
    106. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
    107. HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
    108. HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
    109. HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
    110. HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
    111. /* Disable GPIOs clock */
    112. __HAL_RCC_GPIOA_CLK_DISABLE();
    113. __HAL_RCC_GPIOB_CLK_DISABLE();
    114. __HAL_RCC_GPIOC_CLK_DISABLE();
    115. __HAL_RCC_GPIOD_CLK_DISABLE();
    116. __HAL_RCC_GPIOH_CLK_DISABLE();
    117. __HAL_RCC_GPIOE_CLK_DISABLE();
    118. /* Configure RTC */
    119. RTCHandle.Instance = RTC;
    120. /* Configure RTC prescaler and RTC data registers as follow:
    121. - Hour Format = Format 24
    122. - Asynch Prediv = Value according to source clock
    123. - Synch Prediv = Value according to source clock
    124. - OutPut = Output Disable
    125. - OutPutPolarity = High Polarity
    126. - OutPutType = Open Drain */
    127. RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
    128. RTCHandle.Init.AsynchPrediv = 0x7C;
    129. RTCHandle.Init.SynchPrediv = 0x0127;
    130. RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
    131. RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
    132. RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
    133. if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
    134. {
    135. /* Initialization Error */
    136. Error_Handler();
    137. }
    138. }
    139. /************************ Johnking *****END OF FILE****/
    1. #ifndef __stop_mode_rtc_H
    2. #define __stop_mode_rtc_H
    3. /* Includes ------------------------------------------------------------------*/
    4. #include "stm32l1xx_hal.h"
    5. void enter_stop_rtc(float stoptime);
    6. void enter_standby_rtc(float standbytime);
    7. void enter_sleep_rtc(float sleeptime);
    8. void system_power_config(void);
    9. void system_clock_config(void);
    10. #endif
    11. /************************ Johnking *****END OF FILE****/