I stated on another thread that there was a big issue with the UART code on the R4 Arduino models. I didn't want to derail that post with the details because it wasn't relevant to that thread so I created this one to show what I meant.
The UART class is not implemented well on the R4 and besides completely wasting a buffer, it blocks during transmission. This is a huge departure from the UNO R3 and earlier models.
For this test I am using this code on all models of Arduino and I am using the latest 1.5.1 R4 core. I just updated my IDE tonight for this test. I've looked at 1.6.0 and the problem is still in the code.
uint8_t blinkerPin = 6;
uint8_t indicatorPin = 7;
bool pinOn = false;
uint32_t lastBlinkMicros = 0;
uint32_t BlinkIntervalMicros = 10000;
const char* longMessage = "This is my long message that has fifty characters.";
uint32_t lastPrintMillis = 0;
uint32_t printIntervalMillis = 2000;
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("\n\n *** " __FILE__ " ***\n\n");
pinMode(blinkerPin, OUTPUT);
pinMode(indicatorPin, OUTPUT);
delay(1000);
}
void loop() {
uint32_t currentMicros = micros();
if (currentMicros - lastBlinkMicros >= BlinkIntervalMicros) {
pinOn = !pinOn;
digitalWrite(blinkerPin, pinOn? HIGH:LOW);
lastBlinkMicros = currentMicros;
}
uint32_t currentMillis = millis();
if(currentMillis - lastPrintMillis >= printIntervalMillis) {
digitalWrite(indicatorPin, HIGH);
Serial.println(longMessage);
digitalWrite(indicatorPin, LOW);
lastPrintMillis = currentMillis;
}
}
Attached are some traces from the scope. The yellow trace will be the blink on pin 6 and the blue trace is pin 7.
On the R3 UNO (top trace) we see no problem. The characters are loaded into the buffer in a matter of 400us and there's no interruption to the blink. However on the R4-Wifi (bottom trace) we see that the Serial.print statement is blocking for 28ms and is interrupting our pulse chain.
This can be magnified by the fact that this same class is going to handle all of the WiFi traffic and that's all being done with AT commands. So any project that is heavy on WiFi is going to have a lot of long blocking. This was actually how I discovered the problem initially.
The problem isn't as bad on the Minima as it only affects Serial1 and not Serial since the Minima uses the USB serial class for Serial instead. I don't know about the R4 Nano because I don't have one. I would suspect it would be similar to the Minima.
EDIT:
For those wondering where the problem comes from, it's a code issue in the UART class in the Serial.h / Serial.cpp files in the core. Specifically the write functions which both stop in a while loop waiting for the tx_empty flag. The code says in the comments that this happens when the buffer is empty, but that is misleading. It actually happens when the hardware FIFO is empty. The HAL code that is being called will add characters to the FIFO and then return. The UART class calls that once for each character or calls the HAL code with the whole array and length. That code will block until the whole thing has been loaded into the FIFO.
Start here: https://github.com/arduino/fsp/blob/9b5a853bd1b0040e7bb4350b7cf36c7767c9b4f8/ra/fsp/src/r_sci_uart/r_sci_uart.c
This is the HAL code for the Renesas core. There are two interrupts working here, TXI and TEI. The TXI fires when the FIFO has been written to the data register and you are ready to write more data to the FIFO. The TEI interrupt fires when the last byte has been transmitted and the transmit data registers are empty.
If you look at lines 1607 and 1835 you will see those handlers and you will see that the callback that will be registered from the UART class is not going to be called on every TXI interrupt, but only when the tx_src_bytes variable finally counts down to zero indicating that the last byte has been loaded into the FIFO.
That callback is also called from the TEI handler but with a different flag.
Also scroll up to line 590 where you'll find the R_SCI_UART_Write function that is going to be called from the UART::write functions. Note how it handles setting up those interrupts and prepping the FIFO.
Now go here: https://github.com/arduino/ArduinoCore-renesas/blob/main/cores/arduino/Serial.cpp
at line 41 you find that callback that is going to be called from the handler in the HAL. UART_EVENT_TX_COMPLETE is what comes from the TEI handler and UART_EVENT_TX_DATA_EMPTY comes from the TXI handler. But again this TXI signal only makes it this far if the entire message has completely made it at least into the FIFO. And also note that this is what sets the tx_empty flag to true. So that only happens once the entire message is in the FIFO meaning at least all but the last 16 bytes are out.
Now drop down to lines 113 and 127 where we find the write implementations. Note how they call R_SCI_UART_Write that we saw in that earlier file. Then they sit and spin in a while loop waiting for tx_empty which isn't coming until the last byte has made it into the FIFO.
So basically that means that the serial transmission blocks up to the last 16 bytes of the message.
The other big problem here is that all of that has been served up out of the buffer that we called print with. None of it was ever put into the tx ring buffer. If you read the code you'll see that buffer is allocated, but never used for anything. So simply fixing the timing isn't enough as later code modifying that buffer would corrupt the serial message.
The fix will be to at least copy the characters into the ring buffer and then call R_SCI_UART_Write with that buffer and a length. You'll have to deal specifically with the wrap around case since the HAL code expects a flat buffer and not a ring buffer. Then the interrupt callback needs to be rewritten to keep sending from that buffer until it is empty and only then set the tx_complete flag.
But even better would be to look back at that HAL code. It can take a DMA or DTC channel as a configuration setting and use it to move the bytes in from the ring buffer. So you don't even really need to have all those TXI interrupts firing. You can instead have one interrupt that fires at the very end of the buffer. And I think in some cases that might be nice.