mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-02 03:49:12 +08:00
refactor lis3mdl: use driver base class
This commit is contained in:
@@ -28,7 +28,7 @@ ms5611 -s start
|
||||
if ! hmc5883 -T -S -R 2 start
|
||||
then
|
||||
# lis3mdl internal SPI bus is rotated 90 deg yaw
|
||||
lis3mdl start
|
||||
lis3mdl -s start
|
||||
fi
|
||||
|
||||
# Start either ICM2060X. They are both connected to the same SPI bus and use the same
|
||||
|
||||
@@ -17,7 +17,7 @@ icm20602 -s -R 8 start
|
||||
mpu9250 -s -R 2 start
|
||||
|
||||
# Internal SPI bus
|
||||
lis3mdl -R 0 start
|
||||
lis3mdl -s -R 0 start
|
||||
|
||||
# Possible external compasses
|
||||
hmc5883 -T -X start
|
||||
|
||||
@@ -42,9 +42,9 @@
|
||||
#include <px4_platform_common/time.h>
|
||||
#include "lis3mdl.h"
|
||||
|
||||
LIS3MDL::LIS3MDL(device::Device *interface, const char *path, enum Rotation rotation) :
|
||||
CDev("LIS3MDL", path),
|
||||
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(interface->get_device_id())),
|
||||
LIS3MDL::LIS3MDL(device::Device *interface, enum Rotation rotation, I2CSPIBusOption bus_option, int bus) :
|
||||
CDev("LIS3MDL", nullptr),
|
||||
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(interface->get_device_id()), bus_option, bus),
|
||||
_interface(interface),
|
||||
_reports(nullptr),
|
||||
_scale{},
|
||||
@@ -91,9 +91,6 @@ LIS3MDL::LIS3MDL(device::Device *interface, const char *path, enum Rotation rota
|
||||
|
||||
LIS3MDL::~LIS3MDL()
|
||||
{
|
||||
/* make sure we are truly inactive */
|
||||
stop();
|
||||
|
||||
if (_mag_topic != nullptr) {
|
||||
orb_unadvertise(_mag_topic);
|
||||
}
|
||||
@@ -438,7 +435,7 @@ LIS3MDL::collect()
|
||||
}
|
||||
|
||||
void
|
||||
LIS3MDL::Run()
|
||||
LIS3MDL::RunImpl()
|
||||
{
|
||||
/* _measure_interval == 0 is used as _task_should_exit */
|
||||
if (_measure_interval == 0) {
|
||||
@@ -488,6 +485,9 @@ LIS3MDL::init()
|
||||
|
||||
_class_instance = register_class_devname(MAG_BASE_DEVICE_PATH);
|
||||
|
||||
_measure_interval = LIS3MDL_CONVERSION_INTERVAL;
|
||||
start();
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
@@ -599,8 +599,9 @@ LIS3MDL::measure()
|
||||
}
|
||||
|
||||
void
|
||||
LIS3MDL::print_info()
|
||||
LIS3MDL::print_status()
|
||||
{
|
||||
I2CSPIDriverBase::print_status();
|
||||
perf_print_counter(_sample_perf);
|
||||
perf_print_counter(_comms_errors);
|
||||
PX4_INFO("poll interval: %u", _measure_interval);
|
||||
@@ -793,16 +794,6 @@ LIS3MDL::start()
|
||||
ScheduleNow();
|
||||
}
|
||||
|
||||
void
|
||||
LIS3MDL::stop()
|
||||
{
|
||||
if (_measure_interval > 0) {
|
||||
/* ensure no new items are queued while we cancel this one */
|
||||
_measure_interval = 0;
|
||||
ScheduleClear();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
LIS3MDL::read_reg(uint8_t reg, uint8_t &val)
|
||||
{
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
|
||||
#include <lib/conversion/rotation.h>
|
||||
#include <systemlib/err.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <px4_platform_common/i2c_spi_buses.h>
|
||||
|
||||
#include <perf/perf_counter.h>
|
||||
#include <px4_platform_common/defines.h>
|
||||
@@ -91,16 +91,8 @@
|
||||
#define CNTL_REG5_DEFAULT 0x00
|
||||
|
||||
/* interface factories */
|
||||
extern device::Device *LIS3MDL_SPI_interface(int bus);
|
||||
extern device::Device *LIS3MDL_I2C_interface(int bus);
|
||||
typedef device::Device *(*LIS3MDL_constructor)(int);
|
||||
|
||||
enum LIS3MDL_BUS {
|
||||
LIS3MDL_BUS_ALL = 0,
|
||||
LIS3MDL_BUS_I2C_INTERNAL,
|
||||
LIS3MDL_BUS_I2C_EXTERNAL,
|
||||
LIS3MDL_BUS_SPI
|
||||
};
|
||||
extern device::Device *LIS3MDL_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode);
|
||||
extern device::Device *LIS3MDL_I2C_interface(int bus, int bus_frequency);
|
||||
|
||||
enum OPERATING_MODE {
|
||||
CONTINUOUS = 0,
|
||||
@@ -108,33 +100,32 @@ enum OPERATING_MODE {
|
||||
};
|
||||
|
||||
|
||||
class LIS3MDL : public device::CDev, public px4::ScheduledWorkItem
|
||||
class LIS3MDL : public device::CDev, public I2CSPIDriver<LIS3MDL>
|
||||
{
|
||||
public:
|
||||
LIS3MDL(device::Device *interface, const char *path, enum Rotation rotation);
|
||||
|
||||
LIS3MDL(device::Device *interface, enum Rotation rotation, I2CSPIBusOption bus_option, int bus);
|
||||
virtual ~LIS3MDL();
|
||||
|
||||
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
|
||||
int runtime_instance);
|
||||
static void print_usage();
|
||||
|
||||
void custom_method(const BusCLIArguments &cli) override;
|
||||
|
||||
virtual int init();
|
||||
|
||||
virtual int ioctl(struct file *file_pointer, int cmd, unsigned long arg);
|
||||
|
||||
virtual int read(struct file *file_pointer, char *buffer, size_t buffer_len);
|
||||
|
||||
/**
|
||||
* Diagnostics - print some basic information about the driver.
|
||||
*/
|
||||
void print_info();
|
||||
void print_status() override;
|
||||
|
||||
/**
|
||||
* Configures the device with default register values.
|
||||
*/
|
||||
int set_default_register_values();
|
||||
|
||||
/**
|
||||
* Stop the automatic measurement state machine.
|
||||
*/
|
||||
void stop();
|
||||
void RunImpl();
|
||||
|
||||
protected:
|
||||
Device *_interface;
|
||||
@@ -210,21 +201,6 @@ private:
|
||||
*/
|
||||
int check_offset();
|
||||
|
||||
/**
|
||||
* @brief Performs 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;
|
||||
|
||||
/**
|
||||
* Issue a measurement command.
|
||||
*
|
||||
|
||||
@@ -55,14 +55,12 @@
|
||||
#include "board_config.h"
|
||||
#include "lis3mdl.h"
|
||||
|
||||
#if defined(PX4_I2C_BUS_ONBOARD) || defined(PX4_I2C_BUS_EXPANSION)
|
||||
|
||||
#define LIS3MDLL_ADDRESS 0x1e
|
||||
|
||||
class LIS3MDL_I2C : public device::I2C
|
||||
{
|
||||
public:
|
||||
LIS3MDL_I2C(int bus);
|
||||
LIS3MDL_I2C(int bus, int bus_frequency);
|
||||
virtual ~LIS3MDL_I2C() = default;
|
||||
|
||||
virtual int ioctl(unsigned operation, unsigned &arg);
|
||||
@@ -75,16 +73,16 @@ protected:
|
||||
};
|
||||
|
||||
device::Device *
|
||||
LIS3MDL_I2C_interface(int bus);
|
||||
LIS3MDL_I2C_interface(int bus, int bus_frequency);
|
||||
|
||||
device::Device *
|
||||
LIS3MDL_I2C_interface(int bus)
|
||||
LIS3MDL_I2C_interface(int bus, int bus_frequency)
|
||||
{
|
||||
return new LIS3MDL_I2C(bus);
|
||||
return new LIS3MDL_I2C(bus, bus_frequency);
|
||||
}
|
||||
|
||||
LIS3MDL_I2C::LIS3MDL_I2C(int bus) :
|
||||
I2C("LIS3MDL_I2C", nullptr, bus, LIS3MDLL_ADDRESS, 400000)
|
||||
LIS3MDL_I2C::LIS3MDL_I2C(int bus, int bus_frequency) :
|
||||
I2C("LIS3MDL_I2C", nullptr, bus, LIS3MDLL_ADDRESS, bus_frequency)
|
||||
{
|
||||
_device_id.devid_s.devtype = DRV_MAG_DEVTYPE_LIS3MDL;
|
||||
}
|
||||
@@ -148,5 +146,3 @@ LIS3MDL_I2C::write(unsigned address, void *data, unsigned count)
|
||||
|
||||
return transfer(&buf[0], count + 1, nullptr, 0);
|
||||
}
|
||||
|
||||
#endif /* PX4_I2C_OBDEV_LIS3MDL */
|
||||
|
||||
@@ -37,393 +37,106 @@
|
||||
* Driver for the LIS3MDL magnetometer connected via I2C or SPI.
|
||||
*/
|
||||
|
||||
#include "lis3mdl_main.h"
|
||||
#include "lis3mdl.h"
|
||||
#include <px4_platform_common/getopt.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
|
||||
/**
|
||||
* Driver 'main' command.
|
||||
*/
|
||||
extern "C" __EXPORT int lis3mdl_main(int argc, char *argv[]);
|
||||
|
||||
int
|
||||
lis3mdl::calibrate(struct lis3mdl_bus_option &bus)
|
||||
I2CSPIDriverBase *LIS3MDL::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
|
||||
int runtime_instance)
|
||||
{
|
||||
int ret;
|
||||
const char *path = bus.devpath;
|
||||
device::Device *interface = nullptr;
|
||||
|
||||
PX4_INFO("running on bus: %u (%s)", (unsigned)bus.bus_id, bus.devpath);
|
||||
if (iterator.busType() == BOARD_I2C_BUS) {
|
||||
interface = LIS3MDL_I2C_interface(iterator.bus(), cli.bus_frequency);
|
||||
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
PX4_WARN("%s open failed (try 'lis3mdl start' if the driver is not running", path);
|
||||
return PX4_ERROR;
|
||||
} else if (iterator.busType() == BOARD_SPI_BUS) {
|
||||
interface = LIS3MDL_SPI_interface(iterator.bus(), iterator.devid(), cli.bus_frequency, cli.spi_mode);
|
||||
}
|
||||
|
||||
if ((ret = ioctl(fd, MAGIOCCALIBRATE, fd)) != OK) {
|
||||
PX4_WARN("failed to enable sensor calibration mode");
|
||||
if (interface == nullptr) {
|
||||
PX4_ERR("alloc failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::info(struct lis3mdl_bus_option &bus)
|
||||
{
|
||||
PX4_INFO("running on bus: %u (%s)", (unsigned)bus.bus_id, bus.devpath);
|
||||
bus.dev->print_info();
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::init(struct lis3mdl_bus_option &bus)
|
||||
{
|
||||
const char *path = bus.devpath;
|
||||
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
|
||||
close(fd);
|
||||
errx(1, "Failed to setup poll rate");
|
||||
return PX4_ERROR;
|
||||
|
||||
} else {
|
||||
PX4_INFO("Poll rate set to max (80hz)");
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::start_bus(struct lis3mdl_bus_option &bus, Rotation rotation)
|
||||
{
|
||||
if (bus.dev != nullptr) {
|
||||
errx(1, "bus option already started");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
device::Device *interface = bus.interface_constructor(bus.busnum);
|
||||
|
||||
if (interface->init() != OK) {
|
||||
delete interface;
|
||||
warnx("no device on bus %u", (unsigned)bus.bus_id);
|
||||
return PX4_ERROR;
|
||||
PX4_DEBUG("no device on bus %i (devid 0x%x)", iterator.bus(), iterator.devid());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bus.dev = new LIS3MDL(interface, bus.devpath, rotation);
|
||||
LIS3MDL *dev = new LIS3MDL(interface, cli.rotation, iterator.configuredBusOption(), iterator.bus());
|
||||
|
||||
if (bus.dev != nullptr &&
|
||||
bus.dev->init() != OK) {
|
||||
delete bus.dev;
|
||||
bus.dev = NULL;
|
||||
return PX4_ERROR;
|
||||
if (dev == nullptr) {
|
||||
delete interface;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::start(struct lis3mdl_bus_option &bus, Rotation rotation)
|
||||
{
|
||||
if (bus.dev == NULL) {
|
||||
return start_bus(bus, rotation);
|
||||
|
||||
} else {
|
||||
// this device is already started
|
||||
return PX4_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::stop(struct lis3mdl_bus_option &bus)
|
||||
{
|
||||
if (bus.dev != NULL) {
|
||||
bus.dev->stop();
|
||||
delete bus.dev;
|
||||
bus.dev = nullptr;
|
||||
return PX4_OK;
|
||||
|
||||
} else {
|
||||
// this device is already stopped
|
||||
return PX4_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::test(struct lis3mdl_bus_option &bus)
|
||||
{
|
||||
sensor_mag_s report;
|
||||
ssize_t sz;
|
||||
int ret;
|
||||
const char *path = bus.devpath;
|
||||
|
||||
PX4_INFO("running on bus: %u (%s)", (unsigned)bus.bus_id, bus.devpath);
|
||||
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
PX4_WARN("%s open failed (try 'lis3mdl start')", path);
|
||||
return PX4_ERROR;
|
||||
if (OK != dev->init()) {
|
||||
delete dev;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* do a simple demand read */
|
||||
sz = read(fd, &report, sizeof(report));
|
||||
|
||||
if (sz != sizeof(report)) {
|
||||
PX4_WARN("immediate read failed");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
print_message(report);
|
||||
|
||||
/* check if mag is onboard or external */
|
||||
if (ioctl(fd, MAGIOCGEXTERNAL, 0) < 0) {
|
||||
PX4_WARN("failed to get if mag is onboard or external");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
/* start the sensor polling at 2Hz */
|
||||
if (ioctl(fd, SENSORIOCSPOLLRATE, 2) != OK) {
|
||||
PX4_WARN("failed to set 2Hz poll rate");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
struct pollfd fds;
|
||||
|
||||
/* read the sensor 5x and report each value */
|
||||
for (unsigned i = 0; i < 5; i++) {
|
||||
|
||||
/* wait for data to be ready */
|
||||
fds.fd = fd;
|
||||
fds.events = POLLIN;
|
||||
ret = poll(&fds, 1, 2000);
|
||||
|
||||
if (ret != 1) {
|
||||
PX4_WARN("timed out waiting for sensor data");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
/* now go get it */
|
||||
sz = read(fd, &report, sizeof(report));
|
||||
|
||||
if (sz != sizeof(report)) {
|
||||
PX4_WARN("periodic read failed");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
print_message(report);
|
||||
}
|
||||
|
||||
PX4_INFO("PASS");
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl::reset(struct lis3mdl_bus_option &bus)
|
||||
{
|
||||
const char *path = bus.devpath;
|
||||
|
||||
PX4_INFO("running on bus: %u (%s)", (unsigned)bus.bus_id, bus.devpath);
|
||||
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
PX4_WARN("open failed ");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
|
||||
PX4_WARN("driver reset failed");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
|
||||
PX4_WARN("driver poll restart failed");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
return PX4_OK;
|
||||
return dev;
|
||||
}
|
||||
|
||||
void
|
||||
lis3mdl::usage()
|
||||
LIS3MDL::custom_method(const BusCLIArguments &cli)
|
||||
{
|
||||
PX4_WARN("missing command: try 'start', 'info', 'test', 'reset', 'info', 'calibrate'");
|
||||
PX4_WARN("options:");
|
||||
PX4_WARN(" -R rotation");
|
||||
PX4_WARN(" -C calibrate on start");
|
||||
PX4_WARN(" -X only external bus");
|
||||
PX4_WARN(" -S only spi bus");
|
||||
#if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_LIS)
|
||||
PX4_WARN(" -I only internal bus");
|
||||
#endif
|
||||
reset();
|
||||
}
|
||||
|
||||
int
|
||||
lis3mdl_main(int argc, char *argv[])
|
||||
void LIS3MDL::print_usage()
|
||||
{
|
||||
int myoptind = 1;
|
||||
PRINT_MODULE_USAGE_NAME("lis3mdl", "driver");
|
||||
PRINT_MODULE_USAGE_SUBCATEGORY("magnetometer");
|
||||
PRINT_MODULE_USAGE_COMMAND("start");
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true);
|
||||
PRINT_MODULE_USAGE_PARAM_INT('R', 0, 0, 35, "Rotation", true);
|
||||
PRINT_MODULE_USAGE_COMMAND("reset");
|
||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||
}
|
||||
|
||||
extern "C" int lis3mdl_main(int argc, char *argv[])
|
||||
{
|
||||
using ThisDriver = LIS3MDL;
|
||||
int ch;
|
||||
const char *myoptarg = nullptr;
|
||||
BusCLIArguments cli{true, true};
|
||||
cli.default_i2c_frequency = 400000;
|
||||
cli.default_spi_frequency = 11 * 1000 * 1000;
|
||||
|
||||
bool calibrate = false;
|
||||
enum LIS3MDL_BUS bus_id = LIS3MDL_BUS_ALL;
|
||||
enum Rotation rotation = ROTATION_NONE;
|
||||
|
||||
while ((ch = px4_getopt(argc, argv, "XISR:CT", &myoptind, &myoptarg)) != EOF) {
|
||||
while ((ch = cli.getopt(argc, argv, "R:")) != EOF) {
|
||||
switch (ch) {
|
||||
case 'R':
|
||||
rotation = (enum Rotation)atoi(myoptarg);
|
||||
cli.rotation = (enum Rotation)atoi(cli.optarg());
|
||||
break;
|
||||
#if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_LIS)
|
||||
|
||||
case 'I':
|
||||
bus_id = LIS3MDL_BUS_I2C_INTERNAL;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'X':
|
||||
bus_id = LIS3MDL_BUS_I2C_EXTERNAL;
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
bus_id = LIS3MDL_BUS_SPI;
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
calibrate = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
lis3mdl::usage();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (myoptind >= argc) {
|
||||
lis3mdl::usage();
|
||||
return PX4_ERROR;
|
||||
const char *verb = cli.optarg();
|
||||
|
||||
if (!verb) {
|
||||
ThisDriver::print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *verb = argv[myoptind];
|
||||
int ret;
|
||||
bool dev_found = false;
|
||||
bool cmd_found = false;
|
||||
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_MAG_DEVTYPE_LIS3MDL);
|
||||
|
||||
if (!strcmp(verb, "start")) {
|
||||
// Start/load the driver
|
||||
|
||||
cmd_found = true;
|
||||
ret = 1; // default: failed, will be set to success if one start succeeds
|
||||
|
||||
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
|
||||
if (bus_id != LIS3MDL_BUS_ALL && bus_id != lis3mdl::bus_options[i].bus_id) {
|
||||
// not the one that is asked for
|
||||
continue;
|
||||
}
|
||||
|
||||
dev_found = true;
|
||||
|
||||
// Start/load the driver
|
||||
if (lis3mdl::start(lis3mdl::bus_options[i], rotation) == OK) {
|
||||
if (calibrate) {
|
||||
if (lis3mdl::calibrate(lis3mdl::bus_options[i]) != OK) {
|
||||
PX4_WARN("calibration failed");
|
||||
lis3mdl::stop(lis3mdl::bus_options[i]); //Stop, failed
|
||||
|
||||
} else {
|
||||
PX4_INFO("calibration successful");
|
||||
lis3mdl::init(lis3mdl::bus_options[i]);
|
||||
ret = 0; // one succeed
|
||||
}
|
||||
|
||||
} else {
|
||||
lis3mdl::init(lis3mdl::bus_options[i]);
|
||||
ret = 0; // one succeed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Other commands
|
||||
|
||||
ret = 0; // default: success, will be set to failed if one action fails
|
||||
|
||||
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
|
||||
if (bus_id != LIS3MDL_BUS_ALL && bus_id != lis3mdl::bus_options[i].bus_id) {
|
||||
// not the one that is asked for
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lis3mdl::bus_options[i].dev == NULL) {
|
||||
if (bus_id != LIS3MDL_BUS_ALL) {
|
||||
PX4_ERR("bus %u not started", (unsigned)bus_id);
|
||||
return PX4_ERROR;
|
||||
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
dev_found = true;
|
||||
|
||||
// Stop the driver
|
||||
if (!strcmp(verb, "stop")) {
|
||||
cmd_found = true;
|
||||
ret |= lis3mdl::stop(lis3mdl::bus_options[i]);
|
||||
}
|
||||
|
||||
// Test the driver/device
|
||||
if (!strcmp(verb, "test")) {
|
||||
cmd_found = true;
|
||||
ret |= lis3mdl::test(lis3mdl::bus_options[i]);
|
||||
}
|
||||
|
||||
// Reset the driver
|
||||
if (!strcmp(verb, "reset")) {
|
||||
cmd_found = true;
|
||||
ret |= lis3mdl::reset(lis3mdl::bus_options[i]);
|
||||
}
|
||||
|
||||
// Print driver information
|
||||
if (!strcmp(verb, "info") ||
|
||||
!strcmp(verb, "status")) {
|
||||
cmd_found = true;
|
||||
ret |= lis3mdl::info(lis3mdl::bus_options[i]);
|
||||
}
|
||||
|
||||
// Autocalibrate the scaling
|
||||
if (!strcmp(verb, "calibrate")) {
|
||||
cmd_found = true;
|
||||
|
||||
if (lis3mdl::calibrate(lis3mdl::bus_options[i]) == OK) {
|
||||
PX4_INFO("calibration successful");
|
||||
|
||||
} else {
|
||||
PX4_WARN("calibration failed");
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ThisDriver::module_start(cli, iterator);
|
||||
}
|
||||
|
||||
if (!dev_found) {
|
||||
PX4_WARN("no device found, please start driver first");
|
||||
return PX4_ERROR;
|
||||
|
||||
} else if (!cmd_found) {
|
||||
PX4_WARN("unrecognized command, try 'start', 'test', 'reset', 'calibrate' 'or 'info'");
|
||||
return PX4_ERROR;
|
||||
|
||||
} else {
|
||||
return ret;
|
||||
if (!strcmp(verb, "stop")) {
|
||||
return ThisDriver::module_stop(iterator);
|
||||
}
|
||||
|
||||
if (!strcmp(verb, "status")) {
|
||||
return ThisDriver::module_status(iterator);
|
||||
}
|
||||
|
||||
if (!strcmp(verb, "reset")) {
|
||||
return ThisDriver::module_custom_method(cli, iterator);
|
||||
}
|
||||
|
||||
ThisDriver::print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file lis3mdl_main.h
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lis3mdl.h"
|
||||
|
||||
namespace lis3mdl
|
||||
{
|
||||
/**
|
||||
* @struct List of supported bus configurations
|
||||
*/
|
||||
struct lis3mdl_bus_option {
|
||||
LIS3MDL_BUS bus_id;
|
||||
const char *devpath;
|
||||
LIS3MDL_constructor interface_constructor;
|
||||
uint8_t busnum;
|
||||
LIS3MDL *dev;
|
||||
} bus_options[] = {
|
||||
#ifdef PX4_I2C_BUS_EXPANSION
|
||||
{ LIS3MDL_BUS_I2C_EXTERNAL, "/dev/lis3mdl_ext", &LIS3MDL_I2C_interface, PX4_I2C_BUS_EXPANSION, NULL },
|
||||
#endif /* PX4_I2C_BUS_EXPANSION */
|
||||
#ifdef PX4_I2C_BUS_EXPANSION1
|
||||
{ LIS3MDL_BUS_I2C_EXTERNAL, "/dev/lis3mdl_ext1", &LIS3MDL_I2C_interface, PX4_I2C_BUS_EXPANSION1, NULL },
|
||||
#endif /* PX4_I2C_BUS_EXPANSION1 */
|
||||
#ifdef PX4_I2C_BUS_EXPANSION2
|
||||
{ LIS3MDL_BUS_I2C_EXTERNAL, "/dev/lis3mdl_ext2", &LIS3MDL_I2C_interface, PX4_I2C_BUS_EXPANSION2, NULL },
|
||||
#endif /* PX4_I2C_BUS_EXPANSION2 */
|
||||
#ifdef PX4_I2C_BUS_ONBOARD
|
||||
{ LIS3MDL_BUS_I2C_INTERNAL, "/dev/lis3mdl_int", &LIS3MDL_I2C_interface, PX4_I2C_BUS_ONBOARD, NULL },
|
||||
#endif /* PX4_I2C_BUS_ONBOARD */
|
||||
#ifdef PX4_SPIDEV_LIS
|
||||
{ LIS3MDL_BUS_SPI, "/dev/lis3mdl_spi", &LIS3MDL_SPI_interface, PX4_SPI_BUS_SENSORS, NULL },
|
||||
#endif /* PX4_SPIDEV_LIS */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Calibrate and self test. Self test feature cannot be used to calculate scale.
|
||||
*
|
||||
* SELF TEST OPERATION
|
||||
* Note: To check the LIS3MDL for proper operation, a self test feature is incorporated :
|
||||
* sensor offset straps are excited to create a nominal field strength
|
||||
* (bias field) to be measured. To implement self test, the least significant bits
|
||||
* (MS1 and MS0) of configuration register A are changed from 00 to 01 (positive bias).
|
||||
* A few measurements are taken and stored with and without the additional magnetic
|
||||
* field. According to ST datasheet, those values must stay between thresholds in order
|
||||
* to pass the self test.
|
||||
*/
|
||||
int calibrate(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Prints info about the driver.
|
||||
*/
|
||||
int info(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Initializes the driver -- sets defaults and starts a cycle
|
||||
*/
|
||||
int init(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Resets the driver.
|
||||
*/
|
||||
int reset(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Starts the driver for a specific bus option
|
||||
*/
|
||||
int start_bus(struct lis3mdl_bus_option &bus, Rotation rotation);
|
||||
|
||||
/**
|
||||
* @brief Starts the driver. This function call only returns once the driver
|
||||
* is either successfully up and running or failed to start.
|
||||
*/
|
||||
int start(struct lis3mdl_bus_option &bus, Rotation rotation);
|
||||
|
||||
/**
|
||||
* @brief Stop the driver.
|
||||
*/
|
||||
int stop(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Perform some basic functional tests on the driver;
|
||||
* make sure we can collect data from the sensor in polled
|
||||
* and automatic modes.
|
||||
*/
|
||||
int test(struct lis3mdl_bus_option &bus);
|
||||
|
||||
/**
|
||||
* @brief Prints info about the driver argument usage.
|
||||
*/
|
||||
void usage();
|
||||
|
||||
} // namespace lis3mdl
|
||||
@@ -55,8 +55,6 @@
|
||||
#include "board_config.h"
|
||||
#include "lis3mdl.h"
|
||||
|
||||
#ifdef PX4_SPIDEV_LIS
|
||||
|
||||
/* SPI protocol address bits */
|
||||
#define DIR_READ (1<<7)
|
||||
#define DIR_WRITE (0<<7)
|
||||
@@ -65,7 +63,7 @@
|
||||
class LIS3MDL_SPI : public device::SPI
|
||||
{
|
||||
public:
|
||||
LIS3MDL_SPI(int bus, uint32_t device);
|
||||
LIS3MDL_SPI(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode);
|
||||
virtual ~LIS3MDL_SPI() = default;
|
||||
|
||||
virtual int init();
|
||||
@@ -75,16 +73,16 @@ public:
|
||||
};
|
||||
|
||||
device::Device *
|
||||
LIS3MDL_SPI_interface(int bus);
|
||||
LIS3MDL_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode);
|
||||
|
||||
device::Device *
|
||||
LIS3MDL_SPI_interface(int bus)
|
||||
LIS3MDL_SPI_interface(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode)
|
||||
{
|
||||
return new LIS3MDL_SPI(bus, PX4_SPIDEV_LIS);
|
||||
return new LIS3MDL_SPI(bus, devid, bus_frequency, spi_mode);
|
||||
}
|
||||
|
||||
LIS3MDL_SPI::LIS3MDL_SPI(int bus, uint32_t device) :
|
||||
SPI("LIS3MDL_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */)
|
||||
LIS3MDL_SPI::LIS3MDL_SPI(int bus, uint32_t devid, int bus_frequency, spi_mode_e spi_mode) :
|
||||
SPI("LIS3MDL_SPI", nullptr, bus, devid, spi_mode, bus_frequency)
|
||||
{
|
||||
_device_id.devid_s.devtype = DRV_MAG_DEVTYPE_LIS3MDL;
|
||||
}
|
||||
@@ -172,5 +170,3 @@ LIS3MDL_SPI::write(unsigned address, void *data, unsigned count)
|
||||
|
||||
return transfer(&buf[0], &buf[0], count + 1);
|
||||
}
|
||||
|
||||
#endif /* PX4_SPIDEV_LIS */
|
||||
|
||||
Reference in New Issue
Block a user