Loading...
Searching...
No Matches
lps22df.c
1/***********************************************************************************
2 * @file lps22df.c *
3 * @author Matt Ricci *
4 * @addtogroup LPS22DF *
5 * *
6 * @todo Add altitude calculation method *
7 * @{ *
8 ***********************************************************************************/
9
10#include "lps22df.h"
11
12static uint8_t LPS22DF_readRegister(LPS22DF_t *, uint8_t);
13static void LPS22DF_readRegisters(LPS22DF_t *, uint8_t, uint8_t, uint8_t *);
14static void LPS22DF_writeRegister(LPS22DF_t *, uint8_t, uint8_t);
15
16/* =============================================================================== */
28 LPS22DF_t *baro,
29 SPI_t *spi,
30 GPIOpin_t cs,
31 float tempSensitivity,
32 float pressSensitivity
33) {
34 baro->spi = spi;
35 baro->cs = cs;
36 baro->tempSensitivity = tempSensitivity;
37 baro->base.sensitivity = pressSensitivity;
38 baro->base.pressDataSize = LPS22DF_PRESS_DATA_SIZE;
39 baro->base.tempDataSize = LPS22DF_TEMP_DATA_SIZE;
40 baro->base.update = LPS22DF_update;
41 baro->base.readTemp = LPS22DF_readTemp;
44 baro->base.readPress = LPS22DF_readPress;
47 baro->base.rawPress = baro->rawPress;
48 baro->base.rawTemp = baro->rawTemp;
49
50 // Enable block data update
51 LPS22DF_writeRegister(baro, LPS22DF_CTRL_REG2, LPS22DF_CTRL_REG2_BDU);
52
53 // Enable continuous data with oversampling
54 LPS22DF_writeRegister(
55 baro,
56 LPS22DF_CTRL_REG1,
57 (LPS22DF_CTRL_REG1_ODR_200 // Continuous read 200Hz
58 | LPS22DF_CTRL_REG1_AVG_32) // 32x oversample
59 );
60
61 // Set ground pressure reading on init
62 for (uint32_t i = 0; i < 0x1FFFF; i++); // Wait for at least t_reconf
63 baro->base.readPress((Baro_t *)baro, &baro->base.groundPress); // Read current pressure
64
65 return *baro;
66}
67
68/******************************** DEVICE METHODS ********************************/
69
70/* =============================================================================== */
78 baro->readRawTemp(baro, baro->rawTemp);
79 baro->processRawTemp(baro, baro->rawTemp, &baro->temp);
80
81 baro->readRawPress(baro, baro->rawPress);
82 baro->processRawPress(baro, baro->rawPress, &baro->press);
83}
84
85/* =============================================================================== */
93void LPS22DF_readTemp(Baro_t *baro, float *out) {
94 uint8_t bytes[LPS22DF_TEMP_DATA_SIZE];
95 baro->readRawTemp(baro, bytes);
96 baro->processRawTemp(baro, bytes, out);
97}
98
99/* =============================================================================== */
108void LPS22DF_processRawTemp(Baro_t *baro, uint8_t *bytes, float *out) {
109 *out = ((LPS22DF_t *)baro)->tempSensitivity * (int16_t)(((uint16_t)bytes[1] << 8) | bytes[0]);
110}
111
112/* =============================================================================== */
120void LPS22DF_readRawTemp(Baro_t *baro, uint8_t *out) {
121 out[0] = LPS22DF_readRegister(((LPS22DF_t *)baro), LPS22DF_TEMP_OUT_H); // temp high
122 out[1] = LPS22DF_readRegister(((LPS22DF_t *)baro), LPS22DF_TEMP_OUT_L); // temp low
123}
124
125/* =============================================================================== */
133void LPS22DF_readPress(Baro_t *baro, float *out) {
134 uint8_t bytes[LPS22DF_PRESS_DATA_SIZE];
135 baro->readRawPress(baro, bytes);
136 baro->processRawPress(baro, bytes, out);
137}
138
139/* =============================================================================== */
147void LPS22DF_processRawPress(Baro_t *baro, uint8_t *bytes, float *out) {
148 *out = ((LPS22DF_t *)baro)->base.sensitivity * (int32_t)(((uint32_t)bytes[0] << 16) | ((uint32_t)bytes[1] << 8) | bytes[0]);
149}
150
151/* =============================================================================== */
159void LPS22DF_readRawPress(Baro_t *baro, uint8_t *out) {
160 uint8_t tmp[LPS22DF_PRESS_DATA_SIZE];
161 LPS22DF_readRegisters(((LPS22DF_t *)baro), LPS22DF_PRESS_OUT_XL, LPS22DF_PRESS_DATA_SIZE, tmp);
162 out[0] = tmp[2]; // temp high
163 out[1] = tmp[1]; // temp low
164 out[2] = tmp[0]; // temp mid
165}
166
167/******************************** INTERFACE METHODS ********************************/
168
169void LPS22DF_writeRegister(LPS22DF_t *baro, uint8_t address, uint8_t data) {
170 SPI_t *spi = baro->spi;
171 GPIOpin_t cs = baro->cs;
172
173 cs.reset(&cs);
174
175 // Send read command and address
176 uint8_t payload = address & 0x7F; // Load payload with address and read command
177 spi->transmit(spi, payload); // Transmit payload
178 spi->transmit(spi, data); // Transmit dummy data and read response data
179
180 cs.set(&cs);
181}
182
183uint8_t LPS22DF_readRegister(LPS22DF_t *baro, uint8_t address) {
184 uint8_t response = 0;
185 SPI_t *spi = baro->spi;
186 GPIOpin_t cs = baro->cs;
187
188 cs.reset(&cs);
189
190 // Send read command and address
191 uint8_t payload = address | 0x80; // Load payload with address and read command
192 response = spi->transmit(spi, payload); // Transmit payload
193 response = spi->transmit(spi, 0xFF); // Transmit dummy data and read response data
194
195 cs.set(&cs);
196
197 return response;
198}
199
200void LPS22DF_readRegisters(LPS22DF_t *baro, uint8_t address, uint8_t count, uint8_t *out) {
201 SPI_t *spi = baro->spi;
202 GPIOpin_t cs = baro->cs;
203
204 cs.reset(&cs);
205
206 // Send read command and address
207 uint8_t payload = address | 0x80; // Load payload with address and read command
208 spi->transmit(spi, payload); // Transmit payload
209
210 // Auto increment read through registers
211 for (uint8_t i = 0; i < count; i++) {
212 out[i] = spi->transmit(spi, 0xFF);
213 }
214
215 cs.set(&cs);
216}
void(* readPress)(struct Baro *baro, float *out)
Pointer to readPress method.
Definition barometer.h:51
uint8_t * rawTemp
Pointer to driver defined raw temperature data array.
Definition barometer.h:112
void(* readTemp)(struct Baro *baro, float *out)
Pointer to readTemp method.
Definition barometer.h:38
void(* readRawPress)(struct Baro *baro, uint8_t *out)
Pointer to readRawPress method.
Definition barometer.h:77
float temp
Last read processed temperature value.
Definition barometer.h:115
uint8_t pressDataSize
Size of raw pressure data in bytes.
Definition barometer.h:109
float press
Last read processed pressure value.
Definition barometer.h:114
float groundPress
Stored ground pressure reading.
Definition barometer.h:113
void(* processRawTemp)(struct Baro *baro, uint8_t *bytes, float *out)
Pointer to processRawTemp method.
Definition barometer.h:92
void(* processRawPress)(struct Baro *baro, uint8_t *bytes, float *out)
Pointer to processRawPress method.
Definition barometer.h:107
void(* readRawTemp)(struct Baro *baro, uint8_t *out)
Pointer to readRawTemp method.
Definition barometer.h:64
uint8_t tempDataSize
Size of raw temperature data in bytes.
Definition barometer.h:110
uint8_t * rawPress
Pointer to driver defined raw pressure data array.
Definition barometer.h:111
void(* update)(struct Baro *baro)
Pointer to update method.
Definition barometer.h:25
void(* set)(struct GPIOpin *)
Definition gpiopin.h:155
void(* reset)(struct GPIOpin *)
Definition gpiopin.h:156
Struct definition for a GPIO pin.
Definition gpiopin.h:151
void LPS22DF_readRawPress(Baro_t *, uint8_t *)
Definition lps22df.c:159
void LPS22DF_readPress(Baro_t *, float *)
Definition lps22df.c:133
void LPS22DF_processRawTemp(Baro_t *, uint8_t *, float *)
Processes raw temperature data from LPS22DF sensor.
Definition lps22df.c:108
void LPS22DF_readTemp(Baro_t *, float *)
Read the temperature from the LPS22DF sensor.
Definition lps22df.c:93
void LPS22DF_processRawPress(Baro_t *, uint8_t *, float *)
Definition lps22df.c:147
void LPS22DF_readRawTemp(Baro_t *, uint8_t *)
Definition lps22df.c:120
LPS22DF_t LPS22DF_init(LPS22DF_t *baro, SPI_t *spi, GPIOpin_t cs, float tempSensitivity, float pressSensitivity)
Initialiser for a LPS22DF barometer.
Definition lps22df.c:27
void LPS22DF_update(Baro_t *)
Updates the LPS22DF barometer readings.
Definition lps22df.c:77
uint16_t(* transmit)(struct SPI *, uint16_t)
SPI transmit method.
Definition spi.h:139
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:134