Help with new servo driver

Hello,

I am trying to implement a pretty basic logic to communicate with some servos. So I created a new thread that iteratively calls the function bellow.

The logic:

  • write a message to the 1st device
  • wait for message to be written
  • write a message to the 2nd device
  • wait for the message to be written
  • read the responses
  • exit

The problem is that sometimes the hal.scheduler->delay_microseconds() do not do what they are intended and the situation shown in the picture occurs where simultaneous read/write corrupts the data (the device has one wire for read/write). I can solve the issue setting _uart->_uart->set_unbuffered_writes(true);. But then internal error 0x4000020 occurs.

Any ideas on how to fix the internal error or maybe a workaround? I am using Matek H743 with latest Arduplane FW and using Serial4 which has DMA enabled.

void servo::update()
{
    uint8_t n_bytes;

    if (!_init_done) {
        init();
        return;
    }

    // send 2 packets to servo 1
    send_pos_cmd(SERVO_ID_1, 2048);
    send_status_query(SERVO_ID_1);
    hal.scheduler->delay_microseconds(1500); // wait for reply (clear bus needed)
    
    // send 2 packets to servo 2
    send_pos_cmd(SERVO_ID_2, 2048);
    send_status_query(SERVO_ID_2);
    hal.scheduler->delay_microseconds(1500); // wait for reply (for next iteration's writes)

    // GET REPLIES
    n_bytes = _uart->available();
    if (n_bytes != 28) { // Normally 28 bytes expected
        _uart->discard_input();
    }
    else {
        _uart->read(_rx_buf, n_bytes);
    }
}