@@ -376,77 +376,78 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
376
376
}
377
377
break ;
378
378
#endif
379
- }
379
+ }
380
380
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
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
+ }
390
387
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
- }
388
+ // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
389
+ // it will detach previous UART attached pins
395
390
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 ;
403
- }
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 ();
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
+ }
395
+
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 ;
411
403
}
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
412
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 ;
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 ;
420
424
}
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 ;
424
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
+ // 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
+ }
431
431
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 ;
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;
444
447
}
445
- uartSetRxFIFOFull (_uart, fifoFull);
446
- _rxFIFOFull = fifoFull;
447
- }
448
448
449
- HSERIAL_MUTEX_UNLOCK ();
449
+ HSERIAL_MUTEX_UNLOCK ();
450
+ }
450
451
}
451
452
452
453
void HardwareSerial::updateBaudRate (unsigned long baud) {
0 commit comments