Hi guys, I am a beginner in ESP32 programming.
For self-learning purposes, I am planning to write a small program that receives data via UART, and then executes some calculations on the received data. The reception and the calculation with the received data would compose an infinite routine, so consider e.g. a moving average calculator (with a fix moving window size).
I assume that I should avoid calculations directly on the RX buffer data. Instead, I assume that I should copy some part of the RX buffer, i.e., to work with the copy of the recent few bytes received via UART.
To be consistent with the data, I want to assure that the content of the RX buffer does not change while I am copying several bytes from it.
Shall I realize this "protection" by myself, e.g., using mutex in the copy procedure?
Or is this protection already there, working in the UART reception in the background, in the firmware and via the ESP-IDF utilities (the UART-related functions, such as uart_read_bytes().)
Thank you for your help, and sorry if this was a dummy question.
UART, RX buffer: shall I "lock" it while I am copying data from it?
-
career_changer
- Posts: 2
- Joined: Sat Sep 13, 2025 11:12 pm
-
MicroController
- Posts: 2705
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: UART, RX buffer: shall I "lock" it while I am copying data from it?
uart_read_bytes() is synchronous, thread-safe, and 'atomic' w.r.t. the UART interrupt. So the data you get out of it will not be corrupted by concurrency.
Notice that you provide the buffer where uart_read_bytes() will copy the RX data to. If you don't share that buffer across tasks there will also be no concurrency issue. I.o.w., you cannot access the internal buffers of the UART (driver) directly but will only ever get your own copy in the first place.
Notice that you provide the buffer where uart_read_bytes() will copy the RX data to. If you don't share that buffer across tasks there will also be no concurrency issue. I.o.w., you cannot access the internal buffers of the UART (driver) directly but will only ever get your own copy in the first place.
-
career_changer
- Posts: 2
- Joined: Sat Sep 13, 2025 11:12 pm
Re: UART, RX buffer: shall I "lock" it while I am copying data from it?
Thank you for your professional an informative answer. It sounds great!
Who is online
Users browsing this forum: Baidu [Spider], Bing [Bot], Qwantbot and 3 guests