Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/IWDG/IWDG_Reset/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 IWDG/IWDG_Reset/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>© 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 IWDG_Reset | |
31 * @{ | |
32 */ | |
33 | |
34 /* Private typedef -----------------------------------------------------------*/ | |
35 /* Private define ------------------------------------------------------------*/ | |
36 /* Uncomment/Comment depending on your STM32 device. | |
37 The LSI is internally connected to TIM5 IC4 only on STM32F10x Connectivity | |
38 line, High-Density Value line, High-Density and XL-Density Devices */ | |
39 #define LSI_TIM_MEASURE | |
40 | |
41 /* Private macro -------------------------------------------------------------*/ | |
42 /* Private variables ---------------------------------------------------------*/ | |
43 __IO uint32_t TimingDelay = 0; | |
44 __IO uint32_t LsiFreq = 40000; | |
45 extern __IO uint16_t CaptureNumber; | |
46 | |
47 /* Private function prototypes -----------------------------------------------*/ | |
48 void Delay(__IO uint32_t nTime); | |
49 void TIM5_ConfigForLSI(void); | |
50 | |
51 /* Private functions ---------------------------------------------------------*/ | |
52 | |
53 /** | |
54 * @brief Main program. | |
55 * @param None | |
56 * @retval None | |
57 */ | |
58 int main(void) | |
59 { | |
60 /*!< At this stage the microcontroller clock setting is already configured, | |
61 this is done through SystemInit() function which is called from startup | |
62 file (startup_stm32f10x_xx.s) before to branch to application main. | |
63 To reconfigure the default setting of SystemInit() function, refer to | |
64 system_stm32f10x.c file | |
65 */ | |
66 | |
67 /* Initialize LED1 and Key Button mounted on STM3210X-EVAL board */ | |
68 STM_EVAL_LEDInit(LED1); | |
69 STM_EVAL_LEDInit(LED2); | |
70 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); | |
71 | |
72 /* Setup SysTick Timer for 1 msec interrupts */ | |
73 if (SysTick_Config(SystemCoreClock / 1000)) | |
74 { | |
75 /* Capture error */ | |
76 while (1); | |
77 } | |
78 | |
79 | |
80 /* Check if the system has resumed from IWDG reset */ | |
81 if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) | |
82 { | |
83 /* IWDGRST flag set */ | |
84 /* Turn on LED1 */ | |
85 STM_EVAL_LEDOn(LED1); | |
86 | |
87 /* Clear reset flags */ | |
88 RCC_ClearFlag(); | |
89 } | |
90 else | |
91 { | |
92 /* IWDGRST flag is not set */ | |
93 /* Turn off LED1 */ | |
94 STM_EVAL_LEDOff(LED1); | |
95 } | |
96 | |
97 #ifdef LSI_TIM_MEASURE | |
98 /* Enable the LSI OSC */ | |
99 RCC_LSICmd(ENABLE); | |
100 | |
101 /* Wait till LSI is ready */ | |
102 while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) | |
103 {} | |
104 | |
105 /* TIM Configuration -------------------------------------------------------*/ | |
106 TIM5_ConfigForLSI(); | |
107 | |
108 /* Wait until the TIM5 get 2 LSI edges */ | |
109 while(CaptureNumber != 2) | |
110 { | |
111 } | |
112 | |
113 /* Disable TIM5 CC4 Interrupt Request */ | |
114 TIM_ITConfig(TIM5, TIM_IT_CC4, DISABLE); | |
115 #endif | |
116 | |
117 /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency | |
118 dispersion) */ | |
119 /* Enable write access to IWDG_PR and IWDG_RLR registers */ | |
120 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); | |
121 | |
122 /* IWDG counter clock: LSI/32 */ | |
123 IWDG_SetPrescaler(IWDG_Prescaler_32); | |
124 | |
125 /* Set counter reload value to obtain 250ms IWDG TimeOut. | |
126 Counter Reload Value = 250ms/IWDG counter clock period | |
127 = 250ms / (LSI/32) | |
128 = 0.25s / (LsiFreq/32) | |
129 = LsiFreq/(32 * 4) | |
130 = LsiFreq/128 | |
131 */ | |
132 IWDG_SetReload(LsiFreq/128); | |
133 | |
134 /* Reload IWDG counter */ | |
135 IWDG_ReloadCounter(); | |
136 | |
137 /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ | |
138 IWDG_Enable(); | |
139 | |
140 while (1) | |
141 { | |
142 /* Toggle LED2 */ | |
143 STM_EVAL_LEDToggle(LED2); | |
144 | |
145 /* Insert 240 ms delay */ | |
146 Delay(240); | |
147 | |
148 /* Reload IWDG counter */ | |
149 IWDG_ReloadCounter(); | |
150 } | |
151 } | |
152 | |
153 /** | |
154 * @brief Inserts a delay time. | |
155 * @param nTime: specifies the delay time length, in milliseconds. | |
156 * @retval None | |
157 */ | |
158 void Delay(__IO uint32_t nTime) | |
159 { | |
160 TimingDelay = nTime; | |
161 | |
162 while(TimingDelay != 0); | |
163 } | |
164 | |
165 #ifdef LSI_TIM_MEASURE | |
166 /** | |
167 * @brief Configures TIM5 to measure the LSI oscillator frequency. | |
168 * @param None | |
169 * @retval None | |
170 */ | |
171 void TIM5_ConfigForLSI(void) | |
172 { | |
173 NVIC_InitTypeDef NVIC_InitStructure; | |
174 TIM_ICInitTypeDef TIM_ICInitStructure; | |
175 | |
176 /* Enable TIM5 clocks */ | |
177 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); | |
178 | |
179 /* Enable the TIM5 Interrupt */ | |
180 NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; | |
181 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; | |
182 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; | |
183 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; | |
184 NVIC_Init(&NVIC_InitStructure); | |
185 | |
186 /* Configure TIM5 prescaler */ | |
187 TIM_PrescalerConfig(TIM5, 0, TIM_PSCReloadMode_Immediate); | |
188 | |
189 /* Connect internally the TM5_CH4 Input Capture to the LSI clock output */ | |
190 GPIO_PinRemapConfig(GPIO_Remap_TIM5CH4_LSI, ENABLE); | |
191 | |
192 /* TIM5 configuration: Input Capture mode --------------------- | |
193 The LSI oscillator is connected to TIM5 CH4 | |
194 The Rising edge is used as active edge, | |
195 The TIM5 CCR4 is used to compute the frequency value | |
196 ------------------------------------------------------------ */ | |
197 TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; | |
198 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; | |
199 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; | |
200 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; | |
201 TIM_ICInitStructure.TIM_ICFilter = 0; | |
202 TIM_ICInit(TIM5, &TIM_ICInitStructure); | |
203 | |
204 /* TIM10 Counter Enable */ | |
205 TIM_Cmd(TIM5, ENABLE); | |
206 | |
207 /* Reset the flags */ | |
208 TIM5->SR = 0; | |
209 | |
210 /* Enable the CC4 Interrupt Request */ | |
211 TIM_ITConfig(TIM5, TIM_IT_CC4, ENABLE); | |
212 } | |
213 #endif | |
214 | |
215 #ifdef USE_FULL_ASSERT | |
216 | |
217 /** | |
218 * @brief Reports the name of the source file and the source line number | |
219 * where the assert_param error has occurred. | |
220 * @param file: pointer to the source file name | |
221 * @param line: assert_param error line source number | |
222 * @retval None | |
223 */ | |
224 void assert_failed(uint8_t* file, uint32_t line) | |
225 { | |
226 /* User can add his own implementation to report the file name and line number, | |
227 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
228 | |
229 /* Infinite loop */ | |
230 while (1) | |
231 {} | |
232 } | |
233 | |
234 #endif | |
235 | |
236 /** | |
237 * @} | |
238 */ | |
239 | |
240 /** | |
241 * @} | |
242 */ | |
243 | |
244 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |