diff --git a/src/shared/sensors/ND015X/ND015A.cpp b/src/shared/sensors/ND015X/ND015A.cpp index 5ad1ecb88b6e22f2d2de4cc369a0e376a0ff7d8a..3d4fe9c28685a83f22ece470e158dfee5762c80d 100644 --- a/src/shared/sensors/ND015X/ND015A.cpp +++ b/src/shared/sensors/ND015X/ND015A.cpp @@ -48,7 +48,8 @@ bool ND015A::init() // check if the model returned by the sensor matches with the correct model for (int i = 4; i < 10; i++) { - if ((uint8_t)(&data + i) != (sensorModel[i - 4])) + if (static_cast<uint8_t>(data[i]) != + static_cast<uint8_t>(sensorModel[i - 4])) { LOG_ERR(logger, "sensor model number did not match"); return false; diff --git a/src/shared/sensors/ND015X/ND015D.cpp b/src/shared/sensors/ND015X/ND015D.cpp index 59c9349f527c0b7a58282437ba9c939bf964653b..ec5d2eff35fbadf982d2e42de99a9678000072c6 100644 --- a/src/shared/sensors/ND015X/ND015D.cpp +++ b/src/shared/sensors/ND015X/ND015D.cpp @@ -34,25 +34,29 @@ ND015D::ND015D(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig) bool ND015D::init() { - uint8_t data[10]; + uint8_t* data; + NDD015ADataExtended.pressure = + (modeByte << 8) | rateByte; // updating the first 2 bytes with the + // correct sensor settings SPITransaction spi(slave); - spi.read(data, sizeof(data)); + data = reinterpret_cast<uint8_t*>(&NDD015ADataExtended); - // the following monstrosity is needed to check if the model number read - // from the SPI is correct, the numbers are the ASCII encoding of "ND015D" + spi.transfer(data, sizeof(NDD015ADataExtended)); - if (data[9] == 0x44 && data[8] == 0x35 && data[7] == 0x31 && - data[6] == 0x30 && data[5] == 0x44 && data[4] == 0x4E) + // check if the model returned by the sensor matches with the correct model + for (int i = 4; i < 10; i++) { - return true; - } - else - { - LOG_ERR(logger, "sensor model number did not match"); - return false; + if (static_cast<uint8_t>(data[i]) != + static_cast<uint8_t>(sensorModel[i - 4])) + { + LOG_ERR(logger, "sensor model number did not match"); + return false; + } } + + return true; } bool ND015D::selfTest() { return true; } @@ -68,6 +72,11 @@ void ND015D::setOutputDataRate(uint8_t odr) LOG_ERR(logger, "odr setting not valid, using default value (0x1C)"); rateByte = 0x1C; } + + SPITransaction spi(slave); + + uint16_t SPIDataOut = (modeByte << 8) | rateByte; + spi.transfer16(SPIDataOut); } void ND015D::setFullScaleRange(FullScaleRange fs) @@ -103,21 +112,41 @@ void ND015D::setFullScaleRange(FullScaleRange fs) default: break; } + + SPITransaction spi(slave); + + uint16_t SPIDataOut = (modeByte << 8) | rateByte; + spi.transfer16(SPIDataOut); } void ND015D::setIOWatchdog(IOWatchdogEnable iow) { modeByte = (modeByte & ~IO_WATCHDOG_MASK) | iow; + + SPITransaction spi(slave); + + uint16_t SPIDataOut = (modeByte << 8) | rateByte; + spi.transfer16(SPIDataOut); } void ND015D::setBWLimitFilter(BWLimitFilter bwl) { modeByte = (modeByte & ~BW_LIMIT_MASK) | bwl; + + SPITransaction spi(slave); + + uint16_t SPIDataOut = (modeByte << 8) | rateByte; + spi.transfer16(SPIDataOut); } void ND015D::setNotch(NotchEnable ntc) { modeByte = (modeByte & ~NOTCH_MASK) | ntc; + + SPITransaction spi(slave); + + uint16_t SPIDataOut = (modeByte << 8) | rateByte; + spi.transfer16(SPIDataOut); } ND015XData ND015D::sampleImpl() @@ -127,10 +156,6 @@ ND015XData ND015D::sampleImpl() SPITransaction spi(slave); - spi.transfer16(SPIDataOut); // we need to make an SPI transaction before - // reading the data to make sure the proper - // settings are used - uint16_t SPIDataIn = spi.transfer16(SPIDataOut); data.pressure = (short)SPIDataIn / (0.9 * pow(2, 15)) * range; diff --git a/src/shared/sensors/ND015X/ND015D.h b/src/shared/sensors/ND015X/ND015D.h index 78a7ae08d8b1235c639cdaeb2aef531cc65d0a83..840748dd2b4cd81fa8fc379e000116d7caa0dfd1 100644 --- a/src/shared/sensors/ND015X/ND015D.h +++ b/src/shared/sensors/ND015X/ND015D.h @@ -111,9 +111,19 @@ protected: private: SPISlave slave; - uint8_t modeByte = 0xF3; // settings for the mode control register - uint8_t rateByte = 0x1C; // settings for the rate control register - short range = 1; + uint8_t modeByte = 0xF3; // settings for the mode control register + uint8_t rateByte = 0x1C; // settings for the rate control register + short range = 1; + string sensorModel = "ND015D"; + + struct + { + uint16_t pressure; + uint16_t temperature; + uint8_t model[8]; + uint8_t serial[4]; + uint8_t build[6]; + } NDD015ADataExtended = {0}; enum RegisterMask : uint8_t {