Serial.print() messed up! Why?

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
te-bachi
Posts: 2
Joined: Wed Apr 28, 2021 2:29 pm
Location: Zurich, Switzerland
Contact:

Serial.print() messed up! Why?

Post by te-bachi »

  • PlatformIO
  • Framework: Arduino_Core_STM32 1.9.0
  • Board: Nucleo-G071RB (+ Nucleo-G474RE)
  • Serial, Serial3
I'm new to the STM32 Arduino Core.
I would like the two boards (Nucleo-G071RB + Nucleo-G474RE) to communicate over UART (Serial3).
The G071 sends a request, the G474 replies with a response.
For debugging purposes, I use the standard UART (Serial => ST-Link) to print out some variables.
But the Debug Serial output on the computer (attached via USB to ST-Link) is messed up.

Expected output:

Code: Select all

read=123
read=79
read=75
read=33
read=171
read=77
read=125
<t1=16016 t2=16091 t3=16091 t4=16093 diff=77>
read=123
read=79
read=75
read=33
read=171
read=77
read=125
<t1=17017 t2=17092 t3=17092 t4=17094 diff=77>
Real Output:

Code: Select all

read=123
read=79
read=75
read=33
read=171
read=77
read=125
<t1=16016 t2=16091 t3=163
read=171
read=77
read=125
<t1=16016 t2=16091 t3=16091 t4=16093 diff=77>
re=77
read=125
<t1=16016 t2=16091 t3=16091 t4=16093 diff=77>
read=123
read=79
read=75
read=33
read=171
read=77
read=125
<t1=17017 t2=17092 t3=173
read=171
read=77
read=125
<t1=17017 t2=17092 t3=17092 t4=17094 diff=77>
re=77
read=125
<t1=17017 t2=17092 t3=17092 t4=17094 diff=77>
Is this a STM32 Arduino Core problem?
Serial resp. HardwareSerial uses a ring buffer and interrupts that should protect the concurrent access... right?

Request:

Code: Select all

<SOF>0014: 23.91<CRC1><CRC2><EOF>
123 48 48 49 52 58 32 50 51 46 57 49 113 31 125 
Response:

Code: Select all

<SOF>OK!<CRC1><CRC2><EOF>
123 79 75 33 171 77 125
platformio.ini

Code: Select all

[env:nucleo_g071rb]
platform = ststm32
board = nucleo_g071rb
framework = arduino
build_flags = 
	-DENABLE_HWSERIAL3
	-DPIN_SERIAL3_RX=PB9
	-DPIN_SERIAL3_TX=PB8
main.cpp:
(I know, using snprintf to fill an integer [up to 11 bytes] into a smaller buffer [5 bytes] could be dangerous resp. may be truncated)

Code: Select all

static int i = 0;
static uint32_t tOld = 0;
static uint32_t tNew = 0;
static uint32_t tDiff = 1000; // in ms
static byte rxbuf[512];

int t1;
int t2;
int t3;
int t4;
int diff;

void loop() {

    tNew = millis();

    if (tOld + tDiff < tNew) {
        String request;
        char chr[5];
        float temp;

        tOld = tNew;

        t1 = millis();
        temp = thermo1.temperature(RNOMINAL, RREF);
        t2 = millis();

        i++;
        snprintf(chr, sizeof(chr), "%04d", i);
        if (temp >= minTemp && temp <= maxTemp) {
            request = String(chr) + ": " + String(temp, 2);
        } else {
            request = String(chr) + ": " +"NaN";
        }

        frm_encode_message(&frameContext, reinterpret_cast<const byte *>(request.c_str()), request.length());
        t3 = millis();
        Serial3.flush();
    }
    
    int ch;
    while ((ch = Serial3.read()) != -1) {
        int n_dec;
        Serial.print("read=");
        Serial.println(ch);
        n_dec = frm_decode(&frameContext, rxbuf, sizeof(rxbuf), static_cast<byte>(ch));

        // received full frame
        if (n_dec > 0) {
            t4 = millis();
            diff = t4 - t1;

            Serial.print("<t1=");
            Serial.print(t1);
            Serial.print(" t2=");
            Serial.print(t2);
            Serial.print(" t3=");
            Serial.print(t3);
            Serial.print(" t4=");
            Serial.print(t4);
            Serial.print(" diff=");
            Serial.print(diff);
            Serial.println(">");
            Serial.flush();
            
        }
    }
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Serial.print() messed up! Why?

Post by fpiSTM »

That would be fine you test with 2.0.0 release as it includes several Cube update (G0/G4) and also Serial send by buffer instead of byte per byte.

You can also try to increase Rx/Tx buffer size:

Code: Select all

-DSERIAL_RX_BUFFER_SIZE=256 -DSERIAL_TX_BUFFER_SIZE=256
te-bachi
Posts: 2
Joined: Wed Apr 28, 2021 2:29 pm
Location: Zurich, Switzerland
Contact:

Re: Serial.print() messed up! Why?

Post by te-bachi »

I'm bound to the PlatformIO packages. I never tried it manually overwriting a version. I have to wait for an update from PlatformIO.

Increase Rx/Tx buffer size resolves (or shifts) the problem.
So maybe the HAL/LL and/or the ring buffer inside the HardwareSerial in 1.9.0 for G0/G4 is not working properly.

In any case, I can continue working without a messed up Serial...
Post Reply

Return to “General discussion”