mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 21:43:34 +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:
@@ -42,6 +42,7 @@
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <drivers/device/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
class BMP280_I2C: public device::I2C, public bmp280::IBMP280
|
||||
{
|
||||
@@ -116,3 +117,5 @@ BMP280_I2C::get_calibration(uint8_t addr)
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <drivers/device/spi.h>
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
/* SPI protocol address bits */
|
||||
#define DIR_READ (1<<7) //for set
|
||||
#define DIR_WRITE ~(1<<7) //for clear
|
||||
@@ -132,3 +134,4 @@ BMP280_SPI::get_calibration(uint8_t addr)
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <drivers/device/spi.h>
|
||||
#include <px4_platform_common/i2c_spi_buses.h>
|
||||
|
||||
#define BMP280_ADDR_CAL 0x88 /* address of 12x 2 bytes calibration data */
|
||||
#define BMP280_ADDR_DATA 0xF7 /* address of 2x 3 bytes p-t data */
|
||||
@@ -158,5 +158,9 @@ public:
|
||||
|
||||
|
||||
/* interface factories */
|
||||
#if defined(CONFIG_SPI)
|
||||
extern bmp280::IBMP280 *bmp280_spi_interface(uint8_t busnum, uint32_t device, int bus_frequency, spi_mode_e spi_mode);
|
||||
#endif // CONFIG_SPI
|
||||
#if defined(CONFIG_I2C)
|
||||
extern bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint32_t device, int bus_frequency);
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
|
||||
#include "BMP280.hpp"
|
||||
|
||||
#include <drivers/drv_sensor.h>
|
||||
|
||||
extern "C" { __EXPORT int bmp280_main(int argc, char *argv[]); }
|
||||
|
||||
void
|
||||
@@ -45,8 +47,12 @@ BMP280::print_usage()
|
||||
PRINT_MODULE_USAGE_NAME("bmp280", "driver");
|
||||
PRINT_MODULE_USAGE_SUBCATEGORY("baro");
|
||||
PRINT_MODULE_USAGE_COMMAND("start");
|
||||
#if defined(CONFIG_I2C)
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true);
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x76);
|
||||
#else
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
|
||||
#endif
|
||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||
}
|
||||
|
||||
@@ -54,21 +60,30 @@ I2CSPIDriverBase *BMP280::instantiate(const I2CSPIDriverConfig &config, int runt
|
||||
{
|
||||
bmp280::IBMP280 *interface = nullptr;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (config.bus_type == BOARD_I2C_BUS) {
|
||||
interface = bmp280_i2c_interface(config.bus, config.i2c_address, config.bus_frequency);
|
||||
|
||||
} else if (config.bus_type == BOARD_SPI_BUS) {
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
if (config.bus_type == BOARD_SPI_BUS) {
|
||||
interface = bmp280_spi_interface(config.bus, config.spi_devid, config.bus_frequency, config.spi_mode);
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
if (interface == nullptr) {
|
||||
PX4_ERR("failed creating interface for bus %i (devid 0x%" PRIx32 ")", config.bus, config.spi_devid);
|
||||
PX4_ERR("failed creating interface for bus %i", config.bus);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (interface->init() != OK) {
|
||||
delete interface;
|
||||
PX4_DEBUG("no device on bus %i (devid 0x%" PRIx32 ")", config.bus, config.spi_devid);
|
||||
PX4_DEBUG("no device on bus %i", config.bus);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -92,9 +107,13 @@ bmp280_main(int argc, char *argv[])
|
||||
{
|
||||
using ThisDriver = BMP280;
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.i2c_address = 0x76;
|
||||
cli.default_i2c_frequency = 100 * 1000;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 10 * 1000 * 1000;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
const char *verb = cli.parseDefaultArguments(argc, argv);
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
|
||||
#include <lib/drivers/device/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
namespace dps310
|
||||
{
|
||||
|
||||
@@ -89,3 +91,5 @@ DPS310_I2C::write(unsigned address, void *data, unsigned count)
|
||||
}
|
||||
|
||||
} // namespace dps310
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -36,7 +36,9 @@
|
||||
namespace dps310
|
||||
{
|
||||
extern device::Device *DPS310_SPI_interface(uint8_t bus, uint32_t device, int bus_frequency, spi_mode_e spi_mode);
|
||||
#if defined(CONFIG_I2C)
|
||||
extern device::Device *DPS310_I2C_interface(uint8_t bus, uint32_t device, int bus_frequency);
|
||||
#endif // CONFIG_I2C
|
||||
}
|
||||
|
||||
#include <px4_platform_common/getopt.h>
|
||||
@@ -50,8 +52,12 @@ DPS310::print_usage()
|
||||
PRINT_MODULE_USAGE_NAME("dps310", "driver");
|
||||
PRINT_MODULE_USAGE_SUBCATEGORY("baro");
|
||||
PRINT_MODULE_USAGE_COMMAND("start");
|
||||
#if defined(CONFIG_I2C)
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true);
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x77);
|
||||
#else
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
|
||||
#endif
|
||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||
}
|
||||
|
||||
@@ -59,12 +65,16 @@ I2CSPIDriverBase *DPS310::instantiate(const I2CSPIDriverConfig &config, int runt
|
||||
{
|
||||
device::Device *interface = nullptr;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (config.bus_type == BOARD_I2C_BUS) {
|
||||
interface = DPS310_I2C_interface(config.bus, config.i2c_address, config.bus_frequency);
|
||||
|
||||
} else if (config.bus_type == BOARD_SPI_BUS) {
|
||||
interface = DPS310_SPI_interface(config.bus, config.spi_devid, config.bus_frequency, config.spi_mode);
|
||||
}
|
||||
} else
|
||||
#endif // CONFIG_I2C
|
||||
if (config.bus_type == BOARD_SPI_BUS) {
|
||||
interface = DPS310_SPI_interface(config.bus, config.spi_devid, config.bus_frequency, config.spi_mode);
|
||||
}
|
||||
|
||||
if (interface == nullptr) {
|
||||
PX4_ERR("failed creating interface for bus %i (devid 0x%" PRIx32 ")", config.bus, config.spi_devid);
|
||||
@@ -95,9 +105,15 @@ I2CSPIDriverBase *DPS310::instantiate(const I2CSPIDriverConfig &config, int runt
|
||||
extern "C" int dps310_main(int argc, char *argv[])
|
||||
{
|
||||
using ThisDriver = DPS310;
|
||||
BusCLIArguments cli{true, true};
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
BusCLIArguments cli {true, true};
|
||||
cli.i2c_address = 0x77;
|
||||
cli.default_i2c_frequency = 400000;
|
||||
#else
|
||||
BusCLIArguments cli {false, true};
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
cli.default_spi_frequency = 10 * 1000 * 1000;
|
||||
|
||||
const char *verb = cli.parseDefaultArguments(argc, argv);
|
||||
|
||||
@@ -39,17 +39,19 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <drivers/device/i2c.h>
|
||||
#if defined(CONFIG_I2C)
|
||||
# include <drivers/device/i2c.h>
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
#include <drivers/device/device.h>
|
||||
#include <drivers/device/spi.h>
|
||||
#include <lib/cdev/CDev.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <systemlib/err.h>
|
||||
#include <uORB/uORB.h>
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
#define ADDR_RESET_CMD 0x1E /* write to this address to reset chip */
|
||||
#define ADDR_PROM_SETUP 0xA0 /* address of 8x 2 bytes factory and calibration data */
|
||||
@@ -96,7 +98,10 @@ extern bool crc4(uint16_t *n_prom);
|
||||
/* interface factories */
|
||||
extern device::Device *MS5611_spi_interface(ms5611::prom_u &prom_buf, uint32_t devid, uint8_t busnum, int bus_frequency,
|
||||
spi_mode_e spi_mode);
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
extern device::Device *MS5611_i2c_interface(ms5611::prom_u &prom_buf, uint32_t devid, uint8_t busnum,
|
||||
int bus_frequency);
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
typedef device::Device *(*MS5611_constructor)(ms5611::prom_u &prom_buf, uint32_t devid, uint8_t busnum);
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
|
||||
#include <drivers/device/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#include "ms5611.h"
|
||||
|
||||
class MS5611_I2C : public device::I2C
|
||||
@@ -238,3 +240,5 @@ MS5611_I2C::_read_prom()
|
||||
/* calculate CRC and return success/failure accordingly */
|
||||
return (ms5611::crc4(&_prom.c[0]) && !bits_stuck) ? PX4_OK : -EIO;
|
||||
}
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -45,12 +45,16 @@ I2CSPIDriverBase *MS5611::instantiate(const I2CSPIDriverConfig &config, int runt
|
||||
ms5611::prom_u prom_buf;
|
||||
device::Device *interface = nullptr;
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
if (config.bus_type == BOARD_I2C_BUS) {
|
||||
interface = MS5611_i2c_interface(prom_buf, config.spi_devid, config.bus, config.bus_frequency);
|
||||
|
||||
} else if (config.bus_type == BOARD_SPI_BUS) {
|
||||
interface = MS5611_spi_interface(prom_buf, config.spi_devid, config.bus, config.bus_frequency, config.spi_mode);
|
||||
}
|
||||
} else
|
||||
#endif // CONFIG_I2C
|
||||
if (config.bus_type == BOARD_SPI_BUS) {
|
||||
interface = MS5611_spi_interface(prom_buf, config.spi_devid, config.bus, config.bus_frequency, config.spi_mode);
|
||||
}
|
||||
|
||||
if (interface == nullptr) {
|
||||
PX4_ERR("alloc failed");
|
||||
@@ -83,7 +87,11 @@ void MS5611::print_usage()
|
||||
PRINT_MODULE_USAGE_NAME("ms5611", "driver");
|
||||
PRINT_MODULE_USAGE_SUBCATEGORY("baro");
|
||||
PRINT_MODULE_USAGE_COMMAND("start");
|
||||
#if defined(CONFIG_I2C)
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true);
|
||||
#else
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
|
||||
#endif
|
||||
PRINT_MODULE_USAGE_PARAM_STRING('T', "5611", "5607|5611", "Device type", true);
|
||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||
}
|
||||
@@ -92,8 +100,13 @@ extern "C" int ms5611_main(int argc, char *argv[])
|
||||
{
|
||||
using ThisDriver = MS5611;
|
||||
int ch;
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
BusCLIArguments cli {true, true};
|
||||
cli.default_i2c_frequency = 400000;
|
||||
cli.i2c_address = MS5611_ADDRESS_1;
|
||||
#else
|
||||
BusCLIArguments cli {false, true};
|
||||
#endif
|
||||
cli.default_spi_frequency = 20 * 1000 * 1000;
|
||||
uint16_t dev_type_driver = DRV_BARO_DEVTYPE_MS5611;
|
||||
|
||||
@@ -120,8 +133,6 @@ extern "C" int ms5611_main(int argc, char *argv[])
|
||||
return -1;
|
||||
}
|
||||
|
||||
cli.i2c_address = MS5611_ADDRESS_1;
|
||||
|
||||
BusInstanceIterator iterator(MODULE_NAME, cli, dev_type_driver);
|
||||
|
||||
if (!strcmp(verb, "start")) {
|
||||
|
||||
Reference in New Issue
Block a user