libyggdrasil  v1.0.0
pressure_sensor.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Yggdrasil - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 #include <cpp/common/types.hpp>
30 #include <cpp/common/utils.hpp>
31 
32 namespace bsp::ygg::prph {
33 
34 
39  public:
40  PressureSensor() = delete;
41 
47  static bool init() {
48  u8 retries = 0;
49  u8 id = 0;
50  bsp::SPIA::setMode(bsp::drv::SPIMode::_3);
51  do {
52  bsp::SPIACE = true;
53  bsp::SPIA::write<u8>(enumValue(Register::WHO_AM_I) | RequestResponse);
54  id = bsp::SPIA::read<u8>();
55  bsp::SPIACE = false;
56  if(id == DeviceID) return true;
57  retries++;
58  if(retries > 10) return false;
59  core::delay(1);
60  } while(true);
61 
62  }
63 
64 
70  static float getPressure() {
71  return getSensorData().pressure;
72  }
73 
79  static float getTemperature() {
80  return getSensorData().sensorTemperature;
81  }
82 
83  private:
84 
85  enum class Register : u8{
86  INTERRUPT_CFG = 0x0B,
87  THS_P_L = 0x0C,
88  THS_P_H = 0x0D,
89  WHO_AM_I = 0x0F,
90  CTRL_REG1 = 0x10,
91  CTRL_REG2 = 0x11,
92  CTRL_REG3 = 0x12,
93  FIFO_CTRL = 0x14,
94  REF_P_XL = 0x15,
95  REF_P_L = 0x16,
96  REF_P_H = 0x17,
97  RPDS_L = 0x18,
98  RPDS_H = 0x19,
99  RES_CONF = 0x1A,
100  INT_SOURCE = 0x25,
101  FIFO_STATUS = 0x26,
102  STATUS = 0x27,
103  PRESS_OUT_XL = 0x28,
104  PRESS_OUT_L = 0x29,
105  PRESS_OUT_H = 0x2A,
106  TEMP_OUT_L = 0x2B,
107  TEMP_OUT_H = 0x2C,
108  LPFP_RES = 0x33,
109  };
110 
114  struct ControlRegister2 {
115  u8 ONE_SHOT : 1;
116  u8 Reserved0 : 1;
117  u8 SWRESET : 1;
118  u8 I2C_DIS : 1;
119  u8 IF_ADD_INC : 1;
120  u8 STOP_ON_FTH : 1;
121  u8 FIFO_EN : 1;
122  u8 BOOT : 1;
123  };
124  static_assert (sizeof(ControlRegister2) == sizeof(u8), "Control register 2 definition wrong");
125 
129  struct SensorData {
130  float pressure;
131  float sensorTemperature;
132  };
133 
134  constexpr static inline u8 RequestResponse = 0x80;
135  constexpr static inline u8 DeviceID = 0xb1;
136  constexpr static inline u8 ConversionDone = 0x01;
137 
144  static u8 readRegister(Register reg) {
145  bsp::SPIACE = true;
146  bsp::SPIA::write<u8>(enumValue(reg) | RequestResponse);
147  auto value = bsp::SPIA::read<u8>();
148  bsp::SPIACE = false;
149 
150  return value;
151  }
152 
158  static SensorData getSensorData() {
159  SensorData senorData = {0};
160  ControlRegister2 ctrlReg2 = { .ONE_SHOT = 1, .I2C_DIS = 0, .IF_ADD_INC = 0 };
161 
162  bsp::SPIACE = true;
163  bsp::SPIA::write<std::array<u8,2>>({enumValue(Register::CTRL_REG2), bit_cast<u8>(ctrlReg2)});
164  bsp::SPIACE = false;
165 
166  do{
167  bsp::SPIACE = false;
168  core::delay(10);
169  bsp::SPIACE = true;
170  bsp::SPIA::write<u8>(enumValue(Register::CTRL_REG2) | RequestResponse);
171  } while((bsp::SPIA::read<u8>() & ConversionDone) == 0);
172 
173  {
174  auto xl = readRegister(Register::PRESS_OUT_XL);
175  auto l = readRegister(Register::PRESS_OUT_L);
176  auto h = readRegister(Register::PRESS_OUT_H);
177 
178  senorData.pressure = static_cast<float>(xl | (l << 8) | (h << 16)) / 4096.0;
179  }
180 
181  {
182  auto l = readRegister(Register::TEMP_OUT_L);
183  auto h = readRegister(Register::TEMP_OUT_H);
184 
185  senorData.sensorTemperature = static_cast<float>(l | (h << 8)) / 100;
186  }
187 
188 
189  return senorData;
190  }
191 
192  };
193 
194 }
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
types.hpp
Commonly used type definitions and helper templates.
bsp::ygg::prph::PressureSensor
Pressure sensor driver LPS22HBTR.
Definition: pressure_sensor.hpp:38
bsp::enumValue
auto enumValue(T value)
Casts a scoped enum type into its underlying value.
Definition: utils.hpp:151
bsp::ygg::prph::PressureSensor::getTemperature
static float getTemperature()
Get the current sensor temperature.
Definition: pressure_sensor.hpp:79
bsp::ygg::prph::PressureSensor::getPressure
static float getPressure()
Get the current air pressure.
Definition: pressure_sensor.hpp:70
bsp::ygg::prph::PressureSensor::init
static bool init()
Initializes the LPS22HBTR pressure sensor.
Definition: pressure_sensor.hpp:47
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::ygg::prph::PressureSensor::PressureSensor
PressureSensor()=delete
bsp::core::delay
ALWAYS_INLINE void delay(u32 ms)
Delays execution by a certain number of milliseconds.
Definition: cortex.hpp:39
bsp::ygg::prph
Definition: color_sensor.hpp:32
bsp::drv::SPIMode::_0
@ _0
CPOL = 0 | CPHA = 0.