cancel
Showing results for 
Search instead for 
Did you mean: 

Use of UART HAL Drivers

MFran.10
Associate II

Hello,

I'm designing an application where a PCB with STM32F031C6 has to receive a minimum of 5 bytes via an half-duplex communication. Transmission is delimited by special character, ASCII is used. Ideally, the transmission would be composed by 3 data bytes and 2 delimiters (minimum).

I don't properly understand if the HAL_UART_Receive_IT function suits my interest (i see it works with n° of bytes) and, more importantly, how the HAL_UART_Error_Callback is triggered.

I don't understand the so-called "half transfer" callbacks too.

The documentation is not helping me, i already read some PDFs...

Anyone who understood enough to explain it to me?

Thank you in advance,

Mirco

1 ACCEPTED SOLUTION

Accepted Solutions

More importantly, understand that every byte received generates an interrupt. The IRQ Handler calls into the HAL, and when the HAL accumulates the data length you requested it calls your routine.

This is unduly circuitous in my opinion, and you'd be well advised just to service the UART in IRQ the same way people have done for a quarter century.

The Half/Complete Transfer is a DMA terminology used where the buffer length described to the DMA controller is split in two to provide a ping-pong method to access the inactive half of the buffer.

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

View solution in original post

6 REPLIES 6
Bob S
Principal

In order to receive variable length data, or data that may arrive at any time, you need to either create your own interrupt-driven code, or use the HAL_UART_Receive_DMA() function, with the DMA configured for circular mode. You then ignore the half transfer complete and full transfer complete interrupts. There have been several threads on this forum about this. Try searching for UART and DMA.

Warning: When using the HAL_UART_Receive_DMA() function, the HAL code will disable the receiver if the serial port reports ANY error. You need to add code to the error callback to re-start the receive function. Or edit the HAL UART code directly to keep the receive function going on error.

S.Ma
Principal

Uart is easy to handle with low layer. First, use interrupt for receiving byte by byte like it is done on most 8 bit mcu. In the interrupt, check for your end character, and set a flag for main loop to process the message and block any further incoming. Next, use hw feature to detect the end character, finally, manage a rolling buffer fifo to keep accepting incoming bytes while main loop is still processing any received messages. Later you might consider using dma cycling a buffer and check every 1msec.

More importantly, understand that every byte received generates an interrupt. The IRQ Handler calls into the HAL, and when the HAL accumulates the data length you requested it calls your routine.

This is unduly circuitous in my opinion, and you'd be well advised just to service the UART in IRQ the same way people have done for a quarter century.

The Half/Complete Transfer is a DMA terminology used where the buffer length described to the DMA controller is split in two to provide a ping-pong method to access the inactive half of the buffer.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
MFran.10
Associate II

I will probably stick with the classic method as you both said, placing the code in the stmxxx_it.c routine.

Thank you for the explanation!

Thanks! What do you mean by "use hw feature to detect the end character"? Which hw feature?