Skip to content

Commit 371f382

Browse files
committed
Fix Serial RX and add option for FIFO Full Threshold in Serial.begin
Fixes: espressif#5005
1 parent 425619d commit 371f382

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

‎cores/esp32/HardwareSerial.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ HardwareSerial Serial2(2);
5050

5151
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL){}
5252

53-
voidHardwareSerial::begin(unsignedlong baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsignedlong timeout_ms)
53+
voidHardwareSerial::begin(unsignedlong baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsignedlong timeout_ms, uint8_t rxfifo_full_thrhd)
5454
{
5555
if(0 > _uart_nr || _uart_nr > 2){
5656
log_e("Serial number is invalid, please use 0, 1 or 2");
@@ -78,7 +78,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
7878
txPin = TX2;
7979
}
8080
#endif
81-
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
81+
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
8282
_tx_pin = txPin;
8383
_rx_pin = rxPin;
8484

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

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

‎cores/esp32/HardwareSerial.h‎

Lines 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-
voidbegin(unsignedlong baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsignedlong timeout_ms = 20000UL);
58+
voidbegin(unsignedlong baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsignedlong timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
5959
voidend();
6060
voidupdateBaudRate(unsignedlong baud);
6161
intavailable(void);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ typedef struct{
4848
boolexternal_phy;
4949
} tinyusb_config_t;
5050

51-
staticTaskHandle_ts_tusb_tskh;
52-
5351
staticvoidconfigure_pins(usb_hal_context_t*usb)
5452
{
5553
for (constusb_iopin_dsc_t*iopin=usb_periph_iopins; iopin->pin!=-1; ++iopin){
@@ -74,7 +72,6 @@ static void configure_pins(usb_hal_context_t *usb)
7472

7573
esp_err_ttinyusb_driver_install(consttinyusb_config_t*config)
7674
{
77-
intres;
7875
log_i("Driver installation...");
7976

8077
// Hal init

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ static void ARDUINO_ISR_ATTR _uart_isr(void *arg)
130130
}
131131
}
132132

133-
voiduartEnableInterrupt(uart_t*uart)
133+
staticvoiduartEnableInterrupt(uart_t*uart, uint8_trxfifo_full_thrhd)
134134
{
135135
UART_MUTEX_LOCK();
136-
uart->dev->conf1.rxfifo_full_thrhd=112;
136+
uart->dev->conf1.rxfifo_full_thrhd=rxfifo_full_thrhd;
137137
#ifCONFIG_IDF_TARGET_ESP32
138138
uart->dev->conf1.rx_tout_thrhd=2;
139139
#else
@@ -149,7 +149,7 @@ void uartEnableInterrupt(uart_t* uart)
149149
UART_MUTEX_UNLOCK();
150150
}
151151

152-
voiduartDisableInterrupt(uart_t*uart)
152+
staticvoiduartDisableInterrupt(uart_t*uart)
153153
{
154154
UART_MUTEX_LOCK();
155155
uart->dev->conf1.val=0;
@@ -162,7 +162,7 @@ void uartDisableInterrupt(uart_t* uart)
162162
UART_MUTEX_UNLOCK();
163163
}
164164

165-
voiduartDetachRx(uart_t*uart, uint8_trxPin)
165+
staticvoiduartDetachRx(uart_t*uart, uint8_trxPin)
166166
{
167167
if(uart==NULL){
168168
return;
@@ -171,25 +171,25 @@ void uartDetachRx(uart_t* uart, uint8_t rxPin)
171171
uartDisableInterrupt(uart);
172172
}
173173

174-
voiduartDetachTx(uart_t*uart, uint8_ttxPin)
174+
staticvoiduartDetachTx(uart_t*uart, uint8_ttxPin)
175175
{
176176
if(uart==NULL){
177177
return;
178178
}
179179
pinMatrixOutDetach(txPin, false, false);
180180
}
181181

182-
voiduartAttachRx(uart_t*uart, uint8_trxPin, boolinverted)
182+
staticvoiduartAttachRx(uart_t*uart, uint8_trxPin, boolinverted, uint8_trxfifo_full_thrhd)
183183
{
184184
if(uart==NULL||rxPin >= GPIO_PIN_COUNT){
185185
return;
186186
}
187187
pinMode(rxPin, INPUT);
188-
uartEnableInterrupt(uart);
188+
uartEnableInterrupt(uart, rxfifo_full_thrhd);
189189
pinMatrixInAttach(rxPin, UART_RXD_IDX(uart->num), inverted);
190190
}
191191

192-
voiduartAttachTx(uart_t*uart, uint8_ttxPin, boolinverted)
192+
staticvoiduartAttachTx(uart_t*uart, uint8_ttxPin, boolinverted)
193193
{
194194
if(uart==NULL||txPin >= GPIO_PIN_COUNT){
195195
return;
@@ -198,7 +198,7 @@ void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
198198
pinMatrixOutAttach(txPin, UART_TXD_IDX(uart->num), inverted, false);
199199
}
200200

201-
uart_t*uartBegin(uint8_tuart_nr, uint32_tbaudrate, uint32_tconfig, int8_trxPin, int8_ttxPin, uint16_tqueueLen, boolinverted)
201+
uart_t*uartBegin(uint8_tuart_nr, uint32_tbaudrate, uint32_tconfig, int8_trxPin, int8_ttxPin, uint16_tqueueLen, boolinverted, uint8_trxfifo_full_thrhd)
202202
{
203203
if(uart_nr >= UART_PORTS_NUM){
204204
returnNULL;
@@ -256,7 +256,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
256256
UART_MUTEX_UNLOCK();
257257

258258
if(rxPin!=-1){
259-
uartAttachRx(uart, rxPin, inverted);
259+
uartAttachRx(uart, rxPin, inverted, rxfifo_full_thrhd);
260260
}
261261

262262
if(txPin!=-1){
@@ -322,7 +322,11 @@ uint32_t uartAvailable(uart_t* uart)
322322
if(uart==NULL||uart->queue==NULL){
323323
return0;
324324
}
325+
#ifdefUART_READ_RX_FIFO
325326
return (uxQueueMessagesWaiting(uart->queue) +uart->dev->status.rxfifo_cnt) ;
327+
#else
328+
returnuxQueueMessagesWaiting(uart->queue);
329+
#endif
326330
}
327331

328332
uint32_tuartAvailableForWrite(uart_t*uart)
@@ -333,6 +337,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
333337
return0x7f-uart->dev->status.txfifo_cnt;
334338
}
335339

340+
#ifdefUART_READ_RX_FIFO
336341
voiduartRxFifoToQueue(uart_t*uart)
337342
{
338343
uint8_tc;
@@ -357,17 +362,20 @@ void uartRxFifoToQueue(uart_t* uart)
357362
uart->dev->int_clr.val=0xffffffff;
358363
UART_MUTEX_UNLOCK();
359364
}
365+
#endif
360366

361367
uint8_tuartRead(uart_t*uart)
362368
{
363369
if(uart==NULL||uart->queue==NULL){
364370
return0;
365371
}
366372
uint8_tc;
373+
#ifdefUART_READ_RX_FIFO
367374
if ((uxQueueMessagesWaiting(uart->queue) ==0) && (uart->dev->status.rxfifo_cnt>0))
368375
{
369376
uartRxFifoToQueue(uart);
370377
}
378+
#endif
371379
if(xQueueReceive(uart->queue, &c, 0)){
372380
returnc;
373381
}
@@ -380,10 +388,12 @@ uint8_t uartPeek(uart_t* uart)
380388
return0;
381389
}
382390
uint8_tc;
391+
#ifdefUART_READ_RX_FIFO
383392
if ((uxQueueMessagesWaiting(uart->queue) ==0) && (uart->dev->status.rxfifo_cnt>0))
384393
{
385394
uartRxFifoToQueue(uart);
386395
}
396+
#endif
387397
if(xQueuePeek(uart->queue, &c, 0)){
388398
returnc;
389399
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern "C"{
5151
structuart_struct_t;
5252
typedefstructuart_struct_tuart_t;
5353

54-
uart_t*uartBegin(uint8_tuart_nr, uint32_tbaudrate, uint32_tconfig, int8_trxPin, int8_ttxPin, uint16_tqueueLen, boolinverted);
54+
uart_t*uartBegin(uint8_tuart_nr, uint32_tbaudrate, uint32_tconfig, int8_trxPin, int8_ttxPin, uint16_tqueueLen, boolinverted, uint8_trxfifo_full_thrhd);
5555
voiduartEnd(uart_t*uart, uint8_trxPin, uint8_ttxPin);
5656

5757
uint32_tuartAvailable(uart_t*uart);

0 commit comments

Comments
(0)