cancel
Showing results for 
Search instead for 
Did you mean: 

STM32Cube UART Receive Interrupt

h2399
Associate II
Posted on September 11, 2014 at 17:24

Dear Friends,

Hi,

I want to get an interrupt by receiving each character on UART port. How can I use HAL_UART_Receive_IT() and HAL_UART_RxCpltCallback() functions? When I call HAL_UART_Receive_IT() function its call USART1_IRQHandler I want to know is it a

correct approach

to call HAL_UART_Receive_IT() function before each receiving character?

 

 

Thanks.

#stm32cube-interrupt-uart
12 REPLIES 12
Posted on September 12, 2014 at 18:47

Hi Mohajer,

In fact, the HAL_UART_Receive_IT() API is designed to receive n byte in interrupt mode with (n: “Sizeâ€� parameter). 

It's is called under UART IRQ for each byte received and the user callback HAL_UART_RxCpltCallback() is only executed at the end of the whole receive process, just after disabling the UART receive interrupt and receiving the last data.

You just need to call this UART API: HAL_UART_IRQHandler() under the defined UART IRQ (USART1_IRQHandler) within the stm32fxxx.c file. (For more details please refer to the UART_Hyperterminal_IT example).

If you want get a callback HAL_UART_RxCpltCallback() for each character, in this case, you have to call the HAL_UART_Receive_IT() function (w/ Size parameter = 1) before each receiving character.

Hope that can help you, to have insight into the UART receive in interrupt mode.

Regards.

h2399
Associate II
Posted on September 13, 2014 at 16:16

Hi Heisenberg,

Thank you for your good reply.

I have enabled UART global interrupt.

By using debugger the function void USART1_IRQHandler(void){...} won't be called. When I use HAL_UART_Receive_IT() in a loop I can receive characters

and function void USART1_IRQHandler(void){...} will be called.

The function void USART1_IRQHandler(void){...} is supposed to be called through receiving each character, but it didn't. Would you please help me to solve the problem?

Also, where can I find UART_Hyperterminal_IT example?

Regards.

jasonp4113
Associate II
Posted on September 17, 2014 at 02:45

Hi

What I did to achieve this was to modify HAL_UART.C -> UART_Receive_IT()  to add a weak callback for every byte. This way you can max out the size and be able to interrogate each byte without reloading the peripheral after every byte received (saving processor time). You can then do whatever you need if you detect a certain character. 

snippet:

    if(--huart->RxXferCount == 0)

    {

__HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);

      /* Check if a transmit process is ongoing or not */

      if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 

      {

        huart->State = HAL_UART_STATE_BUSY_TX;

      }

      else

      {

        /* Disable the UART Parity Error Interrupt */

        __HAL_UART_DISABLE_IT(huart, UART_IT_PE);

        /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */

        __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);

        huart->State = HAL_UART_STATE_READY;

      }

HAL_UART_RxCpltCallback(huart);

    }

else

{

HAL_UART_RxByteCallback(huart);

}

    return HAL_OK;

  }

  else

  {

return HAL_BUSY; 

  }

h2399
Associate II
Posted on September 28, 2014 at 14:01

Hi JasonP,

Thank you for your post.

But the main problem is that interrupt never gets generated when I send data to the MCU.

Maybe this is a bug in the STM32cube!

Posted on October 07, 2014 at 10:06

Hi all,

After discussion with our development team, it turned out that this implementation is not adequate as the interruption sub-routine is polling the status register during the 2 last characters period.

We have already the limitation in the STM32Cube HAL bugs list, and it will be fixed in next releases of F4. Please continue submitting your valuable feedback.

Regards,

Heisenberg.
mas
Associate
Posted on January 01, 2016 at 17:38

Hi Developers!

A sample solution:

Step1. Add this code in main function before while(1):

  __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);

  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(USART1_IRQn);

Step2. Add this code in stm32f1xx_it.c:

uint8_t ich;

void USART1_IRQHandler(void)

{

  ich = USART1->DR;

  HAL_UART_Transmit(&huart1, &ich,sizeof(ich), HAL_MAX_DELAY);

}

prabhat
Associate
Posted on July 31, 2015 at 12:24

Hi Mohajer,

I think you have not enabled interrupt correctly so use following line after the USART initialization.

__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);

HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);

  HAL_NVIC_EnableIRQ(USART1_IRQn);

I hope it will resolve your problem 

alberto2399
Associate II
Posted on December 14, 2015 at 10:21

Hi all,

I am using a STM32F415RG and I am trying to make the uart reception based on interrupts work (HAL_UART_Receive_IT). It's my first experience with fw development and though there are many examples available online I have always problems because in these examples are used homemade functions whose description is missing, or it isn't specified in which file the code should be written, or some things are taken for granted.

I am looking for a basic tutorial/example with a step by step explanation of what I should do to make HAL_UART_Receive_IT work and how I should configure interrupts. Do you know such an example/tutorial? Could you kindly help me? Thank you!
hbarta2
Associate III
Posted on December 14, 2015 at 18:15

Hi Cel,

Are you aware of the examples that come bundled with the HAL library download? For example using the processor/board I'm working with it would be found in

E:\STM32Cube_FW_F4_V1.5.0\Projects\STM32F429I-Discovery\Examples\UART\UART_TwoBoards_ComIT

These use the HAL library and all of the configuration is typically in main.c

If you install the libraries via STM32CubeMX then you will have to figure out where MX stores the downloaded libraries. That seems to be listed in MX in the Help -> Updater Settings'' window.