diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 1be050e8c2..fc270cfed1 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -108,14 +108,16 @@ int HardwareSerial::available(void) void HardwareSerial::flush() { + uint8_t bit_length = 0; if(!_uart || !uart_tx_enabled(_uart)) { return; } + bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop uart_wait_tx_empty(_uart); //Workaround for a bug in serial not actually being finished yet //Wait for 8 data bits, 1 parity and 2 stop bits, just in case - delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1); + delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1); } void HardwareSerial::startDetectBaudrate() diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index 6c71efccbf..05a425c9c7 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -48,6 +48,10 @@ #include "user_interface.h" #include "uart_register.h" +#define MODE2WIDTH(mode) (((mode%16)>>2)+5) +#define MODE2STOP(mode) (((mode)>>5)+1) +#define MODE2PARITY(mode) (mode%4) + /* Some general architecture for GDB integration with the UART to enable serial debugging. @@ -204,7 +208,14 @@ uart_read_char_unsafe(uart_t* uart) return -1; } -size_t +uint8_t +uart_get_bit_length(const int uart_nr) +{ + // return bit length from uart mode, +1 for the start bit which is always there. + return MODE2WIDTH(USC0(uart_nr)) + MODE2PARITY(USC0(uart_nr)) + MODE2STOP(USC0(uart_nr)) + 1; +} + +size_t uart_rx_available(uart_t* uart) { if(uart == NULL || !uart->rx_enabled) diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index 7f9dce0f0a..fd61585689 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -147,6 +147,7 @@ int uart_get_debug(); void uart_start_detect_baudrate(int uart_nr); int uart_detect_baudrate(int uart_nr); +uint8_t uart_get_bit_length(const int uart_nr); #if defined (__cplusplus) } // extern "C" diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index 67af7ed2cc..db0d8c8894 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -313,6 +313,15 @@ uart_get_baudrate(uart_t* uart) return uart->baud_rate; } +uint8_t +uart_get_bit_length(const int uart_nr) +{ + uint8_t width = ((uart_nr % 16) >> 2) + 5; + uint8_t parity = (uart_nr >> 5) + 1; + uint8_t stop = uart_nr % 4; + return (width + parity + stop + 1); +} + uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) {