py32f002b_hal_pwr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_pwr.c
  4. * @author MCU Application Team
  5. * @brief PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Power Controller (PWR) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  15. * All rights reserved.</center></h2>
  16. *
  17. * This software component is licensed by Puya under BSD 3-Clause license,
  18. * the "License"; You may not use this file except in compliance with the
  19. * License. You may obtain a copy of the License at:
  20. * opensource.org/licenses/BSD-3-Clause
  21. *
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  26. * All rights reserved.</center></h2>
  27. *
  28. * This software component is licensed by ST under BSD 3-Clause license,
  29. * the "License"; You may not use this file except in compliance with the
  30. * License. You may obtain a copy of the License at:
  31. * opensource.org/licenses/BSD-3-Clause
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "py32f0xx_hal.h"
  37. /** @addtogroup PY32F002B_HAL_Driver
  38. * @{
  39. */
  40. /** @addtogroup PWR
  41. * @{
  42. */
  43. #ifdef HAL_PWR_MODULE_ENABLED
  44. /* Private typedef -----------------------------------------------------------*/
  45. /* Private define ------------------------------------------------------------*/
  46. /* Private macro -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private function prototypes -----------------------------------------------*/
  49. /* Exported functions --------------------------------------------------------*/
  50. /** @addtogroup PWR_Exported_Functions PWR Exported Functions
  51. * @{
  52. */
  53. /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
  54. * @brief Initialization and de-initialization functions
  55. *
  56. @verbatim
  57. ===============================================================================
  58. ##### Initialization and de-initialization functions #####
  59. ===============================================================================
  60. [..]
  61. @endverbatim
  62. * @{
  63. */
  64. /**
  65. * @brief Deinitialize the HAL PWR peripheral registers to their default reset
  66. values.
  67. * @retval None
  68. */
  69. void HAL_PWR_DeInit(void)
  70. {
  71. __HAL_RCC_PWR_FORCE_RESET();
  72. __HAL_RCC_PWR_RELEASE_RESET();
  73. }
  74. /**
  75. * @}
  76. */
  77. /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
  78. * @brief Low Power modes configuration functions
  79. *
  80. @verbatim
  81. ===============================================================================
  82. ##### Peripheral Control functions #####
  83. ===============================================================================
  84. [..]
  85. *** PVD configuration ***
  86. =========================
  87. [..]
  88. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  89. threshold selected by the PVD Level (PVDT[2:0]bits in PWR CR2 register).
  90. (+) PVDO flag is available to indicate if VDD/VDDA is higher or lower
  91. than the PVD threshold. This event is internally connected to the EXTI
  92. line 16 and can generate an interrupt if enabled.
  93. (+) The PVD is stopped in Standby mode.
  94. *** WakeUp pin configuration ***
  95. ================================
  96. [..]
  97. (+) WakeUp pins are used to wakeup the system from Standby mode or
  98. Shutdown mode. WakeUp pins polarity can be set to configure event
  99. detection on high level (rising edge) or low level (falling edge).
  100. *** Low Power mode configuration ***
  101. =====================================
  102. [..]
  103. The devices feature 7 low-power modes:
  104. (+) Low-power run mode: core and peripherals are running at low frequency.
  105. Regulator is in low power mode.
  106. (+) Sleep mode: Cortex-M0+ core stopped, peripherals kept running,
  107. regulator is main mode.
  108. (+) Low-power Sleep mode: Cortex-M0+ core stopped, peripherals kept running
  109. and regulator in low power mode.
  110. (+) Stop 0 mode: all clocks are stopped except LSI and LSE, regulator is
  111. main mode.
  112. (+) Stop 1 mode: all clocks are stopped except LSI and LSE, main regulator
  113. off, low power regulator on.
  114. *** Low-power run mode ***
  115. ==========================
  116. [..]
  117. (+) Entry: (from main run mode)
  118. (++) set LPR bit with HAL_PWREx_EnableLowPowerRunMode() API after
  119. having decreased the system clock below 2 MHz.
  120. (+) Exit:
  121. (++) clear LPR bit then wait for REGLPF bit to be reset with
  122. HAL_PWREx_DisableLowPowerRunMode() API. Only then can the
  123. system clock frequency be increased above 2 MHz.
  124. *** Sleep mode / Low-power sleep mode ***
  125. =========================================
  126. [..]
  127. (+) Entry:
  128. The Sleep & Low-power Sleep modes are entered through
  129. HAL_PWR_EnterSLEEPMode() API specifying whether or not the regulator
  130. is forced to low-power mode and if exit is interrupt or event
  131. triggered.
  132. (++) PWR_MAINREGULATOR_ON: Sleep mode (regulator in main mode).
  133. (++) PWR_LOWPOWERREGULATOR_ON: Low-power Sleep mode (regulator in low
  134. power mode). In this case, the system clock frequency must have
  135. been decreased below 2 MHz beforehand.
  136. (++) PWR_SLEEPENTRY_WFI: Core enters sleep mode with WFI instruction
  137. (++) PWR_SLEEPENTRY_WFE: Core enters sleep mode with WFE instruction
  138. (+) WFI Exit:
  139. (++) Any interrupt enabled in nested vectored interrupt controller (NVIC)
  140. (+) WFE Exit:
  141. (++) Any wakeup event if cortex is configured with SEVONPEND = 0
  142. (++) Interrupt even when disabled in NVIC if cortex is configured with
  143. SEVONPEND = 1
  144. [..] When exiting the Low-power Sleep mode by issuing an interrupt or a wakeup event,
  145. the MCU is in Low-power Run mode.
  146. *** Stop 0 & Stop 1 modes ***
  147. =============================
  148. [..]
  149. (+) Entry:
  150. The Stop modes are entered through the following APIs:
  151. (++) HAL_PWR_EnterSTOPMode() with following settings:
  152. (+++) PWR_MAINREGULATOR_ON to enter STOP0 mode.
  153. (+++) PWR_LOWPOWERREGULATOR_ON to enter STOP1 mode.
  154. (+) Exit (interrupt or event-triggered, specified when entering STOP mode):
  155. (++) PWR_STOPENTRY_WFI: enter Stop mode with WFI instruction
  156. (++) PWR_STOPENTRY_WFE: enter Stop mode with WFE instruction
  157. (+) WFI Exit:
  158. (++) Any EXTI line (internal or external) configured in interrupt mode
  159. with corresponding interrupt enable in NVIC
  160. (+) WFE Exit:
  161. (++) Any EXTI line (internal or external) configured in event mode if
  162. cortex is configured with SEVONPEND = 0
  163. (++) Any EXTI line configured in interrupt mode (even if the
  164. corresponding EXTI Interrupt vector is disabled in the NVIC) if
  165. cortex is configured with SEVONPEND = 0. The interrupt source can
  166. be external interrupts or peripherals with wakeup capability.
  167. [..] When exiting Stop, the MCU is either in Run mode or in Low-power Run mode
  168. depending on the LPR bit setting.
  169. @endverbatim
  170. * @{
  171. */
  172. /**
  173. * @brief Configure LPR voltage,sram retention voltage,and wakeup correlation
  174. timing in Stop mode.
  175. * @param sStopModeConfig pointer to a PWR_StopModeConfigTypeDef structure that
  176. contains the Stop mode configuration information.
  177. * @retval HAL_OK
  178. */
  179. HAL_StatusTypeDef HAL_PWR_ConfigStopMode(PWR_StopModeConfigTypeDef *sStopModeConfig)
  180. {
  181. /* Check the parameters */
  182. assert_param(IS_PWR_WAKEUP_HSIEN_TIMING(sStopModeConfig->WakeUpHsiEnableTime));
  183. #if defined(PWR_DEEPSTOP_SUPPORT)
  184. assert_param(IS_PWR_SRAM_RETENTION_VOLT(sStopModeConfig->SramRetentionVoltDlp));
  185. #endif /* PWR_DEEPSTOP_SUPPORT */
  186. assert_param(IS_PWR_SRAM_RETENTION_VOLT(sStopModeConfig->SramRetentionVolt));
  187. assert_param(IS_PWR_WAKEUP_FLASH_DELAY(sStopModeConfig->FlashDelay));
  188. /* Set the STOP mode and STOP wake-up timing related configurations */
  189. #if defined(PWR_DEEPSTOP_SUPPORT)
  190. MODIFY_REG(PWR->CR1, PWR_CR1_SRAM_RETV_DLP, (sStopModeConfig->SramRetentionVoltDlp << PWR_CR1_SRAM_RETV_DLP_Pos));
  191. #endif /* PWR_DEEPSTOP_SUPPORT */
  192. MODIFY_REG(PWR->CR1, (PWR_CR1_HSION_CTRL | PWR_CR1_SRAM_RETV | PWR_CR1_FLS_SLPTIME),
  193. (sStopModeConfig->WakeUpHsiEnableTime) | \
  194. (sStopModeConfig->SramRetentionVolt << PWR_CR1_SRAM_RETV_Pos) | \
  195. (sStopModeConfig->FlashDelay));
  196. return HAL_OK;
  197. }
  198. /**
  199. * @brief Configure the bias current load source and bias current values.
  200. * @param sBIASConfig pointer to a PWR_BIASConfigTypeDef structure that
  201. contains the bias current configuration information.
  202. * @retval HAL_OK
  203. */
  204. HAL_StatusTypeDef HAL_PWR_ConfigBIAS(PWR_BIASConfigTypeDef *sBIASConfig)
  205. {
  206. /* Check the parameters */
  207. assert_param(IS_BIAS_CURRENTS_SOURCE(sBIASConfig->BiasCurrentSource));
  208. if(((sBIASConfig->BiasCurrentSource) & PWR_BIAS_CURRENTS_FROM_BIAS_CR) == PWR_BIAS_CURRENTS_FROM_BIAS_CR)
  209. {
  210. /* Set the bias currents load source and bias currents value*/
  211. MODIFY_REG(PWR->CR1, (PWR_CR1_BIAS_CR_SEL) | (PWR_CR1_BIAS_CR), (sBIASConfig->BiasCurrentSource) | \
  212. (sBIASConfig->BiasCurrentValue));
  213. }
  214. else
  215. {
  216. /* Set the bias currents load source */
  217. MODIFY_REG(PWR->CR1, PWR_CR1_BIAS_CR_SEL, (sBIASConfig->BiasCurrentSource));
  218. }
  219. return HAL_OK;
  220. }
  221. /**
  222. * @brief Enter Sleep or Low-power Sleep mode.
  223. * @note In Sleep/Low-power Sleep mode, all I/O pins keep the same state as
  224. * in Run mode.
  225. * @param SLEEPEntry Specifies if Sleep mode is entered with WFI or WFE
  226. * instruction. This parameter can be one of the following values:
  227. * @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep
  228. * mode with WFI instruction
  229. * @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep
  230. * mode with WFE instruction
  231. * @note When WFI entry is used, tick interrupt have to be disabled if not
  232. * desired as the interrupt wake up source.
  233. * @retval None
  234. */
  235. void HAL_PWR_EnterSLEEPMode(uint8_t SLEEPEntry)
  236. {
  237. /* Check the parameters */
  238. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  239. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  240. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  241. /* Select SLEEP mode entry -------------------------------------------------*/
  242. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  243. {
  244. /* Request Wait For Interrupt */
  245. __WFI();
  246. }
  247. else
  248. {
  249. /* Request Wait For Event */
  250. __SEV();
  251. __WFE();
  252. __WFE();
  253. }
  254. }
  255. /**
  256. * @brief Enter Stop mode
  257. * @param Regulator Specifies the regulator state in Stop mode
  258. * This parameter can be one of the following values:
  259. * @arg @ref PWR_MAINREGULATOR_ON Stop 0 mode (main regulator ON)
  260. * @arg @ref PWR_LOWPOWERREGULATOR_ON Stop 1 mode (low power regulator ON)
  261. * @arg @ref PWR_DEEPLOWPOWERREGULATOR_ON Stop 2 mode (deep low power regulator ON)
  262. * @param STOPEntry Specifies Stop 0 、Stop 1 or Stop 2 mode is entered with WFI or
  263. * WFE instruction. This parameter can be one of the following values:
  264. * @arg @ref PWR_STOPENTRY_WFI Enter Stop 0 、Stop 1 or Stop 2 mode with WFI
  265. * instruction.
  266. * @arg @ref PWR_STOPENTRY_WFE Enter Stop 0 、Stop 1 or Stop 2 mode with WFE
  267. * instruction.
  268. * @note Depending on devices and packages, some Stop modes may not be available.
  269. * Refer to device datasheet for Stop modes availability.
  270. * @retval None
  271. */
  272. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  273. {
  274. /* Check the parameters */
  275. assert_param(IS_PWR_REGULATOR(Regulator));
  276. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  277. /* Config Regulator */
  278. MODIFY_REG(PWR->CR1, PWR_CR1_LPR, Regulator);
  279. /* Set SLEEPDEEP bit of Cortex System Control Register */
  280. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  281. /* Select Stop mode entry --------------------------------------------------*/
  282. if(STOPEntry == PWR_STOPENTRY_WFI)
  283. {
  284. /* Request Wait For Interrupt */
  285. __WFI();
  286. }
  287. else
  288. {
  289. /* Request Wait For Event */
  290. __SEV();
  291. __WFE();
  292. __WFE();
  293. }
  294. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  295. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  296. }
  297. /**
  298. * @brief Enable Sleep-On-Exit Cortex feature
  299. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the
  300. * processor enters SLEEP or DEEPSLEEP mode when an interruption
  301. * handling is over returning to thread mode. Setting this bit is
  302. * useful when the processor is expected to run only on interruptions
  303. * handling.
  304. * @retval None
  305. */
  306. void HAL_PWR_EnableSleepOnExit(void)
  307. {
  308. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  309. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  310. }
  311. /**
  312. * @brief Disable Sleep-On-Exit Cortex feature
  313. * @note Clear SLEEPONEXIT bit of SCR register. When this bit is set, the
  314. * processor enters SLEEP or DEEPSLEEP mode when an interruption
  315. * handling is over.
  316. * @retval None
  317. */
  318. void HAL_PWR_DisableSleepOnExit(void)
  319. {
  320. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  321. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  322. }
  323. /**
  324. * @brief Enable Cortex Sev On Pending feature.
  325. * @note Set SEVONPEND bit of SCR register. When this bit is set, enabled
  326. * events and all interrupts, including disabled ones can wakeup
  327. * processor from WFE.
  328. * @retval None
  329. */
  330. void HAL_PWR_EnableSEVOnPend(void)
  331. {
  332. /* Set SEVONPEND bit of Cortex System Control Register */
  333. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  334. }
  335. /**
  336. * @brief Disable Cortex Sev On Pending feature.
  337. * @note Clear SEVONPEND bit of SCR register. When this bit is clear, only
  338. * enable interrupts or events can wakeup processor from WFE
  339. * @retval None
  340. */
  341. void HAL_PWR_DisableSEVOnPend(void)
  342. {
  343. /* Clear SEVONPEND bit of Cortex System Control Register */
  344. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  345. }
  346. /**
  347. * @}
  348. */
  349. /**
  350. * @}
  351. */
  352. #endif /* HAL_PWR_MODULE_ENABLED */
  353. /**
  354. * @}
  355. */
  356. /**
  357. * @}
  358. */
  359. /************************ (C) COPYRIGHT Puya *****END OF FILE****/