diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp index 2d9183b678484102aeba1c108ff95dde2ab32ea1..0c5c0e53c94dee6f3df78f063a2756cc04732d2e 100644 --- a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp +++ b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp @@ -31,6 +31,8 @@ LIS3MDL::LIS3MDL(SPIBusInterface& bus, miosix::GpioPin pin, SPIBusConfig spiConfig, Config config) : slave(bus, pin, spiConfig), configuration(config) { + slave.config.byteOrder = SPI::Order::LSB_FIRST; + slave.config.mode = SPI::Mode::MODE_3; } bool LIS3MDL::init() @@ -49,8 +51,8 @@ bool LIS3MDL::init() if (res != WHO_AM_I_VALUE) { LOG_ERR(logger, - "WHO_AM_I value differs from expectation: read 0x{02x} " - "but expected 0x{02x}", + "WHO_AM_I value differs from expectation: read 0x{:x} " + "but expected 0x{:x}", res, WHO_AM_I_VALUE); lastError = INVALID_WHOAMI; return false; @@ -124,9 +126,13 @@ bool LIS3MDL::selfTest() bool passed = true; for (int j = 0; j < 3; ++j) + { if (deltas[j] < (deltaRange[j][0] - t) || deltas[j] > (deltaRange[j][1] + t)) + { passed = false; + } + } // Reset configuration, then return applyConfig(configuration); @@ -209,15 +215,11 @@ LIS3MDLData LIS3MDL::sampleImpl() SPITransaction spi(slave); LIS3MDLData newData; - tempCounter++; if (configuration.temperatureDivider != 0 && tempCounter % configuration.temperatureDivider == 0) { - uint8_t values[2]; - spi.readRegisters(TEMP_OUT_L, values, sizeof(values)); - - int16_t outTemp = values[1] << 8 | values[0]; + int16_t outTemp = spi.readRegister16(TEMP_OUT_L | INCREMENT_REG_FLAG); newData.temperatureTimestamp = TimestampTimer::getTimestamp(); newData.temperature = DEG_PER_LSB * outTemp; newData.temperature += REFERENCE_TEMPERATURE; @@ -227,17 +229,13 @@ LIS3MDLData LIS3MDL::sampleImpl() newData.temperature = lastSample.temperature; } - uint8_t values[6]; - spi.readRegisters(OUT_X_L, values, sizeof(values)); - - int16_t outX = values[1] << 8 | values[0]; - int16_t outY = values[3] << 8 | values[2]; - int16_t outZ = values[5] << 8 | values[4]; + int16_t values[3]; + spi.readRegisters(OUT_X_L | INCREMENT_REG_FLAG, + reinterpret_cast<uint8_t*>(values), sizeof(values)); - newData.magneticFieldTimestamp = TimestampTimer::getTimestamp(); - newData.magneticFieldX = currentUnit * outX; - newData.magneticFieldY = currentUnit * outY; - newData.magneticFieldZ = currentUnit * outZ; + newData.magneticFieldX = currentUnit * values[0]; + newData.magneticFieldY = currentUnit * values[1]; + newData.magneticFieldZ = currentUnit * values[2]; return newData; } diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.h b/src/shared/sensors/LIS3MDL/LIS3MDL.h index a60e02f8403ffd0e511f6f86d1f145cf2229ab60..ed98e2768a85097cdde7ea0e13b74fbc83d56289 100644 --- a/src/shared/sensors/LIS3MDL/LIS3MDL.h +++ b/src/shared/sensors/LIS3MDL/LIS3MDL.h @@ -203,6 +203,13 @@ private: static constexpr uint32_t ENABLE_INT_Y = 1 << 6; static constexpr uint32_t ENABLE_INT_Z = 1 << 5; + /** + * This flag is needed because the device requires the 7th address bit + * asserted in order to increment the address in transaction with more than + * one bytes. + */ + static constexpr uint8_t INCREMENT_REG_FLAG = 0x40; + PrintLogger logger = Logging::getLogger("lis3mdl"); }; diff --git a/src/tests/sensors/test-lis3mdl.cpp b/src/tests/sensors/test-lis3mdl.cpp index af8abf3d3535ddc8fe187479abd88a8dbb857070..1298c92ae0ed8776d9ae95ed7c8b251a5c46c932 100644 --- a/src/tests/sensors/test-lis3mdl.cpp +++ b/src/tests/sensors/test-lis3mdl.cpp @@ -30,8 +30,8 @@ using namespace miosix; int main() { - GpioPin cs(GPIOB_BASE, 1), miso(GPIOB_BASE, 4), mosi(GPIOB_BASE, 5), - clk(GPIOB_BASE, 3); + GpioPin cs(GPIOG_BASE, 6), miso(GPIOA_BASE, 6), mosi(GPIOA_BASE, 7), + clk(GPIOA_BASE, 5); cs.mode(Mode::OUTPUT); cs.high();