You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1370 lines
45 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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) 2019 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 "stm32wbxx_hal.h"
  26. /** @addtogroup STM32WBxx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup PWREx
  30. * @{
  31. */
  32. #ifdef HAL_PWR_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @defgroup PWR_Extended_Private_Defines PWR Extended Private Defines
  36. * @{
  37. */
  38. #define PWR_PORTE_AVAILABLE_PINS (PWR_GPIO_BIT_4 | PWR_GPIO_BIT_3 | PWR_GPIO_BIT_2 | PWR_GPIO_BIT_1 | PWR_GPIO_BIT_0)
  39. #define PWR_PORTH_AVAILABLE_PINS (PWR_GPIO_BIT_3 | PWR_GPIO_BIT_1 | PWR_GPIO_BIT_0)
  40. /** @defgroup PWREx_TimeOut_Value PWR Extended Flag Setting Time Out Value
  41. * @{
  42. */
  43. #define PWR_FLAG_SETTING_DELAY_US 50U /*!< Time out value for REGLPF and VOSF flags setting */
  44. /**
  45. * @}
  46. */
  47. /**
  48. * @}
  49. */
  50. /* Private macro -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private function prototypes -----------------------------------------------*/
  53. /* Exported functions --------------------------------------------------------*/
  54. /** @addtogroup PWREx_Exported_Functions PWR Extended Exported Functions
  55. * @{
  56. */
  57. /** @addtogroup PWREx_Exported_Functions_Group1 Extended Peripheral Control functions
  58. * @brief Extended Peripheral Control functions
  59. *
  60. @verbatim
  61. ===============================================================================
  62. ##### Extended Peripheral Initialization and de-initialization functions #####
  63. ===============================================================================
  64. [..]
  65. @endverbatim
  66. * @{
  67. */
  68. #if defined(PWR_CR1_VOS)
  69. /**
  70. * @brief Return Voltage Scaling Range.
  71. * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_RANGE1 or PWR_REGULATOR_VOLTAGE_RANGE2)
  72. */
  73. uint32_t HAL_PWREx_GetVoltageRange(void)
  74. {
  75. return (PWR->CR1 & PWR_CR1_VOS);
  76. }
  77. /**
  78. * @brief Configure the main internal regulator output voltage.
  79. * @param VoltageScaling specifies the regulator output voltage to achieve
  80. * a tradeoff between performance and power consumption.
  81. * This parameter can be one of the following values:
  82. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1 Regulator voltage output range 1 mode,
  83. * typical output voltage at 1.2 V,
  84. * system frequency up to 64 MHz.
  85. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2 Regulator voltage output range 2 mode,
  86. * typical output voltage at 1.0 V,
  87. * system frequency up to 16 MHz.
  88. * @note When moving from Range 1 to Range 2, the system frequency must be decreased to
  89. * a value below 16 MHz before calling HAL_PWREx_ControlVoltageScaling() API.
  90. * When moving from Range 2 to Range 1, the system frequency can be increased to
  91. * a value up to 64 MHz after calling HAL_PWREx_ControlVoltageScaling() API.
  92. * @note When moving from Range 2 to Range 1, the API waits for VOSF flag to be
  93. * cleared before returning the status. If the flag is not cleared within
  94. * 50 microseconds, HAL_TIMEOUT status is reported.
  95. * @retval HAL Status
  96. */
  97. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  98. {
  99. uint32_t wait_loop_index;
  100. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  101. /* If Set Range 1 */
  102. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  103. {
  104. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE1)
  105. {
  106. /* Set Range 1 */
  107. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  108. /* Wait until VOSF is cleared */
  109. wait_loop_index = (PWR_FLAG_SETTING_DELAY_US * (SystemCoreClock / 1000000U));
  110. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  111. {
  112. wait_loop_index--;
  113. }
  114. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  115. {
  116. return HAL_TIMEOUT;
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE2)
  123. {
  124. /* Set Range 2 */
  125. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE2);
  126. /* No need to wait for VOSF to be cleared for this transition */
  127. }
  128. }
  129. return HAL_OK;
  130. }
  131. #endif
  132. /****************************************************************************/
  133. /**
  134. * @brief Enable battery charging.
  135. * When VDD is present, charge the external battery on VBAT thru an internal resistor.
  136. * @param ResistorSelection specifies the resistor impedance.
  137. * This parameter can be one of the following values:
  138. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_5 5 kOhms resistor
  139. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_1_5 1.5 kOhms resistor
  140. * @retval None
  141. */
  142. void HAL_PWREx_EnableBatteryCharging(uint32_t ResistorSelection)
  143. {
  144. assert_param(IS_PWR_BATTERY_RESISTOR_SELECT(ResistorSelection));
  145. /* Specify resistor selection */
  146. MODIFY_REG(PWR->CR4, PWR_CR4_VBRS, ResistorSelection);
  147. /* Enable battery charging */
  148. SET_BIT(PWR->CR4, PWR_CR4_VBE);
  149. }
  150. /**
  151. * @brief Disable battery charging.
  152. * @retval None
  153. */
  154. void HAL_PWREx_DisableBatteryCharging(void)
  155. {
  156. CLEAR_BIT(PWR->CR4, PWR_CR4_VBE);
  157. }
  158. /****************************************************************************/
  159. #if defined(PWR_CR2_PVME1)
  160. /**
  161. * @brief Enable VDDUSB supply.
  162. * @note Remove VDDUSB electrical and logical isolation, once VDDUSB supply is present.
  163. * @retval None
  164. */
  165. void HAL_PWREx_EnableVddUSB(void)
  166. {
  167. SET_BIT(PWR->CR2, PWR_CR2_USV);
  168. }
  169. /**
  170. * @brief Disable VDDUSB supply.
  171. * @retval None
  172. */
  173. void HAL_PWREx_DisableVddUSB(void)
  174. {
  175. CLEAR_BIT(PWR->CR2, PWR_CR2_USV);
  176. }
  177. #endif
  178. /****************************************************************************/
  179. /**
  180. * @brief Enable Internal Wake-up Line.
  181. * @retval None
  182. */
  183. void HAL_PWREx_EnableInternalWakeUpLine(void)
  184. {
  185. SET_BIT(PWR->CR3, PWR_CR3_EIWUL);
  186. }
  187. /**
  188. * @brief Disable Internal Wake-up Line.
  189. * @retval None
  190. */
  191. void HAL_PWREx_DisableInternalWakeUpLine(void)
  192. {
  193. CLEAR_BIT(PWR->CR3, PWR_CR3_EIWUL);
  194. }
  195. #if defined(PWR_CR5_SMPSEN)
  196. /**
  197. * @brief Enable BORH and SMPS step down converter forced in bypass mode
  198. * interrupt for CPU1
  199. * @retval None
  200. */
  201. void HAL_PWREx_EnableBORH_SMPSBypassIT(void)
  202. {
  203. SET_BIT(PWR->CR3, PWR_CR3_EBORHSMPSFB);
  204. }
  205. /**
  206. * @brief Disable BORH and SMPS step down converter forced in bypass mode
  207. * interrupt for CPU1
  208. * @retval None
  209. */
  210. void HAL_PWREx_DisableBORH_SMPSBypassIT(void)
  211. {
  212. CLEAR_BIT(PWR->CR3, PWR_CR3_EBORHSMPSFB);
  213. }
  214. #endif
  215. /**
  216. * @brief Enable RF Phase interrupt.
  217. * @retval None
  218. */
  219. void HAL_PWREx_EnableRFPhaseIT(void)
  220. {
  221. SET_BIT(PWR->CR3, PWR_CR3_ECRPE_Msk);
  222. }
  223. /**
  224. * @brief Disable RF Phase interrupt.
  225. * @retval None
  226. */
  227. void HAL_PWREx_DisableRFPhaseIT(void)
  228. {
  229. CLEAR_BIT(PWR->CR3, PWR_CR3_ECRPE_Msk);
  230. }
  231. /**
  232. * @brief Enable BLE Activity interrupt.
  233. * @retval None
  234. */
  235. void HAL_PWREx_EnableBLEActivityIT(void)
  236. {
  237. SET_BIT(PWR->CR3, PWR_CR3_EBLEA);
  238. }
  239. /**
  240. * @brief Disable BLE Activity interrupt.
  241. * @retval None
  242. */
  243. void HAL_PWREx_DisableBLEActivityIT(void)
  244. {
  245. CLEAR_BIT(PWR->CR3, PWR_CR3_EBLEA);
  246. }
  247. #if defined(PWR_CR3_E802A)
  248. /**
  249. * @brief Enable 802.15.4 Activity interrupt.
  250. * @retval None
  251. */
  252. void HAL_PWREx_Enable802ActivityIT(void)
  253. {
  254. SET_BIT(PWR->CR3, PWR_CR3_E802A);
  255. }
  256. /**
  257. * @brief Disable 802.15.4 Activity interrupt.
  258. * @retval None
  259. */
  260. void HAL_PWREx_Disable802ActivityIT(void)
  261. {
  262. CLEAR_BIT(PWR->CR3, PWR_CR3_E802A);
  263. }
  264. #endif
  265. /**
  266. * @brief Enable CPU2 on-Hold interrupt.
  267. * @retval None
  268. */
  269. void HAL_PWREx_EnableHOLDC2IT(void)
  270. {
  271. SET_BIT(PWR->CR3, PWR_CR3_EC2H);
  272. }
  273. /**
  274. * @brief Disable CPU2 on-Hold interrupt.
  275. * @retval None
  276. */
  277. void HAL_PWREx_DisableHOLDC2IT(void)
  278. {
  279. CLEAR_BIT(PWR->CR3, PWR_CR3_EC2H);
  280. }
  281. /****************************************************************************/
  282. /**
  283. * @brief Enable GPIO pull-up state in Standby and Shutdown modes.
  284. * @note Set the relevant PUy bits of PWR_PUCRx register to configure the I/O in
  285. * pull-up state in Standby and Shutdown modes.
  286. * @note This state is effective in Standby and Shutdown modes only if APC bit
  287. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  288. * @note The configuration is lost when exiting the Shutdown mode due to the
  289. * power-on reset, maintained when exiting the Standby mode.
  290. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  291. * PDy bit of PWR_PDCRx register is cleared unless it is reserved.
  292. * @note Even if a PUy bit to set is reserved, the other PUy bits entered as input
  293. * parameter at the same time are set.
  294. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  295. * to select the GPIO peripheral.
  296. * @param GPIONumber Specify the I/O pins numbers.
  297. * This parameter can be one of the following values:
  298. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for PORTH where less
  299. * I/O pins are available) or the logical OR of several of them to set
  300. * several bits for a given port in a single API call.
  301. * @retval HAL Status
  302. */
  303. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  304. {
  305. HAL_StatusTypeDef status = HAL_OK;
  306. assert_param(IS_PWR_GPIO(GPIO));
  307. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  308. switch (GPIO)
  309. {
  310. case PWR_GPIO_A:
  311. SET_BIT(PWR->PUCRA, GPIONumber);
  312. CLEAR_BIT(PWR->PDCRA, GPIONumber);
  313. break;
  314. case PWR_GPIO_B:
  315. SET_BIT(PWR->PUCRB, GPIONumber);
  316. CLEAR_BIT(PWR->PDCRB, GPIONumber);
  317. break;
  318. case PWR_GPIO_C:
  319. SET_BIT(PWR->PUCRC, GPIONumber);
  320. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  321. break;
  322. #if defined(GPIOD)
  323. case PWR_GPIO_D:
  324. SET_BIT(PWR->PUCRD, GPIONumber);
  325. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  326. break;
  327. #endif
  328. case PWR_GPIO_E:
  329. SET_BIT(PWR->PUCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  330. CLEAR_BIT(PWR->PDCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  331. break;
  332. case PWR_GPIO_H:
  333. SET_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  334. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  335. break;
  336. default:
  337. status = HAL_ERROR;
  338. break;
  339. }
  340. return status;
  341. }
  342. /**
  343. * @brief Disable GPIO pull-up state in Standby mode and Shutdown modes.
  344. * @note Reset the relevant PUy bits of PWR_PUCRx register used to configure the I/O
  345. * in pull-up state in Standby and Shutdown modes.
  346. * @note Even if a PUy bit to reset is reserved, the other PUy bits entered as input
  347. * parameter at the same time are reset.
  348. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  349. * to select the GPIO peripheral.
  350. * @param GPIONumber Specify the I/O pins numbers.
  351. * This parameter can be one of the following values:
  352. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for PORTH where less
  353. * I/O pins are available) or the logical OR of several of them to reset
  354. * several bits for a given port in a single API call.
  355. * @retval HAL Status
  356. */
  357. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  358. {
  359. HAL_StatusTypeDef status = HAL_OK;
  360. assert_param(IS_PWR_GPIO(GPIO));
  361. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  362. switch (GPIO)
  363. {
  364. case PWR_GPIO_A:
  365. CLEAR_BIT(PWR->PUCRA, GPIONumber);
  366. break;
  367. case PWR_GPIO_B:
  368. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  369. break;
  370. case PWR_GPIO_C:
  371. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  372. break;
  373. #if defined(GPIOD)
  374. case PWR_GPIO_D:
  375. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  376. break;
  377. #endif
  378. case PWR_GPIO_E:
  379. CLEAR_BIT(PWR->PUCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  380. break;
  381. case PWR_GPIO_H:
  382. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  383. break;
  384. default:
  385. status = HAL_ERROR;
  386. break;
  387. }
  388. return status;
  389. }
  390. /**
  391. * @brief Enable GPIO pull-down state in Standby and Shutdown modes.
  392. * @note Set the relevant PDy bits of PWR_PDCRx register to configure the I/O in
  393. * pull-down state in Standby and Shutdown modes.
  394. * @note This state is effective in Standby and Shutdown modes only if APC bit
  395. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  396. * @note The configuration is lost when exiting the Shutdown mode due to the
  397. * power-on reset, maintained when exiting the Standby mode.
  398. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  399. * PUy bit of PWR_PUCRx register is cleared unless it is reserved.
  400. * @note Even if a PDy bit to set is reserved, the other PDy bits entered as input
  401. * parameter at the same time are set.
  402. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  403. * to select the GPIO peripheral.
  404. * @param GPIONumber Specify the I/O pins numbers.
  405. * This parameter can be one of the following values:
  406. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for PORTH where less
  407. * I/O pins are available) or the logical OR of several of them to set
  408. * several bits for a given port in a single API call.
  409. * @retval HAL Status
  410. */
  411. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  412. {
  413. HAL_StatusTypeDef status = HAL_OK;
  414. assert_param(IS_PWR_GPIO(GPIO));
  415. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  416. switch (GPIO)
  417. {
  418. case PWR_GPIO_A:
  419. SET_BIT(PWR->PDCRA, GPIONumber);
  420. CLEAR_BIT(PWR->PUCRA, GPIONumber);
  421. break;
  422. case PWR_GPIO_B:
  423. SET_BIT(PWR->PDCRB, GPIONumber);
  424. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  425. break;
  426. case PWR_GPIO_C:
  427. SET_BIT(PWR->PDCRC, GPIONumber);
  428. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  429. break;
  430. #if defined(GPIOD)
  431. case PWR_GPIO_D:
  432. SET_BIT(PWR->PDCRD, GPIONumber);
  433. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  434. break;
  435. #endif
  436. case PWR_GPIO_E:
  437. SET_BIT(PWR->PDCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  438. CLEAR_BIT(PWR->PUCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  439. break;
  440. case PWR_GPIO_H:
  441. SET_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  442. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  443. break;
  444. default:
  445. status = HAL_ERROR;
  446. break;
  447. }
  448. return status;
  449. }
  450. /**
  451. * @brief Disable GPIO pull-down state in Standby and Shutdown modes.
  452. * @note Reset the relevant PDy bits of PWR_PDCRx register used to configure the I/O
  453. * in pull-down state in Standby and Shutdown modes.
  454. * @note Even if a PDy bit to reset is reserved, the other PDy bits entered as input
  455. * parameter at the same time are reset.
  456. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  457. * to select the GPIO peripheral.
  458. * @param GPIONumber Specify the I/O pins numbers.
  459. * This parameter can be one of the following values:
  460. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for PORTH where less
  461. * I/O pins are available) or the logical OR of several of them to reset
  462. * several bits for a given port in a single API call.
  463. * @retval HAL Status
  464. */
  465. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  466. {
  467. HAL_StatusTypeDef status = HAL_OK;
  468. assert_param(IS_PWR_GPIO(GPIO));
  469. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  470. switch (GPIO)
  471. {
  472. case PWR_GPIO_A:
  473. CLEAR_BIT(PWR->PDCRA, GPIONumber);
  474. break;
  475. case PWR_GPIO_B:
  476. CLEAR_BIT(PWR->PDCRB, GPIONumber);
  477. break;
  478. case PWR_GPIO_C:
  479. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  480. break;
  481. #if defined(GPIOD)
  482. case PWR_GPIO_D:
  483. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  484. break;
  485. #endif
  486. case PWR_GPIO_E:
  487. CLEAR_BIT(PWR->PDCRE, (GPIONumber & PWR_PORTE_AVAILABLE_PINS));
  488. break;
  489. case PWR_GPIO_H:
  490. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  491. break;
  492. default:
  493. status = HAL_ERROR;
  494. break;
  495. }
  496. return status;
  497. }
  498. /**
  499. * @brief Enable pull-up and pull-down configuration.
  500. * @note When APC bit is set, the I/O pull-up and pull-down configurations defined in
  501. * PWR_PUCRx and PWR_PDCRx registers are applied in Standby and Shutdown modes.
  502. * @note Pull-up set by PUy bit of PWR_PUCRx register is not activated if the corresponding
  503. * PDy bit of PWR_PDCRx register is also set (pull-down configuration priority is higher).
  504. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() API's ensure there
  505. * is no conflict when setting PUy or PDy bit.
  506. * @retval None
  507. */
  508. void HAL_PWREx_EnablePullUpPullDownConfig(void)
  509. {
  510. SET_BIT(PWR->CR3, PWR_CR3_APC);
  511. }
  512. /**
  513. * @brief Disable pull-up and pull-down configuration.
  514. * @note When APC bit is cleared, the I/O pull-up and pull-down configurations defined in
  515. * PWR_PUCRx and PWR_PDCRx registers are not applied in Standby and Shutdown modes.
  516. * @retval None
  517. */
  518. void HAL_PWREx_DisablePullUpPullDownConfig(void)
  519. {
  520. CLEAR_BIT(PWR->CR3, PWR_CR3_APC);
  521. }
  522. /****************************************************************************/
  523. #if defined(PWR_CR5_SMPSEN)
  524. /**
  525. * @brief Set BOR configuration
  526. * @param BORConfiguration This parameter can be one of the following values:
  527. * @arg @ref PWR_BOR_SYSTEM_RESET
  528. * @arg @ref PWR_BOR_SMPS_FORCE_BYPASS
  529. */
  530. void HAL_PWREx_SetBORConfig(uint32_t BORConfiguration)
  531. {
  532. LL_PWR_SetBORConfig(BORConfiguration);
  533. }
  534. /**
  535. * @brief Get BOR configuration
  536. * @retval Returned value can be one of the following values:
  537. * @arg @ref PWR_BOR_SYSTEM_RESET
  538. * @arg @ref PWR_BOR_SMPS_FORCE_BYPASS
  539. */
  540. uint32_t HAL_PWREx_GetBORConfig(void)
  541. {
  542. return LL_PWR_GetBORConfig();
  543. }
  544. #endif
  545. /****************************************************************************/
  546. /**
  547. * @brief Hold the CPU and their allocated peripherals after reset or wakeup from stop or standby.
  548. * @param CPU: Specifies the core to be held.
  549. * This parameter can be one of the following values:
  550. * @arg PWR_CORE_CPU2: Hold CPU2 and set CPU1 as master.
  551. * @note Hold CPU2 with CPU1 as master by default.
  552. * @retval None
  553. */
  554. void HAL_PWREx_HoldCore(uint32_t CPU)
  555. {
  556. /* Check the parameters */
  557. assert_param(IS_PWR_CORE_HOLD_RELEASE(CPU));
  558. LL_PWR_DisableBootC2();
  559. }
  560. /**
  561. * @brief Release Cortex CPU2 and allocated peripherals after reset or wakeup from stop or standby.
  562. * @param CPU: Specifies the core to be released.
  563. * This parameter can be one of the following values:
  564. * @arg PWR_CORE_CPU2: Release the CPU2 from holding.
  565. * @retval None
  566. */
  567. void HAL_PWREx_ReleaseCore(uint32_t CPU)
  568. {
  569. /* Check the parameters */
  570. assert_param(IS_PWR_CORE_HOLD_RELEASE(CPU));
  571. LL_PWR_EnableBootC2();
  572. }
  573. /****************************************************************************/
  574. /**
  575. * @brief Enable SRAM2a content retention in Standby mode.
  576. * @note When RRS bit is set, SRAM2a is powered by the low-power regulator in
  577. * Standby mode and its content is kept.
  578. * @note On devices STM32WB15xx, STM32WB10xx, retention is extended
  579. * to SRAM1, SRAM2a and SRAM2b.
  580. * @retval None
  581. */
  582. void HAL_PWREx_EnableSRAMRetention(void)
  583. {
  584. LL_PWR_EnableSRAM2Retention();
  585. }
  586. /**
  587. * @brief Disable SRAM2a content retention in Standby mode.
  588. * @note When RRS bit is reset, SRAM2a is powered off in Standby mode
  589. * and its content is lost.
  590. * @note On devices STM32WB15xx, STM32WB10xx, retention is extended
  591. * to SRAM1, SRAM2a and SRAM2b.
  592. * @retval None
  593. */
  594. void HAL_PWREx_DisableSRAMRetention(void)
  595. {
  596. LL_PWR_DisableSRAM2Retention();
  597. }
  598. /****************************************************************************/
  599. /**
  600. * @brief Enable Flash Power Down.
  601. * @note This API allows to enable flash power down capabilities in low power
  602. * run and low power sleep modes.
  603. * @param PowerMode this can be a combination of following values:
  604. * @arg @ref PWR_FLASHPD_LPRUN
  605. * @arg @ref PWR_FLASHPD_LPSLEEP
  606. * @retval None
  607. */
  608. void HAL_PWREx_EnableFlashPowerDown(uint32_t PowerMode)
  609. {
  610. assert_param(IS_PWR_FLASH_POWERDOWN(PowerMode));
  611. if((PowerMode & PWR_FLASHPD_LPRUN) != 0U)
  612. {
  613. /* Unlock bit FPDR */
  614. WRITE_REG(PWR->CR1, 0x0000C1B0UL);
  615. }
  616. /* Set flash power down mode */
  617. SET_BIT(PWR->CR1, PowerMode);
  618. }
  619. /**
  620. * @brief Disable Flash Power Down.
  621. * @note This API allows to disable flash power down capabilities in low power
  622. * run and low power sleep modes.
  623. * @param PowerMode this can be a combination of following values:
  624. * @arg @ref PWR_FLASHPD_LPRUN
  625. * @arg @ref PWR_FLASHPD_LPSLEEP
  626. * @retval None
  627. */
  628. void HAL_PWREx_DisableFlashPowerDown(uint32_t PowerMode)
  629. {
  630. assert_param(IS_PWR_FLASH_POWERDOWN(PowerMode));
  631. /* Set flash power down mode */
  632. CLEAR_BIT(PWR->CR1, PowerMode);
  633. }
  634. /****************************************************************************/
  635. #if defined(PWR_CR2_PVME1)
  636. /**
  637. * @brief Enable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  638. * @retval None
  639. */
  640. void HAL_PWREx_EnablePVM1(void)
  641. {
  642. SET_BIT(PWR->CR2, PWR_PVM_1);
  643. }
  644. /**
  645. * @brief Disable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  646. * @retval None
  647. */
  648. void HAL_PWREx_DisablePVM1(void)
  649. {
  650. CLEAR_BIT(PWR->CR2, PWR_PVM_1);
  651. }
  652. #endif
  653. /**
  654. * @brief Enable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  655. * @retval None
  656. */
  657. void HAL_PWREx_EnablePVM3(void)
  658. {
  659. SET_BIT(PWR->CR2, PWR_PVM_3);
  660. }
  661. /**
  662. * @brief Disable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  663. * @retval None
  664. */
  665. void HAL_PWREx_DisablePVM3(void)
  666. {
  667. CLEAR_BIT(PWR->CR2, PWR_PVM_3);
  668. }
  669. /**
  670. * @brief Configure the Peripheral Voltage Monitoring (PVM).
  671. * @param sConfigPVM pointer to a PWR_PVMTypeDef structure that contains the
  672. * PVM configuration information.
  673. * @note The API configures a single PVM according to the information contained
  674. * in the input structure. To configure several PVMs, the API must be singly
  675. * called for each PVM used.
  676. * @note Refer to the electrical characteristics of your device datasheet for
  677. * more details about the voltage thresholds corresponding to each
  678. * detection level and to each monitored supply.
  679. * @retval HAL status
  680. */
  681. HAL_StatusTypeDef HAL_PWREx_ConfigPVM(PWR_PVMTypeDef *sConfigPVM)
  682. {
  683. HAL_StatusTypeDef status = HAL_OK;
  684. /* Check the parameters */
  685. assert_param(IS_PWR_PVM_TYPE(sConfigPVM->PVMType));
  686. assert_param(IS_PWR_PVM_MODE(sConfigPVM->Mode));
  687. /* Configure EXTI 31 and 33 interrupts if so required:
  688. scan thru PVMType to detect which PVMx is set and
  689. configure the corresponding EXTI line accordingly. */
  690. switch (sConfigPVM->PVMType)
  691. {
  692. #if defined(PWR_CR2_PVME1)
  693. case PWR_PVM_1:
  694. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  695. __HAL_PWR_PVM1_EXTI_DISABLE_EVENT();
  696. __HAL_PWR_PVM1_EXTI_DISABLE_IT();
  697. __HAL_PWR_PVM1_EXTI_DISABLE_FALLING_EDGE();
  698. __HAL_PWR_PVM1_EXTI_DISABLE_RISING_EDGE();
  699. /* Configure interrupt mode */
  700. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  701. {
  702. __HAL_PWR_PVM1_EXTI_ENABLE_IT();
  703. }
  704. /* Configure event mode */
  705. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  706. {
  707. __HAL_PWR_PVM1_EXTI_ENABLE_EVENT();
  708. }
  709. /* Configure the edge */
  710. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  711. {
  712. __HAL_PWR_PVM1_EXTI_ENABLE_RISING_EDGE();
  713. }
  714. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  715. {
  716. __HAL_PWR_PVM1_EXTI_ENABLE_FALLING_EDGE();
  717. }
  718. break;
  719. #endif
  720. case PWR_PVM_3:
  721. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  722. __HAL_PWR_PVM3_EXTI_DISABLE_EVENT();
  723. __HAL_PWR_PVM3_EXTI_DISABLE_IT();
  724. __HAL_PWR_PVM3_EXTI_DISABLE_FALLING_EDGE();
  725. __HAL_PWR_PVM3_EXTI_DISABLE_RISING_EDGE();
  726. /* Configure interrupt mode */
  727. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  728. {
  729. __HAL_PWR_PVM3_EXTI_ENABLE_IT();
  730. }
  731. /* Configure event mode */
  732. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  733. {
  734. __HAL_PWR_PVM3_EXTI_ENABLE_EVENT();
  735. }
  736. /* Configure the edge */
  737. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  738. {
  739. __HAL_PWR_PVM3_EXTI_ENABLE_RISING_EDGE();
  740. }
  741. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  742. {
  743. __HAL_PWR_PVM3_EXTI_ENABLE_FALLING_EDGE();
  744. }
  745. break;
  746. default:
  747. status = HAL_ERROR;
  748. break;
  749. }
  750. return status;
  751. }
  752. #if defined(PWR_CR5_SMPSEN)
  753. /**
  754. * @brief Configure the SMPS step down converter.
  755. * @note SMPS output voltage is calibrated in production,
  756. * calibration parameters are applied to the voltage level parameter
  757. * to reach the requested voltage value.
  758. * @param sConfigSMPS pointer to a PWR_SMPSTypeDef structure that contains the
  759. * SMPS configuration information.
  760. * @note To set and enable SMPS operating mode, refer to function
  761. * "HAL_PWREx_SMPS_SetMode()".
  762. * @retval HAL status
  763. */
  764. HAL_StatusTypeDef HAL_PWREx_ConfigSMPS(PWR_SMPSTypeDef *sConfigSMPS)
  765. {
  766. HAL_StatusTypeDef status = HAL_OK;
  767. /* Check the parameters */
  768. assert_param(IS_PWR_SMPS_STARTUP_CURRENT(sConfigSMPS->StartupCurrent));
  769. assert_param(IS_PWR_SMPS_OUTPUT_VOLTAGE(sConfigSMPS->OutputVoltage));
  770. __IO const uint32_t OutputVoltageLevel_calibration = (((*SMPS_VOLTAGE_CAL_ADDR) & SMPS_VOLTAGE_CAL) >> SMPS_VOLTAGE_CAL_POS); /* SMPS output voltage level calibrated in production */
  771. int32_t TrimmingSteps; /* Trimming steps between theorical output voltage and calibrated output voltage */
  772. int32_t OutputVoltageLevelTrimmed; /* SMPS output voltage level after calibration: trimming value added to required level */
  773. if(OutputVoltageLevel_calibration == 0UL)
  774. {
  775. /* Device with SMPS output voltage not calibrated in production: Apply output voltage value directly */
  776. /* Update register */
  777. MODIFY_REG(PWR->CR5, PWR_CR5_SMPSVOS, (sConfigSMPS->StartupCurrent | sConfigSMPS->OutputVoltage));
  778. }
  779. else
  780. {
  781. /* Device with SMPS output voltage calibrated in production: Apply output voltage value after correction by calibration value */
  782. TrimmingSteps = ((int32_t)OutputVoltageLevel_calibration - (int32_t)(LL_PWR_SMPS_OUTPUT_VOLTAGE_1V50 >> PWR_CR5_SMPSVOS_Pos));
  783. OutputVoltageLevelTrimmed = ((int32_t)((uint32_t)(sConfigSMPS->OutputVoltage >> PWR_CR5_SMPSVOS_Pos)) + (int32_t)TrimmingSteps);
  784. /* Clamp value to voltage trimming bitfield range */
  785. if(OutputVoltageLevelTrimmed < 0)
  786. {
  787. OutputVoltageLevelTrimmed = 0;
  788. status = HAL_ERROR;
  789. }
  790. else
  791. {
  792. if(OutputVoltageLevelTrimmed > (int32_t)PWR_CR5_SMPSVOS)
  793. {
  794. OutputVoltageLevelTrimmed = (int32_t)PWR_CR5_SMPSVOS;
  795. status = HAL_ERROR;
  796. }
  797. }
  798. /* Update register */
  799. MODIFY_REG(PWR->CR5, (PWR_CR5_SMPSSC | PWR_CR5_SMPSVOS), (sConfigSMPS->StartupCurrent | ((uint32_t) OutputVoltageLevelTrimmed)));
  800. }
  801. return status;
  802. }
  803. /**
  804. * @brief Set SMPS operating mode.
  805. * @param OperatingMode This parameter can be one of the following values:
  806. * @arg @ref PWR_SMPS_BYPASS
  807. * @arg @ref PWR_SMPS_STEP_DOWN (1)
  808. *
  809. * (1) SMPS operating mode step down or open depends on system low-power mode:
  810. * - step down mode if system low power mode is run, LP run or stop,
  811. * - open mode if system low power mode is Stop1, Stop2, Standby or Shutdown
  812. * @retval None
  813. */
  814. void HAL_PWREx_SMPS_SetMode(uint32_t OperatingMode)
  815. {
  816. MODIFY_REG(PWR->CR5, PWR_CR5_SMPSEN, (OperatingMode & PWR_SR2_SMPSF) << (PWR_CR5_SMPSEN_Pos - PWR_SR2_SMPSF_Pos));
  817. }
  818. /**
  819. * @brief Get SMPS effective operating mode
  820. * @note SMPS operating mode can be changed by hardware, therefore
  821. * requested operating mode can differ from effective low power mode.
  822. * - dependency on system low-power mode:
  823. * - step down mode if system low power mode is run, LP run or stop,
  824. * - open mode if system low power mode is Stop1, Stop2, Standby or Shutdown
  825. * - dependency on BOR level:
  826. * - bypass mode if supply voltage drops below BOR level
  827. * @note This functions check flags of SMPS operating modes step down
  828. * and bypass. If the SMPS is not among these 2 operating modes,
  829. * then it can be in mode off or open.
  830. * @retval Returned value can be one of the following values:
  831. * @arg @ref PWR_SMPS_BYPASS
  832. * @arg @ref PWR_SMPS_STEP_DOWN (1)
  833. *
  834. * (1) SMPS operating mode step down or open depends on system low-power mode:
  835. * - step down mode if system low power mode is run, LP run or stop,
  836. * - open mode if system low power mode is Stop1, Stop2, Standby or Shutdown
  837. */
  838. uint32_t HAL_PWREx_SMPS_GetEffectiveMode(void)
  839. {
  840. return (uint32_t)(READ_BIT(PWR->SR2, (PWR_SR2_SMPSF | PWR_SR2_SMPSBF)));
  841. }
  842. #endif
  843. /****************************************************************************/
  844. /**
  845. * @brief Enable the WakeUp PINx functionality.
  846. * @param WakeUpPinPolarity Specifies which Wake-Up pin to enable.
  847. * This parameter can be one of the following legacy values which set the default polarity
  848. * i.e. detection on high level (rising edge):
  849. * @arg @ref PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, PWR_WAKEUP_PIN4, PWR_WAKEUP_PIN5
  850. *
  851. * or one of the following value where the user can explicitly specify the enabled pin and
  852. * the chosen polarity:
  853. * @arg @ref PWR_WAKEUP_PIN1_HIGH or PWR_WAKEUP_PIN1_LOW
  854. * @arg @ref PWR_WAKEUP_PIN2_HIGH or PWR_WAKEUP_PIN2_LOW
  855. * @arg @ref PWR_WAKEUP_PIN3_HIGH or PWR_WAKEUP_PIN3_LOW
  856. * @arg @ref PWR_WAKEUP_PIN4_HIGH or PWR_WAKEUP_PIN4_LOW
  857. * @arg @ref PWR_WAKEUP_PIN5_HIGH or PWR_WAKEUP_PIN5_LOW
  858. * @param wakeupTarget Specifies the wake-up target
  859. * @arg @ref PWR_CORE_CPU1
  860. * @arg @ref PWR_CORE_CPU2
  861. * @note PWR_WAKEUP_PINx and PWR_WAKEUP_PINx_HIGH are equivalent.
  862. * @retval None
  863. */
  864. void HAL_PWREx_EnableWakeUpPin(uint32_t WakeUpPinPolarity, uint32_t wakeupTarget)
  865. {
  866. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinPolarity));
  867. /* Specifies the Wake-Up pin polarity for the event detection
  868. (rising or falling edge) */
  869. MODIFY_REG(PWR->CR4, (PWR_C2CR3_EWUP & WakeUpPinPolarity), (WakeUpPinPolarity >> PWR_WUP_POLARITY_SHIFT));
  870. /* Enable wake-up pin */
  871. if(PWR_CORE_CPU2 == wakeupTarget)
  872. {
  873. SET_BIT(PWR->C2CR3, (PWR_C2CR3_EWUP & WakeUpPinPolarity));
  874. }
  875. else
  876. {
  877. SET_BIT(PWR->CR3, (PWR_CR3_EWUP & WakeUpPinPolarity));
  878. }
  879. }
  880. /**
  881. * @brief Get the Wake-Up pin flag.
  882. * @param WakeUpFlag specifies the Wake-Up PIN flag to check.
  883. * This parameter can be one of the following values:
  884. * @arg PWR_FLAG_WUF1: A wakeup event was received from PA0.
  885. * @arg PWR_FLAG_WUF2: A wakeup event was received from PC13.
  886. * @arg PWR_FLAG_WUF3: A wakeup event was received from PC12.
  887. * @arg PWR_FLAG_WUF4: A wakeup event was received from PA2.
  888. * @arg PWR_FLAG_WUF5: A wakeup event was received from PC5.
  889. * @retval The Wake-Up pin flag.
  890. */
  891. uint32_t HAL_PWREx_GetWakeupFlag(uint32_t WakeUpFlag)
  892. {
  893. return (PWR->SR1 & (1UL << ((WakeUpFlag) & 31U)));
  894. }
  895. /**
  896. * @brief Clear the Wake-Up pin flag.
  897. * @param WakeUpFlag specifies the Wake-Up PIN flag to clear.
  898. * This parameter can be one of the following values:
  899. * @arg PWR_FLAG_WUF1: A wakeup event was received from PA0.
  900. * @arg PWR_FLAG_WUF2: A wakeup event was received from PC13.
  901. * @arg PWR_FLAG_WUF3: A wakeup event was received from PC12.
  902. * @arg PWR_FLAG_WUF4: A wakeup event was received from PA2.
  903. * @arg PWR_FLAG_WUF5: A wakeup event was received from PC5.
  904. * @retval HAL status.
  905. */
  906. HAL_StatusTypeDef HAL_PWREx_ClearWakeupFlag(uint32_t WakeUpFlag)
  907. {
  908. PWR->SCR = (1UL << ((WakeUpFlag) & 31U));
  909. if((PWR->SR1 & (1UL << ((WakeUpFlag) & 31U))) != 0U)
  910. {
  911. return HAL_ERROR;
  912. }
  913. return HAL_OK;
  914. }
  915. /****************************************************************************/
  916. /**
  917. * @brief Enter Low-power Run mode
  918. * @note In Low-power Run mode, all I/O pins keep the same state as in Run mode.
  919. * @note When Regulator is set to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  920. * Flash in power-down mode in setting the RUN_PD bit in FLASH_ACR register.
  921. * Additionally, the clock frequency must be reduced below 2 MHz.
  922. * Setting RUN_PD in FLASH_ACR then appropriately reducing the clock frequency must
  923. * be done before calling HAL_PWREx_EnableLowPowerRunMode() API.
  924. * @retval None
  925. */
  926. void HAL_PWREx_EnableLowPowerRunMode(void)
  927. {
  928. /* Set Regulator parameter */
  929. SET_BIT(PWR->CR1, PWR_CR1_LPR);
  930. }
  931. /**
  932. * @brief Exit Low-power Run mode.
  933. * @note Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
  934. * REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
  935. * returns HAL_TIMEOUT status). The system clock frequency can then be
  936. * increased above 2 MHz.
  937. * @retval HAL Status
  938. */
  939. HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
  940. {
  941. uint32_t wait_loop_index;
  942. /* Clear LPR bit */
  943. CLEAR_BIT(PWR->CR1, PWR_CR1_LPR);
  944. /* Wait until REGLPF is reset */
  945. wait_loop_index = (PWR_FLAG_SETTING_DELAY_US * (SystemCoreClock / 1000000U));
  946. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF)) && (wait_loop_index != 0U))
  947. {
  948. wait_loop_index--;
  949. }
  950. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  951. {
  952. return HAL_TIMEOUT;
  953. }
  954. return HAL_OK;
  955. }
  956. /****************************************************************************/
  957. /**
  958. * @brief Enter Stop 0 mode.
  959. * @note In Stop 0 mode, main and low voltage regulators are ON.
  960. * @note In Stop 0 mode, all I/O pins keep the same state as in Run mode.
  961. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  962. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  963. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  964. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  965. * only to the peripheral requesting it.
  966. * SRAM1, SRAM2 and register contents are preserved.
  967. * The BOR is available.
  968. * @note When exiting Stop 0 mode by issuing an interrupt or a wakeup event,
  969. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  970. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  971. * @note By keeping the internal regulator ON during Stop 0 mode, the consumption
  972. * is higher although the startup time is reduced.
  973. * @note Case of Stop0 mode with SMPS: Before entering Stop 0 mode with SMPS Step Down converter enabled,
  974. * the HSI16 must be kept on by enabling HSI kernel clock (set HSIKERON register bit).
  975. * @note According to system power policy, system entering in Stop mode
  976. * is depending on other CPU power mode.
  977. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  978. * This parameter can be one of the following values:
  979. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  980. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  981. * @retval None
  982. */
  983. void HAL_PWREx_EnterSTOP0Mode(uint8_t STOPEntry)
  984. {
  985. /* Check the parameters */
  986. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  987. /* Stop 0 mode with Main Regulator */
  988. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_STOP0);
  989. /* Set SLEEPDEEP bit of Cortex System Control Register */
  990. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  991. /* Select Stop mode entry --------------------------------------------------*/
  992. if(STOPEntry == PWR_STOPENTRY_WFI)
  993. {
  994. /* Request Wait For Interrupt */
  995. __WFI();
  996. }
  997. else
  998. {
  999. /* Request Wait For Event */
  1000. __SEV();
  1001. __WFE();
  1002. __WFE();
  1003. }
  1004. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1005. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1006. }
  1007. /**
  1008. * @brief Enter Stop 1 mode.
  1009. * @note In Stop 1 mode, only low power voltage regulator is ON.
  1010. * @note In Stop 1 mode, all I/O pins keep the same state as in Run mode.
  1011. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  1012. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  1013. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  1014. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  1015. * only to the peripheral requesting it.
  1016. * SRAM1, SRAM2 and register contents are preserved.
  1017. * The BOR is available.
  1018. * @note When exiting Stop 1 mode by issuing an interrupt or a wakeup event,
  1019. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1020. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1021. * @note Due to low power mode, an additional startup delay is incurred when waking up from Stop 1 mode.
  1022. * @note According to system power policy, system entering in Stop mode
  1023. * is depending on other CPU power mode.
  1024. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1025. * This parameter can be one of the following values:
  1026. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1027. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1028. * @retval None
  1029. */
  1030. void HAL_PWREx_EnterSTOP1Mode(uint8_t STOPEntry)
  1031. {
  1032. /* Check the parameters */
  1033. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1034. /* Stop 1 mode with Low-Power Regulator */
  1035. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_STOP1);
  1036. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1037. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1038. /* Select Stop mode entry --------------------------------------------------*/
  1039. if(STOPEntry == PWR_STOPENTRY_WFI)
  1040. {
  1041. /* Request Wait For Interrupt */
  1042. __WFI();
  1043. }
  1044. else
  1045. {
  1046. /* Request Wait For Event */
  1047. __SEV();
  1048. __WFE();
  1049. __WFE();
  1050. }
  1051. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1052. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1053. }
  1054. #if defined(PWR_SUPPORT_STOP2)
  1055. /**
  1056. * @brief Enter Stop 2 mode.
  1057. * @note In Stop 2 mode, only low power voltage regulator is ON.
  1058. * @note In Stop 2 mode, all I/O pins keep the same state as in Run mode.
  1059. * @note All clocks in the VCORE domain are stopped, the PLL, the MSI,
  1060. * the HSI and the HSE oscillators are disabled. Some peripherals with wakeup capability
  1061. * (LCD, LPTIM1, I2C3 and LPUART) can switch on the HSI to receive a frame, and switch off the HSI after
  1062. * receiving the frame if it is not a wakeup frame. In this case the HSI clock is propagated only
  1063. * to the peripheral requesting it.
  1064. * SRAM1, SRAM2 and register contents are preserved.
  1065. * The BOR is available.
  1066. * The voltage regulator is set in low-power mode but LPR bit must be cleared to enter stop 2 mode.
  1067. * Otherwise, Stop 1 mode is entered.
  1068. * @note When exiting Stop 2 mode by issuing an interrupt or a wakeup event,
  1069. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1070. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1071. * @note Case of Stop2 mode and debugger probe attached: a workaround should be applied.
  1072. * Issue specified in "ES0394 - STM32WB55Cx/Rx/Vx device errata":
  1073. * 2.2.9 Incomplete Stop 2 mode entry after a wakeup from debug upon EXTI line 48 event
  1074. * "With the JTAG debugger enabled on GPIO pins and after a wakeup from debug triggered by an event on EXTI
  1075. * line 48 (CDBGPWRUPREQ), the device may enter in a state in which attempts to enter Stop 2 mode are not fully
  1076. * effective ..."
  1077. * Workaround implementation example using LL driver:
  1078. * LL_EXTI_DisableIT_32_63(LL_EXTI_LINE_48);
  1079. * LL_C2_EXTI_DisableIT_32_63(LL_EXTI_LINE_48);
  1080. * @note According to system power policy, system entering in Stop mode
  1081. * is depending on other CPU power mode.
  1082. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1083. * This parameter can be one of the following values:
  1084. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1085. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1086. * @retval None
  1087. */
  1088. void HAL_PWREx_EnterSTOP2Mode(uint8_t STOPEntry)
  1089. {
  1090. /* Check the parameter */
  1091. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1092. /* Set Stop mode 2 */
  1093. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_STOP2);
  1094. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1095. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1096. /* Select Stop mode entry --------------------------------------------------*/
  1097. if(STOPEntry == PWR_STOPENTRY_WFI)
  1098. {
  1099. /* Request Wait For Interrupt */
  1100. __WFI();
  1101. }
  1102. else
  1103. {
  1104. /* Request Wait For Event */
  1105. __SEV();
  1106. __WFE();
  1107. __WFE();
  1108. }
  1109. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1110. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1111. }
  1112. #endif
  1113. /**
  1114. * @brief Enter Shutdown mode.
  1115. * @note In Shutdown mode, the PLL, the HSI, the MSI, the LSI and the HSE oscillators are switched
  1116. * off. The voltage regulator is disabled and Vcore domain is powered off.
  1117. * SRAM1, SRAM2, BKRAM and registers contents are lost except for registers in the Backup domain.
  1118. * The BOR is not available.
  1119. * @note The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  1120. * @note According to system power policy, system entering in Shutdown mode
  1121. * is depending on other CPU power mode.
  1122. * @retval None
  1123. */
  1124. void HAL_PWREx_EnterSHUTDOWNMode(void)
  1125. {
  1126. /* Set Shutdown mode */
  1127. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_SHUTDOWN);
  1128. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1129. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1130. /* This option is used to ensure that store operations are completed */
  1131. #if defined ( __CC_ARM)
  1132. __force_stores();
  1133. #endif
  1134. /* Request Wait For Interrupt */
  1135. __WFI();
  1136. /* Following code is executed after wake up if system didn't go to SHUTDOWN
  1137. * or STANDBY mode according to power policy */
  1138. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1139. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1140. }
  1141. /**
  1142. * @brief This function handles the PWR PVD/PVMx interrupt request.
  1143. * @note This API should be called under the PVD_PVM_IRQHandler().
  1144. * @retval None
  1145. */
  1146. void HAL_PWREx_PVD_PVM_IRQHandler(void)
  1147. {
  1148. /* Check PWR exti flag */
  1149. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != 0U)
  1150. {
  1151. /* PWR PVD interrupt user callback */
  1152. HAL_PWR_PVDCallback();
  1153. /* Clear PVD exti pending bit */
  1154. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  1155. }
  1156. #if defined(PWR_CR2_PVME1)
  1157. /* Next, successively check PVMx exti flags */
  1158. if(__HAL_PWR_PVM1_EXTI_GET_FLAG() != 0U)
  1159. {
  1160. /* PWR PVM1 interrupt user callback */
  1161. HAL_PWREx_PVM1Callback();
  1162. /* Clear PVM1 exti pending bit */
  1163. __HAL_PWR_PVM1_EXTI_CLEAR_FLAG();
  1164. }
  1165. #endif
  1166. if(__HAL_PWR_PVM3_EXTI_GET_FLAG() != 0U)
  1167. {
  1168. /* PWR PVM3 interrupt user callback */
  1169. HAL_PWREx_PVM3Callback();
  1170. /* Clear PVM3 exti pending bit */
  1171. __HAL_PWR_PVM3_EXTI_CLEAR_FLAG();
  1172. }
  1173. }
  1174. #if defined(PWR_CR2_PVME1)
  1175. /**
  1176. * @brief PWR PVM1 interrupt callback
  1177. * @retval None
  1178. */
  1179. __weak void HAL_PWREx_PVM1Callback(void)
  1180. {
  1181. /* NOTE : This function should not be modified; when the callback is needed,
  1182. HAL_PWREx_PVM1Callback() API can be implemented in the user file
  1183. */
  1184. }
  1185. #endif
  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. * @}
  1198. */
  1199. /**
  1200. * @}
  1201. */
  1202. #endif /* HAL_PWR_MODULE_ENABLED */
  1203. /**
  1204. * @}
  1205. */
  1206. /**
  1207. * @}
  1208. */
  1209. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/