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

Using Serial.begin() more than one time crashes #5112

Closed
VisualMicro opened this issue Apr 26, 2021 · 11 comments · Fixed by #5549
Closed

Using Serial.begin() more than one time crashes #5112

VisualMicro opened this issue Apr 26, 2021 · 11 comments · Fixed by #5549
Assignees
Milestone

Comments

@VisualMicro
Copy link

VisualMicro commented Apr 26, 2021

Describe what is failing

Using Serial.begin(baud) twice crashes the ESP32 Dev Board

Arduino ESP32 Dev Board from this package:-
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json

Show the shortest possible code that will duplicate the error

void setup()
{
  Serial.begin(115200);
  Serial.print("Hello World");
  Serial.begin(115200);
}

void loop() {}

Show the EXACT error message (it doesn't work is not enough)

Hello Worldets Jul 29 2019 12:21:46

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1412
load:0x40078000,len:13400
load:0x40080400,len:3672
entry 0x400805f8

@lbernstone
Copy link
Contributor

lbernstone commented Apr 26, 2021

Please follow the issue template and indicate which device and version has the issue. 2.0.0alpha seems to have some problems in HardwareSerial. Even initializing two uarts causes a crash. Does not occur in esp32-s2 (@me-no-dev)

@VisualMicro
Copy link
Author

VisualMicro commented Apr 26, 2021

Please follow the issue template and indicate which device and version has the issue. 2.0.0alpha seems to have some problems in HardwareSerial. Even initializing two uarts causes a crash. Does not occur in esp32-s2 (@me-no-dev)

Sorry, I have added a link to the latest arduino-esp32 json, the crash info from the Serial Monitor and made a clearer statement of the issue.

The two serial issue you mention might be related. Will leave it with you. Let me know if you need anything else. Thanks

@me-no-dev
Copy link
Member

confirmed

TD-er added a commit to TD-er/ESPEasy that referenced this issue Apr 27, 2021

Verified

This commit was signed with the committer’s verified signature.
eps1lon Sebastian "Sebbie" Silbermann
When calling Serial::begin() (on ESP32) you apparently need to give all parameters or else it may use default parameters (makes no sense!)
Serial::end() may cause a hang or crash -> timing issue

See crash/hang:
- espressif/arduino-esp32#5047
- espressif/arduino-esp32#5004
- espressif/arduino-esp32@81b7c47
- espressif/arduino-esp32#5112
- espressif/arduino-esp32#5032

Switch back to default:
- espressif/arduino-esp32#5026
@pgrawehr
Copy link
Contributor

pgrawehr commented May 5, 2021

Just observed a very similar issue. It does not crash, but fails to work after the second Serial.begin(). Try this simple sketch:

void setup() {
  // initialize serial:
  Serial.begin(115200);
  // Serial.begin(115200); // If this line is uncommented, Serial.available() will always return 0. 
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    Serial.print("There's some data: ");
    // look for the next valid integer in the incoming serial stream:
    int red = Serial.read();
    Serial.println(red, HEX);
  }
  // Serial.print("I'm still alive"); // Enable this to see that the board does not crash
}

If the program is run with the second Serial.begin() commented out, everything works as expected and the sketch prints the hex values of whatever it gets over the serial console. If the line is enabled, nothing is ever printed. But the board is not dead, as can be shown by uncommenting the last line, which keeps being printed.

In case it matters, this is with the released version 1.0.6.

@sansillusion
Copy link

Why would anyone run serial.begin() twice ?

@pgrawehr
Copy link
Contributor

pgrawehr commented May 5, 2021

@sansillusion In my particular case, I have a quite complex library, and I haven't found out where exactly the second call comes from, but it seems to happen while executing a software reset command (initiated over the serial communication). The library compiles and runs flawlessly on other boards. A more common case would be if you want to dynamically change the baudrate.

@pgrawehr
Copy link
Contributor

pgrawehr commented May 7, 2021

Update: While the above finding is still true (it reproduces easily), my problem was actually caused by setting the state of GPIO 1 and 3 to output during a software reset. Apparently, these two logical lines are used for the USB Uart and should not be touched. So I found a bug that wasn't even the cause of my problems...

@stale
Copy link

stale bot commented Jul 8, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Jul 8, 2021
@VojtechBartoska
Copy link
Contributor

keeping this open

@stale
Copy link

stale bot commented Jul 13, 2021

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

@stale stale bot removed the Status: Stale Issue is stale stage (outdated/stuck) label Jul 13, 2021
@VojtechBartoska VojtechBartoska added this to the 2.0.0 milestone Jul 14, 2021
@SuGlider
Copy link
Collaborator

Issue fixed on PR #5385

@SuGlider SuGlider self-assigned this Jul 19, 2021
me-no-dev pushed a commit that referenced this issue Aug 23, 2021

Verified

This commit was signed with the committer’s verified signature.
eps1lon Sebastian "Sebbie" Silbermann
## Summary
This PR is a complete reffactoring of UART Serial Hardware and respective HAL in order to use IDF instead of current Register manipulation approach. 

It  implements Arduino SerialEvent functionality. 

Fix #5287  
Fix #5273 
Fix #5519 
Fix #5247 
Fix #5403
Fix #5429
Fix #5047
Fix #5463
Fix #5362 
Fix #5112  
Fix #5443 

## Impact
It solves many reported issues related to UART.
It was tested and works fine for ESP32, ESP-S2 and ESP32-C3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants