stm32l4xx_hal_pwr_ex.c 45 KB

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