Skip to content

Serial.flush modification #5293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 12 additions & 1 deletion cores/esp8266/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions tests/host/common/MockUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down