Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use begin() method to just change baudrate #11121

Closed
GitHubLionel opened this issue Mar 13, 2025 · 6 comments · Fixed by #11122
Closed

Don't use begin() method to just change baudrate #11121

GitHubLionel opened this issue Mar 13, 2025 · 6 comments · Fixed by #11122
Assignees
Labels
bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Peripheral: UART
Milestone

Comments

@GitHubLionel
Copy link

Ok, in that case, we need a warning for the begin() method to say that if you just want to change the Baudrate you must use updateBaudRate() method even if uartBegin() seems to allow this change.

Originally posted by @GitHubLionel in #10314

@Jason2866 Jason2866 added the Type: Documentation Issue pertains to Documentation of Arduino ESP32 label Mar 13, 2025
@GitHubLionel
Copy link
Author

But I think that it will be better to correct the code in esp32-hal-uart.c since uartBegin() method should be do that.

@Jason2866
Copy link
Collaborator

Jason2866 commented Mar 13, 2025

There is nothing to correct when using wrong command for changing the UART speed.
As shown in example https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Serial/Serial_All_CPU_Freqs/Serial_All_CPU_Freqs.ino

@SuGlider SuGlider self-assigned this Mar 13, 2025
@SuGlider SuGlider added Peripheral: UART bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. and removed Type: Documentation Issue pertains to Documentation of Arduino ESP32 labels Mar 13, 2025
@SuGlider
Copy link
Collaborator

Up on the Arduino specification, Serial.begin(baud_rate) must correctly change the baud rate.
Therefore, I consider it as a bug.

I'll get a PR to fix it.

@SuGlider
Copy link
Collaborator

@GitHubLionel - please test PR #11122 and let me know.

@GitHubLionel
Copy link
Author

GitHubLionel commented Mar 14, 2025

Hi,
No, not work because MUTEX is misplaced.
You should replace this code:

      UART_MUTEX_LOCK();
      //User may just want to change some parameters, such as baudrate, data length, parity, stop bits or pins
      if (uart->_baudrate != baudrate) {
        if (!uartSetBaudRate(uart, baudrate)) {
          log_e("UART%d changing baudrate failed.", uart_nr);
          retCode = false;
        } else {
          log_v("UART%d changed baudrate to %d", uart_nr, baudrate);
          uart->_baudrate = baudrate;
        }
      }

By this one:

      //User may just want to change some parameters, such as baudrate, data length, parity, stop bits or pins
      if (uart->_baudrate != baudrate) {
      	retCode = uartSetBaudRate(uart, baudrate);
      }
      UART_MUTEX_LOCK();

Another thing, if you could use %ld in log message in uartSetBaudRate() for baud_rate to avoid compiler warning.
log_v("Setting UART%d baud rate to %ld.", uart->num, baud_rate);
log_e("Setting UART%d baud rate to %ld has failed.", uart->num, baud_rate);

@SuGlider
Copy link
Collaborator

@GitHubLionel - Thanks for testing and reviewing. All good now. This issue shall be closed when the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Peripheral: UART
Projects
Development

Successfully merging a pull request may close this issue.

3 participants