Skip to content

Commit

Permalink
AP_HAL: make uart pointers private
Browse files Browse the repository at this point in the history
this ensures they cannot be used by library or vehicle code, so we
will be able to remove them in future
  • Loading branch information
tridge committed Dec 14, 2020
1 parent 195ec03 commit 19723e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
13 changes: 13 additions & 0 deletions libraries/AP_HAL/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ HAL::FunCallbacks::FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void))
}

}

// access serial ports using SERIALn numbering
AP_HAL::UARTDriver* AP_HAL::HAL::serial(uint8_t sernum) const
{
UARTDriver **uart_array = const_cast<UARTDriver**>(&uartA);
// this mapping captures the historical use of uartB as SERIAL3
const uint8_t mapping[] = { 0, 2, 3, 1, 4, 5, 6, 7, 8 };
static_assert(sizeof(mapping) == num_serial, "num_serial must match mapping");
if (sernum >= num_serial) {
return nullptr;
}
return uart_array[mapping[sernum]];
}
15 changes: 6 additions & 9 deletions libraries/AP_HAL/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class AP_HAL::HAL {

virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0;

private:
// the uartX ports must be contiguous in ram for the serial() method to work
AP_HAL::UARTDriver* uartA;
AP_HAL::UARTDriver* uartB;
Expand All @@ -111,6 +112,8 @@ class AP_HAL::HAL {
AP_HAL::UARTDriver* uartG;
AP_HAL::UARTDriver* uartH;
AP_HAL::UARTDriver* uartI;

public:
AP_HAL::I2CDeviceManager* i2c_mgr;
AP_HAL::SPIDeviceManager* spi;
AP_HAL::AnalogIn* analogin;
Expand All @@ -131,13 +134,7 @@ class AP_HAL::HAL {
#endif

// access to serial ports using SERIALn_ numbering
UARTDriver* serial(uint8_t sernum) const {
UARTDriver **uart_array = const_cast<UARTDriver**>(&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]];
}
UARTDriver* serial(uint8_t sernum) const;

static constexpr uint8_t num_serial = 9;
};

0 comments on commit 19723e6

Please sign in to comment.