]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_IAR/system_and_ST_code/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_pwr.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_IAR / system_and_ST_code / STM32L1xx_StdPeriph_Driver / src / stm32l1xx_pwr.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_pwr.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    07/02/2010\r
7   * @brief   This file provides all the PWR firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32l1xx_pwr.h"\r
23 #include "stm32l1xx_rcc.h"\r
24 \r
25 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
26   * @{\r
27   */\r
28 \r
29 /** @defgroup PWR \r
30   * @brief PWR driver modules\r
31   * @{\r
32   */ \r
33 \r
34 /** @defgroup PWR_Private_TypesDefinitions\r
35   * @{\r
36   */\r
37 \r
38 /**\r
39   * @}\r
40   */\r
41 \r
42 /** @defgroup PWR_Private_Defines\r
43   * @{\r
44   */\r
45 \r
46 /* --------- PWR registers bit address in the alias region ---------- */\r
47 #define PWR_OFFSET               (PWR_BASE - PERIPH_BASE)\r
48 \r
49 /* --- CR Register ---*/\r
50 \r
51 /* Alias word address of DBP bit */\r
52 #define CR_OFFSET                (PWR_OFFSET + 0x00)\r
53 #define DBP_BitNumber            0x08\r
54 #define CR_DBP_BB                (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))\r
55 \r
56 /* Alias word address of PVDE bit */\r
57 #define PVDE_BitNumber           0x04\r
58 #define CR_PVDE_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PVDE_BitNumber * 4))\r
59 \r
60 /* Alias word address of ULP bit */\r
61 #define ULP_BitNumber           0x09\r
62 #define CR_ULP_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (ULP_BitNumber * 4))\r
63 \r
64 /* Alias word address of FWU bit */\r
65 #define FWU_BitNumber           0x0A\r
66 #define CR_FWU_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (FWU_BitNumber * 4))\r
67 \r
68 /* --- CSR Register ---*/\r
69 \r
70 /* Alias word address of EWUP bit */\r
71 #define CSR_OFFSET               (PWR_OFFSET + 0x04)\r
72 #define EWUP_BitNumber           0x08\r
73 #define CSR_EWUP_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4))\r
74 \r
75 /* ------------------ PWR registers bit mask ------------------------ */\r
76 \r
77 /* CR register bit mask */\r
78 #define CR_DS_MASK               ((uint32_t)0xFFFFFFFC)\r
79 #define CR_PLS_MASK              ((uint32_t)0xFFFFFF1F)\r
80 #define CR_VOS_MASK              ((uint32_t)0xFFFFE7FF)\r
81 \r
82 /**\r
83   * @}\r
84   */\r
85 \r
86 /** @defgroup PWR_Private_Macros\r
87   * @{\r
88   */\r
89 \r
90 /**\r
91   * @}\r
92   */\r
93 \r
94 /** @defgroup PWR_Private_Variables\r
95   * @{\r
96   */\r
97 \r
98 /**\r
99   * @}\r
100   */\r
101 \r
102 /** @defgroup PWR_Private_FunctionPrototypes\r
103   * @{\r
104   */\r
105 \r
106 /**\r
107   * @}\r
108   */\r
109 \r
110 /** @defgroup PWR_Private_Functions\r
111   * @{\r
112   */\r
113 \r
114 /**\r
115   * @brief  Deinitializes the PWR peripheral registers to their default reset values.\r
116   * @param  None\r
117   * @retval None\r
118   */\r
119 void PWR_DeInit(void)\r
120 {\r
121   RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);\r
122   RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);\r
123 }\r
124 \r
125 /**\r
126   * @brief  Enables or disables access to the RTC and backup registers.\r
127   * @param  NewState: new state of the access to the RTC and backup registers.\r
128   *   This parameter can be: ENABLE or DISABLE.\r
129   * @retval None\r
130   */\r
131 void PWR_RTCAccessCmd(FunctionalState NewState)\r
132 {\r
133   /* Check the parameters */\r
134   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
135   \r
136   *(__IO uint32_t *) CR_DBP_BB = (uint32_t)NewState;\r
137 }\r
138 \r
139 /**\r
140   * @brief  Enables or disables the Power Voltage Detector(PVD).\r
141   * @param  NewState: new state of the PVD.\r
142   *   This parameter can be: ENABLE or DISABLE.\r
143   * @retval None\r
144   */\r
145 void PWR_PVDCmd(FunctionalState NewState)\r
146 {\r
147   /* Check the parameters */\r
148   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
149   \r
150   *(__IO uint32_t *) CR_PVDE_BB = (uint32_t)NewState;\r
151 }\r
152 \r
153 /**\r
154   * @brief  Configures the voltage threshold detected by the Power Voltage Detector(PVD).\r
155   * @param  PWR_PVDLevel: specifies the PVD detection level\r
156   *   This parameter can be one of the following values:\r
157   *     @arg PWR_PVDLevel_0: PVD detection level set to 1.9V\r
158   *     @arg PWR_PVDLevel_1: PVD detection level set to 2.1V\r
159   *     @arg PWR_PVDLevel_2: PVD detection level set to 2.3V\r
160   *     @arg PWR_PVDLevel_3: PVD detection level set to 2.5V\r
161   *     @arg PWR_PVDLevel_4: PVD detection level set to 2.7V\r
162   *     @arg PWR_PVDLevel_5: PVD detection level set to 2.9V\r
163   *     @arg PWR_PVDLevel_6: PVD detection level set to 3.1V\r
164   *     @arg PWR_PVDLevel_7: External input analog voltage (Compare internally to VREFINT)\r
165   * @retval None\r
166   */\r
167 void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)\r
168 {\r
169   uint32_t tmpreg = 0;\r
170   \r
171   /* Check the parameters */\r
172   assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));\r
173   \r
174   tmpreg = PWR->CR;\r
175   \r
176   /* Clear PLS[7:5] bits */\r
177   tmpreg &= CR_PLS_MASK;\r
178   \r
179   /* Set PLS[7:5] bits according to PWR_PVDLevel value */\r
180   tmpreg |= PWR_PVDLevel;\r
181   \r
182   /* Store the new value */\r
183   PWR->CR = tmpreg;\r
184 }\r
185 \r
186 /**\r
187   * @brief  Enables or disables the WakeUp Pin functionality.\r
188   * @param  PWR_WakeUpPin: specifies the WakeUpPin.\r
189   *   This parameter can be: PWR_WakeUpPin_1, PWR_WakeUpPin_2 or PWR_WakeUpPin_3.\r
190   * @param  NewState: new state of the WakeUp Pin functionality.\r
191   *   This parameter can be: ENABLE or DISABLE.\r
192   * @retval None\r
193   */\r
194 void PWR_WakeUpPinCmd(uint32_t PWR_WakeUpPin, FunctionalState NewState)\r
195 {\r
196   __IO uint32_t tmp = 0;\r
197   \r
198   /* Check the parameters */\r
199   assert_param(IS_PWR_WAKEUP_PIN(PWR_WakeUpPin));\r
200   \r
201   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
202 \r
203   tmp = CSR_EWUP_BB + PWR_WakeUpPin;\r
204   \r
205   *(__IO uint32_t *) (tmp) = (uint32_t)NewState;\r
206 }\r
207 \r
208 /**\r
209   * @brief  Enables or disables the Fast WakeUp from Ultra Low Power mode.\r
210   * @param  NewState: new state of the Fast WakeUp  functionality.\r
211   *   This parameter can be: ENABLE or DISABLE.\r
212   * @retval None\r
213   */\r
214 void PWR_FastWakeUpCmd(FunctionalState NewState)\r
215 {\r
216   /* Check the parameters */\r
217   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
218 \r
219   *(__IO uint32_t *) CR_FWU_BB = (uint32_t)NewState;\r
220 }\r
221 \r
222 /**\r
223   * @brief  Enables or disables the Ultra Low Power mode.\r
224   * @param  NewState: new state of the Ultra Low Power mode.\r
225   *   This parameter can be: ENABLE or DISABLE.\r
226   * @retval None\r
227   */\r
228 void PWR_UltraLowPowerCmd(FunctionalState NewState)\r
229 {\r
230   /* Check the parameters */\r
231   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
232 \r
233   *(__IO uint32_t *) CR_ULP_BB = (uint32_t)NewState;\r
234 }\r
235 \r
236 /**\r
237   * @brief  Configures the voltage scaling range.\r
238   * @param  PWR_VoltageScaling: specifies the voltage scaling range.\r
239   *   This parameter can be:\r
240   *     @arg PWR_VoltageScaling_Range1: Voltage Scaling Range 1\r
241   *     @arg PWR_VoltageScaling_Range2: Voltage Scaling Range 2\r
242   *     @arg PWR_VoltageScaling_Range3: Voltage Scaling Range 3      \r
243   * @retval None\r
244   */\r
245 void PWR_VoltageScalingConfig(uint32_t PWR_VoltageScaling)\r
246 {\r
247   uint32_t tmp = 0;\r
248   \r
249   /* Check the parameters */\r
250   assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(PWR_VoltageScaling));\r
251   \r
252   tmp = PWR->CR;\r
253 \r
254   tmp &= CR_VOS_MASK;\r
255   tmp |= PWR_VoltageScaling;\r
256   \r
257   PWR->CR = tmp & 0xFFFFFFF3;\r
258 \r
259 }\r
260 \r
261 /**\r
262   * @brief  Enters/Exits the Low Power Run mode.\r
263   * @param  NewState: new state of the Low Power Run mode.\r
264   *   This parameter can be: ENABLE or DISABLE.\r
265   * @retval None\r
266   */\r
267 void PWR_EnterLowPowerRunMode(FunctionalState NewState)\r
268 {\r
269   /* Check the parameters */\r
270   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
271 \r
272   if (NewState != DISABLE)\r
273   {\r
274     PWR->CR |= PWR_CR_LPSDSR;\r
275     PWR->CR |= PWR_CR_LPRUN;     \r
276   }\r
277   else\r
278   {\r
279     PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_LPRUN); \r
280     PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_LPSDSR);  \r
281   }  \r
282 }\r
283 \r
284 /**\r
285   * @brief  Enters Sleep mode.\r
286   * @param  PWR_Regulator: specifies the regulator state in Sleep mode.\r
287   *   This parameter can be one of the following values:\r
288   *     @arg PWR_Regulator_ON: Sleep mode with regulator ON\r
289   *     @arg PWR_Regulator_LowPower: Sleep mode with regulator in low power mode\r
290   * @param  PWR_SLEEPEntry: specifies if SLEEP mode in entered with WFI or WFE instruction.\r
291   *   This parameter can be one of the following values:\r
292   *     @arg PWR_SLEEPEntry_WFI: enter SLEEP mode with WFI instruction\r
293   *     @arg PWR_SLEEPEntry_WFE: enter SLEEP mode with WFE instruction\r
294   * @retval None\r
295   */\r
296 void PWR_EnterSleepMode(uint32_t PWR_Regulator, uint8_t PWR_SLEEPEntry)\r
297 {\r
298   uint32_t tmpreg = 0;\r
299 \r
300   /* Check the parameters */\r
301   assert_param(IS_PWR_REGULATOR(PWR_Regulator));\r
302 \r
303   assert_param(IS_PWR_SLEEP_ENTRY(PWR_SLEEPEntry));\r
304   \r
305   /* Select the regulator state in Sleep mode ---------------------------------*/\r
306   tmpreg = PWR->CR;\r
307   \r
308   /* Clear PDDS and LPDSR bits */\r
309   tmpreg &= CR_DS_MASK;\r
310   \r
311   /* Set LPDSR bit according to PWR_Regulator value */\r
312   tmpreg |= PWR_Regulator;\r
313   \r
314   /* Store the new value */\r
315   PWR->CR = tmpreg;\r
316 \r
317   /* Clear SLEEPDEEP bit of Cortex System Control Register */\r
318   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);\r
319   \r
320   /* Select SLEEP mode entry -------------------------------------------------*/\r
321   if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)\r
322   {   \r
323     /* Request Wait For Interrupt */\r
324     __WFI();\r
325   }\r
326   else\r
327   {\r
328     /* Request Wait For Event */\r
329     __WFE();\r
330   }\r
331 }\r
332 \r
333 /**\r
334   * @brief  Enters STOP mode.\r
335   * @param  PWR_Regulator: specifies the regulator state in STOP mode.\r
336   *   This parameter can be one of the following values:\r
337   *     @arg PWR_Regulator_ON: STOP mode with regulator ON\r
338   *     @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode\r
339   * @param  PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.\r
340   *   This parameter can be one of the following values:\r
341   *     @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction\r
342   *     @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction\r
343   * @retval None\r
344   */\r
345 void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)\r
346 {\r
347   uint32_t tmpreg = 0;\r
348   \r
349   /* Check the parameters */\r
350   assert_param(IS_PWR_REGULATOR(PWR_Regulator));\r
351   assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));\r
352   \r
353   /* Select the regulator state in STOP mode ---------------------------------*/\r
354   tmpreg = PWR->CR;\r
355   /* Clear PDDS and LPDSR bits */\r
356   tmpreg &= CR_DS_MASK;\r
357   \r
358   /* Set LPDSR bit according to PWR_Regulator value */\r
359   tmpreg |= PWR_Regulator;\r
360   \r
361   /* Store the new value */\r
362   PWR->CR = tmpreg;\r
363   \r
364   /* Set SLEEPDEEP bit of Cortex System Control Register */\r
365   SCB->SCR |= SCB_SCR_SLEEPDEEP;\r
366   \r
367   /* Select STOP mode entry --------------------------------------------------*/\r
368   if(PWR_STOPEntry == PWR_STOPEntry_WFI)\r
369   {   \r
370     /* Request Wait For Interrupt */\r
371     __WFI();\r
372   }\r
373   else\r
374   {\r
375     /* Request Wait For Event */\r
376     __WFE();\r
377   }\r
378   /* Reset SLEEPDEEP bit of Cortex System Control Register */\r
379   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);  \r
380 }\r
381 \r
382 /**\r
383   * @brief  Enters STANDBY mode.\r
384   * @param  None\r
385   * @retval None\r
386   */\r
387 void PWR_EnterSTANDBYMode(void)\r
388 {\r
389   /* Clear Wake-up flag */\r
390   PWR->CR |= PWR_CR_CWUF;\r
391   \r
392   /* Select STANDBY mode */\r
393   PWR->CR |= PWR_CR_PDDS;\r
394   \r
395   /* Set SLEEPDEEP bit of Cortex System Control Register */\r
396   SCB->SCR |= SCB_SCR_SLEEPDEEP;\r
397   \r
398 /* This option is used to ensure that store operations are completed */\r
399 #if defined ( __CC_ARM   )\r
400   __force_stores();\r
401 #endif\r
402   /* Request Wait For Interrupt */\r
403   __WFI();\r
404 }\r
405 \r
406 /**\r
407   * @brief  Checks whether the specified PWR flag is set or not.\r
408   * @param  PWR_FLAG: specifies the flag to check.\r
409   *   This parameter can be one of the following values:\r
410   *     @arg PWR_FLAG_WU: Wake Up flag\r
411   *     @arg PWR_FLAG_SB: StandBy flag\r
412   *     @arg PWR_FLAG_PVDO: PVD Output\r
413   *     @arg PWR_FLAG_VREFINTRDY: Internal Voltage Reference Ready flag\r
414   *     @arg PWR_FLAG_VOS: Voltage Scaling select flag\r
415   *     @arg PWR_FLAG_REGLP: Regulator LP flag        \r
416   * @retval The new state of PWR_FLAG (SET or RESET).\r
417   */\r
418 FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)\r
419 {\r
420   FlagStatus bitstatus = RESET;\r
421   /* Check the parameters */\r
422   assert_param(IS_PWR_GET_FLAG(PWR_FLAG));\r
423   \r
424   if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET)\r
425   {\r
426     bitstatus = SET;\r
427   }\r
428   else\r
429   {\r
430     bitstatus = RESET;\r
431   }\r
432   /* Return the flag status */\r
433   return bitstatus;\r
434 }\r
435 \r
436 /**\r
437   * @brief  Clears the PWR's pending flags.\r
438   * @param  PWR_FLAG: specifies the flag to clear.\r
439   *   This parameter can be one of the following values:\r
440   *     @arg PWR_FLAG_WU: Wake Up flag\r
441   *     @arg PWR_FLAG_SB: StandBy flag\r
442   * @retval None\r
443   */\r
444 void PWR_ClearFlag(uint32_t PWR_FLAG)\r
445 {\r
446   /* Check the parameters */\r
447   assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG));\r
448          \r
449   PWR->CR |=  PWR_FLAG << 2;\r
450 }\r
451 \r
452 /**\r
453   * @}\r
454   */\r
455 \r
456 /**\r
457   * @}\r
458   */\r
459 \r
460 /**\r
461   * @}\r
462   */\r
463 \r
464 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r