Skyward boardcore
Loading...
Searching...
No Matches
LIS2MDL.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Giulia Ghirardini
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
27#include <sensors/Sensor.h>
28
29#include "LIS2MDLData.h"
30
31namespace Boardcore
32{
33
37class LIS2MDL : public Sensor<LIS2MDLData>
38{
39public:
40 enum ODR : uint8_t
41 {
42 ODR_10_HZ = 0b00 << 2,
43 ODR_20_HZ = 0b01 << 2,
44 ODR_50_HZ = 0b10 << 2,
45 ODR_100_HZ = 0b11 << 2,
46 };
47
48 enum OperativeMode : uint8_t
49 {
51 MD_SINGLE = 0x01,
52 MD_IDLE0 = 0x02,
53 MD_IDLE1 = 0x03,
54 };
55
76
77 LIS2MDL(SPIBusInterface& bus, miosix::GpioPin pin, SPIBusConfig spiConfig,
78 Config config);
79
81
82 bool init() override;
83
84 bool selfTest() override;
85
100 bool applyConfig(Config config);
101
102protected:
103 LIS2MDLData sampleImpl() override;
104
105private:
106 SPISlave slave;
107 Config configuration;
108
109 unsigned tempCounter = 0;
110 bool isInitialized = false;
111
112 enum Registers : uint8_t
113 {
114 OFFSET_X_REG_L = 0x45,
115 OFFSET_X_REG_H = 0x46,
116 OFFSET_Y_REG_L = 0x47,
117 OFFSET_Y_REG_H = 0x48,
118 OFFSET_Z_REG_L = 0x49,
119 OFFSET_Z_REG_H = 0x4a,
120
121 WHO_AM_I = 0x4f,
122
123 CFG_REG_A = 0x60,
124 CFG_REG_B = 0x61,
125 CFG_REG_C = 0x62,
126
127 INT_CRTL_REG = 0x63,
128 INT_SOURCE_REG = 0x64,
129 INT_THS_L_REG = 0x65,
130 INT_THS_H_REG = 0x66,
131 STATUS_REG = 0x67,
132
133 OUTX_L_REG = 0x68,
134 OUTX_H_REG = 0x69,
135 OUTY_L_REG = 0x6a,
136 OUTY_H_REG = 0x6b,
137 OUTZ_L_REG = 0x6c,
138 OUTZ_H_REG = 0x6d,
139
140 TEMP_OUT_L_REG = 0x6e,
141 TEMP_OUT_H_REG = 0x6f,
142 };
143
144 static constexpr uint32_t WHO_AM_I_VALUE = 0x40;
145 static constexpr uint32_t CONTINUOS_CONVERSION = 0x0;
146
147 static constexpr float REFERENCE_TEMPERATURE = 25;
148 static constexpr float DEG_PER_LSB = 0.125;
149
150 static constexpr float GAUSS_PER_LSB = 0.0015;
151
152 static constexpr uint32_t ENABLE_TEMPERATURE_COMP = 1 << 7;
153 static constexpr uint32_t ENABLE_SELF_TEST = 1 << 1;
154 static constexpr uint32_t ENABLE_BDU = 1 << 4;
155 static constexpr uint32_t ENABLE_4WSPI = 1 << 2;
156 static constexpr uint32_t OFFSET_CANCELLATION = 1 << 1;
157 static constexpr uint32_t I2C_DISABLE = 1 << 5;
158
159 PrintLogger logger = Logging::getLogger("lis2mdl");
160};
161
162} // namespace Boardcore
Driver for LIS2MDL, a three-axis magnetic sensor.
Definition LIS2MDL.h:38
LIS2MDL(SPIBusInterface &bus, miosix::GpioPin pin, SPIBusConfig spiConfig, Config config)
Definition LIS2MDL.cpp:37
bool init() override
Initialize the sensor.
Definition LIS2MDL.cpp:52
bool applyConfig(Config config)
Overwrites the sensor settings.
Definition LIS2MDL.cpp:190
LIS2MDLData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition LIS2MDL.cpp:205
@ ODR_100_HZ
100 Hz
Definition LIS2MDL.h:45
bool selfTest() override
Check if the sensor is working.
Definition LIS2MDL.cpp:85
static SPIBusConfig getDefaultSPIConfig()
Definition LIS2MDL.cpp:43
static PrintLogger getLogger(const string &name)
Interface for low level access of a SPI bus as a master.
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
Sensor configuration.
Definition LIS2MDL.h:64
OperativeMode deviceMode
Definition LIS2MDL.h:66
unsigned temperatureDivider
Divide the temperature sampling rate.
Definition LIS2MDL.h:74
SPI Bus configuration for a specific slave.
Contains information about a single SPI slave device.