espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.69k stars 7.42k forks source link

Add a cb function when the Serial TX buffer emptyed #8997

Open zekageri opened 11 months ago

zekageri commented 11 months ago

Related area

UART

Hardware specification

UART

Is your feature request related to a problem?

Yes. I have a problem. I don't know when the TX buffer is empty when i put data in it. I don't have any CB or interrupt to know. We only have a flush() method but it is blocking and just doing a while loop. This is because i have timing issues. Currently what i do is this:

digitalWrite(MBUS_EN, HIGH);
Serial1.write(buffer, length+2);
Serial1.flush();    // flush is blokcing and the RTOS switches tasks. By the time it gets back i'm missing a lot of bytes
digitalWrite(MBUS_EN, LOW);

This data cut is happening when i have other tasks running. The esp simply has no time to set the EN pin to LOW.

Describe the solution you'd like

I would like a callback or an interrupt when the tx buffer is empty. Something like

Serial1.begin(MBUS_BAUD, SERIAL_8N1, MBUS_RX, MBUS_TX);
Serial1.onTxEmpty( [](){
   // Tx buffer is empty. All data sent.
});

Or to attach an interrupt to it.

Describe alternatives you've considered

Interrupt or cb.

Additional context

This would be useful to set my modbus EN pin.

I have checked existing list of Feature requests and the Contribution Guide

VojtechBartoska commented 11 months ago

@SuGlider can you please help with triage if this is doable? Thanks