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

Corruption on Serial2 after changing baud rate #854

Closed
Edgs opened this issue Nov 20, 2017 · 11 comments
Closed

Corruption on Serial2 after changing baud rate #854

Edgs opened this issue Nov 20, 2017 · 11 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@Edgs
Copy link

Edgs commented Nov 20, 2017

Hardware:

Board: DOIT ESP32 DEVKIT V1
Core Installation/update date: 10/Nov/2017
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600

Description:

HardwareSerial device sends some incorrect characters after changing baudrate.

Serial2 is started at 115200 baud. Write works fine.
Serial2 has baud rate changed to 9600 with a second .begin()
Some .write() characters are then incorrect. It appears to work correctly if that first character is not sent at 115200.
If I send more than 4 characters in sequence like this, it seems to always be some characters at the beginning or end which are affected.

Character sequence should be:
[at 115200]
0x55
[at 9600]
0x01 0x02 0x03 0x04
0x01 0x02 0x03 0x04
etc...

Instead it is:
[at 115200]
0x55
[at 9600]
0xf3 0x03 0x04 0x91
0x02 0x03 0x04 0x45
0x4a 0x03 0x04 0x11
0x73 0x03 0x04 0x1e
etc...finally repeating:
0x02 0x03 0x04 0x01
etc...

Sketch:

#include <Arduino.h>
HardwareSerial SerialGPS(2);

void setup() {
  Serial.begin(115200); // program/debug port
  Serial.println("Starting...");

// start at 115200
  SerialGPS.begin(115200, SERIAL_8N1, 15, 5);
  SerialGPS.write(0x55);
  delay(1000);
  SerialGPS.end();            // < doesn't change anything if not present

// switch to 9600
  SerialGPS.begin(9600, SERIAL_8N1, 15, 5);
}

void loop() {
    SerialGPS.write(0x01);
    SerialGPS.write(0x02);
    SerialGPS.write(0x03);
    SerialGPS.write(0x04);
    delay(1000);
}
@stickbreaker
Copy link
Contributor

This is a recently discovered problem, The UART hardware flush signal on UART2 is cross connected into UART1, when it is activated, bad things happen. There is a conversation on IDF about this. The current work around is to manually drain the fifos by reading and NEVER activating uartFlush()

[TW#16347] Calling uart_flush(2) corrupts data thereafter

BUT currently Serial.begin() includes a call to uartFlush() in esp32-hal-uart.c

Chuck.

@jestebanes
Copy link

Hi, I try to solve this issue implementing a change_baud function into the esp32-hal-uart.c and calling from HardwareSerial.cpp

// esp32-hal-uart.c
void uartChangeBaudRate(uint8_t uart_nr, uint32_t baudrate)
{
if(uart_nr > 2) {
return; //Not return NULL
}
uart_t* uart = &_uart_bus_array[uart_nr];
uartSetBaudRate(uart, baudrate);
}

But with the latest library updates, it stopping to work. Maybe uartSetBaudRate now calls to flush.
If there is a solution in esp-idf, please translate to Arduino as soon as possible

Thanks

@Vorms
Copy link

Vorms commented Jan 5, 2018

Hello,
I am very concerned with issue.
Is that any workarround ?
I definitively need a patch !
Thanks for your help
Best regards
Thierry
vormsty@gmail.com

@jestebanes
Copy link

Hello @Vorms, I made a change in the implementation of uartFlush to avoid the hardware reset of the FIFO. For me, this change wasn't enough, because when I update the software via OTA the UART2 stops to work.
Finally, I change the UART2 for UART1 using the same external pins and that solve my issue, I hope in next hardware revisions of the chip, this trouble will be solved because is a shame that those little failures ruin such an incredible chip.

void uartFlush(uart_t* uart)
{
if(uart == NULL) {
return;
}

UART_MUTEX_LOCK();
while(uart->dev->status.txfifo_cnt && uart->dev->mem_cnt_status.tx_cnt);

// uart->dev->conf0.txfifo_rst = 1;
// uart->dev->conf0.txfifo_rst = 0;

// uart->dev->conf0.rxfifo_rst = 1;
// uart->dev->conf0.rxfifo_rst = 0;
uint8_t c;
while(uart->dev->status.rxfifo_cnt && uart->dev->mem_cnt_status.rx_cnt){
READ_PERI_REG(UART_FIFO_REG(uart->num));
}
while(uxQueueMessagesWaiting(uart->queue)){
xQueueReceive(uart->queue, &c, 0);
}

UART_MUTEX_UNLOCK();

}

@Vorms
Copy link

Vorms commented Jan 6, 2018 via email

@jestebanes
Copy link

Did you try to change the uartFlush function in esp32-hal-uart.c?

@9a4gl
Copy link

9a4gl commented Jan 15, 2018

Same problem here. Uploading data to Nextion display over UART2, in process Serial2.begin is called with different baudrates and UART2 it stops working :(

@everslick
Copy link
Contributor

I think there have been some fixes lately. Is this issue still relevant for latest master?

@nelfata
Copy link

nelfata commented May 30, 2018

Is there any resolution on this issue?

@stale
Copy link

stale bot commented Aug 1, 2019

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 Aug 1, 2019
@stale
Copy link

stale bot commented Aug 15, 2019

This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

7 participants