AnsweredAssumed Answered

stm32f373 interrupt problem

Question asked by yang.seungman on Oct 27, 2016
Latest reply on Oct 27, 2016 by FTITI.Walid

Hello

I am using stm32f373 chip to implement SDADC, USART, Timer, etc, and now I have a problem with interrupt.

I tested USART1 interrupt code as below. If I send any character to STM, then it would receive it and send back to my computer. But I couldn't receive anything.

 

 

void USART1_IRQHandler(void)
{
    char receive_data;
 
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        receive_data = USART_ReceiveData(USART1) & 0xFF;
       
        USART_SendData(USART1, receive_data);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );       
       
        USART_ClearITPendingBit(USART1, USART_IT_RXNE); 
    }
   
}

void usart1_test_interrupt(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
   

    /* AHB Clock enable for USART(GPIOA9, A10) */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
   
    /* USART1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
    /* USART1 Pins configuration **************************************************/
    GPIO_DeInit(GPIOA);
   
    /* Connect pin to Periph */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);   
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
   
    /* Configure pins as AF pushpull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    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_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USARTx configured as follow:
  - BaudRate = 115200 baud 
  - Word Length = 8 Bits
  - Stop Bit = 1 Stop Bit
  - Parity = No Parity
  - Hardware flow control disabled (RTS and CTS signals)
  - Receive and transmit enabled
  */
 
    USART_DeInit(USART1);
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    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(USART1, &USART_InitStructure);
   
    /* Enable the USART1 */
    USART_Cmd(USART1, ENABLE);
   
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   
    /* Configure one bit for preemption priority */
    //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
   
    /* Enable the USART1 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
  
   
   
    /* Enable the USART1 */
    USART_Cmd(USART1, ENABLE);  
}

 

 

 

 

Now I tested USART1 polling code as below which implement just same thing but using polling method instead of using interrupt method. Then it works!

 

 

 

void usart1_test_polling(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
   
    char receive_data;

    /* AHB Clock enable for USART(GPIOA9, A10) */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
   
    /* USART1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);   

   
   
    /* USART1 Pins configuration **************************************************/
    GPIO_DeInit(GPIOA);
   
    /* Connect pin to Periph */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);   
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
   
    /* Configure pins as AF pushpull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    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_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USARTx configured as follow:
  - BaudRate = 115200 baud 
  - Word Length = 8 Bits
  - Stop Bit = 1 Stop Bit
  - Parity = No Parity
  - Hardware flow control disabled (RTS and CTS signals)
  - Receive and transmit enabled
  */
 
    USART_DeInit(USART1);
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    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(USART1, &USART_InitStructure);

    /* Enable the USART1 */
    USART_Cmd(USART1, ENABLE);  
   
    while(1)
    {
   
        while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET );
        receive_data = USART_ReceiveData(USART1) & 0xFF;
       
        USART_SendData(USART1, receive_data);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
       
        if( receive_data == 'x' )
            break;
    }       
}

 

 

 

 

 

So I concluded that I had a problem with interrupt, but I couldn't find the reason. Please help me. Thank you

 

Outcomes