STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/USART/USART_WakeUpFromStop/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/USART_WakeUpFromStop/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>&copy; 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 USART_WakeUpFromStop
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint8_t DataReceived = 0;
00044 extern __IO uint8_t InterruptCounter;
00045 
00046 /* Private function prototypes -----------------------------------------------*/
00047 static void USART_Config(void);
00048 static void WakeUp_StartBitMethod(void);
00049 static void RestoreConfiguration(void);
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   
00067   /* Initialize LEDs available  ***********************************************/
00068   STM_EVAL_LEDInit(LED1);
00069   STM_EVAL_LEDInit(LED2);
00070   STM_EVAL_LEDInit(LED3);
00071   STM_EVAL_LEDInit(LED4);
00072    
00073   /* USART configuration */
00074   USART_Config();
00075   
00076   /* Wake up from USART STOP mode by Start bit Method */
00077   WakeUp_StartBitMethod();
00078   
00079   /* Configure SystemClock*/
00080   RestoreConfiguration();
00081   
00082   /* Configure and enable the systick timer to generate an interrupt each 1 ms */
00083   SysTick_Config((SystemCoreClock / 1000));
00084   
00085   while (1)
00086   {
00087   }
00088 }
00089 
00090 /**
00091   * @brief  Start Bit Method to Wake Up USART from Stop mode Test.
00092   * @param  None
00093   * @retval None
00094   */
00095 static void WakeUp_StartBitMethod(void)
00096 { 
00097   /* Configure the wake up Method = Start bit */ 
00098   USART_StopModeWakeUpSourceConfig(EVAL_COM1, USART_WakeUpSource_StartBit);
00099   
00100   /* Enable USART1 */ 
00101   USART_Cmd(EVAL_COM1, ENABLE);
00102  
00103   /* Before entering the USART in STOP mode the REACK flag must be checked to ensure the USART RX is ready */
00104   while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_REACK) == RESET)
00105   {}
00106   
00107   /* Enable USART STOP mode by setting the UESM bit in the CR1 register.*/
00108   USART_STOPModeCmd(EVAL_COM1, ENABLE);
00109   
00110   /* Enable the wake up from stop Interrupt */ 
00111   USART_ITConfig(EVAL_COM1, USART_IT_WU, ENABLE);   
00112 
00113   /* Enable PWR APB clock */
00114   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00115   
00116   /* Enter USART in STOP mode with regulator in low power mode */
00117   PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
00118   
00119   /* Waiting Wake Up interrupt */
00120   while(InterruptCounter == 0x00)
00121   {}
00122   
00123   /* Disable USART peripheral in STOP mode */ 
00124   USART_STOPModeCmd(EVAL_COM1, DISABLE);
00125     
00126   while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)
00127   {}
00128   DataReceived = USART_ReceiveData(EVAL_COM1);
00129  
00130   /* Clear the TE bit (if a transmission is on going or a data is in the TDR, it will be sent before
00131   efectivelly disabling the transmission) */
00132   USART_DirectionModeCmd(EVAL_COM1, USART_Mode_Tx, DISABLE);
00133   
00134   /* Check the Transfer Complete Flag */
00135   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
00136   {}
00137 
00138   /* USART Disable */
00139   USART_Cmd(EVAL_COM1, DISABLE);
00140 }
00141 
00142 /**
00143   * @brief Configure the USART Device
00144   * @param  None
00145   * @retval None
00146   */
00147 static void USART_Config(void)
00148 { 
00149   USART_InitTypeDef USART_InitStructure;
00150   GPIO_InitTypeDef GPIO_InitStructure; 
00151   NVIC_InitTypeDef NVIC_InitStructure;
00152   
00153   /* Enable GPIO clock */
00154   RCC_AHBPeriphClockCmd(EVAL_COM1_TX_GPIO_CLK , ENABLE);
00155   RCC_AHBPeriphClockCmd(EVAL_COM1_RX_GPIO_CLK , ENABLE);
00156   
00157   /* Enable USARTx APB clock */
00158 #ifdef USE_STM320518_EVAL
00159   RCC_APB2PeriphClockCmd(EVAL_COM1_CLK, ENABLE);
00160 #else 
00161   RCC_APB1PeriphClockCmd(EVAL_COM1_CLK, ENABLE);
00162 #endif /* USE_STM320518_EVAL */ 
00163 
00164   
00165   /* Configure the HSI as USART clock */
00166 #ifdef USE_STM320518_EVAL
00167   RCC_USARTCLKConfig(RCC_USART1CLK_HSI);
00168 #else 
00169   RCC_USARTCLKConfig(RCC_USART2CLK_HSI);
00170 #endif /* USE_STM320518_EVAL */
00171 
00172   
00173   /* USARTx Pins configuration **************************************************/  
00174   /* Connect pin to Periph */
00175   GPIO_PinAFConfig(EVAL_COM1_TX_GPIO_PORT, EVAL_COM1_TX_SOURCE, EVAL_COM1_TX_AF);    
00176   GPIO_PinAFConfig(EVAL_COM1_RX_GPIO_PORT, EVAL_COM1_RX_SOURCE, EVAL_COM1_RX_AF); 
00177   
00178   /* Configure pins as AF pushpull */
00179   GPIO_InitStructure.GPIO_Pin = EVAL_COM1_TX_PIN | EVAL_COM1_RX_PIN;
00180   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00181   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00182   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00183   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00184   GPIO_Init(EVAL_COM1_RX_GPIO_PORT, &GPIO_InitStructure); 
00185   GPIO_Init(EVAL_COM1_TX_GPIO_PORT, &GPIO_InitStructure); 
00186  
00187   /* USARTx configured as follow:
00188   - BaudRate = 115200 baud  
00189   - Word Length = 8 Bits
00190   - Stop Bit = 1 Stop Bit
00191   - Parity = No Parity
00192   - Hardware flow control disabled (RTS and CTS signals)
00193   - Receive and transmit enabled
00194   */
00195   
00196   USART_DeInit(EVAL_COM1);
00197   USART_InitStructure.USART_BaudRate = 115200;
00198   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00199   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00200   USART_InitStructure.USART_Parity = USART_Parity_No;
00201   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00202   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00203   USART_Init(EVAL_COM1, &USART_InitStructure);
00204  
00205   
00206   /* USART2 IRQ Channel configuration */
00207 #ifdef USE_STM320518_EVAL
00208   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
00209 #else 
00210   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
00211 #endif /* USE_STM320518_EVAL */
00212   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
00213   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00214   NVIC_Init(&NVIC_InitStructure);
00215 }
00216 
00217 /**
00218   * @brief  Restore peripheral config before entering STOP mode.
00219   * @param  None
00220   * @retval None
00221   */
00222 static void RestoreConfiguration(void)
00223 {
00224   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00225   
00226   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/    
00227   /* Enable HSE */    
00228   RCC_HSEConfig(RCC_HSE_ON);
00229  
00230   /* Wait till HSE is ready and if Time out is reached exit */
00231   HSEStatus = RCC_WaitForHSEStartUp();
00232 
00233   if (HSEStatus == (uint32_t)0x01)
00234   {
00235     /* Enable Prefetch Buffer */
00236     FLASH_SetLatency(FLASH_Latency_1);
00237     
00238     /* HCLK = SYSCLK */
00239     RCC_HCLKConfig(RCC_SYSCLK_Div1); 
00240     
00241     /* PCLK = HCLK */
00242     RCC_PCLKConfig(RCC_HCLK_Div1);
00243         
00244     /*  PLL configuration:  = HSE *  6 = 48 MHz */
00245     RCC_PREDIV1Config(RCC_PREDIV1_Div1);
00246     RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_CFGR_PLLMULL6);
00247     
00248     /* Enable PLL */
00249     RCC_PLLCmd(ENABLE);
00250     
00251     /* PLL as system clock source */
00252     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
00253   } 
00254 }
00255 #ifdef  USE_FULL_ASSERT
00256 
00257 /**
00258   * @brief  Reports the name of the source file and the source line number
00259   *         where the assert_param error has occurred.
00260   * @param  file: pointer to the source file name
00261   * @param  line: assert_param error line source number
00262   * @retval None
00263   */
00264 void assert_failed(uint8_t* file, uint32_t line)
00265 { 
00266   /* User can add his own implementation to report the file name and line number,
00267      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00268 
00269   /* Infinite loop */
00270   while (1)
00271   {
00272   }
00273 }
00274 #endif
00275 
00276 /**
00277   * @}
00278   */
00279 
00280 /**
00281   * @}
00282   */
00283 
00284 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32