diff options
Diffstat (limited to 'src/drivers/ms5611')
-rw-r--r-- | src/drivers/ms5611/module.mk | 2 | ||||
-rw-r--r-- | src/drivers/ms5611/ms5611.cpp | 59 | ||||
-rw-r--r-- | src/drivers/ms5611/ms5611.h | 11 | ||||
-rw-r--r-- | src/drivers/ms5611/ms5611_i2c.cpp | 46 | ||||
-rw-r--r-- | src/drivers/ms5611/ms5611_spi.cpp | 72 |
5 files changed, 117 insertions, 73 deletions
diff --git a/src/drivers/ms5611/module.mk b/src/drivers/ms5611/module.mk index 3c4b0f093..20f8aa173 100644 --- a/src/drivers/ms5611/module.mk +++ b/src/drivers/ms5611/module.mk @@ -37,4 +37,4 @@ MODULE_COMMAND = ms5611 -SRCS = ms5611.cpp +SRCS = ms5611.cpp ms5611_spi.cpp ms5611_i2c.cpp diff --git a/src/drivers/ms5611/ms5611.cpp b/src/drivers/ms5611/ms5611.cpp index b8c396f7b..9d9888a90 100644 --- a/src/drivers/ms5611/ms5611.cpp +++ b/src/drivers/ms5611/ms5611.cpp @@ -93,7 +93,7 @@ static const int ERROR = -1; class MS5611 : public device::CDev { public: - MS5611(device::Device *interface); + MS5611(device::Device *interface, ms5611::prom_u &prom_buf); ~MS5611(); virtual int init(); @@ -109,7 +109,7 @@ public: protected: Device *_interface; - ms5611::prom_u _prom; + ms5611::prom_s _prom; struct work_s _work; unsigned _measure_ticks; @@ -191,9 +191,10 @@ protected: */ extern "C" __EXPORT int ms5611_main(int argc, char *argv[]); -MS5611::MS5611(device::Device *interface) : +MS5611::MS5611(device::Device *interface, ms5611::prom_u &prom_buf) : CDev("MS5611", BARO_DEVICE_PATH), _interface(interface), + _prom(prom_buf.s), _measure_ticks(0), _num_reports(0), _next_report(0), @@ -230,13 +231,11 @@ MS5611::~MS5611() int MS5611::init() { + int ret; - /* verify that the interface is ok */ - unsigned arg = (unsigned)&_prom; - _interface->ioctl(IOCTL_SET_PROMBUFFER, arg); - int ret = _interface->init(); + ret = CDev::init(); if (ret != OK) { - debug("interface init failed"); + debug("CDev init failed"); goto out; } @@ -598,14 +597,14 @@ MS5611::collect() if (_measure_phase == 0) { /* temperature offset (in ADC units) */ - int32_t dT = (int32_t)raw - ((int32_t)_prom.s.c5_reference_temp << 8); + int32_t dT = (int32_t)raw - ((int32_t)_prom.c5_reference_temp << 8); /* absolute temperature in centidegrees - note intermediate value is outside 32-bit range */ - _TEMP = 2000 + (int32_t)(((int64_t)dT * _prom.s.c6_temp_coeff_temp) >> 23); + _TEMP = 2000 + (int32_t)(((int64_t)dT * _prom.c6_temp_coeff_temp) >> 23); /* base sensor scale/offset values */ - _SENS = ((int64_t)_prom.s.c1_pressure_sens << 15) + (((int64_t)_prom.s.c3_temp_coeff_pres_sens * dT) >> 8); - _OFF = ((int64_t)_prom.s.c2_pressure_offset << 16) + (((int64_t)_prom.s.c4_temp_coeff_pres_offset * dT) >> 7); + _SENS = ((int64_t)_prom.c1_pressure_sens << 15) + (((int64_t)_prom.c3_temp_coeff_pres_sens * dT) >> 8); + _OFF = ((int64_t)_prom.c2_pressure_offset << 16) + (((int64_t)_prom.c4_temp_coeff_pres_offset * dT) >> 7); /* temperature compensation */ if (_TEMP < 2000) { @@ -711,14 +710,14 @@ MS5611::print_info() printf("OFF: %lld\n", _OFF); printf("MSL pressure: %10.4f\n", (double)(_msl_pressure / 100.f)); - printf("factory_setup %u\n", _prom.s.factory_setup); - printf("c1_pressure_sens %u\n", _prom.s.c1_pressure_sens); - printf("c2_pressure_offset %u\n", _prom.s.c2_pressure_offset); - printf("c3_temp_coeff_pres_sens %u\n", _prom.s.c3_temp_coeff_pres_sens); - printf("c4_temp_coeff_pres_offset %u\n", _prom.s.c4_temp_coeff_pres_offset); - printf("c5_reference_temp %u\n", _prom.s.c5_reference_temp); - printf("c6_temp_coeff_temp %u\n", _prom.s.c6_temp_coeff_temp); - printf("serial_and_crc %u\n", _prom.s.serial_and_crc); + printf("factory_setup %u\n", _prom.factory_setup); + printf("c1_pressure_sens %u\n", _prom.c1_pressure_sens); + printf("c2_pressure_offset %u\n", _prom.c2_pressure_offset); + printf("c3_temp_coeff_pres_sens %u\n", _prom.c3_temp_coeff_pres_sens); + printf("c4_temp_coeff_pres_offset %u\n", _prom.c4_temp_coeff_pres_offset); + printf("c5_reference_temp %u\n", _prom.c5_reference_temp); + printf("c6_temp_coeff_temp %u\n", _prom.c6_temp_coeff_temp); + printf("serial_and_crc %u\n", _prom.serial_and_crc); } /** @@ -789,6 +788,7 @@ void start() { int fd; + prom_u prom_buf; if (g_dev != nullptr) errx(1, "already started"); @@ -797,14 +797,19 @@ start() /* create the driver, try SPI first, fall back to I2C if unsuccessful */ if (MS5611_spi_interface != nullptr) - interface = MS5611_spi_interface(); + interface = MS5611_spi_interface(prom_buf); if (interface == nullptr && (MS5611_i2c_interface != nullptr)) - interface = MS5611_i2c_interface(); + interface = MS5611_i2c_interface(prom_buf); if (interface == nullptr) errx(1, "failed to allocate an interface"); - g_dev = new MS5611(interface); + if (interface->init() != OK) { + delete interface; + errx(1, "interface init failed"); + } + + g_dev = new MS5611(interface, prom_buf); if (g_dev == nullptr) { delete interface; errx(1, "failed to allocate driver"); @@ -814,10 +819,14 @@ start() /* set the poll rate to default, starts automatic data collection */ fd = open(BARO_DEVICE_PATH, O_RDONLY); - if (fd < 0) + if (fd < 0) { + warnx("can't open baro device"); goto fail; - if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) + } + if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { + warnx("failed setting default poll rate"); goto fail; + } exit(0); diff --git a/src/drivers/ms5611/ms5611.h b/src/drivers/ms5611/ms5611.h index 872011dc9..76fb84de8 100644 --- a/src/drivers/ms5611/ms5611.h +++ b/src/drivers/ms5611/ms5611.h @@ -45,15 +45,9 @@ #define ADDR_PROM_C1 0xA2 /* address of 6x 2 bytes calibration data */ /* interface ioctls */ -#define IOCTL_SET_PROMBUFFER 1 #define IOCTL_RESET 2 #define IOCTL_MEASURE 3 - -/* interface factories */ -extern device::Device *MS5611_spi_interface() weak_function; -extern device::Device *MS5611_i2c_interface() weak_function; - namespace ms5611 { @@ -84,3 +78,8 @@ union prom_u { extern bool crc4(uint16_t *n_prom); } /* namespace */ + +/* interface factories */ +extern device::Device *MS5611_spi_interface(ms5611::prom_u &prom_buf) weak_function; +extern device::Device *MS5611_i2c_interface(ms5611::prom_u &prom_buf) weak_function; + diff --git a/src/drivers/ms5611/ms5611_i2c.cpp b/src/drivers/ms5611/ms5611_i2c.cpp index 7f5667c28..06867cc9d 100644 --- a/src/drivers/ms5611/ms5611_i2c.cpp +++ b/src/drivers/ms5611/ms5611_i2c.cpp @@ -37,6 +37,25 @@ * I2C interface for MS5611 */ +/* XXX trim includes */ +#include <nuttx/config.h> + +#include <sys/types.h> +#include <stdint.h> +#include <stdbool.h> +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <unistd.h> + +#include <arch/board/board.h> + +#include <drivers/device/i2c.h> + +#include "ms5611.h" + +#ifdef PX4_I2C_OBDEV_MS5611 + #ifndef PX4_I2C_BUS_ONBOARD #define MS5611_BUS 1 #else @@ -48,12 +67,12 @@ -device::Device *MS5611_i2c_interface(); +device::Device *MS5611_i2c_interface(ms5611::prom_u &prom_buf); -class MS5611_I2C : device::I2C +class MS5611_I2C : public device::I2C { public: - MS5611_I2C(int bus); + MS5611_I2C(int bus, ms5611::prom_u &prom_buf); virtual ~MS5611_I2C(); virtual int init(); @@ -64,7 +83,7 @@ protected: virtual int probe(); private: - ms5611::prom_u *_prom + ms5611::prom_u &_prom; int _probe_address(uint8_t address); @@ -92,15 +111,12 @@ private: }; device::Device * -MS5611_i2c_interface() +MS5611_i2c_interface(ms5611::prom_u &prom_buf) { -#ifdef PX4_I2C_OBDEV_MS5611 - return new MS5611_I2C(MS5611_BUS); -#endif - return nullptr; + return new MS5611_I2C(MS5611_BUS, prom_buf); } -MS5611_I2C::MS5611_I2C(int bus, ms5611_prom_u &prom) : +MS5611_I2C::MS5611_I2C(int bus, ms5611::prom_u &prom) : I2C("MS5611_I2C", nullptr, bus, 0, 400000), _prom(prom) { @@ -146,11 +162,6 @@ MS5611_I2C::ioctl(unsigned operation, unsigned &arg) int ret; switch (operation) { - case IOCTL_SET_PROMBUFFER: - _prom = reinterpret_cast<ms5611_prom_u *>(arg); - ret = OK; - break; - case IOCTL_RESET: ret = _reset(); break; @@ -255,10 +266,11 @@ MS5611_I2C::_read_prom() /* assemble 16 bit value and convert from big endian (sensor) to little endian (MCU) */ cvt.b[0] = prom_buf[1]; cvt.b[1] = prom_buf[0]; - _prom->c[i] = cvt.w; + _prom.c[i] = cvt.w; } /* calculate CRC and return success/failure accordingly */ - return ms5611::crc4(&_prom->c[0]) ? OK : -EIO; + return ms5611::crc4(&_prom.c[0]) ? OK : -EIO; } +#endif /* PX4_I2C_OBDEV_MS5611 */ diff --git a/src/drivers/ms5611/ms5611_spi.cpp b/src/drivers/ms5611/ms5611_spi.cpp index eed8e1697..156832a61 100644 --- a/src/drivers/ms5611/ms5611_spi.cpp +++ b/src/drivers/ms5611/ms5611_spi.cpp @@ -37,31 +37,44 @@ * SPI interface for MS5611 */ +/* XXX trim includes */ +#include <nuttx/config.h> + +#include <sys/types.h> +#include <stdint.h> +#include <stdbool.h> +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <unistd.h> + +#include <arch/board/board.h> + +#include <drivers/device/spi.h> + +#include "ms5611.h" /* SPI protocol address bits */ #define DIR_READ (1<<7) #define DIR_WRITE (0<<7) #define ADDR_INCREMENT (1<<6) -device::Device *MS5611_spi_interface(); +#ifdef PX4_SPIDEV_BARO + +device::Device *MS5611_spi_interface(ms5611::prom_u &prom_buf); -class MS5611_SPI : device::SPI +class MS5611_SPI : public device::SPI { public: - MS5611_SPI(int bus, spi_dev_e device); + MS5611_SPI(int bus, spi_dev_e device, ms5611::prom_u &prom_buf); virtual ~MS5611_SPI(); virtual int init(); virtual int read(unsigned offset, void *data, unsigned count); virtual int ioctl(unsigned operation, unsigned &arg); -protected: - virtual int probe(); - private: - ms5611::prom_u *_prom - - int _probe_address(uint8_t address); + ms5611::prom_u &_prom; /** * Send a reset command to the MS5611. @@ -93,12 +106,19 @@ private: }; device::Device * -MS5611_spi_interface() +MS5611_spi_interface(ms5611::prom_u &prom_buf) +{ + return new MS5611_SPI(1 /* XXX MAGIC NUMBER */, (spi_dev_e)PX4_SPIDEV_BARO, prom_buf); +} + +MS5611_SPI::MS5611_SPI(int bus, spi_dev_e device, ms5611::prom_u &prom_buf) : + SPI("MS5611_SPI", nullptr, bus, device, SPIDEV_MODE3, 2000000), + _prom(prom_buf) +{ +} + +MS5611_SPI::~MS5611_SPI() { -#ifdef PX4_SPIDEV_BARO - return new MS5611_SPI(1 /* XXX MAGIC NUMBER */, (spi_dev_e)PX4_SPIDEV_BARO); -#endif - return nullptr; } int @@ -107,18 +127,24 @@ MS5611_SPI::init() int ret; ret = SPI::init(); - if (ret != OK) + if (ret != OK) { + debug("SPI init failed"); goto out; + } /* send reset command */ ret = _reset(); - if (ret != OK) + if (ret != OK) { + debug("reset failed"); goto out; + } /* read PROM */ ret = _read_prom(); - if (ret != OK) + if (ret != OK) { + debug("prom readout failed"); goto out; + } out: return ret; @@ -139,9 +165,9 @@ MS5611_SPI::read(unsigned offset, void *data, unsigned count) if (ret == OK) { /* fetch the raw value */ - cvt->b[0] = data[3]; - cvt->b[1] = data[2]; - cvt->b[2] = data[1]; + cvt->b[0] = buf[3]; + cvt->b[1] = buf[2]; + cvt->b[2] = buf[1]; cvt->b[3] = 0; ret = count; @@ -156,10 +182,6 @@ MS5611_SPI::ioctl(unsigned operation, unsigned &arg) int ret; switch (operation) { - case IOCTL_SET_PROMBUFFER: - _prom = reinterpret_cast<ms5611_prom_u *>(arg); - return OK; - case IOCTL_RESET: ret = _reset(); break; @@ -226,3 +248,5 @@ MS5611_SPI::_reg16(unsigned reg) return (uint16_t)(cmd[1] << 8) | cmd[2]; } + +#endif /* PX4_SPIDEV_BARO */ |