Skip to content

Commit 6580bf3

Browse files
authored
uart: fix wdt input overrun (esp8266#4536)
* uart: fix wdt on uart input overrun, expose in HardwareSerial::hasOverrun()
1 parent 6d3f0b4 commit 6580bf3

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

cores/esp8266/HardwareSerial.h

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class HardwareSerial: public Stream
137137
bool isRxEnabled(void);
138138
int baudRate(void);
139139

140+
bool hasOverrun(void)
141+
{
142+
return uart_has_overrun(_uart);
143+
}
144+
140145
protected:
141146
int _uart_nr;
142147
uart_t* _uart = nullptr;

cores/esp8266/uart.c

+29
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct uart_ {
5959
int baud_rate;
6060
bool rx_enabled;
6161
bool tx_enabled;
62+
bool overrun;
6263
uint8_t rx_pin;
6364
uint8_t tx_pin;
6465
struct uart_rx_buffer_ * rx_buffer;
@@ -102,13 +103,31 @@ inline size_t uart_rx_fifo_available(uart_t* uart) {
102103
return (USS(uart->uart_nr) >> USRXC) & 0x7F;
103104
}
104105

106+
char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n";
107+
105108
// Copy all the rx fifo bytes that fit into the rx buffer
106109
inline void uart_rx_copy_fifo_to_buffer(uart_t* uart) {
107110
while(uart_rx_fifo_available(uart)){
108111
size_t nextPos = (uart->rx_buffer->wpos + 1) % uart->rx_buffer->size;
109112
if(nextPos == uart->rx_buffer->rpos) {
113+
114+
if (!uart->overrun) {
115+
uart->overrun = true;
116+
os_printf_plus(overrun_str);
117+
}
118+
119+
// a choice has to be made here,
120+
// do we discard newest or oldest data?
121+
#if 0
122+
// discard newest data
110123
// Stop copying if rx buffer is full
124+
USF(uart->uart_nr);
111125
break;
126+
#else
127+
// discard oldest data
128+
if (++uart->rx_buffer->rpos == uart->rx_buffer->size)
129+
uart->rx_buffer->rpos = 0;
130+
#endif
112131
}
113132
uint8_t data = USF(uart->uart_nr);
114133
uart->rx_buffer->buffer[uart->rx_buffer->wpos] = data;
@@ -281,6 +300,7 @@ uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, s
281300
}
282301

283302
uart->uart_nr = uart_nr;
303+
uart->overrun = false;
284304

285305
switch(uart->uart_nr) {
286306
case UART0:
@@ -504,6 +524,15 @@ bool uart_rx_enabled(uart_t* uart)
504524
return uart->rx_enabled;
505525
}
506526

527+
bool uart_has_overrun (uart_t* uart)
528+
{
529+
if (uart == NULL || !uart->overrun) {
530+
return false;
531+
}
532+
// clear flag
533+
uart->overrun = false;
534+
return true;
535+
}
507536

508537
static void uart_ignore_char(char c)
509538
{

cores/esp8266/uart.h

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ size_t uart_tx_free(uart_t* uart);
136136
void uart_wait_tx_empty(uart_t* uart);
137137
void uart_flush(uart_t* uart);
138138

139+
bool uart_has_overrun (uart_t* uart); // returns then clear overrun flag
140+
139141
void uart_set_debug(int uart_nr);
140142
int uart_get_debug();
141143

0 commit comments

Comments
 (0)