Skip to content

Commit 2ceacbf

Browse files
committed
HAL read/peek timeout with native freeRTOS blocking mechanism
1 parent 3376ea1 commit 2ceacbf

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,43 @@ int HardwareSerial::availableForWrite(void)
116116

117117
int HardwareSerial::peek(void)
118118
{
119-
if (available()) {
120-
return uartPeek(_uart);
121-
}
122-
return -1;
119+
uint8_t data;
120+
if (!uartPeek(_uart, &data, 0))
121+
return -1;
122+
123+
return data;
123124
}
124125

125126
int HardwareSerial::read(void)
126127
{
127-
if(available()) {
128-
return uartRead(_uart);
129-
}
130-
return -1;
128+
uint8_t data;
129+
if (!uartRead(_uart, &data, 0))
130+
return -1;
131+
132+
return data;
131133
}
132134

133135
void HardwareSerial::flush()
134136
{
135137
uartFlush(_uart);
136138
}
137139

140+
int HardwareSerial::timedPeek() {
141+
uint8_t data;
142+
if (!uartPeek(_uart, &data, _timeout))
143+
return -1;
144+
145+
return data;
146+
}
147+
148+
int HardwareSerial::timedRead() {
149+
uint8_t data;
150+
if (!uartRead(_uart, &data, _timeout))
151+
return -1;
152+
153+
return data;
154+
}
155+
138156
size_t HardwareSerial::write(uint8_t c)
139157
{
140158
uartWrite(_uart, c);

cores/esp32/HardwareSerial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class HardwareSerial: public Stream
6262
int availableForWrite(void);
6363
int peek(void);
6464
int read(void);
65+
virtual int timedRead(); // private method to read uart with timeout
66+
virtual int timedPeek(); // private method to peek uart with timeout
6567
void flush(void);
6668
size_t write(uint8_t);
6769
size_t write(const uint8_t *buffer, size_t size);

cores/esp32/Stream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class Stream: public Print
4040
protected:
4141
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4242
unsigned long _startMillis; // used for timeout measurement
43-
int timedRead(); // private method to read stream with timeout
44-
int timedPeek(); // private method to peek stream with timeout
43+
virtual int timedRead(); // private method to read stream with timeout
44+
virtual int timedPeek(); // private method to peek stream with timeout
4545
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
4646

4747
public:

cores/esp32/esp32-hal-uart.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "soc/dport_reg.h"
3030
#include "soc/rtc.h"
3131
#include "esp_intr_alloc.h"
32+
#include "sdkconfig.h"
3233

3334
#define UART_REG_BASE(u) ((u==0)?DR_REG_UART_BASE:( (u==1)?DR_REG_UART1_BASE:( (u==2)?DR_REG_UART2_BASE:0)))
3435
#define UART_RXD_IDX(u) ((u==0)?U0RXD_IN_IDX:( (u==1)?U1RXD_IN_IDX:( (u==2)?U2RXD_IN_IDX:0)))
@@ -277,26 +278,24 @@ uint32_t uartAvailableForWrite(uart_t* uart)
277278
return 0x7f - uart->dev->status.txfifo_cnt;
278279
}
279280

280-
uint8_t uartRead(uart_t* uart)
281+
uint8_t uartRead(uart_t* uart, uint8_t *data, size_t to)
281282
{
282283
if(uart == NULL || uart->queue == NULL) {
283284
return 0;
284285
}
285-
uint8_t c;
286-
if(xQueueReceive(uart->queue, &c, 0)) {
287-
return c;
286+
if(xQueueReceive(uart->queue, data, to)) {
287+
return 1;
288288
}
289289
return 0;
290290
}
291291

292-
uint8_t uartPeek(uart_t* uart)
292+
uint8_t uartPeek(uart_t* uart, uint8_t *data, size_t to)
293293
{
294294
if(uart == NULL || uart->queue == NULL) {
295295
return 0;
296296
}
297-
uint8_t c;
298-
if(xQueuePeek(uart->queue, &c, 0)) {
299-
return c;
297+
if(xQueuePeek(uart->queue, data, to)) {
298+
return 1;
300299
}
301300
return 0;
302301
}
@@ -377,6 +376,10 @@ static void uart_on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old
377376
}
378377
// wait TX empty
379378
while(uart->dev->status.txfifo_cnt || uart->dev->status.st_utx_out);
379+
380+
if (xHigherPriorityTaskWoken) {
381+
portYIELD_FROM_ISR();
382+
}
380383
} else {
381384
//todo:
382385
// set baudrate

cores/esp32/esp32-hal-uart.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void uartEnd(uart_t* uart);
5656

5757
uint32_t uartAvailable(uart_t* uart);
5858
uint32_t uartAvailableForWrite(uart_t* uart);
59-
uint8_t uartRead(uart_t* uart);
60-
uint8_t uartPeek(uart_t* uart);
59+
uint8_t uartRead(uart_t* uart, uint8_t *data, size_t to);
60+
uint8_t uartPeek(uart_t* uart, uint8_t *data, size_t to);
6161

6262
void uartWrite(uart_t* uart, uint8_t c);
6363
void uartWriteBuf(uart_t* uart, const uint8_t * data, size_t len);

0 commit comments

Comments
 (0)