refactor l3gd20: use driver base class

This commit is contained in:
Beat Küng
2020-03-18 17:34:55 +01:00
committed by Daniel Agar
parent 6cf4de9e02
commit 609eafd4cd
6 changed files with 89 additions and 174 deletions
@@ -16,7 +16,7 @@ qmc5883 -I -R 12 start
mpu6000 -s -R 8 start mpu6000 -s -R 8 start
mpu9250 -s -R 8 start mpu9250 -s -R 8 start
lsm303d -s -R 10 start lsm303d -s -R 10 start
l3gd20 -R 14 start l3gd20 -s -R 14 start
# Internal SPI # Internal SPI
ms5611 -s start ms5611 -s start
+3 -3
View File
@@ -70,8 +70,8 @@ then
# sensor heating is available, but we disable it for now # sensor heating is available, but we disable it for now
param set SENS_EN_THERMAL 0 param set SENS_EN_THERMAL 0
# external L3GD20H is rotated 180 degrees yaw # (external) L3GD20H is rotated 180 degrees yaw
l3gd20 -X -R 4 start l3gd20 -s -R 4 start
# (external) LSM303D is rotated 270 degrees yaw # (external) LSM303D is rotated 270 degrees yaw
lsm303d -s -R 6 start lsm303d -s -R 6 start
@@ -105,7 +105,7 @@ else
# else: On the PixhawkMini the mpu9250 has been disabled due to HW errata # else: On the PixhawkMini the mpu9250 has been disabled due to HW errata
fi fi
l3gd20 start l3gd20 -s start
lsm303d -s start lsm303d -s start
fi fi
+3 -3
View File
@@ -78,8 +78,8 @@ then
ak09916 -X -R 6 start ak09916 -X -R 6 start
fi fi
# external L3GD20H is rotated 180 degrees yaw # (external) L3GD20H is rotated 180 degrees yaw
l3gd20 -X -R 4 start l3gd20 -s -R 4 start
# (external) LSM303D is rotated 270 degrees yaw # (external) LSM303D is rotated 270 degrees yaw
lsm303d -s -R 6 start lsm303d -s -R 6 start
@@ -113,7 +113,7 @@ else
# else: On the PixhawkMini the mpu9250 has been disabled due to HW errata # else: On the PixhawkMini the mpu9250 has been disabled due to HW errata
fi fi
l3gd20 start l3gd20 -s start
lsm303d -s start lsm303d -s start
fi fi
+7 -25
View File
@@ -35,9 +35,10 @@
constexpr uint8_t L3GD20::_checked_registers[]; constexpr uint8_t L3GD20::_checked_registers[];
L3GD20::L3GD20(int bus, uint32_t device, enum Rotation rotation) : L3GD20::L3GD20(I2CSPIBusOption bus_option, int bus, uint32_t device, enum Rotation rotation, int bus_frequency,
SPI("L3GD20", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000), spi_mode_e spi_mode) :
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(this->get_device_id())), SPI("L3GD20", nullptr, bus, device, spi_mode, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus),
_px4_gyro(get_device_id(), ORB_PRIO_DEFAULT, rotation), _px4_gyro(get_device_id(), ORB_PRIO_DEFAULT, rotation),
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")), _sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")),
_errors(perf_alloc(PC_COUNT, MODULE_NAME": err")), _errors(perf_alloc(PC_COUNT, MODULE_NAME": err")),
@@ -49,10 +50,6 @@ L3GD20::L3GD20(int bus, uint32_t device, enum Rotation rotation) :
L3GD20::~L3GD20() L3GD20::~L3GD20()
{ {
/* make sure we are truly inactive */
stop();
/* delete the perf counter */
perf_free(_sample_perf); perf_free(_sample_perf);
perf_free(_errors); perf_free(_errors);
perf_free(_bad_registers); perf_free(_bad_registers);
@@ -226,20 +223,11 @@ L3GD20::set_samplerate(unsigned frequency)
void void
L3GD20::start() L3GD20::start()
{ {
/* make sure we are stopped first */
stop();
/* start polling at the specified rate */ /* start polling at the specified rate */
uint64_t interval = 1000000 / L3GD20_DEFAULT_RATE; uint64_t interval = 1000000 / L3GD20_DEFAULT_RATE;
ScheduleOnInterval(interval - L3GD20_TIMER_REDUCTION, 10000); ScheduleOnInterval(interval - L3GD20_TIMER_REDUCTION, 10000);
} }
void
L3GD20::stop()
{
ScheduleClear();
}
void void
L3GD20::disable_i2c() L3GD20::disable_i2c()
{ {
@@ -288,13 +276,6 @@ L3GD20::reset()
_read = 0; _read = 0;
} }
void
L3GD20::Run()
{
/* make another measurement */
measure();
}
void void
L3GD20::check_registers() L3GD20::check_registers()
{ {
@@ -326,7 +307,7 @@ L3GD20::check_registers()
} }
void void
L3GD20::measure() L3GD20::RunImpl()
{ {
/* status register and data as read back from the device */ /* status register and data as read back from the device */
#pragma pack(push, 1) #pragma pack(push, 1)
@@ -412,8 +393,9 @@ L3GD20::measure()
} }
void void
L3GD20::print_info() L3GD20::print_status()
{ {
I2CSPIDriverBase::print_status();
printf("gyro reads: %u\n", _read); printf("gyro reads: %u\n", _read);
perf_print_counter(_sample_perf); perf_print_counter(_sample_perf);
perf_print_counter(_errors); perf_print_counter(_errors);
+15 -17
View File
@@ -44,7 +44,7 @@
#include <lib/drivers/gyroscope/PX4Gyroscope.hpp> #include <lib/drivers/gyroscope/PX4Gyroscope.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/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/i2c_spi_buses.h>
/* Orientation on board */ /* Orientation on board */
#define SENSOR_BOARD_ROTATION_000_DEG 0 #define SENSOR_BOARD_ROTATION_000_DEG 0
@@ -158,18 +158,22 @@
*/ */
#define L3GD20_TIMER_REDUCTION 600 #define L3GD20_TIMER_REDUCTION 600
class L3GD20 : public device::SPI, public px4::ScheduledWorkItem class L3GD20 : public device::SPI, public I2CSPIDriver<L3GD20>
{ {
public: public:
L3GD20(int bus, uint32_t device, enum Rotation rotation); L3GD20(I2CSPIBusOption bus_option, int bus, uint32_t device, enum Rotation rotation, int bus_frequency,
virtual ~L3GD20(); spi_mode_e spi_mode);
~L3GD20() override;
virtual int init(); static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
/** void RunImpl();
* Diagnostics - print some basic information about the driver.
*/ int init() override;
void print_info();
void print_status() override;
// print register dump // print register dump
void print_registers(); void print_registers();
@@ -178,7 +182,8 @@ public:
void test_error(); void test_error();
protected: protected:
virtual int probe(); void custom_method(const BusCLIArguments &cli) override;
int probe() override;
private: private:
@@ -217,21 +222,14 @@ private:
uint8_t _checked_next{0}; uint8_t _checked_next{0};
void start(); void start();
void stop();
void reset(); void reset();
void disable_i2c(); void disable_i2c();
void Run() override;
/** /**
* check key registers for correct values * check key registers for correct values
*/ */
void check_registers(); void check_registers();
/**
* Fetch measurements from the sensor and update the report ring.
*/
void measure();
/** /**
* Read a register from the L3GD20 * Read a register from the L3GD20
* *
+60 -125
View File
@@ -33,161 +33,96 @@
#include "L3GD20.hpp" #include "L3GD20.hpp"
/** #include <px4_platform_common/getopt.h>
* Local functions in support of the shell command. #include <px4_platform_common/module.h>
*/
namespace l3gd20 void
L3GD20::print_usage()
{ {
PRINT_MODULE_USAGE_NAME("l3gd20", "driver");
L3GD20 *g_dev{nullptr}; PRINT_MODULE_USAGE_SUBCATEGORY("imu");
PRINT_MODULE_USAGE_COMMAND("start");
/** PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
* Start the driver. PRINT_MODULE_USAGE_PARAM_INT('R', 0, 0, 35, "Rotation", true);
* PRINT_MODULE_USAGE_COMMAND("regdump");
* This function call only returns once the driver PRINT_MODULE_USAGE_COMMAND("testerror");
* started or failed to detect the sensor. PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
*/
static void start(bool external_bus, enum Rotation rotation)
{
if (g_dev != nullptr) {
errx(0, "already started");
}
/* create the driver */
if (external_bus) {
#if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_GYRO)
g_dev = new L3GD20(PX4_SPI_BUS_EXT, PX4_SPIDEV_EXT_GYRO, rotation);
#else
errx(0, "External SPI not available");
#endif
} else {
g_dev = new L3GD20(PX4_SPI_BUS_SENSORS, PX4_SPIDEV_GYRO, rotation);
}
if (g_dev == nullptr) {
goto fail;
}
if (OK != g_dev->init()) {
goto fail;
}
exit(0);
fail:
if (g_dev != nullptr) {
delete g_dev;
g_dev = nullptr;
}
errx(1, "driver start failed");
} }
/** I2CSPIDriverBase *L3GD20::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
* Print a little info about the driver. int runtime_instance)
*/
static void info()
{ {
if (g_dev == nullptr) { L3GD20 *instance = new L3GD20(iterator.configuredBusOption(), iterator.bus(), iterator.devid(), cli.rotation,
errx(1, "driver not running\n"); cli.bus_frequency, cli.spi_mode);
if (!instance) {
PX4_ERR("alloc failed");
return nullptr;
} }
printf("state @ %p\n", g_dev); if (OK != instance->init()) {
g_dev->print_info(); delete instance;
return nullptr;
exit(0);
}
/**
* Dump the register information
*/
static void regdump()
{
if (g_dev == nullptr) {
errx(1, "driver not running");
} }
printf("regdump @ %p\n", g_dev); return instance;
g_dev->print_registers();
exit(0);
} }
/** void L3GD20::custom_method(const BusCLIArguments &cli)
* trigger an error
*/
static void test_error()
{ {
if (g_dev == nullptr) { switch (cli.custom1) {
errx(1, "driver not running"); case 0: print_registers(); break;
case 1: test_error(); break;
} }
printf("regdump @ %p\n", g_dev);
g_dev->test_error();
exit(0);
} }
static void usage() extern "C" int l3gd20_main(int argc, char *argv[])
{ {
warnx("missing command: try 'start', 'info', 'testerror' or 'regdump'");
warnx("options:");
warnx(" -X (external bus)");
warnx(" -R rotation");
}
} // namespace
extern "C" __EXPORT int l3gd20_main(int argc, char *argv[])
{
int myoptind = 1;
int ch; int ch;
const char *myoptarg = nullptr; using ThisDriver = L3GD20;
bool external_bus = false; BusCLIArguments cli{false, true};
enum Rotation rotation = ROTATION_NONE; cli.default_spi_frequency = 11 * 1000 * 1000;
while ((ch = px4_getopt(argc, argv, "XR:", &myoptind, &myoptarg)) != EOF) { while ((ch = cli.getopt(argc, argv, "R:")) != EOF) {
switch (ch) { switch (ch) {
case 'X':
external_bus = true;
break;
case 'R': case 'R':
rotation = (enum Rotation)atoi(myoptarg); cli.rotation = (enum Rotation)atoi(cli.optarg());
break; break;
default:
l3gd20::usage();
return 0;
} }
} }
if (myoptind >= argc) { const char *verb = cli.optarg();
l3gd20::usage();
if (!verb) {
ThisDriver::print_usage();
return -1; return -1;
} }
const char *verb = argv[myoptind]; BusInstanceIterator iterator(MODULE_NAME, cli, DRV_GYR_DEVTYPE_L3GD20);
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start")) { if (!strcmp(verb, "start")) {
l3gd20::start(external_bus, rotation); return ThisDriver::module_start(cli, iterator);
} else if (!strcmp(verb, "info")) {
l3gd20::info();
} else if (!strcmp(verb, "regdump")) {
l3gd20::regdump();
} else if (!strcmp(verb, "testerror")) {
l3gd20::test_error();
} }
l3gd20::usage(); if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
if (!strcmp(verb, "regdump")) {
cli.custom1 = 0;
return ThisDriver::module_custom_method(cli, iterator);
}
if (!strcmp(verb, "testerror")) {
cli.custom1 = 1;
return ThisDriver::module_custom_method(cli, iterator);
}
ThisDriver::print_usage();
return -1; return -1;
} }