Skyward boardcore
Loading...
Searching...
No Matches
LIS331HH.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
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 "LIS331HH.h"
24
26
27namespace Boardcore
28{
29
30LIS331HH::LIS331HH(SPIBusInterface& bus, miosix::GpioPin cs,
31 SPIBusConfig spiConfig)
32 : slave(bus, cs, spiConfig)
33{
34}
35
37{
38 SPITransaction spi(slave);
39
40 spi.writeRegister(CTRL_REG1, NORMAL_MODE | CTRL_REG1_Z_EN | CTRL_REG1_Y_EN |
41 CTRL_REG1_X_EN);
42
43 return true;
44}
45
46bool LIS331HH::selfTest() { return true; }
47
49{
50 SPITransaction spi(slave);
51
52 uint8_t ctrl1 = spi.readRegister(CTRL_REG1);
53 ctrl1 = (ctrl1 & ~CTRL_REG1_DR) | odr;
54 spi.writeRegister(CTRL_REG1, ctrl1);
55}
56
58{
59 SPITransaction spi(slave);
60
61 uint8_t ctrl4 = spi.readRegister(CTRL_REG4);
62 ctrl4 = (ctrl4 & ~CTRL_REG4_FS) | fs;
63 spi.writeRegister(CTRL_REG4, ctrl4);
64
65 if (fs == FS_6)
66 sensitivity = 6.0 / 32767.0;
67 else if (fs == FS_12)
68 sensitivity = 12.0 / 32767.0;
69 else
70 sensitivity = 24.0 / 32767.0;
71}
72
74{
75 int16_t val;
76 LIS331HHData data;
77
79
80 SPITransaction spi(slave);
81
82 spi.readRegisters(0x40 | OUT_X_L, reinterpret_cast<uint8_t*>(&val), 2);
83 data.accelerationX = sensitivity * val;
84
85 spi.readRegisters(0x40 | OUT_Y_L, reinterpret_cast<uint8_t*>(&val), 2);
86 data.accelerationY = sensitivity * val;
87
88 spi.readRegisters(0x40 | OUT_Z_L, reinterpret_cast<uint8_t*>(&val), 2);
89 data.accelerationZ = sensitivity * val;
90
91 return data;
92}
93
94} // namespace Boardcore
LIS331HHData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition LIS331HH.cpp:73
bool init() override
Initialize the sensor.
Definition LIS331HH.cpp:36
LIS331HH(SPIBusInterface &bus, miosix::GpioPin cs, SPIBusConfig spiConfig)
Definition LIS331HH.cpp:30
void setFullScaleRange(FullScaleRange fs)
Definition LIS331HH.cpp:57
void setOutputDataRate(OutputDataRate odr)
Definition LIS331HH.cpp:48
bool selfTest() override
Check if the sensor is working.
Definition LIS331HH.cpp:46
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 readRegisters(uint8_t reg, uint8_t *data, size_t size)
Reads multiple bytes starting from the specified register.
void writeRegister(uint8_t reg, uint8_t data)
Writes an 8 bit register.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
SPI Bus configuration for a specific slave.