1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-25 20:02:37 +03:00

Serial.flush modification (#5293)

* Modify Serial.flush

* Add function to uart tests
This commit is contained in:
dsv19 2019-10-08 15:36:39 +03:00 committed by Earle F. Philhower, III
parent 8b16d9c1d1
commit 8c3f1be63f
4 changed files with 25 additions and 2 deletions

View File

@ -108,14 +108,16 @@ int HardwareSerial::available(void)
void HardwareSerial::flush() void HardwareSerial::flush()
{ {
uint8_t bit_length = 0;
if(!_uart || !uart_tx_enabled(_uart)) { if(!_uart || !uart_tx_enabled(_uart)) {
return; return;
} }
bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop
uart_wait_tx_empty(_uart); uart_wait_tx_empty(_uart);
//Workaround for a bug in serial not actually being finished yet //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 //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() void HardwareSerial::startDetectBaudrate()

View File

@ -48,6 +48,10 @@
#include "user_interface.h" #include "user_interface.h"
#include "uart_register.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 Some general architecture for GDB integration with the UART to enable
serial debugging. serial debugging.
@ -204,7 +208,14 @@ uart_read_char_unsafe(uart_t* uart)
return -1; 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) uart_rx_available(uart_t* uart)
{ {
if(uart == NULL || !uart->rx_enabled) if(uart == NULL || !uart->rx_enabled)

View File

@ -147,6 +147,7 @@ int uart_get_debug();
void uart_start_detect_baudrate(int uart_nr); void uart_start_detect_baudrate(int uart_nr);
int uart_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) #if defined (__cplusplus)
} // extern "C" } // extern "C"

View File

@ -313,6 +313,15 @@ uart_get_baudrate(uart_t* uart)
return uart->baud_rate; 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_t*
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
{ {