refactor qmc5883: use driver base class

This commit is contained in:
Daniel Agar
2020-03-21 13:06:18 -04:00
parent b156fe5787
commit 532ccd18ad
8 changed files with 933 additions and 1353 deletions
@@ -36,9 +36,10 @@ px4_add_module(
COMPILE_FLAGS
-Wno-cast-align # TODO: fix and enable
SRCS
qmc5883_i2c.cpp
qmc5883_spi.cpp
qmc5883.cpp
QMC5883_I2C.cpp
QMC5883.cpp
QMC5883.hpp
qmc5883_main.cpp
DEPENDS
px4_work_queue
)
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,223 @@
/****************************************************************************
*
* Copyright (c) 2012-2019 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 qmc5883.cpp
*
* Driver for the QMC5883 magnetometer connected via I2C.
*/
#pragma once
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/time.h>
#include <drivers/device/i2c.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <semaphore.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <lib/perf/perf_counter.h>
#include <drivers/drv_mag.h>
#include <drivers/drv_hrt.h>
#include <drivers/device/ringbuffer.h>
#include <drivers/drv_device.h>
#include <uORB/uORB.h>
#include <float.h>
#include <lib/conversion/rotation.h>
#include "qmc5883.h"
/*
* QMC5883 internal constants and data structures.
*/
/* Max measurement rate is 200Hz */
#define QMC5883_CONVERSION_INTERVAL (1000000 / 150) /* microseconds */
#define QMC5883_MAX_COUNT 32767
#define QMC5883_ADDR_DATA_OUT_X_LSB 0x00
#define QMC5883_ADDR_DATA_OUT_X_MSB 0x01
#define QMC5883_ADDR_DATA_OUT_Y_LSB 0x02
#define QMC5883_ADDR_DATA_OUT_Y_MSB 0x03
#define QMC5883_ADDR_DATA_OUT_Z_LSB 0x04
#define QMC5883_ADDR_DATA_OUT_Z_MSB 0x05
#define QMC5883_ADDR_STATUS 0x06
#define QMC5883_ADDR_TEMP_OUT_LSB 0x07
#define QMC5883_ADDR_TEMP_OUT_MSB 0x08
#define QMC5883_ADDR_CONTROL_1 0x09
#define QMC5883_ADDR_CONTROL_2 0x0A
#define QMC5883_ADDR_SET_RESET 0x0B
#define QMC5883_STATUS_REG_DRDY (1 << 0) /* Data Ready: "0": no new data, "1": new data is ready */
#define QMC5883_STATUS_REG_OVL (1 << 1) /* Overflow Flag: "0": normal, "1": data overflow */
#define QMC5883_STATUS_REG_DOR (1 << 2) /* Data Skip: "0": normal, "1": data skipped for reading */
/* Control Register 1 */
#define QMC5883_MODE_REG_STANDBY (0 << 0)
#define QMC5883_MODE_REG_CONTINOUS_MODE (1 << 0)
#define QMC5883_OUTPUT_DATA_RATE_10 (0 << 2) /* Hz */
#define QMC5883_OUTPUT_DATA_RATE_50 (1 << 2)
#define QMC5883_OUTPUT_DATA_RATE_100 (2 << 2)
#define QMC5883_OUTPUT_DATA_RATE_200 (3 << 2)
#define QMC5883_OUTPUT_RANGE_2G (0 << 4) /* +/- 2 gauss */
#define QMC5883_OUTPUT_RANGE_8G (1 << 4) /* +/- 8 gauss */
#define QMC5883_OVERSAMPLE_512 (0 << 6) /* controls digital filter bw - larger OSR -> smaller bw */
#define QMC5883_OVERSAMPLE_256 (1 << 6)
#define QMC5883_OVERSAMPLE_128 (2 << 6)
#define QMC5883_OVERSAMPLE_64 (3 << 6)
/* Control Register 2 */
#define QMC5883_INT_ENB (1 << 0)
#define QMC5883_ROL_PNT (1 << 6)
#define QMC5883_SOFT_RESET (1 << 7)
/* Set Register */
#define QMC5883_SET_DEFAULT (1 << 0)
#define QMC5883_TEMP_OFFSET 50 /* deg celsius */
class QMC5883 : public device::CDev, public I2CSPIDriver<QMC5883>
{
public:
QMC5883(device::Device *interface, enum Rotation rotation, I2CSPIBusOption bus_option, int bus, int i2c_address);
virtual ~QMC5883();
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
void RunImpl();
int init() override;
ssize_t read(cdev::file_t *filp, char *buffer, size_t buflen) override;
int ioctl(cdev::file_t *filp, int cmd, unsigned long arg) override;
protected:
void print_status() override;
private:
Device *_interface;
unsigned _measure_interval{0};
ringbuffer::RingBuffer *_reports;
struct mag_calibration_s _scale;
float _range_scale;
float _range_ga;
bool _collect_phase;
int _class_instance;
int _orb_class_instance;
orb_advert_t _mag_topic;
perf_counter_t _sample_perf;
perf_counter_t _comms_errors;
perf_counter_t _range_errors;
perf_counter_t _conf_errors;
/* status reporting */
bool _sensor_ok; /**< sensor was found and reports ok */
enum Rotation _rotation;
sensor_mag_s _last_report {}; /**< used for info() */
uint8_t _range_bits;
uint8_t _conf_reg;
uint8_t _temperature_counter;
uint8_t _temperature_error_count;
/**
* Initialise the automatic measurement state machine and start it.
*
* @note This function is called at open and error time. It might make sense
* to make it more aggressive about resetting the bus in case of errors.
*/
void start();
/**
* Reset the device
*/
int reset();
/**
* check the sensor configuration.
*
* checks that the config of the sensor is correctly set, to
* cope with communication errors causing the configuration to
* change
*/
void check_conf(void);
/**
* Write a register.
*
* @param reg The register to write.
* @param val The value to write.
* @return OK on write success.
*/
int write_reg(uint8_t reg, uint8_t val);
/**
* Read a register.
*
* @param reg The register to read.
* @param val The value read.
* @return OK on read success.
*/
int read_reg(uint8_t reg, uint8_t &val);
/**
* Collect the result of the most recent measurement.
*/
int collect();
};
@@ -37,34 +37,19 @@
* I2C interface for QMC5883
*/
/* XXX trim includes */
#include <px4_platform_common/px4_config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <unistd.h>
#include <drivers/device/i2c.h>
#include <drivers/drv_mag.h>
#include <drivers/drv_device.h>
#include "qmc5883.h"
#include "board_config.h"
#define QMC5883L_ADDRESS 0x0D
device::Device *QMC5883_I2C_interface(int bus);
device::Device *QMC5883_I2C_interface(int bus, int bus_frequency, int i2c_address);
class QMC5883_I2C : public device::I2C
{
public:
QMC5883_I2C(int bus);
QMC5883_I2C(int bus, int bus_frequency, int i2c_address);
virtual ~QMC5883_I2C() = default;
virtual int read(unsigned address, void *data, unsigned count);
@@ -78,13 +63,13 @@ protected:
};
device::Device *
QMC5883_I2C_interface(int bus)
QMC5883_I2C_interface(int bus, int bus_frequency, int i2c_address)
{
return new QMC5883_I2C(bus);
return new QMC5883_I2C(bus, bus_frequency, i2c_address);
}
QMC5883_I2C::QMC5883_I2C(int bus) :
I2C("QMC5883_I2C", nullptr, bus, QMC5883L_ADDRESS, 400000)
QMC5883_I2C::QMC5883_I2C(int bus, int bus_frequency, int i2c_address) :
I2C("QMC5883_I2C", nullptr, bus, i2c_address, bus_frequency)
{
_device_id.devid_s.devtype = DRV_MAG_DEVTYPE_QMC5883;
}
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -39,6 +39,8 @@
#pragma once
#include <drivers/device/Device.hpp>
#define ADDR_ID_A 0x0C
#define ADDR_ID_B 0x0D
@@ -47,6 +49,4 @@
/* interface factories */
extern device::Device *QMC5883_SPI_interface(int bus);
extern device::Device *QMC5883_I2C_interface(int bus);
typedef device::Device *(*QMC5883_constructor)(int);
extern device::Device *QMC5883_I2C_interface(int bus, int bus_frequency, int i2c_address);
@@ -0,0 +1,133 @@
/****************************************************************************
*
* Copyright (c) 2012-2019 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 qmc5883.cpp
*
* Driver for the QMC5883 magnetometer connected via I2C.
*/
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
#include "QMC5883.hpp"
#include "qmc5883.h"
extern "C" __EXPORT int qmc5883_main(int argc, char *argv[]);
I2CSPIDriverBase *QMC5883::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance)
{
device::Device *interface = nullptr;
if (iterator.busType() == BOARD_I2C_BUS) {
interface = QMC5883_I2C_interface(iterator.bus(), cli.bus_frequency, cli.i2c_address);
}
if (interface == nullptr) {
PX4_ERR("alloc failed");
return nullptr;
}
if (interface->init() != OK) {
delete interface;
PX4_DEBUG("no device on bus %i (devid 0x%x)", iterator.bus(), iterator.devid());
return nullptr;
}
QMC5883 *dev = new QMC5883(interface, cli.rotation, iterator.configuredBusOption(), iterator.bus(), cli.i2c_address);
if (dev == nullptr) {
delete interface;
return nullptr;
}
if (OK != dev->init()) {
delete dev;
return nullptr;
}
return dev;
}
void QMC5883::print_usage()
{
PRINT_MODULE_USAGE_NAME("qmc5883", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("magnetometer");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x0D);
PRINT_MODULE_USAGE_PARAM_INT('R', 0, 0, 35, "Rotation", true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
extern "C" int qmc5883_main(int argc, char *argv[])
{
using ThisDriver = QMC5883;
int ch;
BusCLIArguments cli{true, false};
cli.i2c_address = ADDR_ID_B;
cli.default_i2c_frequency = 400000;
while ((ch = cli.getopt(argc, argv, "R:")) != EOF) {
switch (ch) {
case 'R':
cli.rotation = (enum Rotation)atoi(cli.optarg());
break;
}
}
const char *verb = cli.optarg();
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_MAG_DEVTYPE_QMC5883);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}
@@ -1,183 +0,0 @@
/****************************************************************************
*
* Copyright (c) 2013-2019 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 QMC5883_SPI.cpp
*
* SPI interface for HMC5983
*/
/* XXX trim includes */
#include <px4_platform_common/px4_config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <unistd.h>
#include <drivers/device/spi.h>
#include <drivers/drv_mag.h>
#include <drivers/drv_device.h>
#include "qmc5883.h"
#include <board_config.h>
#ifdef PX4_SPIDEV_HMC
/* SPI protocol address bits */
#define DIR_READ (1<<7)
#define DIR_WRITE (0<<7)
#define ADDR_INCREMENT (1<<6)
#define HMC_MAX_SEND_LEN 4
#define HMC_MAX_RCV_LEN 8
device::Device *QMC5883_SPI_interface(int bus);
class QMC5883_SPI : public device::SPI
{
public:
QMC5883_SPI(int bus, uint32_t device);
virtual ~QMC5883_SPI() = default;
virtual int init();
virtual int read(unsigned address, void *data, unsigned count);
virtual int write(unsigned address, void *data, unsigned count);
virtual int ioctl(unsigned operation, unsigned &arg);
};
device::Device *
QMC5883_SPI_interface(int bus)
{
return new QMC5883_SPI(bus, PX4_SPIDEV_HMC);
}
QMC5883_SPI::QMC5883_SPI(int bus, uint32_t device) :
SPI("QMC5883_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */)
{
_device_id.devid_s.devtype = DRV_MAG_DEVTYPE_QMC5883;
}
int
QMC5883_SPI::init()
{
int ret;
ret = SPI::init();
if (ret != OK) {
DEVICE_DEBUG("SPI init failed");
return -EIO;
}
// read WHO_AM_I value
uint8_t data[2] = {0, 0};
if (read(ADDR_ID_A, &data[0], 1) ||
read(ADDR_ID_B, &data[1], 1)) {
DEVICE_DEBUG("read_reg fail");
}
if ((data[0] != ID_A_WHO_AM_I) ||
(data[1] != ID_B_WHO_AM_I)) {
DEVICE_DEBUG("ID byte mismatch (%02x,%02x)", data[0], data[1]);
return -EIO;
}
return OK;
}
int
QMC5883_SPI::ioctl(unsigned operation, unsigned &arg)
{
int ret;
switch (operation) {
case MAGIOCGEXTERNAL:
/*
* Even if this sensor is on the external SPI
* bus it is still internal to the autopilot
* assembly, so always return 0 for internal.
*/
return 0;
case DEVIOCGDEVICEID:
return CDev::ioctl(nullptr, operation, arg);
default: {
ret = -EINVAL;
}
}
return ret;
}
int
QMC5883_SPI::write(unsigned address, void *data, unsigned count)
{
uint8_t buf[32];
if (sizeof(buf) < (count + 1)) {
return -EIO;
}
buf[0] = address | DIR_WRITE;
memcpy(&buf[1], data, count);
return transfer(&buf[0], &buf[0], count + 1);
}
int
QMC5883_SPI::read(unsigned address, void *data, unsigned count)
{
uint8_t buf[32];
if (sizeof(buf) < (count + 1)) {
return -EIO;
}
buf[0] = address | DIR_READ | ADDR_INCREMENT;
int ret = transfer(&buf[0], &buf[0], count + 1);
memcpy(data, &buf[1], count);
return ret;
}
#endif /* PX4_SPIDEV_HMC */