- PlatformIO
- Framework: Arduino_Core_STM32 1.9.0
- Board: Nucleo-G071RB (+ Nucleo-G474RE)
- Serial, Serial3
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>
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>
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
Code: Select all
<SOF>OK!<CRC1><CRC2><EOF>
123 79 75 33 171 77 125
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
(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();
}
}
}