2015-12-19 10:15 AM
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
2015-12-20 08:58 AM
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:
USARTx_IRQn
interrupt and implement the correspondingUSARTx_IRQHandler()
ISR.HAL_UART_IRQHandler()
inside theUSARTx_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.HAL_UART_Transmit_IT()
andHAL_UART_Receive_IT()
to exchange data over the UART.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.2015-12-20 11:37 AM
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 RXCpltCallback2015-12-20 11:54 AM
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?
2015-12-20 01:45 PM
I suggest you to give a look to the UART_TwoBoards_ComIT example provided by ST in all Cube HALs.
2015-12-21 11:24 AM
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?2015-12-21 12:23 PM
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.2015-12-22 06:04 AM
Thank you cnoviello for your help!