Skip to content

Commit 807d750

Browse files
authored
adds onReceive() parameter
In order to allow the user to choose if onReceive() call back will be called only when UART Rx timeout happens or also when UART FIFO gets 120 bytes, a new parameter has been added to onReceive() with the default behavior based on timeout. void onReceive(OnReceiveCb function, bool onlyOnTimeout = true); onReceive will setup a callback that will be called whenever an UART interruption occurs (UART_INTR_RXFIFO_FULL or UART_INTR_RXFIFO_TOUT) UART_INTR_RXFIFO_FULL interrupt triggers at UART_FULL_THRESH_DEFAULT bytes received (defined as 120 bytes by default in IDF) UART_INTR_RXFIFO_TOUT interrupt triggers at UART_TOUT_THRESH_DEFAULT symbols passed without any reception (defined as 10 symbos by default in IDF) onlyOnTimeout parameter will define how onReceive will behave: Default: true -- The callback will only be called when RX Timeout happens. Whole stream of bytes will be ready for being read on the callback function at once. This option may lead to Rx Overflow depending on the Rx Buffer Size and number of bytes received in the streaming false -- The callback will be called when FIFO reaches 120 bytes and also on RX Timeout. The stream of incommig bytes will be "split" into blocks of 120 bytes on each callback. This option avoid any sort of Rx Overflow, but leaves the UART packet reassembling work to the Application.
1 parent 281ae37 commit 807d750

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ _uart_nr(uart_nr),
129129
_uart(NULL),
130130
_rxBufferSize(256),
131131
_onReceiveCB(NULL),
132-
_rxTimeout(10),
132+
_onReceiveTimeout(true),
133133
_onReceiveErrorCB(NULL),
134134
_eventTask(NULL)
135135
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -187,11 +187,12 @@ void HardwareSerial::onReceiveError(OnReceiveErrorCb function)
187187
HSERIAL_MUTEX_UNLOCK();
188188
}
189189

190-
void HardwareSerial::onReceive(OnReceiveCb function)
190+
void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
191191
{
192192
HSERIAL_MUTEX_LOCK();
193193
// function may be NULL to cancel onReceive() from its respective task
194194
_onReceiveCB = function;
195+
_onReceiveTimeout = onlyOnTimeout;
195196

196197
// this can be called after Serial.begin(), therefore it shall create the event task
197198
if (function != NULL && _uart != NULL && _eventTask == NULL) {
@@ -200,20 +201,12 @@ void HardwareSerial::onReceive(OnReceiveCb function)
200201
HSERIAL_MUTEX_UNLOCK();
201202
}
202203

203-
void HardwareSerial::setRxTimeout(uint8_t symbols_timeout)
204-
{
205-
HSERIAL_MUTEX_LOCK();
206-
207-
_rxTimeout = symbols_timeout;
208-
209-
if(_uart != NULL) uart_set_rx_timeout(_uart_nr, _rxTimeout); // Set new timeout
210-
211-
HSERIAL_MUTEX_UNLOCK();
212-
}
213-
214204
void HardwareSerial::eventQueueReset()
215205
{
216206
QueueHandle_t uartEventQueue = NULL;
207+
if (_uart == NULL) {
208+
return;
209+
}
217210
uartGetEventQueue(_uart, &uartEventQueue);
218211
if (uartEventQueue != NULL) {
219212
xQueueReset(uartEventQueue);
@@ -232,14 +225,16 @@ void HardwareSerial::_uartEventTask(void *args)
232225
if(xQueueReceive(uartEventQueue, (void * )&event, (portTickType)portMAX_DELAY)) {
233226
switch(event.type) {
234227
case UART_DATA:
235-
if(uart->_onReceiveCB && uart->available() > 0) uart->_onReceiveCB(event.timeout_flag);
228+
if(uart->_onReceiveCB && uart->available() > 0 &&
229+
((uart->_onReceiveTimeout && event.timeout_flag) || !uart->_onReceiveTimeout) )
230+
uart->_onReceiveCB();
236231
break;
237232
case UART_FIFO_OVF:
238233
log_w("UART%d FIFO Overflow. Consider adding Hardware Flow Control to your Application.", uart->_uart_nr);
239234
if(uart->_onReceiveErrorCB) uart->_onReceiveErrorCB(UART_FIFO_OVF_ERROR);
240235
break;
241236
case UART_BUFFER_FULL:
242-
log_w("UART%d Buffer Full. Consider encreasing your buffer size of your Application.", uart->_uart_nr);
237+
log_w("UART%d Buffer Full. Consider increasing your buffer size of your Application.", uart->_uart_nr);
243238
if(uart->_onReceiveErrorCB) uart->_onReceiveErrorCB(UART_BUFFER_FULL_ERROR);
244239
break;
245240
case UART_BREAK:
@@ -343,11 +338,6 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
343338
_createEventTask(this);
344339
}
345340

346-
// Set UART RX timeout
347-
if (_uart != NULL) {
348-
uart_set_rx_timeout(_uart_nr, _rxTimeout);
349-
}
350-
351341
HSERIAL_MUTEX_UNLOCK();
352342
}
353343

0 commit comments

Comments
 (0)