From 252d6ccfd350e616c3353856c1390d3a7913e3ac Mon Sep 17 00:00:00 2001 From: "Dr.-Ing. Amilcar do Carmo Lucas" Date: Thu, 8 Jul 2021 15:25:00 +0200 Subject: [PATCH] AP_HAL_ChibiOS: get the last time when the tx write buffer got emptied --- libraries/AP_HAL_ChibiOS/UARTDriver.cpp | 5 +++++ libraries/AP_HAL_ChibiOS/UARTDriver.h | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/libraries/AP_HAL_ChibiOS/UARTDriver.cpp b/libraries/AP_HAL_ChibiOS/UARTDriver.cpp index a04537fdbddaf..9a247e5d82782 100644 --- a/libraries/AP_HAL_ChibiOS/UARTDriver.cpp +++ b/libraries/AP_HAL_ChibiOS/UARTDriver.cpp @@ -146,6 +146,11 @@ void UARTDriver::uart_thread() if (_tx_initialised && ((mask & EVT_TRANSMIT_DATA_READY) || need_tick || (hd_tx_active && (mask & EVT_TRANSMIT_END)))) { _tx_timer_tick(); } + + // record the time when a half-duplex TX buffer got emptied + if (_tx_initialised && hd_tx_active && (mask & EVT_TRANSMIT_END) && _writebuf.is_empty()) { + _last_write_completed_us = now; + } } } #pragma GCC diagnostic pop diff --git a/libraries/AP_HAL_ChibiOS/UARTDriver.h b/libraries/AP_HAL_ChibiOS/UARTDriver.h index 1a045fa9e7e81..e88e626b8300d 100644 --- a/libraries/AP_HAL_ChibiOS/UARTDriver.h +++ b/libraries/AP_HAL_ChibiOS/UARTDriver.h @@ -131,6 +131,13 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver { */ bool is_dma_enabled() const override { return rx_dma_enabled && tx_dma_enabled; } + // get the last time when the tx write buffer got emptied +#if CH_CFG_USE_EVENTS == TRUE + uint32_t get_last_tx_empty_us() const override { return ((tx_dma_enabled || half_duplex) && _writebuf.is_empty()) ? _last_write_completed_us : 0; } +#else + uint32_t get_last_tx_empty_us() const override { return (tx_dma_enabled && _writebuf.is_empty()) ? _last_write_completed_us : 0; } +#endif + private: const SerialDef &sdef; bool rx_dma_enabled;