stm32h5xx_hal_pwr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_hal_pwr.c
  4. * @author MCD Application Team
  5. * @brief PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Power Controller (PWR) peripheral:
  8. * + Initialization/De-Initialization Functions.
  9. * + Peripheral Control Functions.
  10. * + PWR Attributes Functions.
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2023 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32h5xx_hal.h"
  26. /** @addtogroup STM32H5xx_HAL_Driver
  27. * @{
  28. */
  29. /** @defgroup PWR PWR
  30. * @brief PWR HAL module driver
  31. * @{
  32. */
  33. #if defined (HAL_PWR_MODULE_ENABLED)
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. /** @defgroup PWR_Private_Defines PWR Private Defines
  37. * @{
  38. */
  39. /** @defgroup PWR_PVD_Mode_Mask PWR PVD Mode Mask
  40. * @{
  41. */
  42. #define PVD_RISING_EDGE (0x01U) /*!< Mask for rising edge set as PVD
  43. trigger */
  44. #define PVD_FALLING_EDGE (0x02U) /*!< Mask for falling edge set as PVD
  45. trigger */
  46. #define PVD_MODE_IT (0x04U) /*!< Mask for interruption yielded by PVD
  47. threshold crossing */
  48. #define PVD_MODE_EVT (0x08U) /*!< Mask for event yielded by PVD threshold
  49. crossing */
  50. /**
  51. * @}
  52. */
  53. /**
  54. * @}
  55. */
  56. /* Private macro -------------------------------------------------------------*/
  57. /* Private variables ---------------------------------------------------------*/
  58. /* Private function prototypes -----------------------------------------------*/
  59. /* Exported functions --------------------------------------------------------*/
  60. /** @defgroup PWR_Exported_Functions PWR Exported Functions
  61. * @{
  62. */
  63. /** @defgroup PWR_Exported_Functions_Group1 Initialization and De-Initialization Functions
  64. * @brief Initialization and de-Initialization functions
  65. *
  66. @verbatim
  67. ===============================================================================
  68. ##### Initialization and De-Initialization Functions #####
  69. ===============================================================================
  70. [..]
  71. @endverbatim
  72. * @{
  73. */
  74. /**
  75. * @brief Deinitialize the HAL PWR peripheral registers to their default reset
  76. * values.
  77. * @note This functionality is not available in this product.
  78. * The prototype is kept just to maintain compatibility with other
  79. * products.
  80. * @retval None.
  81. */
  82. void HAL_PWR_DeInit(void)
  83. {
  84. }
  85. /**
  86. * @brief Enable access to the backup domain (RCC Backup domain control
  87. * register RCC_BDCR, RTC registers, TAMP registers, backup registers
  88. * and backup SRAM).
  89. * @note After a system reset, the backup domain is protected against
  90. * possible unwanted write accesses.
  91. * @retval None.
  92. */
  93. void HAL_PWR_EnableBkUpAccess(void)
  94. {
  95. SET_BIT(PWR->DBPCR, PWR_DBPCR_DBP);
  96. }
  97. /**
  98. * @brief Disable access to the backup domain (RCC Backup domain control
  99. * register RCC_BDCR, RTC registers, TAMP registers, backup registers
  100. * and backup SRAM).
  101. * @retval None
  102. */
  103. void HAL_PWR_DisableBkUpAccess(void)
  104. {
  105. CLEAR_BIT(PWR->DBPCR, PWR_DBPCR_DBP);
  106. }
  107. /**
  108. * @}
  109. */
  110. /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control Functions
  111. * @brief Low power modes configuration functions
  112. *
  113. @verbatim
  114. ===============================================================================
  115. ##### Peripheral Control functions #####
  116. ===============================================================================
  117. [..]
  118. @endverbatim
  119. * @{
  120. */
  121. /**
  122. * @brief Configure the voltage threshold detected by the Programmed Voltage
  123. * Detector (PVD).
  124. * @param sConfigPVD : Pointer to a PWR_PVDTypeDef structure that contains the
  125. * PVD configuration information (PVDLevel and EventMode).
  126. * @retval None.
  127. */
  128. HAL_StatusTypeDef HAL_PWR_ConfigPVD(const PWR_PVDTypeDef *sConfigPVD)
  129. {
  130. /* Check the parameters */
  131. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  132. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  133. /* Set PLS[3:1] bits according to PVDLevel value */
  134. MODIFY_REG(PWR->VMCR, PWR_VMCR_PLS, sConfigPVD->PVDLevel);
  135. /* Disable PVD Event/Interrupt */
  136. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  137. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  138. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
  139. __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  140. /* Configure the PVD in interrupt mode */
  141. if ((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  142. {
  143. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  144. }
  145. /* Configure the PVD in event mode */
  146. if ((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  147. {
  148. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  149. }
  150. /* Configure the PVD in rising edge */
  151. if ((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  152. {
  153. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  154. }
  155. /* Configure the PVD in falling edge */
  156. if ((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  157. {
  158. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  159. }
  160. return HAL_OK;
  161. }
  162. /**
  163. * @brief Enable the programmable voltage detector (PVD).
  164. * @retval None.
  165. */
  166. void HAL_PWR_EnablePVD(void)
  167. {
  168. SET_BIT(PWR->VMCR, PWR_VMCR_PVDEN);
  169. }
  170. /**
  171. * @brief Disable the programmable voltage detector (PVD).
  172. * @retval None.
  173. */
  174. void HAL_PWR_DisablePVD(void)
  175. {
  176. CLEAR_BIT(PWR->VMCR, PWR_VMCR_PVDEN);
  177. }
  178. /**
  179. * @brief Enable the WakeUp PINx functionality.
  180. * @param WakeUpPinPolarity : Specifies which Wake-Up pin to enable.
  181. * This parameter can be one of the following legacy values, which
  182. * sets the default (rising edge):
  183. * @arg PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3,PWR_WAKEUP_PIN4,
  184. * PWR_WAKEUP_PIN5, PWR_WAKEUP_PIN6, PWR_WAKEUP_PIN7.PWR_WAKEUP_PIN8.
  185. * or one of the following values where the user can explicitly states
  186. * the enabled pin and the chosen polarity:
  187. * @arg PWR_WAKEUP_PIN1_HIGH, PWR_WAKEUP_PIN1_LOW,
  188. * PWR_WAKEUP_PIN2_HIGH, PWR_WAKEUP_PIN2_LOW,
  189. * PWR_WAKEUP_PIN3_HIGH, PWR_WAKEUP_PIN3_LOW,
  190. * PWR_WAKEUP_PIN4_HIGH, PWR_WAKEUP_PIN4_LOW,
  191. * PWR_WAKEUP_PIN5_HIGH, PWR_WAKEUP_PIN5_LOW,
  192. * PWR_WAKEUP_PIN6_HIGH, PWR_WAKEUP_PIN6_LOW,
  193. * PWR_WAKEUP_PIN7_HIGH, PWR_WAKEUP_PIN7_LOW,
  194. * PWR_WAKEUP_PIN8_HIGH, PWR_WAKEUP_PIN8_LOW.
  195. * @note PWR_WAKEUP_PINx and PWR_WAKEUP_PINx_HIGH are equivalent.
  196. * @note The PWR_WAKEUP_PIN6_HIGH, PWR_WAKEUP_PIN6_LOW, PWR_WAKEUP_PIN7_HIGH, PWR_WAKEUP_PIN7_LOW,
  197. * PWR_WAKEUP_PIN8_HIGH and PWR_WAKEUP_PIN8_LOW are not available for STM32H503xx devices.
  198. * @retval None.
  199. */
  200. void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity)
  201. {
  202. /* Check the parameters */
  203. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinPolarity));
  204. /*
  205. Enable and Specify the Wake-Up pin polarity and the pull configuration
  206. for the event detection (rising or falling edge).
  207. */
  208. MODIFY_REG(PWR->WUCR, PWR_EWUP_MASK, WakeUpPinPolarity);
  209. }
  210. /**
  211. * @brief Disable the WakeUp PINx functionality.
  212. * @param WakeUpPinx : Specifies the Power Wake-Up pin to disable.
  213. * This parameter can be one of the following values:
  214. * @arg PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3,PWR_WAKEUP_PIN4,
  215. * PWR_WAKEUP_PIN5, PWR_WAKEUP_PIN6, PWR_WAKEUP_PIN7.PWR_WAKEUP_PIN8.
  216. * or one of the following values where the user can explicitly states
  217. * the enabled pin and the chosen polarity:
  218. * @arg PWR_WAKEUP_PIN1_HIGH, PWR_WAKEUP_PIN1_LOW,
  219. * PWR_WAKEUP_PIN2_HIGH, PWR_WAKEUP_PIN2_LOW,
  220. * PWR_WAKEUP_PIN3_HIGH, PWR_WAKEUP_PIN3_LOW,
  221. * PWR_WAKEUP_PIN4_HIGH, PWR_WAKEUP_PIN4_LOW,
  222. * PWR_WAKEUP_PIN5_HIGH, PWR_WAKEUP_PIN5_LOW,
  223. * PWR_WAKEUP_PIN6_HIGH, PWR_WAKEUP_PIN6_LOW,
  224. * PWR_WAKEUP_PIN7_HIGH, PWR_WAKEUP_PIN7_LOW,
  225. * PWR_WAKEUP_PIN8_HIGH, PWR_WAKEUP_PIN8_LOW.
  226. * @note The PWR_WAKEUP_PIN6_HIGH, PWR_WAKEUP_PIN6_LOW, PWR_WAKEUP_PIN7_HIGH, PWR_WAKEUP_PIN7_LOW,
  227. * PWR_WAKEUP_PIN8_HIGH and PWR_WAKEUP_PIN8_LOW are not available for STM32H503xx devices.
  228. * @retval None.
  229. */
  230. void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
  231. {
  232. /* Check the parameters */
  233. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  234. /* Disable the wake up pin selected */
  235. CLEAR_BIT(PWR->WUCR, (PWR_WUCR_WUPEN & WakeUpPinx));
  236. }
  237. /**
  238. * @brief Enter the CPU in SLEEP mode.
  239. * @note In SLEEP mode, all I/O pins keep the same state as in Run mode.
  240. * @note CPU clock is off and all peripherals including Cortex-M33 core such
  241. * as NVIC and SysTick can run and wake up the CPU when an interrupt
  242. * or an event occurs.
  243. * @param Regulator : Specifies the regulator state in Sleep mode.
  244. * This parameter can be one of the following values :
  245. * @arg @ref PWR_MAINREGULATOR_ON
  246. * @arg @ref PWR_LOWPOWERREGULATOR_ON
  247. * @note This parameter is not available in this product.
  248. * The parameter is kept just to maintain compatibility with other
  249. * products.
  250. * @param SLEEPEntry : Specifies if SLEEP mode is entered with WFI or WFE
  251. * instruction.
  252. * This parameter can be one of the following values :
  253. * @arg @ref PWR_SLEEPENTRY_WFI enter SLEEP mode with Wait
  254. * For Interrupt request.
  255. * @arg @ref PWR_SLEEPENTRY_WFE enter SLEEP mode with Wait
  256. * For Event request.
  257. * @note When WFI entry is used, ticks interrupt must be disabled to avoid
  258. * unexpected CPU wake up.
  259. * @retval None.
  260. */
  261. void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
  262. {
  263. UNUSED(Regulator);
  264. /* Check the parameter */
  265. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  266. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  267. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  268. /* Select SLEEP mode entry */
  269. if (SLEEPEntry == PWR_SLEEPENTRY_WFI)
  270. {
  271. /* Wait For Interrupt Request */
  272. __WFI();
  273. }
  274. else
  275. {
  276. /* Wait For Event Request */
  277. __SEV();
  278. __WFE();
  279. __WFE();
  280. }
  281. }
  282. /**
  283. * @brief Enter the whole system to STOP mode.
  284. * @note In STOP mode, the regulator remains in main regulator mode,
  285. * allowing a very fast wakeup time but with much higher consumption
  286. * comparing to other STOP modes.
  287. * @note STOP offers the largest number of active peripherals and wakeup
  288. * sources, a smaller wakeup time but a higher consumption.
  289. * STOP mode achieves the lowest power consumption while retaining
  290. * the content of SRAM and registers. All clocks in the VCORE domain
  291. * are stopped. The PLL, the HSI, the CSI and the HSE crystal oscillators
  292. * are disabled. The LSE or LSI is still running.
  293. * @note The system clock when exiting from Stop mode can be either HSI
  294. * or CSI, depending on software configuration.
  295. * @param Regulator : Specifies the regulator state in Sleep mode.
  296. * This parameter can be one of the following values :
  297. * @arg @ref PWR_MAINREGULATOR_ON
  298. * @arg @ref PWR_LOWPOWERREGULATOR_ON
  299. * @note This parameter is not available in this product.
  300. * The parameter is kept just to maintain compatibility with other
  301. * products.
  302. * @param STOPEntry : Specifies if STOP mode is entered with WFI or WFE
  303. * instruction.
  304. * This parameter can be one of the following values :
  305. * @arg @ref PWR_STOPENTRY_WFI enter STOP mode with Wait
  306. * For Interrupt request.
  307. * @arg @ref PWR_STOPENTRY_WFE enter STOP mode with Wait
  308. * For Event request.
  309. * @retval None.
  310. */
  311. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  312. {
  313. UNUSED(Regulator);
  314. /* Check the parameter */
  315. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  316. /* Select STOP mode */
  317. CLEAR_BIT(PWR->PMCR, PWR_PMCR_LPMS);
  318. /* Set SLEEPDEEP bit of Cortex System Control Register */
  319. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  320. /* Select STOP mode entry */
  321. if (STOPEntry == PWR_STOPENTRY_WFI)
  322. {
  323. /* Wait For Interrupt Request */
  324. __WFI();
  325. }
  326. else
  327. {
  328. /* Wait For Event Request */
  329. __SEV();
  330. __WFE();
  331. __WFE();
  332. }
  333. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  334. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  335. }
  336. /**
  337. * @brief Enter the whole system to STANDBY mode.
  338. * @note The STANDBY mode is used to achieve the lowest power consumption
  339. * with BOR. The internal regulator is switched off so that the VCORE
  340. * domain is powered off. The PLL, the HSI, the CSI and the HSE crystal
  341. * oscillators are also switched off.
  342. * @note After entering STANDBY mode, SRAMs and register contents are lost
  343. * except for registers and backup SRAM in the Backup domain and
  344. * STANDBY circuitry.
  345. * @retval None.
  346. */
  347. void HAL_PWR_EnterSTANDBYMode(void)
  348. {
  349. /* Select STANDBY mode */
  350. SET_BIT(PWR->PMCR, PWR_PMCR_LPMS);
  351. /* Set SLEEPDEEP bit of Cortex System Control Register */
  352. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  353. /* Wait For all memory accesses to complete before continuing */
  354. __DSB();
  355. /* Ensure that the processor pipeline is flushed */
  356. __ISB();
  357. /* Wait For Interrupt Request */
  358. __WFI();
  359. }
  360. /**
  361. * @brief Indicate SLEEP-ON-EXIT feature when returning from handler mode to
  362. * thread mode.
  363. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the
  364. * processor re-enters SLEEP mode when an interruption handling is over.
  365. * Setting this bit is useful when the processor is expected to run
  366. * only on interruptions handling.
  367. * @retval None.
  368. */
  369. void HAL_PWR_EnableSleepOnExit(void)
  370. {
  371. /* Set SLEEPONEXIT bit of Cortex-M33 System Control Register */
  372. SET_BIT(SCB->SCR, SCB_SCR_SLEEPONEXIT_Msk);
  373. }
  374. /**
  375. * @brief Disable SLEEP-ON-EXIT feature when returning from handler mode to
  376. * thread mode.
  377. * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the
  378. * processor re-enters SLEEP mode when an interruption handling is over.
  379. * @retval None.
  380. */
  381. void HAL_PWR_DisableSleepOnExit(void)
  382. {
  383. /* Clear SLEEPONEXIT bit of Cortex-M33 System Control Register */
  384. CLEAR_BIT(SCB->SCR, SCB_SCR_SLEEPONEXIT_Msk);
  385. }
  386. /**
  387. * @brief Enable CORTEX SEV-ON-PEND feature.
  388. * @note Sets SEVONPEND bit of SCR register. When this bit is set, any
  389. * pending event / interrupt even if it's disabled or has insufficient
  390. * priority to cause exception entry wakes up the Cortex-M33.
  391. * @retval None.
  392. */
  393. void HAL_PWR_EnableSEVOnPend(void)
  394. {
  395. /* Set SEVONPEND bit of Cortex-M33 System Control Register */
  396. SET_BIT(SCB->SCR, SCB_SCR_SEVONPEND_Msk);
  397. }
  398. /**
  399. * @brief Disable CORTEX SEVONPEND feature.
  400. * @note Resets SEVONPEND bit of SCR register. When this bit is reset, only
  401. * enabled pending causes exception entry wakes up the Cortex-M33.
  402. * @retval None.
  403. */
  404. void HAL_PWR_DisableSEVOnPend(void)
  405. {
  406. /* Clear SEVONPEND bit of Cortex-M33 System Control Register */
  407. CLEAR_BIT(SCB->SCR, SCB_SCR_SEVONPEND_Msk);
  408. }
  409. /**
  410. * @brief This function handles the PWR PVD interrupt request.
  411. * @note This API should be called under the PVD_AVD_IRQHandler().
  412. * @note The use of this API is only when we activate the PVD.
  413. * @note When the PVD and AVD are activated at the same time you must use this API:
  414. * HAL_PWREx_PVD_AVD_IRQHandler.
  415. * @retval None.
  416. */
  417. void HAL_PWR_PVD_IRQHandler(void)
  418. {
  419. uint32_t rising_flag;
  420. uint32_t falling_flag;
  421. /* Get pending flags */
  422. rising_flag = READ_REG(EXTI->RPR1);
  423. falling_flag = READ_REG(EXTI->FPR1);
  424. /* Check PWR EXTI flags for PVD */
  425. if (((rising_flag | falling_flag) & PWR_EXTI_LINE_PVD) != 0U)
  426. {
  427. /* PWR PVD interrupt user callback */
  428. HAL_PWR_PVDCallback();
  429. /* Clear PVD EXTI pending bit */
  430. WRITE_REG(EXTI->RPR1, PWR_EXTI_LINE_PVD);
  431. WRITE_REG(EXTI->FPR1, PWR_EXTI_LINE_PVD);
  432. }
  433. }
  434. /**
  435. * @brief PWR PVD interrupt callback.
  436. * @retval None.
  437. */
  438. __weak void HAL_PWR_PVDCallback(void)
  439. {
  440. /* NOTE : This function should not be modified, when the callback is needed,
  441. the HAL_PWR_PVDCallback can be implemented in the user file
  442. */
  443. }
  444. /**
  445. * @}
  446. */
  447. /** @defgroup PWR_Exported_Functions_Group3 Attributes Management Functions
  448. * @brief Attributes management functions
  449. *
  450. @verbatim
  451. ===============================================================================
  452. ##### PWR Attributes Functions #####
  453. ===============================================================================
  454. [..]
  455. @endverbatim
  456. * @{
  457. */
  458. /**
  459. * @brief Configure the PWR item attributes.
  460. * @note Available attributes are security and privilege protection.
  461. * @note Security attribute can only be set only by secure access.
  462. * @note Privilege attribute for secure items can be managed only by a secure
  463. * privileged access.
  464. * @note Privilege attribute for nsecure items can be managed by a secure
  465. * privileged access or by a nsecure privileged access.
  466. * @note As the privileged attributes concern either all secure or all non-secure
  467. * PWR resources accesses and not each PWR individual items access attribute,
  468. * the application must ensure that the privilege access attribute configurations
  469. * are coherent amongst the security level set on PWR individual items so not to
  470. * overwrite a previous more restricted access rule (consider either all secure
  471. * and/or all non-secure PWR resources accesses by privileged-only transactions
  472. * or privileged and unprivileged transactions).
  473. * @param Item : Specifies the item(s) to set attributes on.
  474. * This parameter can be a combination of @ref PWR_Items.
  475. * @param Attributes : Specifies the available attribute(s).
  476. * This parameter can be one of @ref PWR_Attributes.
  477. * @retval None.
  478. */
  479. void HAL_PWR_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  480. {
  481. /* Check the parameters */
  482. assert_param(IS_PWR_ATTRIBUTES(Attributes));
  483. #if defined (PWR_SECCFGR_WUP1SEC)
  484. assert_param(IS_PWR_ITEMS_ATTRIBUTES(Item));
  485. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  486. /* Secure item management (TZEN = 1) */
  487. if ((Attributes & PWR_ITEM_ATTR_SEC_PRIV_MASK) == PWR_ITEM_ATTR_SEC_PRIV_MASK)
  488. {
  489. /* Privilege item management */
  490. if ((Attributes & PWR_SEC_PRIV) == PWR_SEC_PRIV)
  491. {
  492. SET_BIT(PWR->SECCFGR, Item);
  493. SET_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_SPRIV);
  494. }
  495. else
  496. {
  497. SET_BIT(PWR->SECCFGR, Item);
  498. CLEAR_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_SPRIV);
  499. }
  500. }
  501. /* NSecure item management */
  502. else
  503. {
  504. /* Privilege item management */
  505. if ((Attributes & PWR_NSEC_PRIV) == PWR_NSEC_PRIV)
  506. {
  507. CLEAR_BIT(PWR->SECCFGR, Item);
  508. SET_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_NSPRIV);
  509. }
  510. else
  511. {
  512. CLEAR_BIT(PWR->SECCFGR, Item);
  513. CLEAR_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_NSPRIV);
  514. }
  515. }
  516. #else
  517. /* NSecure item management (TZEN = 0) */
  518. if ((Attributes & PWR_ITEM_ATTR_NSEC_PRIV_MASK) == PWR_ITEM_ATTR_NSEC_PRIV_MASK)
  519. {
  520. /* Privilege item management */
  521. if ((Attributes & PWR_NSEC_PRIV) == PWR_NSEC_PRIV)
  522. {
  523. SET_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_NSPRIV);
  524. }
  525. else
  526. {
  527. CLEAR_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_NSPRIV);
  528. }
  529. }
  530. #endif /* __ARM_FEATURE_CMSE */
  531. #else /* PWR_SECCFGR_WUP1SEC */
  532. /* Prevent unused argument(s) compilation warning */
  533. UNUSED(Item);
  534. /* NSecure item management (TZEN = 0) */
  535. if ((Attributes & PWR_ITEM_ATTR_NSEC_PRIV_MASK) == PWR_ITEM_ATTR_NSEC_PRIV_MASK)
  536. {
  537. /* Privilege item management */
  538. if ((Attributes & PWR_PRIV) == PWR_PRIV)
  539. {
  540. SET_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_PRIV);
  541. }
  542. else
  543. {
  544. CLEAR_BIT(PWR->PRIVCFGR, PWR_PRIVCFGR_PRIV);
  545. }
  546. }
  547. #endif /* PWR_SECCFGR_WUP1SEC */
  548. }
  549. /**
  550. * @brief Get attribute(s) of a PWR item.
  551. * @param Item : Specifies the item(s) to set attributes on.
  552. * This parameter can be one of @ref PWR_Items.
  553. * @param pAttributes : Pointer to return attribute(s).
  554. * Returned value could be on of @ref PWR_Attributes.
  555. * @retval HAL Status.
  556. */
  557. HAL_StatusTypeDef HAL_PWR_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  558. {
  559. uint32_t attributes;
  560. /* Check attribute pointer */
  561. if (pAttributes == NULL)
  562. {
  563. return HAL_ERROR;
  564. }
  565. #if defined (PWR_SECCFGR_WUP1SEC)
  566. /* Check the parameter */
  567. assert_param(IS_PWR_ITEMS_ATTRIBUTES(Item));
  568. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  569. /* Check item security */
  570. if ((PWR->SECCFGR & Item) == Item)
  571. {
  572. /* Get Secure privileges attribute */
  573. attributes = ((PWR->PRIVCFGR & PWR_PRIVCFGR_SPRIV) == 0U) ? PWR_SEC_NPRIV : PWR_SEC_PRIV;
  574. }
  575. else
  576. {
  577. /* Get Non-Secure privileges attribute */
  578. attributes = ((PWR->PRIVCFGR & PWR_PRIVCFGR_NSPRIV) == 0U) ? PWR_NSEC_NPRIV : PWR_NSEC_PRIV;
  579. }
  580. #else
  581. /* Get Non-Secure privileges attribute */
  582. attributes = ((PWR->PRIVCFGR & PWR_PRIVCFGR_NSPRIV) == 0U) ? PWR_NSEC_NPRIV : PWR_NSEC_PRIV;
  583. #endif /* __ARM_FEATURE_CMSE */
  584. #else /* PWR_SECCFGR_WUP1SEC*/
  585. /* Prevent unused argument(s) compilation warning */
  586. UNUSED(Item);
  587. /* Get Non-Secure privileges attribute */
  588. attributes = ((PWR->PRIVCFGR & PWR_PRIVCFGR_PRIV) == 0U) ? PWR_NPRIV : PWR_PRIV;
  589. #endif /* PWR_SECCFGR_WUP1SEC */
  590. /* return value */
  591. *pAttributes = attributes;
  592. return HAL_OK;
  593. }
  594. /**
  595. * @}
  596. */
  597. /**
  598. * @}
  599. */
  600. #endif /* defined (HAL_PWR_MODULE_ENABLED) */
  601. /**
  602. * @}
  603. */
  604. /**
  605. * @}
  606. */