STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/AWU/AWU_ActiveHaltMode/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file AWU_ActiveHaltMode\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 AWU Active Halt Mode 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 #include "stm8s_eval_lcd.h" 00032 00033 /** 00034 * @addtogroup AWU_ActiveHaltMode 00035 * @{ 00036 */ 00037 00038 /* Private typedef -----------------------------------------------------------*/ 00039 /* Private define ------------------------------------------------------------*/ 00040 /* Private macro -------------------------------------------------------------*/ 00041 /* Private variables ---------------------------------------------------------*/ 00042 /* Global variables ----------------------------------------------------------*/ 00043 bool ButtonPressed = FALSE; 00044 /* Private function prototypes -----------------------------------------------*/ 00045 void Delay (uint16_t nCount); 00046 uint32_t LSIMeasurment(void); 00047 static void CLK_Config(void); 00048 void AWU_Config(void); 00049 static void GPIO_Config(void); 00050 static void LCD_Config(void); 00051 /* Private functions ---------------------------------------------------------*/ 00052 00053 /** 00054 * @brief Example main entry point. 00055 * @param None 00056 * @retval None 00057 */ 00058 void main(void) 00059 { 00060 uint16_t i = 0; 00061 uint16_t j = 0; 00062 00063 /* Clock configuration -----------------------------------------*/ 00064 CLK_Config(); 00065 00066 /* GPIO Configuration ---------------------------------------*/ 00067 GPIO_Config(); 00068 00069 /* Configure LCD mounted on STM8-128 EVAL board ----------------*/ 00070 LCD_Config(); 00071 00072 /*AWU configuration --------------------------------------------*/ 00073 AWU_Config(); 00074 00075 /* Enable general interrupts */ 00076 enableInterrupts(); 00077 00078 while (1) 00079 { 00080 /* Check button status */ 00081 if (ButtonPressed == TRUE) /* Button is pressed */ 00082 { 00083 00084 ButtonPressed = FALSE; 00085 00086 /* Clear LCD */ 00087 LCD_Clear(); 00088 LCD_SetCursorPos(LCD_LINE1, 0); 00089 LCD_Print(" Preparing to "); 00090 LCD_SetCursorPos(LCD_LINE2, 0); 00091 LCD_Print(" sleep... "); 00092 00093 /* LEDs toggle quickly */ 00094 for (j = 0; j < 20; j++) 00095 { 00096 STM_EVAL_LEDToggle(LED1); 00097 STM_EVAL_LEDToggle(LED2); 00098 STM_EVAL_LEDToggle(LED3); 00099 STM_EVAL_LEDToggle(LED4); 00100 for (i = 0; i < 2; i++) 00101 { 00102 Delay((uint16_t)60000); 00103 } 00104 } 00105 00106 /* Switch LEDs OFF */ 00107 STM_EVAL_LEDOff(LED1); 00108 STM_EVAL_LEDOff(LED2); 00109 STM_EVAL_LEDOff(LED3); 00110 STM_EVAL_LEDOff(LED4); 00111 00112 /* Clear LCD */ 00113 LCD_Clear(); 00114 00115 halt(); /* Program halted */ 00116 00117 /* Program re-starts here, thanks to AWU */ 00118 LCD_Clear(); 00119 00120 /* Print "Running..." on LCD line1*/ 00121 LCD_SetCursorPos(LCD_LINE1, 0); 00122 LCD_Print(" Running... "); 00123 /* Print "Press Key" on LCD line2*/ 00124 LCD_SetCursorPos(LCD_LINE2, 0); 00125 LCD_Print(" Press Key "); 00126 } 00127 else 00128 { 00129 /* Toggle LEDs slowly */ 00130 STM_EVAL_LEDToggle(LED1); 00131 STM_EVAL_LEDToggle(LED2); 00132 STM_EVAL_LEDToggle(LED3); 00133 STM_EVAL_LEDToggle(LED4); 00134 for (i = 0; i < 15; i++) 00135 { 00136 Delay((uint16_t)60000); 00137 } 00138 } 00139 } 00140 } 00141 00142 /** 00143 * @brief Configure system clock to run at 16Mhz 00144 * @param None 00145 * @retval None 00146 */ 00147 static void CLK_Config(void) 00148 { 00149 /* Initialization of the clock */ 00150 /* Clock divider to HSI/1 */ 00151 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00152 } 00153 00154 /** 00155 * @brief Configure the AWU time base to 12s 00156 * @param None 00157 * @retval None 00158 */ 00159 void AWU_Config(void) 00160 { 00161 /* Initialization of AWU */ 00162 /* LSI calibration for accurate auto wake up time base*/ 00163 AWU_LSICalibrationConfig(LSIMeasurment()); 00164 00165 /* The delay corresponds to the time we will stay in Halt mode */ 00166 AWU_Init(AWU_TIMEBASE_12S); 00167 } 00168 00169 /** 00170 * @brief Configure GPIO for LEDs and buttons available on the evaluation board 00171 * @param None 00172 * @retval None 00173 */ 00174 static void GPIO_Config(void) 00175 { 00176 /* Initialize LEDs mounted on STM8-128 EVAL board */ 00177 STM_EVAL_LEDInit(LED1); 00178 STM_EVAL_LEDInit(LED2); 00179 STM_EVAL_LEDInit(LED3); 00180 STM_EVAL_LEDInit(LED4); 00181 00182 /* Initialize KEY pushbutton mounted on STM8-128 EVAL board */ 00183 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); 00184 } 00185 00186 /** 00187 * @brief Configure the LCD available on the evaluation board 00188 * @param None 00189 * @retval None 00190 */ 00191 static void LCD_Config(void) 00192 { 00193 /* Init the Eval board LCD */ 00194 STM8S_EVAL_LCD_Init(); 00195 00196 /* Clear LCD */ 00197 LCD_Clear(); 00198 00199 /* Print "Running..." on LCD line1*/ 00200 LCD_SetCursorPos(LCD_LINE1, 0); 00201 LCD_Print(" Running... "); 00202 /* Print "Press Key" on LCD line2*/ 00203 LCD_SetCursorPos(LCD_LINE2, 0); 00204 LCD_Print(" Press Key "); 00205 } 00206 00207 /** 00208 * @brief Measure the LSI frequency using timer IC1 and update the calibration registers. 00209 * @note It is recommended to use a timer clock frequency of at least 10MHz in order 00210 * to obtain a better in the LSI frequency measurement. 00211 * @param None 00212 * @retval None 00213 */ 00214 uint32_t LSIMeasurment(void) 00215 { 00216 00217 uint32_t lsi_freq_hz = 0x0; 00218 uint32_t fmaster = 0x0; 00219 uint16_t ICValue1 = 0x0; 00220 uint16_t ICValue2 = 0x0; 00221 00222 /* Get master frequency */ 00223 fmaster = CLK_GetClockFreq(); 00224 00225 /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */ 00226 AWU->CSR |= AWU_CSR_MSR; 00227 00228 #if defined (STM8S903) || defined (STM8S103) || defined (STM8S003) 00229 /* Measure the LSI frequency with TIMER Input Capture 1 */ 00230 00231 /* Capture only every 8 events!!! */ 00232 /* Enable capture of TI1 */ 00233 TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0); 00234 00235 /* Enable TIM1 */ 00236 TIM1_Cmd(ENABLE); 00237 00238 /* wait a capture on cc1 */ 00239 while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1); 00240 /* Get CCR1 value*/ 00241 ICValue1 = TIM1_GetCapture1(); 00242 TIM1_ClearFlag(TIM1_FLAG_CC1); 00243 00244 /* wait a capture on cc1 */ 00245 while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1); 00246 /* Get CCR1 value*/ 00247 ICValue2 = TIM1_GetCapture1(); 00248 TIM1_ClearFlag(TIM1_FLAG_CC1); 00249 00250 /* Disable IC1 input capture */ 00251 TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1E); 00252 /* Disable timer2 */ 00253 TIM1_Cmd(DISABLE); 00254 00255 #else 00256 /* Measure the LSI frequency with TIMER Input Capture 1 */ 00257 00258 /* Capture only every 8 events!!! */ 00259 /* Enable capture of TI1 */ 00260 TIM3_ICInit(TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, TIM3_ICPSC_DIV8, 0); 00261 00262 /* Enable TIM3 */ 00263 TIM3_Cmd(ENABLE); 00264 00265 /* wait a capture on cc1 */ 00266 while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); 00267 /* Get CCR1 value*/ 00268 ICValue1 = TIM3_GetCapture1(); 00269 TIM3_ClearFlag(TIM3_FLAG_CC1); 00270 00271 /* wait a capture on cc1 */ 00272 while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); 00273 /* Get CCR1 value*/ 00274 ICValue2 = TIM3_GetCapture1(); 00275 TIM3_ClearFlag(TIM3_FLAG_CC1); 00276 00277 /* Disable IC1 input capture */ 00278 TIM3->CCER1 &= (uint8_t)(~TIM3_CCER1_CC1E); 00279 /* Disable timer3 */ 00280 TIM3_Cmd(DISABLE); 00281 #endif /* STM8S903 || STM8S103*/ 00282 00283 /* Compute LSI clock frequency */ 00284 lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1); 00285 00286 /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */ 00287 AWU->CSR &= (uint8_t)(~AWU_CSR_MSR); 00288 00289 return (lsi_freq_hz); 00290 } 00291 00292 /** 00293 * @brief Delay. 00294 * @param nCount 00295 * @retval None 00296 */ 00297 void Delay(uint16_t nCount) 00298 { 00299 /* Decrement nCount value */ 00300 while (nCount != 0) 00301 { 00302 nCount--; 00303 } 00304 } 00305 #ifdef USE_FULL_ASSERT 00306 00307 /** 00308 * @brief Reports the name of the source file and the source line number 00309 * where the assert_param error has occurred. 00310 * @param file: pointer to the source file name 00311 * @param line: assert_param error line source number 00312 * @retval None 00313 */ 00314 void assert_failed(uint8_t* file, uint32_t line) 00315 { 00316 /* User can add his own implementation to report the file name and line number, 00317 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00318 00319 /* Infinite loop */ 00320 while (1) 00321 { 00322 } 00323 } 00324 #endif 00325 00326 /** 00327 * @} 00328 */ 00329 00330 00331 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/