From ebfae73108af109bff893906bde082f16a0f8ddf Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sat, 5 Sep 2015 22:06:15 +0200 Subject: [PATCH] MS5611 code style fix --- src/drivers/ms5611/ms5611.h | 2 +- src/drivers/ms5611/ms5611_i2c.cpp | 34 ++++--- src/drivers/ms5611/ms5611_nuttx.cpp | 152 ++++++++++++++++++---------- src/drivers/ms5611/ms5611_posix.cpp | 133 ++++++++++++++++-------- src/drivers/ms5611/ms5611_sim.cpp | 15 +-- src/drivers/ms5611/ms5611_spi.cpp | 41 +++++--- 6 files changed, 246 insertions(+), 131 deletions(-) diff --git a/src/drivers/ms5611/ms5611.h b/src/drivers/ms5611/ms5611.h index 1d6239467d..a7e305b2aa 100644 --- a/src/drivers/ms5611/ms5611.h +++ b/src/drivers/ms5611/ms5611.h @@ -83,4 +83,4 @@ extern bool crc4(uint16_t *n_prom); extern device::Device *MS5611_spi_interface(ms5611::prom_u &prom_buf, uint8_t busnum) __attribute__((weak)); extern device::Device *MS5611_i2c_interface(ms5611::prom_u &prom_buf, uint8_t busnum) __attribute__((weak)); extern device::Device *MS5611_sim_interface(ms5611::prom_u &prom_buf, uint8_t busnum) __attribute__((weak)); -typedef device::Device* (*MS5611_constructor)(ms5611::prom_u &prom_buf, uint8_t busnum); +typedef device::Device *(*MS5611_constructor)(ms5611::prom_u &prom_buf, uint8_t busnum); diff --git a/src/drivers/ms5611/ms5611_i2c.cpp b/src/drivers/ms5611/ms5611_i2c.cpp index 8f66304065..74f67c0f26 100644 --- a/src/drivers/ms5611/ms5611_i2c.cpp +++ b/src/drivers/ms5611/ms5611_i2c.cpp @@ -31,11 +31,11 @@ * ****************************************************************************/ - /** - * @file ms5611_i2c.cpp - * - * I2C interface for MS5611 - */ +/** + * @file ms5611_i2c.cpp + * + * I2C interface for MS5611 + */ /* XXX trim includes */ #include @@ -116,13 +116,13 @@ MS5611_i2c_interface(ms5611::prom_u &prom_buf, uint8_t busnum) } MS5611_I2C::MS5611_I2C(uint8_t bus, ms5611::prom_u &prom) : - I2C("MS5611_I2C", + I2C("MS5611_I2C", #ifdef __PX4_NUTTX -nullptr, bus, 0, 400000 + nullptr, bus, 0, 400000 #else -"/dev/MS5611_I2C", bus, 0 + "/dev/MS5611_I2C", bus, 0 #endif -), + ), _prom(prom) { } @@ -150,6 +150,7 @@ MS5611_I2C::read(device::file_t *handlep, char *data, size_t buflen) /* read the most recent measurement */ uint8_t cmd = 0; int ret = transfer(&cmd, 1, &buf[0], 3); + if (ret == PX4_OK) { /* fetch the raw value */ cvt->b[0] = buf[2]; @@ -191,9 +192,9 @@ MS5611_I2C::probe() if ((PX4_OK == _probe_address(MS5611_ADDRESS_1)) || (PX4_OK == _probe_address(MS5611_ADDRESS_2))) { /* - * Disable retries; we may enable them selectively in some cases, + * Disable retries; we may enable them selectively in some cases, * but the device gets confused if we retry some of the commands. - */ + */ _retries = 0; return PX4_OK; } @@ -208,12 +209,14 @@ MS5611_I2C::_probe_address(uint8_t address) set_address(address); /* send reset command */ - if (PX4_OK != _reset()) + if (PX4_OK != _reset()) { return -EIO; + } /* read PROM */ - if (PX4_OK != _read_prom()) + if (PX4_OK != _read_prom()) { return -EIO; + } return PX4_OK; } @@ -239,7 +242,7 @@ int MS5611_I2C::_measure(unsigned addr) { /* - * Disable retries on this command; we can't know whether failure + * Disable retries on this command; we can't know whether failure * means the device did or did not see the command. */ _retries = 0; @@ -267,8 +270,9 @@ MS5611_I2C::_read_prom() for (int i = 0; i < 8; i++) { uint8_t cmd = ADDR_PROM_SETUP + (i * 2); - if (PX4_OK != transfer(&cmd, 1, &prom_buf[0], 2)) + if (PX4_OK != transfer(&cmd, 1, &prom_buf[0], 2)) { break; + } /* assemble 16 bit value and convert from big endian (sensor) to little endian (MCU) */ cvt.b[0] = prom_buf[1]; diff --git a/src/drivers/ms5611/ms5611_nuttx.cpp b/src/drivers/ms5611/ms5611_nuttx.cpp index 4a304e130b..847d3e3a03 100644 --- a/src/drivers/ms5611/ms5611_nuttx.cpp +++ b/src/drivers/ms5611/ms5611_nuttx.cpp @@ -106,7 +106,7 @@ static const int ERROR = -1; class MS5611 : public device::CDev { public: - MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char* path); + MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char *path); ~MS5611(); virtual int init(); @@ -214,7 +214,7 @@ protected: */ extern "C" __EXPORT int ms5611_main(int argc, char *argv[]); -MS5611::MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char* path) : +MS5611::MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char *path) : CDev("MS5611", path), _interface(interface), _prom(prom_buf.s), @@ -243,12 +243,14 @@ MS5611::~MS5611() /* make sure we are truly inactive */ stop_cycle(); - if (_class_instance != -1) + if (_class_instance != -1) { unregister_class_devname(get_devname(), _class_instance); + } /* free any existing reports */ - if (_reports != nullptr) + if (_reports != nullptr) { delete _reports; + } // free perf counters perf_free(_sample_perf); @@ -265,6 +267,7 @@ MS5611::init() int ret; ret = CDev::init(); + if (ret != OK) { DEVICE_DEBUG("CDev init failed"); goto out; @@ -321,7 +324,7 @@ MS5611::init() ret = OK; _baro_topic = orb_advertise_multi(ORB_ID(sensor_baro), &brp, - &_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT); + &_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT); if (_baro_topic == nullptr) { @@ -342,8 +345,9 @@ MS5611::read(struct file *filp, char *buffer, size_t buflen) int ret = 0; /* buffer must be large enough */ - if (count < 1) + if (count < 1) { return -ENOSPC; + } /* if automatic measurement is enabled */ if (_measure_ticks > 0) { @@ -396,8 +400,9 @@ MS5611::read(struct file *filp, char *buffer, size_t buflen) } /* state machine will have generated a report, copy it out */ - if (_reports->get(brp)) + if (_reports->get(brp)) { ret = sizeof(*brp); + } } while (0); @@ -412,20 +417,20 @@ MS5611::ioctl(struct file *filp, int cmd, unsigned long arg) case SENSORIOCSPOLLRATE: { switch (arg) { - /* switching to manual polling */ + /* switching to manual polling */ case SENSOR_POLLRATE_MANUAL: stop_cycle(); _measure_ticks = 0; return OK; - /* external signalling not supported */ + /* external signalling not supported */ case SENSOR_POLLRATE_EXTERNAL: - /* zero would be bad */ + /* zero would be bad */ case 0: return -EINVAL; - /* set default/max polling rate */ + /* set default/max polling rate */ case SENSOR_POLLRATE_MAX: case SENSOR_POLLRATE_DEFAULT: { /* do we need to start internal polling? */ @@ -435,13 +440,14 @@ MS5611::ioctl(struct file *filp, int cmd, unsigned long arg) _measure_ticks = USEC2TICK(MS5611_CONVERSION_INTERVAL); /* if we need to start the poll state machine, do it */ - if (want_start) + if (want_start) { start_cycle(); + } return OK; } - /* adjust to a legal polling interval in Hz */ + /* adjust to a legal polling interval in Hz */ default: { /* do we need to start internal polling? */ bool want_start = (_measure_ticks == 0); @@ -450,15 +456,17 @@ MS5611::ioctl(struct file *filp, int cmd, unsigned long arg) unsigned ticks = USEC2TICK(1000000 / arg); /* check against maximum rate */ - if (ticks < USEC2TICK(MS5611_CONVERSION_INTERVAL)) + if (ticks < USEC2TICK(MS5611_CONVERSION_INTERVAL)) { return -EINVAL; + } /* update interval for next measurement */ _measure_ticks = ticks; /* if we need to start the poll state machine, do it */ - if (want_start) + if (want_start) { start_cycle(); + } return OK; } @@ -466,24 +474,28 @@ MS5611::ioctl(struct file *filp, int cmd, unsigned long arg) } case SENSORIOCGPOLLRATE: - if (_measure_ticks == 0) + if (_measure_ticks == 0) { return SENSOR_POLLRATE_MANUAL; + } return (1000 / _measure_ticks); case SENSORIOCSQUEUEDEPTH: { - /* lower bound is mandatory, upper bound is a sanity check */ - if ((arg < 1) || (arg > 100)) - return -EINVAL; + /* lower bound is mandatory, upper bound is a sanity check */ + if ((arg < 1) || (arg > 100)) { + return -EINVAL; + } + + irqstate_t flags = irqsave(); + + if (!_reports->resize(arg)) { + irqrestore(flags); + return -ENOMEM; + } - irqstate_t flags = irqsave(); - if (!_reports->resize(arg)) { irqrestore(flags); - return -ENOMEM; + return OK; } - irqrestore(flags); - return OK; - } case SENSORIOCGQUEUEDEPTH: return _reports->size(); @@ -498,8 +510,9 @@ MS5611::ioctl(struct file *filp, int cmd, unsigned long arg) case BAROIOCSMSLPRESSURE: /* range-check for sanity */ - if ((arg < 80000) || (arg > 120000)) + if ((arg < 80000) || (arg > 120000)) { return -EINVAL; + } _msl_pressure = arg; return OK; @@ -554,6 +567,7 @@ MS5611::cycle() /* perform collection */ ret = collect(); + if (ret != OK) { if (ret == -6) { /* @@ -564,6 +578,7 @@ MS5611::cycle() } else { //DEVICE_LOG("collection error %d", ret); } + /* issue a reset command to the sensor */ _interface->ioctl(IOCTL_RESET, dummy); /* reset the collection state machine and try again - we need @@ -598,6 +613,7 @@ MS5611::cycle() /* measurement phase */ ret = measure(); + if (ret != OK) { /* issue a reset command to the sensor */ _interface->ioctl(IOCTL_RESET, dummy); @@ -633,8 +649,10 @@ MS5611::measure() * Send the command to begin measuring. */ ret = _interface->ioctl(IOCTL_MEASURE, addr); - if (OK != ret) + + if (OK != ret) { perf_count(_comms_errors); + } perf_end(_measure_perf); @@ -652,10 +670,11 @@ MS5611::collect() struct baro_report report; /* this should be fairly close to the end of the conversion, so the best approximation of the time */ report.timestamp = hrt_absolute_time(); - report.error_count = perf_event_count(_comms_errors); + report.error_count = perf_event_count(_comms_errors); /* read the most recent measurement - read offset/size are hardcoded in the interface */ ret = _interface->read(0, (void *)&raw, 0); + if (ret < 0) { perf_count(_comms_errors); perf_end(_sample_perf); @@ -883,11 +902,13 @@ crc4(uint16_t *n_prom) bool start_bus(struct ms5611_bus_option &bus) { - if (bus.dev != nullptr) - errx(1,"bus option already started"); + if (bus.dev != nullptr) { + errx(1, "bus option already started"); + } prom_u prom_buf; device::Device *interface = bus.interface_constructor(prom_buf, bus.busnum); + if (interface->init() != OK) { delete interface; warnx("no device on bus %u", (unsigned)bus.busid); @@ -895,6 +916,7 @@ start_bus(struct ms5611_bus_option &bus) } bus.dev = new MS5611(interface, prom_buf, bus.devpath); + if (bus.dev != nullptr && OK != bus.dev->init()) { delete bus.dev; bus.dev = NULL; @@ -907,6 +929,7 @@ start_bus(struct ms5611_bus_option &bus) if (fd == -1) { errx(1, "can't open baro device"); } + if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { close(fd); errx(1, "failed setting default poll rate"); @@ -929,20 +952,23 @@ start(enum MS5611_BUS busid) uint8_t i; bool started = false; - for (i=0; iprint_info(); } } + exit(0); } @@ -1084,12 +1124,14 @@ calibrate(unsigned altitude, enum MS5611_BUS busid) fd = open(bus.devpath, O_RDONLY); - if (fd < 0) + if (fd < 0) { err(1, "open failed (try 'ms5611 start' if the driver is not running)"); + } /* start the sensor polling at max */ - if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) + if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) { errx(1, "failed to set poll rate"); + } /* average a few measurements */ pressure = 0.0f; @@ -1104,14 +1146,16 @@ calibrate(unsigned altitude, enum MS5611_BUS busid) fds.events = POLLIN; ret = poll(&fds, 1, 1000); - if (ret != 1) + if (ret != 1) { errx(1, "timed out waiting for sensor data"); + } /* now go get it */ sz = read(fd, &report, sizeof(report)); - if (sz != sizeof(report)) + if (sz != sizeof(report)) { err(1, "sensor read failed"); + } pressure += report.pressure; } @@ -1134,8 +1178,9 @@ calibrate(unsigned altitude, enum MS5611_BUS busid) /* save as integer Pa */ p1 *= 1000.0f; - if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) + if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) { err(1, "BAROIOCSMSLPRESSURE"); + } close(fd); exit(0); @@ -1166,15 +1211,19 @@ ms5611_main(int argc, char *argv[]) case 'X': busid = MS5611_BUS_I2C_EXTERNAL; break; + case 'I': busid = MS5611_BUS_I2C_INTERNAL; break; + case 'S': busid = MS5611_BUS_SPI_EXTERNAL; break; + case 's': busid = MS5611_BUS_SPI_INTERNAL; break; + default: ms5611::usage(); exit(0); @@ -1215,10 +1264,11 @@ ms5611_main(int argc, char *argv[]) * Perform MSL pressure calibration given an altitude in metres */ if (!strcmp(verb, "calibrate")) { - if (argc < 2) + if (argc < 2) { errx(1, "missing altitude"); + } - long altitude = strtol(argv[optind+1], nullptr, 10); + long altitude = strtol(argv[optind + 1], nullptr, 10); ms5611::calibrate(altitude, busid); } diff --git a/src/drivers/ms5611/ms5611_posix.cpp b/src/drivers/ms5611/ms5611_posix.cpp index d41a186c38..b4b22f6c35 100644 --- a/src/drivers/ms5611/ms5611_posix.cpp +++ b/src/drivers/ms5611/ms5611_posix.cpp @@ -105,7 +105,7 @@ static const int ERROR = -1; class MS5611 : public device::VDev { public: - MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char* path); + MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char *path); ~MS5611(); virtual int init(); @@ -211,7 +211,7 @@ protected: */ extern "C" __EXPORT int ms5611_main(int argc, char *argv[]); -MS5611::MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char* path) : +MS5611::MS5611(device::Device *interface, ms5611::prom_u &prom_buf, const char *path) : VDev("MS5611", path), _interface(interface), _prom(prom_buf.s), @@ -240,12 +240,14 @@ MS5611::~MS5611() /* make sure we are truly inactive */ stop_cycle(); - if (_class_instance != -1) + if (_class_instance != -1) { unregister_class_devname(get_devname(), _class_instance); + } /* free any existing reports */ - if (_reports != nullptr) + if (_reports != nullptr) { delete _reports; + } // free perf counters perf_free(_sample_perf); @@ -263,6 +265,7 @@ MS5611::init() warnx("MS5611::init"); ret = VDev::init(); + if (ret != OK) { DEVICE_DEBUG("VDev init failed"); goto out; @@ -323,11 +326,12 @@ MS5611::init() ret = OK; _baro_topic = orb_advertise_multi(ORB_ID(sensor_baro), &brp, - &_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT); + &_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT); if (_baro_topic == nullptr) { warnx("failed to create sensor_baro publication"); } + //warnx("sensor_baro publication %ld", _baro_topic); } while (0); @@ -344,8 +348,9 @@ MS5611::read(device::file_t *handlep, char *buffer, size_t buflen) int ret = 0; /* buffer must be large enough */ - if (count < 1) + if (count < 1) { return -ENOSPC; + } /* if automatic measurement is enabled */ if (_measure_ticks > 0) { @@ -398,8 +403,9 @@ MS5611::read(device::file_t *handlep, char *buffer, size_t buflen) } /* state machine will have generated a report, copy it out */ - if (_reports->get(brp)) + if (_reports->get(brp)) { ret = sizeof(*brp); + } } while (0); @@ -414,20 +420,20 @@ MS5611::ioctl(device::file_t *handlep, int cmd, unsigned long arg) case SENSORIOCSPOLLRATE: { switch (arg) { - /* switching to manual polling */ + /* switching to manual polling */ case SENSOR_POLLRATE_MANUAL: stop_cycle(); _measure_ticks = 0; return OK; - /* external signalling not supported */ + /* external signalling not supported */ case SENSOR_POLLRATE_EXTERNAL: - /* zero would be bad */ + /* zero would be bad */ case 0: return -EINVAL; - /* set default/max polling rate */ + /* set default/max polling rate */ case SENSOR_POLLRATE_MAX: case SENSOR_POLLRATE_DEFAULT: { /* do we need to start internal polling? */ @@ -437,13 +443,14 @@ MS5611::ioctl(device::file_t *handlep, int cmd, unsigned long arg) _measure_ticks = USEC2TICK(MS5611_CONVERSION_INTERVAL); /* if we need to start the poll state machine, do it */ - if (want_start) + if (want_start) { start_cycle(); + } return OK; } - /* adjust to a legal polling interval in Hz */ + /* adjust to a legal polling interval in Hz */ default: { /* do we need to start internal polling? */ bool want_start = (_measure_ticks == 0); @@ -452,15 +459,17 @@ MS5611::ioctl(device::file_t *handlep, int cmd, unsigned long arg) unsigned ticks = USEC2TICK(1000000 / arg); /* check against maximum rate */ - if ((unsigned long)ticks < USEC2TICK(MS5611_CONVERSION_INTERVAL)) + if ((unsigned long)ticks < USEC2TICK(MS5611_CONVERSION_INTERVAL)) { return -EINVAL; + } /* update interval for next measurement */ _measure_ticks = ticks; /* if we need to start the poll state machine, do it */ - if (want_start) + if (want_start) { start_cycle(); + } return OK; } @@ -468,21 +477,24 @@ MS5611::ioctl(device::file_t *handlep, int cmd, unsigned long arg) } case SENSORIOCGPOLLRATE: - if (_measure_ticks == 0) + if (_measure_ticks == 0) { return SENSOR_POLLRATE_MANUAL; + } return (1000 / _measure_ticks); case SENSORIOCSQUEUEDEPTH: { - /* lower bound is mandatory, upper bound is a sanity check */ - if ((arg < 1) || (arg > 100)) - return -EINVAL; + /* lower bound is mandatory, upper bound is a sanity check */ + if ((arg < 1) || (arg > 100)) { + return -EINVAL; + } - if (!_reports->resize(arg)) { - return -ENOMEM; + if (!_reports->resize(arg)) { + return -ENOMEM; + } + + return OK; } - return OK; - } case SENSORIOCGQUEUEDEPTH: return _reports->size(); @@ -497,8 +509,9 @@ MS5611::ioctl(device::file_t *handlep, int cmd, unsigned long arg) case BAROIOCSMSLPRESSURE: /* range-check for sanity */ - if ((arg < 80000) || (arg > 120000)) + if ((arg < 80000) || (arg > 120000)) { return -EINVAL; + } _msl_pressure = arg; return OK; @@ -553,6 +566,7 @@ MS5611::cycle() /* perform collection */ ret = collect(); + if (ret != OK) { if (ret == -6) { /* @@ -563,6 +577,7 @@ MS5611::cycle() } else { //DEVICE_LOG("collection error %d", ret); } + /* issue a reset command to the sensor */ _interface->dev_ioctl(IOCTL_RESET, dummy); /* reset the collection state machine and try again */ @@ -594,6 +609,7 @@ MS5611::cycle() /* measurement phase */ ret = measure(); + if (ret != OK) { //DEVICE_LOG("measure error %d", ret); /* issue a reset command to the sensor */ @@ -630,8 +646,10 @@ MS5611::measure() * Send the command to begin measuring. */ ret = _interface->dev_ioctl(IOCTL_MEASURE, addr); - if (OK != ret) + + if (OK != ret) { perf_count(_comms_errors); + } perf_end(_measure_perf); @@ -649,10 +667,11 @@ MS5611::collect() struct baro_report report; /* this should be fairly close to the end of the conversion, so the best approximation of the time */ report.timestamp = hrt_absolute_time(); - report.error_count = perf_event_count(_comms_errors); + report.error_count = perf_event_count(_comms_errors); /* read the most recent measurement - read offset/size are hardcoded in the interface */ ret = _interface->dev_read(0, (void *)&raw, 0); + if (ret < 0) { perf_count(_comms_errors); perf_end(_sample_perf); @@ -745,8 +764,8 @@ MS5611::collect() if (_baro_topic != (orb_advert_t)(-1)) { /* publish it */ orb_publish(ORB_ID(sensor_baro), _baro_topic, &report); - } - else { + + } else { printf("MS5611::collect _baro_topic not initialized\n"); } } @@ -897,6 +916,7 @@ start_bus(struct ms5611_bus_option &bus) prom_u prom_buf; device::Device *interface = bus.interface_constructor(prom_buf, bus.busnum); + if (interface->init() != OK) { delete interface; warnx("no device on bus %u", (unsigned)bus.busid); @@ -904,13 +924,14 @@ start_bus(struct ms5611_bus_option &bus) } bus.dev = new MS5611(interface, prom_buf, bus.devpath); + if (bus.dev != nullptr && OK != bus.dev->init()) { delete bus.dev; bus.dev = NULL; warnx("bus init failed %p", bus.dev); return false; } - + int fd = px4_open(bus.devpath, O_RDONLY); /* set the poll rate to default, starts automatic data collection */ @@ -918,6 +939,7 @@ start_bus(struct ms5611_bus_option &bus) warnx("can't open baro device"); return false; } + if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { px4_close(fd); warnx("failed setting default poll rate"); @@ -941,15 +963,17 @@ start(enum MS5611_BUS busid) uint8_t i; bool started = false; - for (i=0; iprint_info(); } } + return 0; } @@ -1118,12 +1151,15 @@ int calibrate(unsigned altitude, enum MS5611_BUS busid) { struct ms5611_bus_option bus; + if (!find_bus(busid, bus)) { return 1; } struct baro_report report; + float pressure; + float p1; int fd; @@ -1224,18 +1260,23 @@ ms5611_main(int argc, char *argv[]) /* jump over start/off/etc and look at options first */ int myoptind = 1; const char *myoptarg = NULL; + while ((ch = px4_getopt(argc, argv, "XIS", &myoptind, &myoptarg)) != EOF) { printf("ch = %d\n", ch); + switch (ch) { case 'X': busid = MS5611_BUS_I2C_EXTERNAL; break; + case 'I': busid = MS5611_BUS_I2C_INTERNAL; break; + case 'S': busid = MS5611_BUS_SIM_EXTERNAL; break; + default: ms5611::usage(); return 1; @@ -1247,42 +1288,48 @@ ms5611_main(int argc, char *argv[]) /* * Start/load the driver. */ - if (!strcmp(verb, "start")) + if (!strcmp(verb, "start")) { ret = ms5611::start(busid); + } /* * Test the driver/device. */ - else if (!strcmp(verb, "test")) + else if (!strcmp(verb, "test")) { ret = ms5611::test(busid); + } /* * Reset the driver. */ - else if (!strcmp(verb, "reset")) + else if (!strcmp(verb, "reset")) { ret = ms5611::reset(busid); + } /* * Print driver information. */ - else if (!strcmp(verb, "info")) + else if (!strcmp(verb, "info")) { ret = ms5611::info(); + } /* * Perform MSL pressure calibration given an altitude in metres */ else if (!strcmp(verb, "calibrate")) { - if (argc < 2) - PX4_WARN("missing altitude"); + if (argc < 2) { + PX4_WARN("missing altitude"); + } - long altitude = strtol(argv[optind+1], nullptr, 10); + long altitude = strtol(argv[optind + 1], nullptr, 10); ret = ms5611::calibrate(altitude, busid); - } - else { + + } else { ms5611::usage(); warnx("unrecognised command, try 'start', 'test', 'reset' or 'info'"); return 1; } + return ret; } diff --git a/src/drivers/ms5611/ms5611_sim.cpp b/src/drivers/ms5611/ms5611_sim.cpp index ab7656b9c3..72c02f6b38 100644 --- a/src/drivers/ms5611/ms5611_sim.cpp +++ b/src/drivers/ms5611/ms5611_sim.cpp @@ -31,11 +31,11 @@ * ****************************************************************************/ - /** - * @file ms5611_i2c.cpp - * - * SIM interface for MS5611 - */ +/** + * @file ms5611_i2c.cpp + * + * SIM interface for MS5611 + */ /* XXX trim includes */ #include @@ -127,6 +127,7 @@ MS5611_SIM::dev_read(unsigned offset, void *data, unsigned count) /* read the most recent measurement */ uint8_t cmd = 0; int ret = transfer(&cmd, 1, &buf[0], 3); + if (ret == PX4_OK) { /* fetch the raw value */ cvt->b[0] = buf[2]; @@ -178,7 +179,7 @@ int MS5611_SIM::_measure(unsigned addr) { /* - * Disable retries on this command; we can't know whether failure + * Disable retries on this command; we can't know whether failure * means the device did or did not see the command. */ _retries = 0; @@ -197,7 +198,7 @@ MS5611_SIM::_read_prom() int MS5611_SIM::transfer(const uint8_t *send, unsigned send_len, - uint8_t *recv, unsigned recv_len) + uint8_t *recv, unsigned recv_len) { // TODO add Simulation data connection so calls retrieve // data from the simulator diff --git a/src/drivers/ms5611/ms5611_spi.cpp b/src/drivers/ms5611/ms5611_spi.cpp index 37c9fb4c12..78192c3a98 100644 --- a/src/drivers/ms5611/ms5611_spi.cpp +++ b/src/drivers/ms5611/ms5611_spi.cpp @@ -31,11 +31,11 @@ * ****************************************************************************/ - /** - * @file ms5611_spi.cpp - * - * SPI interface for MS5611 - */ +/** + * @file ms5611_spi.cpp + * + * SPI interface for MS5611 + */ /* XXX trim includes */ #include @@ -117,6 +117,7 @@ device::Device * MS5611_spi_interface(ms5611::prom_u &prom_buf, uint8_t busnum) { #ifdef PX4_SPI_BUS_EXT + if (busnum == PX4_SPI_BUS_EXT) { #ifdef PX4_SPIDEV_EXT_BARO return new MS5611_SPI(busnum, (spi_dev_e)PX4_SPIDEV_EXT_BARO, prom_buf); @@ -124,12 +125,13 @@ MS5611_spi_interface(ms5611::prom_u &prom_buf, uint8_t busnum) return nullptr; #endif } + #endif return new MS5611_SPI(busnum, (spi_dev_e)PX4_SPIDEV_BARO, prom_buf); } MS5611_SPI::MS5611_SPI(uint8_t bus, spi_dev_e device, ms5611::prom_u &prom_buf) : - SPI("MS5611_SPI", nullptr, bus, device, SPIDEV_MODE3, 11*1000*1000 /* will be rounded to 10.4 MHz */), + SPI("MS5611_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */), _prom(prom_buf) { } @@ -144,6 +146,7 @@ MS5611_SPI::init() int ret; ret = SPI::init(); + if (ret != OK) { DEVICE_DEBUG("SPI init failed"); goto out; @@ -151,6 +154,7 @@ MS5611_SPI::init() /* send reset command */ ret = _reset(); + if (ret != OK) { DEVICE_DEBUG("reset failed"); goto out; @@ -158,6 +162,7 @@ MS5611_SPI::init() /* read PROM */ ret = _read_prom(); + if (ret != OK) { DEVICE_DEBUG("prom readout failed"); goto out; @@ -214,6 +219,7 @@ MS5611_SPI::ioctl(unsigned operation, unsigned &arg) errno = ret; return -1; } + return 0; } @@ -244,25 +250,32 @@ MS5611_SPI::_read_prom() usleep(3000); /* read and convert PROM words */ - bool all_zero = true; + bool all_zero = true; + for (int i = 0; i < 8; i++) { uint8_t cmd = (ADDR_PROM_SETUP + (i * 2)); _prom.c[i] = _reg16(cmd); - if (_prom.c[i] != 0) + + if (_prom.c[i] != 0) { all_zero = false; - //DEVICE_DEBUG("prom[%u]=0x%x", (unsigned)i, (unsigned)_prom.c[i]); + } + + //DEVICE_DEBUG("prom[%u]=0x%x", (unsigned)i, (unsigned)_prom.c[i]); } /* calculate CRC and return success/failure accordingly */ int ret = ms5611::crc4(&_prom.c[0]) ? OK : -EIO; - if (ret != OK) { + + if (ret != OK) { DEVICE_DEBUG("crc failed"); - } - if (all_zero) { + } + + if (all_zero) { DEVICE_DEBUG("prom all zero"); ret = -EIO; - } - return ret; + } + + return ret; } uint16_t