Skip to content

Serial1.setPins not working on ESP32-S3 #8755

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
Erklei opened this issue Oct 11, 2023 · 8 comments
Closed
1 task done

Serial1.setPins not working on ESP32-S3 #8755

Erklei opened this issue Oct 11, 2023 · 8 comments
Assignees
Labels
Peripheral: UART Resolution: Awaiting response Waiting for response of author Resolution: Unable to reproduce With given information issue is unable to reproduce

Comments

@Erklei
Copy link

Erklei commented Oct 11, 2023

Board

ESP32-S3

Device Description

Custom board with ESP32-S3

Hardware Configuration

custom board, hardware checked and good.

Version

v2.0.14

IDE Name

Arduino IDE 2.2.1

Operating System

Win10

Flash frequency

80MHz

PSRAM enabled

yes

Upload speed

921600 through USB CDC

Description

Serial1 pins can be assigned using begin(rx,tx etc
But I need to assign also RTS pin.
If I use Serial1.setPins, it looses Rx assignment. TX and RTS assignment are kept correct.

I test it by commenting or uncommenting Serial1.setPins function after Serial1.begin

Sketch

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600,SERIAL_8N1,13,11,false,500,112);//works, but I cannot assign RTS pin
  //Serial1.setPins(13, 11, -1, 12); //Does not work, but I need to assign RTS pin. 

  //Serial1.setMode(MODE_RS485_HALF_DUPLEX);//I need RTS for RS-485 mode
}

void loop() {
  if (Serial1.available()) {
    int bait = Serial1.read();
    Serial.println(bait);
  }
}

Debug Message

NA

Other Steps to Reproduce

No response

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

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@Erklei Erklei added the Status: Awaiting triage Issue is waiting for triage label Oct 11, 2023
@SuGlider
Copy link
Collaborator

SuGlider commented Oct 11, 2023

@Erklei

please use it this way (set the RTS Pins, set Hardware Flow Control to activate CTS/RTS control, set the RS485 Mode)

void setup() {
  Serial.begin(9600);
  
  Serial1.begin(9600,SERIAL_8N1,13,11,false,500,112);//works, but I cannot assign RTS pin
  if (!Serial1.setPins(-1, -1, -1, 12)) Serial.println("Failed setting RTS pin!"); // just  assigns RTS pin. 
  
  // UART_HW_FLOWCTRL_CTS_RTS or only UART_HW_FLOWCTRL_CTS | UART_HW_FLOWCTRL_RTS
  // UART_HW_FLOWCTRL_DISABLE to disable Hardware Control on UART
  // 64 is the Threshold for RX Flow control when RTS is used. It goes from 0 to 127 (bytes).
  if (!Serial1.setHwFlowCtrlMode(UART_HW_FLOWCTRL_RTS, 64)) 
    Serial.println("Failed changing Flow Control from Software to Hardware RTS!"); // use RTS only instead of xon/xoff

  //I need RTS for RS-485 mode
  if (!Serial1.setMode(MODE_RS485_HALF_DUPLEX)) Serial.println("Failed setting RS485 Mode"); 
}

void loop() {
  if (Serial1.available()) {
    int bait = Serial1.read();
    Serial.println(bait);
  }
}

// Negative Pin Number will keep it unmodified, thus this function can set individual pins
// SetPins shall be called after Serial begin()
bool setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin = -1, int8_t rtsPin = -1);
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
bool setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
// Used to set RS485 modes such as UART_MODE_RS485_HALF_DUPLEX for Auto RTS function on ESP32
bool setMode(uint8_t mode);

@SuGlider SuGlider added Type: Question Only question Peripheral: UART and removed Status: Awaiting triage Issue is waiting for triage labels Oct 11, 2023
@Erklei
Copy link
Author

Erklei commented Oct 12, 2023

Hi. I couldn't compile it with "if (!Serial1.setHwFlowCtrlMode(UART_HW_FLOWCTRL_RTS, 64)) "
I had to use "HW_FLOWCTRL_RTS" instead.
Then it compiled and uploaded but returned:

E (4383) uart: uart_set_mode(1729): disable hw flowctrl before using RS485 mode
Failed setting RS485 Mode

I think this setPins issue should be listed as bug.

Edit: Your solution does not work anyway even without the MODBUS mode thing:

Serial1.begin(9600,SERIAL_8N1,13,11,false,500,112);
Serial1.setPins(-1, -1, -1, 12); //Still breaks RX pin assignment

@SuGlider SuGlider added Status: Needs investigation We need to do some research before taking next steps on this issue and removed Type: Question Only question labels Oct 12, 2023
@SuGlider
Copy link
Collaborator

@Erklei - setPins() works as expected. I see no issue.

I tried these 2 examples with the ESP32-S3:

Example 1 - Uses Serial Monitor to send data to Serial, read it and print it back

(don't forget to set Serial Monitor to 9600 baud)

void setup() {
  Serial.begin(9600, SERIAL_8N1, -1, -1, false, 500, 112);
  Serial.println("Started.");

  Serial.setPins(-1, -1, -1, 5); // sets RTS Pin only
}

void loop() {
  if (Serial.available() > 0) {
    Serial.print((char)Serial.read());
  }
  delay(50);
}

Example 2 - Cross connect Serial1 RX pin (13) with Serial TX pin. It executes by itself.

(don't forget to set Serial Monitor to 115200 baud to see the results)

void setup() {
  Serial.begin(115200, SERIAL_8N1);
  // RX 13 -- TX 11
  Serial1.begin(115200,SERIAL_8N1,13,11,false,500,112);//works, but I cannot assign RTS pin

  // Both of these next 2 lines works fine!
  //Serial1.setPins(13, 11, -1, 12); //Does not work, but I need to assign RTS pin. 
  Serial1.setPins(-1, -1, -1, 12); //Does not work, but I need to assign RTS pin. 
  
  //Serial1.setMode(MODE_RS485_HALF_DUPLEX);//I need RTS for RS-485 mode
  delay(100);
  
  Serial.println("Started.");
  // waits Serial to send all its data
  delay(100);
  
  // a lot of data has been sent to Serial1 RX by Serial TX in boot.
  while (Serial1.available()) Serial1.read(); // just ignores the last sent data
}

void loop() {
  if (Serial1.available()) {
    char bait = Serial1.read() + 1; // next letter A->B
    Serial.println(bait); // sends "B\r\n" to Serial Monitor and Serial1
    delay(100);
    // consumes the 3 chars just sent to Serial1 by cross connection
    bool ERR = false;
    char ch = Serial1.read();
    if (ch != 'B') {
      Serial.println();
      Serial.println((int) ch);
      ERR = true;
    }
    ch = Serial1.read();
    if (ch != '\r') {
      Serial.println();
      Serial.println((int) ch);
      ERR = true;
    }
    ch = Serial1.read();
    if (ch != '\n') {
      Serial.println();
      Serial.println((int) ch);
      ERR = true;
    }
    if (ERR) {
      Serial.println("\n===> ERRROR reading Serial1!");
      while(1); // halts execution.
    }
  }

  // send 'A' to be read by Serial1 in the cross connections TX0->RX1
  Serial.print('A');
  delay(500);
}

Output of the Example2:

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:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
Started.
AB
AB
AB
AB
AB
AB
AB

@SuGlider
Copy link
Collaborator

Example 2 works also when Serial1.setMode(MODE_RS485_HALF_DUPLEX); is uncommented.

@SuGlider
Copy link
Collaborator

Please make sure you are using Arduino Core 2.0.14

@SuGlider SuGlider added Resolution: Unable to reproduce With given information issue is unable to reproduce Resolution: Awaiting response Waiting for response of author and removed Status: Needs investigation We need to do some research before taking next steps on this issue labels Oct 12, 2023
@Erklei
Copy link
Author

Erklei commented Oct 12, 2023

Strange. I am using 2.0.14, but cant get past the second line of your code. Maybe you are using new 3.0 alpha?
image

@SuGlider
Copy link
Collaborator

SuGlider commented Oct 12, 2023

The error in compilation is due to the settings that are using the USB CDC, not the UART as Serial.

Set "USB CDC On Boot" to "Disable" and then Serial will use HardwareSerial API instead of HWCDC API.

@Erklei
Copy link
Author

Erklei commented Oct 13, 2023

Ok, got it working correctly after disabling USB.

In summary:

  • No information that different API is used if USB CDC is enabled.
  • CTRL+clicking on functions still take me to HardwareSerial.cpp file, so I was assuming it is used.
  • It would be helpful if ESP on-board USB could be used because it is more convenient than hooking up jumper wires for separate USB-serial adapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Peripheral: UART Resolution: Awaiting response Waiting for response of author Resolution: Unable to reproduce With given information issue is unable to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants