STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/USART/USART_AutoBaudRate/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/USART_AutoBaudRate/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_AutoBaudRate
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private function prototypes -----------------------------------------------*/
00044 static void USART_Config(void);
00045 static void AutoBauRate_StartBitMethod(void);
00046 
00047 /* Private functions ---------------------------------------------------------*/
00048 
00049 /**
00050   * @brief   Main program
00051   * @param  None
00052   * @retval None
00053   */
00054 int main(void)
00055 { 
00056   /*!< At this stage the microcontroller clock setting is already configured, 
00057        this is done through SystemInit() function which is called from startup
00058        file (startup_stm32f0xx.s) before to branch to application main.
00059        To reconfigure the default setting of SystemInit() function, refer to
00060        system_stm32f0xx.c file
00061   */
00062   
00063   /* Initialize LEDs available on STM320518-EVAL board ************************/
00064   STM_EVAL_LEDInit(LED1);
00065   STM_EVAL_LEDInit(LED2);
00066   STM_EVAL_LEDInit(LED3);
00067   STM_EVAL_LEDInit(LED4);
00068   
00069   /* Configure and enable the systick timer to generate an interrupt each 1 ms */
00070   SysTick_Config((SystemCoreClock / 1000));
00071   
00072   /* USART configuration */
00073   USART_Config();
00074   
00075   /* AutoBaudRate for USART by Start bit Method */
00076   AutoBauRate_StartBitMethod();
00077   
00078   while(1);
00079 }
00080 
00081 /**
00082   * @brief Configure the USART Device
00083   * @param  None
00084   * @retval None
00085   */
00086 static void USART_Config(void)
00087 { 
00088   USART_InitTypeDef USART_InitStructure;
00089   
00090   /* USARTx configured as follow:
00091   - BaudRate = 115200 baud  
00092   - Word Length = 8 Bits
00093   - Stop Bit = 1 Stop Bit
00094   - Parity = No Parity
00095   - Hardware flow control disabled (RTS and CTS signals)
00096   - Receive and transmit enabled
00097   */
00098   USART_InitStructure.USART_BaudRate = 115200;
00099   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00100   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00101   USART_InitStructure.USART_Parity = USART_Parity_No;
00102   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00103   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00104   STM_EVAL_COMInit(COM1, &USART_InitStructure);
00105 }
00106 
00107 /**
00108   * @brief  Start Bit Method to USART AutoBaudRate.
00109   * @param  None
00110   * @retval None
00111   */
00112 static void AutoBauRate_StartBitMethod(void)
00113 { 
00114   /* USART enable */
00115   USART_Cmd(EVAL_COM1, ENABLE);
00116   
00117   /* Configure the AutoBaudRate method */
00118   USART_AutoBaudRateConfig(EVAL_COM1, USART_AutoBaudRate_StartBit);
00119   
00120   /* Enable AutoBaudRate feature */
00121   USART_AutoBaudRateCmd(EVAL_COM1, ENABLE);
00122   
00123   /* Wait until Receive enable acknowledge flag is set */
00124   while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_REACK) == RESET)
00125   {}  
00126   
00127   /* Wait until Transmit enable acknowledge flag is set */  
00128   while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TEACK) == RESET)
00129   {}  
00130   
00131   /* Loop until the end of Autobaudrate phase */
00132   while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_ABRF) == RESET)
00133   {}  
00134   
00135   /* If AutoBaudBate error occurred */
00136   if (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_ABRE) != RESET)
00137   {
00138     /* Turn on LED3 */
00139     STM_EVAL_LEDOn(LED3);
00140   }
00141   else
00142   {
00143     /* Turn on LED2 */
00144     STM_EVAL_LEDOn(LED2);
00145     
00146     /* Wait until RXNE flag is set */
00147     while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)
00148     {}
00149     
00150     /* Wait until TXE flag is set */    
00151     while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET)
00152     {}
00153     
00154     /* Send received character */
00155     USART_SendData(EVAL_COM1, USART_ReceiveData(EVAL_COM1)); 
00156     
00157     /* clear the TE bit (if a transmission is on going or a data is in the TDR, it will be sent before
00158     efectivelly disabling the transmission) */
00159     USART_DirectionModeCmd(EVAL_COM1, USART_Mode_Tx, DISABLE);
00160     
00161     /* Check the Transfer Complete Flag */
00162     while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
00163     {}
00164   }
00165   
00166   /* USART Disable */
00167   USART_Cmd(EVAL_COM1, DISABLE);
00168 }
00169 
00170 #ifdef  USE_FULL_ASSERT
00171 
00172 /**
00173   * @brief  Reports the name of the source file and the source line number
00174   *         where the assert_param error has occurred.
00175   * @param  file: pointer to the source file name
00176   * @param  line: assert_param error line source number
00177   * @retval None
00178   */
00179 void assert_failed(uint8_t* file, uint32_t line)
00180 { 
00181   /* User can add his own implementation to report the file name and line number,
00182      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00183 
00184   /* Infinite loop */
00185   while (1)
00186   {
00187   }
00188 }
00189 #endif
00190 
00191 /**
00192   * @}
00193   */
00194 
00195 /**
00196   * @}
00197   */
00198 
00199 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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