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>
130 lines
4.1 KiB
C++
130 lines
4.1 KiB
C++
/****************************************************************************
|
|
*
|
|
* Copyright (c) 2020 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 <board_config.h>
|
|
#include <px4_platform_common/bus_topology.h>
|
|
|
|
#if defined(CONFIG_I2C)
|
|
|
|
#define I2C_BUS_MAX_BUS_ITEMS PX4_NUMBER_I2C_BUSES
|
|
|
|
struct px4_i2c_bus_t {
|
|
int bus{-1}; ///< physical bus number (1, ...) (-1 means this is unused)
|
|
BusTopology topology{BusTopology::External};
|
|
};
|
|
|
|
__EXPORT extern const px4_i2c_bus_t px4_i2c_buses[I2C_BUS_MAX_BUS_ITEMS]; ///< board-specific I2C bus configuration
|
|
|
|
static inline constexpr px4_i2c_bus_t initI2CBusInternal(int bus)
|
|
{
|
|
px4_i2c_bus_t ret{};
|
|
ret.bus = bus;
|
|
ret.topology = BusTopology::Internal;
|
|
return ret;
|
|
}
|
|
|
|
static inline constexpr px4_i2c_bus_t initI2CBusExternal(int bus)
|
|
{
|
|
px4_i2c_bus_t ret{};
|
|
ret.bus = bus;
|
|
ret.topology = BusTopology::External;
|
|
return ret;
|
|
}
|
|
|
|
static inline constexpr px4_i2c_bus_t initI2CBusShared(int bus)
|
|
{
|
|
px4_i2c_bus_t ret{};
|
|
ret.bus = bus;
|
|
ret.topology = BusTopology::Shared;
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Bus topology for a physical I2C bus.
|
|
* Unknown bus numbers are External (a connector we have not listed).
|
|
* Boards may override this (BOARD_OVERRIDE_I2C_BUS_TOPOLOGY) for runtime HW variants.
|
|
*/
|
|
__EXPORT BusTopology px4_i2c_bus_topology(int bus);
|
|
|
|
static inline bool px4_i2c_bus_has_external(int bus)
|
|
{
|
|
return bus_topology_has_external(px4_i2c_bus_topology(bus));
|
|
}
|
|
|
|
static inline bool px4_i2c_bus_has_internal(int bus)
|
|
{
|
|
return bus_topology_has_internal(px4_i2c_bus_topology(bus));
|
|
}
|
|
|
|
/**
|
|
* @class I2CBusIterator
|
|
* Iterate over configured I2C buses by the board.
|
|
*
|
|
* InternalBus: Internal buses, plus a Shared bus when -b pins that bus.
|
|
* ExternalBus: External and Shared buses (probe for unknown devices).
|
|
*
|
|
* -b is the physical bus number (1, 2, ...), not an external-bus index.
|
|
* external() is the sensor classification implied by the filter (-I vs -X),
|
|
* not the bus topology.
|
|
*/
|
|
class I2CBusIterator
|
|
{
|
|
public:
|
|
enum class FilterType {
|
|
All, ///< specific or all buses
|
|
InternalBus, ///< onboard sensors: Internal buses, Shared if -b is given
|
|
ExternalBus, ///< connector sensors: External and Shared buses
|
|
};
|
|
|
|
I2CBusIterator(FilterType filter, int bus = -1)
|
|
: _filter(filter), _bus(bus) {}
|
|
|
|
bool next();
|
|
|
|
const px4_i2c_bus_t &bus() const { return px4_i2c_buses[_index]; }
|
|
|
|
int externalBusIndex() const { return _external_bus_counter; }
|
|
|
|
bool external() const { return _filter == FilterType::ExternalBus; }
|
|
|
|
private:
|
|
const FilterType _filter;
|
|
const int _bus;
|
|
int _index{-1};
|
|
int _external_bus_counter{0};
|
|
};
|
|
|
|
#endif // CONFIG_I2C
|