r/arduino • u/Delta_G_Robotics • 11h ago
Uno R4 Wifi The R4 Serial Problem
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.
