comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/PWR/STOP/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file PWR/STOP/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body.
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24 #include "stm32_eval.h"
25
26 /** @addtogroup STM32F10x_StdPeriph_Examples
27 * @{
28 */
29
30 /** @addtogroup PWR_STOP
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 /* Private define ------------------------------------------------------------*/
36 /* Private macro -------------------------------------------------------------*/
37 /* Private variables ---------------------------------------------------------*/
38 extern __IO uint32_t TimingDelay;
39 ErrorStatus HSEStartUpStatus;
40
41 /* Private function prototypes -----------------------------------------------*/
42 void SYSCLKConfig_STOP(void);
43 void EXTI_Configuration(void);
44 void RTC_Configuration(void);
45 void NVIC_Configuration(void);
46 void SysTick_Configuration(void);
47 void Delay(__IO uint32_t nTime);
48
49 /* Private functions ---------------------------------------------------------*/
50
51 /**
52 * @brief Main program.
53 * @param None
54 * @retval None
55 */
56 int main(void)
57 {
58 /*!< At this stage the microcontroller clock setting is already configured,
59 this is done through SystemInit() function which is called from startup
60 file (startup_stm32f10x_xx.s) before to branch to application main.
61 To reconfigure the default setting of SystemInit() function, refer to
62 system_stm32f10x.c file
63 */
64
65 /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */
66 STM_EVAL_LEDInit(LED1);
67 STM_EVAL_LEDInit(LED2);
68 STM_EVAL_LEDInit(LED3);
69 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
70
71 /* Enable PWR and BKP clock */
72 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
73
74 /* Configure EXTI Line to generate an interrupt on falling edge */
75 EXTI_Configuration();
76
77 /* Configure RTC clock source and prescaler */
78 RTC_Configuration();
79
80 /* NVIC configuration */
81 NVIC_Configuration();
82
83 /* Configure the SysTick to generate an interrupt each 1 millisecond */
84 SysTick_Configuration();
85
86 /* Turn on LED1 */
87 STM_EVAL_LEDOn(LED1);
88
89 while (1)
90 {
91 /* Insert 1.5 second delay */
92 Delay(1500);
93
94 /* Wait till RTC Second event occurs */
95 RTC_ClearFlag(RTC_FLAG_SEC);
96 while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);
97
98 /* Alarm in 3 second */
99 RTC_SetAlarm(RTC_GetCounter()+ 3);
100 /* Wait until last write operation on RTC registers has finished */
101 RTC_WaitForLastTask();
102
103 /* Turn off LED1 */
104 STM_EVAL_LEDOff(LED1);
105
106 /* Request to enter STOP mode with regulator in low power mode*/
107 PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
108
109 /* At this stage the system has resumed from STOP mode -------------------*/
110 /* Turn on LED1 */
111 STM_EVAL_LEDOn(LED1);
112
113 /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
114 PLL as system clock source (HSE and PLL are disabled in STOP mode) */
115 SYSCLKConfig_STOP();
116 }
117 }
118
119 /**
120 * @brief Configures system clock after wake-up from STOP: enable HSE, PLL
121 * and select PLL as system clock source.
122 * @param None
123 * @retval None
124 */
125 void SYSCLKConfig_STOP(void)
126 {
127 /* Enable HSE */
128 RCC_HSEConfig(RCC_HSE_ON);
129
130 /* Wait till HSE is ready */
131 HSEStartUpStatus = RCC_WaitForHSEStartUp();
132
133 if(HSEStartUpStatus == SUCCESS)
134 {
135
136 #ifdef STM32F10X_CL
137 /* Enable PLL2 */
138 RCC_PLL2Cmd(ENABLE);
139
140 /* Wait till PLL2 is ready */
141 while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
142 {
143 }
144
145 #endif
146
147 /* Enable PLL */
148 RCC_PLLCmd(ENABLE);
149
150 /* Wait till PLL is ready */
151 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
152 {
153 }
154
155 /* Select PLL as system clock source */
156 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
157
158 /* Wait till PLL is used as system clock source */
159 while(RCC_GetSYSCLKSource() != 0x08)
160 {
161 }
162 }
163 }
164
165 /**
166 * @brief Configures EXTI Lines.
167 * @param None
168 * @retval None
169 */
170 void EXTI_Configuration(void)
171 {
172 EXTI_InitTypeDef EXTI_InitStructure;
173
174 /* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
175 EXTI_ClearITPendingBit(EXTI_Line17);
176 EXTI_InitStructure.EXTI_Line = EXTI_Line17;
177 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
178 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
179 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
180 EXTI_Init(&EXTI_InitStructure);
181 }
182
183 /**
184 * @brief Configures RTC clock source and prescaler.
185 * @param None
186 * @retval None
187 */
188 void RTC_Configuration(void)
189 {
190 /* RTC clock source configuration ------------------------------------------*/
191 /* Allow access to BKP Domain */
192 PWR_BackupAccessCmd(ENABLE);
193
194 /* Reset Backup Domain */
195 BKP_DeInit();
196
197 /* Enable the LSE OSC */
198 RCC_LSEConfig(RCC_LSE_ON);
199 /* Wait till LSE is ready */
200 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
201 {
202 }
203
204 /* Select the RTC Clock Source */
205 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
206
207 /* Enable the RTC Clock */
208 RCC_RTCCLKCmd(ENABLE);
209
210 /* RTC configuration -------------------------------------------------------*/
211 /* Wait for RTC APB registers synchronisation */
212 RTC_WaitForSynchro();
213
214 /* Set the RTC time base to 1s */
215 RTC_SetPrescaler(32767);
216 /* Wait until last write operation on RTC registers has finished */
217 RTC_WaitForLastTask();
218
219 /* Enable the RTC Alarm interrupt */
220 RTC_ITConfig(RTC_IT_ALR, ENABLE);
221 /* Wait until last write operation on RTC registers has finished */
222 RTC_WaitForLastTask();
223 }
224
225 /**
226 * @brief Configures NVIC and Vector Table base location.
227 * @param None
228 * @retval None
229 */
230 void NVIC_Configuration(void)
231 {
232 NVIC_InitTypeDef NVIC_InitStructure;
233
234 /* 2 bits for Preemption Priority and 2 bits for Sub Priority */
235 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
236
237 NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
238 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
239 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
240 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
241 NVIC_Init(&NVIC_InitStructure);
242 }
243
244 /**
245 * @brief Configures the SysTick to generate an interrupt each 1 millisecond.
246 * @param None
247 * @retval None
248 */
249 void SysTick_Configuration(void)
250 {
251 /* Setup SysTick Timer for 1 msec interrupts */
252 if (SysTick_Config(SystemCoreClock / 1000))
253 {
254 /* Capture error */
255 while (1);
256 }
257 /* Set SysTick Priority to 3 */
258 NVIC_SetPriority(SysTick_IRQn, 0x0C);
259 }
260
261 /**
262 * @brief Inserts a delay time.
263 * @param nTime: specifies the delay time length, in milliseconds.
264 * @retval None
265 */
266 void Delay(__IO uint32_t nTime)
267 {
268 TimingDelay = nTime;
269
270 while(TimingDelay != 0);
271
272 }
273
274 #ifdef USE_FULL_ASSERT
275
276 /**
277 * @brief Reports the name of the source file and the source line number
278 * where the assert_param error has occurred.
279 * @param file: pointer to the source file name
280 * @param line: assert_param error line source number
281 * @retval None
282 */
283 void assert_failed(uint8_t* file, uint32_t line)
284 {
285 /* User can add his own implementation to report the file name and line number,
286 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
287
288 /* Infinite loop */
289 while (1)
290 {
291 }
292 }
293
294 #endif
295
296 /**
297 * @}
298 */
299
300 /**
301 * @}
302 */
303
304 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/