Skip to content

Commit c1b08e8

Browse files
authored
Adds back setRxTimeout()
1 parent d5de62c commit c1b08e8

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ _uart(NULL),
130130
_rxBufferSize(256),
131131
_onReceiveCB(NULL),
132132
_onReceiveTimeout(true),
133+
_rxTimeout(10),
133134
_onReceiveErrorCB(NULL),
134135
_eventTask(NULL)
135136
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -192,7 +193,8 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
192193
HSERIAL_MUTEX_LOCK();
193194
// function may be NULL to cancel onReceive() from its respective task
194195
_onReceiveCB = function;
195-
_onReceiveTimeout = onlyOnTimeout;
196+
// When Rx timeout is Zero (disabled), there is only one possible option that is callback when FIFO reaches 120 bytes
197+
_onReceiveTimeout = _rxTimeout > 0 ? onlyOnTimeout : false;
196198

197199
// this can be called after Serial.begin(), therefore it shall create the event task
198200
if (function != NULL && _uart != NULL && _eventTask == NULL) {
@@ -201,6 +203,22 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
201203
HSERIAL_MUTEX_UNLOCK();
202204
}
203205

206+
// timout is calculates in time to receive UART symbols at the UART baudrate.
207+
// the estimation is about 11 bits per symbol (SERIAL_8N1)
208+
void HardwareSerial::setRxTimeout(uint8_t symbols_timeout)
209+
{
210+
HSERIAL_MUTEX_LOCK();
211+
212+
// Zero disables timeout, thus, onReceive callback will only be called when RX FIFO reaches 120 bytes
213+
// Any non-zero value will activate onReceive callback based on UART baudrate with about 11 bits per symbol
214+
_rxTimeout = symbols_timeout;
215+
if (!symbols_timeout) _onReceiveTimeout = false; // only when RX timeout is disabled, we also must disable this flag
216+
217+
if(_uart != NULL) uart_set_rx_timeout(_uart_nr, _rxTimeout); // Set new timeout
218+
219+
HSERIAL_MUTEX_UNLOCK();
220+
}
221+
204222
void HardwareSerial::eventQueueReset()
205223
{
206224
QueueHandle_t uartEventQueue = NULL;
@@ -338,6 +356,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
338356
_createEventTask(this);
339357
}
340358

359+
// Set UART RX timeout
360+
if (_uart != NULL) {
361+
uart_set_rx_timeout(_uart_nr, _rxTimeout);
362+
}
363+
341364
HSERIAL_MUTEX_UNLOCK();
342365
}
343366

0 commit comments

Comments
 (0)