Hi All!
This is my first post so please bear with me.
General Information
- Board: STM32 Nucleo-144 H743ZI2
- RTOS: Yes / Mbed OS 6
- Host OS: Ubuntu 20.04 LTS
- IDE: Mbed Studio
Problem
- Communication between host PC (Ubuntu) and H743ZI2
- Communication must sustain bi-directional high rates
USBSerial
class slows to a crawl very shortly after boot
Code Structure
Threads
- Main thread
- Read from serial and parse data
- Write to serial (run in
EventQueue
)
Snippets
USBSerial serial;
EventQueue write_q;
Thread write_th;
int main() {
serial.init();
serial.connect();
serial.wait_ready();
serial.puts("starting program\n");
write_th.start(callback(&write_q, &EventQueue::dispatch_forever));
write_q.call_every(8ms, task_write);
read_th.start(task_read);
while (true) {
// blink led and sleep
}
}
// reading thread; 250hz
if (!TakeSerialIn()) { // protect read in
return;
}
size_t n{0};
char c{'0'};
char buff[MAX_RCV_SIZE + 1]{};
while (c != '\n' && n < MAX_RCV_SIZE) {
if (serial.available()) {
c = (char)serial._getc();
buff[n] = c;
++n;
}
}
GiveSerialIn();
return;
// writing thread; 125Hz
string long_string = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzvabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
TakeSerialOut(); // protect write out
serial.puts(long_string);
GiveSerialOut();
Observations
- When I don’t start the reading thread, the writing thread is able to write at the expected rate
- When I start the reading thread but don’t read from
serial
, the writing thread will start fast but quickly slow to a crawl - When I start the reading thread, even if I do nothing in the
while
loop I still see the writing thread slow down significantly - I see the same problems with and without checking / locking / unlocking my mutexes
- I see the same problems running the write thread in an
EventQueue
and in a function withwhile (true)
- When I increase the priority of the writing thread, the data is sent at the expected rate
Please let me know if I can answer any questions or clarifications. Thank you!
– Updates –
I’m very new to embedded programming. I’m running 8 threads total, although I only mentioned the 3 relevant ones. The 8 tasks run between 2 and 250hz - is that potentially too much for this chip?
5 of those 8 are very simple “take this and write to UART” threads, if that makes a difference.