Description
I work on a project with the RP2040 (Pico) where both Serial Ports are needed. The second HardwareSerial _UART2_
does not work correctly.
This simple example:
UART Serial2(8,9);
void setup() {
Serial.begin(115200);
Serial2.begin(9600);
delay(3000);
Serial.println("Start");
}
void loop() {
if (Serial2.available()) {
Serial.write(Serial2.read());
}
}
does not reach the Serial.println("Start");
line if there is Data incoming over the RX pin of the Serial2.
it initializes fine and the forwarding works if I disconnect the RX pin during the setup and then connect it after Start is printed. Then RX data works no problem.
to Test this I connected a second Pico to generate some Serial Data with this sketch:
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.println("abcdefghijklmnopqrstuvwxyz01234567890");
delay(250);
}
I was able to fix this bug by increasing the rx_buffer size in Serial.h. if I change it to RingBufferN<(1 << 12)> rx_buffer;
This points to a buffer overflow.
it seems to be a problem with the UART::on_rx() function. If the RX buffer is full no bytes are read from _serial->obj. this a guess causes the system to stall. I was able to fix this by just dropping serial data if the RX buffer is full. here is a working code:
void UART::on_rx() {
#if defined(SERIAL_CDC)
if (is_usb) {
return;
}
#endif
while(_serial->obj->readable()) {
char c;
core_util_critical_section_enter();
_serial->obj->read(&c, 1);
if(rx_buffer.availableForStore())
rx_buffer.store_char(c);
core_util_critical_section_exit();
}
}
Receiving from 2 Serials at the same time still seems to not work but at least it does not crash anymore