Skip to content

Serial end() call create an unexpected available byte on the next begin-read calls (arduino-esp32 2.0.14) #11648

@red-bar

Description

@red-bar

Board

ESP32S3 Dev Module

Device Description

ESP32-S3-DevKitC-1

Hardware Configuration

Serial1 used on pins 13 (Rx), 14 (Tx) in loopback mode (pin 14 connected to pin 13). Other pins choices (1-2, 4-5, 17-18 tested) give the same behaviour.

Version

v2.0.14

IDE Name

Arduino IDE

Operating System

Linux Mint 21

Flash frequency

80MHz

PSRAM enabled

yes

Upload speed

115200

Description

If you call Serial end() after a first begin() and read(), and then call begin() (same parameters) and read() again, an unexpected byte is available even though nothing was received. The problem does not occur when you don't call end() between two begin()-read(). So to solve this issues, DON'T CALL end() BETWEEN 2 begin() with the same uart config.

Sketch

#define U1TXPIN 14 //1 5 17
#define U1RXPIN 13 //2 4 18

void setup() {

 char sendBuffer[2]; char receiveBuffer[3];

 Serial.begin(115200);

 for(;;){
  Serial1.begin(9600, SERIAL_8N1, U1RXPIN, U1TXPIN, false, 5000); // open (1st run) or reopen (infinite loop) Serial1 (same config)
  delay(10);
  sendBuffer[0]=1; sendBuffer[1]=2; // char 1,2 to send
  Serial1.write(sendBuffer, 2); // send 1,2
  Serial1.flush();
  delay(100);
  receiveBuffer[0] = Serial1.read(); // read 1 -> OK
  receiveBuffer[1] = Serial1.read(); // read 2 -> OK
  Serial.printf("%02x %02x\n", receiveBuffer[0], receiveBuffer[1]); // print 01 02 to console -> OK
  Serial1.end(); // end() called, CREATE A BUG, see the next begin-write-read code below

  Serial1.begin(9600, SERIAL_8N1, U1RXPIN, U1TXPIN, false, 5000); // reopen Serial1 (same config)
  delay(10);
  sendBuffer[0]=3; sendBuffer[1]=4; // char 3,4 to send
  Serial1.write(sendBuffer, 2); // send 3,4
  Serial1.flush();
  delay(100);
  // 3 success read even if only 2 char were sent
  receiveBuffer[0] = Serial1.read(); // succes read, unexpected char (sometime 0xf8 or 0xfc for example), even if we hadn't sent anything before
  receiveBuffer[1] = Serial1.read(); // read 3, 1st char sent before
  receiveBuffer[2] = Serial1.read(); // read 4, 2nd char sent before
  Serial.printf("%02x %02x %02x\n", receiveBuffer[0], receiveBuffer[1], receiveBuffer[2]); // print 0xfc 03 04
  // end() NOT CALLED HERE, NO BUG, the next begin-write-read works
  delay(3000);
 }
}

void loop() {
}

Debug Message

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
[   106][I][esp32-hal-psram.c:96] psramInit(): PSRAM enabled
01 02
f8 03 04 /* wait 3 secondes */
01 02
f8 03 04 /// wait 3 secondes
01 02
f8 03 04 /// wait 3 secondes
01 02
f8 03 04
///...

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions