STM32F10x_StdPeriph_Examples/USART/Polling/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/Polling/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include "platform_config.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup USART_Polling
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define TxBufferSize   (countof(TxBuffer))
00038 
00039 /* Private macro -------------------------------------------------------------*/
00040 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00041 
00042 /* Private variables ---------------------------------------------------------*/
00043 USART_InitTypeDef USART_InitStructure;
00044 uint8_t TxBuffer[] = "Buffer Send from USARTy to USARTz using Flags";
00045 uint8_t RxBuffer[TxBufferSize];
00046 __IO uint8_t TxCounter = 0, RxCounter = 0;  
00047 volatile TestStatus TransferStatus = FAILED;  
00048 
00049 /* Private function prototypes -----------------------------------------------*/
00050 void RCC_Configuration(void);
00051 void GPIO_Configuration(void);
00052 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00053 __IO uint8_t index = 0;
00054   
00055 /* Private functions ---------------------------------------------------------*/
00056 
00057 /**
00058   * @brief   Main program
00059   * @param  None
00060   * @retval None
00061   */
00062 int main(void)
00063 {
00064   /*!< At this stage the microcontroller clock setting is already configured, 
00065        this is done through SystemInit() function which is called from startup
00066        file (startup_stm32f10x_xx.s) before to branch to application main.
00067        To reconfigure the default setting of SystemInit() function, refer to
00068        system_stm32f10x.c file
00069      */     
00070        
00071   /* System Clocks Configuration */
00072   RCC_Configuration();
00073 
00074   /* Configure the GPIO ports */
00075   GPIO_Configuration();
00076 
00077 /* USARTy and USARTz configuration ------------------------------------------------------*/
00078   /* USARTy and USARTz configured as follow:
00079         - BaudRate = 230400 baud  
00080         - Word Length = 8 Bits
00081         - One Stop Bit
00082         - Even parity
00083         - Hardware flow control disabled (RTS and CTS signals)
00084         - Receive and transmit enabled
00085   */
00086   USART_InitStructure.USART_BaudRate = 230400;
00087   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00088   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00089   USART_InitStructure.USART_Parity = USART_Parity_Even;
00090   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00091   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00092   
00093   /* Configure USARTy */
00094   USART_Init(USARTy, &USART_InitStructure);
00095   /* Configure USARTz */
00096   USART_Init(USARTz, &USART_InitStructure);
00097   
00098   /* Enable the USARTy */
00099   USART_Cmd(USARTy, ENABLE);
00100 
00101   /* Enable the USARTz */
00102   USART_Cmd(USARTz, ENABLE);
00103 
00104   while(TxCounter < TxBufferSize)
00105   {   
00106     /* Send one byte from USARTy to USARTz */
00107     USART_SendData(USARTy, TxBuffer[TxCounter++]);
00108     
00109     /* Loop until USARTy DR register is empty */ 
00110     while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00111     {
00112     }
00113     
00114     /* Loop until the USARTz Receive Data Register is not empty */
00115     while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET)
00116     {
00117     }
00118 
00119     /* Store the received byte in RxBuffer */
00120     RxBuffer[RxCounter++] = (USART_ReceiveData(USARTz) & 0x7F);  
00121     
00122   } 
00123   /* Check the received data with the send ones */
00124   TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize);
00125   /* TransferStatus = PASSED, if the data transmitted from USARTy and  
00126      received by USARTz are the same */
00127   /* TransferStatus = FAILED, if the data transmitted from USARTy and 
00128      received by USARTz are different */
00129 
00130   while (1)
00131   {
00132   }
00133 }
00134 
00135 /**
00136   * @brief  Configures the different system clocks.
00137   * @param  None
00138   * @retval None
00139   */
00140 void RCC_Configuration(void)
00141 {    
00142   /* Enable GPIO clock */
00143   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00144 
00145 #ifndef USE_STM3210C_EVAL
00146   /* Enable USARTy Clock */
00147   RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE); 
00148 #else
00149   /* Enable USARTy Clock */
00150   RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE); 
00151 #endif
00152   /* Enable USARTz Clock */
00153   RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);  
00154 }
00155 
00156 /**
00157   * @brief  Configures the different GPIO ports.
00158   * @param  None
00159   * @retval None
00160   */
00161 void GPIO_Configuration(void)
00162 {
00163   GPIO_InitTypeDef GPIO_InitStructure;
00164 
00165 #ifdef USE_STM3210C_EVAL
00166   /* Enable the USART3 Pins Software Remapping */
00167   GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
00168   
00169   /* Enable the USART2 Pins Software Remapping */
00170   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  
00171 #elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
00172   /* Enable the USART2 Pins Software Remapping */
00173   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00174 #endif
00175 
00176   /* Configure USARTy Rx as input floating */
00177   GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
00178   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00179   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00180   
00181   /* Configure USARTz Rx as input floating */
00182   GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
00183   GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);  
00184   
00185   /* Configure USARTy Tx as alternate function push-pull */
00186   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
00187   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00188   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00189   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00190 
00191   /* Configure USARTz Tx as alternate function push-pull */
00192   GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
00193   GPIO_Init(USARTz_GPIO, &GPIO_InitStructure); 
00194 }
00195 
00196 /**
00197   * @brief  Compares two buffers.
00198   * @param  pBuffer1, pBuffer2: buffers to be compared.
00199   * @param  BufferLength: buffer's length
00200   * @retval PASSED: pBuffer1 identical to pBuffer2
00201   *   FAILED: pBuffer1 differs from pBuffer2
00202   */
00203 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00204 {
00205   while(BufferLength--)
00206   {
00207     if(*pBuffer1 != *pBuffer2)
00208     {
00209       return FAILED;
00210     }
00211     
00212     pBuffer1++;
00213     pBuffer2++;
00214   }
00215 
00216   return PASSED;  
00217 }
00218 
00219 #ifdef  USE_FULL_ASSERT
00220 
00221 /**
00222   * @brief  Reports the name of the source file and the source line number
00223   *         where the assert_param error has occurred.
00224   * @param  file: pointer to the source file name
00225   * @param  line: assert_param error line source number
00226   * @retval None
00227   */
00228 void assert_failed(uint8_t* file, uint32_t line)
00229 { 
00230   /* User can add his own implementation to report the file name and line number,
00231      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00232 
00233   /* Infinite loop */
00234   while (1)
00235   {
00236   }
00237 }
00238 #endif
00239 
00240 /**
00241   * @}
00242   */ 
00243 
00244 /**
00245   * @}
00246   */ 
00247 
00248 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32