Skyward boardcore
Loading...
Searching...
No Matches
BMX160.h
Go to the documentation of this file.
1/* Copyright (c) 2020 Skyward Experimental Rocketry
2 * Author: Davide Mor
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
28#include <sensors/Sensor.h>
29#include <utils/Constants.h>
30
31#include <algorithm>
32#include <cstring>
33
34#include "BMX160Config.h"
35#include "BMX160Data.h"
36#include "BMX160Defs.h"
37
38namespace Boardcore
39{
40
44class BMX160 : public SensorFIFO<BMX160Data, BMX160Defs::FIFO_SIZE>
45{
46public:
56
63 BMX160(SPIBusInterface& bus, miosix::GpioPin cs, BMX160Config config = {});
64
72 BMX160(SPIBusInterface& bus, miosix::GpioPin cs, BMX160Config config,
73 SPIBusConfig busConfig);
74
78 bool init() override;
79
83 bool selfTest() override;
84
88 BMX160Data sampleImpl() override;
89
93 BMX160Temperature getTemperature();
94
98 BMX160FifoStats getFifoStats();
99
105 void IRQupdateTimestamp(uint64_t ts) override;
106
107private:
114 void sendCmd(SPITransaction& spi, BMX160Defs::Cmd cmd,
116
124 void pushSample(BMX160Data sample);
125
131 void confMag(SPITransaction& spi, uint8_t value);
132
138 void mapMag(SPITransaction& spi, uint8_t reg);
139
146 uint8_t readMag(SPITransaction& spi, uint8_t reg);
147
155 void readMag(SPITransaction& spi, uint8_t reg, uint8_t* data, size_t size);
156
163 void writeMag(SPITransaction& spi, uint8_t reg, uint8_t value);
164
170 bool checkChipid();
171
175 void softReset();
176
182 bool setPowerMode();
183
187 void initAcc();
188
192 void initGyr();
193
197 void initMag();
198
202 void initFifo();
203
207 void initInt();
208
214 bool testAcc();
215
221 bool testGyr();
222
228 bool testMag();
229
236 MagnetometerData buildMagData(BMX160Defs::MagRaw data, uint64_t timestamp);
237
244 AccelerometerData buildAccData(BMX160Defs::AccRaw data, uint64_t timestamp);
245
252 GyroscopeData buildGyrData(BMX160Defs::GyrRaw data, uint64_t timestamp);
253
259 const char* debugErr(SPITransaction& spi);
260
271 uint64_t odrToTimeOffset(BMX160Config::OutputDataRate odr, uint8_t downs);
272
276 void readTemp();
277
281 void readData();
282
288 void readFifo(bool headerless);
289
296 template <typename T>
297 T parseStruct(uint8_t* buf, int& idx);
298
309 void boschReadTrim(SPITransaction& spi);
310
314 float boschMagCompensateX(int16_t x, uint16_t rhall);
315
319 float boschMagCompensateY(int16_t y, uint16_t rhall);
320
324 float boschMagCompensateZ(int16_t z, uint16_t rhall);
325
326 float temperature = 0.0f;
327 MagnetometerData oldMag;
328 GyroscopeData oldGyr;
329 AccelerometerData oldAcc;
330
331 bool isInit = false;
332 SPISlave spiSlave;
333
334 BMX160Defs::TrimData trimData = {};
335 BMX160Config config;
336
337 BMX160FifoStats stats = {};
338
339 float gyrSensibility = 0.0f;
340 float accSensibility = 0.0f;
341
342 int tempCounter = 0;
343
344 // Sometimes the buffer gets over 1000
345 static constexpr unsigned int FIFO_BUF_SIZE = 1100;
346
352 bool irqEnabled = true;
353
354 PrintLogger logger = Logging::getLogger("bmx160");
355};
356
357} // namespace Boardcore
BMX160 Driver.
Definition BMX160.h:45
BMX160Temperature getTemperature()
Get last read temperature.
Definition BMX160.cpp:161
BMX160Data sampleImpl() override
Gather data from FIFO/data registers and temperature sensor.
Definition BMX160.cpp:112
bool init() override
Initialize the driver.
Definition BMX160.cpp:49
BMX160FifoStats getFifoStats()
Retrieve last fifo stats.
Definition BMX160.cpp:169
BMX160(SPIBusInterface &bus, miosix::GpioPin cs, BMX160Config config={})
BMX160 Constructor.
Definition BMX160.cpp:31
bool selfTest() override
Perform selftest on the device.
Definition BMX160.cpp:84
BMX160Errors
BMX160 Custom errors.
Definition BMX160.h:51
void IRQupdateTimestamp(uint64_t ts) override
Sometimes the sensor pulls down the interrupt pin while reading data. We override this method and upd...
Definition BMX160.cpp:105
static PrintLogger getLogger(const string &name)
Interface for low level access of a SPI bus as a master.
Interface for sensor that implement a FIFO.
Definition Sensor.h:149
void sample() override
Sample the sensor.
Definition Sensor.h:116
This file includes all the types the logdecoder script will decode.
@ END_OF_BASE_ERRORS
Definition SensorData.h:50
BMX160 Configuration.
OutputDataRate
Output Data Rate expressed in Hz.