mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-27 03:23:33 +08:00
i2c_spi_buses: respect CONFIG_I2C and CONFIG_SPI
- bmp280, dps310, and ms5611 barometers support boards without I2C
This commit is contained in:
@@ -32,10 +32,11 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include <board_config.h>
|
||||
#ifndef BOARD_DISABLE_I2C_SPI
|
||||
|
||||
#include <px4_platform_common/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#ifndef BOARD_OVERRIDE_I2C_BUS_EXTERNAL
|
||||
bool px4_i2c_bus_external(const px4_i2c_bus_t &bus)
|
||||
{
|
||||
@@ -85,4 +86,4 @@ bool I2CBusIterator::next()
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif /* BOARD_DISABLE_I2C_SPI */
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include <board_config.h>
|
||||
#ifndef BOARD_DISABLE_I2C_SPI
|
||||
|
||||
#ifndef MODULE_NAME
|
||||
#define MODULE_NAME "SPI_I2C"
|
||||
@@ -57,11 +56,15 @@ I2CSPIDriverConfig::I2CSPIDriverConfig(const BusCLIArguments &cli, const BusInst
|
||||
bus_option(iterator.configuredBusOption()),
|
||||
bus_type(iterator.busType()),
|
||||
bus(iterator.bus()),
|
||||
#if defined(CONFIG_I2C)
|
||||
i2c_address(cli.i2c_address),
|
||||
#endif // CONFIG_I2C
|
||||
bus_frequency(cli.bus_frequency),
|
||||
#if defined(CONFIG_SPI)
|
||||
drdy_gpio(iterator.DRDYGPIO()),
|
||||
spi_mode(cli.spi_mode),
|
||||
spi_devid(iterator.devid()),
|
||||
#endif // CONFIG_SPI
|
||||
bus_device_index(iterator.busDeviceIndex()),
|
||||
rotation(cli.rotation),
|
||||
quiet_start(cli.quiet_start),
|
||||
@@ -93,6 +96,8 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
|
||||
char *p = (char *)&_options;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (_i2c_support) {
|
||||
*(p++) = 'X'; // external
|
||||
*(p++) = 'I'; // internal
|
||||
@@ -102,6 +107,9 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (_spi_support) {
|
||||
*(p++) = 'S'; // external
|
||||
*(p++) = 's'; // internal
|
||||
@@ -109,6 +117,8 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
*(p++) = 'm'; *(p++) = ':'; // spi mode
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
if (support_keep_running) {
|
||||
*(p++) = 'k';
|
||||
}
|
||||
@@ -148,6 +158,8 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
|
||||
while ((ch = px4_getopt(argc, argv, _options, &_optind, &_optarg)) != EOF) {
|
||||
switch (ch) {
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
case 'X':
|
||||
bus_option = I2CSPIBusOption::I2CExternal;
|
||||
break;
|
||||
@@ -163,6 +175,8 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
|
||||
i2c_address = (int)strtol(_optarg, nullptr, 0);
|
||||
break;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
case 'S':
|
||||
bus_option = I2CSPIBusOption::SPIExternal;
|
||||
@@ -175,6 +189,7 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
case 'c':
|
||||
chipselect_index = atoi(_optarg);
|
||||
break;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
case 'b':
|
||||
requested_bus = atoi(_optarg);
|
||||
@@ -183,10 +198,12 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
case 'f':
|
||||
bus_frequency = 1000 * atoi(_optarg);
|
||||
break;
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
case 'm':
|
||||
spi_mode = (spi_mode_e)atoi(_optarg);
|
||||
break;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
case 'q':
|
||||
quiet_start = true;
|
||||
@@ -216,12 +233,21 @@ int BusCLIArguments::getOpt(int argc, char *argv[], const char *options)
|
||||
|
||||
// apply defaults if not provided
|
||||
if (bus_frequency == 0) {
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (bus_option == I2CSPIBusOption::I2CExternal || bus_option == I2CSPIBusOption::I2CInternal) {
|
||||
bus_frequency = default_i2c_frequency;
|
||||
|
||||
} else if (bus_option == I2CSPIBusOption::SPIExternal || bus_option == I2CSPIBusOption::SPIInternal) {
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (bus_option == I2CSPIBusOption::SPIExternal || bus_option == I2CSPIBusOption::SPIInternal) {
|
||||
bus_frequency = default_spi_frequency;
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,16 +258,22 @@ bool BusCLIArguments::validateConfiguration()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (_i2c_support && default_i2c_frequency == -1) {
|
||||
PX4_ERR("Bug: driver %s does not set default_i2c_frequency", px4_get_taskname());
|
||||
success = false;
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (_spi_support && default_spi_frequency == -1) {
|
||||
PX4_ERR("Bug: driver %s does not set default_spi_frequency", px4_get_taskname());
|
||||
success = false;
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -249,11 +281,17 @@ bool BusCLIArguments::validateConfiguration()
|
||||
BusInstanceIterator::BusInstanceIterator(const char *module_name,
|
||||
const BusCLIArguments &cli_arguments, uint16_t devid_driver_index)
|
||||
: _module_name(module_name), _bus_option(cli_arguments.bus_option), _devid_driver_index(devid_driver_index),
|
||||
#if defined(CONFIG_I2C)
|
||||
_i2c_address(cli_arguments.i2c_address),
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
_spi_bus_iterator(spiFilter(cli_arguments.bus_option),
|
||||
cli_arguments.bus_option == I2CSPIBusOption::SPIExternal ? cli_arguments.chipselect_index : devid_driver_index,
|
||||
cli_arguments.requested_bus),
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
_i2c_bus_iterator(i2cFilter(cli_arguments.bus_option), cli_arguments.requested_bus),
|
||||
#endif // CONFIG_I2C
|
||||
_current_instance(i2c_spi_module_instances.end())
|
||||
{
|
||||
// We lock the module instance list as long as this object is alive, since we iterate over the list.
|
||||
@@ -290,20 +328,31 @@ bool BusInstanceIterator::next()
|
||||
|
||||
return false;
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
if (_spi_bus_iterator.next()) {
|
||||
bus = _spi_bus_iterator.bus().bus;
|
||||
}
|
||||
|
||||
} else {
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
} else if (busType() == BOARD_I2C_BUS) {
|
||||
if (_i2c_bus_iterator.next()) {
|
||||
bus = _i2c_bus_iterator.bus().bus;
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
}
|
||||
|
||||
if (bus != -1) {
|
||||
// find matching runtime instance
|
||||
#if defined(CONFIG_I2C)
|
||||
bool is_i2c = busType() == BOARD_I2C_BUS;
|
||||
#else
|
||||
bool is_i2c = false;
|
||||
#endif
|
||||
|
||||
for (_current_instance = i2c_spi_module_instances.begin(); _current_instance != i2c_spi_module_instances.end();
|
||||
++_current_instance) {
|
||||
@@ -314,7 +363,12 @@ bool BusInstanceIterator::next()
|
||||
if (_bus_option == (*_current_instance)->_bus_option && bus == (*_current_instance)->_bus &&
|
||||
_devid_driver_index == (*_current_instance)->_devid_driver_index &&
|
||||
busDeviceIndex() == (*_current_instance)->_bus_device_index &&
|
||||
(!is_i2c || _i2c_address == (*_current_instance)->_i2c_address)) {
|
||||
(!is_i2c
|
||||
#if defined(CONFIG_I2C)
|
||||
|| _i2c_address == (*_current_instance)->_i2c_address
|
||||
#endif // CONFIG_I2C
|
||||
)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -371,13 +425,19 @@ board_bus_types BusInstanceIterator::busType() const
|
||||
case I2CSPIBusOption::All:
|
||||
return BOARD_INVALID_BUS;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
case I2CSPIBusOption::I2CInternal:
|
||||
case I2CSPIBusOption::I2CExternal:
|
||||
return BOARD_I2C_BUS;
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
case I2CSPIBusOption::SPIInternal:
|
||||
case I2CSPIBusOption::SPIExternal:
|
||||
return BOARD_SPI_BUS;
|
||||
#endif // CONFIG_SPI
|
||||
}
|
||||
|
||||
return BOARD_INVALID_BUS;
|
||||
@@ -385,78 +445,104 @@ board_bus_types BusInstanceIterator::busType() const
|
||||
|
||||
int BusInstanceIterator::bus() const
|
||||
{
|
||||
if (busType() == BOARD_INVALID_BUS) {
|
||||
return -1;
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.bus().bus;
|
||||
}
|
||||
|
||||
} else {
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (busType() == BOARD_I2C_BUS) {
|
||||
return _i2c_bus_iterator.bus().bus;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t BusInstanceIterator::devid() const
|
||||
{
|
||||
if (busType() == BOARD_INVALID_BUS) {
|
||||
return 0;
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.devid();
|
||||
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
spi_drdy_gpio_t BusInstanceIterator::DRDYGPIO() const
|
||||
{
|
||||
if (busType() == BOARD_INVALID_BUS) {
|
||||
return 0;
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.DRDYGPIO();
|
||||
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool BusInstanceIterator::external() const
|
||||
{
|
||||
if (busType() == BOARD_INVALID_BUS) {
|
||||
return false;
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.external();
|
||||
|
||||
} else {
|
||||
return _i2c_bus_iterator.external();
|
||||
}
|
||||
}
|
||||
|
||||
int BusInstanceIterator::externalBusIndex() const
|
||||
{
|
||||
if (busType() == BOARD_INVALID_BUS) {
|
||||
return 0;
|
||||
|
||||
} else if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.externalBusIndex();
|
||||
|
||||
} else {
|
||||
return _i2c_bus_iterator.externalBusIndex();
|
||||
}
|
||||
}
|
||||
|
||||
int BusInstanceIterator::busDeviceIndex() const
|
||||
{
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.busDeviceIndex();
|
||||
}
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t BusInstanceIterator::devid() const
|
||||
{
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.devid();
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
spi_drdy_gpio_t BusInstanceIterator::DRDYGPIO() const
|
||||
{
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.DRDYGPIO();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
bool BusInstanceIterator::external() const
|
||||
{
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.external();
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (busType() == BOARD_I2C_BUS) {
|
||||
return _i2c_bus_iterator.external();
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int BusInstanceIterator::externalBusIndex() const
|
||||
{
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.externalBusIndex();
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (busType() == BOARD_I2C_BUS) {
|
||||
return _i2c_bus_iterator.externalBusIndex();
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BusInstanceIterator::busDeviceIndex() const
|
||||
{
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (busType() == BOARD_SPI_BUS) {
|
||||
return _spi_bus_iterator.busDeviceIndex();
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
I2CBusIterator::FilterType BusInstanceIterator::i2cFilter(I2CSPIBusOption bus_option)
|
||||
{
|
||||
switch (bus_option) {
|
||||
@@ -471,7 +557,9 @@ I2CBusIterator::FilterType BusInstanceIterator::i2cFilter(I2CSPIBusOption bus_op
|
||||
|
||||
return I2CBusIterator::FilterType::All;
|
||||
}
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
SPIBusIterator::FilterType BusInstanceIterator::spiFilter(I2CSPIBusOption bus_option)
|
||||
{
|
||||
switch (bus_option) {
|
||||
@@ -484,6 +572,7 @@ SPIBusIterator::FilterType BusInstanceIterator::spiFilter(I2CSPIBusOption bus_op
|
||||
|
||||
return SPIBusIterator::FilterType::InternalBus;
|
||||
}
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
struct I2CSPIDriverInitializing {
|
||||
const I2CSPIDriverConfig &config;
|
||||
@@ -520,9 +609,15 @@ int I2CSPIDriverBase::module_start(const BusCLIArguments &cli, BusInstanceIterat
|
||||
device_id.devid_s.bus = iterator.bus();
|
||||
|
||||
switch (iterator.busType()) {
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
case BOARD_I2C_BUS: device_id.devid_s.bus_type = device::Device::DeviceBusType_I2C; break;
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
case BOARD_SPI_BUS: device_id.devid_s.bus_type = device::Device::DeviceBusType_SPI; break;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
case BOARD_INVALID_BUS: device_id.devid_s.bus_type = device::Device::DeviceBusType_UNKNOWN; break;
|
||||
}
|
||||
@@ -543,15 +638,21 @@ int I2CSPIDriverBase::module_start(const BusCLIArguments &cli, BusInstanceIterat
|
||||
continue;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (cli.i2c_address != 0 && instance->_i2c_address == 0) {
|
||||
PX4_ERR("Bug: driver %s does not pass the I2C address to I2CSPIDriverBase", instance->ItemName());
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
iterator.addInstance(instance);
|
||||
started = true;
|
||||
|
||||
// print some info that we are running
|
||||
switch (iterator.busType()) {
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
case BOARD_I2C_BUS:
|
||||
PX4_INFO_RAW("%s #%i on I2C bus %d", instance->ItemName(), runtime_instance, iterator.bus());
|
||||
|
||||
@@ -570,6 +671,8 @@ int I2CSPIDriverBase::module_start(const BusCLIArguments &cli, BusInstanceIterat
|
||||
PX4_INFO_RAW("\n");
|
||||
|
||||
break;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
case BOARD_SPI_BUS:
|
||||
PX4_INFO_RAW("%s #%i on SPI bus %d", instance->ItemName(), runtime_instance, iterator.bus());
|
||||
@@ -585,6 +688,7 @@ int I2CSPIDriverBase::module_start(const BusCLIArguments &cli, BusInstanceIterat
|
||||
PX4_INFO_RAW("\n");
|
||||
|
||||
break;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
case BOARD_INVALID_BUS:
|
||||
break;
|
||||
@@ -594,15 +698,18 @@ int I2CSPIDriverBase::module_start(const BusCLIArguments &cli, BusInstanceIterat
|
||||
if (!started && !cli.quiet_start) {
|
||||
PX4_WARN("%s: no instance started (no device on bus?)", px4_get_taskname());
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (iterator.busType() == BOARD_I2C_BUS && cli.i2c_address == 0) {
|
||||
PX4_ERR("%s: driver does not set i2c address", px4_get_taskname());
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
}
|
||||
|
||||
return started ? 0 : -1;
|
||||
}
|
||||
|
||||
|
||||
int I2CSPIDriverBase::module_stop(BusInstanceIterator &iterator)
|
||||
{
|
||||
bool is_running = false;
|
||||
@@ -680,14 +787,23 @@ int I2CSPIDriverBase::module_custom_method(const BusCLIArguments &cli, BusInstan
|
||||
|
||||
void I2CSPIDriverBase::print_status()
|
||||
{
|
||||
bool is_i2c_bus = _bus_option == I2CSPIBusOption::I2CExternal || _bus_option == I2CSPIBusOption::I2CInternal;
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (is_i2c_bus) {
|
||||
if (_bus_option == I2CSPIBusOption::I2CExternal || _bus_option == I2CSPIBusOption::I2CInternal) {
|
||||
PX4_INFO("Running on I2C Bus %i, Address 0x%02X", _bus, get_i2c_address());
|
||||
|
||||
} else {
|
||||
PX4_INFO("Running on SPI Bus %i", _bus);
|
||||
return;
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (_bus_option == I2CSPIBusOption::SPIExternal || _bus_option == I2CSPIBusOption::SPIInternal) {
|
||||
PX4_INFO("Running on SPI Bus %i", _bus);
|
||||
return;
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
}
|
||||
|
||||
void I2CSPIDriverBase::request_stop_and_wait()
|
||||
@@ -705,5 +821,3 @@ void I2CSPIDriverBase::request_stop_and_wait()
|
||||
PX4_ERR("Module did not respond to stop request");
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* BOARD_DISABLE_I2C_SPI */
|
||||
|
||||
@@ -1015,8 +1015,12 @@ int board_register_power_state_notification_cb(power_button_state_notification_t
|
||||
|
||||
enum board_bus_types {
|
||||
BOARD_INVALID_BUS = 0,
|
||||
#if defined(CONFIG_SPI)
|
||||
BOARD_SPI_BUS = 1,
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
BOARD_I2C_BUS = 2
|
||||
#endif // CONFIG_I2C
|
||||
};
|
||||
|
||||
#if defined(BOARD_HAS_BUS_MANIFEST)
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
|
||||
#include <board_config.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#define I2C_BUS_MAX_BUS_ITEMS PX4_NUMBER_I2C_BUSES
|
||||
|
||||
struct px4_i2c_bus_t {
|
||||
@@ -99,3 +101,5 @@ private:
|
||||
int _index{-1};
|
||||
int _external_bus_counter{0};
|
||||
};
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -33,8 +33,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "i2c.h"
|
||||
#include "spi.h"
|
||||
#include <board_config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -43,15 +42,29 @@
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <px4_platform_common/sem.h>
|
||||
#include <board_config.h>
|
||||
#include <drivers/device/spi.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
# include "i2c.h"
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
# include "spi.h"
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
# include <drivers/device/spi.h>
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
enum class I2CSPIBusOption : uint8_t {
|
||||
All = 0, ///< select all runnning instances
|
||||
#if defined(CONFIG_I2C)
|
||||
I2CInternal,
|
||||
I2CExternal,
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
SPIInternal,
|
||||
SPIExternal,
|
||||
#endif // CONFIG_SPI
|
||||
};
|
||||
|
||||
class BusCLIArguments;
|
||||
@@ -66,11 +79,15 @@ struct I2CSPIDriverConfig {
|
||||
I2CSPIBusOption bus_option;
|
||||
board_bus_types bus_type;
|
||||
int bus;
|
||||
#if defined(CONFIG_I2C)
|
||||
uint8_t i2c_address;
|
||||
#endif // CONFIG_I2C
|
||||
int bus_frequency;
|
||||
#if defined(CONFIG_SPI)
|
||||
spi_drdy_gpio_t drdy_gpio;
|
||||
spi_mode_e spi_mode;
|
||||
uint32_t spi_devid;
|
||||
#endif // CONFIG_SPI
|
||||
int bus_device_index;
|
||||
|
||||
Rotation rotation;
|
||||
@@ -93,13 +110,17 @@ class I2CSPIInstance : public ListNode<I2CSPIInstance *>
|
||||
{
|
||||
public:
|
||||
virtual ~I2CSPIInstance() = default;
|
||||
#if defined(CONFIG_I2C)
|
||||
virtual int8_t get_i2c_address() {return _i2c_address;}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
private:
|
||||
I2CSPIInstance(const I2CSPIDriverConfig &config)
|
||||
: _module_name(config.module_name), _bus_option(config.bus_option), _bus(config.bus),
|
||||
_devid_driver_index(config.devid_driver_index), _bus_device_index(config.bus_device_index),
|
||||
_i2c_address(config.i2c_address) {}
|
||||
_devid_driver_index(config.devid_driver_index), _bus_device_index(config.bus_device_index)
|
||||
#if defined(CONFIG_I2C)
|
||||
, _i2c_address(config.i2c_address)
|
||||
#endif // CONFIG_I2C
|
||||
{}
|
||||
|
||||
|
||||
friend class BusInstanceIterator;
|
||||
@@ -110,14 +131,28 @@ private:
|
||||
const int _bus;
|
||||
const uint16_t _devid_driver_index;
|
||||
const int8_t _bus_device_index;
|
||||
#if defined(CONFIG_I2C)
|
||||
const int8_t _i2c_address; ///< I2C address (optional)
|
||||
#endif // CONFIG_I2C
|
||||
};
|
||||
|
||||
class BusCLIArguments
|
||||
{
|
||||
public:
|
||||
BusCLIArguments(bool i2c_support, bool spi_support)
|
||||
: _i2c_support(i2c_support), _spi_support(spi_support) {}
|
||||
#if defined(CONFIG_I2C) || defined(CONFIG_SPI)
|
||||
:
|
||||
#endif // CONFIG_I2C || CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
_i2c_support(i2c_support)
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_I2C) && defined(CONFIG_SPI)
|
||||
,
|
||||
#endif // CONFIG_I2C && CONFIG_SPI
|
||||
#if defined(CONFIG_SPI)
|
||||
_spi_support(spi_support)
|
||||
#endif // CONFIG_SPI
|
||||
{}
|
||||
|
||||
/**
|
||||
* Parse CLI arguments (for drivers that don't need any custom arguments, otherwise getopt() should be used)
|
||||
@@ -139,11 +174,15 @@ public:
|
||||
|
||||
I2CSPIBusOption bus_option{I2CSPIBusOption::All};
|
||||
int requested_bus{-1};
|
||||
int chipselect_index{1};
|
||||
int bus_frequency{0};
|
||||
#if defined(CONFIG_SPI)
|
||||
int chipselect_index {1};
|
||||
spi_mode_e spi_mode{SPIDEV_MODE3};
|
||||
uint8_t i2c_address{0}; ///< I2C address (a driver must set the default address)
|
||||
bool quiet_start{false}; ///< do not print a message when startup fails
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
uint8_t i2c_address {0}; ///< I2C address (a driver must set the default address)
|
||||
#endif // CONFIG_I2C
|
||||
bool quiet_start {false}; ///< do not print a message when startup fails
|
||||
bool keep_running{false}; ///< keep driver running even if no device is detected on startup
|
||||
|
||||
Rotation rotation{ROTATION_NONE}; ///< sensor rotation (MAV_SENSOR_ROTATION_* or distance_sensor_s::ROTATION_*)
|
||||
@@ -153,8 +192,12 @@ public:
|
||||
void *custom_data{nullptr}; ///< driver-specific custom argument
|
||||
|
||||
// driver defaults, if not specified via CLI
|
||||
int default_spi_frequency{-1}; ///< default spi bus frequency (driver needs to set this) [Hz]
|
||||
int default_i2c_frequency{-1}; ///< default i2c bus frequency (driver needs to set this) [Hz]
|
||||
#if defined(CONFIG_SPI)
|
||||
int default_spi_frequency {-1}; ///< default spi bus frequency (driver needs to set this) [Hz]
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
int default_i2c_frequency {-1}; ///< default i2c bus frequency (driver needs to set this) [Hz]
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
bool support_keep_running{false}; ///< true if keep_running (see above) is supported
|
||||
|
||||
@@ -164,8 +207,12 @@ private:
|
||||
char _options[32] {};
|
||||
int _optind{1};
|
||||
const char *_optarg{nullptr};
|
||||
#if defined(CONFIG_I2C)
|
||||
const bool _i2c_support;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
const bool _spi_support;
|
||||
#endif // CONFIG_SPI
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -189,15 +236,23 @@ public:
|
||||
board_bus_types busType() const;
|
||||
int bus() const;
|
||||
uint32_t devid() const;
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
spi_drdy_gpio_t DRDYGPIO() const;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
bool external() const;
|
||||
int externalBusIndex() const;
|
||||
int busDeviceIndex() const;
|
||||
|
||||
void addInstance(I2CSPIInstance *instance);
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
static I2CBusIterator::FilterType i2cFilter(I2CSPIBusOption bus_option);
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
static SPIBusIterator::FilterType spiFilter(I2CSPIBusOption bus_option);
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
const char *moduleName() const { return _module_name; }
|
||||
uint16_t devidDriverIndex() const { return _devid_driver_index; }
|
||||
@@ -206,9 +261,15 @@ private:
|
||||
const char *_module_name;
|
||||
const I2CSPIBusOption _bus_option;
|
||||
const uint16_t _devid_driver_index;
|
||||
#if defined(CONFIG_I2C)
|
||||
const uint8_t _i2c_address;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
SPIBusIterator _spi_bus_iterator;
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
I2CBusIterator _i2c_bus_iterator;
|
||||
#endif // CONFIG_I2C
|
||||
List<I2CSPIInstance *>::Iterator _current_instance;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
#include <stdint.h>
|
||||
#include <board_config.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.
|
||||
@@ -168,3 +170,5 @@ private:
|
||||
int _external_bus_counter{1};
|
||||
int _bus_device_index{-1};
|
||||
};
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include <board_config.h>
|
||||
#ifndef BOARD_DISABLE_I2C_SPI
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
#include <px4_platform_common/spi.h>
|
||||
|
||||
@@ -163,4 +164,4 @@ bool SPIBusIterator::next()
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif /* BOARD_DISABLE_I2C_SPI */
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
Reference in New Issue
Block a user