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.
 
 
 

565 lines
19 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_pwr_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief Extended PWR HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of PWR extension peripheral:
  10. * + Peripheral Extended features functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f7xx_hal.h"
  43. /** @addtogroup STM32F7xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup PWREx PWREx
  47. * @brief PWR HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_PWR_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /** @addtogroup PWREx_Private_Constants
  54. * @{
  55. */
  56. #define PWR_OVERDRIVE_TIMEOUT_VALUE 1000
  57. #define PWR_UDERDRIVE_TIMEOUT_VALUE 1000
  58. #define PWR_BKPREG_TIMEOUT_VALUE 1000
  59. #define PWR_VOSRDY_TIMEOUT_VALUE 1000
  60. /**
  61. * @}
  62. */
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. /* Private functions ---------------------------------------------------------*/
  67. /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
  68. * @{
  69. */
  70. /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended features functions
  71. * @brief Peripheral Extended features functions
  72. *
  73. @verbatim
  74. ===============================================================================
  75. ##### Peripheral extended features functions #####
  76. ===============================================================================
  77. *** Main and Backup Regulators configuration ***
  78. ================================================
  79. [..]
  80. (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from
  81. the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is
  82. retained even in Standby or VBAT mode when the low power backup regulator
  83. is enabled. It can be considered as an internal EEPROM when VBAT is
  84. always present. You can use the HAL_PWREx_EnableBkUpReg() function to
  85. enable the low power backup regulator.
  86. (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
  87. the backup SRAM is powered from VDD which replaces the VBAT power supply to
  88. save battery life.
  89. (+) The backup SRAM is not mass erased by a tamper event. It is read
  90. protected to prevent confidential data, such as cryptographic private
  91. key, from being accessed. The backup SRAM can be erased only through
  92. the Flash interface when a protection level change from level 1 to
  93. level 0 is requested.
  94. -@- Refer to the description of Read protection (RDP) in the Flash
  95. programming manual.
  96. (+) The main internal regulator can be configured to have a tradeoff between
  97. performance and power consumption when the device does not operate at
  98. the maximum frequency. This is done through __HAL_PWR_MAINREGULATORMODE_CONFIG()
  99. macro which configure VOS bit in PWR_CR register
  100. Refer to the product datasheets for more details.
  101. *** FLASH Power Down configuration ****
  102. =======================================
  103. [..]
  104. (+) By setting the FPDS bit in the PWR_CR register by using the
  105. HAL_PWREx_EnableFlashPowerDown() function, the Flash memory also enters power
  106. down mode when the device enters Stop mode. When the Flash memory
  107. is in power down mode, an additional startup delay is incurred when
  108. waking up from Stop mode.
  109. *** Over-Drive and Under-Drive configuration ****
  110. =================================================
  111. [..]
  112. (+) In Run mode: the main regulator has 2 operating modes available:
  113. (++) Normal mode: The CPU and core logic operate at maximum frequency at a given
  114. voltage scaling (scale 1, scale 2 or scale 3)
  115. (++) Over-drive mode: This mode allows the CPU and the core logic to operate at a
  116. higher frequency than the normal mode for a given voltage scaling (scale 1,
  117. scale 2 or scale 3). This mode is enabled through HAL_PWREx_EnableOverDrive() function and
  118. disabled by HAL_PWREx_DisableOverDrive() function, to enter or exit from Over-drive mode please follow
  119. the sequence described in Reference manual.
  120. (+) In Stop mode: the main regulator or low power regulator supplies a low power
  121. voltage to the 1.2V domain, thus preserving the content of registers
  122. and internal SRAM. 2 operating modes are available:
  123. (++) Normal mode: the 1.2V domain is preserved in nominal leakage mode. This mode is only
  124. available when the main regulator or the low power regulator is used in Scale 3 or
  125. low voltage mode.
  126. (++) Under-drive mode: the 1.2V domain is preserved in reduced leakage mode. This mode is only
  127. available when the main regulator or the low power regulator is in low voltage mode.
  128. @endverbatim
  129. * @{
  130. */
  131. /**
  132. * @brief Enables the Backup Regulator.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)
  136. {
  137. uint32_t tickstart = 0;
  138. /* Enable Backup regulator */
  139. PWR->CSR1 |= PWR_CSR1_BRE;
  140. /* Get tick */
  141. tickstart = HAL_GetTick();
  142. /* Wait till Backup regulator ready flag is set */
  143. while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)
  144. {
  145. if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
  146. {
  147. return HAL_TIMEOUT;
  148. }
  149. }
  150. return HAL_OK;
  151. }
  152. /**
  153. * @brief Disables the Backup Regulator.
  154. * @retval HAL status
  155. */
  156. HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)
  157. {
  158. uint32_t tickstart = 0;
  159. /* Disable Backup regulator */
  160. PWR->CSR1 &= (uint32_t)~((uint32_t)PWR_CSR1_BRE);
  161. /* Get tick */
  162. tickstart = HAL_GetTick();
  163. /* Wait till Backup regulator ready flag is set */
  164. while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)
  165. {
  166. if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
  167. {
  168. return HAL_TIMEOUT;
  169. }
  170. }
  171. return HAL_OK;
  172. }
  173. /**
  174. * @brief Enables the Flash Power Down in Stop mode.
  175. * @retval None
  176. */
  177. void HAL_PWREx_EnableFlashPowerDown(void)
  178. {
  179. /* Enable the Flash Power Down */
  180. PWR->CR1 |= PWR_CR1_FPDS;
  181. }
  182. /**
  183. * @brief Disables the Flash Power Down in Stop mode.
  184. * @retval None
  185. */
  186. void HAL_PWREx_DisableFlashPowerDown(void)
  187. {
  188. /* Disable the Flash Power Down */
  189. PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_FPDS);
  190. }
  191. /**
  192. * @brief Enables Main Regulator low voltage mode.
  193. * @retval None
  194. */
  195. void HAL_PWREx_EnableMainRegulatorLowVoltage(void)
  196. {
  197. /* Enable Main regulator low voltage */
  198. PWR->CR1 |= PWR_CR1_MRUDS;
  199. }
  200. /**
  201. * @brief Disables Main Regulator low voltage mode.
  202. * @retval None
  203. */
  204. void HAL_PWREx_DisableMainRegulatorLowVoltage(void)
  205. {
  206. /* Disable Main regulator low voltage */
  207. PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_MRUDS);
  208. }
  209. /**
  210. * @brief Enables Low Power Regulator low voltage mode.
  211. * @retval None
  212. */
  213. void HAL_PWREx_EnableLowRegulatorLowVoltage(void)
  214. {
  215. /* Enable low power regulator */
  216. PWR->CR1 |= PWR_CR1_LPUDS;
  217. }
  218. /**
  219. * @brief Disables Low Power Regulator low voltage mode.
  220. * @retval None
  221. */
  222. void HAL_PWREx_DisableLowRegulatorLowVoltage(void)
  223. {
  224. /* Disable low power regulator */
  225. PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_LPUDS);
  226. }
  227. /**
  228. * @brief Activates the Over-Drive mode.
  229. * @note This mode allows the CPU and the core logic to operate at a higher frequency
  230. * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
  231. * @note It is recommended to enter or exit Over-drive mode when the application is not running
  232. * critical tasks and when the system clock source is either HSI or HSE.
  233. * During the Over-drive switch activation, no peripheral clocks should be enabled.
  234. * The peripheral clocks must be enabled once the Over-drive mode is activated.
  235. * @retval HAL status
  236. */
  237. HAL_StatusTypeDef HAL_PWREx_EnableOverDrive(void)
  238. {
  239. uint32_t tickstart = 0;
  240. __HAL_RCC_PWR_CLK_ENABLE();
  241. /* Enable the Over-drive to extend the clock frequency to 216 MHz */
  242. __HAL_PWR_OVERDRIVE_ENABLE();
  243. /* Get tick */
  244. tickstart = HAL_GetTick();
  245. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
  246. {
  247. if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  248. {
  249. return HAL_TIMEOUT;
  250. }
  251. }
  252. /* Enable the Over-drive switch */
  253. __HAL_PWR_OVERDRIVESWITCHING_ENABLE();
  254. /* Get tick */
  255. tickstart = HAL_GetTick();
  256. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
  257. {
  258. if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  259. {
  260. return HAL_TIMEOUT;
  261. }
  262. }
  263. return HAL_OK;
  264. }
  265. /**
  266. * @brief Deactivates the Over-Drive mode.
  267. * @note This mode allows the CPU and the core logic to operate at a higher frequency
  268. * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
  269. * @note It is recommended to enter or exit Over-drive mode when the application is not running
  270. * critical tasks and when the system clock source is either HSI or HSE.
  271. * During the Over-drive switch activation, no peripheral clocks should be enabled.
  272. * The peripheral clocks must be enabled once the Over-drive mode is activated.
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_PWREx_DisableOverDrive(void)
  276. {
  277. uint32_t tickstart = 0;
  278. __HAL_RCC_PWR_CLK_ENABLE();
  279. /* Disable the Over-drive switch */
  280. __HAL_PWR_OVERDRIVESWITCHING_DISABLE();
  281. /* Get tick */
  282. tickstart = HAL_GetTick();
  283. while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
  284. {
  285. if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  286. {
  287. return HAL_TIMEOUT;
  288. }
  289. }
  290. /* Disable the Over-drive */
  291. __HAL_PWR_OVERDRIVE_DISABLE();
  292. /* Get tick */
  293. tickstart = HAL_GetTick();
  294. while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
  295. {
  296. if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  297. {
  298. return HAL_TIMEOUT;
  299. }
  300. }
  301. return HAL_OK;
  302. }
  303. /**
  304. * @brief Enters in Under-Drive STOP mode.
  305. *
  306. * @note This mode can be selected only when the Under-Drive is already active
  307. *
  308. * @note This mode is enabled only with STOP low power mode.
  309. * In this mode, the 1.2V domain is preserved in reduced leakage mode. This
  310. * mode is only available when the main regulator or the low power regulator
  311. * is in low voltage mode
  312. *
  313. * @note If the Under-drive mode was enabled, it is automatically disabled after
  314. * exiting Stop mode.
  315. * When the voltage regulator operates in Under-drive mode, an additional
  316. * startup delay is induced when waking up from Stop mode.
  317. *
  318. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  319. *
  320. * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
  321. * the HSI RC oscillator is selected as system clock.
  322. *
  323. * @note When the voltage regulator operates in low power mode, an additional
  324. * startup delay is incurred when waking up from Stop mode.
  325. * By keeping the internal regulator ON during Stop mode, the consumption
  326. * is higher although the startup time is reduced.
  327. *
  328. * @param Regulator: specifies the regulator state in STOP mode.
  329. * This parameter can be one of the following values:
  330. * @arg PWR_MAINREGULATOR_UNDERDRIVE_ON: Main Regulator in under-drive mode
  331. * and Flash memory in power-down when the device is in Stop under-drive mode
  332. * @arg PWR_LOWPOWERREGULATOR_UNDERDRIVE_ON: Low Power Regulator in under-drive mode
  333. * and Flash memory in power-down when the device is in Stop under-drive mode
  334. * @param STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
  335. * This parameter can be one of the following values:
  336. * @arg PWR_SLEEPENTRY_WFI: enter STOP mode with WFI instruction
  337. * @arg PWR_SLEEPENTRY_WFE: enter STOP mode with WFE instruction
  338. * @retval None
  339. */
  340. HAL_StatusTypeDef HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  341. {
  342. uint32_t tempreg = 0;
  343. uint32_t tickstart = 0;
  344. /* Check the parameters */
  345. assert_param(IS_PWR_REGULATOR_UNDERDRIVE(Regulator));
  346. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  347. /* Enable Power ctrl clock */
  348. __HAL_RCC_PWR_CLK_ENABLE();
  349. /* Enable the Under-drive Mode ---------------------------------------------*/
  350. /* Clear Under-drive flag */
  351. __HAL_PWR_CLEAR_ODRUDR_FLAG();
  352. /* Enable the Under-drive */
  353. __HAL_PWR_UNDERDRIVE_ENABLE();
  354. /* Get tick */
  355. tickstart = HAL_GetTick();
  356. /* Wait for UnderDrive mode is ready */
  357. while(__HAL_PWR_GET_FLAG(PWR_FLAG_UDRDY))
  358. {
  359. if((HAL_GetTick() - tickstart ) > PWR_UDERDRIVE_TIMEOUT_VALUE)
  360. {
  361. return HAL_TIMEOUT;
  362. }
  363. }
  364. /* Select the regulator state in STOP mode ---------------------------------*/
  365. tempreg = PWR->CR1;
  366. /* Clear PDDS, LPDS, MRLUDS and LPLUDS bits */
  367. tempreg &= (uint32_t)~(PWR_CR1_PDDS | PWR_CR1_LPDS | PWR_CR1_LPUDS | PWR_CR1_MRUDS);
  368. /* Set LPDS, MRLUDS and LPLUDS bits according to PWR_Regulator value */
  369. tempreg |= Regulator;
  370. /* Store the new value */
  371. PWR->CR1 = tempreg;
  372. /* Set SLEEPDEEP bit of Cortex System Control Register */
  373. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  374. /* Select STOP mode entry --------------------------------------------------*/
  375. if(STOPEntry == PWR_SLEEPENTRY_WFI)
  376. {
  377. /* Request Wait For Interrupt */
  378. __WFI();
  379. }
  380. else
  381. {
  382. /* Request Wait For Event */
  383. __WFE();
  384. }
  385. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  386. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  387. return HAL_OK;
  388. }
  389. /**
  390. * @brief Returns Voltage Scaling Range.
  391. * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1, PWR_REGULATOR_VOLTAGE_SCALE2 or
  392. * PWR_REGULATOR_VOLTAGE_SCALE3)PWR_REGULATOR_VOLTAGE_SCALE1
  393. */
  394. uint32_t HAL_PWREx_GetVoltageRange(void)
  395. {
  396. return (PWR->CR1 & PWR_CR1_VOS);
  397. }
  398. /**
  399. * @brief Configures the main internal regulator output voltage.
  400. * @param VoltageScaling: specifies the regulator output voltage to achieve
  401. * a tradeoff between performance and power consumption.
  402. * This parameter can be one of the following values:
  403. * @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output range 1 mode,
  404. * typical output voltage at 1.4 V,
  405. * system frequency up to 216 MHz.
  406. * @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output range 2 mode,
  407. * typical output voltage at 1.2 V,
  408. * system frequency up to 180 MHz.
  409. * @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output range 2 mode,
  410. * typical output voltage at 1.00 V,
  411. * system frequency up to 151 MHz.
  412. * @note To update the system clock frequency(SYSCLK):
  413. * - Set the HSI or HSE as system clock frequency using the HAL_RCC_ClockConfig().
  414. * - Call the HAL_RCC_OscConfig() to configure the PLL.
  415. * - Call HAL_PWREx_ConfigVoltageScaling() API to adjust the voltage scale.
  416. * - Set the new system clock frequency using the HAL_RCC_ClockConfig().
  417. * @note The scale can be modified only when the HSI or HSE clock source is selected
  418. * as system clock source, otherwise the API returns HAL_ERROR.
  419. * @note When the PLL is OFF, the voltage scale 3 is automatically selected and the VOS bits
  420. * value in the PWR_CR1 register are not taken in account.
  421. * @note This API forces the PLL state ON to allow the possibility to configure the voltage scale 1 or 2.
  422. * @note The new voltage scale is active only when the PLL is ON.
  423. * @retval HAL Status
  424. */
  425. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  426. {
  427. uint32_t tickstart = 0;
  428. assert_param(IS_PWR_REGULATOR_VOLTAGE(VoltageScaling));
  429. /* Enable Power ctrl clock */
  430. __HAL_RCC_PWR_CLK_ENABLE();
  431. /* Check if the PLL is used as system clock or not */
  432. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  433. {
  434. /* Disable the main PLL */
  435. __HAL_RCC_PLL_DISABLE();
  436. /* Get Start Tick */
  437. tickstart = HAL_GetTick();
  438. /* Wait till PLL is disabled */
  439. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  440. {
  441. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  442. {
  443. return HAL_TIMEOUT;
  444. }
  445. }
  446. /* Set Range */
  447. __HAL_PWR_VOLTAGESCALING_CONFIG(VoltageScaling);
  448. /* Enable the main PLL */
  449. __HAL_RCC_PLL_ENABLE();
  450. /* Get Start Tick */
  451. tickstart = HAL_GetTick();
  452. /* Wait till PLL is ready */
  453. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  454. {
  455. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  456. {
  457. return HAL_TIMEOUT;
  458. }
  459. }
  460. /* Get Start Tick */
  461. tickstart = HAL_GetTick();
  462. while((__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY) == RESET))
  463. {
  464. if((HAL_GetTick() - tickstart ) > PWR_VOSRDY_TIMEOUT_VALUE)
  465. {
  466. return HAL_TIMEOUT;
  467. }
  468. }
  469. }
  470. else
  471. {
  472. return HAL_ERROR;
  473. }
  474. return HAL_OK;
  475. }
  476. /**
  477. * @}
  478. */
  479. /**
  480. * @}
  481. */
  482. #endif /* HAL_PWR_MODULE_ENABLED */
  483. /**
  484. * @}
  485. */
  486. /**
  487. * @}
  488. */
  489. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/