cancel
Showing results for 
Search instead for 
Did you mean: 

Uart interrupt

martin239955
Associate II
Posted on December 19, 2015 at 19:15

Hello,

I tried to get working the uart with interrupt. I can send and receive data by polling with the function HAL_UART_Receive. First how I can do to check if a byte is in the data_register, because when I don't send a byte HAL_UART_Receive waits until time out, but I would check and then execute the other code and check if byte is received anytime later? The other thing is, how does it work to enable interrupt for uart. At the moment I have:

HAL_NVIC_SetPriority(USART1_IRQ, 4,1);
HAL_NVIC_EnableIRQ(USART1_IRQ);

I found the function HAL_UART_Receive_IT(...) but I didn't understand how it works. Can anyone explain me? Martin
7 REPLIES 7
carmine
Associate II
Posted on December 20, 2015 at 17:58

Strictly speaking, using the HAL_UART mode in non-blocking mode (that is, interrupt mode) it is not so different from how you use it in polling mode. These are the minimum steps needed:

  • Enable the

    USARTx_IRQn

    interrupt and implement the corresponding

    USARTx_IRQHandler()

    ISR.
  • Call

    HAL_UART_IRQHandler()

    inside the

    USARTx_IRQHandler()

    passing the pointer to the

    UART_HandleTypeDef

    of the UART

    : this will perform all activities related to the management of interrupts generated by the UART peripheral.
  • Use the functions

    HAL_UART_Transmit_IT()

    and

    HAL_UART_Receive_IT()

    to exchange data over the UART.
  • Rearrange your application code to deal with asynchronous events.

Pay also attention to the priority of the 

USARTx_IRQn 

interrupt. The priority you have assigned to the IRQ works only if the priority grouping is higher than

NVIC_PRIORITYGROUP_2

 (I'm assuming you are working on a Cortex-M3 or higher).

Hope it helps.

martin239955
Associate II
Posted on December 20, 2015 at 20:37

Thank you cnoviello, but the UART_Receive_IT has as parameter a pointer to a uint8, that means that the received data is saved in this variable, so I have to have this variable global?

But there don't exist a UARTx_IRQHandler, there exist only a UART_IRQHandler which handles the IRQ and call then the RXCpltCallback

martin239955
Associate II
Posted on December 20, 2015 at 20:54

Ok now it jumps into the USART1_IRQHandler, but when I would call the UART_IRQHandler I need the Uart_Handle_TypeDef. That means I have to set them to global?

carmine
Associate II
Posted on December 20, 2015 at 22:45

I suggest you to give a look to the UART_TwoBoards_ComIT example provided by ST in all Cube HALs.

martin239955
Associate II
Posted on December 21, 2015 at 20:24

Yes I read them. The interrupt works but I wouldn't set my UART_HandleTypeDef variable as global. Is there another possibility?

When the USART1_IRQHandler was called how I can give the to HAL_UART_IRQHandler function my uart variable as parameter when I don't want to set them global?
carmine
Associate II
Posted on December 21, 2015 at 21:23

You simply can't, because the Cortex-M core is not designed to pass parameters to ISR routines, and I can't see the reason to do that. An interrupt is just an asynchronous notification that says to you that something happened.

The only way I can see is to have a ''custom'' HAL_UART_IRQHandler() function that has inside hardcoded the reference to the UART you are using, which for a given STM32 MCU is mapped to a specific address in the peripheral memory. But this means that you need to rearrange the HAL code at your needs. Honestly speaking, I think that for so relative ''simple'' architectures having a global variable defined somewhere in the source code is not a bad programming style, as long as you know what exactly you are doing.

martin239955
Associate II
Posted on December 22, 2015 at 15:04

Thank you cnoviello for your help!