Skip to content

Commit 20ecdae

Browse files
authored
Adds setRxTimeout()
1 parent c1b08e8 commit 20ecdae

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: cores/esp32/HardwareSerial.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef enum {
6464
UART_PARITY_ERROR
6565
} hardwareSerial_error_t;
6666

67-
typedef std::function<void()> OnReceiveCb;
67+
typedef std::function<void(void)> OnReceiveCb;
6868
typedef std::function<void(hardwareSerial_error_t)> OnReceiveErrorCb;
6969

7070
class HardwareSerial: public Stream
@@ -73,6 +73,14 @@ class HardwareSerial: public Stream
7373
HardwareSerial(int uart_nr);
7474
~HardwareSerial();
7575

76+
// setRxTimeout sets the timeout after which onReceive callback will be called (after receiving data, it waits for this time of UART rx inactivity to call the callback fnc)
77+
// param symbols_timeout defines a timeout threshold in uart symbol periods. Setting 0 symbol timeout disables the callback call by timeout.
78+
// Maximum timeout setting is calculacted automatically by IDF. If set above the maximum, it is ignored and an error is printed on Serial0 (check console).
79+
// Examples: Maximum for 11 bits symbol is 92 (SERIAL_8N2, SERIAL_8E1, SERIAL_8O1, etc), Maximum for 10 bits symbol is 101 (SERIAL_8N1).
80+
// For example symbols_timeout=1 defines a timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
81+
// For a baudrate of 9600, SERIAL_8N1 (10 bit symbol) and symbols_timeout = 3, the timeout would be 3 / (9600 / 10) = 3.125 ms
82+
void setRxTimeout(uint8_t symbols_timeout);
83+
7684
// onReceive will setup a callback that will be called whenever an UART interruption occurs (UART_INTR_RXFIFO_FULL or UART_INTR_RXFIFO_TOUT)
7785
// UART_INTR_RXFIFO_FULL interrupt triggers at UART_FULL_THRESH_DEFAULT bytes received (defined as 120 bytes by default in IDF)
7886
// UART_INTR_RXFIFO_TOUT interrupt triggers at UART_TOUT_THRESH_DEFAULT symbols passed without any reception (defined as 10 symbos by default in IDF)
@@ -151,7 +159,9 @@ class HardwareSerial: public Stream
151159
uart_t* _uart;
152160
size_t _rxBufferSize;
153161
OnReceiveCb _onReceiveCB;
162+
// _onReceive and _rxTimeout have be consistent when timeout is disabled
154163
bool _onReceiveTimeout;
164+
uint8_t _rxTimeout;
155165
OnReceiveErrorCb _onReceiveErrorCB;
156166
TaskHandle_t _eventTask;
157167

0 commit comments

Comments
 (0)