Skip to content
Snippets Groups Projects
Commit df63f6af authored by Federico Mandelli's avatar Federico Mandelli
Browse files

[LIS3MDL] Fixed casting bug inside lis3mdl driver

parent 4b974e83
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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");
};
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment