stm32l4xx_hal_pwr_ex.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.</center></h2>
  16. *
  17. * This software component is licensed by ST 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. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l4xx_hal.h"
  26. /** @addtogroup STM32L4xx_HAL_Driver
  27. * @{
  28. */
  29. /** @defgroup PWREx PWREx
  30. * @brief PWR Extended HAL module driver
  31. * @{
  32. */
  33. #ifdef HAL_PWR_MODULE_ENABLED
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx)
  37. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000000B) /* PH0/PH1/PH3 */
  38. #elif defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  39. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000000B) /* PH0/PH1/PH3 */
  40. #elif defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx)
  41. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x00000003) /* PH0/PH1 */
  42. #elif defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  43. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000FFFF) /* PH0..PH15 */
  44. #endif
  45. #if defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  46. #define PWR_PORTI_AVAILABLE_PINS ((uint32_t)0x00000FFF) /* PI0..PI11 */
  47. #endif
  48. /** @defgroup PWR_Extended_Private_Defines PWR Extended Private Defines
  49. * @{
  50. */
  51. /** @defgroup PWREx_PVM_Mode_Mask PWR PVM Mode Mask
  52. * @{
  53. */
  54. #define PVM_MODE_IT ((uint32_t)0x00010000) /*!< Mask for interruption yielded by PVM threshold crossing */
  55. #define PVM_MODE_EVT ((uint32_t)0x00020000) /*!< Mask for event yielded by PVM threshold crossing */
  56. #define PVM_RISING_EDGE ((uint32_t)0x00000001) /*!< Mask for rising edge set as PVM trigger */
  57. #define PVM_FALLING_EDGE ((uint32_t)0x00000002) /*!< Mask for falling edge set as PVM trigger */
  58. /**
  59. * @}
  60. */
  61. /** @defgroup PWREx_TimeOut_Value PWR Extended Flag Setting Time Out Value
  62. * @{
  63. */
  64. #define PWR_FLAG_SETTING_DELAY_US 50UL /*!< Time out value for REGLPF and VOSF flags setting */
  65. /**
  66. * @}
  67. */
  68. /**
  69. * @}
  70. */
  71. /* Private macro -------------------------------------------------------------*/
  72. /* Private variables ---------------------------------------------------------*/
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Exported functions --------------------------------------------------------*/
  75. /** @defgroup PWREx_Exported_Functions PWR Extended Exported Functions
  76. * @{
  77. */
  78. /** @defgroup PWREx_Exported_Functions_Group1 Extended Peripheral Control functions
  79. * @brief Extended Peripheral Control functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. ##### Extended Peripheral Initialization and de-initialization functions #####
  84. ===============================================================================
  85. [..]
  86. @endverbatim
  87. * @{
  88. */
  89. /**
  90. * @brief Return Voltage Scaling Range.
  91. * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_RANGE1 or PWR_REGULATOR_VOLTAGE_RANGE2
  92. * or PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when applicable)
  93. */
  94. uint32_t HAL_PWREx_GetVoltageRange(void)
  95. {
  96. #if defined(PWR_CR5_R1MODE)
  97. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  98. {
  99. return PWR_REGULATOR_VOLTAGE_SCALE2;
  100. }
  101. else if (READ_BIT(PWR->CR5, PWR_CR5_R1MODE) == PWR_CR5_R1MODE)
  102. {
  103. /* PWR_CR5_R1MODE bit set means that Range 1 Boost is disabled */
  104. return PWR_REGULATOR_VOLTAGE_SCALE1;
  105. }
  106. else
  107. {
  108. return PWR_REGULATOR_VOLTAGE_SCALE1_BOOST;
  109. }
  110. #else
  111. return (PWR->CR1 & PWR_CR1_VOS);
  112. #endif
  113. }
  114. /**
  115. * @brief Configure the main internal regulator output voltage.
  116. * @param VoltageScaling: specifies the regulator output voltage to achieve
  117. * a tradeoff between performance and power consumption.
  118. * This parameter can be one of the following values:
  119. @if STM32L4S9xx
  120. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when available, Regulator voltage output range 1 boost mode,
  121. * typical output voltage at 1.2 V,
  122. * system frequency up to 120 MHz.
  123. @endif
  124. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1 Regulator voltage output range 1 mode,
  125. * typical output voltage at 1.2 V,
  126. * system frequency up to 80 MHz.
  127. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2 Regulator voltage output range 2 mode,
  128. * typical output voltage at 1.0 V,
  129. * system frequency up to 26 MHz.
  130. * @note When moving from Range 1 to Range 2, the system frequency must be decreased to
  131. * a value below 26 MHz before calling HAL_PWREx_ControlVoltageScaling() API.
  132. * When moving from Range 2 to Range 1, the system frequency can be increased to
  133. * a value up to 80 MHz after calling HAL_PWREx_ControlVoltageScaling() API. For
  134. * some devices, the system frequency can be increased up to 120 MHz.
  135. * @note When moving from Range 2 to Range 1, the API waits for VOSF flag to be
  136. * cleared before returning the status. If the flag is not cleared within
  137. * 50 microseconds, HAL_TIMEOUT status is reported.
  138. * @retval HAL Status
  139. */
  140. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  141. {
  142. uint32_t wait_loop_index;
  143. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  144. #if defined(PWR_CR5_R1MODE)
  145. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1_BOOST)
  146. {
  147. /* If current range is range 2 */
  148. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  149. {
  150. /* Make sure Range 1 Boost is enabled */
  151. CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);
  152. /* Set Range 1 */
  153. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  154. /* Wait until VOSF is cleared */
  155. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1;
  156. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  157. {
  158. wait_loop_index--;
  159. }
  160. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  161. {
  162. return HAL_TIMEOUT;
  163. }
  164. }
  165. /* If current range is range 1 normal or boost mode */
  166. else
  167. {
  168. /* Enable Range 1 Boost (no issue if bit already reset) */
  169. CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);
  170. }
  171. }
  172. else if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  173. {
  174. /* If current range is range 2 */
  175. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  176. {
  177. /* Make sure Range 1 Boost is disabled */
  178. SET_BIT(PWR->CR5, PWR_CR5_R1MODE);
  179. /* Set Range 1 */
  180. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  181. /* Wait until VOSF is cleared */
  182. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1;
  183. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  184. {
  185. wait_loop_index--;
  186. }
  187. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  188. {
  189. return HAL_TIMEOUT;
  190. }
  191. }
  192. /* If current range is range 1 normal or boost mode */
  193. else
  194. {
  195. /* Disable Range 1 Boost (no issue if bit already set) */
  196. SET_BIT(PWR->CR5, PWR_CR5_R1MODE);
  197. }
  198. }
  199. else
  200. {
  201. /* Set Range 2 */
  202. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE2);
  203. /* No need to wait for VOSF to be cleared for this transition */
  204. /* PWR_CR5_R1MODE bit setting has no effect in Range 2 */
  205. }
  206. #else
  207. /* If Set Range 1 */
  208. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  209. {
  210. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE1)
  211. {
  212. /* Set Range 1 */
  213. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  214. /* Wait until VOSF is cleared */
  215. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
  216. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  217. {
  218. wait_loop_index--;
  219. }
  220. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  221. {
  222. return HAL_TIMEOUT;
  223. }
  224. }
  225. }
  226. else
  227. {
  228. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE2)
  229. {
  230. /* Set Range 2 */
  231. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE2);
  232. /* No need to wait for VOSF to be cleared for this transition */
  233. }
  234. }
  235. #endif
  236. return HAL_OK;
  237. }
  238. /**
  239. * @brief Enable battery charging.
  240. * When VDD is present, charge the external battery on VBAT thru an internal resistor.
  241. * @param ResistorSelection: specifies the resistor impedance.
  242. * This parameter can be one of the following values:
  243. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_5 5 kOhms resistor
  244. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_1_5 1.5 kOhms resistor
  245. * @retval None
  246. */
  247. void HAL_PWREx_EnableBatteryCharging(uint32_t ResistorSelection)
  248. {
  249. assert_param(IS_PWR_BATTERY_RESISTOR_SELECT(ResistorSelection));
  250. /* Specify resistor selection */
  251. MODIFY_REG(PWR->CR4, PWR_CR4_VBRS, ResistorSelection);
  252. /* Enable battery charging */
  253. SET_BIT(PWR->CR4, PWR_CR4_VBE);
  254. }
  255. /**
  256. * @brief Disable battery charging.
  257. * @retval None
  258. */
  259. void HAL_PWREx_DisableBatteryCharging(void)
  260. {
  261. CLEAR_BIT(PWR->CR4, PWR_CR4_VBE);
  262. }
  263. #if defined(PWR_CR2_USV)
  264. /**
  265. * @brief Enable VDDUSB supply.
  266. * @note Remove VDDUSB electrical and logical isolation, once VDDUSB supply is present.
  267. * @retval None
  268. */
  269. void HAL_PWREx_EnableVddUSB(void)
  270. {
  271. SET_BIT(PWR->CR2, PWR_CR2_USV);
  272. }
  273. /**
  274. * @brief Disable VDDUSB supply.
  275. * @retval None
  276. */
  277. void HAL_PWREx_DisableVddUSB(void)
  278. {
  279. CLEAR_BIT(PWR->CR2, PWR_CR2_USV);
  280. }
  281. #endif /* PWR_CR2_USV */
  282. #if defined(PWR_CR2_IOSV)
  283. /**
  284. * @brief Enable VDDIO2 supply.
  285. * @note Remove VDDIO2 electrical and logical isolation, once VDDIO2 supply is present.
  286. * @retval None
  287. */
  288. void HAL_PWREx_EnableVddIO2(void)
  289. {
  290. SET_BIT(PWR->CR2, PWR_CR2_IOSV);
  291. }
  292. /**
  293. * @brief Disable VDDIO2 supply.
  294. * @retval None
  295. */
  296. void HAL_PWREx_DisableVddIO2(void)
  297. {
  298. CLEAR_BIT(PWR->CR2, PWR_CR2_IOSV);
  299. }
  300. #endif /* PWR_CR2_IOSV */
  301. /**
  302. * @brief Enable Internal Wake-up Line.
  303. * @retval None
  304. */
  305. void HAL_PWREx_EnableInternalWakeUpLine(void)
  306. {
  307. SET_BIT(PWR->CR3, PWR_CR3_EIWF);
  308. }
  309. /**
  310. * @brief Disable Internal Wake-up Line.
  311. * @retval None
  312. */
  313. void HAL_PWREx_DisableInternalWakeUpLine(void)
  314. {
  315. CLEAR_BIT(PWR->CR3, PWR_CR3_EIWF);
  316. }
  317. /**
  318. * @brief Enable GPIO pull-up state in Standby and Shutdown modes.
  319. * @note Set the relevant PUy bits of PWR_PUCRx register to configure the I/O in
  320. * pull-up state in Standby and Shutdown modes.
  321. * @note This state is effective in Standby and Shutdown modes only if APC bit
  322. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  323. * @note The configuration is lost when exiting the Shutdown mode due to the
  324. * power-on reset, maintained when exiting the Standby mode.
  325. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  326. * PDy bit of PWR_PDCRx register is cleared unless it is reserved.
  327. * @note Even if a PUy bit to set is reserved, the other PUy bits entered as input
  328. * parameter at the same time are set.
  329. * @param GPIO: Specify the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  330. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  331. * @param GPIONumber: Specify the I/O pins numbers.
  332. * This parameter can be one of the following values:
  333. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  334. * I/O pins are available) or the logical OR of several of them to set
  335. * several bits for a given port in a single API call.
  336. * @retval HAL Status
  337. */
  338. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  339. {
  340. HAL_StatusTypeDef status = HAL_OK;
  341. assert_param(IS_PWR_GPIO(GPIO));
  342. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  343. switch (GPIO)
  344. {
  345. case PWR_GPIO_A:
  346. SET_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  347. CLEAR_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  348. break;
  349. case PWR_GPIO_B:
  350. SET_BIT(PWR->PUCRB, GPIONumber);
  351. CLEAR_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  352. break;
  353. case PWR_GPIO_C:
  354. SET_BIT(PWR->PUCRC, GPIONumber);
  355. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  356. break;
  357. #if defined(GPIOD)
  358. case PWR_GPIO_D:
  359. SET_BIT(PWR->PUCRD, GPIONumber);
  360. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  361. break;
  362. #endif
  363. #if defined(GPIOE)
  364. case PWR_GPIO_E:
  365. SET_BIT(PWR->PUCRE, GPIONumber);
  366. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  367. break;
  368. #endif
  369. #if defined(GPIOF)
  370. case PWR_GPIO_F:
  371. SET_BIT(PWR->PUCRF, GPIONumber);
  372. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  373. break;
  374. #endif
  375. #if defined(GPIOG)
  376. case PWR_GPIO_G:
  377. SET_BIT(PWR->PUCRG, GPIONumber);
  378. CLEAR_BIT(PWR->PDCRG, GPIONumber);
  379. break;
  380. #endif
  381. case PWR_GPIO_H:
  382. SET_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  383. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  384. CLEAR_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  385. #else
  386. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  387. #endif
  388. break;
  389. #if defined(GPIOI)
  390. case PWR_GPIO_I:
  391. SET_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  392. CLEAR_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  393. break;
  394. #endif
  395. default:
  396. status = HAL_ERROR;
  397. break;
  398. }
  399. return status;
  400. }
  401. /**
  402. * @brief Disable GPIO pull-up state in Standby mode and Shutdown modes.
  403. * @note Reset the relevant PUy bits of PWR_PUCRx register used to configure the I/O
  404. * in pull-up state in Standby and Shutdown modes.
  405. * @note Even if a PUy bit to reset is reserved, the other PUy bits entered as input
  406. * parameter at the same time are reset.
  407. * @param GPIO: Specifies the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  408. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  409. * @param GPIONumber: Specify the I/O pins numbers.
  410. * This parameter can be one of the following values:
  411. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  412. * I/O pins are available) or the logical OR of several of them to reset
  413. * several bits for a given port in a single API call.
  414. * @retval HAL Status
  415. */
  416. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  417. {
  418. HAL_StatusTypeDef status = HAL_OK;
  419. assert_param(IS_PWR_GPIO(GPIO));
  420. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  421. switch (GPIO)
  422. {
  423. case PWR_GPIO_A:
  424. CLEAR_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  425. break;
  426. case PWR_GPIO_B:
  427. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  428. break;
  429. case PWR_GPIO_C:
  430. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  431. break;
  432. #if defined(GPIOD)
  433. case PWR_GPIO_D:
  434. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  435. break;
  436. #endif
  437. #if defined(GPIOE)
  438. case PWR_GPIO_E:
  439. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  440. break;
  441. #endif
  442. #if defined(GPIOF)
  443. case PWR_GPIO_F:
  444. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  445. break;
  446. #endif
  447. #if defined(GPIOG)
  448. case PWR_GPIO_G:
  449. CLEAR_BIT(PWR->PUCRG, GPIONumber);
  450. break;
  451. #endif
  452. case PWR_GPIO_H:
  453. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  454. break;
  455. #if defined(GPIOI)
  456. case PWR_GPIO_I:
  457. CLEAR_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  458. break;
  459. #endif
  460. default:
  461. status = HAL_ERROR;
  462. break;
  463. }
  464. return status;
  465. }
  466. /**
  467. * @brief Enable GPIO pull-down state in Standby and Shutdown modes.
  468. * @note Set the relevant PDy bits of PWR_PDCRx register to configure the I/O in
  469. * pull-down state in Standby and Shutdown modes.
  470. * @note This state is effective in Standby and Shutdown modes only if APC bit
  471. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  472. * @note The configuration is lost when exiting the Shutdown mode due to the
  473. * power-on reset, maintained when exiting the Standby mode.
  474. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  475. * PUy bit of PWR_PUCRx register is cleared unless it is reserved.
  476. * @note Even if a PDy bit to set is reserved, the other PDy bits entered as input
  477. * parameter at the same time are set.
  478. * @param GPIO: Specify the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  479. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  480. * @param GPIONumber: Specify the I/O pins numbers.
  481. * This parameter can be one of the following values:
  482. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  483. * I/O pins are available) or the logical OR of several of them to set
  484. * several bits for a given port in a single API call.
  485. * @retval HAL Status
  486. */
  487. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  488. {
  489. HAL_StatusTypeDef status = HAL_OK;
  490. assert_param(IS_PWR_GPIO(GPIO));
  491. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  492. switch (GPIO)
  493. {
  494. case PWR_GPIO_A:
  495. SET_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  496. CLEAR_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  497. break;
  498. case PWR_GPIO_B:
  499. SET_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  500. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  501. break;
  502. case PWR_GPIO_C:
  503. SET_BIT(PWR->PDCRC, GPIONumber);
  504. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  505. break;
  506. #if defined(GPIOD)
  507. case PWR_GPIO_D:
  508. SET_BIT(PWR->PDCRD, GPIONumber);
  509. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  510. break;
  511. #endif
  512. #if defined(GPIOE)
  513. case PWR_GPIO_E:
  514. SET_BIT(PWR->PDCRE, GPIONumber);
  515. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  516. break;
  517. #endif
  518. #if defined(GPIOF)
  519. case PWR_GPIO_F:
  520. SET_BIT(PWR->PDCRF, GPIONumber);
  521. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  522. break;
  523. #endif
  524. #if defined(GPIOG)
  525. case PWR_GPIO_G:
  526. SET_BIT(PWR->PDCRG, GPIONumber);
  527. CLEAR_BIT(PWR->PUCRG, GPIONumber);
  528. break;
  529. #endif
  530. case PWR_GPIO_H:
  531. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  532. SET_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  533. #else
  534. SET_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  535. #endif
  536. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  537. break;
  538. #if defined(GPIOI)
  539. case PWR_GPIO_I:
  540. SET_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  541. CLEAR_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  542. break;
  543. #endif
  544. default:
  545. status = HAL_ERROR;
  546. break;
  547. }
  548. return status;
  549. }
  550. /**
  551. * @brief Disable GPIO pull-down state in Standby and Shutdown modes.
  552. * @note Reset the relevant PDy bits of PWR_PDCRx register used to configure the I/O
  553. * in pull-down state in Standby and Shutdown modes.
  554. * @note Even if a PDy bit to reset is reserved, the other PDy bits entered as input
  555. * parameter at the same time are reset.
  556. * @param GPIO: Specifies the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  557. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  558. * @param GPIONumber: Specify the I/O pins numbers.
  559. * This parameter can be one of the following values:
  560. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  561. * I/O pins are available) or the logical OR of several of them to reset
  562. * several bits for a given port in a single API call.
  563. * @retval HAL Status
  564. */
  565. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  566. {
  567. HAL_StatusTypeDef status = HAL_OK;
  568. assert_param(IS_PWR_GPIO(GPIO));
  569. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  570. switch (GPIO)
  571. {
  572. case PWR_GPIO_A:
  573. CLEAR_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  574. break;
  575. case PWR_GPIO_B:
  576. CLEAR_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  577. break;
  578. case PWR_GPIO_C:
  579. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  580. break;
  581. #if defined(GPIOD)
  582. case PWR_GPIO_D:
  583. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  584. break;
  585. #endif
  586. #if defined(GPIOE)
  587. case PWR_GPIO_E:
  588. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  589. break;
  590. #endif
  591. #if defined(GPIOF)
  592. case PWR_GPIO_F:
  593. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  594. break;
  595. #endif
  596. #if defined(GPIOG)
  597. case PWR_GPIO_G:
  598. CLEAR_BIT(PWR->PDCRG, GPIONumber);
  599. break;
  600. #endif
  601. case PWR_GPIO_H:
  602. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  603. CLEAR_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  604. #else
  605. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  606. #endif
  607. break;
  608. #if defined(GPIOI)
  609. case PWR_GPIO_I:
  610. CLEAR_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  611. break;
  612. #endif
  613. default:
  614. status = HAL_ERROR;
  615. break;
  616. }
  617. return status;
  618. }
  619. /**
  620. * @brief Enable pull-up and pull-down configuration.
  621. * @note When APC bit is set, the I/O pull-up and pull-down configurations defined in
  622. * PWR_PUCRx and PWR_PDCRx registers are applied in Standby and Shutdown modes.
  623. * @note Pull-up set by PUy bit of PWR_PUCRx register is not activated if the corresponding
  624. * PDy bit of PWR_PDCRx register is also set (pull-down configuration priority is higher).
  625. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() API's ensure there
  626. * is no conflict when setting PUy or PDy bit.
  627. * @retval None
  628. */
  629. void HAL_PWREx_EnablePullUpPullDownConfig(void)
  630. {
  631. SET_BIT(PWR->CR3, PWR_CR3_APC);
  632. }
  633. /**
  634. * @brief Disable pull-up and pull-down configuration.
  635. * @note When APC bit is cleared, the I/O pull-up and pull-down configurations defined in
  636. * PWR_PUCRx and PWR_PDCRx registers are not applied in Standby and Shutdown modes.
  637. * @retval None
  638. */
  639. void HAL_PWREx_DisablePullUpPullDownConfig(void)
  640. {
  641. CLEAR_BIT(PWR->CR3, PWR_CR3_APC);
  642. }
  643. /**
  644. * @brief Enable SRAM2 content retention in Standby mode.
  645. * @note When RRS bit is set, SRAM2 is powered by the low-power regulator in
  646. * Standby mode and its content is kept.
  647. * @retval None
  648. */
  649. void HAL_PWREx_EnableSRAM2ContentRetention(void)
  650. {
  651. SET_BIT(PWR->CR3, PWR_CR3_RRS);
  652. }
  653. /**
  654. * @brief Disable SRAM2 content retention in Standby mode.
  655. * @note When RRS bit is reset, SRAM2 is powered off in Standby mode
  656. * and its content is lost.
  657. * @retval None
  658. */
  659. void HAL_PWREx_DisableSRAM2ContentRetention(void)
  660. {
  661. CLEAR_BIT(PWR->CR3, PWR_CR3_RRS);
  662. }
  663. #if defined(PWR_CR3_ENULP)
  664. /**
  665. * @brief Enable Ultra Low Power BORL, BORH and PVD for STOP2 and Standby modes.
  666. * @note All the other modes are not affected by this bit.
  667. * @retval None
  668. */
  669. void HAL_PWREx_EnableBORPVD_ULP(void)
  670. {
  671. SET_BIT(PWR->CR3, PWR_CR3_ENULP);
  672. }
  673. /**
  674. * @brief Disable Ultra Low Power BORL, BORH and PVD for STOP2 and Standby modes.
  675. * @note All the other modes are not affected by this bit
  676. * @retval None
  677. */
  678. void HAL_PWREx_DisableBORPVD_ULP(void)
  679. {
  680. CLEAR_BIT(PWR->CR3, PWR_CR3_ENULP);
  681. }
  682. #endif /* PWR_CR3_ENULP */
  683. #if defined(PWR_CR4_EXT_SMPS_ON)
  684. /**
  685. * @brief Enable the CFLDO working @ 0.95V.
  686. * @note When external SMPS is used & CFLDO operating in Range 2, the regulated voltage of the
  687. * internal CFLDO can be reduced to 0.95V.
  688. * @retval None
  689. */
  690. void HAL_PWREx_EnableExtSMPS_0V95(void)
  691. {
  692. SET_BIT(PWR->CR4, PWR_CR4_EXT_SMPS_ON);
  693. }
  694. /**
  695. * @brief Disable the CFLDO working @ 0.95V
  696. * @note Before SMPS is switched off, the regulated voltage of the
  697. * internal CFLDO shall be set to 1.00V.
  698. * 1.00V. is also default operating Range 2 voltage.
  699. * @retval None
  700. */
  701. void HAL_PWREx_DisableExtSMPS_0V95(void)
  702. {
  703. CLEAR_BIT(PWR->CR4, PWR_CR4_EXT_SMPS_ON);
  704. }
  705. #endif /* PWR_CR4_EXT_SMPS_ON */
  706. #if defined(PWR_CR1_RRSTP)
  707. /**
  708. * @brief Enable SRAM3 content retention in Stop 2 mode.
  709. * @note When RRSTP bit is set, SRAM3 is powered by the low-power regulator in
  710. * Stop 2 mode and its content is kept.
  711. * @retval None
  712. */
  713. void HAL_PWREx_EnableSRAM3ContentRetention(void)
  714. {
  715. SET_BIT(PWR->CR1, PWR_CR1_RRSTP);
  716. }
  717. /**
  718. * @brief Disable SRAM3 content retention in Stop 2 mode.
  719. * @note When RRSTP bit is reset, SRAM3 is powered off in Stop 2 mode
  720. * and its content is lost.
  721. * @retval None
  722. */
  723. void HAL_PWREx_DisableSRAM3ContentRetention(void)
  724. {
  725. CLEAR_BIT(PWR->CR1, PWR_CR1_RRSTP);
  726. }
  727. #endif /* PWR_CR1_RRSTP */
  728. #if defined(PWR_CR3_DSIPDEN)
  729. /**
  730. * @brief Enable pull-down activation on DSI pins.
  731. * @retval None
  732. */
  733. void HAL_PWREx_EnableDSIPinsPDActivation(void)
  734. {
  735. SET_BIT(PWR->CR3, PWR_CR3_DSIPDEN);
  736. }
  737. /**
  738. * @brief Disable pull-down activation on DSI pins.
  739. * @retval None
  740. */
  741. void HAL_PWREx_DisableDSIPinsPDActivation(void)
  742. {
  743. CLEAR_BIT(PWR->CR3, PWR_CR3_DSIPDEN);
  744. }
  745. #endif /* PWR_CR3_DSIPDEN */
  746. #if defined(PWR_CR2_PVME1)
  747. /**
  748. * @brief Enable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  749. * @retval None
  750. */
  751. void HAL_PWREx_EnablePVM1(void)
  752. {
  753. SET_BIT(PWR->CR2, PWR_PVM_1);
  754. }
  755. /**
  756. * @brief Disable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  757. * @retval None
  758. */
  759. void HAL_PWREx_DisablePVM1(void)
  760. {
  761. CLEAR_BIT(PWR->CR2, PWR_PVM_1);
  762. }
  763. #endif /* PWR_CR2_PVME1 */
  764. #if defined(PWR_CR2_PVME2)
  765. /**
  766. * @brief Enable the Power Voltage Monitoring 2: VDDIO2 versus 0.9V.
  767. * @retval None
  768. */
  769. void HAL_PWREx_EnablePVM2(void)
  770. {
  771. SET_BIT(PWR->CR2, PWR_PVM_2);
  772. }
  773. /**
  774. * @brief Disable the Power Voltage Monitoring 2: VDDIO2 versus 0.9V.
  775. * @retval None
  776. */
  777. void HAL_PWREx_DisablePVM2(void)
  778. {
  779. CLEAR_BIT(PWR->CR2, PWR_PVM_2);
  780. }
  781. #endif /* PWR_CR2_PVME2 */
  782. /**
  783. * @brief Enable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  784. * @retval None
  785. */
  786. void HAL_PWREx_EnablePVM3(void)
  787. {
  788. SET_BIT(PWR->CR2, PWR_PVM_3);
  789. }
  790. /**
  791. * @brief Disable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  792. * @retval None
  793. */
  794. void HAL_PWREx_DisablePVM3(void)
  795. {
  796. CLEAR_BIT(PWR->CR2, PWR_PVM_3);
  797. }
  798. /**
  799. * @brief Enable the Power Voltage Monitoring 4: VDDA versus 2.2V.
  800. * @retval None
  801. */
  802. void HAL_PWREx_EnablePVM4(void)
  803. {
  804. SET_BIT(PWR->CR2, PWR_PVM_4);
  805. }
  806. /**
  807. * @brief Disable the Power Voltage Monitoring 4: VDDA versus 2.2V.
  808. * @retval None
  809. */
  810. void HAL_PWREx_DisablePVM4(void)
  811. {
  812. CLEAR_BIT(PWR->CR2, PWR_PVM_4);
  813. }
  814. /**
  815. * @brief Configure the Peripheral Voltage Monitoring (PVM).
  816. * @param sConfigPVM: pointer to a PWR_PVMTypeDef structure that contains the
  817. * PVM configuration information.
  818. * @note The API configures a single PVM according to the information contained
  819. * in the input structure. To configure several PVMs, the API must be singly
  820. * called for each PVM used.
  821. * @note Refer to the electrical characteristics of your device datasheet for
  822. * more details about the voltage thresholds corresponding to each
  823. * detection level and to each monitored supply.
  824. * @retval HAL status
  825. */
  826. HAL_StatusTypeDef HAL_PWREx_ConfigPVM(PWR_PVMTypeDef *sConfigPVM)
  827. {
  828. HAL_StatusTypeDef status = HAL_OK;
  829. /* Check the parameters */
  830. assert_param(IS_PWR_PVM_TYPE(sConfigPVM->PVMType));
  831. assert_param(IS_PWR_PVM_MODE(sConfigPVM->Mode));
  832. /* Configure EXTI 35 to 38 interrupts if so required:
  833. scan thru PVMType to detect which PVMx is set and
  834. configure the corresponding EXTI line accordingly. */
  835. switch (sConfigPVM->PVMType)
  836. {
  837. #if defined(PWR_CR2_PVME1)
  838. case PWR_PVM_1:
  839. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  840. __HAL_PWR_PVM1_EXTI_DISABLE_EVENT();
  841. __HAL_PWR_PVM1_EXTI_DISABLE_IT();
  842. __HAL_PWR_PVM1_EXTI_DISABLE_FALLING_EDGE();
  843. __HAL_PWR_PVM1_EXTI_DISABLE_RISING_EDGE();
  844. /* Configure interrupt mode */
  845. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  846. {
  847. __HAL_PWR_PVM1_EXTI_ENABLE_IT();
  848. }
  849. /* Configure event mode */
  850. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  851. {
  852. __HAL_PWR_PVM1_EXTI_ENABLE_EVENT();
  853. }
  854. /* Configure the edge */
  855. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  856. {
  857. __HAL_PWR_PVM1_EXTI_ENABLE_RISING_EDGE();
  858. }
  859. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  860. {
  861. __HAL_PWR_PVM1_EXTI_ENABLE_FALLING_EDGE();
  862. }
  863. break;
  864. #endif /* PWR_CR2_PVME1 */
  865. #if defined(PWR_CR2_PVME2)
  866. case PWR_PVM_2:
  867. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  868. __HAL_PWR_PVM2_EXTI_DISABLE_EVENT();
  869. __HAL_PWR_PVM2_EXTI_DISABLE_IT();
  870. __HAL_PWR_PVM2_EXTI_DISABLE_FALLING_EDGE();
  871. __HAL_PWR_PVM2_EXTI_DISABLE_RISING_EDGE();
  872. /* Configure interrupt mode */
  873. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  874. {
  875. __HAL_PWR_PVM2_EXTI_ENABLE_IT();
  876. }
  877. /* Configure event mode */
  878. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  879. {
  880. __HAL_PWR_PVM2_EXTI_ENABLE_EVENT();
  881. }
  882. /* Configure the edge */
  883. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  884. {
  885. __HAL_PWR_PVM2_EXTI_ENABLE_RISING_EDGE();
  886. }
  887. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  888. {
  889. __HAL_PWR_PVM2_EXTI_ENABLE_FALLING_EDGE();
  890. }
  891. break;
  892. #endif /* PWR_CR2_PVME2 */
  893. case PWR_PVM_3:
  894. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  895. __HAL_PWR_PVM3_EXTI_DISABLE_EVENT();
  896. __HAL_PWR_PVM3_EXTI_DISABLE_IT();
  897. __HAL_PWR_PVM3_EXTI_DISABLE_FALLING_EDGE();
  898. __HAL_PWR_PVM3_EXTI_DISABLE_RISING_EDGE();
  899. /* Configure interrupt mode */
  900. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  901. {
  902. __HAL_PWR_PVM3_EXTI_ENABLE_IT();
  903. }
  904. /* Configure event mode */
  905. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  906. {
  907. __HAL_PWR_PVM3_EXTI_ENABLE_EVENT();
  908. }
  909. /* Configure the edge */
  910. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  911. {
  912. __HAL_PWR_PVM3_EXTI_ENABLE_RISING_EDGE();
  913. }
  914. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  915. {
  916. __HAL_PWR_PVM3_EXTI_ENABLE_FALLING_EDGE();
  917. }
  918. break;
  919. case PWR_PVM_4:
  920. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  921. __HAL_PWR_PVM4_EXTI_DISABLE_EVENT();
  922. __HAL_PWR_PVM4_EXTI_DISABLE_IT();
  923. __HAL_PWR_PVM4_EXTI_DISABLE_FALLING_EDGE();
  924. __HAL_PWR_PVM4_EXTI_DISABLE_RISING_EDGE();
  925. /* Configure interrupt mode */
  926. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  927. {
  928. __HAL_PWR_PVM4_EXTI_ENABLE_IT();
  929. }
  930. /* Configure event mode */
  931. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  932. {
  933. __HAL_PWR_PVM4_EXTI_ENABLE_EVENT();
  934. }
  935. /* Configure the edge */
  936. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  937. {
  938. __HAL_PWR_PVM4_EXTI_ENABLE_RISING_EDGE();
  939. }
  940. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  941. {
  942. __HAL_PWR_PVM4_EXTI_ENABLE_FALLING_EDGE();
  943. }
  944. break;
  945. default:
  946. status = HAL_ERROR;
  947. break;
  948. }
  949. return status;
  950. }
  951. /**
  952. * @brief Enter Low-power Run mode
  953. * @note In Low-power Run mode, all I/O pins keep the same state as in Run mode.
  954. * @note When Regulator is set to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  955. * Flash in power-down monde in setting the RUN_PD bit in FLASH_ACR register.
  956. * Additionally, the clock frequency must be reduced below 2 MHz.
  957. * Setting RUN_PD in FLASH_ACR then appropriately reducing the clock frequency must
  958. * be done before calling HAL_PWREx_EnableLowPowerRunMode() API.
  959. * @retval None
  960. */
  961. void HAL_PWREx_EnableLowPowerRunMode(void)
  962. {
  963. /* Set Regulator parameter */
  964. SET_BIT(PWR->CR1, PWR_CR1_LPR);
  965. }
  966. /**
  967. * @brief Exit Low-power Run mode.
  968. * @note Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
  969. * REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
  970. * returns HAL_TIMEOUT status). The system clock frequency can then be
  971. * increased above 2 MHz.
  972. * @retval HAL Status
  973. */
  974. HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
  975. {
  976. uint32_t wait_loop_index;
  977. /* Clear LPR bit */
  978. CLEAR_BIT(PWR->CR1, PWR_CR1_LPR);
  979. /* Wait until REGLPF is reset */
  980. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
  981. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF)) && (wait_loop_index != 0U))
  982. {
  983. wait_loop_index--;
  984. }
  985. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  986. {
  987. return HAL_TIMEOUT;
  988. }
  989. return HAL_OK;
  990. }
  991. /**
  992. * @brief Enter Stop 0 mode.
  993. * @note In Stop 0 mode, main and low voltage regulators are ON.
  994. * @note In Stop 0 mode, all I/O pins keep the same state as in Run mode.
  995. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  996. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  997. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  998. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  999. * only to the peripheral requesting it.
  1000. * SRAM1, SRAM2 and register contents are preserved.
  1001. * The BOR is available.
  1002. * @note When exiting Stop 0 mode by issuing an interrupt or a wakeup event,
  1003. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1004. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1005. * @note By keeping the internal regulator ON during Stop 0 mode, the consumption
  1006. * is higher although the startup time is reduced.
  1007. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1008. * This parameter can be one of the following values:
  1009. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1010. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1011. * @retval None
  1012. */
  1013. void HAL_PWREx_EnterSTOP0Mode(uint8_t STOPEntry)
  1014. {
  1015. /* Check the parameters */
  1016. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1017. /* Stop 0 mode with Main Regulator */
  1018. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP0);
  1019. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1020. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1021. /* Select Stop mode entry --------------------------------------------------*/
  1022. if(STOPEntry == PWR_STOPENTRY_WFI)
  1023. {
  1024. /* Request Wait For Interrupt */
  1025. __WFI();
  1026. }
  1027. else
  1028. {
  1029. /* Request Wait For Event */
  1030. __SEV();
  1031. __WFE();
  1032. __WFE();
  1033. }
  1034. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1035. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1036. }
  1037. /**
  1038. * @brief Enter Stop 1 mode.
  1039. * @note In Stop 1 mode, only low power voltage regulator is ON.
  1040. * @note In Stop 1 mode, all I/O pins keep the same state as in Run mode.
  1041. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  1042. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  1043. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  1044. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  1045. * only to the peripheral requesting it.
  1046. * SRAM1, SRAM2 and register contents are preserved.
  1047. * The BOR is available.
  1048. * @note When exiting Stop 1 mode by issuing an interrupt or a wakeup event,
  1049. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1050. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1051. * @note Due to low power mode, an additional startup delay is incurred when waking up from Stop 1 mode.
  1052. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1053. * This parameter can be one of the following values:
  1054. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1055. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1056. * @retval None
  1057. */
  1058. void HAL_PWREx_EnterSTOP1Mode(uint8_t STOPEntry)
  1059. {
  1060. /* Check the parameters */
  1061. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1062. /* Stop 1 mode with Low-Power Regulator */
  1063. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP1);
  1064. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1065. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1066. /* Select Stop mode entry --------------------------------------------------*/
  1067. if(STOPEntry == PWR_STOPENTRY_WFI)
  1068. {
  1069. /* Request Wait For Interrupt */
  1070. __WFI();
  1071. }
  1072. else
  1073. {
  1074. /* Request Wait For Event */
  1075. __SEV();
  1076. __WFE();
  1077. __WFE();
  1078. }
  1079. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1080. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1081. }
  1082. /**
  1083. * @brief Enter Stop 2 mode.
  1084. * @note In Stop 2 mode, only low power voltage regulator is ON.
  1085. * @note In Stop 2 mode, all I/O pins keep the same state as in Run mode.
  1086. * @note All clocks in the VCORE domain are stopped, the PLL, the MSI,
  1087. * the HSI and the HSE oscillators are disabled. Some peripherals with wakeup capability
  1088. * (LCD, LPTIM1, I2C3 and LPUART) can switch on the HSI to receive a frame, and switch off the HSI after
  1089. * receiving the frame if it is not a wakeup frame. In this case the HSI clock is propagated only
  1090. * to the peripheral requesting it.
  1091. * SRAM1, SRAM2 and register contents are preserved.
  1092. * The BOR is available.
  1093. * The voltage regulator is set in low-power mode but LPR bit must be cleared to enter stop 2 mode.
  1094. * Otherwise, Stop 1 mode is entered.
  1095. * @note When exiting Stop 2 mode by issuing an interrupt or a wakeup event,
  1096. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1097. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1098. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1099. * This parameter can be one of the following values:
  1100. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1101. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1102. * @retval None
  1103. */
  1104. void HAL_PWREx_EnterSTOP2Mode(uint8_t STOPEntry)
  1105. {
  1106. /* Check the parameter */
  1107. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1108. /* Set Stop mode 2 */
  1109. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP2);
  1110. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1111. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1112. /* Select Stop mode entry --------------------------------------------------*/
  1113. if(STOPEntry == PWR_STOPENTRY_WFI)
  1114. {
  1115. /* Request Wait For Interrupt */
  1116. __WFI();
  1117. }
  1118. else
  1119. {
  1120. /* Request Wait For Event */
  1121. __SEV();
  1122. __WFE();
  1123. __WFE();
  1124. }
  1125. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1126. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1127. }
  1128. /**
  1129. * @brief Enter Shutdown mode.
  1130. * @note In Shutdown mode, the PLL, the HSI, the MSI, the LSI and the HSE oscillators are switched
  1131. * off. The voltage regulator is disabled and Vcore domain is powered off.
  1132. * SRAM1, SRAM2 and registers contents are lost except for registers in the Backup domain.
  1133. * The BOR is not available.
  1134. * @note The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  1135. * @retval None
  1136. */
  1137. void HAL_PWREx_EnterSHUTDOWNMode(void)
  1138. {
  1139. /* Set Shutdown mode */
  1140. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_SHUTDOWN);
  1141. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1142. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1143. /* This option is used to ensure that store operations are completed */
  1144. #if defined ( __CC_ARM)
  1145. __force_stores();
  1146. #endif
  1147. /* Request Wait For Interrupt */
  1148. __WFI();
  1149. }
  1150. /**
  1151. * @brief This function handles the PWR PVD/PVMx interrupt request.
  1152. * @note This API should be called under the PVD_PVM_IRQHandler().
  1153. * @retval None
  1154. */
  1155. void HAL_PWREx_PVD_PVM_IRQHandler(void)
  1156. {
  1157. /* Check PWR exti flag */
  1158. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != 0x0U)
  1159. {
  1160. /* PWR PVD interrupt user callback */
  1161. HAL_PWR_PVDCallback();
  1162. /* Clear PVD exti pending bit */
  1163. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  1164. }
  1165. /* Next, successively check PVMx exti flags */
  1166. #if defined(PWR_CR2_PVME1)
  1167. if(__HAL_PWR_PVM1_EXTI_GET_FLAG() != 0x0U)
  1168. {
  1169. /* PWR PVM1 interrupt user callback */
  1170. HAL_PWREx_PVM1Callback();
  1171. /* Clear PVM1 exti pending bit */
  1172. __HAL_PWR_PVM1_EXTI_CLEAR_FLAG();
  1173. }
  1174. #endif /* PWR_CR2_PVME1 */
  1175. #if defined(PWR_CR2_PVME2)
  1176. if(__HAL_PWR_PVM2_EXTI_GET_FLAG() != 0x0U)
  1177. {
  1178. /* PWR PVM2 interrupt user callback */
  1179. HAL_PWREx_PVM2Callback();
  1180. /* Clear PVM2 exti pending bit */
  1181. __HAL_PWR_PVM2_EXTI_CLEAR_FLAG();
  1182. }
  1183. #endif /* PWR_CR2_PVME2 */
  1184. if(__HAL_PWR_PVM3_EXTI_GET_FLAG() != 0x0U)
  1185. {
  1186. /* PWR PVM3 interrupt user callback */
  1187. HAL_PWREx_PVM3Callback();
  1188. /* Clear PVM3 exti pending bit */
  1189. __HAL_PWR_PVM3_EXTI_CLEAR_FLAG();
  1190. }
  1191. if(__HAL_PWR_PVM4_EXTI_GET_FLAG() != 0x0U)
  1192. {
  1193. /* PWR PVM4 interrupt user callback */
  1194. HAL_PWREx_PVM4Callback();
  1195. /* Clear PVM4 exti pending bit */
  1196. __HAL_PWR_PVM4_EXTI_CLEAR_FLAG();
  1197. }
  1198. }
  1199. #if defined(PWR_CR2_PVME1)
  1200. /**
  1201. * @brief PWR PVM1 interrupt callback
  1202. * @retval None
  1203. */
  1204. __weak void HAL_PWREx_PVM1Callback(void)
  1205. {
  1206. /* NOTE : This function should not be modified; when the callback is needed,
  1207. HAL_PWREx_PVM1Callback() API can be implemented in the user file
  1208. */
  1209. }
  1210. #endif /* PWR_CR2_PVME1 */
  1211. #if defined(PWR_CR2_PVME2)
  1212. /**
  1213. * @brief PWR PVM2 interrupt callback
  1214. * @retval None
  1215. */
  1216. __weak void HAL_PWREx_PVM2Callback(void)
  1217. {
  1218. /* NOTE : This function should not be modified; when the callback is needed,
  1219. HAL_PWREx_PVM2Callback() API can be implemented in the user file
  1220. */
  1221. }
  1222. #endif /* PWR_CR2_PVME2 */
  1223. /**
  1224. * @brief PWR PVM3 interrupt callback
  1225. * @retval None
  1226. */
  1227. __weak void HAL_PWREx_PVM3Callback(void)
  1228. {
  1229. /* NOTE : This function should not be modified; when the callback is needed,
  1230. HAL_PWREx_PVM3Callback() API can be implemented in the user file
  1231. */
  1232. }
  1233. /**
  1234. * @brief PWR PVM4 interrupt callback
  1235. * @retval None
  1236. */
  1237. __weak void HAL_PWREx_PVM4Callback(void)
  1238. {
  1239. /* NOTE : This function should not be modified; when the callback is needed,
  1240. HAL_PWREx_PVM4Callback() API can be implemented in the user file
  1241. */
  1242. }
  1243. /**
  1244. * @}
  1245. */
  1246. /**
  1247. * @}
  1248. */
  1249. #endif /* HAL_PWR_MODULE_ENABLED */
  1250. /**
  1251. * @}
  1252. */
  1253. /**
  1254. * @}
  1255. */
  1256. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/