@@ -377,77 +377,77 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
377
377
break ;
378
378
#endif
379
379
}
380
+ }
381
+
382
+ // if no RX/TX pins are defined, it will not start the UART driver
383
+ if (rxPin < 0 && txPin < 0 ) {
384
+ log_e (" No RX/TX pins defined. Please set RX/TX pins." );
385
+ HSERIAL_MUTEX_UNLOCK ();
386
+ return ;
387
+ }
380
388
381
- // if no RX/TX pins are defined, it will not start the UART driver
382
- if (rxPin < 0 && txPin < 0 ) {
383
- log_e (" No RX/TX pins defined. Please set RX/TX pins." );
384
- HSERIAL_MUTEX_UNLOCK ();
385
- return ;
386
- }
387
-
388
- // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
389
- // it will detach previous UART attached pins
389
+ // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
390
+ // it will detach previous UART attached pins
390
391
391
- // indicates that uartbegin() has to initialize a new IDF driver
392
- if (_testUartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) {
393
- _destroyEventTask (); // when IDF uart driver must be restarted, _eventTask must finish too
394
- }
392
+ // indicates that uartbegin() has to initialize a new IDF driver
393
+ if (_testUartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) {
394
+ _destroyEventTask (); // when IDF uart driver must be restarted, _eventTask must finish too
395
+ }
395
396
396
- // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
397
- // it will detach previous UART attached pins
398
- _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
399
- if (_uart == NULL ) {
400
- log_e (" UART driver failed to start. Please check the logs." );
401
- HSERIAL_MUTEX_UNLOCK ();
402
- return ;
397
+ // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
398
+ // it will detach previous UART attached pins
399
+ _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
400
+ if (_uart == NULL ) {
401
+ log_e (" UART driver failed to start. Please check the logs." );
402
+ HSERIAL_MUTEX_UNLOCK ();
403
+ return ;
404
+ }
405
+ if (!baud) {
406
+ // using baud rate as zero, forces it to try to detect the current baud rate in place
407
+ uartStartDetectBaudrate (_uart);
408
+ time_t startMillis = millis ();
409
+ unsigned long detectedBaudRate = 0 ;
410
+ while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
411
+ yield ();
403
412
}
404
- if (!baud) {
405
- // using baud rate as zero, forces it to try to detect the current baud rate in place
406
- uartStartDetectBaudrate (_uart);
407
- time_t startMillis = millis ();
408
- unsigned long detectedBaudRate = 0 ;
409
- while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
410
- yield ();
411
- }
412
413
413
- if (detectedBaudRate) {
414
- delay (100 ); // Give some time...
415
- _uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
416
- if (_uart == NULL ) {
417
- log_e (" UART driver failed to start. Please check the logs." );
418
- HSERIAL_MUTEX_UNLOCK ();
419
- return ;
420
- }
421
- } else {
422
- log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
423
- _uart = NULL ;
414
+ if (detectedBaudRate) {
415
+ delay (100 ); // Give some time...
416
+ _uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
417
+ if (_uart == NULL ) {
418
+ log_e (" UART driver failed to start. Please check the logs." );
419
+ HSERIAL_MUTEX_UNLOCK ();
420
+ return ;
424
421
}
422
+ } else {
423
+ log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
424
+ _uart = NULL ;
425
425
}
426
- // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate,
427
- // or when setting the callback before calling begin()
428
- if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL ) && _eventTask == NULL ) {
429
- _createEventTask (this );
430
- }
426
+ }
427
+ // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate,
428
+ // or when setting the callback before calling begin()
429
+ if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL ) && _eventTask == NULL ) {
430
+ _createEventTask (this );
431
+ }
431
432
432
- // Set UART RX timeout
433
- uartSetRxTimeout (_uart, _rxTimeout);
434
-
435
- // Set UART FIFO Full depending on the baud rate.
436
- // Lower baud rates will force to emulate byte-by-byte reading
437
- // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt
438
- // It can also be changed by the application at any time
439
- if (!_rxFIFOFull) { // it has not being changed before calling begin()
440
- // set a default FIFO Full value for the IDF driver
441
- uint8_t fifoFull = 1 ;
442
- if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) {
443
- fifoFull = 120 ;
444
- }
445
- uartSetRxFIFOFull (_uart, fifoFull);
446
- _rxFIFOFull = fifoFull;
433
+ // Set UART RX timeout
434
+ uartSetRxTimeout (_uart, _rxTimeout);
435
+
436
+ // Set UART FIFO Full depending on the baud rate.
437
+ // Lower baud rates will force to emulate byte-by-byte reading
438
+ // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt
439
+ // It can also be changed by the application at any time
440
+ if (!_rxFIFOFull) { // it has not being changed before calling begin()
441
+ // set a default FIFO Full value for the IDF driver
442
+ uint8_t fifoFull = 1 ;
443
+ if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) {
444
+ fifoFull = 120 ;
447
445
}
448
-
449
- HSERIAL_MUTEX_UNLOCK () ;
446
+ uartSetRxFIFOFull (_uart, fifoFull);
447
+ _rxFIFOFull = fifoFull ;
450
448
}
449
+
450
+ HSERIAL_MUTEX_UNLOCK ();
451
451
}
452
452
453
453
void HardwareSerial::updateBaudRate (unsigned long baud) {
0 commit comments