-
-
Notifications
You must be signed in to change notification settings - Fork 64
Serial output is garbled if done immediately after calling Serial.begin() #37
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
If the second "hello" is still being sent when you reconfigure the UART, I would not be surprised with garbage. Perhaps flush first, or add significant delay before the second .begin() to be sure its done sending and safely in idle? |
The issue still occurs even with a call to But it's a good criticism of my demo code. It's funny because I originally had a |
So does the same behaviour happen if you call Serial.end() inbetween? |
Yes it does. I think I made a bad decision by using two calls to void setup() {
delay(1000); // to prove the issue is not related to printing immediately after startup
Serial.begin(9600);
Serial.println("hello");
}
void loop() {} output:
|
Hi @per1234 , diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp
index fa8098c..8e0520b 100644
--- a/cores/arduino/UART.cpp
+++ b/cores/arduino/UART.cpp
@@ -147,13 +147,6 @@ void UartClass::begin(unsigned long baud, uint16_t config)
_written = false;
- //Set up the rx pin
- pinMode(_hwserial_rx_pin, INPUT_PULLUP);
-
- //Set up the tx pin
- digitalWrite(_hwserial_tx_pin, HIGH);
- pinMode(_hwserial_tx_pin, OUTPUT);
-
int8_t sigrow_val = SIGROW.OSC16ERR5V;
baud_setting *= (1024 + sigrow_val);
baud_setting /= (1024 - abs(sigrow_val));
@@ -169,6 +162,13 @@ void UartClass::begin(unsigned long baud, uint16_t config)
(*_hwserial_module).CTRLA |= USART_RXCIE_bm;
+ //Set up the rx pin
+ pinMode(_hwserial_rx_pin, INPUT_PULLUP);
+
+ //Set up the tx pin
+ digitalWrite(_hwserial_tx_pin, HIGH);
+ pinMode(_hwserial_tx_pin, OUTPUT);
+
// Restore SREG content
SREG = oldSREG;
} If you could test it in your setup (also to make sure it doesn't regress anything else) I'll be more than happy to merge it. |
Thanks @facchinm! That patch fixes the problem for me also. I ran some other random sketches that use Serial without encountering any problems, but didn't do any more formal testing than that. I'll leave the patch in place and report if I encounter any problems. |
Arduino Uno WiFi Rev2
Arduino megaAVR Boards 1.8.1
Arduino IDE 1.8.10 Hourly Build 2019/05/21 09:33 / Arduino IDE 1.9.0-beta build 107
Windows 10 64 bit / Ubuntu 19.04
The following code:
results in this output:
Without a delay of around 100 us between the call to
Serial.begin()
andSerial.println()
, the output is garbled. As you can see from the output, this is not related to being soon after the program starts. It occurs even with subsequent calls toSerial.begin()
(it also would have after the first call without the delay).I've never experienced this with any other official Arduino board.
I've seen at least two threads on the Arduino Forum so far where this caused confusion.
The text was updated successfully, but these errors were encountered: