refactor mpl3115a2: use driver base class

This commit is contained in:
Beat Küng 2020-02-26 19:58:45 +01:00 committed by Daniel Agar
parent d81fedfcfa
commit dc5ffb43a3
3 changed files with 53 additions and 161 deletions

View File

@ -54,9 +54,9 @@
#define MPL3115A2_OSR 2 /* Over Sample rate of 4 18MS Minimum time between data samples */
#define MPL3115A2_CTRL_TRIGGER (CTRL_REG1_OST | CTRL_REG1_OS(MPL3115A2_OSR))
MPL3115A2::MPL3115A2(const int bus) :
I2C("MPL3115A2", nullptr, bus, MPL3115A2_ADDRESS, 400000),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())),
MPL3115A2::MPL3115A2(I2CSPIBusOption bus_option, const int bus, int bus_frequency) :
I2C("MPL3115A2", nullptr, bus, MPL3115A2_ADDRESS, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus),
_px4_barometer(get_device_id()),
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")),
_measure_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": measure")),
@ -68,8 +68,6 @@ MPL3115A2::MPL3115A2(const int bus) :
MPL3115A2::~MPL3115A2()
{
stop();
perf_free(_sample_perf);
perf_free(_measure_perf);
perf_free(_comms_errors);
@ -129,11 +127,6 @@ void MPL3115A2::start()
ScheduleNow();
}
void MPL3115A2::stop()
{
ScheduleClear();
}
int MPL3115A2::reset()
{
int max = 10;
@ -149,7 +142,7 @@ int MPL3115A2::reset()
return ret == 1 ? PX4_OK : ret;
}
void MPL3115A2::Run()
void MPL3115A2::RunImpl()
{
int ret = PX4_ERROR;
@ -243,7 +236,6 @@ int MPL3115A2::collect()
}
/* read the most recent measurement
* 3 Pressure and 2 temprtture
*/
@ -289,8 +281,9 @@ int MPL3115A2::collect()
return PX4_OK;
}
void MPL3115A2::print_info()
void MPL3115A2::print_status()
{
I2CSPIDriverBase::print_status();
perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors);

View File

@ -46,27 +46,30 @@
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/log.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <px4_platform_common/i2c_spi_buses.h>
class MPL3115A2 : public device::I2C, public px4::ScheduledWorkItem
class MPL3115A2 : public device::I2C, public I2CSPIDriver<MPL3115A2>
{
public:
MPL3115A2(const int bus);
MPL3115A2(I2CSPIBusOption bus_option, const int bus, int bus_frequency);
~MPL3115A2() override;
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
int init() override;
int probe() override;
void print_info();
void print_status();
void RunImpl();
private:
void start();
void stop();
int reset();
void Run() override;
int measure();
int collect();

View File

@ -34,165 +34,61 @@
#include "MPL3115A2.hpp"
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
enum class MPL3115A2_BUS {
ALL = 0,
I2C_INTERNAL,
I2C_EXTERNAL,
};
namespace mpl3115a2
void
MPL3115A2::print_usage()
{
struct mpl3115a2_bus_option {
MPL3115A2_BUS busid;
uint8_t busnum;
MPL3115A2 *dev;
} bus_options[] = {
#if defined(PX4_I2C_BUS_ONBOARD)
{ MPL3115A2_BUS::I2C_INTERNAL, PX4_I2C_BUS_ONBOARD, nullptr },
#endif
#if defined(PX4_I2C_BUS_EXPANSION)
{ MPL3115A2_BUS::I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION, nullptr },
#endif
#if defined(PX4_I2C_BUS_EXPANSION1)
{ MPL3115A2_BUS::I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION1, nullptr },
#endif
#if defined(PX4_I2C_BUS_EXPANSION2)
{ MPL3115A2_BUS::I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION2, nullptr },
#endif
};
// find a bus structure for a busid
static mpl3115a2_bus_option *find_bus(MPL3115A2_BUS busid)
{
for (mpl3115a2_bus_option &bus_option : bus_options) {
if ((busid == MPL3115A2_BUS::ALL ||
busid == bus_option.busid) && bus_option.dev != nullptr) {
return &bus_option;
}
}
return nullptr;
PRINT_MODULE_USAGE_NAME("mpl3115a2", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("baro");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
static bool start_bus(mpl3115a2_bus_option &bus)
I2CSPIDriverBase *MPL3115A2::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance)
{
MPL3115A2 *dev = new MPL3115A2(bus.busnum);
MPL3115A2 *dev = new MPL3115A2(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency);
if (dev == nullptr || (dev->init() != PX4_OK)) {
PX4_ERR("driver start failed");
if (dev == nullptr) {
return nullptr;
}
if (OK != dev->init()) {
delete dev;
return false;
return nullptr;
}
bus.dev = dev;
return true;
return dev;
}
static int start(MPL3115A2_BUS busid)
{
for (mpl3115a2_bus_option &bus_option : bus_options) {
if (bus_option.dev != nullptr) {
// this device is already started
PX4_WARN("already started");
continue;
}
if (busid != MPL3115A2_BUS::ALL && bus_option.busid != busid) {
// not the one that is asked for
continue;
}
if (start_bus(bus_option)) {
return PX4_OK;
}
}
return PX4_ERROR;
}
static int stop(MPL3115A2_BUS busid)
{
mpl3115a2_bus_option *bus = find_bus(busid);
if (bus != nullptr && bus->dev != nullptr) {
delete bus->dev;
bus->dev = nullptr;
} else {
PX4_WARN("driver not running");
return PX4_ERROR;
}
return PX4_OK;
}
static int status(MPL3115A2_BUS busid)
{
mpl3115a2_bus_option *bus = find_bus(busid);
if (bus != nullptr && bus->dev != nullptr) {
bus->dev->print_info();
return PX4_OK;
}
PX4_WARN("driver not running");
return PX4_ERROR;
}
static int usage()
{
PX4_INFO("missing command: try 'start', 'stop', 'status'");
PX4_INFO("options:");
PX4_INFO(" -X (i2c external bus)");
PX4_INFO(" -I (i2c internal bus)");
return 0;
}
} // namespace
extern "C" int mpl3115a2_main(int argc, char *argv[])
{
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
using ThisDriver = MPL3115A2;
BusCLIArguments cli{true, false};
const char *verb = cli.parseDefaultArguments(argc, argv);
MPL3115A2_BUS busid = MPL3115A2_BUS::ALL;
while ((ch = px4_getopt(argc, argv, "XI", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'X':
busid = MPL3115A2_BUS::I2C_EXTERNAL;
break;
case 'I':
busid = MPL3115A2_BUS::I2C_INTERNAL;
break;
default:
return mpl3115a2::usage();
}
if (!verb) {
ThisDriver::print_usage();
return -1;
}
if (myoptind >= argc) {
return mpl3115a2::usage();
}
const char *verb = argv[myoptind];
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_BARO_DEVTYPE_MPL3115A2);
if (!strcmp(verb, "start")) {
return mpl3115a2::start(busid);
} else if (!strcmp(verb, "stop")) {
return mpl3115a2::stop(busid);
} else if (!strcmp(verb, "status")) {
return mpl3115a2::status(busid);
return ThisDriver::module_start(cli, iterator);
}
return mpl3115a2::usage();
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}