i2c_spi_buses: add support for multiple instances of the same device on a bus

This also simplifies the API a bit, since we anyway have to change the
drivers to pass additional information (the bus device index).

The orientation flag is merged with the rotation.
This commit is contained in:
Beat Küng
2021-04-06 17:36:33 +02:00
committed by Daniel Agar
parent e4983ab88c
commit c5aef9d512
19 changed files with 252 additions and 60 deletions
@@ -73,16 +73,22 @@ static inline constexpr px4_spi_bus_t initSPIBus(SPI::Bus bus, const px4_spi_bus
for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) {
ret.devices[i] = devices.devices[i];
// check that the same device is configured only once (the chip-select code depends on that)
for (int j = i + 1; j < SPI_BUS_MAX_DEVICES; ++j) {
if (ret.devices[j].cs_gpio != 0) {
constexpr_assert(ret.devices[i].devid != ret.devices[j].devid, "Same device configured multiple times");
}
}
if (ret.devices[i].cs_gpio != 0) {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
if (PX4_SPI_DEVICE_ID != PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
if (PX4_SPI_DEVICE_ID == PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
int same_devices_count = 0;
for (int j = 0; j < i; ++j) {
if (ret.devices[j].cs_gpio != 0) {
same_devices_count += (ret.devices[i].devid & 0xff) == (ret.devices[j].devid & 0xff);
}
}
// increment the 2. LSB byte to allow multiple devices of the same type
ret.devices[i].devid |= same_devices_count << 8;
} else {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
ret.requires_locking = true;
}
}
@@ -67,16 +67,22 @@ static inline constexpr px4_spi_bus_t initSPIBus(SPI::Bus bus, const px4_spi_bus
for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) {
ret.devices[i] = devices.devices[i];
// check that the same device is configured only once (the chip-select code depends on that)
for (int j = i + 1; j < SPI_BUS_MAX_DEVICES; ++j) {
if (ret.devices[j].cs_gpio != 0) {
constexpr_assert(ret.devices[i].devid != ret.devices[j].devid, "Same device configured multiple times");
}
}
if (ret.devices[i].cs_gpio != 0) {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
if (PX4_SPI_DEVICE_ID != PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
if (PX4_SPI_DEVICE_ID == PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
int same_devices_count = 0;
for (int j = 0; j < i; ++j) {
if (ret.devices[j].cs_gpio != 0) {
same_devices_count += (ret.devices[i].devid & 0xff) == (ret.devices[j].devid & 0xff);
}
}
// increment the 2. LSB byte to allow multiple devices of the same type
ret.devices[i].devid |= same_devices_count << 8;
} else {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
ret.requires_locking = true;
}
}
@@ -69,16 +69,22 @@ static inline constexpr px4_spi_bus_t initSPIBus(SPI::Bus bus, const px4_spi_bus
for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) {
ret.devices[i] = devices.devices[i];
// check that the same device is configured only once (the chip-select code depends on that)
for (int j = i + 1; j < SPI_BUS_MAX_DEVICES; ++j) {
if (ret.devices[j].cs_gpio != 0) {
constexpr_assert(ret.devices[i].devid != ret.devices[j].devid, "Same device configured multiple times");
}
}
if (ret.devices[i].cs_gpio != 0) {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
if (PX4_SPI_DEVICE_ID != PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
if (PX4_SPI_DEVICE_ID == PX4_SPIDEVID_TYPE(ret.devices[i].devid)) {
int same_devices_count = 0;
for (int j = 0; j < i; ++j) {
if (ret.devices[j].cs_gpio != 0) {
same_devices_count += (ret.devices[i].devid & 0xff) == (ret.devices[j].devid & 0xff);
}
}
// increment the 2. LSB byte to allow multiple devices of the same type
ret.devices[i].devid |= same_devices_count << 8;
} else {
// A bus potentially requires locking if it is accessed by non-PX4 devices (i.e. NuttX drivers)
ret.requires_locking = true;
}
}