From fad10c0a6d1ccd18da82f704cf0b643fa21bf7ab Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 1 Jun 2023 11:47:37 -0500 Subject: [PATCH] pbio/uartdev: fix sync on city hub The flush needs to be immediately before the read, otherwise the read buffer can contain garbage that was read while sending the baud rate. Also, the ACK usually comes in < 1 ms so we don't need to wait so long. This improves the chances of not missing data if the ACK doesn't come, e.g. on the BOOST Color Distance sensor. Fixes: https://github.com/pybricks/support/issues/747 --- CHANGELOG.md | 2 ++ lib/pbio/src/uartdev.c | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2b1cf628..08c08ab46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,10 @@ ### Fixed - Fixed BLE broadcast not working on City hub. - Fixed crash on BTStack hubs when program stopped during call to `ble.broadcast()`. +- Fixed delayed sensor sync on boot on City hub ([support#747]). [support#402]: https://github.com/pybricks/support/issues/402 +[support#747]: https://github.com/pybricks/support/issues/747 ## [3.3.0b5] - 2023-05-16 diff --git a/lib/pbio/src/uartdev.c b/lib/pbio/src/uartdev.c index 99d70b4a2..65922b47c 100644 --- a/lib/pbio/src/uartdev.c +++ b/lib/pbio/src/uartdev.c @@ -756,15 +756,15 @@ static PT_THREAD(pbio_uartdev_update(uartdev_port_data_t * data)) { // block until pbio_uartdev_ready() is called PT_WAIT_UNTIL(&data->pt, data->status == PBIO_UARTDEV_STATUS_SYNCING); - pbdrv_uart_flush(data->uart); - // Send SPEED command at 115200 baud pbdrv_uart_set_baud_rate(data->uart, EV3_UART_SPEED_LPF2); debug_pr("set baud: %d\n", EV3_UART_SPEED_LPF2); PT_SPAWN(&data->pt, &data->speed_pt, pbio_uartdev_send_speed_msg(data, EV3_UART_SPEED_LPF2)); + pbdrv_uart_flush(data->uart); + // read one byte to check for ACK - PBIO_PT_WAIT_READY(&data->pt, err = pbdrv_uart_read_begin(data->uart, data->rx_msg, 1, 100)); + PBIO_PT_WAIT_READY(&data->pt, err = pbdrv_uart_read_begin(data->uart, data->rx_msg, 1, 10)); if (err != PBIO_SUCCESS) { DBG_ERR(data->last_err = "UART Rx error during baud"); goto err;