H743ZI2 Slow bi-directional USB communication

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

  1. Main thread
  2. Read from serial and parse data
  3. 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

  1. When I don’t start the reading thread, the writing thread is able to write at the expected rate
  2. When I start the reading thread but don’t read from serial, the writing thread will start fast but quickly slow to a crawl
  3. When I start the reading thread, even if I do nothing in the while loop I still see the writing thread slow down significantly
  4. I see the same problems with and without checking / locking / unlocking my mutexes
  5. I see the same problems running the write thread in an EventQueue and in a function with while (true)
  6. 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.

Have you tried assigning priorities to the threads?

I have used osPriorityRealtime to ensure that one thread would run as fast as possible.