Skyward boardcore
Loading...
Searching...
No Matches
LPS22DF.cpp
Go to the documentation of this file.
1/* Copyright (c) 2023 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#include "LPS22DF.h"
24
26
27#include "LPS22DFDefs.h"
28
29using namespace Boardcore::LPS22DFDefs;
30
31namespace Boardcore
32{
33
34LPS22DF::LPS22DF(SPIBusInterface& bus, miosix::GpioPin cs)
35 : slave(bus, cs, getDefaultSPIConfig())
36{
37}
38
39LPS22DF::LPS22DF(SPIBusInterface& bus, miosix::GpioPin cs,
40 SPIBusConfig spiConfig, Config config)
41 : slave(bus, cs, spiConfig), config(config)
42{
43}
44
46{
47 SPIBusConfig spiConfig;
49 spiConfig.mode = SPI::Mode::MODE_3;
50 spiConfig.byteOrder = SPI::Order::LSB_FIRST;
51 return spiConfig;
52}
53
55{
56 SPITransaction spi(slave);
57
58 if (isInitialized)
59 {
60 LOG_ERR(logger, "Attempted to initialized sensor twice");
62 return false;
63 }
64
65 // Disable I2C and I3C interfaces
66 spi.writeRegister(IF_CTRL, IF_CTRL::I2C_I3C_DIS);
67
68 // Setting the actual sensor configurations (Mode, ODR, AVG)
69 setConfig(config);
70
72 isInitialized = true;
73 return true;
74}
75
77{
78 // Since the sensor doesn't provide any self-test feature we just try to
79 // probe the sensor and read his whoami register.
80 if (!isInitialized)
81 {
82 LOG_ERR(logger, "Invoked selfTest() but sensor was uninitialized");
84 return false;
85 }
86
87 // Reading the whoami value to assure communication
88 SPITransaction spi(slave);
89 uint8_t whoamiValue = spi.readRegister(WHO_AM_I);
90
91 // Checking the whoami value to assure correct communication
92 if (whoamiValue != WHO_AM_I_VALUE)
93 {
94 LOG_ERR(logger, "WHO_AM_I: read 0x{:x} but expected 0x{:x}",
95 whoamiValue, WHO_AM_I_VALUE);
97 return false;
98 }
99
100 return true;
101}
102
103void LPS22DF::setConfig(const Config& config)
104{
105 setAverage(config.avg);
106 setOutputDataRate(config.odr);
107}
108
110{
111 SPITransaction spi(slave);
112
119 spi.writeRegister(CTRL_REG1, config.odr | avg);
120
121 config.avg = avg;
122}
123
125{
126 SPITransaction spi(slave);
127
134 spi.writeRegister(CTRL_REG1, odr | config.avg);
135
136 config.odr = odr;
137}
138
140{
141 SPITransaction spi(slave);
142
143 if (!isInitialized)
144 {
145 LOG_ERR(logger, "Invoked sampleImpl() but sensor was not initialized");
147 return lastSample;
148 }
149
150 LPS22DFData data;
151 uint8_t statusValue = 0;
152
153 if (config.odr == ODR::ONE_SHOT)
154 {
155 // Reading previous value of Control Register 2
156 uint8_t ctrl_reg2_val = spi.readRegister(CTRL_REG2);
157
158 // Trigger sample
159 spi.writeRegister(CTRL_REG2, ctrl_reg2_val | CTRL_REG2::ONE_SHOT_START);
160
161 // Pull status register until the sample is ready
162 do
163 {
164 statusValue = spi.readRegister(STATUS);
165 } while (!(statusValue & (STATUS::P_DA | STATUS::T_DA)));
166 }
167 else
168 {
169 // Read status register value
170 statusValue = spi.readRegister(STATUS);
171 }
172
174
175 // Sample pressure if data is available, return last sample otherwise
176 if (statusValue & STATUS::P_DA)
177 {
178 data.pressureTimestamp = ts;
179 data.pressure = spi.readRegister24(PRESS_OUT_XL) / PRES_SENS;
180 }
181 else
182 {
186 }
187
188 // Sample temperature if data is available, return last sample otherwise
189 if (statusValue & STATUS::T_DA)
190 {
191 data.temperatureTimestamp = ts;
192 data.temperature = spi.readRegister16(TEMP_OUT_L) / TEMP_SENS;
193 }
194 else
195 {
198 }
199
200 return data;
201}
202
203} // namespace Boardcore
#define LOG_ERR(logger,...)
SensorErrors lastError
Definition Sensor.h:54
void setConfig(const Config &config)
Sets and saves the configurations passed on the parameters.
Definition LPS22DF.cpp:103
void setOutputDataRate(ODR odr)
Sets and saves the output data rate.
Definition LPS22DF.cpp:124
AVG
Oversampling average values.
Definition LPS22DF.h:71
static SPIBusConfig getDefaultSPIConfig()
Definition LPS22DF.cpp:45
LPS22DFData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition LPS22DF.cpp:139
bool selfTest() override
The self test method returns true if we read the right whoami value. We can't make a better self test...
Definition LPS22DF.cpp:76
ODR
Enumeration for Output Data Rate Configuration.
Definition LPS22DF.h:48
LPS22DF(SPIBusInterface &bus, miosix::GpioPin cs)
Constructor that stores the initial default settings (without applying them to the sensor).
Definition LPS22DF.cpp:34
bool init() override
Initializes the sensor with the current settings.
Definition LPS22DF.cpp:54
void setAverage(AVG avg)
Sets and saves the oversampling on the sensor.
Definition LPS22DF.cpp:109
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
uint8_t readRegister(uint8_t reg)
Reads an 8 bit register.
void writeRegister(uint8_t reg, uint8_t data)
Writes an 8 bit register.
uint32_t readRegister24(uint8_t reg)
Reads a 24 bit register.
uint16_t readRegister16(uint8_t reg)
Reads a 16 bit register.
@ TEMP_OUT_L
Temperature output value LSB data.
Definition LPS22DFDefs.h:67
@ WHO_AM_I
Device Who am I register.
Definition LPS22DFDefs.h:45
@ CTRL_REG1
Control Register 1 [ODR, AVG].
Definition LPS22DFDefs.h:47
@ PRESS_OUT_XL
Pressure output value LSB data.
Definition LPS22DFDefs.h:63
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
@ INVALID_WHOAMI
Definition SensorData.h:40
Struct that sums up all the settings of the sensor.
Definition LPS22DF.h:85
SPI Bus configuration for a specific slave.
SPI::ClockDivider clockDivider
< Peripheral clock division