STM32F10x_StdPeriph_Examples/USART/Synchronous/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/Synchronous/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_Synchronous
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 #define DYMMY_BYTE      0x00
00040 
00041 /* Private macro -------------------------------------------------------------*/
00042 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00043 
00044 /* Private variables ---------------------------------------------------------*/
00045 USART_InitTypeDef USART_InitStructure;
00046 USART_ClockInitTypeDef USART_ClockInitStructure; 
00047 
00048 uint8_t TxBuffer1[] = "USART Synchronous Example: USARTy -> SPIy using TXE and RXNE Flags";
00049 uint8_t TxBuffer2[] = "USART Synchronous Example: SPIy -> USARTy using TXE and RXNE Flags";
00050 uint8_t RxBuffer1[TxBufferSize2];
00051 uint8_t RxBuffer2[TxBufferSize1];
00052 __IO uint8_t NbrOfDataToRead1 = TxBufferSize2;
00053 __IO uint8_t NbrOfDataToRead2 = TxBufferSize1;
00054 __IO uint8_t TxCounter1 = 0, RxCounter1 = 0;
00055 __IO uint8_t TxCounter2 = 0, RxCounter2 = 0;
00056 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED; 
00057 
00058 /* Private function prototypes -----------------------------------------------*/
00059 void RCC_Configuration(void);
00060 void GPIO_Configuration(void);
00061 void SPI_Configuration(void);
00062 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00063 
00064 /* Private functions ---------------------------------------------------------*/
00065 
00066 /**
00067   * @brief   Main program
00068   * @param  None
00069   * @retval None
00070   */
00071 int main(void)
00072 {
00073   /*!< At this stage the microcontroller clock setting is already configured, 
00074        this is done through SystemInit() function which is called from startup
00075        file (startup_stm32f10x_xx.s) before to branch to application main.
00076        To reconfigure the default setting of SystemInit() function, refer to
00077        system_stm32f10x.c file
00078      */     
00079        
00080   /* System Clocks Configuration */
00081   RCC_Configuration();
00082 
00083   /* Configure the GPIO ports */
00084   GPIO_Configuration();
00085 
00086   /* Configure the SPI */
00087   SPI_Configuration();
00088 
00089 /* USARTy configuration ------------------------------------------------------*/
00090   /* USARTy configured as follow:
00091         - BaudRate = 115200 baud  
00092         - Word Length = 8 Bits
00093         - One Stop Bit
00094         - No parity
00095         - Hardware flow control disabled (RTS and CTS signals)
00096         - Receive and transmit enabled
00097         - USART Clock Enabled
00098         - USART CPOL: Clock is active High
00099         - USART CPHA: Data is captured on the second edge 
00100         - USART LastBit: The clock pulse of the last data bit is output to 
00101                          the SCLK pin
00102   */
00103   USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
00104   USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
00105   USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
00106   USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
00107   USART_ClockInit(USARTy, &USART_ClockInitStructure);
00108 
00109   USART_InitStructure.USART_BaudRate = 115200;
00110   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00111   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00112   USART_InitStructure.USART_Parity = USART_Parity_No ;
00113   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00114   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00115   USART_Init(USARTy, &USART_InitStructure);
00116   
00117   /* Configure the USARTy */
00118   USART_Init(USARTy, &USART_InitStructure);
00119 
00120   /* Enable the USARTy */
00121   USART_Cmd(USARTy, ENABLE);
00122 
00123   while(NbrOfDataToRead2--)
00124   {
00125     /* Write one byte in the USARTy Transmit Data Register */
00126     USART_SendData(USARTy, TxBuffer1[TxCounter1++]);
00127     /* Wait until end of transmit */
00128     while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
00129     {
00130     }
00131     /* Wait the byte is entirtly received by SPIy */  
00132     while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET)
00133     {
00134     }
00135     /* Store the received byte in the RxBuffer2 */
00136     RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPIy);
00137   }
00138 
00139   /* Clear the USARTy Data Register */
00140   USART_ReceiveData(USARTy);
00141 
00142   while(NbrOfDataToRead1--)
00143   {
00144     /* Wait until end of transmit */
00145     while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE)== RESET)
00146     {
00147     }
00148     /* Write one byte in the SPIy Transmit Data Register */
00149     SPI_I2S_SendData(SPIy, TxBuffer2[TxCounter2++]);
00150 
00151     /* Send a Dummy byte to generate clock to slave */ 
00152     USART_SendData(USARTy, DYMMY_BYTE);
00153     /* Wait until end of transmit */
00154     while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
00155     {
00156     }
00157     /* Wait the byte is entirtly received by USARTy */
00158     while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET)
00159     {
00160     }
00161     /* Store the received byte in the RxBuffer1 */
00162     RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy);
00163   }
00164   
00165   /* Check the received data with the send ones */
00166   TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
00167   /* TransferStatus = PASSED, if the data transmitted from USARTy and  
00168      received by SPIy are the same */
00169   /* TransferStatus = FAILED, if the data transmitted from USARTy and 
00170      received by SPIy are different */
00171   TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
00172   /* TransferStatus = PASSED, if the data transmitted from SPIy and  
00173      received by USARTy are the same */
00174   /* TransferStatus = FAILED, if the data transmitted from SPIy and 
00175      received by USARTy are different */
00176 
00177   while (1)
00178   {
00179   }
00180 }
00181 
00182 /**
00183   * @brief  Configures the different system clocks.
00184   * @param  None
00185   * @retval None
00186   */
00187 void RCC_Configuration(void)
00188 {   
00189   /* Enable GPIO clock */
00190   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | SPIy_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00191 
00192   /* Enable USARTy Clock */
00193   RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE); 
00194   /* Enable SPIy Clock */
00195   RCC_APB2PeriphClockCmd(SPIy_CLK, ENABLE);    
00196 }
00197 
00198 /**
00199   * @brief  Configures the different GPIO ports.
00200   * @param  None
00201   * @retval None
00202   */
00203 void GPIO_Configuration(void)
00204 {
00205   GPIO_InitTypeDef GPIO_InitStructure;
00206 
00207   /* Configure USARTy TX and USARTy CK pins as alternate function push-pull */
00208   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin | USARTy_ClkPin;
00209   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00210   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00211   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00212 
00213   /* Configure SPI1 pins: SCK, MISO and MOSI */
00214   GPIO_InitStructure.GPIO_Pin = SPIy_SCKPin | SPIy_MISOPin | SPIy_MOSIPin;
00215   GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
00216 
00217   /* Configure USARTy RX as input floating */
00218   GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
00219   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00220   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00221 }
00222 
00223 /**
00224   * @brief  Configures the SPI.
00225   * @param  None
00226   * @retval None
00227   */
00228 void SPI_Configuration(void)
00229 {
00230   SPI_InitTypeDef SPI_InitStructure;
00231 
00232   SPI_StructInit(&SPI_InitStructure);
00233 
00234   SPI_I2S_DeInit(SPIy);
00235 
00236   /* SPIy Config */
00237   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
00238   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
00239   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
00240   SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
00241   SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
00242   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
00243   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
00244   
00245   /* Configure SPIy */
00246   SPI_Init(SPIy, &SPI_InitStructure);
00247 
00248   /* SPIy enable */
00249   SPI_Cmd(SPIy, ENABLE);
00250 }
00251 
00252 /**
00253   * @brief  Compares two buffers.
00254   * @param  pBuffer1, pBuffer2: buffers to be compared.
00255   * @param  BufferLength: buffer's length
00256   * @retval PASSED: pBuffer1 identical to pBuffer2
00257   *   FAILED: pBuffer1 differs from pBuffer2
00258   */
00259 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00260 {
00261   while(BufferLength--)
00262   {
00263     if(*pBuffer1 != *pBuffer2)
00264     {
00265       return FAILED;
00266     }
00267 
00268     pBuffer1++;
00269     pBuffer2++;
00270   }
00271 
00272   return PASSED;
00273 }
00274 
00275 #ifdef  USE_FULL_ASSERT
00276 
00277 /**
00278   * @brief  Reports the name of the source file and the source line number
00279   *         where the assert_param error has occurred.
00280   * @param  file: pointer to the source file name
00281   * @param  line: assert_param error line source number
00282   * @retval None
00283   */
00284 void assert_failed(uint8_t* file, uint32_t line)
00285 { 
00286   /* User can add his own implementation to report the file name and line number,
00287      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00288 
00289   /* Infinite loop */
00290   while (1)
00291   {
00292   }
00293 }
00294 #endif
00295 
00296 /**
00297   * @}
00298   */ 
00299 
00300 /**
00301   * @}
00302   */ 
00303 
00304 /******************* (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