ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
lis3dh.cpp
Go to the documentation of this file.
1#include "lis3dh.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome::lis3dh {
6
7ESPHOME_LOG_TAG(TAG, "lis3dh");
8
9// g per LSB of the left-justified 16-bit sample, indexed by range. The same in every
10// operating mode, since the chip leaves the unused low bits at zero.
11static const float SCALE_G[] PROGMEM = {0.0625e-3f, 0.125e-3f, 0.25e-3f, 0.75e-3f};
12
13static const LogString *mode_to_str(LIS3DHOperatingMode mode) {
14 switch (mode) {
16 return LOG_STR("low power (8-bit)");
18 return LOG_STR("normal (10-bit)");
19 default:
20 return LOG_STR("high resolution (12-bit)");
21 }
22}
23
24// Output data rate in Hz, indexed by the ODR field minus one.
25static const uint16_t RATE_HZ[] PROGMEM = {1, 10, 25, 50, 100, 200, 400, 1620, 1344};
26
28 MotionComponent::setup();
29
30 if (!this->check_chip_id(this, LIS3DH_REG_WHO_AM_I, LIS3DH_WHO_AM_I_VALUE))
31 return;
32
33 if (!this->setup_accelerometer_() || !this->setup_temperature_() || !this->setup_interrupt_()) {
34 this->mark_failed(LOG_STR("Failed to configure sensor"));
35 return;
36 }
37}
38
40 // CTRL_REG1: output data rate, low-power flag and axis enable.
41 uint8_t ctrl_reg1 = ((uint8_t) this->data_rate_ << 4) | LIS3DH_CTRL_REG1_AXES_EN;
43 ctrl_reg1 |= LIS3DH_CTRL_REG1_LPEN;
44 if (!this->write_byte(LIS3DH_REG_CTRL_REG1, ctrl_reg1))
45 return false;
46
47 // CTRL_REG4: block data update, full-scale range and high-resolution flag.
48 uint8_t ctrl_reg4 = LIS3DH_CTRL_REG4_BDU | ((uint8_t) this->range_ << 4);
50 ctrl_reg4 |= LIS3DH_CTRL_REG4_HR;
51 return this->write_byte(LIS3DH_REG_CTRL_REG4, ctrl_reg4);
52}
53
55 // Only power the ADC / temperature sensor when a temperature sensor is used.
56 uint8_t temp_cfg =
57 this->temperature_callback_.empty() ? 0x00 : (uint8_t) (LIS3DH_TEMP_CFG_ADC_EN | LIS3DH_TEMP_CFG_TEMP_EN);
58 return this->write_byte(LIS3DH_REG_TEMP_CFG, temp_cfg);
59}
60
62 // Runs even with no interrupt configured: the chip usually stays powered across resets,
63 // so this drops whatever the previous firmware left in the interrupt registers.
64 // High-pass filter the interrupt (IA1) input so the ~1 g of gravity is removed
65 // from the threshold comparison. The output data registers stay unfiltered
66 // (FDS = 0), so acceleration / pitch / roll still report the full gravity vector.
67 uint8_t ctrl_reg2 = this->interrupt_high_pass_ ? LIS3DH_CTRL_REG2_HP_IA1 : 0x00;
68 if (!this->write_byte(LIS3DH_REG_CTRL_REG2, ctrl_reg2))
69 return false;
70
71 if (!this->write_byte(LIS3DH_REG_INT1_CFG, this->interrupt_axes_cfg_) ||
72 !this->write_byte(LIS3DH_REG_INT1_THS, this->interrupt_threshold_) ||
73 !this->write_byte(LIS3DH_REG_INT1_DURATION, this->interrupt_duration_))
74 return false;
75
76 // Latch the request so a brief movement keeps the pad asserted until INT1_SRC is read.
77 uint8_t ctrl_reg5 = this->interrupt_latched_ ? LIS3DH_CTRL_REG5_LIR_INT1 : 0;
78 if (!this->write_byte(LIS3DH_REG_CTRL_REG5, ctrl_reg5))
79 return false;
80
81 // Route the IA1 engine to the chosen physical pad and set the pad polarity.
82 uint8_t ctrl_reg3 = 0;
83 uint8_t ctrl_reg6 = this->interrupt_active_high_ ? 0 : LIS3DH_CTRL_REG6_INT_POLARITY;
84 if (this->interrupt_axes_cfg_ != 0) {
86 ctrl_reg3 |= LIS3DH_CTRL_REG3_I1_IA1;
87 } else {
88 ctrl_reg6 |= LIS3DH_CTRL_REG6_I2_IA1;
89 }
90 }
91 if (!this->write_byte(LIS3DH_REG_CTRL_REG3, ctrl_reg3) || !this->write_byte(LIS3DH_REG_CTRL_REG6, ctrl_reg6))
92 return false;
93 if (this->interrupt_axes_cfg_ == 0)
94 return true;
95
96 // With the high-pass filter enabled, reading REFERENCE captures the current
97 // acceleration as the filter's zero point (removing the standing gravity bias).
98 if (this->interrupt_high_pass_) {
99 uint8_t reference = 0;
100 this->read_byte(LIS3DH_REG_REFERENCE, &reference);
101 }
102
103 // Clear any latched request left over from the motion that woke the device.
104 uint8_t src = 0;
105 this->read_byte(LIS3DH_REG_INT1_SRC, &src);
106 return true;
107}
108
110 ESP_LOGCONFIG(TAG, "LIS3DH:");
111 LOG_I2C_DEVICE(this);
112 if (this->is_failed())
113 return;
114
115 ESP_LOGCONFIG(TAG,
116 " Range: ±%ug\n"
117 " Operating mode: %s\n"
118 " Data rate: %uHz",
119 2u << this->range_, LOG_STR_ARG(mode_to_str(this->operating_mode_)),
120 (unsigned) progmem_read_uint16(&RATE_HZ[this->data_rate_ - 1]));
121 if (this->interrupt_axes_cfg_ != 0) {
122 ESP_LOGCONFIG(TAG,
123 " Motion interrupt: %s (%s%s%s)\n"
124 " Threshold: %u steps\n"
125 " Duration: %u samples",
126 this->interrupt_pin_ == LIS3DH_INT_PIN_INT1 ? LOG_STR_LITERAL("INT1") : LOG_STR_LITERAL("INT2"),
127 this->interrupt_active_high_ ? LOG_STR_LITERAL("active high") : LOG_STR_LITERAL("active low"),
128 this->interrupt_high_pass_ ? LOG_STR_LITERAL(", high-pass") : LOG_STR_LITERAL(""),
129 this->interrupt_latched_ ? LOG_STR_LITERAL(", latched") : LOG_STR_LITERAL(""),
131 }
132 MotionComponent::dump_config();
133}
134
136 // Clear a latched request from motion seen while awake, so it does not wake the device straight away.
137 if (this->interrupt_latched_ && this->interrupt_axes_cfg_ != 0 && !this->is_failed()) {
138 uint8_t src = 0;
139 this->read_byte(LIS3DH_REG_INT1_SRC, &src);
140 }
141}
142
144 if (this->is_failed())
145 return false;
146
147 int16_t raw[3];
148 if (!this->read_bytes_16_le(LIS3DH_REG_OUT_X_L | LIS3DH_AUTO_INCREMENT, raw, 3)) {
149 this->status_set_warning(LOG_STR("Failed to read acceleration data"));
150 return false;
151 }
152
153 float scale;
154 progmem_memcpy(&scale, &SCALE_G[this->range_], sizeof(scale));
155
156 // Data is little-endian and left-justified in a signed 16-bit register.
157 int16_t raw_x = raw[0];
158 int16_t raw_y = raw[1];
159 int16_t raw_z = raw[2];
160 ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z);
161 data.acceleration[motion::X_AXIS] = raw_x * scale;
162 data.acceleration[motion::Y_AXIS] = raw_y * scale;
163 data.acceleration[motion::Z_AXIS] = raw_z * scale;
164
165 if (this->temperature_callback_.empty() || this->publish_temperature_())
166 this->status_clear_warning();
167 return true;
168}
169
171 int16_t raw;
172 if (!this->read_bytes_16_le(LIS3DH_REG_OUT_ADC3_L | LIS3DH_AUTO_INCREMENT, &raw, 1)) {
173 this->status_set_warning(LOG_STR("Failed to read temperature data"));
174 return false;
175 }
176 // Signed, left-justified, relative value with 1 °C per count of the high byte; the
177 // low byte carries the fractional bits in the modes that have them.
178 float temperature = raw / 256.0f + LIS3DH_TEMP_REFERENCE_C;
179 this->temperature_callback_.call(temperature);
180 return true;
181}
182
183} // namespace esphome::lis3dh
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
void status_clear_warning()
Definition component.h:309
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
bool read_bytes_16_le(uint8_t a_register, uint16_t *data, uint8_t len)
Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endia...
Definition i2c.h:284
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
LIS3DHInterruptPin interrupt_pin_
Definition lis3dh.h:124
LIS3DHOperatingMode operating_mode_
Definition lis3dh.h:121
LazyCallbackManager< void(float)> temperature_callback_
Definition lis3dh.h:132
bool update_data(motion::MotionData &data) override
Definition lis3dh.cpp:143
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
@ LIS3DH_INT_PIN_INT1
Definition lis3dh.h:85
ESPHOME_LOG_TAG(TAG, "lis3dh")
@ LIS3DH_MODE_LOW_POWER
Definition lis3dh.h:78
@ LIS3DH_MODE_HIGH_RESOLUTION
Definition lis3dh.h:80
@ LIS3DH_MODE_NORMAL
Definition lis3dh.h:79
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:53
const void * src
Definition hal.h:68
uint16_t temperature
Definition sun_gtil2.cpp:12