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.
 
 
 

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