@@ -130,6 +130,7 @@ _uart(NULL),
130
130
_rxBufferSize(256 ),
131
131
_onReceiveCB(NULL ),
132
132
_onReceiveTimeout(true ),
133
+ _rxTimeout(10 ),
133
134
_onReceiveErrorCB(NULL ),
134
135
_eventTask(NULL )
135
136
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -192,7 +193,8 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
192
193
HSERIAL_MUTEX_LOCK ();
193
194
// function may be NULL to cancel onReceive() from its respective task
194
195
_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 ;
196
198
197
199
// this can be called after Serial.begin(), therefore it shall create the event task
198
200
if (function != NULL && _uart != NULL && _eventTask == NULL ) {
@@ -201,6 +203,22 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
201
203
HSERIAL_MUTEX_UNLOCK ();
202
204
}
203
205
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
+
204
222
void HardwareSerial::eventQueueReset ()
205
223
{
206
224
QueueHandle_t uartEventQueue = NULL ;
@@ -338,6 +356,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
338
356
_createEventTask (this );
339
357
}
340
358
359
+ // Set UART RX timeout
360
+ if (_uart != NULL ) {
361
+ uart_set_rx_timeout (_uart_nr, _rxTimeout);
362
+ }
363
+
341
364
HSERIAL_MUTEX_UNLOCK ();
342
365
}
343
366
0 commit comments