]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Keil/STM32F10xFWLib/src/stm32f10x_pwr.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_STM32F103_Keil / STM32F10xFWLib / src / stm32f10x_pwr.c
1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************\r
2 * File Name          : stm32f10x_pwr.c\r
3 * Author             : MCD Application Team\r
4 * Date First Issued  : 09/29/2006\r
5 * Description        : This file provides all the PWR firmware functions.\r
6 ********************************************************************************\r
7 * History:\r
8 * 04/02/2007: V0.2\r
9 * 02/05/2007: V0.1\r
10 * 09/29/2006: V0.01\r
11 ********************************************************************************\r
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.\r
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,\r
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE\r
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING\r
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
18 *******************************************************************************/\r
19 \r
20 /* Includes ------------------------------------------------------------------*/\r
21 #include "stm32f10x_pwr.h"\r
22 #include "stm32f10x_rcc.h"\r
23 \r
24 /* Private typedef -----------------------------------------------------------*/\r
25 /* Private define ------------------------------------------------------------*/\r
26 /* --------- PWR registers bit address in the alias region ---------- */\r
27 #define PWR_OFFSET               (PWR_BASE - PERIPH_BASE)\r
28 \r
29 /* --- CR Register ---*/\r
30 /* Alias word address of DBP bit */\r
31 #define CR_OFFSET                (PWR_OFFSET + 0x00)\r
32 #define DBP_BitNumber            0x08\r
33 #define CR_DBP_BB                (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))\r
34 \r
35 /* Alias word address of PVDE bit */\r
36 #define PVDE_BitNumber           0x04\r
37 #define CR_PVDE_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PVDE_BitNumber * 4))\r
38 \r
39 /* --- CSR Register ---*/\r
40 /* Alias word address of EWUP bit */\r
41 #define CSR_OFFSET               (PWR_OFFSET + 0x04)\r
42 #define EWUP_BitNumber           0x08\r
43 #define CSR_EWUP_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4))\r
44 \r
45 /* ------------------ PWR registers bit mask ------------------------ */\r
46 /* CR register bit mask */\r
47 #define CR_PDDS_Set              ((u32)0x00000002)\r
48 #define CR_DS_Mask               ((u32)0xFFFFFFFC)\r
49 #define CR_CWUF_Set              ((u32)0x00000004)\r
50 #define CR_PLS_Mask              ((u32)0xFFFFFF1F)\r
51 \r
52 /* --------- Cortex System Control register bit mask ---------------- */\r
53 /* Cortex System Control register address */\r
54 #define SCB_SysCtrl              ((u32)0xE000ED10)\r
55 /* SLEEPDEEP bit mask */\r
56 #define SysCtrl_SLEEPDEEP_Set    ((u32)0x00000004)\r
57 \r
58 /* Private macro -------------------------------------------------------------*/\r
59 /* Private variables ---------------------------------------------------------*/\r
60 /* Private function prototypes -----------------------------------------------*/\r
61 /* Private functions ---------------------------------------------------------*/\r
62 \r
63 /*******************************************************************************\r
64 * Function Name  : PWR_DeInit\r
65 * Description    : Deinitializes the PWR peripheral registers to their default\r
66 *                  reset values.\r
67 * Input          : None\r
68 * Output         : None\r
69 * Return         : None\r
70 *******************************************************************************/\r
71 void PWR_DeInit(void)\r
72 {\r
73   RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);\r
74   RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);\r
75 }\r
76 \r
77 /*******************************************************************************\r
78 * Function Name  : PWR_BackupAccessCmd\r
79 * Description    : Enables or disables access to the RTC and backup registers.\r
80 * Input          : - NewState: new state of the access to the RTC and backup\r
81 *                    registers. This parameter can be: ENABLE or DISABLE.\r
82 * Output         : None\r
83 * Return         : None\r
84 *******************************************************************************/\r
85 void PWR_BackupAccessCmd(FunctionalState NewState)\r
86 {\r
87   /* Check the parameters */\r
88   assert(IS_FUNCTIONAL_STATE(NewState));\r
89 \r
90   *(vu32 *) CR_DBP_BB = (u32)NewState;\r
91 }\r
92 \r
93 /*******************************************************************************\r
94 * Function Name  : PWR_PVDCmd\r
95 * Description    : Enables or disables the Power Voltage Detector(PVD).\r
96 * Input          : - NewState: new state of the PVD.\r
97 *                    This parameter can be: ENABLE or DISABLE.\r
98 * Output         : None\r
99 * Return         : None\r
100 *******************************************************************************/\r
101 void PWR_PVDCmd(FunctionalState NewState)\r
102 {\r
103   /* Check the parameters */\r
104   assert(IS_FUNCTIONAL_STATE(NewState));\r
105 \r
106   *(vu32 *) CR_PVDE_BB = (u32)NewState;\r
107 }\r
108 \r
109 /*******************************************************************************\r
110 * Function Name  : PWR_PVDLevelConfig\r
111 * Description    : Configures the value detected by the Power Voltage Detector(PVD).\r
112 * Input          : - PWR_PVDLevel: specifies the PVD detection level\r
113 *                    This parameter can be one of the following values:\r
114 *                       - PWR_PVDLevel_2V2: PVD detection level set to 2.2V\r
115 *                       - PWR_PVDLevel_2V3: PVD detection level set to 2.3V\r
116 *                       - PWR_PVDLevel_2V4: PVD detection level set to 2.4V\r
117 *                       - PWR_PVDLevel_2V5: PVD detection level set to 2.5V\r
118 *                       - PWR_PVDLevel_2V6: PVD detection level set to 2.6V\r
119 *                       - PWR_PVDLevel_2V7: PVD detection level set to 2.7V\r
120 *                       - PWR_PVDLevel_2V8: PVD detection level set to 2.8V\r
121 *                       - PWR_PVDLevel_2V9: PVD detection level set to 2.9V\r
122 * Output         : None\r
123 * Return         : None\r
124 *******************************************************************************/\r
125 void PWR_PVDLevelConfig(u32 PWR_PVDLevel)\r
126 {\r
127   u32 tmpreg = 0;\r
128 \r
129   /* Check the parameters */\r
130   assert(IS_PWR_PVD_LEVEL(PWR_PVDLevel));\r
131 \r
132   tmpreg = PWR->CR;\r
133 \r
134   /* Clear PLS[7:5] bits */\r
135   tmpreg &= CR_PLS_Mask;\r
136 \r
137   /* Set PLS[7:5] bits according to PWR_PVDLevel value */\r
138   tmpreg |= PWR_PVDLevel;\r
139 \r
140   /* Store the new value */\r
141   PWR->CR = tmpreg;\r
142 }\r
143 \r
144 /*******************************************************************************\r
145 * Function Name  : PWR_WakeUpPinCmd\r
146 * Description    : Enables or disables the WakeUp Pin functionality.\r
147 * Input          : - NewState: new state of the WakeUp Pin functionality.\r
148 *                    This parameter can be: ENABLE or DISABLE.\r
149 * Output         : None\r
150 * Return         : None\r
151 *******************************************************************************/\r
152 void PWR_WakeUpPinCmd(FunctionalState NewState)\r
153 {\r
154   /* Check the parameters */\r
155   assert(IS_FUNCTIONAL_STATE(NewState));\r
156 \r
157   *(vu32 *) CSR_EWUP_BB = (u32)NewState;\r
158 }\r
159 \r
160 /*******************************************************************************\r
161 * Function Name  : PWR_EnterSTOPMode\r
162 * Description    : Enters STOP mode.\r
163 * Input          : - PWR_Regulator: specifies the regulator state in STOP mode.\r
164 *                    This parameter can be one of the following values:\r
165 *                       - PWR_Regulator_ON: STOP mode with regulator ON\r
166 *                       - PWR_Regulator_LowPower: STOP mode with\r
167 *                         regulator in low power mode\r
168 *                  - PWR_STOPEntry: specifies if STOP mode in entered with WFI or \r
169 *                    WFE instruction.\r
170 *                    This parameter can be one of the following values:\r
171 *                       - PWR_STOPEntry_WFI: enter STOP mode with WFI instruction\r
172 *                       - PWR_STOPEntry_WFE: enter STOP mode with WFE instruction\r
173 * Output         : None\r
174 * Return         : None\r
175 *******************************************************************************/\r
176 void PWR_EnterSTOPMode(u32 PWR_Regulator, u8 PWR_STOPEntry)\r
177 {\r
178   u32 tmpreg = 0;\r
179 \r
180   /* Check the parameters */\r
181   assert(IS_PWR_REGULATOR(PWR_Regulator));\r
182   assert(IS_PWR_STOP_ENTRY(PWR_STOPEntry));\r
183   \r
184   /* Select the regulator state in STOP mode ---------------------------------*/\r
185   tmpreg = PWR->CR;\r
186 \r
187   /* Clear PDDS and LPDS bits */\r
188   tmpreg &= CR_DS_Mask;\r
189 \r
190   /* Set LPDS bit according to PWR_Regulator value */\r
191   tmpreg |= PWR_Regulator;\r
192 \r
193   /* Store the new value */\r
194   PWR->CR = tmpreg;\r
195 \r
196   /* Set SLEEPDEEP bit of Cortex System Control Register */\r
197   *(vu32 *) SCB_SysCtrl |= SysCtrl_SLEEPDEEP_Set;\r
198   \r
199   /* Select STOP mode entry --------------------------------------------------*/\r
200   if(PWR_STOPEntry == PWR_STOPEntry_WFI)\r
201   {   \r
202     /* Request Wait For Interrupt */\r
203     __WFI();\r
204   }\r
205   else\r
206   {\r
207     /* Request Wait For Event */\r
208     __WFE();\r
209   }\r
210 }\r
211 \r
212 /*******************************************************************************\r
213 * Function Name  : PWR_EnterSTANDBYMode\r
214 * Description    : Enters STANDBY mode.\r
215 * Input          : None\r
216 * Output         : None\r
217 * Return         : None\r
218 *******************************************************************************/\r
219 void PWR_EnterSTANDBYMode(void)\r
220 {\r
221   /* Clear Wake-up flag */\r
222   PWR->CR |= CR_CWUF_Set;\r
223 \r
224   /* Select STANDBY mode */\r
225   PWR->CR |= CR_PDDS_Set;\r
226 \r
227   /* Set SLEEPDEEP bit of Cortex System Control Register */\r
228   *(vu32 *) SCB_SysCtrl |= SysCtrl_SLEEPDEEP_Set;\r
229 \r
230   /* Request Wait For Interrupt */\r
231   __WFI();\r
232 }\r
233 \r
234 /*******************************************************************************\r
235 * Function Name  : PWR_GetFlagStatus\r
236 * Description    : Checks whether the specified PWR flag is set or not.\r
237 * Input          : - PWR_FLAG: specifies the flag to check.\r
238 *                    This parameter can be one of the following values:\r
239 *                       - PWR_FLAG_WU: Wake Up flag\r
240 *                       - PWR_FLAG_SB: StandBy flag\r
241 *                       - PWR_FLAG_PVDO: PVD Output\r
242 * Output         : None\r
243 * Return         : The new state of PWR_FLAG (SET or RESET).\r
244 *******************************************************************************/\r
245 FlagStatus PWR_GetFlagStatus(u32 PWR_FLAG)\r
246 {\r
247   FlagStatus bitstatus = RESET;\r
248 \r
249   /* Check the parameters */\r
250   assert(IS_PWR_GET_FLAG(PWR_FLAG));\r
251   \r
252   if ((PWR->CSR & PWR_FLAG) != (u32)RESET)\r
253   {\r
254     bitstatus = SET;\r
255   }\r
256   else\r
257   {\r
258     bitstatus = RESET;\r
259   }\r
260 \r
261   /* Return the flag status */\r
262   return bitstatus;\r
263 }\r
264 \r
265 /*******************************************************************************\r
266 * Function Name  : PWR_ClearFlag\r
267 * Description    : Clears the PWR's pending flags.\r
268 * Input          : - PWR_FLAG: specifies the flag to clear.\r
269 *                    This parameter can be one of the following values:\r
270 *                       - PWR_FLAG_WU: Wake Up flag\r
271 *                       - PWR_FLAG_SB: StandBy flag\r
272 * Output         : None\r
273 * Return         : None\r
274 *******************************************************************************/\r
275 void PWR_ClearFlag(u32 PWR_FLAG)\r
276 {\r
277   /* Check the parameters */\r
278   assert(IS_PWR_CLEAR_FLAG(PWR_FLAG));\r
279          \r
280   PWR->CR |=  PWR_FLAG << 2;\r
281 }\r
282 \r
283 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/\r