Port si7210 drivers to new driver framework

This commit ports the si7210 hall sensor to the new driver framework
This commit is contained in:
Jaeyoung-Lim
2021-05-07 11:17:00 +02:00
parent d20f353342
commit e62a569f63
10 changed files with 590 additions and 938 deletions
+2
View File
@@ -342,6 +342,8 @@ rtps:
id: 158
- msg: estimator_event_flags
id: 159
- msg: sensor_hall
id: 160
########## multi topics: begin ##########
- msg: actuator_controls_0
id: 170
-3
View File
@@ -40,7 +40,6 @@
#ifndef _DRV_HALL_H
#define _DRV_HALL_H
#include <px4_defines.h>
#include <stdint.h>
#include <sys/ioctl.h>
@@ -53,7 +52,5 @@
#define HALL2_DEVICE_PATH "/dev/hall2"
#define HALL3_DEVICE_PATH "/dev/hall3"
#include <uORB/topics/sensor_hall.h>
#define si7210_report sensor_hall_s
#endif /* _DRV_HALL_H */
+5 -2
View File
@@ -34,9 +34,12 @@ px4_add_module(
MODULE drivers__si7210
MAIN si7210
COMPILE_FLAGS
-O0
# -DDEBUG_BUILD
SRCS
si7210_main.cpp
si7210.cpp
DEPENDS
px4_work_queue
drivers__vane
mathlib
)
# vim: set noet ft=cmake fenc=utf-8 ff=unix :
File diff suppressed because it is too large Load Diff
+47 -138
View File
@@ -41,55 +41,18 @@
#ifndef SI7210_HPP_
#define SI7210_HPP_
#include <px4_config.h>
#include <parameters/param.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.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 <getopt.h>
#include <px4_log.h>
#include <perf/perf_counter.h>
#include <systemlib/err.h>
#include <nuttx/wqueue.h>
#include <systemlib/conversions.h>
#include <nuttx/arch.h>
#include <nuttx/clock.h>
#include <uORB/uORB.h>
#include <uORB/topics/parameter_update.h>
#include <board_config.h>
#include <drivers/drv_hrt.h>
#include <drivers/device/ringbuffer.h>
#include <drivers/device/integrator.h>
#include <drivers/device/i2c.h>
#include <drivers/drv_hall.h>
#include <mathlib/math/filter/LowPassFilter2p.hpp>
#include <drivers/vane/vane.h>
#include <math.h>
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <uORB/topics/sensor_hall.h>
#include "parameters.h"
using namespace si7210;
#define SI7210_BUS PX4_I2C_BUS_EXPANSION
#define SI7210_SLAVE_ADDRESS_0 0x30 /* SI7210 I2C address */
#define SI7210_SLAVE_ADDRESS_1 0x31 /* SI7210 I2C address */
#define SI7210_SLAVE_ADDRESS_2 0x32 /* SI7210 I2C address */
#define SI7210_SLAVE_ADDRESS_3 0x33 /* SI7210 I2C address */
#define I2C_ADDRESS_0_SI7210 0x30 /* SI7210 I2C address */
#define I2C_ADDRESS_1_SI7210 0x31 /* SI7210 I2C address */
#define I2C_ADDRESS_2_SI7210 0x32 /* SI7210 I2C address */
#define I2C_ADDRESS_3_SI7210 0x33 /* SI7210 I2C address */
#define SI7210_MAX_DATA_RATE 50
@@ -149,104 +112,40 @@ using namespace si7210;
#define MAG_BIAS 0xC000
#define MAG_CONV 0.00125F
class SI7210 : public device::I2C
class SI7210 : public Vane, public I2CSPIDriver<SI7210>
{
public:
enum Instance : int8_t {
INVALID = -1,
ID_0 = 0,
ID_1 = 1,
ID_2 = 2,
ID_3 = 3,
};
SI7210(I2CSPIBusOption bus_option, const int bus, int bus_frequency, int address = I2C_ADDRESS_0_SI7210,
bool keep_retrying = false) :
Vane(bus, bus_frequency, address, SI7210_CONVERSION_INTERVAL),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus, address),
_keep_retrying{keep_retrying}
{
}
SI7210(int bus, SI7210::Instance instance, const char *path);
virtual ~SI7210();
virtual ~SI7210() = default;
virtual int init();
virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
/**
* Stop automatic measurement.
*/
void stop();
void RunImpl();
/**
* Diagnostics - print some basic information about the driver.
*/
void print_info();
protected:
virtual int probe();
void start();
private:
work_s _work{};
enum class State {
RequireConfig,
Configuring,
Running
};
bool _running;
int measure() override { return 0; }
int collect() override;
int probe() override;
int configure();
/* altitude conversion calibration */
unsigned _call_interval;
si7210_report _report {};
ringbuffer::RingBuffer *_reports;
bool _collect_phase;
orb_advert_t _hall_topic;
int _orb_class_instance;
perf_counter_t _sample_perf;
perf_counter_t _bad_transfers;
perf_counter_t _good_transfers;
perf_counter_t _measure_perf;
perf_counter_t _comms_errors;
perf_counter_t _duplicates;
bool _got_duplicate;
Instance _instance; /**< index of the i2c address and publisher instance */
int _params_sub{-1}; /**< notification of parameter updates */
si7210_report _last_report {}; /**< used for info() */
Parameters _parameters{}; /**< local copies of interesting parameters */
ParameterHandles _parameter_handles{}; /**< handles for interesting parameters */
/**
* Start automatic measurement.
*/
void start();
int measure(); //start measure
int collect(); //get results and publish
int parameters_update(); // update parameters
static void cycle_trampoline(void *arg);
void cycle(); //main execution
/**
* Read the specified number of bytes from SI7210.
*
* @param reg The register to read.
* @param data Pointer to buffer for bytes read.
* @param len Number of bytes to read
* @return OK if the transfer was successful, -errno otherwise.
*/
int get_data(uint8_t reg, uint8_t *data, unsigned len);
/**
* Resets the chip.
*/
int reset();
/**
* Measurement self test
*
* @return 0 on success, 1 on failure
*/
int self_test();
bool init_si7210();
/**
* Get registers values
@@ -276,9 +175,19 @@ private:
*/
int get_sensor_data(uint8_t otpAddr, int8_t *data);
/* do not allow to copy this class due to pointer data members */
SI7210(const SI7210 &);
SI7210 operator=(const SI7210 &);
/**
* Calculate the CRC8 for the sensor payload data
*/
bool crc(const uint8_t data[], unsigned size, uint8_t checksum);
/**
* Write a command in Sensirion specific logic
*/
int write_command(uint16_t command);
uint16_t _scale{0};
const bool _keep_retrying;
State _state{State::RequireConfig};
};
#endif /* SI7210_HPP_ */
+110
View File
@@ -0,0 +1,110 @@
/****************************************************************************
*
* Copyright (c) 2012-2021 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 si7210_main.cpp
* Driver for the SI7210 connected via I2C.
*
* Author: Amir Melzer <amir.melzer@mavt.ethz.ch>
*/
#include "si7210.h"
extern "C" __EXPORT int si7210_main(int argc, char *argv[]);
I2CSPIDriverBase *SI7210::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance)
{
SI7210 *instance = new SI7210(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency, cli.i2c_address,
cli.keep_running);
if (instance == nullptr) {
PX4_ERR("alloc failed");
return nullptr;
}
if (instance->init() != PX4_OK) {
delete instance;
return nullptr;
}
instance->start();
return instance;
}
void
SI7210::print_usage()
{
PRINT_MODULE_USAGE_NAME("si7210", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("airspeed_sensor");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x30);
PRINT_MODULE_USAGE_PARAMS_I2C_KEEP_RUNNING_FLAG();
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
int
si7210_main(int argc, char *argv[])
{
using ThisDriver = SI7210;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 100000;
cli.i2c_address = I2C_ADDRESS_2_SI7210;
cli.support_keep_running = true;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_HALL_DEVTYPE_SI7210);
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
View File
@@ -40,3 +40,4 @@ add_subdirectory(led)
add_subdirectory(magnetometer)
add_subdirectory(rangefinder)
add_subdirectory(smbus)
add_subdirectory(vane)
+35
View File
@@ -0,0 +1,35 @@
############################################################################
#
# Copyright (c) 2021 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.
#
############################################################################
px4_add_library(drivers__vane vane.cpp)
target_link_libraries(drivers__vane PRIVATE drivers__device)
+122
View File
@@ -0,0 +1,122 @@
/****************************************************************************
*
* Copyright (c) 2013-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 airspeed.cpp
* @author Simon Wilks <simon@px4.io>
* @author Lorenz Meier <lorenz@px4.io>
*
*/
#include <px4_platform_common/px4_config.h>
#include <drivers/device/device.h>
#include <drivers/device/i2c.h>
#include <systemlib/err.h>
#include <parameters/param.h>
#include <perf/perf_counter.h>
#include <drivers/drv_airspeed.h>
#include <drivers/drv_hrt.h>
#include <uORB/uORB.h>
#include <uORB/topics/sensor_hall.h>
#include <drivers/vane/vane.h>
Vane::Vane(int bus, int bus_frequency, int address, unsigned conversion_interval) :
I2C(0, "Vane", bus, address, bus_frequency),
_sensor_ok(false),
_measure_interval(conversion_interval),
_collect_phase(false),
_vane_orb_class_instance(-1),
_class_instance(-1),
_conversion_interval(conversion_interval),
_sample_perf(perf_alloc(PC_ELAPSED, "vane_read")),
_comms_errors(perf_alloc(PC_COUNT, "vane_com_err"))
{
}
Vane::~Vane()
{
if (_class_instance != -1) {
unregister_class_devname(HALL_BASE_DEVICE_PATH, _class_instance);
}
// free perf counters
perf_free(_sample_perf);
perf_free(_comms_errors);
}
int
Vane::init()
{
/* do I2C init (and probe) first */
if (I2C::init() != PX4_OK) {
return PX4_ERROR;
}
/* register alternate interfaces if we have to */
_class_instance = register_class_devname(HALL_BASE_DEVICE_PATH);
/* advertise sensor topic, measure manually to initialize valid report */
measure();
return PX4_OK;
}
int
Vane::probe()
{
/* on initial power up the device may need more than one retry
for detection. Once it is running the number of retries can
be reduced
*/
_retries = 4;
int ret = measure();
// drop back to 2 retries once initialised
_retries = 2;
return ret;
}
int
Vane::ioctl(device::file_t *filp, int cmd, unsigned long arg)
{
switch (cmd) {
default:
/* give it to the superclass */
return I2C::ioctl(filp, cmd, arg);
}
}
+84
View File
@@ -0,0 +1,84 @@
/****************************************************************************
*
* Copyright (c) 2013, 2014 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.
*
****************************************************************************/
#pragma once
#include <string.h>
#include <drivers/device/i2c.h>
#include <drivers/drv_hall.h>
#include <drivers/drv_hrt.h>
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <perf/perf_counter.h>
#include <uORB/topics/sensor_hall.h>
#include <uORB/PublicationMulti.hpp>
class __EXPORT Vane : public device::I2C
{
public:
Vane(int bus, int bus_frequency, int address, unsigned conversion_interval);
virtual ~Vane();
int init() override;
int ioctl(device::file_t *filp, int cmd, unsigned long arg) override;
private:
Vane(const Vane &) = delete;
Vane &operator=(const Vane &) = delete;
protected:
int probe() override;
/**
* Perform a poll cycle; collect from the previous measurement
* and start a new one.
*/
virtual int measure() = 0;
virtual int collect() = 0;
bool _sensor_ok;
int _measure_interval;
bool _collect_phase;
uORB::PublicationMulti<sensor_hall_s> _vane_pub{ORB_ID(sensor_hall)};
int _vane_orb_class_instance;
int _class_instance;
unsigned _conversion_interval;
perf_counter_t _sample_perf;
perf_counter_t _comms_errors;
};