95521488548e0cc7b7dad94c22e109a5f86c367c
[lwext4.git] / demos / stm32f429_disco / stm / stm32f4_spl / src / stm32f4xx_hal_pwr_ex.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f4xx_hal_pwr_ex.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    18-February-2014\r
7   * @brief   Extended PWR HAL module driver.\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of PWR extension peripheral:           \r
10   *           + Peripheral Extended features functions\r
11   *         \r
12   ******************************************************************************\r
13   * @attention\r
14   *\r
15   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>\r
16   *\r
17   * Redistribution and use in source and binary forms, with or without modification,\r
18   * are permitted provided that the following conditions are met:\r
19   *   1. Redistributions of source code must retain the above copyright notice,\r
20   *      this list of conditions and the following disclaimer.\r
21   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
22   *      this list of conditions and the following disclaimer in the documentation\r
23   *      and/or other materials provided with the distribution.\r
24   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
25   *      may be used to endorse or promote products derived from this software\r
26   *      without specific prior written permission.\r
27   *\r
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
29   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
30   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
31   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
32   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
34   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
35   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
36   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
37   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
38   *\r
39   ******************************************************************************\r
40   */ \r
41 \r
42 /* Includes ------------------------------------------------------------------*/\r
43 #include "stm32f4xx_hal.h"\r
44 \r
45 /** @addtogroup STM32F4xx_HAL_Driver\r
46   * @{\r
47   */\r
48 \r
49 /** @defgroup PWREx \r
50   * @brief PWR HAL module driver\r
51   * @{\r
52   */\r
53 \r
54 #ifdef HAL_PWR_MODULE_ENABLED\r
55 \r
56 /* Private typedef -----------------------------------------------------------*/\r
57 /* Private define ------------------------------------------------------------*/\r
58 #define PWR_OVERDRIVE_TIMEOUT_VALUE  1000\r
59 #define PWR_BKPREG_TIMEOUT_VALUE     1000\r
60 /* Private macro -------------------------------------------------------------*/\r
61 /* Private variables ---------------------------------------------------------*/\r
62 /* Private function prototypes -----------------------------------------------*/\r
63 /* Private functions ---------------------------------------------------------*/\r
64 \r
65 /** @defgroup PWREx_Private_Functions\r
66   * @{\r
67   */\r
68 \r
69 /** @defgroup PWREx_Group1 Peripheral Extended features functions \r
70   *  @brief Peripheral Extended features functions \r
71   *\r
72 @verbatim   \r
73 \r
74  ===============================================================================\r
75                  ##### Peripheral extended features functions #####\r
76  ===============================================================================\r
77 \r
78     *** Main and Backup Regulators configuration ***\r
79     ================================================\r
80     [..] \r
81       (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from \r
82           the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is \r
83           retained even in Standby or VBAT mode when the low power backup regulator\r
84           is enabled. It can be considered as an internal EEPROM when VBAT is \r
85           always present. You can use the HAL_PWR_EnableBkUpReg() function to \r
86           enable the low power backup regulator. \r
87 \r
88       (+) When the backup domain is supplied by VDD (analog switch connected to VDD) \r
89           the backup SRAM is powered from VDD which replaces the VBAT power supply to \r
90           save battery life.\r
91 \r
92       (+) The backup SRAM is not mass erased by a tamper event. It is read \r
93           protected to prevent confidential data, such as cryptographic private \r
94           key, from being accessed. The backup SRAM can be erased only through \r
95           the Flash interface when a protection level change from level 1 to \r
96           level 0 is requested. \r
97       -@- Refer to the description of Read protection (RDP) in the Flash \r
98           programming manual.\r
99 \r
100       (+) The main internal regulator can be configured to have a tradeoff between \r
101           performance and power consumption when the device does not operate at \r
102           the maximum frequency. This is done through __HAL_PWR_MAINREGULATORMODE_CONFIG() \r
103           macro which configure VOS bit in PWR_CR register\r
104           \r
105         Refer to the product datasheets for more details.\r
106 \r
107     *** FLASH Power Down configuration ****\r
108     =======================================\r
109     [..] \r
110       (+) By setting the FPDS bit in the PWR_CR register by using the \r
111           HAL_PWR_EnableFlashPowerDown() function, the Flash memory also enters power \r
112           down mode when the device enters Stop mode. When the Flash memory \r
113           is in power down mode, an additional startup delay is incurred when \r
114           waking up from Stop mode.\r
115           \r
116            (+) For STM32F42xxx/43xxx Devices, the scale can be modified only when the PLL \r
117            is OFF and the HSI or HSE clock source is selected as system clock. \r
118            The new value programmed is active only when the PLL is ON.\r
119            When the PLL is OFF, the voltage scale 3 is automatically selected. \r
120         Refer to the datasheets for more details.\r
121 \r
122     *** Over-Drive and Under-Drive configuration ****\r
123     =================================================\r
124     [..]         \r
125        (+) For STM32F42xxx/43xxx Devices, in Run mode: the main regulator has\r
126            2 operating modes available:\r
127         (++) Normal mode: The CPU and core logic operate at maximum frequency at a given \r
128              voltage scaling (scale 1, scale 2 or scale 3)\r
129         (++) Over-drive mode: This mode allows the CPU and the core logic to operate at a \r
130             higher frequency than the normal mode for a given voltage scaling (scale 1,  \r
131             scale 2 or scale 3). This mode is enabled through HAL_PWREx_EnableOverDrive() function and\r
132             disabled by HAL_PWREx_DisableOverDrive() function, to enter or exit from Over-drive mode please follow \r
133             the sequence described in Reference manual.\r
134              \r
135        (+) For STM32F42xxx/43xxx Devices, in Stop mode: the main regulator or low power regulator \r
136            supplies a low power voltage to the 1.2V domain, thus preserving the content of registers \r
137            and internal SRAM. 2 operating modes are available:\r
138          (++) Normal mode: the 1.2V domain is preserved in nominal leakage mode. This mode is only \r
139               available when the main regulator or the low power regulator is used in Scale 3 or \r
140               low voltage mode.\r
141          (++) Under-drive mode: the 1.2V domain is preserved in reduced leakage mode. This mode is only\r
142               available when the main regulator or the low power regulator is in low voltage mode.\r
143 \r
144 @endverbatim\r
145   * @{\r
146   */\r
147 \r
148 /**\r
149   * @brief Enables the Backup Regulator.\r
150   * @param None\r
151   * @retval HAL status\r
152   */\r
153 HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)\r
154 {\r
155   uint32_t timeout = 0;   \r
156 \r
157   *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)ENABLE;\r
158 \r
159   /* Get timeout */\r
160   timeout = HAL_GetTick() + PWR_BKPREG_TIMEOUT_VALUE;\r
161   /* Wait till Backup regulator ready flag is set */  \r
162   while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)\r
163   {\r
164     if(HAL_GetTick() >= timeout)\r
165     {\r
166       return HAL_TIMEOUT;\r
167     } \r
168   }\r
169   return HAL_OK;\r
170 }\r
171 \r
172 /**\r
173   * @brief Disables the Backup Regulator.\r
174   * @param None\r
175   * @retval None\r
176   */\r
177 HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)\r
178 {\r
179   uint32_t timeout = 0; \r
180 \r
181   *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)DISABLE;\r
182 \r
183   /* Get timeout */\r
184   timeout = HAL_GetTick() + PWR_BKPREG_TIMEOUT_VALUE;\r
185   /* Wait till Backup regulator ready flag is set */  \r
186   while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)\r
187   {\r
188     if(HAL_GetTick() >= timeout)\r
189     {\r
190       return HAL_TIMEOUT;\r
191     } \r
192   }\r
193   return HAL_OK;\r
194 }\r
195 \r
196 /**\r
197   * @brief Enables the Flash Power Down in Stop mode.\r
198   * @param None\r
199   * @retval None\r
200   */\r
201 void HAL_PWREx_EnableFlashPowerDown(void)\r
202 {\r
203   *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)ENABLE;\r
204 }\r
205 \r
206 /**\r
207   * @brief Disables the Flash Power Down in Stop mode.\r
208   * @param None\r
209   * @retval None\r
210   */\r
211 void HAL_PWREx_DisableFlashPowerDown(void)\r
212 {\r
213   *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)DISABLE;\r
214 }\r
215 \r
216 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\r
217 /**\r
218   * @brief  Activates the Over-Drive mode.\r
219   * @note   These macros can be used only for STM32F42xx/STM32F43xx devices.\r
220   *         This mode allows the CPU and the core logic to operate at a higher frequency\r
221   *         than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).   \r
222   * @note   It is recommended to enter or exit Over-drive mode when the application is not running \r
223   *         critical tasks and when the system clock source is either HSI or HSE. \r
224   *         During the Over-drive switch activation, no peripheral clocks should be enabled.   \r
225   *         The peripheral clocks must be enabled once the Over-drive mode is activated.   \r
226   * @param  None\r
227   * @retval HAL status\r
228   */\r
229 HAL_StatusTypeDef HAL_PWREx_ActivateOverDrive(void)\r
230 {\r
231   uint32_t timeout = 0;\r
232   \r
233   __PWR_CLK_ENABLE();\r
234   \r
235   /* Enable the Over-drive to extend the clock frequency to 180 Mhz */\r
236   __HAL_PWR_OVERDRIVE_ENABLE();\r
237   \r
238   /* Get timeout */\r
239   timeout = HAL_GetTick() + PWR_OVERDRIVE_TIMEOUT_VALUE;\r
240   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))\r
241   {\r
242     if(HAL_GetTick() >= timeout)\r
243     {\r
244       return HAL_TIMEOUT;\r
245     }\r
246   }\r
247   \r
248   /* Enable the Over-drive switch */\r
249   __HAL_PWR_OVERDRIVESWITCHING_ENABLE();\r
250   \r
251   /* Get timeout */\r
252   timeout = HAL_GetTick() + PWR_OVERDRIVE_TIMEOUT_VALUE;\r
253   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))\r
254   {\r
255     if(HAL_GetTick() >= timeout)\r
256     {\r
257       return HAL_TIMEOUT;\r
258     }\r
259   } \r
260   return HAL_OK;\r
261 }\r
262 \r
263 /**\r
264   * @brief  Deactivates the Over-Drive mode.\r
265   * @note   These macros can be used only for STM32F42xx/STM32F43xx devices.\r
266   *         This mode allows the CPU and the core logic to operate at a higher frequency\r
267   *         than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).    \r
268   * @note   It is recommended to enter or exit Over-drive mode when the application is not running \r
269   *         critical tasks and when the system clock source is either HSI or HSE. \r
270   *         During the Over-drive switch activation, no peripheral clocks should be enabled.   \r
271   *         The peripheral clocks must be enabled once the Over-drive mode is activated.\r
272   * @param  None\r
273   * @retval HAL status\r
274   */\r
275 HAL_StatusTypeDef HAL_PWREx_DeactivateOverDrive(void)\r
276 {\r
277   uint32_t timeout = 0;\r
278   \r
279   __PWR_CLK_ENABLE();\r
280     \r
281   /* Disable the Over-drive switch */\r
282   __HAL_PWR_OVERDRIVESWITCHING_DISABLE();\r
283   \r
284   /* Get timeout */\r
285   timeout = HAL_GetTick() + PWR_OVERDRIVE_TIMEOUT_VALUE;\r
286  \r
287   while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))\r
288   {\r
289     if(HAL_GetTick() >= timeout)\r
290     {\r
291       return HAL_TIMEOUT;\r
292     }\r
293   } \r
294   \r
295   /* Disable the Over-drive */\r
296   __HAL_PWR_OVERDRIVE_DISABLE();\r
297 \r
298   /* Get timeout */\r
299   timeout = HAL_GetTick() + PWR_OVERDRIVE_TIMEOUT_VALUE;  \r
300 \r
301   while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))\r
302   {\r
303     if(HAL_GetTick() >= timeout)\r
304     {\r
305       return HAL_TIMEOUT;\r
306     }\r
307   }\r
308   \r
309   return HAL_OK;\r
310 }\r
311 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */\r
312 /**\r
313   * @}\r
314   */\r
315 \r
316 /**\r
317   * @}\r
318   */\r
319 \r
320 #endif /* HAL_PWR_MODULE_ENABLED */\r
321 /**\r
322   * @}\r
323   */\r
324 \r
325 /**\r
326   * @}\r
327   */\r
328 \r
329 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r