Skip to content

Commit 79285ac

Browse files
committed
Modify Serial.flush
1 parent e56aed6 commit 79285ac

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

cores/esp8266/HardwareSerial.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ int HardwareSerial::available(void)
9999

100100
void HardwareSerial::flush()
101101
{
102+
uint8_t bit_length = 0;
102103
if(!_uart || !uart_tx_enabled(_uart)) {
103104
return;
104105
}
105106

107+
bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop
106108
uart_wait_tx_empty(_uart);
107109
//Workaround for a bug in serial not actually being finished yet
108110
//Wait for 8 data bits, 1 parity and 2 stop bits, just in case
109-
delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1);
111+
delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1);
110112
}
111113

112114
void HardwareSerial::startDetectBaudrate()

cores/esp8266/uart.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#include "user_interface.h"
4949
#include "uart_register.h"
5050

51+
#define MODE2WIDTH(mode) (((mode%16)>>2)+5)
52+
#define MODE2STOP(mode) (((mode)>>5)+1)
53+
#define MODE2PARITY(mode) (mode%4)
54+
5155
/*
5256
Some general architecture for GDB integration with the UART to enable
5357
serial debugging.
@@ -204,6 +208,13 @@ uart_read_char_unsafe(uart_t* uart)
204208
return -1;
205209
}
206210

211+
uint8_t
212+
uart_get_bit_length(const int uart_nr)
213+
{
214+
// return bit length from uart mode, +1 for the start bit which is always there.
215+
return MODE2WIDTH(USC0(uart_nr)) + MODE2PARITY(USC0(uart_nr)) + MODE2STOP(USC0(uart_nr)) + 1;
216+
}
217+
207218
size_t
208219
uart_rx_available(uart_t* uart)
209220
{

cores/esp8266/uart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ int uart_get_debug();
147147
void uart_start_detect_baudrate(int uart_nr);
148148
int uart_detect_baudrate(int uart_nr);
149149

150+
uint8_t uart_get_bit_length(const int uart_nr);
150151

151152
#if defined (__cplusplus)
152153
} // extern "C"

0 commit comments

Comments
 (0)