mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-21 00:23:34 +08:00
refactor aerofc_adc: use driver base class
This commit is contained in:
@@ -35,17 +35,15 @@
|
|||||||
|
|
||||||
using namespace time_literals;
|
using namespace time_literals;
|
||||||
|
|
||||||
AEROFC_ADC::AEROFC_ADC(uint8_t bus) :
|
AEROFC_ADC::AEROFC_ADC(I2CSPIBusOption bus_option, int bus_number, int bus_frequency) :
|
||||||
I2C("AEROFC_ADC", AEROFC_ADC_DEVICE_PATH, bus, SLAVE_ADDR, 400000),
|
I2C("AEROFC_ADC", AEROFC_ADC_DEVICE_PATH, bus_number, SLAVE_ADDR, bus_frequency),
|
||||||
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())),
|
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus_number),
|
||||||
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": sample"))
|
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": sample"))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AEROFC_ADC::~AEROFC_ADC()
|
AEROFC_ADC::~AEROFC_ADC()
|
||||||
{
|
{
|
||||||
ScheduleClear();
|
|
||||||
perf_free(_sample_perf);
|
perf_free(_sample_perf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +92,7 @@ error:
|
|||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEROFC_ADC::Run()
|
void AEROFC_ADC::RunImpl()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* https://github.com/intel-aero/intel-aero-fpga/blob/master/aero_sample/adc/adc.html
|
* https://github.com/intel-aero/intel-aero-fpga/blob/master/aero_sample/adc/adc.html
|
||||||
@@ -112,8 +110,8 @@ void AEROFC_ADC::Run()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
adc_report_s adc_report;
|
adc_report_s adc_report{};
|
||||||
adc_report.device_id = this->get_device_id();
|
adc_report.device_id = get_device_id();
|
||||||
adc_report.timestamp = hrt_absolute_time();
|
adc_report.timestamp = hrt_absolute_time();
|
||||||
adc_report.v_ref = 3.0f;
|
adc_report.v_ref = 3.0f;
|
||||||
adc_report.resolution = 1 << 12;
|
adc_report.resolution = 1 << 12;
|
||||||
@@ -132,4 +130,4 @@ void AEROFC_ADC::Run()
|
|||||||
_to_adc_report.publish(adc_report);
|
_to_adc_report.publish(adc_report);
|
||||||
|
|
||||||
perf_end(_sample_perf);
|
perf_end(_sample_perf);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
#include <px4_platform_common/log.h>
|
#include <px4_platform_common/log.h>
|
||||||
#include <drivers/device/i2c.h>
|
#include <drivers/device/i2c.h>
|
||||||
#include <drivers/drv_adc.h>
|
#include <drivers/drv_adc.h>
|
||||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
#include <px4_platform_common/i2c_spi_buses.h>
|
||||||
#include <uORB/Publication.hpp>
|
#include <uORB/Publication.hpp>
|
||||||
#include <uORB/topics/adc_report.h>
|
#include <uORB/topics/adc_report.h>
|
||||||
|
|
||||||
@@ -52,17 +52,21 @@
|
|||||||
#define MAX_CHANNEL 5
|
#define MAX_CHANNEL 5
|
||||||
#define AEROFC_ADC_DEVICE_PATH "/dev/aerofc_adc"
|
#define AEROFC_ADC_DEVICE_PATH "/dev/aerofc_adc"
|
||||||
|
|
||||||
class AEROFC_ADC : public device::I2C, public px4::ScheduledWorkItem
|
class AEROFC_ADC : public device::I2C, public I2CSPIDriver<AEROFC_ADC>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AEROFC_ADC(uint8_t bus);
|
AEROFC_ADC(I2CSPIBusOption bus_option, int bus_number, int bus_frequency);
|
||||||
~AEROFC_ADC() override;
|
~AEROFC_ADC() override;
|
||||||
|
|
||||||
|
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
|
||||||
|
int runtime_instance);
|
||||||
|
static void print_usage();
|
||||||
|
|
||||||
int init() override;
|
int init() override;
|
||||||
|
|
||||||
|
void RunImpl();
|
||||||
private:
|
private:
|
||||||
int probe() override;;
|
int probe() override;;
|
||||||
void Run() override;
|
|
||||||
|
|
||||||
uORB::Publication<adc_report_s> _to_adc_report{ORB_ID(adc_report)};
|
uORB::Publication<adc_report_s> _to_adc_report{ORB_ID(adc_report)};
|
||||||
|
|
||||||
|
|||||||
@@ -33,117 +33,66 @@
|
|||||||
|
|
||||||
#include <px4_platform_common/px4_config.h>
|
#include <px4_platform_common/px4_config.h>
|
||||||
#include <px4_platform_common/getopt.h>
|
#include <px4_platform_common/getopt.h>
|
||||||
|
#include <px4_platform_common/module.h>
|
||||||
|
|
||||||
#include "AEROFC_ADC.hpp"
|
#include "AEROFC_ADC.hpp"
|
||||||
|
|
||||||
enum AEROFC_ADC_BUS {
|
I2CSPIDriverBase *
|
||||||
AEROFC_ADC_BUS_ALL = 0,
|
AEROFC_ADC::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator, int runtime_instance)
|
||||||
AEROFC_ADC_BUS_I2C_INTERNAL,
|
|
||||||
AEROFC_ADC_BUS_I2C_EXTERNAL
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr struct aerofc_adc_bus_option {
|
|
||||||
enum AEROFC_ADC_BUS busid;
|
|
||||||
uint8_t busnum;
|
|
||||||
} bus_options[] = {
|
|
||||||
#ifdef PX4_I2C_BUS_EXPANSION
|
|
||||||
{ AEROFC_ADC_BUS_I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION },
|
|
||||||
#endif
|
|
||||||
#ifdef PX4_I2C_BUS_EXPANSION1
|
|
||||||
{ AEROFC_ADC_BUS_I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION1 },
|
|
||||||
#endif
|
|
||||||
#ifdef PX4_I2C_BUS_ONBOARD
|
|
||||||
{ AEROFC_ADC_BUS_I2C_INTERNAL, PX4_I2C_BUS_ONBOARD },
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
extern "C" { __EXPORT int aerofc_adc_main(int argc, char *argv[]); }
|
|
||||||
|
|
||||||
static AEROFC_ADC *instance = nullptr;
|
|
||||||
|
|
||||||
static int test()
|
|
||||||
{
|
{
|
||||||
PX4_INFO("test is currently unavailable");
|
AEROFC_ADC *interface = new AEROFC_ADC(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency);
|
||||||
|
|
||||||
return 0;
|
if (interface == nullptr) {
|
||||||
}
|
PX4_ERR("alloc failed");
|
||||||
|
return nullptr;
|
||||||
static void help()
|
|
||||||
{
|
|
||||||
printf("missing command: try 'start' or 'test'\n");
|
|
||||||
printf("options:\n");
|
|
||||||
printf(" -I only internal I2C bus\n");
|
|
||||||
printf(" -X only external I2C bus\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int aerofc_adc_main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ch;
|
|
||||||
enum AEROFC_ADC_BUS busid = AEROFC_ADC_BUS_ALL;
|
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "XI")) != EOF) {
|
|
||||||
switch (ch) {
|
|
||||||
case 'X':
|
|
||||||
busid = AEROFC_ADC_BUS_I2C_EXTERNAL;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'I':
|
|
||||||
busid = AEROFC_ADC_BUS_I2C_INTERNAL;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
help();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optind >= argc) {
|
if (interface->init() != OK) {
|
||||||
help();
|
delete interface;
|
||||||
return PX4_ERROR;
|
PX4_DEBUG("no device on bus %i (devid 0x%x)", iterator.bus(), iterator.devid());
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *verb = argv[optind];
|
return interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AEROFC_ADC::print_usage()
|
||||||
|
{
|
||||||
|
PRINT_MODULE_USAGE_NAME("aerofc_adc", "driver");
|
||||||
|
PRINT_MODULE_USAGE_COMMAND("start");
|
||||||
|
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
|
||||||
|
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int
|
||||||
|
aerofc_adc_main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
using ThisDriver = AEROFC_ADC;
|
||||||
|
BusCLIArguments cli{true, false};
|
||||||
|
cli.default_i2c_frequency = 400000;
|
||||||
|
|
||||||
|
const char *verb = cli.parseDefaultArguments(argc, argv);
|
||||||
|
|
||||||
|
if (!verb) {
|
||||||
|
ThisDriver::print_usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_ADC_DEVTYPE_AEROFC);
|
||||||
|
|
||||||
if (!strcmp(verb, "start")) {
|
if (!strcmp(verb, "start")) {
|
||||||
if (instance) {
|
return ThisDriver::module_start(cli, iterator);
|
||||||
PX4_WARN("AEROFC_ADC was already started");
|
|
||||||
return PX4_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < (sizeof(bus_options) / sizeof(bus_options[0])); i++) {
|
|
||||||
if (busid != AEROFC_ADC_BUS_ALL && busid != bus_options[i].busid) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
instance = new AEROFC_ADC(bus_options[i].busnum);
|
|
||||||
|
|
||||||
if (!instance) {
|
|
||||||
PX4_WARN("No memory to instance AEROFC_ADC");
|
|
||||||
return PX4_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (instance->init() == PX4_OK) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
PX4_WARN("AEROFC_ADC not found on busnum=%u", bus_options[i].busnum);
|
|
||||||
delete instance;
|
|
||||||
instance = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!instance) {
|
|
||||||
PX4_WARN("AEROFC_ADC not found");
|
|
||||||
return PX4_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (!strcmp(verb, "test")) {
|
|
||||||
return test();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
PX4_WARN("Action not supported");
|
|
||||||
help();
|
|
||||||
return PX4_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return PX4_OK;
|
if (!strcmp(verb, "stop")) {
|
||||||
|
return ThisDriver::module_stop(iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strcmp(verb, "status")) {
|
||||||
|
return ThisDriver::module_status(iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
ThisDriver::print_usage();
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
# intel aero specific board sensors init
|
# intel aero specific board sensors init
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
aerofc_adc start
|
aerofc_adc -I start
|
||||||
|
|
||||||
if ! ms5611 -T 5607 start
|
if ! ms5611 -T 5607 start
|
||||||
then
|
then
|
||||||
|
|||||||
@@ -114,6 +114,7 @@
|
|||||||
#define DRV_IMU_DEVTYPE_ADIS16448 0x57
|
#define DRV_IMU_DEVTYPE_ADIS16448 0x57
|
||||||
#define DRV_BARO_DEVTYPE_LPS22HB 0x58
|
#define DRV_BARO_DEVTYPE_LPS22HB 0x58
|
||||||
#define DRV_IMU_DEVTYPE_ADIS16477 0x59
|
#define DRV_IMU_DEVTYPE_ADIS16477 0x59
|
||||||
|
#define DRV_ADC_DEVTYPE_AEROFC 0x5a
|
||||||
|
|
||||||
#define DRV_ACC_DEVTYPE_LSM303AGR 0x61
|
#define DRV_ACC_DEVTYPE_LSM303AGR 0x61
|
||||||
#define DRV_MAG_DEVTYPE_LSM303AGR 0x62
|
#define DRV_MAG_DEVTYPE_LSM303AGR 0x62
|
||||||
|
|||||||
Reference in New Issue
Block a user