diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp index 34b7ce31945d75b44f6cc0f05005aa9c21e413be..bdb11ce427c0db4f9a130da6210af0a9d783501a 100644 --- a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp +++ b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp @@ -27,6 +27,37 @@ namespace Boardcore { +namespace LIS3MDLDefs +{ + +static constexpr uint32_t WHO_AM_I_VALUE = 0x3d; +static constexpr uint32_t CONTINUOS_CONVERSION = 0x0; + +static constexpr uint32_t REFERENCE_TEMPERATURE = 25; +static constexpr float DEG_PER_LSB = 0.125; + +static constexpr float GAUSS_PER_LSB_FS_4 = 0.000146156; +static constexpr float GAUSS_PER_LSB_FS_8 = 0.000292312; +static constexpr float GAUSS_PER_LSB_FS_12 = 0.000438404; +static constexpr float GAUSS_PER_LSB_FS_16 = 0.000584454; + +static constexpr uint32_t ENABLE_TEMPERATURE = 1 << 7; +static constexpr uint32_t ENABLE_SELF_TEST = 1 << 0; +static constexpr uint32_t ENABLE_BDU = 1 << 6; + +static constexpr uint32_t ENABLE_INT_PIN = 1 << 0; +static constexpr uint32_t ENABLE_INT_X = 1 << 7; +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; +} // namespace LIS3MDLDefs + LIS3MDL::LIS3MDL(SPIBusInterface& bus, miosix::GpioPin pin, SPIBusConfig spiConfig, Config config) : slave(bus, pin, spiConfig), configuration(config) @@ -48,12 +79,12 @@ bool LIS3MDL::init() SPITransaction spi(slave); uint8_t res = spi.readRegister(WHO_AM_I); - if (res != WHO_AM_I_VALUE) + if (res != LIS3MDLDefs::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, LIS3MDLDefs::WHO_AM_I_VALUE); lastError = INVALID_WHOAMI; return false; } @@ -108,7 +139,7 @@ bool LIS3MDL::selfTest() { SPITransaction spi(slave); - spi.writeRegister(CTRL_REG1, ODR_20_HZ | ENABLE_SELF_TEST | + spi.writeRegister(CTRL_REG1, ODR_20_HZ | LIS3MDLDefs::ENABLE_SELF_TEST | (OM_ULTRA_HIGH_POWER << 4)); spi.writeRegister(CTRL_REG2, FS_12_GAUSS); spi.writeRegister(CTRL_REG4, OM_ULTRA_HIGH_POWER << 2); @@ -157,7 +188,7 @@ bool LIS3MDL::applyConfig(Config config) // CTRL_REG1 if (config.temperatureDivider != 0) { - reg = ENABLE_TEMPERATURE; + reg = LIS3MDLDefs::ENABLE_TEMPERATURE; } reg |= config.odr; @@ -173,7 +204,7 @@ bool LIS3MDL::applyConfig(Config config) err |= spi.readRegister(CTRL_REG2) != reg; // CTRL_REG3 - reg = CONTINUOS_CONVERSION; + reg = LIS3MDLDefs::CONTINUOS_CONVERSION; spi.writeRegister(CTRL_REG3, reg); err |= spi.readRegister(CTRL_REG3) != reg; @@ -184,7 +215,7 @@ bool LIS3MDL::applyConfig(Config config) // CTRL_REG5 if (config.doBlockDataUpdate) - reg = ENABLE_BDU; + reg = LIS3MDLDefs::ENABLE_BDU; else reg = 0; @@ -219,10 +250,11 @@ LIS3MDLData LIS3MDL::sampleImpl() if (configuration.temperatureDivider != 0 && tempCounter % configuration.temperatureDivider == 0) { - int16_t outTemp = spi.readRegister16(TEMP_OUT_L | INCREMENT_REG_FLAG); + int16_t outTemp = + spi.readRegister16(TEMP_OUT_L | LIS3MDLDefs::INCREMENT_REG_FLAG); newData.temperatureTimestamp = TimestampTimer::getTimestamp(); - newData.temperature = DEG_PER_LSB * outTemp; - newData.temperature += REFERENCE_TEMPERATURE; + newData.temperature = LIS3MDLDefs::DEG_PER_LSB * outTemp; + newData.temperature += LIS3MDLDefs::REFERENCE_TEMPERATURE; } else { @@ -230,7 +262,7 @@ LIS3MDLData LIS3MDL::sampleImpl() } int16_t values[3]; - spi.readRegisters(OUT_X_L | INCREMENT_REG_FLAG, + spi.readRegisters(OUT_X_L | LIS3MDLDefs::INCREMENT_REG_FLAG, reinterpret_cast<uint8_t*>(values), sizeof(values)); newData.magneticFieldTimestamp = TimestampTimer::getTimestamp(); newData.magneticFieldX = currentUnit * values[0]; @@ -245,19 +277,19 @@ void LIS3MDL::updateUnit(FullScale fs) switch (fs) { case FS_4_GAUSS: - currentUnit = GAUSS_PER_LSB_FS_4; + currentUnit = LIS3MDLDefs::GAUSS_PER_LSB_FS_4; break; case FS_8_GAUSS: - currentUnit = GAUSS_PER_LSB_FS_8; + currentUnit = LIS3MDLDefs::GAUSS_PER_LSB_FS_8; break; case FS_12_GAUSS: - currentUnit = GAUSS_PER_LSB_FS_12; + currentUnit = LIS3MDLDefs::GAUSS_PER_LSB_FS_12; break; case FS_16_GAUSS: - currentUnit = GAUSS_PER_LSB_FS_16; + currentUnit = LIS3MDLDefs::GAUSS_PER_LSB_FS_16; break; } }; diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.h b/src/shared/sensors/LIS3MDL/LIS3MDL.h index 2e0a5d90514b563930a3ef55532ceb86111fbf5a..259c423cf52010e4c4a2a5ef0c56a67c96f6dff0 100644 --- a/src/shared/sensors/LIS3MDL/LIS3MDL.h +++ b/src/shared/sensors/LIS3MDL/LIS3MDL.h @@ -184,33 +184,6 @@ private: INT_THS_H = 0x33, }; - static constexpr uint32_t WHO_AM_I_VALUE = 0x3d; - static constexpr uint32_t CONTINUOS_CONVERSION = 0x0; - - static constexpr uint32_t REFERENCE_TEMPERATURE = 25; - static constexpr float DEG_PER_LSB = 0.125; - - static constexpr float GAUSS_PER_LSB_FS_4 = 0.000146156; - static constexpr float GAUSS_PER_LSB_FS_8 = 0.000292312; - static constexpr float GAUSS_PER_LSB_FS_12 = 0.000438404; - static constexpr float GAUSS_PER_LSB_FS_16 = 0.000584454; - - static constexpr uint32_t ENABLE_TEMPERATURE = 1 << 7; - static constexpr uint32_t ENABLE_SELF_TEST = 1 << 0; - static constexpr uint32_t ENABLE_BDU = 1 << 6; - - static constexpr uint32_t ENABLE_INT_PIN = 1 << 0; - static constexpr uint32_t ENABLE_INT_X = 1 << 7; - 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/shared/sensors/UBXGPS/UBXGPSSerial.cpp b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp index c7051b97e07f6faaa5637081e19384a7ce0cc3b2..d6b719572161e1fb7650651be0ed2fa96dda67f6 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp @@ -31,6 +31,12 @@ using namespace miosix; namespace Boardcore { +namespace UBXGPSSerialDefs +{ +static constexpr unsigned int RESET_SLEEP_TIME = 5000; // [ms] +static constexpr unsigned int MAX_TRIES = 5; // [1] +} // namespace UBXGPSSerialDefs + UBXGPSSerial::UBXGPSSerial(int baudrate, uint8_t sampleRate, USARTType* usartNumber, int defaultBaudrate) { @@ -135,7 +141,7 @@ bool UBXGPSSerial::reset() return false; // Do not interact with the module while it is resetting - miosix::Thread::sleep(RESET_SLEEP_TIME); + miosix::Thread::sleep(UBXGPSSerialDefs::RESET_SLEEP_TIME); return true; } @@ -306,11 +312,11 @@ bool UBXGPSSerial::writeUBXFrame(const UBXFrame& frame) bool UBXGPSSerial::safeWriteUBXFrame(const UBXFrame& frame) { - for (unsigned int i = 0; i < MAX_TRIES; i++) + for (unsigned int i = 0; i < UBXGPSSerialDefs::MAX_TRIES; i++) { if (i > 0) LOG_DEBUG(logger, "Retrying (attempt {:#d} of {:#d})...", i + 1, - MAX_TRIES); + UBXGPSSerialDefs::MAX_TRIES); if (!writeUBXFrame(frame)) return false; @@ -340,7 +346,7 @@ bool UBXGPSSerial::safeWriteUBXFrame(const UBXFrame& frame) return true; } - LOG_ERR(logger, "Gave up after {:#d} tries", MAX_TRIES); + LOG_ERR(logger, "Gave up after {:#d} tries", UBXGPSSerialDefs::MAX_TRIES); return false; } diff --git a/src/shared/sensors/UBXGPS/UBXGPSSerial.h b/src/shared/sensors/UBXGPS/UBXGPSSerial.h index 9187466a692e354a3454855c7d8f43073835a845..faabfe313fd6e712a0dd436522eec7f3ac2c930b 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.h +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.h @@ -169,9 +169,6 @@ private: UBXGPSData threadSample{}; PrintLogger logger = Logging::getLogger("ubloxgps"); - - static constexpr unsigned int RESET_SLEEP_TIME = 5000; // [ms] - static constexpr unsigned int MAX_TRIES = 5; // [1] }; } // namespace Boardcore