From 130f2c3bc86d2acb43ea5219ccc7db959b19d1d4 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 21 Feb 2023 20:35:57 +1100 Subject: [PATCH] AP_HAL_ChibiOS: add and use a "bool read(c)" method to AP_HAL this is much less likely to not work vs the int16_t equivalent --- libraries/AP_HAL_ChibiOS/UARTDriver.cpp | 17 +++++++++-------- libraries/AP_HAL_ChibiOS/UARTDriver.h | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/UARTDriver.cpp b/libraries/AP_HAL_ChibiOS/UARTDriver.cpp index 460b395b60..edbc6d6170 100644 --- a/libraries/AP_HAL_ChibiOS/UARTDriver.cpp +++ b/libraries/AP_HAL_ChibiOS/UARTDriver.cpp @@ -736,31 +736,32 @@ ssize_t UARTDriver::read(uint8_t *buffer, uint16_t count) return ret; } -int16_t UARTDriver::read() +bool UARTDriver::read(uint8_t &b) { if (_uart_owner_thd != chThdGetSelfX()) { - return -1; + return false; } - return UARTDriver::read_locked(0); + return UARTDriver::read_locked(0, b); } -int16_t UARTDriver::read_locked(uint32_t key) +bool UARTDriver::read_locked(uint32_t key, uint8_t &b) { if (lock_read_key != 0 && key != lock_read_key) { - return -1; + return false; } if (!_rx_initialised) { - return -1; + return false; } uint8_t byte; if (!_readbuf.read_byte(&byte)) { - return -1; + return false; } if (!_rts_is_active) { update_rts_line(); } - return byte; + b = byte; + return true; } /* write one byte to the port */ diff --git a/libraries/AP_HAL_ChibiOS/UARTDriver.h b/libraries/AP_HAL_ChibiOS/UARTDriver.h index 2d43dc4136..6a8a67d049 100644 --- a/libraries/AP_HAL_ChibiOS/UARTDriver.h +++ b/libraries/AP_HAL_ChibiOS/UARTDriver.h @@ -52,9 +52,9 @@ public: uint32_t available_locked(uint32_t key) override; uint32_t txspace() override; - int16_t read() override; + bool read(uint8_t &data) override WARN_IF_UNUSED; ssize_t read(uint8_t *buffer, uint16_t count) override; - int16_t read_locked(uint32_t key) override; + bool read_locked(uint32_t key, uint8_t &b) override WARN_IF_UNUSED; void _rx_timer_tick(void); void _tx_timer_tick(void);