diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index d0d83bd3..c449b164 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -62,6 +62,10 @@ int HardwareSerial::available(void) { return uartAvailable(_uart); } +int HardwareSerial::availableForWrite(void) +{ + return uartAvailableForWrite(_uart); +} int HardwareSerial::peek(void) { diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index f1a9d36c..c0e00ec2 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -40,6 +40,7 @@ public: void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false); void end(); int available(void); + int availableForWrite(void); int peek(void); int read(void); void flush(void); diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 66126689..bcf9d045 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -248,6 +248,14 @@ uint32_t uartAvailable(uart_t* uart) return uxQueueMessagesWaiting(uart->queue); } +uint32_t uartAvailableForWrite(uart_t* uart) +{ + if(uart == NULL) { + return 0; + } + return 0x7f - uart->dev->status.txfifo_cnt; +} + uint8_t uartRead(uart_t* uart) { if(uart == NULL || uart->queue == NULL) { diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index ce83f146..5cd6e965 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -55,6 +55,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx void uartEnd(uart_t* uart); uint32_t uartAvailable(uart_t* uart); +uint32_t uartAvailableForWrite(uart_t* uart); uint8_t uartRead(uart_t* uart); uint8_t uartPeek(uart_t* uart);