Hi,
Newbie here, so please forgive me if this is the wrong place to post.
I am using a Raspberry Pi Pico, RP2040 on Platform IO. I am using the default framework and board options. I realise that I am not using an Arduino, but the same platform and framework are used for the Arduino Nano RP2040 Connect so I hoped the documentation would be pertinent.
Basically I have a query regarding the behaviour of Serial.write() described in the documentation, " Serial transmission is asynchronous. If there is enough empty space in the transmit buffer, Serial.write()
will return before any characters are transmitted over serial." I have run some tests and this does not seem to be the case for the Pico with this setup. Serial.write() - Arduino Reference
My platformio.ini looks like this
[env:pico]
platform = raspberrypi
board = pico
framework = arduino
I am using Serial which is USB, Serial1 and Serial2. Serial1 worked out of the box, Serial 2 required instantiating as described in this post:
serial - How to use Serial2 on Raspberry Pi Pico - Arduino Stack Exchange
UART Serial2(4, 5, 0, 0);
I wrote some simple code to see if sending was buffered.
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
for (int i = 0; i < 10; i++) {
Serial1.print(micros()); // this will be about 9 characters soon after starting
Serial1.write(',');
}
Here Serial is class UART defined in Serial.h - wrapper over mbed RawSerial, Part of Arduino - http://www.arduino.cc/ - hence I am posting here.
For Serial1 on 9600 baud this gave,
10201203,10210573,10219947,10229323,10238698...
roughly 9000 microseconds for each 9 bytes, about 1 ms per byte, which is roughly correct for 9600 baud (about 960 bytes per second). Therefore if my test code is reasonable the serial.print() call is blocking.
Performing the same code for Serial (USB) @ 9600 baud gave:
20854472,20854640,20854806,20854941,20855214...
about 150 microseconds. This still seems like quite a bit of time for a 25MHz processor, but is way faster than 9600 baud.
I have looked at the code in:
.platformio\packages\framework-arduino-mbed\cores\arduino\api
to try to understand this.
The write() method is defined in Serial.cpp
size_t UART::write(uint8_t c) {
#if defined(SERIAL_CDC)
if (is_usb) {
return _SerialUSB.write(c);
}
#endif
while (!_serial->obj->writeable()) {}
int ret = _serial->obj->write(&c, 1);
return ret == -1 ? 0 : 1;
}
Even if I use a buffer and write one byte at a time in the the main loop in my main.cpp each byte written will block for 1ms (I am stuck with hardware on 9600 baud).
The way I see it I can
- Try to find a fix (or correction of my code)
- Try to get the project to work with the limitation of the blocking write
- Move to using /earlephilhower/arduino-pico (should work on platform IO)
- Give up on Platform IO and use the pico sdk www.raspberrypi.com/documentation/pico-sdk/
Can anyone let me know if this makes sense and what the solution might be?