refactor lsm303agr: use driver base class

This commit is contained in:
Beat Küng
2020-03-24 09:46:20 -04:00
committed by Daniel Agar
parent 4d511868e6
commit 74db018012
4 changed files with 70 additions and 172 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ adis16477 -s -R 8 start
lps22hb -s start lps22hb -s start
lsm303agr -R 4 start lsm303agr -s -R 4 start
ms4525_airspeed -T 4515 -I -b 3 start ms4525_airspeed -T 4515 -I -b 3 start
@@ -61,9 +61,10 @@ static constexpr uint8_t LSM303AGR_WHO_AM_I_M = 0x40;
*/ */
#define LSM303AGR_TIMER_REDUCTION 200 #define LSM303AGR_TIMER_REDUCTION 200
LSM303AGR::LSM303AGR(int bus, const char *path, uint32_t device, enum Rotation rotation) : LSM303AGR::LSM303AGR(I2CSPIBusOption bus_option, int bus, int device, enum Rotation rotation, int bus_frequency,
SPI("LSM303AGR", path, bus, device, SPIDEV_MODE3, 8 * 1000 * 1000), spi_mode_e spi_mode) :
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())), SPI("LSM303AGR", nullptr, bus, device, spi_mode, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus),
_mag_sample_perf(perf_alloc(PC_ELAPSED, "LSM303AGR_mag_read")), _mag_sample_perf(perf_alloc(PC_ELAPSED, "LSM303AGR_mag_read")),
_bad_registers(perf_alloc(PC_COUNT, "LSM303AGR_bad_reg")), _bad_registers(perf_alloc(PC_COUNT, "LSM303AGR_bad_reg")),
_bad_values(perf_alloc(PC_COUNT, "LSM303AGR_bad_val")), _bad_values(perf_alloc(PC_COUNT, "LSM303AGR_bad_val")),
@@ -83,9 +84,6 @@ LSM303AGR::LSM303AGR(int bus, const char *path, uint32_t device, enum Rotation r
LSM303AGR::~LSM303AGR() LSM303AGR::~LSM303AGR()
{ {
/* make sure we are truly inactive */
stop();
if (_class_instance != -1) { if (_class_instance != -1) {
unregister_class_devname(MAG_BASE_DEVICE_PATH, _class_instance); unregister_class_devname(MAG_BASE_DEVICE_PATH, _class_instance);
} }
@@ -99,12 +97,12 @@ LSM303AGR::~LSM303AGR()
int int
LSM303AGR::init() LSM303AGR::init()
{ {
int ret = PX4_OK;
/* do SPI init (and probe) first */ /* do SPI init (and probe) first */
if (SPI::init() != OK) { int ret = SPI::init();
PX4_ERR("SPI init failed");
return PX4_ERROR; if (ret != OK) {
DEVICE_DEBUG("SPI init failed (%i)", ret);
return ret;
} }
_class_instance = register_class_devname(MAG_BASE_DEVICE_PATH); _class_instance = register_class_devname(MAG_BASE_DEVICE_PATH);
@@ -118,6 +116,9 @@ LSM303AGR::init()
/* fill report structures */ /* fill report structures */
measure(); measure();
_measure_interval = CONVERSION_INTERVAL;
start();
return ret; return ret;
} }
@@ -350,17 +351,7 @@ LSM303AGR::start()
} }
void void
LSM303AGR::stop() LSM303AGR::RunImpl()
{
if (_measure_interval > 0) {
/* ensure no new items are queued while we cancel this one */
_measure_interval = 0;
ScheduleClear();
}
}
void
LSM303AGR::Run()
{ {
if (_measure_interval == 0) { if (_measure_interval == 0) {
return; return;
@@ -476,8 +467,9 @@ LSM303AGR::collect()
} }
void void
LSM303AGR::print_info() LSM303AGR::print_status()
{ {
I2CSPIDriverBase::print_status();
perf_print_counter(_mag_sample_perf); perf_print_counter(_mag_sample_perf);
perf_print_counter(_bad_registers); perf_print_counter(_bad_registers);
perf_print_counter(_bad_values); perf_print_counter(_bad_values);
@@ -31,8 +31,7 @@
* *
****************************************************************************/ ****************************************************************************/
#ifndef DRIVERS_IMU_LSM303AGR_LSM303AGR_HPP_ #pragma once
#define DRIVERS_IMU_LSM303AGR_LSM303AGR_HPP_
#include <drivers/drv_hrt.h> #include <drivers/drv_hrt.h>
#include <drivers/device/spi.h> #include <drivers/device/spi.h>
@@ -42,7 +41,7 @@
#include <mathlib/math/filter/LowPassFilter2p.hpp> #include <mathlib/math/filter/LowPassFilter2p.hpp>
#include <lib/conversion/rotation.h> #include <lib/conversion/rotation.h>
#include <perf/perf_counter.h> #include <perf/perf_counter.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/i2c_spi_buses.h>
#include <systemlib/err.h> #include <systemlib/err.h>
// Register mapping // Register mapping
@@ -88,23 +87,26 @@ static constexpr uint8_t OUTY_H_REG_M = 0x6B;
static constexpr uint8_t OUTZ_L_REG_M = 0x6C; static constexpr uint8_t OUTZ_L_REG_M = 0x6C;
static constexpr uint8_t OUTZ_H_REG_M = 0x6D; static constexpr uint8_t OUTZ_H_REG_M = 0x6D;
class LSM303AGR : public device::SPI, public px4::ScheduledWorkItem class LSM303AGR : public device::SPI, public I2CSPIDriver<LSM303AGR>
{ {
public: public:
LSM303AGR(int bus, const char *path, uint32_t device, enum Rotation rotation); LSM303AGR(I2CSPIBusOption bus_option, int bus, int device, enum Rotation rotation, int bus_frequency,
spi_mode_e spi_mode);
virtual ~LSM303AGR(); virtual ~LSM303AGR();
virtual int init(); static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
virtual int ioctl(struct file *filp, int cmd, unsigned long arg); int init() override;
/** int ioctl(struct file *filp, int cmd, unsigned long arg) override;
* Diagnostics - print some basic information about the driver.
*/
void print_info();
void print_status() override;
void RunImpl();
protected: protected:
virtual int probe(); int probe() override;
private: private:
@@ -137,11 +139,6 @@ private:
*/ */
void start(); void start();
/**
* Stop automatic measurement.
*/
void stop();
/** /**
* Reset chip. * Reset chip.
* *
@@ -164,21 +161,6 @@ private:
*/ */
int collect(); int collect();
/**
* 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;
/** /**
* Read a register from the LSM303AGR * Read a register from the LSM303AGR
* *
@@ -200,4 +182,3 @@ private:
LSM303AGR operator=(const LSM303AGR &); LSM303AGR operator=(const LSM303AGR &);
}; };
#endif /* DRIVERS_IMU_LSM303AGR_LSM303AGR_HPP_ */
@@ -33,149 +33,74 @@
#include "LSM303AGR.hpp" #include "LSM303AGR.hpp"
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/getopt.h> #include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
#define LSM303AGR_DEVICE_PATH_MAG "/dev/lsm303agr_mag"
extern "C" { __EXPORT int lsm303agr_main(int argc, char *argv[]); }
/**
* Local functions in support of the shell command.
*/
namespace lsm303agr
{
LSM303AGR *g_dev;
void start(enum Rotation rotation);
void info();
void usage();
/**
* Start the driver.
*
* This function call only returns once the driver is
* up and running or failed to detect the sensor.
*/
void void
start(enum Rotation rotation) LSM303AGR::print_usage()
{ {
int fd_mag = -1; PRINT_MODULE_USAGE_NAME("lsm303agr", "driver");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
PRINT_MODULE_USAGE_PARAM_INT('R', 0, 0, 35, "Rotation", true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
if (g_dev != nullptr) { I2CSPIDriverBase *LSM303AGR::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
errx(0, "already started"); int runtime_instance)
} {
LSM303AGR *instance = new LSM303AGR(iterator.configuredBusOption(), iterator.bus(), iterator.devid(), cli.rotation,
cli.bus_frequency, cli.spi_mode);
/* create the driver */ if (!instance) {
#if defined(PX4_SPIDEV_LSM303A_M) && defined(PX4_SPIDEV_LSM303A_X)
g_dev = new LSM303AGR(PX4_SPI_BUS_SENSOR5, LSM303AGR_DEVICE_PATH_MAG, PX4_SPIDEV_LSM303A_M, rotation);
#else
errx(0, "External SPI not available");
#endif
if (g_dev == nullptr) {
PX4_ERR("alloc failed"); PX4_ERR("alloc failed");
goto fail; return nullptr;
} }
if (OK != g_dev->init()) { if (OK != instance->init()) {
goto fail; delete instance;
return nullptr;
} }
fd_mag = px4_open(LSM303AGR_DEVICE_PATH_MAG, O_RDONLY); return instance;
/* don't fail if open cannot be opened */
if (0 <= fd_mag) {
if (px4_ioctl(fd_mag, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
goto fail;
}
}
px4_close(fd_mag);
exit(0);
fail:
if (g_dev != nullptr) {
delete g_dev;
g_dev = nullptr;
}
errx(1, "driver start failed");
} }
/** extern "C" int lsm303agr_main(int argc, char *argv[])
* Print a little info about the driver.
*/
void
info()
{ {
if (g_dev == nullptr) {
errx(1, "driver not running\n");
}
printf("state @ %p\n", g_dev);
g_dev->print_info();
exit(0);
}
void
usage()
{
PX4_INFO("missing command: try 'start', 'info', 'reset'");
PX4_INFO("options:");
PX4_INFO(" -X (external bus)");
PX4_INFO(" -R rotation");
}
} // namespace
int
lsm303agr_main(int argc, char *argv[])
{
int myoptind = 1;
int ch; int ch;
const char *myoptarg = nullptr; using ThisDriver = LSM303AGR;
BusCLIArguments cli{false, true};
cli.default_spi_frequency = 8 * 1000 * 1000;
enum Rotation rotation = ROTATION_NONE; while ((ch = cli.getopt(argc, argv, "R:")) != EOF) {
/* jump over start/off/etc and look at options first */
while ((ch = px4_getopt(argc, argv, "XR:a:", &myoptind, &myoptarg)) != EOF) {
switch (ch) { switch (ch) {
case 'R': case 'R':
rotation = (enum Rotation)atoi(myoptarg); cli.rotation = (enum Rotation)atoi(cli.optarg());
break; break;
default:
lsm303agr::usage();
exit(0);
} }
} }
if (myoptind >= argc) { const char *verb = cli.optarg();
lsm303agr::usage();
exit(0); if (!verb) {
ThisDriver::print_usage();
return -1;
} }
const char *verb = argv[myoptind]; BusInstanceIterator iterator(MODULE_NAME, cli, DRV_MAG_DEVTYPE_LSM303AGR);
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start")) { if (!strcmp(verb, "start")) {
lsm303agr::start(rotation); return ThisDriver::module_start(cli, iterator);
} }
/* if (!strcmp(verb, "stop")) {
* Print driver information. return ThisDriver::module_stop(iterator);
*/
if (!strcmp(verb, "info")) {
lsm303agr::info();
} }
errx(1, "unrecognized command, try 'start', 'info'"); if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
} }