gd32f10x_pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. ******************************************************************************
  3. * @brief PWR functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_pwr.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup PWR
  13. * @brief PWR driver modules
  14. * @{
  15. */
  16. /** @defgroup PWR_Private_Defines
  17. * @{
  18. */
  19. /* Left shift SBF or WUF(PWR_STR) to SBFR or WUFR(PWR_CTLR) */
  20. #define BIT_SHIFT 2
  21. /**
  22. * @}
  23. */
  24. /** @defgroup PWR_Private_Functions
  25. * @{
  26. */
  27. /**
  28. * @brief Reset the PWR peripheral registers.
  29. * @param None
  30. * @retval None
  31. */
  32. void PWR_DeInit(void)
  33. {
  34. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_PWRRST, ENABLE);
  35. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_PWRRST, DISABLE);
  36. }
  37. /**
  38. * @brief Enable or disable write access to the registers in Backup domain.
  39. * @note After reset, any write access to the registers in Backup domain is disabled.
  40. * This bit has to be set to enable write access to these registers.
  41. * @param NewValue: New value of write access state to the registers in Backup domain.
  42. * This parameter can be: ENABLE or DISABLE.
  43. * @retval None
  44. */
  45. void PWR_BackupAccess_Enable(TypeState NewValue)
  46. {
  47. if (NewValue != DISABLE) {
  48. /* Enable the Backup Domain Access */
  49. PWR->CTLR |= PWR_CTLR_BKPWE;
  50. } else {
  51. /* Disable the Backup Domain Access */
  52. PWR->CTLR &= ~((uint32_t)PWR_CTLR_BKPWE);
  53. }
  54. }
  55. /**
  56. * @brief Enable or disable the LVD(Low Voltage Detector), and configure the voltage
  57. * threshold detected by the LVD.
  58. * @note The LVDE bit controls whether LVD is enabled or not, while the LVDT[2:0]
  59. * bits select the LVDT from 2.2V to 2.9V.
  60. * @note The LVD is stopped in Standby mode.
  61. * @param PWR_LVDT: the LVD threshold.
  62. * This parameter can be one of the following values:
  63. * @arg PWR_LVDT_0
  64. * @arg PWR_LVDT_1
  65. * @arg PWR_LVDT_2
  66. * @arg PWR_LVDT_3
  67. * @arg PWR_LVDT_4
  68. * @arg PWR_LVDT_5
  69. * @arg PWR_LVDT_6
  70. * @arg PWR_LVDT_7
  71. * @param NewValue: New value of the LVD state.
  72. * This parameter can be: ENABLE or DISABLE.
  73. * @retval None
  74. */
  75. void PWR_LVDConfig(uint32_t PWR_LVDT, TypeState NewValue)
  76. {
  77. uint32_t temp = 0;
  78. temp = PWR->CTLR;
  79. /* Clear LVDT[7:5] bits */
  80. temp &= ~((uint32_t)PWR_CTLR_LVDT);
  81. /* Set LVDT[7:5] bits according to PWR_LVDT value */
  82. temp |= PWR_LVDT;
  83. /* Store the new value */
  84. PWR->CTLR = temp;
  85. /* Enable or disable the LVD */
  86. if (NewValue != DISABLE) {
  87. /* Enable the LVD */
  88. PWR->CTLR |= PWR_CTLR_LVDE;
  89. } else {
  90. /* Disable the LVD */
  91. PWR->CTLR &= ~((uint32_t)PWR_CTLR_LVDE);
  92. }
  93. }
  94. /**
  95. * @brief Enable or disable the WakeUp Pin(PA0 pin) function.
  96. * @note If WUPE is set before entering the power saving mode,
  97. * a rising edge on the WKUP Pin wakes up the system from the power
  98. * saving mode. As the WKUP pin is active high, it will setup
  99. * an input pull down mode when this bit is high.
  100. * @param NewValue: New value of the WKUP Pin state.
  101. * This parameter can be: ENABLE or DISABLE.
  102. * @retval None
  103. */
  104. void PWR_WKUP_Pin_Enable(TypeState NewValue)
  105. {
  106. if (NewValue != DISABLE) {
  107. /* Enable the WKUP pin */
  108. PWR->STR |= PWR_STR_WUPE;
  109. } else {
  110. /* Disable the WKUP pin */
  111. PWR->STR &= ~PWR_STR_WUPE;
  112. }
  113. }
  114. /**
  115. *@verbatim
  116. [..] The GD32F10x series of devices provide three types of power saving modes.
  117. *** Sleep mode ***
  118. ==================
  119. [..] The Sleep mode is based on the SLEEPING mode of the Cortex-M3. In Sleep
  120. mode, only clock of Cortex-M3 is off.
  121. (+) Entry:
  122. (++) To enter the Sleep mode, it is only necessary to clear SLEEPDEEP bit
  123. in the Cortex-M3 System Control Register, and execute a WFI or WFE
  124. instruction.
  125. (++) The Sleep mode is entered using the PWR_SLEEPMode_Entry() function.
  126. (+) Wakeup:
  127. (++) Any interrupt for WFI or Any event for WFE.
  128. (+) Wakeup Latency:
  129. (++) None
  130. *** Deep-sleep mode ***
  131. =================
  132. [..] The Deep-sleep mode is based on the SLEEPDEEP mode of the Cortex-M3. In
  133. Deep-sleep mode, all clocks in the 1.2V domain are off, and all of HSI,
  134. HSE and PLL are disabled. The LDO can operate normally or in low
  135. power mode depending on the LDOLP bit in the PWR_CTLR register.
  136. (+) Entry:
  137. (++) Before entering the Deep-sleep mode, it is necessary to set the
  138. SLEEPDEEP bit in the Cortex-M3 System Control Register, and clear
  139. the SDBM bit in the PWR_CTLR register.
  140. (++) The Deep-sleep mode is entered using the PWR_DEEPSLEEPMode_Entry()
  141. function.
  142. (+) Wakeup:
  143. (++) Any interrupt or event from EXTI Lines.
  144. (+) Wakeup Latency:
  145. (++) HSI wakeup time; LDO wakeup time if LDO is in low power mode.
  146. *** Standby mode ***
  147. ====================
  148. [..] The Standby mode is based on the SLEEPDEEP mode of the Cortex-M3. In Standby
  149. mode, the whole 1.2V domain is power off, the LDO is shut down, and all of
  150. HSI, HSE and PLL are disabled. Besides, the contents of SRAM and
  151. registers(except Backup Registers) are lost in Standby mode.
  152. (+) Entry:
  153. (++) Before entering the Standby mode, it is necessary to set the
  154. SLEEPDEEP bit in the Cortex-M3 System Control Register, and set
  155. the SDBM bit in the PWR_CTLR register, and clear WUF bit in the
  156. PWR_STR register. .
  157. (++) The Standby mode is entered using the PWR_STDBYMode_Entry() function.
  158. (+) Wakeup:
  159. (++) NRST pin, WKUP pin rising edge, RTC alarm, IWDG reset.
  160. (+) Wakeup Latency:
  161. (++) Power on sequence
  162. *@endverbatim
  163. */
  164. /**
  165. * @brief Enter Sleep mode.
  166. * @note By default, this function selects the Sleep-now entry mechanism (SLEEPONEXIT = 0).
  167. * @param PWR_SLEEPENTRY: WFI or WFE instruction.
  168. * This parameter can be one of the following values:
  169. * @arg PWR_SLEEPENTRY_WFI: enter Sleep mode with WFI instruction
  170. * @arg PWR_SLEEPENTRY_WFE: enter Sleep mode with WFE instruction
  171. * @retval None
  172. */
  173. void PWR_SLEEPMode_Entry(uint8_t PWR_SLEEPENTRY)
  174. {
  175. /* Clear SLEEPDEEP bit of Cortex-M3 System Control Register */
  176. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  177. /* Select WFI or WFE to enter Sleep mode */
  178. if (PWR_SLEEPENTRY == PWR_SLEEPENTRY_WFI) {
  179. __WFI();
  180. } else {
  181. __WFE();
  182. }
  183. }
  184. /**
  185. * @brief Enter Deep-sleep mode.
  186. * @note When exiting Deep-sleep mode, the HSI is selected as the system clock.
  187. * @note An additional wakeup delay will be incurred if the LDO operates in
  188. * low power mode.
  189. * @param PWR_LDO: the LDO state in Deep-sleep mode.
  190. * This parameter can be one of the following values:
  191. * @arg PWR_LDO_ON: Deep-sleep mode with LDO ON
  192. * @arg PWR_LDO_LOWPOWER: Deep-sleep mode with LDO in low power mode
  193. * @param PWR_DEEPSLEEPENTRY: WFI or WFE instruction.
  194. * This parameter can be one of the following values:
  195. * @arg PWR_DEEPSLEEPENTRY_WFI: enter Deep-sleep mode with WFI instruction
  196. * @arg PWR_DEEPSLEEPENTRY_WFE: enter Deep-sleep mode with WFE instruction
  197. * @retval None
  198. */
  199. void PWR_DEEPSLEEPMode_Entry(uint32_t PWR_LDO, uint8_t PWR_DEEPSLEEPENTRY)
  200. {
  201. uint32_t temp = 0;
  202. /* Select the LDO state in Deep-sleep mode */
  203. temp = PWR->CTLR;
  204. /* Clear SDBM and LDOLP bits, and select Deep-sleep mode */
  205. temp &= ~((uint32_t)(PWR_CTLR_SDBM | PWR_CTLR_LDOLP));
  206. /* Set LDOLP bit according to PWR_LDO value, and select the LDO's state */
  207. temp |= PWR_LDO;
  208. /* Store the new value */
  209. PWR->CTLR = temp;
  210. /* Set SLEEPDEEP bit of Cortex-M3 System Control Register */
  211. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  212. /* Select WFI or WFE to enter Deep-sleep mode */
  213. if (PWR_DEEPSLEEPENTRY == PWR_DEEPSLEEPENTRY_WFI) {
  214. __WFI();
  215. } else {
  216. __SEV();
  217. __WFE();
  218. __WFE();
  219. }
  220. /* Reset SLEEPDEEP bit of Cortex-M3 System Control Register */
  221. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  222. }
  223. /**
  224. * @brief Enter Standby mode.
  225. * @note The Standby mode achieves the lowest power consumption, but spends
  226. * longest time to wake up.
  227. * @note When exiting from the Standby mode, a power-on reset occurs and the
  228. * Cortex-M3 will execute instructions code from the 0x0000_0000 address.
  229. * @param PWR_STDBYENTRY: WFI or WFE instruction.
  230. * This parameter can be one of the following values:
  231. * @arg PWR_STDBYENTRY_WFI: enter Standby mode with WFI instruction
  232. * @arg PWR_STDBYENTRY_WFE: enter Standby mode with WFE instruction
  233. * @retval None
  234. */
  235. void PWR_STDBYMode_Entry(uint8_t PWR_STDBYENTRY)
  236. {
  237. /* Set SLEEPDEEP bit of Cortex-M3 System Control Register */
  238. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  239. /* Set SDBM bit, and select Standby mode */
  240. PWR->CTLR |= PWR_CTLR_SDBM;
  241. /* Reset Wakeup flag */
  242. PWR->CTLR |= PWR_CTLR_WUFR;
  243. /* Select WFI or WFE to enter Standby mode */
  244. if (PWR_STDBYENTRY == PWR_STDBYENTRY_WFI) {
  245. __WFI();
  246. } else {
  247. __WFE();
  248. }
  249. }
  250. /**
  251. * @brief Get the bit flag of some PWR_STR registers.
  252. * @param PWR_FLAG: the flag of PWR_STR registers.
  253. * This parameter can be one of the following values:
  254. * @arg PWR_FLAG_WKUP: WakeUp flag. This flag indicates that a wakeup
  255. * event was received from the WKUP pin or from the RTC alarm,
  256. * IWDG reset or NRST pin.
  257. * @arg PWR_FLAG_STB: StandBy flag. This flag indicates that the
  258. * system has been in Standby mode.
  259. * @arg PWR_FLAG_LVDF: LVD State flag. This flag is valid only if LVD
  260. * is enabled by the PWR_LVD_Config()function.
  261. * @retval The new value of PWR_FLAG (SET or RESET).
  262. */
  263. TypeState PWR_GetBitState(uint32_t PWR_FLAG)
  264. {
  265. /* Check and get the selected PWR flag */
  266. if ((PWR->STR & PWR_FLAG) != (uint32_t)RESET) {
  267. /* PWR_FLAG bit is SET */
  268. return SET;
  269. } else {
  270. /* PWR_FLAG bit is RESET */
  271. return RESET;
  272. }
  273. }
  274. /**
  275. * @brief Clear the bit flag of some PWR_STR registers.
  276. * @param PWR_FLAG: the flag of PWR_STR registers.
  277. * This parameter can be one of the following values:
  278. * @arg PWR_FLAG_WKUP: Wake_Up flag
  279. * @arg PWR_FLAG_STB: StandBy flag
  280. * @retval None
  281. */
  282. void PWR_ClearBitState(uint32_t PWR_FLAG)
  283. {
  284. /* Clear the selected PWR flag */
  285. PWR->CTLR |= PWR_FLAG << BIT_SHIFT;
  286. }
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */