Skip to content

Commit 8c3f1be

Browse files
dsv19earlephilhower
authored andcommitted
Serial.flush modification (#5293)
* Modify Serial.flush * Add function to uart tests
1 parent 8b16d9c commit 8c3f1be

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

cores/esp8266/HardwareSerial.cpp

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

109109
void HardwareSerial::flush()
110110
{
111+
uint8_t bit_length = 0;
111112
if(!_uart || !uart_tx_enabled(_uart)) {
112113
return;
113114
}
114115

116+
bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop
115117
uart_wait_tx_empty(_uart);
116118
//Workaround for a bug in serial not actually being finished yet
117119
//Wait for 8 data bits, 1 parity and 2 stop bits, just in case
118-
delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1);
120+
delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1);
119121
}
120122

121123
void HardwareSerial::startDetectBaudrate()

cores/esp8266/uart.cpp

Lines changed: 12 additions & 1 deletion
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,7 +208,14 @@ uart_read_char_unsafe(uart_t* uart)
204208
return -1;
205209
}
206210

207-
size_t
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+
218+
size_t
208219
uart_rx_available(uart_t* uart)
209220
{
210221
if(uart == NULL || !uart->rx_enabled)

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"

tests/host/common/MockUART.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ uart_get_baudrate(uart_t* uart)
313313
return uart->baud_rate;
314314
}
315315

316+
uint8_t
317+
uart_get_bit_length(const int uart_nr)
318+
{
319+
uint8_t width = ((uart_nr % 16) >> 2) + 5;
320+
uint8_t parity = (uart_nr >> 5) + 1;
321+
uint8_t stop = uart_nr % 4;
322+
return (width + parity + stop + 1);
323+
}
324+
316325
uart_t*
317326
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
318327
{

0 commit comments

Comments
 (0)