STM32F10x_StdPeriph_Examples/USART/Interrupt/main.c

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