Skip to content

Commit

Permalink
pbio/uartdev: fix sync on city hub
Browse files Browse the repository at this point in the history
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: pybricks/support#747
  • Loading branch information
dlech committed Jun 1, 2023
1 parent ded2068 commit fad10c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/pbio/src/uartdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fad10c0

Please sign in to comment.