STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/IWDG/IWDG_Example/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file IWDG_Example\main.c 00004 * @author MCD Application Team 00005 * @version V2.2.0 00006 * @date 30-September-2014 00007 * @brief This file contains the main function for IWDG Reset example. 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 "stm8s.h" 00030 #include "stm8s_eval.h" 00031 00032 /** 00033 * @addtogroup IWDG_Example 00034 * @{ 00035 */ 00036 00037 /* Private typedef -----------------------------------------------------------*/ 00038 /* Private define ------------------------------------------------------------*/ 00039 /* Private macro -------------------------------------------------------------*/ 00040 /* Private variables ---------------------------------------------------------*/ 00041 __IO uint32_t LsiFreq = 0; 00042 /* Private function prototypes -----------------------------------------------*/ 00043 static void CLK_Config(void); 00044 static void GPIO_Config(void); 00045 static void IWDG_Config(void); 00046 static uint32_t LSIMeasurment(void); 00047 /* Private functions ---------------------------------------------------------*/ 00048 00049 /** 00050 * @brief Main program. 00051 * @param None 00052 * @retval None 00053 */ 00054 void main(void) 00055 { 00056 /* Clock Configuration */ 00057 CLK_Config(); 00058 00059 /* GPIO Configuration */ 00060 GPIO_Config(); 00061 00062 /* Check if the system has resumed from IWDG reset */ 00063 if (RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET) 00064 { 00065 /* IWDGF flag set */ 00066 /* Turn on LED1 */ 00067 STM_EVAL_LEDOn(LED1); 00068 00069 /* Clear IWDGF Flag */ 00070 RST_ClearFlag(RST_FLAG_IWDGF); 00071 } 00072 else 00073 { 00074 /* IWDGF flag is not set */ 00075 /* Turn off LED1 */ 00076 STM_EVAL_LEDOff(LED1); 00077 } 00078 00079 /* Get measured LSI frequency */ 00080 LsiFreq = LSIMeasurment(); 00081 00082 /* IWDG Configuration */ 00083 IWDG_Config(); 00084 00085 while (1) 00086 { 00087 /* Reload IWDG counter */ 00088 IWDG_ReloadCounter(); 00089 } 00090 } 00091 00092 /** 00093 * @brief Configure system clock to run at 16Mhz 00094 * @param None 00095 * @retval None 00096 */ 00097 static void CLK_Config(void) 00098 { 00099 /* Initialization of the clock */ 00100 /* Clock divider to HSI/1 */ 00101 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00102 } 00103 00104 /** 00105 * @brief Configures the IWDG to generate a Reset if it is not refreshed at the 00106 * correct time. 00107 * @param None 00108 * @retval None 00109 */ 00110 static void IWDG_Config(void) 00111 { 00112 /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ 00113 IWDG_Enable(); 00114 00115 /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency 00116 dispersion) */ 00117 /* Enable write access to IWDG_PR and IWDG_RLR registers */ 00118 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); 00119 00120 /* IWDG counter clock: LSI/128 */ 00121 IWDG_SetPrescaler(IWDG_Prescaler_128); 00122 00123 /* Set counter reload value to obtain 250ms IWDG TimeOut. 00124 Counter Reload Value = 250ms/IWDG counter clock period 00125 = 250ms / (LSI/128) 00126 = 0.25s / (LsiFreq/128) 00127 = LsiFreq/(128 * 4) 00128 = LsiFreq/512 00129 */ 00130 IWDG_SetReload((uint8_t)(LsiFreq/512)); 00131 00132 /* Reload IWDG counter */ 00133 IWDG_ReloadCounter(); 00134 } 00135 00136 /** 00137 * @brief Initialize LED1 and Key Button mounted on the evaluation board 00138 * @param None 00139 * @retval None 00140 */ 00141 static void GPIO_Config(void) 00142 { 00143 /* Configure LED1 */ 00144 STM_EVAL_LEDInit(LED1); 00145 STM_EVAL_LEDOff(LED1); 00146 00147 /* Configure Key button */ 00148 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); 00149 00150 /* enable interrupts */ 00151 enableInterrupts(); 00152 } 00153 00154 /** 00155 * @brief Measure the LSI frequency using timer IC1 and update the calibration registers. 00156 * @note It is recommended to use a timer clock frequency of at least 10MHz in order 00157 * to obtain a better in the LSI frequency measurement. 00158 * @param None 00159 * @retval None 00160 */ 00161 static uint32_t LSIMeasurment(void) 00162 { 00163 uint32_t lsi_freq_hz = 0x0; 00164 uint32_t fmaster = 0x0; 00165 uint16_t ICValue1 = 0x0; 00166 uint16_t ICValue2 = 0x0; 00167 00168 /* Get master frequency */ 00169 fmaster = CLK_GetClockFreq(); 00170 00171 /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */ 00172 AWU->CSR |= AWU_CSR_MSR; 00173 00174 #if defined (STM8S903) || defined (STM8S103) || defined (STM8S003) 00175 /* Measure the LSI frequency with TIMER Input Capture 1 */ 00176 00177 /* Capture only every 8 events!!! */ 00178 /* Enable capture of TI1 */ 00179 TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 00180 TIM1_ICPSC_DIV8, 0); 00181 00182 /* Enable TIM1 */ 00183 TIM1_Cmd(ENABLE); 00184 00185 /* wait a capture on cc1 */ 00186 while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1); 00187 /* Get CCR1 value*/ 00188 ICValue1 = TIM1_GetCapture1(); 00189 TIM1_ClearFlag(TIM1_FLAG_CC1); 00190 00191 /* wait a capture on cc1 */ 00192 while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1); 00193 /* Get CCR1 value*/ 00194 ICValue2 = TIM1_GetCapture1(); 00195 TIM1_ClearFlag(TIM1_FLAG_CC1); 00196 00197 /* Disable IC1 input capture */ 00198 TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1E); 00199 /* Disable timer2 */ 00200 TIM1_Cmd(DISABLE); 00201 00202 #else 00203 /* Measure the LSI frequency with TIMER Input Capture 1 */ 00204 00205 /* Capture only every 8 events!!! */ 00206 /* Enable capture of TI1 */ 00207 TIM3_ICInit(TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, 00208 TIM3_ICPSC_DIV8, 0); 00209 00210 /* Enable TIM3 */ 00211 TIM3_Cmd(ENABLE); 00212 00213 /* wait a capture on cc1 */ 00214 while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); 00215 /* Get CCR1 value*/ 00216 ICValue1 = TIM3_GetCapture1(); 00217 TIM3_ClearFlag(TIM3_FLAG_CC1); 00218 00219 /* wait a capture on cc1 */ 00220 while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); 00221 /* Get CCR1 value*/ 00222 ICValue2 = TIM3_GetCapture1(); 00223 TIM3_ClearFlag(TIM3_FLAG_CC1); 00224 00225 /* Disable IC1 input capture */ 00226 TIM3->CCER1 &= (uint8_t)(~TIM3_CCER1_CC1E); 00227 /* Disable timer3 */ 00228 TIM3_Cmd(DISABLE); 00229 #endif 00230 00231 /* Compute LSI clock frequency */ 00232 lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1); 00233 00234 /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */ 00235 AWU->CSR &= (uint8_t)(~AWU_CSR_MSR); 00236 00237 return (lsi_freq_hz); 00238 } 00239 00240 #ifdef USE_FULL_ASSERT 00241 /** 00242 * @brief Reports the name of the source file and the source line number 00243 * where the assert_param error has occurred. 00244 * @param file: pointer to the source file name 00245 * @param line: assert_param error line source number 00246 * @retval None 00247 */ 00248 void assert_failed(uint8_t* file, uint32_t line) 00249 { 00250 /* User can add his own implementation to report the file name and line number, 00251 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00252 00253 /* Infinite loop */ 00254 while (1) 00255 { 00256 } 00257 } 00258 #endif 00259 00260 /** 00261 * @} 00262 */ 00263 00264 00265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/