mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 09:53: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:
@@ -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")) {
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
|
||||
#include "I2C.hpp"
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#include <px4_platform_common/i2c_spi_buses.h>
|
||||
#include <nuttx/i2c/i2c_master.h>
|
||||
|
||||
@@ -236,3 +238,5 @@ I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const
|
||||
}
|
||||
|
||||
} // namespace device
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2012-2021 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
|
||||
@@ -31,26 +31,23 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file i2c.h
|
||||
* @file I2C.hpp
|
||||
*
|
||||
* Base class for devices connected via I2C.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_I2C_H
|
||||
#define _DEVICE_I2C_H
|
||||
|
||||
#include "../CDev.hpp"
|
||||
#include <px4_platform_common/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#include <nuttx/i2c/i2c_master.h>
|
||||
|
||||
struct I2CSPIDriverConfig;
|
||||
|
||||
#if !defined(CONFIG_I2C)
|
||||
# error I2C support requires CONFIG_I2C
|
||||
#endif
|
||||
|
||||
namespace device __EXPORT
|
||||
{
|
||||
|
||||
@@ -123,4 +120,4 @@ private:
|
||||
|
||||
} // namespace device
|
||||
|
||||
#endif /* _DEVICE_I2C_H */
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012-2019, 2021 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2012-2021 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
|
||||
@@ -32,21 +32,16 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file spi.cpp
|
||||
* @file SPI.cpp
|
||||
*
|
||||
* Base class for devices connected via SPI.
|
||||
*
|
||||
* @todo Work out if caching the mode/frequency would save any time.
|
||||
*
|
||||
* @todo A separate bus/device abstraction would allow for mixed interrupt-mode
|
||||
* and non-interrupt-mode clients to arbitrate for the bus. As things stand,
|
||||
* a bus shared between clients of both kinds is vulnerable to races between
|
||||
* the two, where an interrupt-mode client will ignore the lock held by the
|
||||
* non-interrupt-mode client.
|
||||
*/
|
||||
|
||||
#include "SPI.hpp"
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
#include <px4_platform_common/i2c_spi_buses.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <nuttx/arch.h>
|
||||
@@ -237,3 +232,4 @@ SPI::_transferhword(uint16_t *send, uint16_t *recv, unsigned len)
|
||||
}
|
||||
|
||||
} // namespace device
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2012-2021 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
|
||||
@@ -31,16 +31,18 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file SPI.hpp
|
||||
*
|
||||
* Base class for devices connected via SPI.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_SPI_H
|
||||
#define _DEVICE_SPI_H
|
||||
|
||||
#include "../CDev.hpp"
|
||||
#include <px4_platform_common/spi.h>
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
#include <nuttx/spi/spi.h>
|
||||
#include <px4_platform_common/spi.h>
|
||||
@@ -178,4 +180,4 @@ protected:
|
||||
|
||||
} // namespace device
|
||||
|
||||
#endif /* _DEVICE_SPI_H */
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
* Copyright (c) 2016-2021 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
|
||||
@@ -42,6 +42,8 @@
|
||||
|
||||
#include "I2C.hpp"
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
|
||||
#include <linux/i2c.h>
|
||||
@@ -181,3 +183,5 @@ I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const
|
||||
} // namespace device
|
||||
|
||||
#endif // __PX4_LINUX
|
||||
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2016-2021 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
|
||||
@@ -31,18 +31,19 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file I2C.hpp
|
||||
*
|
||||
* Base class for devices connected via I2C.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_I2C_H
|
||||
#define _DEVICE_I2C_H
|
||||
|
||||
#include "../CDev.hpp"
|
||||
#include <px4_platform_common/i2c.h>
|
||||
|
||||
#if defined(CONFIG_I2C)
|
||||
|
||||
struct I2CSPIDriverConfig;
|
||||
|
||||
namespace device __EXPORT
|
||||
@@ -112,4 +113,4 @@ private:
|
||||
|
||||
} // namespace device
|
||||
|
||||
#endif /* _DEVICE_I2C_H */
|
||||
#endif // CONFIG_I2C
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2019-2021 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
|
||||
@@ -40,6 +40,8 @@
|
||||
|
||||
#include "SPI.hpp"
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
|
||||
#include <fcntl.h>
|
||||
@@ -73,7 +75,6 @@ SPI::SPI(const I2CSPIDriverConfig &config)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SPI::~SPI()
|
||||
{
|
||||
if (_fd >= 0) {
|
||||
@@ -197,3 +198,4 @@ SPI::transferhword(uint16_t *send, uint16_t *recv, unsigned len)
|
||||
} // namespace device
|
||||
|
||||
#endif // __PX4_LINUX
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2019-2021 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
|
||||
@@ -31,18 +31,19 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file SPI.hpp
|
||||
*
|
||||
* Base class for devices connected via SPI.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_SPI_H
|
||||
#define _DEVICE_SPI_H
|
||||
|
||||
#include "../CDev.hpp"
|
||||
#include <px4_platform_common/spi.h>
|
||||
|
||||
#if defined(CONFIG_SPI)
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
|
||||
#include <fcntl.h>
|
||||
@@ -201,4 +202,4 @@ enum spi_mode_e {
|
||||
};
|
||||
#endif // __PX4_LINUX
|
||||
|
||||
#endif /* _DEVICE_SPI_H */
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
@@ -84,55 +84,80 @@ bool I2CSPICLITest::test_basic()
|
||||
|
||||
{
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 1234;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 12345;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "start", "-I" };
|
||||
CLIArgsHelper cli_args(argv, 2);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
ut_assert_true(verb != nullptr);
|
||||
ut_assert_true(strcmp(verb, "start") == 0);
|
||||
#if defined(CONFIG_I2C)
|
||||
ut_assert_true(cli.bus_option == I2CSPIBusOption::I2CInternal);
|
||||
ut_assert_true(cli.bus_frequency == cli.default_i2c_frequency);
|
||||
#endif // CONFIG_I2C
|
||||
}
|
||||
|
||||
{
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 1234;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 12345;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "start", "-s", "-f", "10"};
|
||||
CLIArgsHelper cli_args(argv, 4);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
ut_assert_true(verb != nullptr);
|
||||
ut_assert_true(strcmp(verb, "start") == 0);
|
||||
#if defined(CONFIG_SPI)
|
||||
ut_assert_true(cli.bus_option == I2CSPIBusOption::SPIInternal);
|
||||
#endif // CONFIG_SPI
|
||||
ut_assert_true(cli.bus_frequency == 10000);
|
||||
}
|
||||
|
||||
{
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "-S", "-b", "3", "stop"};
|
||||
CLIArgsHelper cli_args(argv, 4);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
ut_assert_true(verb != nullptr);
|
||||
ut_assert_true(strcmp(verb, "stop") == 0);
|
||||
#if defined(CONFIG_SPI)
|
||||
ut_assert_true(cli.bus_option == I2CSPIBusOption::SPIExternal);
|
||||
#endif // CONFIG_SPI
|
||||
ut_assert_true(cli.requested_bus == 3);
|
||||
}
|
||||
|
||||
{
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
cli.default_spi_frequency = 22222;
|
||||
cli.i2c_address = 0xab;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
const char *argv[] = { "start", "-X", "-a", "0x14"};
|
||||
CLIArgsHelper cli_args(argv, 4);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
ut_assert_true(verb != nullptr);
|
||||
ut_assert_true(strcmp(verb, "start") == 0);
|
||||
#if defined(CONFIG_I2C)
|
||||
ut_assert_true(cli.bus_option == I2CSPIBusOption::I2CExternal);
|
||||
ut_assert_true(cli.i2c_address == 0x14);
|
||||
#endif // CONFIG_I2C
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -143,7 +168,9 @@ bool I2CSPICLITest::test_invalid()
|
||||
{
|
||||
// SPI disabled, but SPI option provided
|
||||
BusCLIArguments cli{true, false};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
const char *argv[] = { "start", "-S"};
|
||||
CLIArgsHelper cli_args(argv, 2);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
@@ -153,8 +180,12 @@ bool I2CSPICLITest::test_invalid()
|
||||
{
|
||||
// Unknown argument
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "start", "-I", "-x", "3"};
|
||||
CLIArgsHelper cli_args(argv, 3);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
@@ -173,8 +204,12 @@ bool I2CSPICLITest::test_invalid()
|
||||
{
|
||||
// Another unknown argument
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "-x", "start" };
|
||||
CLIArgsHelper cli_args(argv, 2);
|
||||
const char *verb = cli.parseDefaultArguments(cli_args.argc, cli_args.argv);
|
||||
@@ -188,8 +223,12 @@ bool I2CSPICLITest::test_custom()
|
||||
{
|
||||
// custom argument
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "start", "-T", "432", "-a", "12"};
|
||||
CLIArgsHelper cli_args(argv, 5);
|
||||
int ch;
|
||||
@@ -216,8 +255,12 @@ bool I2CSPICLITest::test_custom()
|
||||
{
|
||||
// duplicate argument
|
||||
BusCLIArguments cli{true, true};
|
||||
#if defined(CONFIG_I2C)
|
||||
cli.default_i2c_frequency = 11111;
|
||||
#endif // CONFIG_I2C
|
||||
#if defined(CONFIG_SPI)
|
||||
cli.default_spi_frequency = 22222;
|
||||
#endif // CONFIG_SPI
|
||||
const char *argv[] = { "start"};
|
||||
CLIArgsHelper cli_args(argv, 1);
|
||||
int ch;
|
||||
|
||||
Reference in New Issue
Block a user