refactor lps22hb: use driver base class

This commit is contained in:
Beat Küng
2020-03-17 23:31:17 -04:00
committed by Daniel Agar
parent 83a3deb9da
commit d81fedfcfa
6 changed files with 77 additions and 287 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ adc start
adis16477 -R 8 start adis16477 -R 8 start
lps22hb -S start lps22hb -s start
lsm303agr -R 4 start lsm303agr -R 4 start
+6 -15
View File
@@ -42,9 +42,9 @@
/* Max measurement rate is 25Hz */ /* Max measurement rate is 25Hz */
#define LPS22HB_CONVERSION_INTERVAL (1000000 / 25) /* microseconds */ #define LPS22HB_CONVERSION_INTERVAL (1000000 / 25) /* microseconds */
LPS22HB::LPS22HB(device::Device *interface, const char *path) : LPS22HB::LPS22HB(I2CSPIBusOption bus_option, int bus, device::Device *interface) :
CDev(path), CDev(nullptr),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(interface->get_device_id())), I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(interface->get_device_id()), bus_option, bus),
_interface(interface), _interface(interface),
_sample_perf(perf_alloc(PC_ELAPSED, "lps22hb_read")), _sample_perf(perf_alloc(PC_ELAPSED, "lps22hb_read")),
_comms_errors(perf_alloc(PC_COUNT, "lps22hb_comms_errors")) _comms_errors(perf_alloc(PC_COUNT, "lps22hb_comms_errors"))
@@ -55,9 +55,6 @@ LPS22HB::LPS22HB(device::Device *interface, const char *path) :
LPS22HB::~LPS22HB() LPS22HB::~LPS22HB()
{ {
/* make sure we are truly inactive */
stop();
if (_class_instance != -1) { if (_class_instance != -1) {
unregister_class_devname(BARO_BASE_DEVICE_PATH, _class_instance); unregister_class_devname(BARO_BASE_DEVICE_PATH, _class_instance);
} }
@@ -88,7 +85,6 @@ LPS22HB::init()
ret = OK; ret = OK;
PX4_INFO("starting");
_measure_interval = LPS22HB_CONVERSION_INTERVAL; _measure_interval = LPS22HB_CONVERSION_INTERVAL;
start(); start();
@@ -174,12 +170,6 @@ LPS22HB::start()
ScheduleNow(); ScheduleNow();
} }
void
LPS22HB::stop()
{
ScheduleClear();
}
int int
LPS22HB::reset() LPS22HB::reset()
{ {
@@ -191,7 +181,7 @@ LPS22HB::reset()
} }
void void
LPS22HB::Run() LPS22HB::RunImpl()
{ {
/* collection phase? */ /* collection phase? */
if (_collect_phase) { if (_collect_phase) {
@@ -324,8 +314,9 @@ LPS22HB::read_reg(uint8_t reg, uint8_t &val)
} }
void void
LPS22HB::print_info() LPS22HB::print_status()
{ {
I2CSPIDriverBase::print_status();
perf_print_counter(_sample_perf); perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors); perf_print_counter(_comms_errors);
+12 -28
View File
@@ -42,6 +42,7 @@
#include <lib/cdev/CDev.hpp> #include <lib/cdev/CDev.hpp>
#include <perf/perf_counter.h> #include <perf/perf_counter.h>
#include <px4_platform_common/getopt.h> #include <px4_platform_common/getopt.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <systemlib/err.h> #include <systemlib/err.h>
#include <uORB/uORB.h> #include <uORB/uORB.h>
@@ -77,24 +78,26 @@ static constexpr uint8_t TEMP_OUT_L = 0x2B;
static constexpr uint8_t TEMP_OUT_H = 0x2C; static constexpr uint8_t TEMP_OUT_H = 0x2C;
/* interface factories */ /* interface factories */
extern device::Device *LPS22HB_SPI_interface(int bus); extern device::Device *LPS22HB_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode);
extern device::Device *LPS22HB_I2C_interface(int bus); extern device::Device *LPS22HB_I2C_interface(int bus, int bus_frequency);
typedef device::Device *(*LPS22HB_constructor)(int);
class LPS22HB : public cdev::CDev, public px4::ScheduledWorkItem class LPS22HB : public cdev::CDev, public I2CSPIDriver<LPS22HB>
{ {
public: public:
LPS22HB(device::Device *interface, const char *path); LPS22HB(I2CSPIBusOption bus_option, int bus, device::Device *interface);
virtual ~LPS22HB(); virtual ~LPS22HB();
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
virtual int init(); virtual int init();
virtual int ioctl(struct file *filp, int cmd, unsigned long arg); virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
/** void print_status();
* Diagnostics - print some basic information about the driver.
*/ void RunImpl();
void print_info();
protected: protected:
device::Device *_interface; device::Device *_interface;
@@ -122,30 +125,11 @@ private:
*/ */
void start(); void start();
/**
* Stop the automatic measurement state machine.
*/
void stop();
/** /**
* Reset the device * Reset the device
*/ */
int reset(); int reset();
/**
* Perform a poll cycle; collect from the previous measurement
* and start a new one.
*
* This is the heart of the measurement state machine. This function
* alternately starts a measurement, or collects the data from the
* previous measurement.
*
* When the interval between measurements is greater than the minimum
* measurement interval, a gap is inserted between collection
* and measurement to provide the most recent measurement possible
* at the next interval.
*/
void Run() override;
/** /**
* Write a register. * Write a register.
@@ -41,12 +41,12 @@
#define LPS22HB_ADDRESS 0x5D #define LPS22HB_ADDRESS 0x5D
device::Device *LPS22HB_I2C_interface(int bus); device::Device *LPS22HB_I2C_interface(int bus, int bus_frequency);
class LPS22HB_I2C : public device::I2C class LPS22HB_I2C : public device::I2C
{ {
public: public:
LPS22HB_I2C(int bus); LPS22HB_I2C(int bus, int bus_frequency);
virtual ~LPS22HB_I2C() = default; virtual ~LPS22HB_I2C() = default;
virtual int read(unsigned address, void *data, unsigned count); virtual int read(unsigned address, void *data, unsigned count);
@@ -58,13 +58,13 @@ protected:
}; };
device::Device * device::Device *
LPS22HB_I2C_interface(int bus) LPS22HB_I2C_interface(int bus, int bus_frequency)
{ {
return new LPS22HB_I2C(bus); return new LPS22HB_I2C(bus, bus_frequency);
} }
LPS22HB_I2C::LPS22HB_I2C(int bus) : LPS22HB_I2C::LPS22HB_I2C(int bus, int bus_frequency) :
I2C("LPS22HB_I2C", nullptr, bus, LPS22HB_ADDRESS, 400000) I2C("LPS22HB_I2C", nullptr, bus, LPS22HB_ADDRESS, bus_frequency)
{ {
} }
@@ -39,18 +39,16 @@
#include "LPS22HB.hpp" #include "LPS22HB.hpp"
#ifdef PX4_SPIDEV_LPS22HB
/* SPI protocol address bits */ /* SPI protocol address bits */
#define DIR_READ (1<<7) #define DIR_READ (1<<7)
#define DIR_WRITE (0<<7) #define DIR_WRITE (0<<7)
device::Device *LPS22HB_SPI_interface(int bus); device::Device *LPS22HB_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode);
class LPS22HB_SPI : public device::SPI class LPS22HB_SPI : public device::SPI
{ {
public: public:
LPS22HB_SPI(int bus, uint32_t device); LPS22HB_SPI(int bus, uint32_t device, int bus_frequency, spi_mode_e spi_mode);
virtual ~LPS22HB_SPI() = default; virtual ~LPS22HB_SPI() = default;
virtual int init(); virtual int init();
@@ -60,12 +58,13 @@ public:
}; };
device::Device * device::Device *
LPS22HB_SPI_interface(int bus) LPS22HB_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode)
{ {
return new LPS22HB_SPI(bus, PX4_SPIDEV_LPS22HB); return new LPS22HB_SPI(bus, devid, bus_frequency, spi_mode);
} }
LPS22HB_SPI::LPS22HB_SPI(int bus, uint32_t device) : SPI("LPS22HB_SPI", nullptr, bus, device, SPIDEV_MODE3, 10000000) LPS22HB_SPI::LPS22HB_SPI(int bus, uint32_t device, int bus_frequency, spi_mode_e spi_mode)
: SPI("LPS22HB_SPI", nullptr, bus, device, spi_mode, bus_frequency)
{ {
_device_id.devid_s.devtype = DRV_BARO_DEVTYPE_LPS22HB; _device_id.devid_s.devtype = DRV_BARO_DEVTYPE_LPS22HB;
} }
@@ -126,5 +125,3 @@ LPS22HB_SPI::read(unsigned address, void *data, unsigned count)
memcpy(data, &buf[1], count); memcpy(data, &buf[1], count);
return ret; return ret;
} }
#endif /* PX4_SPIDEV_LPS22HB */
+46 -228
View File
@@ -32,268 +32,86 @@
****************************************************************************/ ****************************************************************************/
#include "LPS22HB.hpp" #include "LPS22HB.hpp"
#include <px4_platform_common/module.h>
extern "C" __EXPORT int lps22hb_main(int argc, char *argv[]); extern "C" __EXPORT int lps22hb_main(int argc, char *argv[]);
enum LPS22HB_BUS {
LPS22HB_BUS_ALL = 0,
LPS22HB_BUS_I2C_INTERNAL,
LPS22HB_BUS_I2C_EXTERNAL,
LPS22HB_BUS_SPI
};
/** void
* Local functions in support of the shell command. LPS22HB::print_usage()
*/
namespace lps22hb
{ {
PRINT_MODULE_USAGE_NAME("lps22hb", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("baro");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
struct lps22hb_bus_option { I2CSPIDriverBase *LPS22HB::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
enum LPS22HB_BUS busid; int runtime_instance)
const char *devpath;
LPS22HB_constructor interface_constructor;
uint8_t busnum;
LPS22HB *dev;
} bus_options[] = {
{ LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION, NULL },
#ifdef PX4_I2C_BUS_EXPANSION1
{ LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext1", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION1, NULL },
#endif
#ifdef PX4_I2C_BUS_EXPANSION2
{ LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext2", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION2, NULL },
#endif
#ifdef PX4_I2C_BUS_ONBOARD
{ LPS22HB_BUS_I2C_INTERNAL, "/dev/lps22hb_int", &LPS22HB_I2C_interface, PX4_I2C_BUS_ONBOARD, NULL },
#endif
#ifdef PX4_SPIDEV_LPS22HB
{ LPS22HB_BUS_SPI, "/dev/lps22hb_spi", &LPS22HB_SPI_interface, PX4_SPI_BUS_SENSOR4, NULL },
#endif
};
#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
int start(enum LPS22HB_BUS busid);
bool start_bus(struct lps22hb_bus_option &bus);
struct lps22hb_bus_option &find_bus(enum LPS22HB_BUS busid);
int reset(enum LPS22HB_BUS busid);
int info();
void usage();
/**
* start driver for a specific bus option
*/
bool
start_bus(struct lps22hb_bus_option &bus)
{ {
PX4_INFO("starting %s", bus.devpath); device::Device *interface = nullptr;
if (bus.dev != nullptr) { if (iterator.busType() == BOARD_I2C_BUS) {
PX4_WARN("bus option already started"); interface = LPS22HB_I2C_interface(iterator.bus(), cli.bus_frequency);
return false;
} else if (iterator.busType() == BOARD_SPI_BUS) {
interface = LPS22HB_SPI_interface(iterator.bus(), iterator.devid(), cli.bus_frequency, cli.spi_mode);
} }
device::Device *interface = bus.interface_constructor(bus.busnum); if (interface == nullptr) {
PX4_ERR("failed creating interface for bus %i (devid 0x%x)", iterator.bus(), iterator.devid());
return nullptr;
}
if (interface->init() != OK) { if (interface->init() != OK) {
delete interface; delete interface;
PX4_WARN("no device on bus %u", (unsigned)bus.busid); PX4_DEBUG("no device on bus %i (devid 0x%x)", iterator.bus(), iterator.devid());
return false; return nullptr;
} }
bus.dev = new LPS22HB(interface, bus.devpath); LPS22HB *dev = new LPS22HB(iterator.configuredBusOption(), iterator.bus(), interface);
if (bus.dev != nullptr && OK != bus.dev->init()) { if (dev == nullptr) {
PX4_WARN("init failed"); delete interface;
delete bus.dev; return nullptr;
bus.dev = nullptr;
return false;
} }
int fd = px4_open(bus.devpath, O_RDONLY); if (OK != dev->init()) {
delete dev;
/* set the poll rate to default, starts automatic data collection */ return nullptr;
if (fd == -1) {
PX4_ERR("can't open baro device");
return false;
} }
if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { return dev;
px4_close(fd);
PX4_ERR("failed setting default poll rate");
return false;
}
px4_close(fd);
return true;
} }
extern "C" int lps22hb_main(int argc, char *argv[])
/**
* Start the driver.
*
* This function call only returns once the driver
* is either successfully up and running or failed to start.
*/
int
start(enum LPS22HB_BUS busid)
{ {
bool started = false; using ThisDriver = LPS22HB;
BusCLIArguments cli{true, true};
cli.default_spi_frequency = 10 * 1000 * 1000;
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { const char *verb = cli.parseDefaultArguments(argc, argv);
if (busid == LPS22HB_BUS_ALL && bus_options[i].dev != NULL) {
// this device is already started
continue;
}
if (busid != LPS22HB_BUS_ALL && bus_options[i].busid != busid) { if (!verb) {
// not the one that is asked for ThisDriver::print_usage();
continue;
}
started |= start_bus(bus_options[i]);
}
if (!started) {
return PX4_ERROR;
}
return PX4_OK;
}
/**
* find a bus structure for a busid
*/
struct lps22hb_bus_option &find_bus(enum LPS22HB_BUS busid)
{
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
if ((busid == LPS22HB_BUS_ALL ||
busid == bus_options[i].busid) && bus_options[i].dev != NULL) {
return bus_options[i];
}
}
errx(1, "bus %u not started", (unsigned)busid);
}
/**
* Reset the driver.
*/
int
reset(enum LPS22HB_BUS busid)
{
struct lps22hb_bus_option &bus = find_bus(busid);
const char *path = bus.devpath;
int fd = open(path, O_RDONLY);
if (fd < 0) {
PX4_ERR("failed");
return PX4_ERROR;
}
if (px4_ioctl(fd, SENSORIOCRESET, 0) < 0) {
PX4_ERR("driver reset failed");
return PX4_ERROR;
}
if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
PX4_ERR("driver poll restart failed");
return PX4_ERROR;
}
return 0;
}
/**
* Print a little info about the driver.
*/
int
info()
{
for (uint8_t i = 0; i < NUM_BUS_OPTIONS; i++) {
struct lps22hb_bus_option &bus = bus_options[i];
if (bus.dev != nullptr) {
warnx("%s", bus.devpath);
bus.dev->print_info();
}
}
return 0;
}
void
usage()
{
PX4_INFO("missing command: try 'start', 'info', 'reset'");
PX4_INFO("options:");
PX4_INFO(" -X (external I2C bus)");
PX4_INFO(" -I (internal I2C bus)");
PX4_INFO(" -S (external SPI bus)");
}
} // namespace
int
lps22hb_main(int argc, char *argv[])
{
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
enum LPS22HB_BUS busid = LPS22HB_BUS_ALL;
while ((ch = px4_getopt(argc, argv, "IXS", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
#if (PX4_I2C_BUS_ONBOARD)
case 'I':
busid = LPS22HB_BUS_I2C_INTERNAL;
break;
#endif /* PX4_I2C_BUS_ONBOARD */
case 'X':
busid = LPS22HB_BUS_I2C_EXTERNAL;
break;
case 'S':
busid = LPS22HB_BUS_SPI;
break;
default:
lps22hb::usage();
return 0;
}
}
if (myoptind >= argc) {
lps22hb::usage();
return -1; return -1;
} }
const char *verb = argv[myoptind]; BusInstanceIterator iterator(MODULE_NAME, cli, DRV_BARO_DEVTYPE_LPS22HB);
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start")) { if (!strcmp(verb, "start")) {
return lps22hb::start(busid); return ThisDriver::module_start(cli, iterator);
} }
/* if (!strcmp(verb, "stop")) {
* Reset the driver. return ThisDriver::module_stop(iterator);
*/
if (!strcmp(verb, "reset")) {
return lps22hb::reset(busid);
} }
/* if (!strcmp(verb, "status")) {
* Print driver information. return ThisDriver::module_status(iterator);
*/
if (!strcmp(verb, "info")) {
return lps22hb::info();
} }
PX4_WARN("unrecognised command, try 'start', 'reset' or 'info'"); ThisDriver::print_usage();
return 0; return -1;
} }