ft32f0xx_pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_pwr.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Power Controller (PWR) peripheral:
  7. * + Backup Domain Access
  8. * + PVD configuration
  9. * + WakeUp pins configuration
  10. * + Low Power modes configuration
  11. * + Flags management
  12. * @version V1.0.0
  13. * @data 2021-07-01
  14. ******************************************************************************
  15. */
  16. /* Includes ------------------------------------------------------------------*/
  17. #include "ft32f0xx_pwr.h"
  18. #include "ft32f0xx_rcc.h"
  19. /* ------------------ PWR registers bit mask ------------------------ */
  20. /* CR register bit mask */
  21. #define CR_DS_MASK ((uint32_t)0xFFFFFFFC)
  22. #define CR_PLS_MASK ((uint32_t)0xFFFFFD1F)
  23. /**
  24. * @brief Deinitializes the PWR peripheral registers to their default reset values.
  25. * @param None
  26. * @retval None
  27. */
  28. void PWR_DeInit(void)
  29. {
  30. RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
  31. RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);
  32. }
  33. /**
  34. * @brief Enables or disables access to the Backup domain registers.
  35. * @note If the HSE divided by 32 is used as the RTC clock, the
  36. * Backup Domain Access should be kept enabled.
  37. * @param NewState: new state of the access to the Backup domain registers.
  38. * This parameter can be: ENABLE or DISABLE.
  39. * @retval None
  40. */
  41. void PWR_BackupAccessCmd(FunctionalState NewState)
  42. {
  43. /* Check the parameters */
  44. assert_param(IS_FUNCTIONAL_STATE(NewState));
  45. if (NewState != DISABLE)
  46. {
  47. /* Enable the Backup Domain Access */
  48. PWR->CR |= PWR_CR_DBP;
  49. }
  50. else
  51. {
  52. /* Disable the Backup Domain Access */
  53. PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_DBP);
  54. }
  55. }
  56. /**
  57. * @}
  58. */
  59. /**
  60. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  61. * @param PWR_PVDLevel: specifies the PVD detection level
  62. * This parameter can be one of the following values:
  63. * @arg PWR_PVDLevel_0
  64. * @arg PWR_PVDLevel_1
  65. * @arg PWR_PVDLevel_2
  66. * @arg PWR_PVDLevel_3
  67. * @arg PWR_PVDLevel_4
  68. * @arg PWR_PVDLevel_5
  69. * @arg PWR_PVDLevel_6
  70. * @arg PWR_PVDLevel_7
  71. * @arg PWR_PVDLevel_8
  72. * @arg PWR_PVDLevel_9
  73. * @arg PWR_PVDLevel_10
  74. * @arg PWR_PVDLevel_11
  75. * @arg PWR_PVDLevel_12
  76. * @arg PWR_PVDLevel_13
  77. * @arg PWR_PVDLevel_14
  78. * @arg PWR_PVDLevel_15
  79. * @note Refer to the electrical characteristics of your device datasheet for
  80. * more details about the voltage threshold corresponding to each
  81. * detection level.
  82. * @retval None
  83. */
  84. void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)
  85. {
  86. uint32_t tmpreg = 0;
  87. /* Check the parameters */
  88. assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
  89. tmpreg = PWR->CR;
  90. /* Clear PLS[7:5] bits PLS3*/
  91. tmpreg &= CR_PLS_MASK;
  92. /* Set PLS[7:5] and PLS3 bits according to PWR_PVDLevel value */
  93. tmpreg |= PWR_PVDLevel;
  94. /* Store the new value */
  95. PWR->CR = tmpreg;
  96. }
  97. /**
  98. * @brief Enables or disables the Power Voltage Detector(PVD).
  99. * @param NewState: new state of the PVD.
  100. * This parameter can be: ENABLE or DISABLE.
  101. * @retval None
  102. */
  103. void PWR_PVDCmd(FunctionalState NewState)
  104. {
  105. /* Check the parameters */
  106. assert_param(IS_FUNCTIONAL_STATE(NewState));
  107. if (NewState != DISABLE)
  108. {
  109. /* Enable the PVD */
  110. PWR->CR |= PWR_CR_PVDE;
  111. }
  112. else
  113. {
  114. /* Disable the PVD */
  115. PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_PVDE);
  116. }
  117. }
  118. /**
  119. * @}
  120. */
  121. /**
  122. * @brief Enables or disables the WakeUp Pin functionality.
  123. * @param PWR_WakeUpPin: specifies the WakeUpPin.
  124. * This parameter can be one of the following values
  125. * @arg PWR_WakeUpPin_1
  126. * @arg PWR_WakeUpPin_2
  127. * @arg PWR_WakeUpPin_3
  128. * @arg PWR_WakeUpPin_4
  129. * @arg PWR_WakeUpPin_5
  130. * @arg PWR_WakeUpPin_6
  131. * @arg PWR_WakeUpPin_7
  132. * @arg PWR_WakeUpPin_8
  133. * @param NewState: new state of the WakeUp Pin functionality.
  134. * This parameter can be: ENABLE or DISABLE.
  135. * @retval None
  136. */
  137. void PWR_WakeUpPinCmd(uint32_t PWR_WakeUpPin, FunctionalState NewState)
  138. {
  139. /* Check the parameters */
  140. assert_param(IS_PWR_WAKEUP_PIN(PWR_WakeUpPin));
  141. assert_param(IS_FUNCTIONAL_STATE(NewState));
  142. if (NewState != DISABLE)
  143. {
  144. /* Enable the EWUPx pin */
  145. PWR->CSR |= PWR_WakeUpPin;
  146. }
  147. else
  148. {
  149. /* Disable the EWUPx pin */
  150. PWR->CSR &= ~PWR_WakeUpPin;
  151. }
  152. }
  153. /**
  154. * @}
  155. */
  156. /**
  157. * @brief Enters Sleep mode.
  158. * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
  159. * @param PWR_SLEEPEntry: specifies if SLEEP mode in entered with WFI or WFE instruction.
  160. * This parameter can be one of the following values:
  161. * @arg PWR_SLEEPEntry_WFI: enter SLEEP mode with WFI instruction
  162. * @arg PWR_SLEEPEntry_WFE: enter SLEEP mode with WFE instruction
  163. * @retval None
  164. */
  165. void PWR_EnterSleepMode(uint8_t PWR_SLEEPEntry)
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_PWR_SLEEP_ENTRY(PWR_SLEEPEntry));
  169. /* Clear SLEEPDEEP bit of Cortex-M0 System Control Register */
  170. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  171. /* Select SLEEP mode entry -------------------------------------------------*/
  172. if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
  173. {
  174. /* Request Wait For Interrupt */
  175. __WFI();
  176. }
  177. else
  178. {
  179. /* Request Wait For Event */
  180. __SEV();
  181. __WFE();
  182. __WFE();
  183. }
  184. }
  185. /**
  186. * @brief Enters STOP mode.
  187. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  188. * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
  189. * the HSI RC oscillator is selected as system clock.
  190. * @note When the voltage regulator operates in low power mode, an additional
  191. * startup delay is incurred when waking up from Stop mode.
  192. * By keeping the internal regulator ON during Stop mode, the consumption
  193. * is higher although the startup time is reduced.
  194. * @param PWR_Regulator: specifies the regulator state in STOP mode.
  195. * This parameter can be one of the following values:
  196. * @arg PWR_Regulator_ON: STOP mode with regulator ON
  197. * @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode
  198. * @param PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
  199. * This parameter can be one of the following values:
  200. * @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
  201. * @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
  202. @arg PWR_STOPEntry_SLEEPONEXIT: enter STOP mode with SLEEPONEXIT instruction
  203. * @retval None
  204. */
  205. void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
  206. {
  207. uint32_t tmpreg = 0;
  208. /* Check the parameters */
  209. assert_param(IS_PWR_REGULATOR(PWR_Regulator));
  210. assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
  211. /* Select the regulator state in STOP mode ---------------------------------*/
  212. tmpreg = PWR->CR;
  213. /* Clear PDDS and LPDSR bits */
  214. tmpreg &= CR_DS_MASK;
  215. /* Set LPDSR bit according to PWR_Regulator value */
  216. tmpreg |= PWR_Regulator;
  217. /* Store the new value */
  218. PWR->CR = tmpreg;
  219. /* Set SLEEPDEEP bit of Cortex-M0 System Control Register */
  220. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  221. /* Select STOP mode entry --------------------------------------------------*/
  222. if(PWR_STOPEntry == PWR_STOPEntry_WFI)
  223. {
  224. /* Request Wait For Interrupt */
  225. __WFI();
  226. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  227. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  228. }
  229. else if (PWR_STOPEntry == PWR_STOPEntry_WFE)
  230. {
  231. /* Request Wait For Event */
  232. __WFE();
  233. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  234. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  235. }
  236. else
  237. {
  238. /* Set SLEEP on exit bit of Cortex-M0 System Control Register */
  239. SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;
  240. }
  241. }
  242. /**
  243. * @brief Enters STANDBY mode.
  244. * @note In Standby mode, all I/O pins are high impedance except for:
  245. * - Reset pad (still available)
  246. * - RTC_AF1 pin (PC13) if configured for Wakeup pin 2 (WKUP2), tamper,
  247. * time-stamp, RTC Alarm out, or RTC clock calibration out.
  248. * - WKUP pin 1 (PA0) if enabled.
  249. * @note The Wakeup flag (WUF) need to be cleared at application level before to call this function
  250. * @param None
  251. * @retval None
  252. */
  253. void PWR_EnterSTANDBYMode(void)
  254. {
  255. /* Select STANDBY mode */
  256. PWR->CR |= PWR_CR_PDDS;
  257. /* Set SLEEPDEEP bit of Cortex-M0 System Control Register */
  258. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  259. /* Request Wait For Interrupt */
  260. __WFI();
  261. }
  262. /**
  263. * @}
  264. */
  265. /**
  266. * @brief Checks whether the specified PWR flag is set or not.
  267. * @param PWR_FLAG: specifies the flag to check.
  268. * This parameter can be one of the following values:
  269. * @arg PWR_FLAG_WU: Wake Up flag. This flag indicates that a wakeup
  270. * event was received from the WKUP pin or from the RTC alarm
  271. * (Alarm A or Alarm B), RTC Tamper event or RTC TimeStamp event.
  272. * @arg PWR_FLAG_SB: StandBy flag. This flag indicates that the
  273. * system was resumed from StandBy mode.
  274. * @arg PWR_FLAG_PVDO: PVD Output. This flag is valid only if PVD
  275. * is enabled by the PWR_PVDCmd() function.
  276. * @arg PWR_FLAG_VREFINTRDY: Internal Voltage Reference Ready flag.
  277. * This flag indicates the state of the internal voltage
  278. * reference, VREFINT.
  279. * @retval The new state of PWR_FLAG (SET or RESET).
  280. */
  281. FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
  282. {
  283. FlagStatus bitstatus = RESET;
  284. /* Check the parameters */
  285. assert_param(IS_PWR_GET_FLAG(PWR_FLAG));
  286. if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET)
  287. {
  288. bitstatus = SET;
  289. }
  290. else
  291. {
  292. bitstatus = RESET;
  293. }
  294. /* Return the flag status */
  295. return bitstatus;
  296. }
  297. /**
  298. * @brief Clears the PWR's pending flags.
  299. * @param PWR_FLAG: specifies the flag to clear.
  300. * This parameter can be one of the following values:
  301. * @arg PWR_FLAG_WU: Wake Up flag
  302. * @arg PWR_FLAG_SB: StandBy flag
  303. * @retval None
  304. */
  305. void PWR_ClearFlag(uint32_t PWR_FLAG)
  306. {
  307. /* Check the parameters */
  308. assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG));
  309. PWR->CR |= PWR_FLAG << 2;
  310. }
  311. /**
  312. * @}
  313. */
  314. /**
  315. * @}
  316. */
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /************************ (C) COPYRIGHT FMD *****END OF FILE****/