@@ -140,7 +140,7 @@ _txBufferSize(0),
140140_onReceiveCB(NULL ),
141141_onReceiveErrorCB(NULL ),
142142_onReceiveTimeout(true ),
143- _rxTimeout(10 ),
143+ _rxTimeout(2 ),
144144_eventTask(NULL )
145145#if !CONFIG_DISABLE_HAL_LOCKS
146146 ,_lock(NULL )
@@ -212,6 +212,18 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout)
212212 HSERIAL_MUTEX_UNLOCK ();
213213}
214214
215+ // This function allow the user to define how many bytes will trigger an Interrupt that will copy RX FIFO to the internal RX Ringbuffer
216+ // ISR will also move data from FIFO to RX Ringbuffer after a RX Timeout defined in HardwareSerial::setRxTimeout(uint8_t symbols_timeout)
217+ // A low value of FIFO Full bytes will consume more CPU time within the ISR
218+ // A high value of FIFO Full bytes will make the application wait longer to have byte available for the Stkech in a streaming scenario
219+ // Both RX FIFO Full and RX Timeout may affect when onReceive() will be called
220+ void HardwareSerial::setRxFIFOFull (uint8_t fifoBytes)
221+ {
222+ HSERIAL_MUTEX_LOCK ();
223+ uartSetRxFIFOFull (_uart, fifoBytes); // Set new timeout
224+ HSERIAL_MUTEX_UNLOCK ();
225+ }
226+
215227// timout is calculates in time to receive UART symbols at the UART baudrate.
216228// the estimation is about 11 bits per symbol (SERIAL_8N1)
217229void HardwareSerial::setRxTimeout (uint8_t symbols_timeout)
@@ -223,7 +235,7 @@ void HardwareSerial::setRxTimeout(uint8_t symbols_timeout)
223235 _rxTimeout = symbols_timeout;
224236 if (!symbols_timeout) _onReceiveTimeout = false ; // only when RX timeout is disabled, we also must disable this flag
225237
226- if (_uart != NULL ) uart_set_rx_timeout (_uart_nr , _rxTimeout); // Set new timeout
238+ uartSetRxTimeout (_uart, _rxTimeout); // Set new timeout
227239
228240 HSERIAL_MUTEX_UNLOCK ();
229241}
@@ -250,6 +262,7 @@ void HardwareSerial::_uartEventTask(void *args)
250262 for (;;) {
251263 // Waiting for UART event.
252264 if (xQueueReceive (uartEventQueue, (void * )&event, (portTickType)portMAX_DELAY)) {
265+ hardwareSerial_error_t currentErr = UART_NO_ERROR;
253266 switch (event.type ) {
254267 case UART_DATA:
255268 if (uart->_onReceiveCB && uart->available () > 0 &&
@@ -258,28 +271,32 @@ void HardwareSerial::_uartEventTask(void *args)
258271 break ;
259272 case UART_FIFO_OVF:
260273 log_w (" UART%d FIFO Overflow. Consider adding Hardware Flow Control to your Application." , uart->_uart_nr );
261- if (uart-> _onReceiveErrorCB ) uart-> _onReceiveErrorCB ( UART_FIFO_OVF_ERROR) ;
274+ currentErr = UART_FIFO_OVF_ERROR;
262275 break ;
263276 case UART_BUFFER_FULL:
264277 log_w (" UART%d Buffer Full. Consider increasing your buffer size of your Application." , uart->_uart_nr );
265- if (uart-> _onReceiveErrorCB ) uart-> _onReceiveErrorCB ( UART_BUFFER_FULL_ERROR) ;
278+ currentErr = UART_BUFFER_FULL_ERROR;
266279 break ;
267280 case UART_BREAK:
268281 log_w (" UART%d RX break." , uart->_uart_nr );
269- if (uart-> _onReceiveErrorCB ) uart-> _onReceiveErrorCB ( UART_BREAK_ERROR) ;
282+ currentErr = UART_BREAK_ERROR;
270283 break ;
271284 case UART_PARITY_ERR:
272285 log_w (" UART%d parity error." , uart->_uart_nr );
273- if (uart-> _onReceiveErrorCB ) uart-> _onReceiveErrorCB ( UART_PARITY_ERROR) ;
286+ currentErr = UART_PARITY_ERROR;
274287 break ;
275288 case UART_FRAME_ERR:
276289 log_w (" UART%d frame error." , uart->_uart_nr );
277- if (uart-> _onReceiveErrorCB ) uart-> _onReceiveErrorCB ( UART_FRAME_ERROR) ;
290+ currentErr = UART_FRAME_ERROR;
278291 break ;
279292 default :
280293 log_w (" UART%d unknown event type %d." , uart->_uart_nr , event.type );
281294 break ;
282295 }
296+ if (currentErr != UART_NO_ERROR) {
297+ if (uart->_onReceiveErrorCB ) uart->_onReceiveErrorCB (currentErr);
298+ if (uart->_onReceiveCB && uart->available () > 0 ) uart->_onReceiveCB (); // forces User Callback too
299+ }
283300 }
284301 }
285302 }
@@ -366,9 +383,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
366383 }
367384
368385 // Set UART RX timeout
369- if (_uart != NULL ) {
370- uart_set_rx_timeout (_uart_nr, _rxTimeout);
371- }
386+ uartSetRxTimeout (_uart, _rxTimeout);
372387
373388 HSERIAL_MUTEX_UNLOCK ();
374389}
0 commit comments