cancel
Showing results for 
Search instead for 
Did you mean: 

UART RX

SGian.1
Senior

Hi there!

Here i'm with another question about UART. I know i have a tree mode for receive data from uart

1) Polling

2) Interrupt

3) DMA

i know the difference , but i'm soo confiuse about :

Example : I work with interrupt where i put this declaration

HAL_UART_Receive_IT(&huart1, rx, 4);

in the main before the while ??? , and why ? afther i receive a data i need the clear some register ? if yes who ?

and the same question is for DMA .

If i diden't know the lenght of the data how inizialize my array ? and in this case if better to work with interrupt and not with DMA ?

In the end where i can find information about UART and register i need the check and clear ?

Sorry but really i found information in the internet but right now i'm more confuse the before

Thank you soo much

Sergio

6 REPLIES 6
Ozone
Lead

> Example : I work with interrupt where i put this declaration

> HAL_UART_Receive_IT(&huart1, rx, 4);

I would suggest to check the implementation of this function, and see what it does.

I don't use Cube/HAL, for good reasons.

Besides of others, the handling of interrupts is obscure and overinflated for almost all purposes.

> In the end where i can find information about UART and register i need the check and clear ?

The first source would be the reference manual of the MCU, i.e. the UART/USART section.

Polling is the least recommendable method, wasting performance and prone to overflow errors.

DMA is fine for sending, but tricky for reception, especially when your protocol consists of periodical "burst" transmissions of unknown length.

And it complicates error handling. Especially with external devices, receive errors are not so rare.

Your code must be able to deal with receive errors, and be able to re-synchronize.

I used to go with interrupt handling for reception.

You usually want to enable the RXNE and error interrupts.

In the handler, you retrieve the received character, and do basic protocol checks (buffer handling, buffer size check, termination character etc.)

Check for error interrupt conditions as well (and clear them), or else reception will be blocked.

I used to pass complete messages from the handler to the main loop - whatever that is in the context of your protocol.

SGian.1
Senior

I understand.

Thank you

Sergio

Radosław
Senior II

As You notice, receiving is more tricky, we can't be sure how many data will be send. So with DMA i receive in circular mode. is very simply works perfect.

SGian.1
Senior

yes is not easy. My problem is the data are not fixed but change. I think i need the use interrupt routine. And the problem is need to comunicate with ESP32 with AT command and right now give me a little comunications problem

Thanks a lot

Sergio

Hi @SGian.1​ ,

For more explanation of the UART behavior mainly when using interrupt and DMA modes, you can refer to this article: STM32 UART DMA RX/TX.

The topic "Receiving data with UART and DMA when application does not know in advance size of bytes to be received" may answer your request.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

SGian.1
Senior

Sorry for the late answert i was busy . Please cann you tell me what do you wanna do ? mabye is more easy help you