rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Private Attributes
Lps25 Class Reference

#include <lps25.h>

Collaboration diagram for Lps25:
Collaboration graph
[legend]

Public Member Functions

bool init (brain_pin_e scl, brain_pin_e sda)
 
expected< floatreadPressureKpa ()
 
bool hasInit () const
 

Private Types

enum class  Type { Lps22 , Lps25 }
 

Private Member Functions

uint8_t regCr1 () const
 

Private Attributes

BitbangI2c m_i2c
 
Type m_type
 
bool m_hasInit = false
 

Detailed Description

Definition at line 13 of file lps25.h.

Member Enumeration Documentation

◆ Type

enum class Lps25::Type
strongprivate
Enumerator
Lps22 
Lps25 

Definition at line 26 of file lps25.h.

26 {
27 Lps22,
28 Lps25,
29 };

Member Function Documentation

◆ hasInit()

bool Lps25::hasInit ( ) const
inline

Definition at line 19 of file lps25.h.

19 {
20 return m_hasInit;
21 }
bool m_hasInit
Definition lps25.h:33

Referenced by baroLps25Update().

Here is the caller graph for this function:

◆ init()

bool Lps25::init ( brain_pin_e  scl,
brain_pin_e  sda 
)

Definition at line 37 of file lps25.cpp.

37 {
38 if (!m_i2c.init(scl, sda)) {
39 return false;
40 }
41
42 // Read ident register
43 auto whoAmI = m_i2c.readRegister(addr, REG_WhoAmI);
44
45 switch (whoAmI)
46 {
49 break;
52 break;
53 default:
54 // chip not detected
55 return false;
56 }
57
58 uint8_t cr1 =
59 LPS_CR1_ODR_25hz | // 25hz update rate
60 // TODO: should bdu be set?
61 LPS_CR1_BDU; // Output registers update only when read
62
63 if (m_type == Type::Lps25) {
64 // Set to active mode
65 // this bit must be 0 on LPS22
66 cr1 |= LPS_CR1_PD;
67 }
68
69 // Set the control registers
71
72 m_hasInit = true;
73 return true;
74}
constexpr uint8_t addr
Definition ads1015.cpp:14
void writeRegister(uint8_t addr, uint8_t reg, uint8_t val)
Definition i2c_bb.cpp:235
bool init(brain_pin_e scl, brain_pin_e sda)
Definition i2c_bb.cpp:37
uint8_t readRegister(uint8_t addr, uint8_t reg)
Definition i2c_bb.cpp:227
uint8_t regCr1() const
Definition lps25.cpp:116
BitbangI2c m_i2c
Definition lps25.h:24
Type m_type
Definition lps25.h:31
static constexpr uint8_t expectedWhoAmILps25
Definition lps25.cpp:17
static constexpr uint8_t expectedWhoAmILps22
Definition lps25.cpp:16

Referenced by initBaro().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readPressureKpa()

expected< float > Lps25::readPressureKpa ( )

Definition at line 76 of file lps25.cpp.

76 {
77 if (!m_hasInit) {
78 return unexpected;
79 }
80
81 uint8_t buffer[4];
82 // Sequential multi-byte reads need to set the high bit of the
83 // register address to enable multi-byte read
84 constexpr uint8_t readAddr = REG_Status | 0x80;
85 m_i2c.writeRead(addr, &readAddr, 1, buffer, 4);
86
87 // First check the status reg to check if there are data available
88 bool hasPressure = buffer[0] & LPS_SR_P_DA;
89
90 if (!hasPressure) {
91 return unexpected;
92 }
93
94 // Glue the 3 bytes back in to a 24 bit integer
95 uint32_t counts = buffer[3] << 16 | buffer[2] << 8 | buffer[1];
96
97 // 4096 counts per hectopascal
98 // = 40960 counts per kilopascal
99 constexpr float ratio = 1 / 40960.0f;
100
101 float kilopascal = counts * ratio;
102
103 // Sensor limits are 26-126 kPa
104 // The highest ever barometric pressure measured was only 108.3 kPa
105 // The pressure at the highest altitude road (Khardung La, India/Tibet) is at 5600 meters,
106 // which should have a nominal barometric pressure of around 50 kPa
107 // Anything outside that range is not a place we expect your engine to run, so we assume
108 // some sensing problem (sealed ECU case and high temperature?)
109 if (kilopascal > 120 || kilopascal < 50) {
110 return unexpected;
111 }
112
113 return kilopascal;
114}
void writeRead(uint8_t addr, const uint8_t *writeData, size_t writeSize, uint8_t *readData, size_t readSize)
Definition i2c_bb.cpp:204
static BigBufferHandle buffer

Referenced by Lps25Sensor::update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ regCr1()

uint8_t Lps25::regCr1 ( ) const
private

Definition at line 116 of file lps25.cpp.

116 {
117 switch (m_type)
118 {
119 case Type::Lps22:
120 return REG_Cr1_Lps22;
121 case Type::Lps25:
122 default:
123 return REG_Cr1_Lps25;
124 }
125}

Referenced by init().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ m_hasInit

bool Lps25::m_hasInit = false
private

Definition at line 33 of file lps25.h.

Referenced by hasInit(), init(), and readPressureKpa().

◆ m_i2c

BitbangI2c Lps25::m_i2c
private

Definition at line 24 of file lps25.h.

Referenced by init(), and readPressureKpa().

◆ m_type

Type Lps25::m_type
private

Definition at line 31 of file lps25.h.

Referenced by init(), and regCr1().


The documentation for this class was generated from the following files: