mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-22 14:19:57 +08:00
* feat(sensors): decouple sensor internal/external classification from the bus
A sensor's internal/external classification was derived from its bus
(px4_i2c_bus_external / px4_spi_bus_external), but a bus is a wire: one bus
routinely serves both chips soldered on the FC and a pinned-out connector, and
any onboard sensor on such a shared bus was classified external. That hands an
onboard mag an operator-settable rotation instead of the board rotation, feeds
an onboard baro's self-heated die temperature into air density as if it were
ambient, and inverts the 75/50 default priority. FMU-v6C and AirBrainH743
worked around it with hand-rolled device_id whitelists behind
BOARD_OVERRIDE_I2C_DEVICE_EXTERNAL; FMU-v6XRT's second onboard baro was simply
misclassified.
Classify the device instead of the bus: drivers publish is_external in
sensor_accel/gyro/mag/baro, derived from the device id by default and
overridden by the new -O start flag ("onboard") for onboard sensors that share
a bus with an external connector. -I/-X stay pure bus probe filters.
The question "is this sensor external" had four answers. It now has one, with
a single override point:
-O -> I2CSPIDriverConfig::external -> Device::set_external() and the
PX4* wrappers -> is_external in the sensor topic
px4_i2c_device_external() was px4_i2c_bus_external() with a device id decode in
front, and calibration::DeviceExternal() forwarded to device_is_external();
both are gone. Device::external() stops being a bus query: it is non-virtual,
defaults to device_is_external(), and takes the declared value through
set_external(), so the five I2C/SPI overrides that used to answer it from the
bus are deleted and cannot diverge again. device::I2C and device::SPI set it
from the config, so every driver built on the bus framework follows -O without
doing anything. px4_i2c_bus_external() and px4_spi_bus_external() survive as
what they honestly are - bus predicates - reachable only through the fallback.
-O is opt-in per driver (BusCLIArguments::support_onboard), the same way -k is:
a flag that every driver advertised but only a handful honoured would be a
silent no-op on the rest. This also drops the special case for the mcp23009 and
mcp23017 GPIO expanders, which use -O for their output state and simply do not
opt in.
sensor_gyro_fifo carries is_external too. VehicleAngularVelocity prefers the
FIFO topic for any IMU that publishes one, so without it the rate controller
would take its rotation from the bus while VehicleIMU took it from the topic -
the same chip, two classifications.
The BOARD_OVERRIDE_I2C_DEVICE_EXTERNAL hook is removed along with both board
implementations, replaced by -O on the affected rc.board_sensors lines.
FMU-v5x, MR-CANHUBK3, KakuteF7, NXT-Dual and MicoAir H743-Lite each start an
onboard barometer on an external bus and get the flag as well.
Assisted-by: Claude:claude-fable-5, Claude:claude-opus-5[1m]
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
* feat(sensors): treat shared buses as first-class topology
A boolean internal/external flag cannot describe a bus that carries both
hard-mounted chips and a connector. -O was an opt-in override for that
case, so most drivers silently ignored it and classification still
followed the bus.
Declare Internal / External / Shared on the bus, probe External and
Shared, and classify each sensor from -I/-s vs -X/-S.
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
* fix(ci): drop stale ITCM symbols and clang-tidy errors
ITCM lists still named calibration::DeviceExternal and
px4_spi_bus_external after both were removed. The host test stubs
forwarded varargs in a way the analyzer rejected, and stripped I2C
headers kept extra trailing newlines.
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
* fix(sensors): inline device_is_external into the header
Reaching it through drivers__device pulled the kernel-only library into
the userspace image of a protected build; giving it a library of its own
put an archive referencing px4_i2c_buses/px4_spi_buses after the board
library that defines them. Inlining sidesteps both.
* fix(sensors): compile tcbp001ta and probe canhubk3 GPS mag
tcbp001ta is not an I2CSPIDriver, so config.external does not exist; it
only ever starts on internal SPI. -X on canhubk3 I2C2 never ran because
that bus is Internal.
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
---------
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
199 lines
7.3 KiB
C++
199 lines
7.3 KiB
C++
/****************************************************************************
|
|
*
|
|
* Copyright (c) 2020-2022 PX4 Development Team. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include <board_config.h>
|
|
#include <px4_platform_common/bus_topology.h>
|
|
|
|
#if defined(CONFIG_SPI)
|
|
|
|
/*
|
|
* Helper macros to handle device ID's. They are used to match drivers against SPI buses and chip-select signals.
|
|
* They match with corresponding definitions in NuttX.
|
|
* 'type' is typically PX4_SPI_DEVICE_ID for PX4 drivers, and 'index' is used for the driver (DRV_*) or chip-select index.
|
|
*/
|
|
#define PX4_SPIDEV_ID(type, index) ((((type) & 0xffff) << 16) | ((index) & 0xffff))
|
|
#define PX4_SPI_DEVICE_ID (1 << 12)
|
|
#define PX4_SPI_DEV_ID(devid) ((devid) & 0xffff)
|
|
#define PX4_SPIDEVID_TYPE(devid) (((uint32_t)(devid) >> 16) & 0xffff)
|
|
|
|
typedef uint32_t spi_drdy_gpio_t;
|
|
|
|
#define SPI_BUS_MAX_DEVICES 6
|
|
struct px4_spi_bus_device_t {
|
|
uint32_t cs_gpio; ///< chip-select GPIO (0 if this device is not used)
|
|
spi_drdy_gpio_t drdy_gpio; ///< data ready GPIO (0 if not set)
|
|
uint32_t devid; ///< SPIDEV_ID(type,index). For PX4 devices on NuttX: index is the device type, and for external buses the CS index
|
|
uint16_t devtype_driver; ///< driver device type, e.g. DRV_IMU_DEVTYPE_ICM20689 (on NuttX: PX4_SPI_DEV_ID(devid) == devtype_driver)
|
|
bool external; ///< true if this CS is a connector pin, not an onboard chip
|
|
};
|
|
|
|
struct px4_spi_bus_devices_t {
|
|
px4_spi_bus_device_t devices[SPI_BUS_MAX_DEVICES];
|
|
};
|
|
|
|
struct px4_spi_bus_t {
|
|
px4_spi_bus_device_t devices[SPI_BUS_MAX_DEVICES];
|
|
uint32_t power_enable_gpio{0}; ///< GPIO (if non-zero) to control the power of the attached devices on this bus (0 means power is off)
|
|
int8_t bus{-1}; ///< physical bus number (1, ...) (-1 means this is unused)
|
|
BusTopology topology{BusTopology::Internal};
|
|
bool requires_locking; ///< whether the bus should be locked during transfers (true if NuttX drivers access the bus)
|
|
};
|
|
|
|
|
|
struct px4_spi_bus_all_hw_t {
|
|
px4_spi_bus_t buses[SPI_BUS_MAX_BUS_ITEMS];
|
|
#if defined(BOARD_HAS_HW_SPLIT_VERSIONING)
|
|
hw_fmun_id_t board_hw_fmun_id {USHRT_MAX};
|
|
#else
|
|
int board_hw_version_revision {-1}; ///< 0=default, >0 for a specific revision (see board_get_hw_version & board_get_hw_revision), -1=unused
|
|
#endif
|
|
};
|
|
|
|
#if BOARD_NUM_SPI_CFG_HW_VERSIONS > 1
|
|
/**
|
|
* initialze px4_spi_buses from px4_spi_buses_all_hw and the hardware version.
|
|
* Call this on early boot before anything else accesses px4_spi_buses (e.g. from stm32_spiinitialize).
|
|
*/
|
|
__EXPORT void px4_set_spi_buses_from_hw_version();
|
|
|
|
__EXPORT extern const px4_spi_bus_all_hw_t
|
|
px4_spi_buses_all_hw[BOARD_NUM_SPI_CFG_HW_VERSIONS]; ///< board-specific SPI bus configuration all hw versions
|
|
|
|
__EXPORT extern const px4_spi_bus_t *px4_spi_buses; ///< board-specific SPI bus configuration for current board revision
|
|
#else
|
|
static inline void px4_set_spi_buses_from_hw_version() {}
|
|
__EXPORT extern const px4_spi_bus_t px4_spi_buses[SPI_BUS_MAX_BUS_ITEMS]; ///< board-specific SPI bus configuration
|
|
#endif
|
|
|
|
|
|
/**
|
|
* Find a SPI bus given a device ID. Note: only internal buses are checked.
|
|
* @return the bus or -1
|
|
*/
|
|
__EXPORT int px4_find_spi_bus(uint32_t devid);
|
|
|
|
/**
|
|
* Check if a bus requires locking during a SPI transfer (because it is potentially accessed by different threads)
|
|
*/
|
|
__EXPORT bool px4_spi_bus_requires_locking(int bus);
|
|
|
|
/**
|
|
* Topology of a configured SPI bus.
|
|
*/
|
|
static inline BusTopology px4_spi_bus_topology(const px4_spi_bus_t &bus)
|
|
{
|
|
return bus.topology;
|
|
}
|
|
|
|
static inline BusTopology px4_spi_bus_topology(int bus)
|
|
{
|
|
for (int i = 0; i < SPI_BUS_MAX_BUS_ITEMS; ++i) {
|
|
if (px4_spi_buses[i].bus == bus) {
|
|
return px4_spi_buses[i].topology;
|
|
}
|
|
}
|
|
|
|
return BusTopology::External;
|
|
}
|
|
|
|
static inline bool px4_spi_bus_has_external(const px4_spi_bus_t &bus)
|
|
{
|
|
return bus_topology_has_external(bus.topology);
|
|
}
|
|
|
|
static inline bool px4_spi_bus_has_external(int bus)
|
|
{
|
|
return bus_topology_has_external(px4_spi_bus_topology(bus));
|
|
}
|
|
|
|
|
|
/**
|
|
* @class SPIBusIterator
|
|
* Iterate over configured SPI buses by the board.
|
|
*
|
|
* InternalBus: onboard CS (Internal buses, Shared if -b is given).
|
|
* ExternalBus: connector CS on External and Shared buses (n-th such bus, CS index from 1).
|
|
*
|
|
* external() is the sensor classification implied by the filter (-s vs -S),
|
|
* not the bus topology.
|
|
*/
|
|
class SPIBusIterator
|
|
{
|
|
public:
|
|
enum class FilterType {
|
|
InternalBus, ///< onboard sensors: Internal buses, Shared if -b is given
|
|
ExternalBus, ///< connector sensors: External and Shared buses
|
|
};
|
|
|
|
/**
|
|
* Constructor
|
|
* Note: only for devices of type PX4_SPI_DEVICE_ID
|
|
* @param filter
|
|
* @param devid_driver_index DRV_*
|
|
* @param chipselect pin of SPIInternal (-1=all) or chip-select index of SPIExternal starting from 1 (optional)
|
|
* @param bus starts with 1 (-1=all, but only for internal). Numbering for internal is arch-specific, for external
|
|
* it is the n-th external-capable bus (External or Shared).
|
|
*/
|
|
SPIBusIterator(FilterType filter, uint16_t devid_driver_index, int16_t chipselect = -1, int bus = -1)
|
|
: _filter(filter), _devid_driver_index(devid_driver_index),
|
|
_chipselect(chipselect),
|
|
_bus(filter == FilterType::ExternalBus && bus == -1 ? 1 : bus) {}
|
|
|
|
bool next();
|
|
|
|
const px4_spi_bus_t &bus() const { return px4_spi_buses[_index]; }
|
|
spi_drdy_gpio_t DRDYGPIO() const { return px4_spi_buses[_index].devices[_bus_device_index].drdy_gpio; }
|
|
|
|
uint32_t devid() const { return px4_spi_buses[_index].devices[_bus_device_index].devid; }
|
|
|
|
int externalBusIndex() const { return _external_bus_counter; }
|
|
|
|
bool external() const { return _filter == FilterType::ExternalBus; }
|
|
|
|
int busDeviceIndex() const { return _bus_device_index; }
|
|
|
|
private:
|
|
const FilterType _filter;
|
|
const uint16_t _devid_driver_index;
|
|
const int16_t _chipselect;
|
|
const int _bus;
|
|
int _index{0};
|
|
int _external_bus_counter{1};
|
|
int _bus_device_index{-1};
|
|
};
|
|
|
|
#endif // CONFIG_SPI
|