Skip to content
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

uart: BW improvements #4620

Merged
merged 37 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e5c2fea
uart fixes and BW improvements
d-a-v Apr 4, 2018
f79bc90
uart: read_char straightly use hw buffer
d-a-v Apr 5, 2018
45e2bd5
+attributes for functions called by ISR
d-a-v Apr 5, 2018
974b758
uart: BW improvements
d-a-v Apr 4, 2018
3eb8c95
Merge branch 'master' into serialagain
devyte Apr 9, 2018
0aefb09
Merge branch 'master' into serialagain
devyte Jun 7, 2018
4c6bab4
Merge branch 'master' into serialagain
devyte Jul 3, 2018
3c1e312
Merge branch 'master' into serialagain
d-a-v Nov 30, 2018
132f7f3
Merge branch 'serialagain' of github.com:d-a-v/Arduino into serialagain
d-a-v Nov 30, 2018
6560ab8
fix merge
d-a-v Nov 30, 2018
ebdf57a
Merge branch 'master' into serialagain
d-a-v Dec 5, 2018
c68d474
fix buffer overflow
d-a-v Dec 5, 2018
ff63718
serial stress test sketch
d-a-v Dec 5, 2018
40f237f
astyle
d-a-v Dec 5, 2018
d6316aa
Merge branch 'serialagain' of github.com:d-a-v/Arduino into serialagain
d-a-v Dec 5, 2018
3428144
serial stress example: interactive keyboard, stop reading, overrun
d-a-v Dec 5, 2018
684879d
Merge branch 'master' into serialagain
d-a-v Dec 6, 2018
882989e
serial device test: bandwidth & overrun
d-a-v Dec 6, 2018
3ec2a60
update + HardwareSerial::hasError()
d-a-v Dec 6, 2018
a38f9d8
interactive overrun in example
d-a-v Dec 6, 2018
6b77715
astyle
d-a-v Dec 6, 2018
b639261
Test using @plerup's SoftwareSerial as submodule (tag 3.4.1)
d-a-v Dec 6, 2018
40d1237
update upstream ref (fix warning)
d-a-v Dec 6, 2018
64f7ac1
Merge branch 'master' into serialagain
d-a-v Dec 6, 2018
c48b54f
host mock uart/read(buf,size)
d-a-v Dec 6, 2018
18a189f
reset style changes in submodules before style diff
d-a-v Dec 7, 2018
ddcd908
Merge branch 'serialagain' of github.com:d-a-v/Arduino into serialagain
d-a-v Dec 7, 2018
48ad6bc
Merge branch 'master' into serialagain
earlephilhower Dec 7, 2018
0a7322f
Merge branch 'master' into serialagain
devyte Dec 8, 2018
fb25c8d
update build_boards_manager_package.sh for submodules
d-a-v Dec 8, 2018
8785e3a
Merge branch 'master' into serialagain
d-a-v Dec 8, 2018
e83316f
Merge branch 'master' into serialagain
devyte Dec 9, 2018
62ead27
Merge branch 'master' into serialagain
devyte Dec 9, 2018
c7eba46
Merge branch 'master' into serialagain
devyte Dec 10, 2018
cbd8b44
trigger CI (removing space)
d-a-v Dec 10, 2018
99fcfda
cannot reproduce locally the CI issue, setting bash -x option to get …
d-a-v Dec 10, 2018
6234fe0
remove previously added (in this PR) 'set -e' in package builder (pas…
d-a-v Dec 10, 2018
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "tools/sdk/ssl/bearssl"]
path = tools/sdk/ssl/bearssl
url = https://github.com/earlephilhower/bearssl-esp8266
[submodule "libraries/SoftwareSerial"]
path = libraries/SoftwareSerial
url = https://github.com/plerup/espsoftwareserial.git
21 changes: 19 additions & 2 deletions cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class HardwareSerial: public Stream
void end();

size_t setRxBufferSize(size_t size);
size_t getRxBufferSize()
{
return uart_get_rx_buffer_size(_uart);
}

void swap()
{
Expand Down Expand Up @@ -120,14 +124,22 @@ class HardwareSerial: public Stream

int peek(void) override
{
// this may return -1, but that's okay
// return -1 when data is unvailable (arduino api)
return uart_peek_char(_uart);
}
int read(void) override
{
// this may return -1, but that's okay
// return -1 when data is unvailable (arduino api)
return uart_read_char(_uart);
}
size_t readBytes(char* buffer, size_t size) override
{
return uart_read(_uart, buffer, size);
}
size_t readBytes(uint8_t* buffer, size_t size) override
{
return uart_read(_uart, (char*)buffer, size);
}
int availableForWrite(void)
{
return static_cast<int>(uart_tx_free(_uart));
Expand Down Expand Up @@ -184,6 +196,11 @@ class HardwareSerial: public Stream
return uart_has_overrun(_uart);
}

bool hasRxError(void)
{
return uart_has_rx_error(_uart);
}

void startDetectBaudrate();

unsigned long testBaudrate();
Expand Down
140 changes: 110 additions & 30 deletions cores/esp8266/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "user_interface.h"
#include "uart_register.h"

const char overrun_str [] PROGMEM STORE_ATTR = "uart input full!\r\n";
//const char overrun_str [] PROGMEM STORE_ATTR = "uart input full!\r\n";
static int s_uart_debug_nr = UART0;


Expand All @@ -65,7 +65,8 @@ struct uart_
int baud_rate;
bool rx_enabled;
bool tx_enabled;
bool overrun;
bool rx_overrun;
bool rx_error;
uint8_t rx_pin;
uint8_t tx_pin;
struct uart_rx_buffer_ * rx_buffer;
Expand All @@ -85,7 +86,8 @@ struct uart_



inline size_t
// called by ISR
inline size_t ICACHE_RAM_ATTR
d-a-v marked this conversation as resolved.
Show resolved Hide resolved
uart_rx_fifo_available(const int uart_nr)
{
return (USS(uart_nr) >> USRXC) & 0xFF;
Expand All @@ -110,11 +112,11 @@ uart_rx_available_unsafe(uart_t* uart)
return uart_rx_buffer_available_unsafe(uart->rx_buffer) + uart_rx_fifo_available(uart->uart_nr);
}


//#define UART_DISCARD_NEWEST

// Copy all the rx fifo bytes that fit into the rx buffer
inline void
// called by ISR
inline void ICACHE_RAM_ATTR
d-a-v marked this conversation as resolved.
Show resolved Hide resolved
uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart)
{
struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer;
Expand All @@ -124,11 +126,10 @@ uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart)
size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size;
if(nextPos == rx_buffer->rpos)
{

if (!uart->overrun)
if (!uart->rx_overrun)
{
uart->overrun = true;
os_printf_plus(overrun_str);
uart->rx_overrun = true;
//os_printf_plus(overrun_str);
}

// a choice has to be made here,
Expand Down Expand Up @@ -158,25 +159,27 @@ uart_peek_char_unsafe(uart_t* uart)

//without the following if statement and body, there is a good chance of a fifo overrun
if (uart_rx_buffer_available_unsafe(uart->rx_buffer) == 0)
// hw fifo can't be peeked, data need to be copied to sw
uart_rx_copy_fifo_to_buffer_unsafe(uart);

return uart->rx_buffer->buffer[uart->rx_buffer->rpos];
}

inline int
// taking data straight from hw fifo: loopback-test BW jumps by 19%
inline int
devyte marked this conversation as resolved.
Show resolved Hide resolved
uart_read_char_unsafe(uart_t* uart)
{
int data = uart_peek_char_unsafe(uart);
if(data != -1)
if (uart_rx_buffer_available_unsafe(uart->rx_buffer))
{
// take oldest sw data
int ret = uart->rx_buffer->buffer[uart->rx_buffer->rpos];
uart->rx_buffer->rpos = (uart->rx_buffer->rpos + 1) % uart->rx_buffer->size;
return data;
return ret;
}
// unavailable
return -1;
}


/**********************************************************/



size_t
uart_rx_available(uart_t* uart)
{
Expand Down Expand Up @@ -204,14 +207,47 @@ uart_peek_char(uart_t* uart)

int
uart_read_char(uart_t* uart)
{
uint8_t ret;
return uart_read(uart, (char*)&ret, 1)? ret: -1;
}

// loopback-test BW jumps by 190%
size_t
uart_read(uart_t* uart, char* userbuffer, size_t usersize)
devyte marked this conversation as resolved.
Show resolved Hide resolved
{
if(uart == NULL || !uart->rx_enabled)
return -1;

return 0;

size_t ret = 0;
ETS_UART_INTR_DISABLE();
int data = uart_read_char_unsafe(uart);

while (ret < usersize && uart_rx_available_unsafe(uart))
{
if (!uart_rx_buffer_available_unsafe(uart->rx_buffer))
{
// no more data in sw buffer, take them from hw fifo
while (ret < usersize && uart_rx_fifo_available(uart->uart_nr))
userbuffer[ret++] = USF(uart->uart_nr);

// no more sw/hw data available
break;
}

// pour sw buffer to user's buffer
// get largest linear length from sw buffer
size_t chunk = uart->rx_buffer->rpos < uart->rx_buffer->wpos?
uart->rx_buffer->wpos - uart->rx_buffer->rpos:
uart->rx_buffer->size - uart->rx_buffer->rpos;
if (ret + chunk > usersize)
chunk = usersize - ret;
memcpy(userbuffer + ret, uart->rx_buffer->buffer + uart->rx_buffer->rpos, chunk);
uart->rx_buffer->rpos = (uart->rx_buffer->rpos + chunk) % uart->rx_buffer->size;
ret += chunk;
}

ETS_UART_INTR_ENABLE();
return data;
return ret;
}

size_t
Expand All @@ -231,6 +267,8 @@ uart_resize_rx_buffer(uart_t* uart, size_t new_size)
ETS_UART_INTR_DISABLE();
while(uart_rx_available_unsafe(uart) && new_wpos < new_size)
new_buf[new_wpos++] = uart_read_char_unsafe(uart); //if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1
if (new_wpos == new_size)
new_wpos = 0;

uint8_t * old_buf = uart->rx_buffer->buffer;
uart->rx_buffer->rpos = 0;
Expand All @@ -242,22 +280,39 @@ uart_resize_rx_buffer(uart_t* uart, size_t new_size)
return uart->rx_buffer->size;
}

size_t
uart_get_rx_buffer_size(uart_t* uart)
{
return uart && uart->rx_enabled? uart->rx_buffer->size: 0;
}


void ICACHE_RAM_ATTR
uart_isr(void * arg)
{
uart_t* uart = (uart_t*)arg;
uint32_t usis = USIS(uart->uart_nr);

if(uart == NULL || !uart->rx_enabled)
{
USIC(uart->uart_nr) = USIS(uart->uart_nr);
USIC(uart->uart_nr) = usis;
ETS_UART_INTR_DISABLE();
return;
}
if(USIS(uart->uart_nr) & ((1 << UIFF) | (1 << UITO)))

if(usis & (1 << UIFF))
uart_rx_copy_fifo_to_buffer_unsafe(uart);

if((usis & (1 << UIOF)) && !uart->rx_overrun)
{
uart->rx_overrun = true;
//os_printf_plus(overrun_str);
}

USIC(uart->uart_nr) = USIS(uart->uart_nr);
if (usis & ((1 << UIFR) | (1 << UIPE) | (1 << UITO)))
uart->rx_error = true;

USIC(uart->uart_nr) = usis;
}

static void
Expand All @@ -270,9 +325,22 @@ uart_start_isr(uart_t* uart)
// triggers the IRS very often. A value of 127 would not leave much time
// for ISR to clear fifo before the next byte is dropped. So pick a value
// in the middle.
USC1(uart->uart_nr) = (100 << UCFFT) | (0x02 << UCTOT) | (1 <<UCTOE );
// update: loopback test @ 3Mbauds/8n1 (=2343Kibits/s):
// - 4..120 give > 2300Kibits/s
// - 1, 2, 3 are below
// was 100, use 16 to stay away from overrun
#define INTRIGG 16

//was:USC1(uart->uart_nr) = (INTRIGG << UCFFT) | (0x02 << UCTOT) | (1 <<UCTOE);
USC1(uart->uart_nr) = (INTRIGG << UCFFT);
USIC(uart->uart_nr) = 0xffff;
USIE(uart->uart_nr) = (1 << UIFF) | (1 << UIFR) | (1 << UITO);
//was: USIE(uart->uart_nr) = (1 << UIFF) | (1 << UIFR) | (1 << UITO);
// UIFF: rx fifo full
// UIOF: rx fifo overflow (=overrun)
// UIFR: frame error
// UIPE: parity error
// UITO: rx fifo timeout
USIE(uart->uart_nr) = (1 << UIFF) | (1 << UIOF) | (1 << UIFR) | (1 << UIPE) | (1 << UITO);
ETS_UART_INTR_ATTACH(uart_isr, (void *)uart);
ETS_UART_INTR_ENABLE();
}
Expand Down Expand Up @@ -415,7 +483,8 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx
return NULL;

uart->uart_nr = uart_nr;
uart->overrun = false;
uart->rx_overrun = false;
uart->rx_error = false;

switch(uart->uart_nr)
{
Expand Down Expand Up @@ -678,11 +747,22 @@ uart_rx_enabled(uart_t* uart)
bool
uart_has_overrun (uart_t* uart)
{
if (uart == NULL || !uart->overrun)
if (uart == NULL || !uart->rx_overrun)
return false;

// clear flag
uart->rx_overrun = false;
return true;
}

bool
uart_has_rx_error (uart_t* uart)
{
if (uart == NULL || !uart->rx_error)
return false;

// clear flag
uart->overrun = false;
uart->rx_error = false;
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions cores/esp8266/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,20 @@ void uart_set_baudrate(uart_t* uart, int baud_rate);
int uart_get_baudrate(uart_t* uart);

size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size);
size_t uart_get_rx_buffer_size(uart_t* uart);

size_t uart_write_char(uart_t* uart, char c);
size_t uart_write(uart_t* uart, const char* buf, size_t size);
int uart_read_char(uart_t* uart);
int uart_peek_char(uart_t* uart);
size_t uart_read(uart_t* uart, char* buffer, size_t size);
size_t uart_rx_available(uart_t* uart);
size_t uart_tx_free(uart_t* uart);
void uart_wait_tx_empty(uart_t* uart);
void uart_flush(uart_t* uart);

bool uart_has_overrun (uart_t* uart); // returns then clear overrun flag
bool uart_has_rx_error (uart_t* uart); // returns then clear rxerror flag

void uart_set_debug(int uart_nr);
int uart_get_debug();
Expand Down
1 change: 1 addition & 0 deletions libraries/SoftwareSerial
Submodule SoftwareSerial added at 23ae00
Loading