STM32F10x_StdPeriph_Examples/USART/HalfDuplex/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/HalfDuplex/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_HalfDuplex
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define TxBufferSize1   (countof(TxBuffer1) - 1)
00038 #define TxBufferSize2   (countof(TxBuffer2) - 1)
00039 
00040 /* Private macro -------------------------------------------------------------*/
00041 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00042 
00043 /* Private variables ---------------------------------------------------------*/
00044 USART_InitTypeDef USART_InitStructure;
00045 uint8_t TxBuffer1[] = "USART Half Duplex: USARTy -> USARTz using HalfDuplex mode";
00046 uint8_t TxBuffer2[] = "USART Half Duplex: USARTz -> USARTy using HalfDuplex mode";
00047 uint8_t RxBuffer1[TxBufferSize2];
00048 uint8_t RxBuffer2[TxBufferSize1];
00049 uint32_t NbrOfDataToRead1 = TxBufferSize2;
00050 uint32_t NbrOfDataToRead2 = TxBufferSize1;
00051 uint8_t TxCounter1 = 0, RxCounter1 = 0;
00052 uint8_t TxCounter2 = 0, RxCounter2 = 0;
00053 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;  
00054 
00055 /* Private function prototypes -----------------------------------------------*/
00056 void RCC_Configuration(void);
00057 void GPIO_Configuration(void);
00058 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00059   
00060 /* Private functions ---------------------------------------------------------*/
00061 
00062 /**
00063   * @brief   Main program
00064   * @param  None
00065   * @retval None
00066   */
00067 int main(void)
00068 {
00069   /*!< At this stage the microcontroller clock setting is already configured, 
00070        this is done through SystemInit() function which is called from startup
00071        file (startup_stm32f10x_xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f10x.c file
00074      */     
00075        
00076   /* System Clocks Configuration */
00077   RCC_Configuration();
00078 
00079   /* Configure the GPIO ports */
00080   GPIO_Configuration();
00081 
00082 /* USARTy and USARTz configuration -------------------------------------------*/
00083   /* USARTy and USARTz configured as follow:
00084         - BaudRate = 230400 baud  
00085         - Word Length = 8 Bits
00086         - One Stop Bit
00087         - No parity
00088         - Hardware flow control disabled (RTS and CTS signals)
00089         - Receive and transmit enabled
00090 
00091   */
00092   USART_InitStructure.USART_BaudRate = 230400;
00093   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00094   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00095   USART_InitStructure.USART_Parity = USART_Parity_No;
00096   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00097   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00098 
00099   /* Configure USARTy */
00100   USART_Init(USARTy, &USART_InitStructure);
00101   /* Configure USARTz */
00102   USART_Init(USARTz, &USART_InitStructure);
00103   
00104   /* Enable the USARTy */
00105   USART_Cmd(USARTy, ENABLE);
00106   /* Enable the USARTz */
00107   USART_Cmd(USARTz, ENABLE);
00108 
00109   /* Enable USARTy Half Duplex Mode*/
00110   USART_HalfDuplexCmd(USARTy, ENABLE);
00111   /* Enable USARTz Half Duplex Mode*/
00112   USART_HalfDuplexCmd(USARTz, ENABLE);
00113 
00114   while(NbrOfDataToRead2--)
00115   {
00116     /* Wait until end of transmit */
00117     while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00118     {
00119     }
00120     /* Write one byte in the USARTy Transmit Data Register */
00121     USART_SendData(USARTy, TxBuffer1[TxCounter1++]);
00122 
00123     /* Wait the byte is entirtly received by USARTz */  
00124     while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET)
00125     {
00126     }
00127     /* Store the received byte in the RxBuffer2 */
00128     RxBuffer2[RxCounter2++] = USART_ReceiveData(USARTz);
00129   }
00130 
00131   /* Clear the USARTy Data Register */
00132   USART_ReceiveData(USARTy);
00133 
00134   while(NbrOfDataToRead1--)
00135   { 
00136     /* Wait until end of transmit */
00137     while(USART_GetFlagStatus(USARTz, USART_FLAG_TXE)== RESET)
00138     {
00139     }
00140     /* Write one byte in the USARTz Transmit Data Register */
00141     USART_SendData(USARTz, TxBuffer2[TxCounter2++]);
00142 
00143     /* Wait the byte is entirtly received by USARTy */
00144     while(USART_GetFlagStatus(USARTy,USART_FLAG_RXNE) == RESET)
00145     {
00146     }
00147     /* Store the received byte in the RxBuffer1 */
00148     RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy);
00149   }
00150   
00151   /* Check the received data with the send ones */
00152   TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
00153   /* TransferStatus = PASSED, if the data transmitted from USARTy and  
00154      received by USARTz are the same */
00155   /* TransferStatus = FAILED, if the data transmitted from USARTy and 
00156      received by USARTz are different */
00157   TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
00158   /* TransferStatus = PASSED, if the data transmitted from USARTz and  
00159      received by USARTy are the same */
00160   /* TransferStatus = FAILED, if the data transmitted from USARTz and 
00161      received by USARTy are different */
00162 
00163   while (1)
00164   {
00165   }
00166 }
00167 
00168 /**
00169   * @brief  Configures the different system clocks.
00170   * @param  None
00171   * @retval None
00172   */
00173 void RCC_Configuration(void)
00174 {
00175   /* Enable GPIO clock */
00176   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00177 
00178 #ifndef USE_STM3210C_EVAL
00179   /* Enable USARTy Clock */
00180   RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE); 
00181 #else
00182   /* Enable USARTy Clock */
00183   RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE); 
00184 #endif
00185   /* Enable USARTz Clock */
00186   RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);  
00187 }
00188 
00189 /**
00190   * @brief  Configures the different GPIO ports.
00191   * @param  None
00192   * @retval None
00193   */
00194 void GPIO_Configuration(void)
00195 {
00196   GPIO_InitTypeDef GPIO_InitStructure;
00197 
00198 #ifdef USE_STM3210C_EVAL
00199   /* Enable the USART3 Pins Software Remapping */
00200   GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
00201   
00202   /* Enable the USART2 Pins Software Remapping */
00203   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  
00204 #elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
00205   /* Enable the USART2 Pins Software Remapping */
00206   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00207 #endif
00208   
00209   /* Configure USARTy Tx as alternate function open-drain */
00210   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
00211   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00212   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
00213   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00214 
00215   /* Configure USARTz Tx as alternate function open-drain */
00216   GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
00217   GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);  
00218 }
00219 
00220 /**
00221   * @brief  Compares two buffers.
00222   * @param  pBuffer1, pBuffer2: buffers to be compared.
00223   * @param  BufferLength: buffer's length
00224   * @retval PASSED: pBuffer1 identical to pBuffer2
00225   *   FAILED: pBuffer1 differs from pBuffer2
00226   */
00227 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00228 {
00229   while(BufferLength--)
00230   {
00231     if(*pBuffer1 != *pBuffer2)
00232     {
00233       return FAILED;
00234     }
00235     
00236     pBuffer1++;
00237     pBuffer2++;
00238   }
00239 
00240   return PASSED;  
00241 }
00242 
00243 #ifdef  USE_FULL_ASSERT
00244 /**
00245   * @brief  Reports the name of the source file and the source line number
00246   *         where the assert_param error has occurred.
00247   * @param  file: pointer to the source file name
00248   * @param  line: assert_param error line source number
00249   * @retval None
00250   */
00251 void assert_failed(uint8_t* file, uint32_t line)
00252 { 
00253   /* User can add his own implementation to report the file name and line number,
00254      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00255 
00256   /* Infinite loop */
00257   while (1)
00258   {
00259   }
00260 }
00261 #endif
00262 
00263 /**
00264   * @}
00265   */ 
00266 
00267 /**
00268   * @}
00269   */ 
00270 
00271 /******************* (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