-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes HardwareSerial::availableForWrite + setTxBufferSize #6998
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test code:
#include <Arduino.h>
#define BUFFER_SIZE 256
HardwareSerial HWS{1};
void setup() {
Serial.begin(115200);
while(!Serial){delay(10);}
Serial.printf("Serial ready.\n");
#if CONFIG_IDF_TARGET_ESP32
HWS.begin(9600, 12, 13); // Default pins for ESP32 conflict with SPI FLASH resulting in WDT
#else
HWS.begin(9600);
#endif
Serial.printf("Available for write before setting up buffer: %d\n", HWS.availableForWrite());
HWS.end();
HWS.setTxBufferSize(BUFFER_SIZE);
#if CONFIG_IDF_TARGET_ESP32
HWS.begin(9600, 12, 13); // Default pins for ESP32 conflict with SPI FLASH resulting in WDT
#else
HWS.begin(9600);
#endif
Serial.printf("Available for write after setting up buffer of size %d: %d\n", BUFFER_SIZE, HWS.availableForWrite());
}
void loop() {
delay(1000);
}
Output:
Available for write before setting up buffer: 128
Available for write after setting up buffer of size 256: 384
Tested on:
- ESP32
- ESP32S2
- ESP32C3
@SuGlider Rodrigo, Sorry to open this again. but I have also run into this issue.. and was wondering if it was supposed to be corrected in 4.4.6-dirty .. or if it is waiting for the "full breaking" new V3 compiler.? |
Hi, the IDF version is OK. It may have been released a bit before the final 4.4.6 was out. But both (HardwareSerial::availableForWrite() and setTxBufferSize()) shall work correctly. Both depended on a new feature added to IDF 4.4.6.
Yes, but not using Arduino API. It is possible to read the UART TX FIFO Count Register and see that it is empty, with data, etc.
128 is the size of the HW TX FIFO, therefore is it not a bug. The total available space for writing is the Buffer Size + 128 bytes. |
@dagnall53 - Please feel free to open a new issue report if you consider/find that there is a bug with it. |
@SuGlider Thanks for the fast reply. I think there is an argument that says that the setTxBufferSize is buggy or at the least, confusing. Certainly setTxBufferSize(N) does nothing if N<128. Which seems reasonable. and makes one expect that (say) It also means its impossible to set a (total size) buffer of (say) 200! as setTxBufferSize(72) will be rejected. Its very complex and I have been getting lost in this.. but does that make sense? |
I see your point. Yes, I'll think about it and place a PR to make it a single size. Your arguments are good. Thanks! |
@SuGlider |
@dagnall53 - analysing the question, this is the possible solution for it: Considering that IDF driver (mandatory for our Arduino implementation) only allows to set an additional Tx (or Rx) Buffer when its size is over 128 bytes, which is the HW TX FIFO size:
I think that it would address your points. Let me know if you agree. |
@SuGlider I hate to cross subjects.. but I came across this while trying to sort some very strange results with a Serial2.print. |
Yes, both use exactly the same driver and code. What is the SoC (ESP32, ESP32-S2 or ESP32-S3)? |
Just for tracking the Buffer Size problem better, I've open a new issue #9317 |
@dagnall53 - PR submitted #9319 |
Description of Change
Fixes an issue with
HardwareSerial::availableForWrite()
that only returns TX FIFO available space even when there is a TX RingBuffer in use throughHardwareSerial::setTxBufferSize()
.This isse was fixed by IDF 4.4 espressif/esp-idf@dfb75aa
new IDF UART API
esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size)
has been incorporated to the code inuint32_t uartAvailableForWrite(uart_t* uart)
NOTE:
This will only pass CI whenever latest IDF 4.4 gets merged once it depends on
uart.h
and repective uart precompiled library to include new UART APIesp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size)
DEPENDENCY on PR #6994
Tests scenarios
Tested with ESP32, ESP32-S2, ESP32-S3 and ESP32-C3 using a modified local version of IDF
uart.c
anduart.h
Related links
Fixes #6697