From 8678759da4b817e1534e1c1c186d3dc99c78115c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 11 Dec 2020 11:18:03 +1100 Subject: [PATCH] AP_HAL: added hal.serial() access to uarts this gives access to serial ports in the SERIALn_ order. It is inlined by the compiler so using hal.uartB and hal.serial(3) generates idential code on stm32 (tested on H7). This is a step towards eliminating hal.uartX completely and the horrible uartB ordering --- libraries/AP_HAL/HAL.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libraries/AP_HAL/HAL.h b/libraries/AP_HAL/HAL.h index ccc7eaeebc..ea77d6f1ca 100644 --- a/libraries/AP_HAL/HAL.h +++ b/libraries/AP_HAL/HAL.h @@ -101,6 +101,7 @@ public: virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0; + // the uartX ports must be contiguous in ram for the serial() method to work AP_HAL::UARTDriver* uartA; AP_HAL::UARTDriver* uartB; AP_HAL::UARTDriver* uartC; @@ -128,4 +129,15 @@ public: #else AP_HAL::CANIface** can; #endif + + // access to serial ports using SERIALn_ numbering + UARTDriver* serial(uint8_t sernum) const { + UARTDriver **uart_array = const_cast(&uartA); + // this mapping captures the historical use of uartB as SERIAL3 + const uint8_t mapping[] = { 0, 2, 3, 1, 4, 5, 6, 7, 8 }; + if (sernum >= ARRAY_SIZE(mapping)) { + return nullptr; + } + return uart_array[mapping[sernum]]; + } };