Skip to main content
Taehyeong Gu
Associate II
April 18, 2017
Question

A question about UART interrupt

  • April 18, 2017
  • 3 replies
  • 2204 views
Posted on April 18, 2017 at 14:45

In order to configure UART interrupt receive, I put HAL_UART_Receive_IT function in HAL_UART_RxCpltCallback.

but it doesnt work. below is my callback code.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

uint8_t i;

if (huart->Instance == USART2) //current UART

{

if (Rx_indx==0) {for (i=0;i<RXBUFFERBYTE;i++) aRxBuffer[i]=0;} //clear Rx_Buffer before receiving new data

if (Rx_data[0]!=10) //if received data different from ascii 10

{

aRxBuffer[Rx_indx++]=Rx_data[0]; //add data to Rx_Buffer

}

else //if received data = 10

{

Rx_indx=0;

USART2_RX_STANBY=1;//transfer complete, data is ready to read

}

if(HAL_UART_Receive_IT(&huart2,Rx_data, 1) != HAL_OK)//activate UART receive interrupt every time

{

Error_Handler();

}

}

}

The main program has HAL_UART_Receive_IT in beginning part. so i put some character of ASCII and my program reach if(HAL_UART_Receive_IT(&huart2,Rx_data, 1) != HAL_OK) . I checked that HAL_UART_Receive_IT returned HAL_OK then it means preparation of UART receive interrupt. 

but the program doenst take interrupt when I send ASCII to MCU.

I have no clue.. anyone who have had same experience??

help me please.

Thank you.

    This topic has been closed for replies.

    3 replies

    Technical Moderator
    April 18, 2017
    Posted on April 18, 2017 at 15:56

    Hi i, 

    You may refer to the UART example of the STM32cube firmware package relevant to the device that you are using.

    The example help you in your project to ensure UART transmission and reception with Interrupt.

    You can use STM32CubeMx tool which help you to develop the basic configuration of your application and generate an initialization code.

     

    Imen

    Alex R
    Associate II
    April 18, 2017
    Posted on April 18, 2017 at 18:10

    Hi,

    1. Make sure the USART2 Global Interrupt is enabled (See NVIC settings in the USART2 Configuration in STM32CubeMx if you are using it).

    2. I don't think you need to use HAL_UART_Receive_IT in the Interrupt handler. This call is only necessary one time at initialization time.

    Regards,

    Alex

    Taehyeong Gu
    Associate II
    April 19, 2017
    Posted on April 19, 2017 at 07:44

    Thank you. guy

    1. I enabled USART2 global Interrupt by HAL_UART_Receive_IT  in beginning part of my program.

    2. I am a little confused of the fact that HAL_UART_Receive_IT  or  __HAL_USART_ENABLE_IT should be executed to wait for next UART Receive Interrupt whenever UART Receive ends.  where can i get a manual related with it?? 

    I will update this post after making sure that HAL_UART_Receive_IT should be executed only one time by some experiment.

    Taehyeong Gu
    Associate II
    April 19, 2017
    Posted on April 19, 2017 at 08:05

    I executed the same program and it worked but has different problem.

    my main while program is below

    while (1)

    {

    if(TIM2_Loop_On==1)

    {

    if(USART2_RX_STANBY==1)

    {

    sprintf(aTxBuffer,'%s \n',aRxBuffer);

    if(HAL_UART_Transmit(&huart2, aTxBuffer, TXBUFFERSIZE, 1000)!= HAL_OK)

    {

    Error_Handler();

    }

    USART2_RX_STANBY=0;

    }

    /*

    sprintf(aTxBuffer,'welcome \n');

    if(HAL_UART_Transmit(&huart2, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 1000)!= HAL_OK)

    {

    Error_Handler();

    }

    */

    TIM2_Loop_On=0;

    }

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

    }

    /* USER CODE END 3 */

    }

    When i executed this program, i can get same character as what i send.

    I erased /* */(annotation) then i can get 'welcome' message at a regular interval.

    but when i send some character, my program stops.

    I guess the problem is when Rx Interrupt and Tx routine happen at the same time, program counter enter the Error_Handler().

    so how can i use Rx interrupt and Tx routine together?

    Thank you

    ali rostami
    Associate III
    October 4, 2017
    Posted on October 04, 2017 at 06:06

    Could you solve your problem?

    What was the version of your HAl library ?

    Tesla DeLorean
    Guru
    October 4, 2017
    Posted on October 04, 2017 at 06:38

    Evidence suggests an STM32L0 version of HAL..

    I will observe that it's not unduly hard to code very efficient register based IRQ Handlers for (LP)U(S)ART on the L0 parts. The Rx and Tx can definitely occur concurrently.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..