Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

stm32 - How UART Rx interrupt routine is implemented for variable length data?

I have a custom STM32F4 MCU board. I need to receive different types of sequences from Master board in a RS485 n/w. Lengths of data to be received is variable say 10 bytes, 25 bytes and so on. I have to handle every sequence and respond accordingly.

How to properly make use of HAL functions HAL_UART_Receive_IT and HAL_UART_RxCpltCallback to receive variable data properly?

I see that 3rd argument of HAL_UART_Receive_IT is looking for number of bytes to be fixed like shown:

HAL_UART_Receive_IT(&huart1, &uart1_rx_byte, no_of_bytes);

Please suggest a good implementation...

Receiving frame format:
| Start byte | Slave Addr | Byte count | Func. ID | Data1 | Data2 |...|Data n| Checksum byte 1 | Checksum byte 2 |

Where Byte count = no. of bytes from Func. ID to Checksum byte 2

question from:https://stackoverflow.com/questions/65930701/how-uart-rx-interrupt-routine-is-implemented-for-variable-length-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

pleas take a look at this tutorial:

http://www.bepat.de/2020/12/02/stm32f103c8-uart-with-dma-buffer-and-idle-detection/

you have to adapt it a bit for the F4, the correct register for counting the data in the RX-Buffer is

huart->hdmarx->Instance->NDTR

/wo the "C"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...