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

Uart detach 2.0.13 #8629

Merged
merged 5 commits into from
Sep 13, 2023
Merged

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Sep 13, 2023

Description of Change

There is a bug in HardwareSerial::setPins() that sets the UART pins internally to the wrong value at the end of the operation, which may lead to errors in HardwareSerial::end(), not dettaching the right GPIO from UART.

This PR also makes sure that HardwareSerial::setPins() will always detach previous attached GPIOs.

ESP32 SoCs attach RX0 and TX0 console pins in boot time, connecting those GPIOs to IOMUX or GPIO Matrix.
The PR makes sure that if the application calls Serial.begin(baud, rxPin, txPin) at first to setup UART, it will detach Console RX0/TX0.
The same, if the user calls HardwareSerial::setPins() or HardwareSerial::end() with no previous HardwareSerial::begin(),

Summary of changes:

  • It is now possible to dettach UART0 pins by calling Serial.end() with no previous begin()
  • It is now possible to call setPins() before begin() or in any order
  • setPins() will detach any previous pins that have been changed
  • begin(baud, rx, tx) will detach any previous attached pins
  • setPins() or begin(baud, rx, tx), when called at first, will detach console RX0/TX0, attached in boot
  • any pin set as -1 in begin() or setPins() won't be changed nor detached
  • begin(baud) will not changed any pins that have been set before this call through a previous begin(baud, rx, tx) or setPin()
  • If the application only uses RX or TX, begin(baud, -1, tx) or begin(baud, rx) will change only the assigned pin and keep the other unchanged.

Tests scenarios

Use Cases:

  1. First time calling end() or setPins() to detach default RX0/TX0
  2. First time calling begin(baud) --> it will use SoC default RX/TX pins
  3. First calls begin(baud, rx, tx) then calls begin(baud) --> it will keep rx, tx from previous begin() + detaches default RX/TX
  4. First calls begin(baud), then setPin(rx, tx), then calls begin(baud) --> detaches RX0/TX0 and attach rx/tx from setPins()
  5. First calls begin(baud), then end(), then calls begin(baud, rx, tx) --> it will use rx/tx pins + detaches default RX/TX
  6. First calls begin(baud) then begin(baud, rx, tx) --> it will use rx/tx from second begin() + detaches default RX/TX
  7. First calls begin(baud, rx, tx) --> detaches default RX/TX (UART0 only) + attachs RX/TX from begin()
  8. First calls begin(baud, rx1, tx1) then calls begin(baud, rx2, tx2) --> detaches rx1/tx1 + attachs rx2/tx2
  9. First calls begin(baud) then calls begin(baud, -1, tx) --> detaches default TX, attaches tx and default RX
  10. First calls begin(baud, -1, tx) --> attaches tx, leaves RX unchenged, but in UART0.end(), it will force dettach default RX
  11. First calls begin(baud, rx) --> attaches rx, leaves TX unchanged, but in UART0.end(), it will force dettahc default TX
  12. First calls begin(baud, rx) then calls begin(baud, rx2, tx2) --> detaches default RX/TX (UART0 only) + detaches rx + attaches rx2/tx2
void setup() {
  ets_printf("\nThis Line is printed fine because it uses TX Pin attached in Boot Time.\n\n");
  Serial.end();                 // shall detach default UART0 RX/TX
  ets_printf("1:: This Log line shall not be displayed!\n"); // using IDF console ets_printf() -- TX unattached!
  
  Serial.begin(115200);  // set default RX/TX pin because nothing was defined in the call
  Serial.println("2:: This line shall be seen.");
  Serial.flush();

  Serial.setPins(-1, 2);    // shall set UART0 to GPIO2 and detach default TX 
  Serial.println("3:: This line shall NOT be seen.");  // default TX is no longer attached
  Serial.flush();
  
  Serial.end();
  Serial.setPins(-1, 2); // it sets TX as GPIO2
  Serial.begin(115200);  // This shall not set default TX pin because the previous setPin() was executed
  Serial.println("4:: This line shall NOT be seen.");  // default TX is no longer attached
  Serial.flush();
}

void loop() {}
// Check previous GPIOs with a Logic Analyzer
void setup() {
  Serial.begin(115200);  // sets default RX and TX
  Serial.println("This will go out in the default TX pin");
  Serial.flush();
  
  Serial.setPins(-1, 2);  // Now TX shall be GPIO2 and default TX GPIO can't send any data
  Serial.println("This line can't be seen in the default TX GPIO!");
  Serial.flush();
}

void loop(){
}

Related links

Closes #8324
Closes #8573
Closes #8607

@SuGlider
Copy link
Collaborator Author

SuGlider commented Sep 13, 2023

@me-no-dev - Please review and merge this PR to 2.0.13

I found a few differences from master to 2.0.x and to the 3.0.0 version.
I'll need to rework carefully UART driver for 3.0.0.

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

Successfully merging this pull request may close these issues.

None yet

2 participants