STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/IWDG/IWDG_Reset/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file IWDG/IWDG_Reset/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup IWDG_Reset 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 __IO uint32_t TimingDelay = 0; 00044 __IO uint32_t LsiFreq = 40000; 00045 extern __IO uint16_t CaptureNumber; 00046 00047 /* Private function prototypes -----------------------------------------------*/ 00048 static void TIM14_ConfigForLSI(void); 00049 void Delay(__IO uint32_t nTime); 00050 00051 /* Private functions ---------------------------------------------------------*/ 00052 00053 /** 00054 * @brief Main program. 00055 * @param None 00056 * @retval None 00057 */ 00058 int main(void) 00059 { 00060 /*!< At this stage the microcontroller clock setting is already configured, 00061 this is done through SystemInit() function which is called from startup 00062 file (startup_stm32f0xx.s) before to branch to application main. 00063 To reconfigure the default setting of SystemInit() function, refer to 00064 system_stm32f0xx.c file 00065 */ 00066 /* Initialize LED and Tamper Button mounted on the Eval board */ 00067 STM_EVAL_LEDInit(LED1); 00068 STM_EVAL_LEDInit(LED2); 00069 STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI); 00070 00071 /* Setup SysTick Timer for 1 msec interrupts */ 00072 if (SysTick_Config(SystemCoreClock / 1000)) 00073 { 00074 /* Capture error */ 00075 while (1); 00076 } 00077 00078 00079 /* Check if the system has resumed from IWDG reset */ 00080 if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) 00081 { 00082 /* IWDGRST flag set */ 00083 /* Turn on LED1 */ 00084 STM_EVAL_LEDOn(LED1); 00085 00086 /* Clear reset flags */ 00087 RCC_ClearFlag(); 00088 } 00089 else 00090 { 00091 /* IWDGRST flag is not set */ 00092 /* Turn off LED1 */ 00093 STM_EVAL_LEDOff(LED1); 00094 } 00095 00096 #ifdef LSI_TIM_MEASURE 00097 /* TIM Configuration -------------------------------------------------------*/ 00098 TIM14_ConfigForLSI(); 00099 00100 /* Wait until the TIM14 get 2 LSI edges */ 00101 while(CaptureNumber != 2) 00102 { 00103 } 00104 00105 /* Disable TIM14 CC1 Interrupt Request */ 00106 TIM_ITConfig(TIM14, TIM_IT_CC1, DISABLE); 00107 #endif /* LSI_TIM_MEASURE */ 00108 00109 /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency 00110 dispersion) */ 00111 /* Enable write access to IWDG_PR and IWDG_RLR registers */ 00112 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); 00113 00114 /* IWDG counter clock: LSI/32 */ 00115 IWDG_SetPrescaler(IWDG_Prescaler_32); 00116 00117 /* Set counter reload value to obtain 250ms IWDG TimeOut. 00118 Counter Reload Value = 250ms/IWDG counter clock period 00119 = 250ms / (LSI/32) 00120 = 0.25s / (LsiFreq/32) 00121 = LsiFreq/(32 * 4) 00122 = LsiFreq/128 00123 */ 00124 IWDG_SetReload(LsiFreq/128); 00125 00126 /* Reload IWDG counter */ 00127 IWDG_ReloadCounter(); 00128 00129 /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ 00130 IWDG_Enable(); 00131 00132 while (1) 00133 { 00134 /* Toggle LED2 */ 00135 STM_EVAL_LEDToggle(LED2); 00136 00137 /* Insert 220 ms delay */ 00138 Delay(220); 00139 00140 /* Reload IWDG counter */ 00141 IWDG_ReloadCounter(); 00142 } 00143 } 00144 00145 #ifdef LSI_TIM_MEASURE 00146 /** 00147 * @brief Configures TIM14 to measure the LSI oscillator frequency. 00148 * @param None 00149 * @retval None 00150 */ 00151 static void TIM14_ConfigForLSI(void) 00152 { 00153 NVIC_InitTypeDef NVIC_InitStructure; 00154 TIM_ICInitTypeDef TIM_ICInitStructure; 00155 00156 /* Enable peripheral clocks ------------------------------------------------*/ 00157 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 00158 00159 /* Allow access to the RTC */ 00160 PWR_BackupAccessCmd(ENABLE); 00161 00162 /* Reset RTC Domain */ 00163 RCC_BackupResetCmd(ENABLE); 00164 RCC_BackupResetCmd(DISABLE); 00165 00166 /*!< LSI Enable */ 00167 RCC_LSICmd(ENABLE); 00168 00169 /*!< Wait till LSI is ready */ 00170 while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) 00171 {} 00172 00173 /* Select the RTC Clock Source */ 00174 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); 00175 00176 /* Enable the RTC Clock */ 00177 RCC_RTCCLKCmd(ENABLE); 00178 00179 /* Wait for RTC APB registers synchronisation */ 00180 RTC_WaitForSynchro(); 00181 00182 /* Enable TIM14 clocks */ 00183 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); 00184 00185 /* Enable the TIM14 Interrupt */ 00186 NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn; 00187 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00188 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00189 NVIC_Init(&NVIC_InitStructure); 00190 00191 /* Configure TIM14 prescaler */ 00192 TIM_PrescalerConfig(TIM14, 0, TIM_PSCReloadMode_Immediate); 00193 00194 /* Connect internally the TM14_CH1 Input Capture to the LSI clock output */ 00195 TIM_RemapConfig(TIM14, TIM14_RTC_CLK); 00196 00197 /* TIM14 configuration: Input Capture mode --------------------- 00198 The LSI oscillator is connected to TIM14 CH1 00199 The Rising edge is used as active edge, 00200 The TIM14 CCR1 is used to compute the frequency value 00201 ------------------------------------------------------------ */ 00202 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; 00203 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; 00204 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 00205 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; 00206 TIM_ICInitStructure.TIM_ICFilter = 0; 00207 TIM_ICInit(TIM14, &TIM_ICInitStructure); 00208 00209 /* TIM14 Counter Enable */ 00210 TIM_Cmd(TIM14, ENABLE); 00211 00212 /* Reset the flags */ 00213 TIM14->SR = 0; 00214 00215 /* Enable the CC1 Interrupt Request */ 00216 TIM_ITConfig(TIM14, TIM_IT_CC1, ENABLE); 00217 } 00218 #endif /* LSI_TIM_MEASURE */ 00219 00220 /** 00221 * @brief Inserts a delay time. 00222 * @param nTime: specifies the delay time length, in 10 ms. 00223 * @retval None 00224 */ 00225 void Delay(__IO uint32_t nTime) 00226 { 00227 TimingDelay = nTime; 00228 00229 while(TimingDelay != 0); 00230 } 00231 00232 /** 00233 * @brief Decrements the TimingDelay variable. 00234 * @param None 00235 * @retval None 00236 */ 00237 void TimingDelay_Decrement(void) 00238 { 00239 if (TimingDelay != 0x00) 00240 { 00241 TimingDelay--; 00242 } 00243 } 00244 00245 #ifdef USE_FULL_ASSERT 00246 00247 /** 00248 * @brief Reports the name of the source file and the source line number 00249 * where the assert_param error has occurred. 00250 * @param file: pointer to the source file name 00251 * @param line: assert_param error line source number 00252 * @retval None 00253 */ 00254 void assert_failed(uint8_t* file, uint32_t line) 00255 { 00256 /* User can add his own implementation to report the file name and line number, 00257 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00258 00259 /* Infinite loop */ 00260 while (1) 00261 { 00262 } 00263 } 00264 #endif 00265 00266 /** 00267 * @} 00268 */ 00269 00270 /** 00271 * @} 00272 */ 00273 00274 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/