AnsweredAssumed Answered

The USART Problem of STM32F303 discovery Board

Question asked by O'Neill Shih on Jun 3, 2015
Latest reply on Jun 15, 2015 by O'Neill Shih
Hi! everybody ! I get a problem on the UART3 of 303 discovery board .
Originally, I test my program on the UART2 and work fine on discovery board .
But it didn't work when I re-config it to UART3 .
I compare the workable code with  NG Code and didn't find anything wield .
I try to put UART2 and UART3 work together , but failed .
But the UART2 work fine after I mark the ConfigUart3Int() .
it means It the UART3 didn't work and enable UART3 Interrupt cause UART2 didn't work too . 
Could someone tell me what's wrong ? thanks! 

Following are what I wrote :

int main(void)
{

     ConfigSystemIO();
     ConfigSpiMaster();     
     ConfigPwmTimer();
     ConfigUartxIO();
     ConfigUart3IO();
     //ConfigUartxRxDMA();
     ConfigUartxInt();
     //ConfigUart3Int();  // mark it , the uart2 will work  OK ! Why ? 
     SelectAout(AO_TEMP);     
     BSD_LEDOn(LD1);
     BSD_LEDOn(LD2);
     BSD_LEDOff(LD3);
     BSD_LEDOff(LD4);


    while (1)
    {
         DebugPort8up(0x11);
      CmdInterpreter();
      RfTask();

    }
}

/* USART Communication boards Interface */
#define USARTx                                                    USART2
#define USARTx_CLK                                          RCC_APB1Periph_USART2
#define USARTx_APBPERIPHCLOCK              RCC_APB1PeriphClockCmd


#define USARTx_TX_PIN                                    GPIO_Pin_2                
#define USARTx_TX_GPIO_PORT                    GPIOA                       
#define USARTx_TX_GPIO_CLK                      RCC_AHBPeriph_GPIOA
#define USARTx_TX_SOURCE                          GPIO_PinSource2
#define USARTx_TX_AF                                     GPIO_AF_7
 
#define USARTx_RX_PIN                                   GPIO_Pin_3               
#define USARTx_RX_GPIO_PORT                   GPIOA                    
#define USARTx_RX_GPIO_CLK                     RCC_AHBPeriph_GPIOA
#define USARTx_RX_SOURCE                        GPIO_PinSource3
#define USARTx_RX_AF                                   GPIO_AF_7


static void ConfigUartxIO(void)
{
     /* Enable GPIO clock */
     RCC_AHBPeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
     
     /* Enable USART clock */
     USARTx_APBPERIPHCLOCK(USARTx_CLK, ENABLE);
      
     /* Connect PXx to USARTx_Tx */
     GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
   
     /* Configure USART Tx and Rx as alternate function push-pull */
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
     
     GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
     GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
     
       /* Connect PXx to USARTx_Rx */
     GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
     
     GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
     GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
     
     /* USARTx configuration ----------------------------------------------------*/
     /* USARTx configured as follow:
     - BaudRate = 115200 baud  
     - Word Length = 8 Bits
     - one Stop Bit
     - No parity
     - Hardware flow control disabled (RTS and CTS signals)
     - Receive and transmit enabled
     */
     USART_InitStructure.USART_BaudRate = 115200;
     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
     USART_InitStructure.USART_StopBits = USART_StopBits_1;
     /* When using Parity the word length must be configured to 9 bits */
     USART_InitStructure.USART_Parity = USART_Parity_No;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
     USART_Init(USARTx, &USART_InitStructure);
       
     /* Enable USART */
     USART_Cmd(USARTx, ENABLE);
}

static void ConfigUart3IO(void)
{
     /* Enable GPIO clock */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC , ENABLE);
     
     /* Enable USART clock */
     USARTx_APBPERIPHCLOCK(RCC_APB1Periph_USART3, ENABLE);
    
    
     /* Connect PXx to USARTx_Tx */
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7);

     /* Configure USART Tx and Rx as alternate function push-pull */
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
     
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
     GPIO_Init(GPIOC, &GPIO_InitStructure);
     
       /* Connect PXx to USARTx_Rx */
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_7);
     
     GPIO_InitStructure.GPIO_Pin = GPIO_PinSource11;
     GPIO_Init(GPIOC, &GPIO_InitStructure);
     
     /* USARTx configuration ----------------------------------------------------*/
     /* USARTx configured as follow:
     - BaudRate = 115200 baud  
     - Word Length = 8 Bits
     - one Stop Bit
     - No parity
     - Hardware flow control disabled (RTS and CTS signals)
     - Receive and transmit enabled
     */
     USART_InitStructure.USART_BaudRate = 115200;
     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
     USART_InitStructure.USART_StopBits = USART_StopBits_1;
     /* When using Parity the word length must be configured to 9 bits */
     USART_InitStructure.USART_Parity = USART_Parity_No;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
     USART_Init(USART3, &USART_InitStructure);
       
     /* Enable USART */
     USART_Cmd(USART3, ENABLE);
}

static void ConfigUartxInt(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;


  /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);   
  USART_ITConfig(USART2, USART_IT_RXNE , ENABLE);
}
 
static void ConfigUart3Int(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;


  /* Enable the USART3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);   
  USART_ITConfig(USART3, USART_IT_RXNE , ENABLE);
}

void USART3_IRQHandler(void)
{
     uint8_t i ;
      if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {
          
           uartCmdBuffer[uartRxCnt++] = USART_ReceiveData(USART3) ;
           if ( uartRxCnt == uartCmdNo )
           {
                        uartRxCnt = 0 ;
                uartCmd = YES ;
                BSD_LEDToggle(LD3);
                DebugPort8up(0x77);
                for ( i =0 ; i < uartCmdNo ;i++) DebugPort8up( uartCmdBuffer[i]);
           }
    }     
}


void USART2_IRQHandler(void)
{
     uint8_t i ;
      if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {
          
           uartCmdBuffer[uartRxCnt++] = USART_ReceiveData(USART2) ;
           if ( uartRxCnt == uartCmdNo )
           {
                        uartRxCnt = 0 ;
                uartCmd = YES ;
                BSD_LEDToggle(LD2);
                DebugPort8up(0x77);
                for ( i =0 ; i < uartCmdNo ;i++) DebugPort8up( uartCmdBuffer[i]);
           }
    }     
}  

Outcomes