diff --git a/src/shared/sensors/LIS2MDL/LIS2MDL.cpp b/src/shared/sensors/LIS2MDL/LIS2MDL.cpp index 9017aaa365ce084fa2e52e955cab6a972234fc37..e18bb65a83dfb0e1badd8c470e0774936da5fc8d 100644 --- a/src/shared/sensors/LIS2MDL/LIS2MDL.cpp +++ b/src/shared/sensors/LIS2MDL/LIS2MDL.cpp @@ -60,19 +60,20 @@ bool LIS2MDL::init() { SPITransaction spi(slave); - spi.writeRegister(CFG_REG_C, ENABLE_4WSPI | I2C_DISABLE); + spi.writeRegister(CFG_REG_C, + LIS2MDLDefs::ENABLE_4WSPI | LIS2MDLDefs::I2C_DISABLE); } { SPITransaction spi(slave); uint8_t res = spi.readRegister(WHO_AM_I); - if (res != WHO_AM_I_VALUE) + if (res != LIS2MDLDefs::WHO_AM_I_VALUE) { LOG_ERR(logger, "WHO_AM_I value differs from expectation: read 0x{:x} " "but expected 0x{:x}", - res, WHO_AM_I_VALUE); + res, LIS2MDLDefs::WHO_AM_I_VALUE); lastError = INVALID_WHOAMI; return false; } @@ -105,11 +106,12 @@ bool LIS2MDL::selfTest() // 1. Set configuration for selfTest procedure. selfTest still not enabled { SPITransaction spi(slave); - spi.writeRegister(CFG_REG_A, - ENABLE_TEMPERATURE_COMP | ODR_100_HZ | MD_CONTINUOUS); - spi.writeRegister(CFG_REG_B, - spi.readRegister(CFG_REG_B) | OFFSET_CANCELLATION); - spi.writeRegister(CFG_REG_C, spi.readRegister(CFG_REG_C) | ENABLE_BDU); + spi.writeRegister(CFG_REG_A, LIS2MDLDefs::ENABLE_TEMPERATURE_COMP | + ODR_100_HZ | MD_CONTINUOUS); + spi.writeRegister(CFG_REG_B, spi.readRegister(CFG_REG_B) | + LIS2MDLDefs::OFFSET_CANCELLATION); + spi.writeRegister( + CFG_REG_C, spi.readRegister(CFG_REG_C) | LIS2MDLDefs::ENABLE_BDU); } // Wait for power up, ~20ms for a stable output @@ -134,8 +136,8 @@ bool LIS2MDL::selfTest() // 3. Enable self-test { SPITransaction spi(slave); - spi.writeRegister(CFG_REG_C, - spi.readRegister(CFG_REG_C) | ENABLE_SELF_TEST); + spi.writeRegister(CFG_REG_C, spi.readRegister(CFG_REG_C) | + LIS2MDLDefs::ENABLE_SELF_TEST); } // Wait 60ms (suggested in AN) @@ -221,8 +223,8 @@ LIS2MDLData LIS2MDL::sampleImpl() { int16_t outTemp = spi.readRegister16(TEMP_OUT_L_REG); newData.temperatureTimestamp = TimestampTimer::getTimestamp(); - newData.temperature = DEG_PER_LSB * outTemp; - newData.temperature += REFERENCE_TEMPERATURE; + newData.temperature = LIS2MDLDefs::DEG_PER_LSB * outTemp; + newData.temperature += LIS2MDLDefs::REFERENCE_TEMPERATURE; } else { @@ -237,9 +239,9 @@ LIS2MDLData LIS2MDL::sampleImpl() int16_t outZ = values[5] << 8 | values[4]; newData.magneticFieldTimestamp = TimestampTimer::getTimestamp(); - newData.magneticFieldX = GAUSS_PER_LSB * outX; - newData.magneticFieldY = GAUSS_PER_LSB * outY; - newData.magneticFieldZ = GAUSS_PER_LSB * outZ; + newData.magneticFieldX = LIS2MDLDefs::GAUSS_PER_LSB * outX; + newData.magneticFieldY = LIS2MDLDefs::GAUSS_PER_LSB * outY; + newData.magneticFieldZ = LIS2MDLDefs::GAUSS_PER_LSB * outZ; return newData; } diff --git a/src/shared/sensors/LIS2MDL/LIS2MDL.h b/src/shared/sensors/LIS2MDL/LIS2MDL.h index 7e781f9557c2cea4ad0d446bf33d32ea50e7c8b1..f96f5c6ef14a6c2cd09957a051960a22bea4d75a 100644 --- a/src/shared/sensors/LIS2MDL/LIS2MDL.h +++ b/src/shared/sensors/LIS2MDL/LIS2MDL.h @@ -27,6 +27,7 @@ #include <sensors/Sensor.h> #include "LIS2MDLData.h" +#include "LIS2MDLDefs.h" namespace Boardcore { @@ -141,21 +142,6 @@ private: TEMP_OUT_H_REG = 0x6f, }; - static constexpr uint32_t WHO_AM_I_VALUE = 0x40; - static constexpr uint32_t CONTINUOS_CONVERSION = 0x0; - - static constexpr float REFERENCE_TEMPERATURE = 25; - static constexpr float DEG_PER_LSB = 0.125; - - static constexpr float GAUSS_PER_LSB = 0.0015; - - static constexpr uint32_t ENABLE_TEMPERATURE_COMP = 1 << 7; - static constexpr uint32_t ENABLE_SELF_TEST = 1 << 1; - static constexpr uint32_t ENABLE_BDU = 1 << 4; - static constexpr uint32_t ENABLE_4WSPI = 1 << 2; - static constexpr uint32_t OFFSET_CANCELLATION = 1 << 1; - static constexpr uint32_t I2C_DISABLE = 1 << 5; - PrintLogger logger = Logging::getLogger("lis2mdl"); }; diff --git a/src/shared/sensors/LIS2MDL/LIS2MDLDefs.h b/src/shared/sensors/LIS2MDL/LIS2MDLDefs.h new file mode 100644 index 0000000000000000000000000000000000000000..fb41a3d58fb6b2ac01ff66937cf1b8067dcddf3c --- /dev/null +++ b/src/shared/sensors/LIS2MDL/LIS2MDLDefs.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Fabrizio Monti + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <stdint.h> + +namespace Boardcore +{ + +namespace LIS2MDLDefs +{ +static constexpr uint32_t WHO_AM_I_VALUE = 0x40; +static constexpr uint32_t CONTINUOS_CONVERSION = 0x0; + +static constexpr float REFERENCE_TEMPERATURE = 25; +static constexpr float DEG_PER_LSB = 0.125; + +static constexpr float GAUSS_PER_LSB = 0.0015; + +static constexpr uint32_t ENABLE_TEMPERATURE_COMP = 1 << 7; +static constexpr uint32_t ENABLE_SELF_TEST = 1 << 1; +static constexpr uint32_t ENABLE_BDU = 1 << 4; +static constexpr uint32_t ENABLE_4WSPI = 1 << 2; +static constexpr uint32_t OFFSET_CANCELLATION = 1 << 1; +static constexpr uint32_t I2C_DISABLE = 1 << 5; +} // namespace LIS2MDLDefs +} // namespace Boardcore \ No newline at end of file