cancel
Showing results for 
Search instead for 
Did you mean: 

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

Vel
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

6 REPLIES 6
waclawek.jan
Super User

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

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
Up vote any posts that you find helpful, it shows what's working..

Hi @Community member​ 

Can you please elaborate that in detail.

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
Up vote any posts that you find helpful, it shows what's working..
Ken Macfarlane
Associate II

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?


@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..  

I was told that if a devices starts to smoke, put the smoke back in. I guess I never got all the smoke because the device never worked afterwards.
Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.