stm32g0xx_hal_pwr_ex.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_pwr_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Power Controller (PWR) peripheral:
  8. * + Extended Initialization and de-initialization functions
  9. * + Extended Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2018 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32g0xx_hal.h"
  25. /** @addtogroup STM32G0xx_HAL_Driver
  26. * @{
  27. */
  28. /** @addtogroup PWREx
  29. * @{
  30. */
  31. #ifdef HAL_PWR_MODULE_ENABLED
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /** @defgroup PWR_Extended_Private_Defines PWR Extended Private Defines
  35. * @{
  36. */
  37. #if defined(PWR_PVD_SUPPORT)
  38. /** @defgroup PWR_PVD_Mode_Mask PWR PVD Mode Mask
  39. * @{
  40. */
  41. #define PVD_MODE_IT 0x00010000U /*!< Mask for interruption yielded
  42. by PVD threshold crossing */
  43. #define PVD_MODE_EVT 0x00020000U /*!< Mask for event yielded
  44. by PVD threshold crossing */
  45. #define PVD_RISING_EDGE 0x00000001U /*!< Mask for rising edge set as
  46. PVD trigger */
  47. #define PVD_FALLING_EDGE 0x00000002U /*!< Mask for falling edge set as
  48. PVD trigger */
  49. /**
  50. * @}
  51. */
  52. #endif /* PWR_PVD_SUPPORT */
  53. /** @defgroup PWREx_TimeOut_Value PWREx Flag Setting Time Out Value
  54. * @{
  55. */
  56. #define PWR_REGLPF_SETTING_DELAY_6_US 6u /*!< REGLPF should rise in about 5 us plus
  57. 2 APB clock. Taking in account max Sysclk at
  58. 2 MHz, and rounded to upper value */
  59. #define PWR_VOSF_SETTING_DELAY_6_US 6u /*!< VOSF should rise in about 5 us plus
  60. 2 APB clock. Taking in account max Sysclk at
  61. 16 MHz, and rounded to upper value */
  62. /**
  63. * @}
  64. */
  65. /** @defgroup PWREx_Gpio_Pin_Number PWREx Gpio Pin Number
  66. * @{
  67. */
  68. #define PWR_GPIO_PIN_NB 16u /*!< Number of gpio pin in bank */
  69. /**
  70. * @}
  71. */
  72. /**
  73. * @}
  74. */
  75. /* Private macro -------------------------------------------------------------*/
  76. /* Private variables ---------------------------------------------------------*/
  77. /* Private function prototypes -----------------------------------------------*/
  78. /* Exported functions --------------------------------------------------------*/
  79. /** @addtogroup PWREx_Exported_Functions PWR Extended Exported Functions
  80. * @{
  81. */
  82. /** @addtogroup PWREx_Exported_Functions_Group1 Extended Peripheral Control functions
  83. * @brief Extended Peripheral Control functions
  84. *
  85. @verbatim
  86. ===============================================================================
  87. ##### Extended Peripheral Initialization and de-initialization functions #####
  88. ===============================================================================
  89. [..]
  90. *** PVD configuration ***
  91. =========================
  92. [..]
  93. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  94. threshold selected by the PVD Level (PVDRT[2:0] & PVDFT[2:0] bits in
  95. PWR CR2 register).
  96. (+) PVDO flag is available to indicate if VDD/VDDA is higher or lower
  97. than the PVD threshold. This event is internally connected to the EXTI
  98. line 16 and can generate an interrupt if enabled.
  99. (+) The PVD is stopped in Standby & Shutdown mode.
  100. *** PVM configuration ***
  101. =========================
  102. [..]
  103. @endverbatim
  104. * @{
  105. */
  106. /**
  107. * @brief Enable battery charging.
  108. * @note When VDD is present, charge the external battery on VBAT through an
  109. * internal resistor.
  110. * @param ResistorSelection specifies the resistor impedance.
  111. * This parameter can be one of the following values:
  112. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_5 5 kOhms resistor
  113. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_1_5 1.5 kOhms resistor
  114. * @retval None
  115. */
  116. void HAL_PWREx_EnableBatteryCharging(uint32_t ResistorSelection)
  117. {
  118. uint32_t tmpreg;
  119. assert_param(IS_PWR_BATTERY_RESISTOR_SELECT(ResistorSelection));
  120. /* Specify resistor selection and enable battery charging */
  121. tmpreg = (PWR->CR4 & ~PWR_CR4_VBRS);
  122. PWR->CR4 = (tmpreg | ResistorSelection | PWR_CR4_VBE);
  123. }
  124. /**
  125. * @brief Disable battery charging.
  126. * @retval None
  127. */
  128. void HAL_PWREx_DisableBatteryCharging(void)
  129. {
  130. CLEAR_BIT(PWR->CR4, PWR_CR4_VBE);
  131. }
  132. #if defined(PWR_CR3_ENB_ULP)
  133. /**
  134. * @brief Enable POR Monitor sampling mode.
  135. * @note When entering ultra low power modes (standby, shutdown) this feature
  136. * can be enabled to reduce further consumption: Power On Reset monitor
  137. * is then set in sampling mode, and no more in always on mode.
  138. * @retval None
  139. */
  140. void HAL_PWREx_EnablePORMonitorSampling(void)
  141. {
  142. PWR->CR3 |= PWR_CR3_ENB_ULP;
  143. }
  144. /**
  145. * @brief Disable POR Monitor sampling mode.
  146. * @retval None
  147. */
  148. void HAL_PWREx_DisablePORMonitorSampling(void)
  149. {
  150. PWR->CR3 &= ~PWR_CR3_ENB_ULP;
  151. }
  152. #endif /* PWR_CR3_ENB_ULP */
  153. #if defined(PWR_PVD_SUPPORT)
  154. /**
  155. * @brief Configure the Power Voltage Detector (PVD).
  156. * @param sConfigPVD pointer to a PWR_PVDTypeDef structure that contains the
  157. PVD configuration information: threshold levels, operating mode.
  158. * @note Refer to the electrical characteristics of your device datasheet for
  159. * more details about the voltage thresholds corresponding to each
  160. * detection level.
  161. * @note User should take care that rising threshold is higher than falling
  162. * one in order to avoid having always PVDO output set.
  163. * @retval HAL_OK
  164. */
  165. HAL_StatusTypeDef HAL_PWREx_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  169. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  170. /* Set PVD level bits only according to PVDLevel value */
  171. MODIFY_REG(PWR->CR2, (PWR_CR2_PVDFT | PWR_CR2_PVDRT), sConfigPVD->PVDLevel);
  172. /* Clear any previous config, in case no event or IT mode is selected */
  173. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  174. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  175. __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  176. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
  177. /* Configure interrupt mode */
  178. if ((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  179. {
  180. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  181. }
  182. /* Configure event mode */
  183. if ((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  184. {
  185. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  186. }
  187. /* Configure the edge */
  188. if ((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  189. {
  190. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  191. }
  192. if ((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  193. {
  194. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  195. }
  196. return HAL_OK;
  197. }
  198. /**
  199. * @brief Enable the Power Voltage Detector (PVD).
  200. * @retval None
  201. */
  202. void HAL_PWREx_EnablePVD(void)
  203. {
  204. SET_BIT(PWR->CR2, PWR_CR2_PVDE);
  205. }
  206. /**
  207. * @brief Disable the Power Voltage Detector (PVD).
  208. * @retval None
  209. */
  210. void HAL_PWREx_DisablePVD(void)
  211. {
  212. CLEAR_BIT(PWR->CR2, PWR_CR2_PVDE);
  213. }
  214. #endif /* PWR_PVD_SUPPORT */
  215. #if defined(PWR_CR2_USV)
  216. /**
  217. * @brief Enable VDDUSB supply.
  218. * @note Remove VDDUSB electrical and logical isolation, once VDDUSB supply is present.
  219. * @retval None
  220. */
  221. void HAL_PWREx_EnableVddUSB(void)
  222. {
  223. SET_BIT(PWR->CR2, PWR_CR2_USV);
  224. }
  225. /**
  226. * @brief Disable VDDUSB supply.
  227. * @retval None
  228. */
  229. void HAL_PWREx_DisableVddUSB(void)
  230. {
  231. CLEAR_BIT(PWR->CR2, PWR_CR2_USV);
  232. }
  233. #endif /* PWR_CR2_USV */
  234. #if defined(PWR_CR2_IOSV)
  235. /**
  236. * @brief Enable VDDIO2 supply.
  237. * @note Remove VDDIO2 electrical and logical isolation, once VDDIO2 supply is present.
  238. * @retval None
  239. */
  240. void HAL_PWREx_EnableVddIO2(void)
  241. {
  242. SET_BIT(PWR->CR2, PWR_CR2_IOSV);
  243. }
  244. /**
  245. * @brief Disable VDDIO2 supply.
  246. * @retval None
  247. */
  248. void HAL_PWREx_DisableVddIO2(void)
  249. {
  250. CLEAR_BIT(PWR->CR2, PWR_CR2_IOSV);
  251. }
  252. #endif /* PWR_CR2_IOSV */
  253. #if defined (PWR_PVM_SUPPORT)
  254. /**
  255. * @brief Enable the Power Voltage Monitoring for USB peripheral (power domain Vddio2)
  256. * @retval None
  257. */
  258. void HAL_PWREx_EnablePVMUSB(void)
  259. {
  260. SET_BIT(PWR->CR2, PWR_PVM_USB);
  261. }
  262. /**
  263. * @brief Disable the Power Voltage Monitoring for USB peripheral (power domain Vddio2)
  264. * @retval None
  265. */
  266. void HAL_PWREx_DisablePVMUSB(void)
  267. {
  268. CLEAR_BIT(PWR->CR2, PWR_PVM_USB);
  269. }
  270. #endif /* PWR_PVM_SUPPORT */
  271. #if defined(PWR_PVM_SUPPORT)
  272. /**
  273. * @brief Configure the Peripheral Voltage Monitoring (PVM).
  274. * @param sConfigPVM: pointer to a PWR_PVMTypeDef structure that contains the
  275. * PVM configuration information.
  276. * @note The API configures a single PVM according to the information contained
  277. * in the input structure. To configure several PVMs, the API must be singly
  278. * called for each PVM used.
  279. * @note Refer to the electrical characteristics of your device datasheet for
  280. * more details about the voltage thresholds corresponding to each
  281. * detection level and to each monitored supply.
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_PWREx_ConfigPVM(PWR_PVMTypeDef *sConfigPVM)
  285. {
  286. HAL_StatusTypeDef status = HAL_OK;
  287. /* Check the parameters */
  288. assert_param(IS_PWR_PVM_TYPE(sConfigPVM->PVMType));
  289. assert_param(IS_PWR_PVM_MODE(sConfigPVM->Mode));
  290. /* Configure EXTI 34 interrupts if so required:
  291. scan through PVMType to detect which PVMx is set and
  292. configure the corresponding EXTI line accordingly. */
  293. switch (sConfigPVM->PVMType)
  294. {
  295. case PWR_PVM_USB:
  296. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  297. __HAL_PWR_PVM_EXTI_DISABLE_EVENT();
  298. __HAL_PWR_PVM_EXTI_DISABLE_IT();
  299. __HAL_PWR_PVM_EXTI_DISABLE_FALLING_EDGE();
  300. __HAL_PWR_PVM_EXTI_DISABLE_RISING_EDGE();
  301. /* Configure interrupt mode */
  302. if ((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  303. {
  304. __HAL_PWR_PVM_EXTI_ENABLE_IT();
  305. }
  306. /* Configure event mode */
  307. if ((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  308. {
  309. __HAL_PWR_PVM_EXTI_ENABLE_EVENT();
  310. }
  311. /* Configure the edge */
  312. if ((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  313. {
  314. __HAL_PWR_PVM_EXTI_ENABLE_RISING_EDGE();
  315. }
  316. if ((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  317. {
  318. __HAL_PWR_PVM_EXTI_ENABLE_FALLING_EDGE();
  319. }
  320. break;
  321. default:
  322. status = HAL_ERROR;
  323. break;
  324. }
  325. return status;
  326. }
  327. #endif /* PWR_PVM_SUPPORT */
  328. /**
  329. * @brief Enable Internal Wake-up Line.
  330. * @retval None
  331. */
  332. void HAL_PWREx_EnableInternalWakeUpLine(void)
  333. {
  334. SET_BIT(PWR->CR3, PWR_CR3_EIWUL);
  335. }
  336. /**
  337. * @brief Disable Internal Wake-up Line.
  338. * @retval None
  339. */
  340. void HAL_PWREx_DisableInternalWakeUpLine(void)
  341. {
  342. CLEAR_BIT(PWR->CR3, PWR_CR3_EIWUL);
  343. }
  344. /**
  345. * @brief Enable GPIO pull-up state in Standby and Shutdown modes.
  346. * @note Set the relevant PUy bit of PWR_PUCRx register to configure the I/O in
  347. * pull-up state in Standby and Shutdown modes.
  348. * @note This state is effective in Standby and Shutdown modes only if APC bit
  349. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  350. * @note The configuration is lost when exiting the Shutdown mode due to the
  351. * power-on reset, maintained when exiting the Standby mode.
  352. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  353. * PDy bit of PWR_PDCRx register is cleared unless it is reserved.
  354. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_F
  355. * to select the GPIO peripheral.
  356. * @param GPIONumber Specify the I/O pins numbers.
  357. * This parameter can be one of the following values:
  358. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for ports where less
  359. * I/O pins are available) or the logical OR of several of them to set
  360. * several bits for a given port in a single API call.
  361. * @retval HAL Status
  362. */
  363. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  364. {
  365. HAL_StatusTypeDef status = HAL_OK;
  366. assert_param(IS_PWR_GPIO(GPIO));
  367. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  368. switch (GPIO)
  369. {
  370. case PWR_GPIO_A:
  371. SET_BIT(PWR->PUCRA, (GPIONumber & ~PWR_GPIO_BIT_14));
  372. CLEAR_BIT(PWR->PDCRA, (GPIONumber & ~PWR_GPIO_BIT_13));
  373. break;
  374. case PWR_GPIO_B:
  375. SET_BIT(PWR->PUCRB, GPIONumber);
  376. CLEAR_BIT(PWR->PDCRB, GPIONumber);
  377. break;
  378. case PWR_GPIO_C:
  379. SET_BIT(PWR->PUCRC, GPIONumber);
  380. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  381. break;
  382. case PWR_GPIO_D:
  383. SET_BIT(PWR->PUCRD, GPIONumber);
  384. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  385. break;
  386. #if defined(GPI0E)
  387. case PWR_GPIO_E:
  388. SET_BIT(PWR->PUCRE, GPIONumber);
  389. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  390. break;
  391. #endif /* GPI0E */
  392. case PWR_GPIO_F:
  393. SET_BIT(PWR->PUCRF, GPIONumber);
  394. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  395. break;
  396. default:
  397. status = HAL_ERROR;
  398. break;
  399. }
  400. return status;
  401. }
  402. /**
  403. * @brief Disable GPIO pull-up state in Standby mode and Shutdown modes.
  404. * @note Reset the relevant PUy bit of PWR_PUCRx register used to configure the I/O
  405. * in pull-up state in Standby and Shutdown modes.
  406. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_F
  407. * to select the GPIO peripheral.
  408. * @param GPIONumber Specify the I/O pins numbers.
  409. * This parameter can be one of the following values:
  410. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for ports where less
  411. * I/O pins are available) or the logical OR of several of them to reset
  412. * several bits for a given port in a single API call.
  413. * @retval HAL Status
  414. */
  415. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  416. {
  417. HAL_StatusTypeDef status = HAL_OK;
  418. assert_param(IS_PWR_GPIO(GPIO));
  419. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  420. switch (GPIO)
  421. {
  422. case PWR_GPIO_A:
  423. CLEAR_BIT(PWR->PUCRA, (GPIONumber & ~PWR_GPIO_BIT_14));
  424. break;
  425. case PWR_GPIO_B:
  426. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  427. break;
  428. case PWR_GPIO_C:
  429. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  430. break;
  431. case PWR_GPIO_D:
  432. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  433. break;
  434. #if defined(GPI0E)
  435. case PWR_GPIO_E:
  436. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  437. break;
  438. #endif /* GPI0E */
  439. case PWR_GPIO_F:
  440. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  441. break;
  442. default:
  443. status = HAL_ERROR;
  444. break;
  445. }
  446. return status;
  447. }
  448. /**
  449. * @brief Enable GPIO pull-down state in Standby and Shutdown modes.
  450. * @note Set the relevant PDy bit of PWR_PDCRx register to configure the I/O in
  451. * pull-down state in Standby and Shutdown modes.
  452. * @note This state is effective in Standby and Shutdown modes only if APC bit
  453. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  454. * @note The configuration is lost when exiting the Shutdown mode due to the
  455. * power-on reset, maintained when exiting the Standby mode.
  456. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  457. * PUy bit of PWR_PUCRx register is cleared unless it is reserved.
  458. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_F
  459. * to select the GPIO peripheral.
  460. * @param GPIONumber Specify the I/O pins numbers.
  461. * This parameter can be one of the following values:
  462. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for ports where less
  463. * I/O pins are available) or the logical OR of several of them to set
  464. * several bits for a given port in a single API call.
  465. * @retval HAL Status
  466. */
  467. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  468. {
  469. HAL_StatusTypeDef status = HAL_OK;
  470. assert_param(IS_PWR_GPIO(GPIO));
  471. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  472. switch (GPIO)
  473. {
  474. case PWR_GPIO_A:
  475. SET_BIT(PWR->PDCRA, (GPIONumber & ~PWR_GPIO_BIT_13));
  476. CLEAR_BIT(PWR->PUCRA, (GPIONumber & ~PWR_GPIO_BIT_14));
  477. break;
  478. case PWR_GPIO_B:
  479. SET_BIT(PWR->PDCRB, GPIONumber);
  480. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  481. break;
  482. case PWR_GPIO_C:
  483. SET_BIT(PWR->PDCRC, GPIONumber);
  484. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  485. break;
  486. case PWR_GPIO_D:
  487. SET_BIT(PWR->PDCRD, GPIONumber);
  488. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  489. break;
  490. #if defined(GPIOE)
  491. case PWR_GPIO_E:
  492. SET_BIT(PWR->PDCRE, GPIONumber);
  493. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  494. break;
  495. #endif /* GPI0E */
  496. case PWR_GPIO_F:
  497. SET_BIT(PWR->PDCRF, GPIONumber);
  498. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  499. break;
  500. default:
  501. status = HAL_ERROR;
  502. break;
  503. }
  504. return status;
  505. }
  506. /**
  507. * @brief Disable GPIO pull-down state in Standby and Shutdown modes.
  508. * @note Reset the relevant PDy bit of PWR_PDCRx register used to configure the I/O
  509. * in pull-down state in Standby and Shutdown modes.
  510. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_F
  511. * to select the GPIO peripheral.
  512. * @param GPIONumber Specify the I/O pins numbers.
  513. * This parameter can be one of the following values:
  514. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for ports where less
  515. * I/O pins are available) or the logical OR of several of them to reset
  516. * several bits for a given port in a single API call.
  517. * @retval HAL Status
  518. */
  519. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  520. {
  521. HAL_StatusTypeDef status = HAL_OK;
  522. assert_param(IS_PWR_GPIO(GPIO));
  523. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  524. switch (GPIO)
  525. {
  526. case PWR_GPIO_A:
  527. CLEAR_BIT(PWR->PDCRA, (GPIONumber & ~PWR_GPIO_BIT_13));
  528. break;
  529. case PWR_GPIO_B:
  530. CLEAR_BIT(PWR->PDCRB, GPIONumber);
  531. break;
  532. case PWR_GPIO_C:
  533. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  534. break;
  535. case PWR_GPIO_D:
  536. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  537. break;
  538. #if defined(GPIOE)
  539. case PWR_GPIO_E:
  540. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  541. break;
  542. #endif /* GPI0E */
  543. case PWR_GPIO_F:
  544. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  545. break;
  546. default:
  547. status = HAL_ERROR;
  548. break;
  549. }
  550. return status;
  551. }
  552. /**
  553. * @brief Enable pull-up and pull-down configuration.
  554. * @note When APC bit is set, the I/O pull-up and pull-down configurations defined in
  555. * PWR_PUCRx and PWR_PDCRx registers are applied in Standby and Shutdown modes.
  556. * @note Pull-up set by PUy bit of PWR_PUCRx register is not activated if the corresponding
  557. * PDy bit of PWR_PDCRx register is also set (pull-down configuration priority is higher).
  558. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() APIs ensure there
  559. * is no conflict when setting PUy or PDy bit.
  560. * @retval None
  561. */
  562. void HAL_PWREx_EnablePullUpPullDownConfig(void)
  563. {
  564. SET_BIT(PWR->CR3, PWR_CR3_APC);
  565. }
  566. /**
  567. * @brief Disable pull-up and pull-down configuration.
  568. * @note When APC bit is cleared, the I/O pull-up and pull-down configurations defined in
  569. * PWR_PUCRx and PWR_PDCRx registers are not applied in Standby and Shutdown modes.
  570. * @retval None
  571. */
  572. void HAL_PWREx_DisablePullUpPullDownConfig(void)
  573. {
  574. CLEAR_BIT(PWR->CR3, PWR_CR3_APC);
  575. }
  576. #if defined(PWR_CR3_RRS)
  577. /**
  578. * @brief Enable SRAM content retention in Standby mode.
  579. * @note When RRS bit is set, SRAM is powered by the low-power regulator in
  580. * Standby mode and its content is kept.
  581. * @retval None
  582. */
  583. void HAL_PWREx_EnableSRAMRetention(void)
  584. {
  585. SET_BIT(PWR->CR3, PWR_CR3_RRS);
  586. }
  587. /**
  588. * @brief Disable SRAM content retention in Standby mode.
  589. * @note When RRS bit is reset, SRAM is powered off in Standby mode
  590. * and its content is lost.
  591. * @retval None
  592. */
  593. void HAL_PWREx_DisableSRAMRetention(void)
  594. {
  595. CLEAR_BIT(PWR->CR3, PWR_CR3_RRS);
  596. }
  597. #endif /* PWR_CR3_RRS */
  598. /**
  599. * @brief Enable Flash Power Down.
  600. * @note This API allows to enable flash power down capabilities in low power
  601. * run, low power sleep and stop modes.
  602. * @param PowerMode this can be a combination of following values:
  603. * @arg @ref PWR_FLASHPD_LPRUN
  604. * @arg @ref PWR_FLASHPD_LPSLEEP
  605. * @arg @ref PWR_FLASHPD_STOP
  606. * @retval None
  607. */
  608. void HAL_PWREx_EnableFlashPowerDown(uint32_t PowerMode)
  609. {
  610. assert_param(IS_PWR_FLASH_POWERDOWN(PowerMode));
  611. PWR->CR1 |= PowerMode;
  612. }
  613. /**
  614. * @brief Disable Flash Power Down.
  615. * @note This API allows to disable flash power down capabilities in low power
  616. * run, low power sleep and stop modes.
  617. * @param PowerMode this can be a combination of following values:
  618. * @arg @ref PWR_FLASHPD_LPRUN
  619. * @arg @ref PWR_FLASHPD_LPSLEEP
  620. * @arg @ref PWR_FLASHPD_STOP
  621. * @retval None
  622. */
  623. void HAL_PWREx_DisableFlashPowerDown(uint32_t PowerMode)
  624. {
  625. assert_param(IS_PWR_FLASH_POWERDOWN(PowerMode));
  626. PWR->CR1 &= ~PowerMode;
  627. }
  628. /**
  629. * @brief Return Voltage Scaling Range.
  630. * @retval VOS bit field:
  631. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1
  632. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2
  633. */
  634. uint32_t HAL_PWREx_GetVoltageRange(void)
  635. {
  636. return (PWR->CR1 & PWR_CR1_VOS);
  637. }
  638. /**
  639. * @brief Configure the main regulator output voltage.
  640. * @param VoltageScaling specifies the regulator output voltage to achieve
  641. * a tradeoff between performance and power consumption.
  642. * This parameter can be one of the following values:
  643. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1 Regulator voltage output range 1 mode,
  644. * typical output voltage at 1.2 V,
  645. * system frequency up to 64 MHz.
  646. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2 Regulator voltage output range 2 mode,
  647. * typical output voltage at 1.0 V,
  648. * system frequency up to 16 MHz.
  649. * @note When moving from Range 1 to Range 2, the system frequency must be decreased to
  650. * a value below 16 MHz before calling HAL_PWREx_ControlVoltageScaling() API.
  651. * When moving from Range 2 to Range 1, the system frequency can be increased to
  652. * a value up to 64 MHz after calling HAL_PWREx_ControlVoltageScaling() API.
  653. * @note When moving from Range 2 to Range 1, the API waits for VOSF flag to be
  654. * cleared before returning the status. If the flag is not cleared within
  655. * 6 microseconds, HAL_TIMEOUT status is reported.
  656. * @retval HAL Status
  657. */
  658. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  659. {
  660. uint32_t wait_loop_index;
  661. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  662. /* Modify voltage scaling range */
  663. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, VoltageScaling);
  664. /* In case of Range 1 selected, we need to ensure that main regulator reaches new value */
  665. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  666. {
  667. /* Set timeout value */
  668. wait_loop_index = ((PWR_VOSF_SETTING_DELAY_6_US * SystemCoreClock) / 1000000U) + 1U;
  669. /* Wait until VOSF is reset */
  670. while (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  671. {
  672. if (wait_loop_index != 0U)
  673. {
  674. wait_loop_index--;
  675. }
  676. else
  677. {
  678. return HAL_TIMEOUT;
  679. }
  680. }
  681. }
  682. return HAL_OK;
  683. }
  684. /**
  685. * @brief Enter Low-power Run mode
  686. * @note System clock frequency has to be decreased below 2 MHz before entering
  687. * low power run mode
  688. * @note In Low-power Run mode, all I/O pins keep the same state as in Run mode.
  689. * @retval None
  690. */
  691. void HAL_PWREx_EnableLowPowerRunMode(void)
  692. {
  693. /* Set Regulator parameter */
  694. SET_BIT(PWR->CR1, PWR_CR1_LPR);
  695. }
  696. /**
  697. * @brief Exit Low-power Run mode.
  698. * @note Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
  699. * REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
  700. * returns HAL_TIMEOUT status). The system clock frequency can then be
  701. * increased above 2 MHz.
  702. * @retval HAL Status
  703. */
  704. HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
  705. {
  706. uint32_t wait_loop_index = ((PWR_REGLPF_SETTING_DELAY_6_US * SystemCoreClock) / 1000000U) + 1U;
  707. /* Clear LPR bit */
  708. CLEAR_BIT(PWR->CR1, PWR_CR1_LPR);
  709. /* Wait until REGLPF is reset */
  710. while (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  711. {
  712. if (wait_loop_index != 0U)
  713. {
  714. wait_loop_index--;
  715. }
  716. else
  717. {
  718. return HAL_TIMEOUT;
  719. }
  720. }
  721. return HAL_OK;
  722. }
  723. #if defined(PWR_SHDW_SUPPORT)
  724. /**
  725. * @brief Enter Shutdown mode.
  726. * @note In Shutdown mode, the PLL, the HSI, the LSI and the HSE oscillators are switched
  727. * off. The voltage regulator is disabled and Vcore domain is powered off.
  728. * SRAM and registers contents are lost except for registers in the Backup domain.
  729. * The BOR is not available.
  730. * @note The I/Os can be configured either with a pull-up or pull-down or can
  731. * be kept in analog state.
  732. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown()
  733. * respectively enable Pull Up and PullDown state.
  734. * HAL_PWREx_DisableGPIOPullUp() & HAL_PWREx_DisableGPIOPullDown()
  735. * disable the same. These states are effective in Standby mode only if
  736. * APC bit is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  737. * @retval None
  738. * @retval None
  739. */
  740. void HAL_PWREx_EnterSHUTDOWNMode(void)
  741. {
  742. /* Set Shutdown mode */
  743. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_SHUTDOWN);
  744. /* Set SLEEPDEEP bit of Cortex System Control Register */
  745. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  746. /* This option is used to ensure that store operations are completed */
  747. #if defined ( __CC_ARM)
  748. __force_stores();
  749. #endif /* __CC_ARM */
  750. /* Request Wait For Interrupt */
  751. __WFI();
  752. }
  753. #endif /* PWR_SHDW_SUPPORT */
  754. #if defined(PWR_PVD_SUPPORT) && defined(PWR_PVM_SUPPORT)
  755. /**
  756. * @brief This function handles the PWR PVD interrupt request.
  757. * @note This API should be called under the PVD_IRQHandler().
  758. * @retval None
  759. */
  760. void HAL_PWREx_PVD_PVM_IRQHandler(void)
  761. {
  762. /* Check PWR PVD exti Rising flag */
  763. if (__HAL_PWR_PVD_EXTI_GET_RISING_FLAG() != 0x0U)
  764. {
  765. /* Clear PVD exti pending bit */
  766. __HAL_PWR_PVD_EXTI_CLEAR_RISING_FLAG();
  767. /* PWR PVD interrupt rising user callback */
  768. HAL_PWREx_PVD_PVM_Rising_Callback();
  769. }
  770. /* Check PWR exti fallling flag */
  771. if (__HAL_PWR_PVD_EXTI_GET_FALLING_FLAG() != 0x0U)
  772. {
  773. /* Clear PVD exti pending bit */
  774. __HAL_PWR_PVD_EXTI_CLEAR_FALLING_FLAG();
  775. /* PWR PVD interrupt falling user callback */
  776. HAL_PWREx_PVD_PVM_Falling_Callback();
  777. }
  778. /* Check PWR PVM exti Rising flag */
  779. if (__HAL_PWR_PVM_EXTI_GET_RISING_FLAG() != 0x0U)
  780. {
  781. /* Clear PVM exti pending bit */
  782. __HAL_PWR_PVM_EXTI_CLEAR_RISING_FLAG();
  783. /* PWR PVD PVM interrupt rising user callback */
  784. HAL_PWREx_PVD_PVM_Rising_Callback();
  785. }
  786. /* Check PWR PVM exti fallling flag */
  787. if (__HAL_PWR_PVM_EXTI_GET_FALLING_FLAG() != 0x0U)
  788. {
  789. /* Clear PVM exti pending bit */
  790. __HAL_PWR_PVM_EXTI_CLEAR_FALLING_FLAG();
  791. /* PWR PVM interrupt falling user callback */
  792. HAL_PWREx_PVD_PVM_Falling_Callback();
  793. }
  794. }
  795. /**
  796. * @brief PWR PVD interrupt rising callback
  797. * @retval None
  798. */
  799. __weak void HAL_PWREx_PVD_PVM_Rising_Callback(void)
  800. {
  801. /* NOTE : This function should not be modified; when the callback is needed,
  802. the HAL_PWR_PVD_Rising_Callback can be implemented in the user file
  803. */
  804. }
  805. /**
  806. * @brief PWR PVD interrupt Falling callback
  807. * @retval None
  808. */
  809. __weak void HAL_PWREx_PVD_PVM_Falling_Callback(void)
  810. {
  811. /* NOTE : This function should not be modified; when the callback is needed,
  812. the HAL_PWR_PVD_Falling_Callback can be implemented in the user file
  813. */
  814. }
  815. #elif defined(PWR_PVD_SUPPORT)
  816. /**
  817. * @brief This function handles the PWR PVD interrupt request.
  818. * @note This API should be called under the PVD_IRQHandler().
  819. * @retval None
  820. */
  821. void HAL_PWREx_PVD_IRQHandler(void)
  822. {
  823. /* Check PWR exti Rising flag */
  824. if (__HAL_PWR_PVD_EXTI_GET_RISING_FLAG() != 0x0U)
  825. {
  826. /* Clear PVD exti pending bit */
  827. __HAL_PWR_PVD_EXTI_CLEAR_RISING_FLAG();
  828. /* PWR PVD interrupt rising user callback */
  829. HAL_PWREx_PVD_Rising_Callback();
  830. }
  831. /* Check PWR exti fallling flag */
  832. if (__HAL_PWR_PVD_EXTI_GET_FALLING_FLAG() != 0x0U)
  833. {
  834. /* Clear PVD exti pending bit */
  835. __HAL_PWR_PVD_EXTI_CLEAR_FALLING_FLAG();
  836. /* PWR PVD interrupt falling user callback */
  837. HAL_PWREx_PVD_Falling_Callback();
  838. }
  839. }
  840. /**
  841. * @brief PWR PVD interrupt rising callback
  842. * @retval None
  843. */
  844. __weak void HAL_PWREx_PVD_Rising_Callback(void)
  845. {
  846. /* NOTE : This function should not be modified; when the callback is needed,
  847. the HAL_PWR_PVD_Rising_Callback can be implemented in the user file
  848. */
  849. }
  850. /**
  851. * @brief PWR PVD interrupt Falling callback
  852. * @retval None
  853. */
  854. __weak void HAL_PWREx_PVD_Falling_Callback(void)
  855. {
  856. /* NOTE : This function should not be modified; when the callback is needed,
  857. the HAL_PWR_PVD_Falling_Callback can be implemented in the user file
  858. */
  859. }
  860. #endif /* PWR_PVD_SUPPORT && PWR_PVM_SUPPORT */
  861. /**
  862. * @}
  863. */
  864. /**
  865. * @}
  866. */
  867. #endif /* HAL_PWR_MODULE_ENABLED */
  868. /**
  869. * @}
  870. */
  871. /**
  872. * @}
  873. */