Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5ee6aac

Browse filesBrowse files
committed
Fix Serial RX and add option for FIFO Full Threshold in Serial.begin
Fixes: espressif#5005
1 parent 46d5afb commit 5ee6aac
Copy full SHA for 5ee6aac

File tree

Expand file treeCollapse file tree

4 files changed

+25
-15
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+25
-15
lines changed

‎cores/esp32/HardwareSerial.cpp

Copy file name to clipboardExpand all lines: cores/esp32/HardwareSerial.cpp
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ HardwareSerial Serial2(2);
3030

3131
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
3232

33-
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
33+
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
3434
{
3535
if(0 > _uart_nr || _uart_nr > 2) {
3636
log_e("Serial number is invalid, please use 0, 1 or 2");
@@ -52,7 +52,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
5252
txPin = TX2;
5353
}
5454

55-
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
55+
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
5656
_tx_pin = txPin;
5757
_rx_pin = rxPin;
5858

@@ -68,7 +68,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
6868

6969
if(detectedBaudRate) {
7070
delay(100); // Give some time...
71-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert);
71+
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
7272
} else {
7373
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
7474
_uart = NULL;

‎cores/esp32/HardwareSerial.h

Copy file name to clipboardExpand all lines: cores/esp32/HardwareSerial.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class HardwareSerial: public Stream
5555
public:
5656
HardwareSerial(int uart_nr);
5757

58-
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);
58+
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
5959
void end();
6060
void updateBaudRate(unsigned long baud);
6161
int available(void);

‎cores/esp32/esp32-hal-uart.c

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-uart.c
+20-10Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ static void IRAM_ATTR _uart_isr(void *arg)
9696
}
9797
}
9898

99-
void uartEnableInterrupt(uart_t* uart)
99+
static void uartEnableInterrupt(uart_t* uart, uint8_t rxfifo_full_thrhd)
100100
{
101101
UART_MUTEX_LOCK();
102-
uart->dev->conf1.rxfifo_full_thrhd = 112;
102+
uart->dev->conf1.rxfifo_full_thrhd = rxfifo_full_thrhd;
103103
uart->dev->conf1.rx_tout_thrhd = 2;
104104
uart->dev->conf1.rx_tout_en = 1;
105105
uart->dev->int_ena.rxfifo_full = 1;
@@ -111,7 +111,7 @@ void uartEnableInterrupt(uart_t* uart)
111111
UART_MUTEX_UNLOCK();
112112
}
113113

114-
void uartDisableInterrupt(uart_t* uart)
114+
static void uartDisableInterrupt(uart_t* uart)
115115
{
116116
UART_MUTEX_LOCK();
117117
uart->dev->conf1.val = 0;
@@ -124,7 +124,7 @@ void uartDisableInterrupt(uart_t* uart)
124124
UART_MUTEX_UNLOCK();
125125
}
126126

127-
void uartDetachRx(uart_t* uart, uint8_t rxPin)
127+
static void uartDetachRx(uart_t* uart, uint8_t rxPin)
128128
{
129129
if(uart == NULL) {
130130
return;
@@ -133,25 +133,25 @@ void uartDetachRx(uart_t* uart, uint8_t rxPin)
133133
uartDisableInterrupt(uart);
134134
}
135135

136-
void uartDetachTx(uart_t* uart, uint8_t txPin)
136+
static void uartDetachTx(uart_t* uart, uint8_t txPin)
137137
{
138138
if(uart == NULL) {
139139
return;
140140
}
141141
pinMatrixOutDetach(txPin, false, false);
142142
}
143143

144-
void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted)
144+
static void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted, uint8_t rxfifo_full_thrhd)
145145
{
146146
if(uart == NULL || rxPin > 39) {
147147
return;
148148
}
149149
pinMode(rxPin, INPUT);
150+
uartEnableInterrupt(uart, rxfifo_full_thrhd);
150151
pinMatrixInAttach(rxPin, UART_RXD_IDX(uart->num), inverted);
151-
uartEnableInterrupt(uart);
152152
}
153153

154-
void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
154+
static void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
155155
{
156156
if(uart == NULL || txPin > 39) {
157157
return;
@@ -160,7 +160,7 @@ void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
160160
pinMatrixOutAttach(txPin, UART_TXD_IDX(uart->num), inverted, false);
161161
}
162162

163-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted)
163+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
164164
{
165165
if(uart_nr > 2) {
166166
return NULL;
@@ -216,7 +216,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
216216
UART_MUTEX_UNLOCK();
217217

218218
if(rxPin != -1) {
219-
uartAttachRx(uart, rxPin, inverted);
219+
uartAttachRx(uart, rxPin, inverted, rxfifo_full_thrhd);
220220
}
221221

222222
if(txPin != -1) {
@@ -282,7 +282,11 @@ uint32_t uartAvailable(uart_t* uart)
282282
if(uart == NULL || uart->queue == NULL) {
283283
return 0;
284284
}
285+
#ifdef UART_READ_RX_FIFO
285286
return (uxQueueMessagesWaiting(uart->queue) + uart->dev->status.rxfifo_cnt) ;
287+
#else
288+
return uxQueueMessagesWaiting(uart->queue);
289+
#endif
286290
}
287291

288292
uint32_t uartAvailableForWrite(uart_t* uart)
@@ -293,6 +297,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
293297
return 0x7f - uart->dev->status.txfifo_cnt;
294298
}
295299

300+
#ifdef UART_READ_RX_FIFO
296301
void uartRxFifoToQueue(uart_t* uart)
297302
{
298303
uint8_t c;
@@ -311,17 +316,20 @@ void uartRxFifoToQueue(uart_t* uart)
311316
uart->dev->int_clr.val = 0xffffffff;
312317
UART_MUTEX_UNLOCK();
313318
}
319+
#endif
314320

315321
uint8_t uartRead(uart_t* uart)
316322
{
317323
if(uart == NULL || uart->queue == NULL) {
318324
return 0;
319325
}
320326
uint8_t c;
327+
#ifdef UART_READ_RX_FIFO
321328
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
322329
{
323330
uartRxFifoToQueue(uart);
324331
}
332+
#endif
325333
if(xQueueReceive(uart->queue, &c, 0)) {
326334
return c;
327335
}
@@ -334,10 +342,12 @@ uint8_t uartPeek(uart_t* uart)
334342
return 0;
335343
}
336344
uint8_t c;
345+
#ifdef UART_READ_RX_FIFO
337346
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
338347
{
339348
uartRxFifoToQueue(uart);
340349
}
350+
#endif
341351
if(xQueuePeek(uart->queue, &c, 0)) {
342352
return c;
343353
}

‎cores/esp32/esp32-hal-uart.h

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-uart.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern "C" {
5151
struct uart_struct_t;
5252
typedef struct uart_struct_t uart_t;
5353

54-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted);
54+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd);
5555
void uartEnd(uart_t* uart, uint8_t rxPin, uint8_t txPin);
5656

5757
uint32_t uartAvailable(uart_t* uart);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.