diff --git a/CMakeLists.txt b/CMakeLists.txt index 568fbacdd2aec77566fd557771ae64d7e70df236..9f728f7790aed5d2023be436c06bd86f82cd5f14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -376,6 +376,10 @@ sbs_target(test-ubxgps-spi stm32f429zi_skyward_death_stack_x) add_executable(test-vn100 src/tests/sensors/test-vn100.cpp) sbs_target(test-vn100 stm32f407vg_stm32f4discovery) +add_executable(test-lis2mdl src/tests/sensors/test-lis2mdl.cpp) +sbs_target(test-lis2mdl stm32f407vg_stm32f4discovery) + + #-----------------------------------------------------------------------------# # Tests - Utils # #-----------------------------------------------------------------------------# diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index caea138a30977227222c31c1f0beae22b3f415f4..9aeb3c2e55c60eecdff7637ad8640054eea9b961 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -98,6 +98,7 @@ foreach(OPT_BOARD ${BOARDS}) ${SBS_BASE}/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp ${SBS_BASE}/src/shared/sensors/UBXGPS/UBXGPSSpi.cpp ${SBS_BASE}/src/shared/sensors/VN100/VN100.cpp + ${SBS_BASE}/src/shared/sensors/LIS2MDL/LIS2MDL.cpp # Calibration ${SBS_BASE}/src/shared/sensors/calibration/BiasCalibration/BiasCalibration.cpp diff --git a/src/shared/sensors/LIS2MDL/LIS2MDL.cpp b/src/shared/sensors/LIS2MDL/LIS2MDL.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65937053d3663ba2e2f9b7c833f77d4bd60b70b6 --- /dev/null +++ b/src/shared/sensors/LIS2MDL/LIS2MDL.cpp @@ -0,0 +1,76 @@ +/* Copyright (c) 2022 Skyward Experimental Rocketry + * Author: Giulia Ghirardini + * + * 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. + */ + +#include "LIS2MDL.h" + +#include <drivers/timer/TimestampTimer.h> + +namespace Boardcore +{ + +LIS2MDL::LIS2MDL(SPIBusInterface& bus, miosix::GpioPin pin, + SPIBusConfig spiConfig, Config config) + : mSlave(bus, pin, spiConfig), mConfig(config), currDiv(0), + isInitialized(false) +{ +} + +bool LIS2MDL::applyConfig(Config config) { return true; } + +bool LIS2MDL::init() +{ + if (isInitialized) + { + LOG_ERR(logger, "Attempted to initialized sensor twice but failed"); + lastError = ALREADY_INIT; + return false; + } + + { + SPITransaction spi(mSlave); + spi.writeRegister(CFG_REG_C, 4); + } + + { + SPITransaction spi(mSlave); + uint8_t res = spi.readRegister(WHO_AM_I); + + if (res != 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); + lastError = INVALID_WHOAMI; + return false; + } + } + + isInitialized = true; + return applyConfig(mConfig); +} + +bool LIS2MDL::selfTest() { return true; } + +LIS2MDLData LIS2MDL::sampleImpl() { return LIS2MDLData{}; } + +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/sensors/LIS2MDL/LIS2MDLData.h b/src/shared/sensors/LIS2MDL/LIS2MDLData.h new file mode 100644 index 0000000000000000000000000000000000000000..2a2e4ff0f90b5471bb654590c4fc7d750b1df93b --- /dev/null +++ b/src/shared/sensors/LIS2MDL/LIS2MDLData.h @@ -0,0 +1,64 @@ +/* Copyright (c) 2022 Skyward Experimental Rocketry + * Author: Giulia Ghirardini + * + * 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 <sensors/SensorData.h> + +namespace Boardcore +{ + +struct LIS2MDLData : public MagnetometerData, public TemperatureData +{ + LIS2MDLData() : MagnetometerData{0, 0.0, 0.0, 0.0}, TemperatureData{0, 0.0} + { + } + + LIS2MDLData(uint64_t t, float mx, float my, float mz, float deg) + : MagnetometerData{t, mx, my, mz}, TemperatureData{t, deg} + + { + } + + LIS2MDLData(MagnetometerData magData, TemperatureData tempData) + : MagnetometerData(magData), TemperatureData(tempData) + + { + } + + static std::string header() + { + return "magneticFieldTimestamp,magneticFieldX,magneticFieldY," + "magneticFieldZ," + "temperatureTimestamp," + "temp\n"; + } + + void print(std::ostream& os) const + { + os << magneticFieldTimestamp << "," << magneticFieldX << "," + << magneticFieldY << "," << magneticFieldZ << "," + << temperatureTimestamp << "," << temperature << "\n"; + } +}; + +} // namespace Boardcore \ No newline at end of file diff --git a/src/tests/sensors/test-lis2mdl.cpp b/src/tests/sensors/test-lis2mdl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6722967ecea593d70156ed926f572d746d06361a --- /dev/null +++ b/src/tests/sensors/test-lis2mdl.cpp @@ -0,0 +1,67 @@ +/* Copyright (c) 2020 Skyward Experimental Rocketry + * Author: Riccardo Musso + * + * 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. + */ + +#include <drivers/spi/SPIDriver.h> +#include <miosix.h> +#include <sensors/LIS2MDL/LIS2MDL.h> +#include <utils/Debug.h> + +using namespace Boardcore; +using namespace miosix; + +int main() +{ + GpioPin cs(GPIOA_BASE, 3), miso(GPIOA_BASE, 6), mosi(GPIOA_BASE, 7), + clk(GPIOA_BASE, 5); + + cs.mode(Mode::OUTPUT); + cs.high(); + + clk.mode(Mode::ALTERNATE); + clk.alternateFunction(5); + + miso.mode(Mode::ALTERNATE); + miso.alternateFunction(5); + + mosi.mode(Mode::ALTERNATE); + mosi.alternateFunction(5); + + SPIBus bus(SPI1); + + SPIBusConfig busConfig; + busConfig.clockDivider = SPI::ClockDivider::DIV_256; + busConfig.mode = SPI::Mode::MODE_0; + + LIS2MDL::Config config; + config.odr = LIS2MDL::ODR_10_HZ; + // config.scale = LIS2MDL::FS_16_GAUSS; + config.temperatureDivider = 5; + + LIS2MDL sensor(bus, cs, busConfig, config); + + if (!sensor.init()) + { + TRACE("LIS2MDL: Init failed"); + return 1; + } + TRACE("LIS2MDL: Init done"); +}