STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/WWDG/WWDG_Example/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file WWDG_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 wwdg Rearm 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 * @addtogroup WWDG_Example 00033 * @{ 00034 */ 00035 00036 /* Private typedef -----------------------------------------------------------*/ 00037 /* Private define ------------------------------------------------------------*/ 00038 #define WINDOW_VALUE 97 00039 #define COUNTER_INIT 104 00040 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 __IO FunctionalState NonAlowedRefresh = DISABLE; 00044 __IO FunctionalState AllowedRefresh = ENABLE; 00045 uint8_t Index; 00046 /* Private function prototypes -----------------------------------------------*/ 00047 void Delay (uint16_t nCount); 00048 static void GPIO_Config(void); 00049 static void WWDG_Config(void); 00050 /* Private functions ---------------------------------------------------------*/ 00051 /* Public functions ----------------------------------------------------------*/ 00052 00053 /** 00054 * @brief Main program. 00055 * @param None 00056 * @retval None 00057 */ 00058 void main(void) 00059 { 00060 00061 /* GPIO Configuration */ 00062 GPIO_Config(); 00063 00064 /* Check if the MCU has resumed from WWDG reset */ 00065 if (RST_GetFlagStatus(RST_FLAG_WWDGF) != RESET) 00066 { 00067 /* WWDGF flag set */ 00068 /* Toggle LED1 */ 00069 for (Index = 7; Index != 0; Index--) 00070 { 00071 STM_EVAL_LEDToggle(LED1); 00072 Delay(0x7FFF); 00073 } 00074 /* Clear WWDGF Flag */ 00075 RST_ClearFlag(RST_FLAG_WWDGF); 00076 } 00077 00078 /* WWDG Configuration */ 00079 WWDG_Config(); 00080 00081 while (1) 00082 { 00083 /* Check if WWDG counter refresh is allowed in Allowed window */ 00084 if (AllowedRefresh != DISABLE) 00085 { 00086 /* get WWDG counter value */ 00087 /* wait until WWDG counter becomes lower than window value */ 00088 while ((WWDG_GetCounter() & 0x7F) > WINDOW_VALUE); 00089 /* Refresh WWDG counter during allowed window so no MCU reset will occur */ 00090 WWDG_SetCounter(COUNTER_INIT); 00091 } 00092 00093 /* Check if WWDG counter refresh is allowed in non Allowed window */ 00094 if (NonAlowedRefresh != DISABLE) 00095 { 00096 /* wait until WWDG counter becomes higher than window value */ 00097 while ((WWDG_GetCounter() & 0x7F) < WINDOW_VALUE); 00098 /* Refresh WWDG counter during non allowed window so MCU reset will occur */ 00099 WWDG_SetCounter(COUNTER_INIT); 00100 } 00101 /* Toggle LED2 */ 00102 STM_EVAL_LEDToggle(LED2); 00103 Delay(0x6FFF); 00104 } 00105 } 00106 00107 /** 00108 * @brief Inserts a delay time. 00109 * @param nCount: specifies the delay time length. 00110 * @retval None 00111 */ 00112 void Delay(uint16_t nCount) 00113 { 00114 /* Decrement nCount value */ 00115 while (nCount != 0) 00116 { 00117 nCount--; 00118 } 00119 } 00120 00121 00122 /** 00123 * @brief Configures the WWDG to generate a Reset if the WWDG is not refreshed 00124 * during the correct window. 00125 * @param None 00126 * @retval None 00127 */ 00128 00129 static void WWDG_Config(void) 00130 { 00131 /* WWDG configuration: WWDG is clocked by SYSCLK = 2MHz */ 00132 /* WWDG timeout is equal to 251,9 ms */ 00133 /* Watchdog Window = (COUNTER_INIT - 63) * 1 step 00134 = 41 * (12288 / 2Mhz) 00135 = 251,9 ms 00136 */ 00137 /* Non Allowed Window = (COUNTER_INIT - WINDOW_VALUE) * 1 step 00138 = (104 - 97) * 1 step 00139 = 7 * 1 step 00140 = 7 * (12288 / 2Mhz) 00141 = 43.008 ms 00142 */ 00143 /* So the non allowed window starts from 0.0 ms to 43.008 ms 00144 and the allowed window starts from 43.008 ms to 251,9 ms 00145 If refresh is done during non allowed window, a reset will occur. 00146 If refresh is done during allowed window, no reset will occur. 00147 If the WWDG down counter reaches 63, a reset will occur. */ 00148 WWDG_Init(COUNTER_INIT, WINDOW_VALUE); 00149 } 00150 00151 /** 00152 * @brief Configures the Leds and the Buttons IO. 00153 * @param None 00154 * @retval None 00155 */ 00156 static void GPIO_Config(void) 00157 { 00158 00159 /* Initialize LED1 and LED2 mounted on the evaluation board */ 00160 STM_EVAL_LEDInit(LED1); 00161 STM_EVAL_LEDInit(LED2); 00162 00163 STM_EVAL_LEDOff(LED1); 00164 STM_EVAL_LEDOff(LED2); 00165 00166 /* Initialize Key and Joystick down push-buttons mounted on the evaluation board */ 00167 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); 00168 STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI); 00169 00170 /* enable interrupts */ 00171 enableInterrupts(); 00172 } 00173 00174 #ifdef USE_FULL_ASSERT 00175 00176 /** 00177 * @brief Reports the name of the source file and the source line number 00178 * where the assert_param error has occurred. 00179 * @param file: pointer to the source file name 00180 * @param line: assert_param error line source number 00181 * @retval None 00182 */ 00183 void assert_failed(uint8_t* file, uint32_t line) 00184 { 00185 /* User can add his own implementation to report the file name and line number, 00186 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00187 00188 /* Infinite loop */ 00189 while (1) 00190 { 00191 } 00192 } 00193 #endif 00194 00195 /** 00196 * @} 00197 */ 00198 00199 00200 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/