changes on drivers according to comments from DavidS

This commit is contained in:
Sergej Scheiermann
2017-04-24 10:38:39 +02:00
committed by Lorenz Meier
parent da31e6e0b5
commit 641a03510c
14 changed files with 422 additions and 1396 deletions
+1
View File
@@ -37,6 +37,7 @@ px4_add_module(
COMPILE_FLAGS
SRCS
bmp280_spi.cpp
bmp280_i2c.cpp
bmp280.cpp
DEPENDS
platforms__common
+95 -54
View File
@@ -51,6 +51,7 @@
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <px4_log.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
@@ -104,6 +105,8 @@ public:
private:
bmp280::IBMP280 *_interface;
bool _running;
uint8_t _curr_ctrl;
struct work_s _work;
@@ -150,6 +153,7 @@ extern "C" __EXPORT int bmp280_main(int argc, char *argv[]);
BMP280::BMP280(bmp280::IBMP280 *interface, const char *path) :
CDev("BMP280", path),
_interface(interface),
_running(false),
_report_ticks(0),
_reports(nullptr),
_collect_phase(false),
@@ -161,8 +165,6 @@ BMP280::BMP280(bmp280::IBMP280 *interface, const char *path) :
_measure_perf(perf_alloc(PC_ELAPSED, "bmp280_measure")),
_comms_errors(perf_alloc(PC_COUNT, "bmp280_comms_errors"))
{
_device_id.devid_s.devtype = DRV_BARO_DEVTYPE_BMP280;
// work_cancel in stop_cycle called from the dtor will explode if we don't do this...
memset(&_work, 0, sizeof(_work));
}
@@ -181,6 +183,11 @@ BMP280::~BMP280()
delete _reports;
}
if (_baro_topic != nullptr) {
orb_unadvertise(_baro_topic);
}
// free perf counters
perf_free(_sample_perf);
perf_free(_measure_perf);
@@ -221,7 +228,7 @@ BMP280::init()
/* check id*/
if (_interface->get_reg(BPM280_ADDR_ID) != BPM280_VALUE_ID) {
warnx("id of your baro is not: 0x%02x", BPM280_VALUE_ID);
PX4_WARN("id of your baro is not: 0x%02x", BPM280_VALUE_ID);
return -EIO;
}
@@ -270,7 +277,7 @@ BMP280::init()
&_orb_class_instance, _interface->is_external() ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT);
if (_baro_topic == nullptr) {
warnx("failed to create sensor_baro publication");
PX4_WARN("failed to create sensor_baro publication");
return -ENOMEM;
}
@@ -440,6 +447,7 @@ BMP280::start_cycle()
/* reset the report ring and state machine */
_collect_phase = false;
_running = true;
_reports->flush();
/* schedule a cycle to start things */
@@ -449,6 +457,7 @@ BMP280::start_cycle()
void
BMP280::stop_cycle()
{
_running = false;
work_cancel(HPWORK, &_work);
}
@@ -467,7 +476,7 @@ BMP280::cycle()
collect();
unsigned wait_gap = _report_ticks - _max_mesure_ticks;
if (wait_gap != 0) {
if ((wait_gap != 0) && (_running)) {
work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this,
wait_gap); //need to wait some time before new measurement
return;
@@ -476,7 +485,10 @@ BMP280::cycle()
}
measure();
work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this, _max_mesure_ticks);
if (_running) {
work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this, _max_mesure_ticks);
}
}
@@ -542,8 +554,6 @@ BMP280::collect()
report.temperature = _T;
report.pressure = _P / 100.0f; // to mbar
/* Get device ID */
report.device_id = _device_id.devid;
/* altitude calculations based on http://www.kansasflyer.org/index.asp?nav=Avi&sec=Alti&tab=Theory&pg=1 */
@@ -620,10 +630,10 @@ struct bmp280_bus_option {
{ BMP280_BUS_SPI_INTERNAL, "/dev/bmp280_spi_int", &bmp280_spi_interface, PX4_SPI_BUS_SENSORS, PX4_SPIDEV_BARO, false, NULL },
#endif
#ifdef PX4_I2C_OBDEV_BMP280
{ BMP280_BUS_I2C_INTERNAL, "/dev/bmp280_i2c_int", nullptr, PX4_I2C_BUS_ONBOARD, PX4_I2C_OBDEV_BMP280, false, NULL },
{ BMP280_BUS_I2C_INTERNAL, "/dev/bmp280_i2c_int", &bmp280_i2c_interface, PX4_I2C_BUS_EXPANSION, PX4_I2C_OBDEV_BMP280, false, NULL },
#endif
#if defined(PX4_I2C_BUS_EXPANSION) && defined(PX4_I2C_EXT_OBDEV_BMP280)
{ BMP280_BUS_I2C_EXTERNAL, "/dev/bmp280_i2c_ext", nullptr, PX4_I2C_BUS_EXPANSION, PX4_I2C_EXT_OBDEV_BMP280, true, NULL },
{ BMP280_BUS_I2C_EXTERNAL, "/dev/bmp280_i2c_ext", &bmp280_i2c_interface, PX4_I2C_BUS_EXPANSION, PX4_I2C_EXT_OBDEV_BMP280, true, NULL },
#endif
};
#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
@@ -645,14 +655,15 @@ bool
start_bus(struct bmp280_bus_option &bus)
{
if (bus.dev != nullptr) {
errx(1, "bus option already started");
PX4_ERR("bus option already started");
exit(1);
}
bmp280::IBMP280 *interface = bus.interface_constructor(bus.busnum, bus.device, bus.external);
if (interface->init() != OK) {
delete interface;
warnx("no device on bus %u", (unsigned)bus.busid);
PX4_WARN("no device on bus %u", (unsigned)bus.busid);
return false;
}
@@ -668,14 +679,17 @@ start_bus(struct bmp280_bus_option &bus)
/* set the poll rate to default, starts automatic data collection */
if (fd == -1) {
errx(1, "can't open baro device");
PX4_ERR("can't open baro device");
exit(1);
}
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
close(fd);
errx(1, "failed setting default poll rate");
PX4_ERR("failed setting default poll rate");
exit(1);
}
close(fd);
return true;
}
@@ -708,7 +722,9 @@ start(enum BMP280_BUS busid)
}
if (!started) {
errx(1, "driver start failed");
PX4_WARN("bus option number is %d", i);
PX4_ERR("driver start failed");
exit(1);
}
// one or more drivers started OK
@@ -728,7 +744,8 @@ struct bmp280_bus_option &find_bus(enum BMP280_BUS busid)
}
}
errx(1, "bus %u not started", (unsigned)busid);
PX4_ERR("bus %u not started", (unsigned)busid);
exit(1);
}
/**
@@ -749,30 +766,34 @@ test(enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY);
if (fd < 0) {
err(1, "open failed (try 'bmp280 start' if the driver is not running)");
PX4_ERR("open failed (try 'bmp280 start' if the driver is not running)");
exit(1);
}
/* do a simple demand read */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "immediate read failed");
PX4_ERR("immediate read failed");
exit(1);
}
warnx("single read");
warnx("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude);
warnx("temperature: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp);
PX4_WARN("single read");
PX4_WARN("pressure: %10.4f", (double)report.pressure);
PX4_WARN("altitude: %11.4f", (double)report.altitude);
PX4_WARN("temperature: %8.4f", (double)report.temperature);
PX4_WARN("time: %lld", report.timestamp);
/* set the queue depth to 10 */
if (OK != ioctl(fd, SENSORIOCSQUEUEDEPTH, 10)) {
errx(1, "failed to set queue depth");
PX4_ERR("failed to set queue depth");
exit(1);
}
/* start the sensor polling at 2Hz */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
errx(1, "failed to set 2Hz poll rate");
PX4_ERR("failed to set 2Hz poll rate");
exit(1);
}
/* read the sensor 5x and report each value */
@@ -785,25 +806,28 @@ test(enum BMP280_BUS busid)
ret = poll(&fds, 1, 2000);
if (ret != 1) {
errx(1, "timed out waiting for sensor data");
PX4_ERR("timed out waiting for sensor data");
exit(1);
}
/* now go get it */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "periodic read failed");
PX4_ERR("periodic read failed");
exit(1);
}
warnx("periodic read %u", i);
warnx("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude);
warnx("temperature K: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp);
PX4_WARN("periodic read %u", i);
PX4_WARN("pressure: %10.4f", (double)report.pressure);
PX4_WARN("altitude: %11.4f", (double)report.altitude);
PX4_WARN("temperature K: %8.4f", (double)report.temperature);
PX4_WARN("time: %lld", report.timestamp);
}
close(fd);
errx(0, "PASS");
PX4_ERR("PASS");
exit(0);
}
/**
@@ -818,15 +842,18 @@ reset(enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY);
if (fd < 0) {
err(1, "failed ");
PX4_ERR("failed ");
exit(1);
}
if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
err(1, "driver reset failed");
PX4_ERR("driver reset failed");
exit(1);
}
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
err(1, "driver poll restart failed");
PX4_ERR("driver poll restart failed");
exit(1);
}
exit(0);
@@ -842,7 +869,7 @@ info()
struct bmp280_bus_option &bus = bus_options[i];
if (bus.dev != nullptr) {
warnx("%s", bus.devpath);
PX4_WARN("%s", bus.devpath);
bus.dev->print_info();
}
}
@@ -866,12 +893,14 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY);
if (fd < 0) {
err(1, "open failed (try 'bmp280 start' if the driver is not running)");
PX4_ERR("open failed (try 'bmp280 start' if the driver is not running)");
exit(1);
}
/* start the sensor polling at max */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) {
errx(1, "failed to set poll rate");
PX4_ERR("failed to set poll rate");
exit(1);
}
/* average a few measurements */
@@ -888,14 +917,16 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
ret = poll(&fds, 1, 1000);
if (ret != 1) {
errx(1, "timed out waiting for sensor data");
PX4_ERR("timed out waiting for sensor data");
exit(1);
}
/* now go get it */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "sensor read failed");
PX4_ERR("sensor read failed");
exit(1);
}
pressure += report.pressure;
@@ -910,17 +941,18 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
const float g = 9.80665f; /* gravity constant in m/s/s */
const float R = 287.05f; /* ideal gas constant in J/kg/K */
warnx("averaged pressure %10.4fkPa at %um", (double)pressure, altitude);
PX4_WARN("averaged pressure %10.4fkPa at %um", (double)pressure, altitude);
p1 = pressure * (powf(((T1 + (a * (float)altitude)) / T1), (g / (a * R))));
warnx("calculated MSL pressure %10.4fkPa", (double)p1);
PX4_WARN("calculated MSL pressure %10.4fkPa", (double)p1);
/* save as integer Pa */
p1 *= 1000.0f;
if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) {
err(1, "BAROIOCSMSLPRESSURE");
PX4_ERR("BAROIOCSMSLPRESSURE");
exit(1);
}
close(fd);
@@ -930,16 +962,23 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
void
usage()
{
warnx("missing command: try 'start', 'info', 'test', 'test2', 'reset', 'calibrate'");
warnx("options:");
warnx(" -X (external I2C bus TODO)");
warnx(" -I (internal I2C bus TODO)");
warnx(" -S (external SPI bus)");
warnx(" -s (internal SPI bus)");
PX4_WARN("missing command: try 'start', 'info', 'test', 'test2', 'reset', 'calibrate'");
PX4_WARN("options:");
PX4_WARN(" -X (external I2C bus TODO)");
PX4_WARN(" -I (internal I2C bus TODO)");
PX4_WARN(" -S (external SPI bus)");
PX4_WARN(" -s (internal SPI bus)");
}
} // namespace
bmp280::IBMP280::~IBMP280()
{
}
int
bmp280_main(int argc, char *argv[])
{
@@ -951,12 +990,12 @@ bmp280_main(int argc, char *argv[])
switch (ch) {
case 'X':
busid = BMP280_BUS_I2C_EXTERNAL;
errx(1, "not supported yet");
break;
case 'I':
busid = BMP280_BUS_I2C_INTERNAL;
errx(1, "not supported yet");
//PX4_ERR("not supported yet");
//exit(1);
break;
case 'S':
@@ -1008,7 +1047,8 @@ bmp280_main(int argc, char *argv[])
*/
if (!strcmp(verb, "calibrate")) {
if (argc < 2) {
errx(1, "missing altitude");
PX4_ERR("missing altitude");
exit(1);
}
long altitude = strtol(argv[optind + 1], nullptr, 10);
@@ -1016,5 +1056,6 @@ bmp280_main(int argc, char *argv[])
bmp280::calibrate(altitude, busid);
}
errx(1, "unrecognized command, try 'start', 'test', 'reset' or 'info'");
PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'");
exit(1);
}
+21 -19
View File
@@ -50,28 +50,28 @@
#define BPM280_VALUE_ID 0x58 /* chip id */
#define BPM280_VALUE_RESET 0xB6 /* reset */
#define BPM280_STATUS_MEASURING 1<<3 /* if in process of measure */
#define BPM280_STATUS_COPING 1<<0 /* if in process of data copy */
#define BPM280_STATUS_MEASURING (1<<3) /* if in process of measure */
#define BPM280_STATUS_COPING (1<<0) /* if in process of data copy */
#define BPM280_CTRL_P0 0x0<<2 /* no p measure */
#define BPM280_CTRL_P1 0x1<<2
#define BPM280_CTRL_P2 0x2<<2
#define BPM280_CTRL_P4 0x3<<2
#define BPM280_CTRL_P8 0x4<<2
#define BPM280_CTRL_P16 0x5<<2
#define BPM280_CTRL_P0 (0x0<<2) /* no p measure */
#define BPM280_CTRL_P1 (0x1<<2)
#define BPM280_CTRL_P2 (0x2<<2)
#define BPM280_CTRL_P4 (0x3<<2)
#define BPM280_CTRL_P8 (0x4<<2)
#define BPM280_CTRL_P16 (0x5<<2)
#define BPM280_CTRL_T0 0x0<<5 /* no t measure */
#define BPM280_CTRL_T1 0x1<<5
#define BPM280_CTRL_T2 0x2<<5
#define BPM280_CTRL_T4 0x3<<5
#define BPM280_CTRL_T8 0x4<<5
#define BPM280_CTRL_T16 0x5<<5
#define BPM280_CTRL_T0 (0x0<<5) /* no t measure */
#define BPM280_CTRL_T1 (0x1<<5)
#define BPM280_CTRL_T2 (0x2<<5)
#define BPM280_CTRL_T4 (0x3<<5)
#define BPM280_CTRL_T8 (0x4<<5)
#define BPM280_CTRL_T16 (0x5<<5)
#define BPM280_CONFIG_F0 0x0<<2 /* no filter */
#define BPM280_CONFIG_F2 0x1<<2
#define BPM280_CONFIG_F4 0x2<<2
#define BPM280_CONFIG_F8 0x3<<2
#define BPM280_CONFIG_F16 0x4<<2
#define BPM280_CONFIG_F0 (0x0<<2) /* no filter */
#define BPM280_CONFIG_F2 (0x1<<2)
#define BPM280_CONFIG_F4 (0x2<<2)
#define BPM280_CONFIG_F8 (0x3<<2)
#define BPM280_CONFIG_F16 (0x4<<2)
#define BPM280_CTRL_MODE_SLEEP 0x0
@@ -146,6 +146,8 @@ public:
} /* namespace */
/* interface factories */
extern bmp280::IBMP280 *bmp280_spi_interface(uint8_t busnum, uint8_t device, bool external);
extern bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint8_t device, bool external);
+142
View File
@@ -0,0 +1,142 @@
/****************************************************************************
*
* Copyright (c) 2016 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 bmp280_spi.cpp
*
* SPI interface for BMP280
*/
#include <px4_config.h>
#include <drivers/bmp280/bmp280.h>
#include <drivers/device/i2c.h>
#include "board_config.h"
#if defined(PX4_I2C_OBDEV_BMP280) || defined(PX4_I2C_EXT_OBDEV_BMP280)
class BMP280_I2C: public device::I2C, public bmp280::IBMP280
{
public:
BMP280_I2C(uint8_t bus, uint8_t device, bool external);
~BMP280_I2C();
bool is_external();
int init();
uint8_t get_reg(uint8_t addr);
int set_reg(uint8_t value, uint8_t addr);
bmp280::data_s *get_data(uint8_t addr);
bmp280::calibration_s *get_calibration(uint8_t addr);
private:
struct bmp280::calibration_s _cal;
struct bmp280::data_s _data;
bool _external;
};
bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint8_t device, bool external)
{
return new BMP280_I2C(busnum, device, external);
}
BMP280_I2C::BMP280_I2C(uint8_t bus, uint8_t device, bool external) :
I2C("BMP280_I2C", nullptr, bus, device, 100 * 1000)
{
_external = external;
}
BMP280_I2C::~BMP280_I2C()
{
}
bool BMP280_I2C::is_external()
{
return _external;
}
int BMP280_I2C::init()
{
return I2C::init();
}
uint8_t BMP280_I2C::get_reg(uint8_t addr)
{
uint8_t cmd[2] = { (uint8_t)(addr), 0};
transfer(&cmd[0], 1, &cmd[1], 1);
return cmd[1];
}
int BMP280_I2C::set_reg(uint8_t value, uint8_t addr)
{
uint8_t cmd[2] = { (uint8_t)(addr), value};
return transfer(cmd, sizeof(cmd), nullptr, 0);
}
bmp280::data_s *BMP280_I2C::get_data(uint8_t addr)
{
const uint8_t cmd = (uint8_t)(addr);
if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_data, sizeof(struct bmp280::data_s)) == OK) {
return (&_data);
} else {
return nullptr;
}
}
bmp280::calibration_s *BMP280_I2C::get_calibration(uint8_t addr)
{
const uint8_t cmd = (uint8_t)(addr) ;
if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_cal, sizeof(struct bmp280::calibration_s)) == OK) {
return &(_cal);
} else {
return nullptr;
}
}
#endif /* PX4_I2C_OBDEV_BMP280 || PX4_I2C_EXT_OBDEV_BMP280 */
-3
View File
@@ -93,9 +93,6 @@ BMP280_SPI::BMP280_SPI(uint8_t bus, spi_dev_e device, bool external) :
_external = external;
}
bmp280::IBMP280::~IBMP280()
{
}
BMP280_SPI::~BMP280_SPI()
{