Skip to content

Serial.begin() flushes RX FIFO + changing RX IO inserts BREAK to UART Line #9020

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

Closed
1 task done
terrafirma2021 opened this issue Dec 18, 2023 · 12 comments
Closed
1 task done

Comments

@terrafirma2021
Copy link

terrafirma2021 commented Dec 18, 2023

Board

esp32-s3

Device Description

wokwi S3
Feather-S3

Hardware Configuration

No

Version

latest master (checkout manually)

IDE Name

2.2.1

Operating System

win 10 x64

Flash frequency

240

PSRAM enabled

yes

Upload speed

115200

Description

// works fine
void sendHOverSerial1() {
Serial1.begin(115200, SERIAL_8N1, 38, 3); // Re-initialize Serial1 with different pins
Serial1.write("H");
delay(1000);
Serial1.begin(115200, SERIAL_8N1, 38, 1); // Initialize Serial1 back to stock pins
}
// Causes panic
void sendIOverSerial2() {
Serial2.begin(115200, SERIAL_8N1, 47, 21); // Initialize Serial2 with pin swap
Serial2.write("I");
delay(1000);
// Serial2.begin(115200, SERIAL_8N1, 11, 10); // <------- causes panic
}

Also when
void sendIOverSerial2() {

is enabled

void sendHOverSerial1() { // breaks

Sketch

//https://wokwi.com/projects/384481453752614913 attached for easy viewing

void setup() {
  Serial.begin(115200);                
  Serial2.begin(115200, SERIAL_8N1, 11, 10);
  Serial1.begin(115200, SERIAL_8N1, 38, 1);
}

void loop() {
  // Check if Serial1 has received any data
  if (Serial1.available()) {
    char byteReceived1 = Serial1.read();
    Serial.print("Byte received on Serial1: ");
    Serial.println(byteReceived1);
  }

  // Check if Serial2 has received any data
  if (Serial2.available()) {
    char byteReceived2 = Serial2.read();
    Serial.print("Byte received on Serial2: ");
    Serial.println(byteReceived2);
  }

  // Call the functions to send "H" and "I"
  sendHOverSerial1();
  sendIOverSerial2(); // Stops sendHOverSerial1() working
}

void sendHOverSerial1() {
  Serial1.begin(115200, SERIAL_8N1, 38, 3); // Re-initialize Serial1 with different pins
  Serial1.write("H"); 
  delay(1000); 
 Serial1.begin(115200, SERIAL_8N1, 38, 1); // Initialize Serial1 back to stock pins
}

void sendIOverSerial2() {
  Serial2.begin(115200, SERIAL_8N1, 47, 21);  // Initialize Serial2 with pin swap 
  Serial2.write("I"); 
  delay(1000); 
  // Serial2.begin(115200, SERIAL_8N1, 11, 10); // <------- causes panic
}

Debug Message

00:04.200
100%

ESP-ROM:esp32s3-20210327

Build:Mar 27 2021

rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)

SPIWP:0xee

mode:DIO, clock div:1

load:0x3fce3808,len:0x370

load:0x403c9700,len:0x900

load:0x403cc700,len:0x2364

SHA-256 comparison failed:

Calculated: bea01f04c6f0e287a682f128805e3fce115955179100e98d8f412fe7697f8bdc

Expected: 08ea492e448f88de75319ed18ac319444e578d9c6bc7003c5c4807382bf389bd

Attempting to boot anyway...

entry 0x403c98ac

Other Steps to Reproduce

https://wokwi.com/projects/384481453752614913

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@terrafirma2021 terrafirma2021 added the Status: Awaiting triage Issue is waiting for triage label Dec 18, 2023
@mrengineer7777
Copy link
Collaborator

I don't know about the S3, but on ESP32-WROOM32UE "Serial1" isn't available because it's used internally for the flash memory. I always use "Serial" and "Serial2".

@terrafirma2021
Copy link
Author

@mrengineer7777
Thanks for the reply, serial1 is working without issues, however serial2 is the issue, that only occurs with the above config,

using standard configs, both serial 1+2 work without issues

@terrafirma2021
Copy link
Author

Hi, any updates to this please :)

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 3, 2024

Yes, there is some problem with ESP32-S3 UART2 indeed.
It sends a BREAK whenever it is started causing a bad reading.
It is not related to the GPIOs 10 and 11.

I'll continue to investigating it.

@terrafirma2021
Copy link
Author

Thank you for getting round to looking at this.

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 3, 2024

@terrafirma2021 - Just to check, you are using Arduino Core 3.0.0-Alpha3 or some other specifc version (or actually it was checked out from master)?

I don't get any panic (in the sense of a Guru Mediation and a reset), but it seems that there is some issue with how Serial2 is working.

Please post the error messages / output from Serial Monitor / etc.

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 6, 2024

I found out something:

void sendHOverSerial1() {
  Serial1.begin(115200, SERIAL_8N1, 38, 3); // Re-initialize Serial1 with different pins
  Serial1.write("H"); 
  delay(1000); 
 Serial1.begin(115200, SERIAL_8N1, 38, 1); // Initialize Serial1 back to stock pins
}

void sendIOverSerial2() {
  Serial2.begin(115200, SERIAL_8N1, 47, 21);  // Initialize Serial2 with pin swap 
  Serial2.write("I"); 
  delay(1000); 
  // Serial2.begin(115200, SERIAL_8N1, 11, 10); // <------- causes panic
}

sendHOverSerial1() doesn't change the RX IO.
sendIOverSerial2() changes RX IO.

Changing RX IO causes a UART BREAK signal in the line. This causes an error in the HardwareSerial driver.

Other detail:
HardwareSerial::begin() has a problem that flushes the TX FIFO by using IDF internally.
Therefore, when the function writes to the Serial, the byte goes to the FIFO, but when next executing begin(), it will flush FIFO and loop() won't read any data.

I'm working on some possible patch.

@SuGlider SuGlider added Type: Bug 🐛 All bugs and removed Status: Awaiting triage Issue is waiting for triage labels Jan 6, 2024
@SuGlider SuGlider added this to the 3.0.0-RC1 milestone Jan 6, 2024
@SuGlider SuGlider moved this from Todo to Under investigation in Arduino ESP32 Core Project Roadmap Jan 6, 2024
@SuGlider SuGlider changed the title Serial2 breaks Serial1 Serial.begin() flushes RX FIFO + changing RX IO inserts BREAK to UART Line Jan 6, 2024
@terrafirma2021
Copy link
Author

terrafirma2021 commented Jan 9, 2024

Hi, sorry for not reaching back to you sooner, I never tested on the latest core ide it was an earlier version.

The issue still exists as noted above on the latest core ide,

seeing the serial.begin bug also explains why I was having issues whereby the serial monitor would drop out, I thought it was the way I handled my code, but it was a bug for sure, thanks for confirming this also!

As a workaround I was keeping track of sent bytes to handle echo, which avoided pin swap.

@SuGlider
Copy link
Collaborator

@terrafirma2021 - #9095 shall fix it for Arduino 3.0.0.
I'll also add a patch for the Arduino 2.0.15.

@terrafirma2021
Copy link
Author

Your a star thank you very much :D

@VojtechBartoska VojtechBartoska added the Status: Review needed Issue or PR is awaiting review label Jan 15, 2024
@me-no-dev me-no-dev modified the milestones: 3.0.0-RC1, 2.0.15 Jan 23, 2024
@VojtechBartoska VojtechBartoska moved this from In Review to In Progress in Arduino ESP32 Core Project Roadmap Jan 25, 2024
@VojtechBartoska
Copy link
Contributor

Closing this as solved by above mentioned PR.

@VojtechBartoska VojtechBartoska added Status: Solved and removed Status: Review needed Issue or PR is awaiting review labels Feb 20, 2024
@terrafirma2021
Copy link
Author

Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

5 participants