Skip to main content
Vel
Associate III
Nov 13, 2019 7:16 AM UTC
Solved

How can work with UART in Interrupt mode transmit and receive some amount of data in byte by byte?

Hi,

I am working on two STM32 microcontrollers communicating over UART with baud rate 9600. The functional requirement states that I need to transmit and receive data at both microcontrollers. Currently this is not happening as the transmit and receive interrupts conflict I suppose. Please guide me for this problem statement.

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Nov 13, 2019 2:00 PM UTC

You're familiar with the concept of ring/fifo buffers, right?

​

The USART IRQ Handler should test for the RXNE status and move the data register to a receive buffer, and then check TXE and if a transmit buffer has any data, if there is data that gets sent, if not the TXE is disabled.

​

When you put data in the transmit buffer you also enable the TXE interrupt again.​

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

6 replies

waclawek.jan
Super User
Nov 13, 2019 7:32 AM UTC

https://community.st.com/s/question/0D50X0000BVpldhSQB/how-to-have-continuous-uart-burst-transmit-while-ensuring-receive-works-

JW

@Vincent Onde​ 

So this might be the basic "how to bring up UART" AN. Starting with the "test pins by wiggling them as Out/In", moving on to trivial polled implementation perhaps first using a loopback, then connecting to real counterpart and doing a software echo (perhaps with slight modifications); then moving on to the simplest interrupt based one, outlined above, with highlighting the overrun gotcha. Discuss RS232/level conversion issues. Discuss baudrates, primary clock precision impact - mutual mismatch of both parties, impact of fractional baudrate, impact of oversampling (maybe this all ought to be another appnote); hint about autobaud (which ought to be a separate AN in the UART dozen), point to the bintbanged USART appnote.

JW

Tesla DeLorean
Guru
Nov 13, 2019 7:37 AM UTC

One can handle with a single interrupt, keep it simple, and only input / output to a ring buffer based on the flagging interrupt sources.​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Vel
VelAuthor
Associate III
Nov 13, 2019 11:50 AM UTC

Hi @Community member​ 

Can you please elaborate that in detail.

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Nov 13, 2019 2:00 PM UTC

You're familiar with the concept of ring/fifo buffers, right?

​

The USART IRQ Handler should test for the RXNE status and move the data register to a receive buffer, and then check TXE and if a transmit buffer has any data, if there is data that gets sent, if not the TXE is disabled.

​

When you put data in the transmit buffer you also enable the TXE interrupt again.​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Ken Macfarlane
Associate
Dec 4, 2024 12:43 PM UTC

How do you access bits in registers with the HAL? There's a lot of detail that's masked & not available by using the HAL functions. Also, how do you generate timeouts? In a simple polled UART application, I just call HAL_UART_Receive_IT with a 10 second timeout to give me time to type a string into my terminal emulator. The interrupt version doesn't wait, so how do I gather characters into a buffer?

Karl Yamashita
Principal
Dec 4, 2024 10:00 PM UTC

@Ken Macfarlane wrote:

How do you access bits in registers with the HAL? There's a lot of detail that's masked & not available by using the HAL functions. Also, how do you generate timeouts? In a simple polled UART application, I just call HAL_UART_Receive_IT with a 10 second timeout to give me time to type a string into my terminal emulator. The interrupt version doesn't wait, so how do I gather characters into a buffer?


Use HAL_UART_Receive_IT to receive 1 byte at a time and you don't need a timeout. Set the terminal to send a LF when you press Return. As you type characters, you'll get an interrupt. Save each character received to a buffer and when you get a LF, you can set a flag to indicate you have a complete message..  

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source