ESPHome 2026.12.0-dev
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 = 0x00;
57#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
58 if (this->temperature_publisher_ != nullptr)
59 temp_cfg = LIS3DH_TEMP_CFG_ADC_EN | LIS3DH_TEMP_CFG_TEMP_EN;
60#endif
61 return this->write_byte(LIS3DH_REG_TEMP_CFG, temp_cfg);
62}
63
65 // Runs even with no interrupt configured: the chip usually stays powered across resets,
66 // so this drops whatever the previous firmware left in the interrupt registers.
67 // High-pass filter the interrupt (IA1) input so the ~1 g of gravity is removed
68 // from the threshold comparison. The output data registers stay unfiltered
69 // (FDS = 0), so acceleration / pitch / roll still report the full gravity vector.
70 uint8_t ctrl_reg2 = this->interrupt_high_pass_ ? LIS3DH_CTRL_REG2_HP_IA1 : 0x00;
71 if (!this->write_byte(LIS3DH_REG_CTRL_REG2, ctrl_reg2))
72 return false;
73
74 if (!this->write_byte(LIS3DH_REG_INT1_CFG, this->interrupt_axes_cfg_) ||
75 !this->write_byte(LIS3DH_REG_INT1_THS, this->interrupt_threshold_) ||
76 !this->write_byte(LIS3DH_REG_INT1_DURATION, this->interrupt_duration_))
77 return false;
78
79 // Latch the request so a brief movement keeps the pad asserted until INT1_SRC is read.
80 uint8_t ctrl_reg5 = this->interrupt_latched_ ? LIS3DH_CTRL_REG5_LIR_INT1 : 0;
81 if (!this->write_byte(LIS3DH_REG_CTRL_REG5, ctrl_reg5))
82 return false;
83
84 // Route the IA1 engine to the chosen physical pad and set the pad polarity.
85 uint8_t ctrl_reg3 = 0;
86 uint8_t ctrl_reg6 = this->interrupt_active_high_ ? 0 : LIS3DH_CTRL_REG6_INT_POLARITY;
87 if (this->interrupt_axes_cfg_ != 0) {
89 ctrl_reg3 |= LIS3DH_CTRL_REG3_I1_IA1;
90 } else {
91 ctrl_reg6 |= LIS3DH_CTRL_REG6_I2_IA1;
92 }
93 }
94 if (!this->write_byte(LIS3DH_REG_CTRL_REG3, ctrl_reg3) || !this->write_byte(LIS3DH_REG_CTRL_REG6, ctrl_reg6))
95 return false;
96 if (this->interrupt_axes_cfg_ == 0)
97 return true;
98
99 // With the high-pass filter enabled, reading REFERENCE captures the current
100 // acceleration as the filter's zero point (removing the standing gravity bias).
101 if (this->interrupt_high_pass_) {
102 uint8_t reference = 0;
103 this->read_byte(LIS3DH_REG_REFERENCE, &reference);
104 }
105
106 // Clear any latched request left over from the motion that woke the device.
107 uint8_t src = 0;
108 this->read_byte(LIS3DH_REG_INT1_SRC, &src);
109 return true;
110}
111
113 ESP_LOGCONFIG(TAG, "LIS3DH:");
114 LOG_I2C_DEVICE(this);
115 if (this->is_failed())
116 return;
117
118 ESP_LOGCONFIG(TAG,
119 " Range: ±%ug\n"
120 " Operating mode: %s\n"
121 " Data rate: %uHz",
122 2u << this->range_, LOG_STR_ARG(mode_to_str(this->operating_mode_)),
123 (unsigned) progmem_read_uint16(&RATE_HZ[this->data_rate_ - 1]));
124 if (this->interrupt_axes_cfg_ != 0) {
125 ESP_LOGCONFIG(TAG,
126 " Motion interrupt: %s (%s%s%s)\n"
127 " Threshold: %u steps\n"
128 " Duration: %u samples",
129 this->interrupt_pin_ == LIS3DH_INT_PIN_INT1 ? LOG_STR_LITERAL("INT1") : LOG_STR_LITERAL("INT2"),
130 this->interrupt_active_high_ ? LOG_STR_LITERAL("active high") : LOG_STR_LITERAL("active low"),
131 this->interrupt_high_pass_ ? LOG_STR_LITERAL(", high-pass") : LOG_STR_LITERAL(""),
132 this->interrupt_latched_ ? LOG_STR_LITERAL(", latched") : LOG_STR_LITERAL(""),
134 }
135 MotionComponent::dump_config();
136}
137
139 // Clear a latched request from motion seen while awake, so it does not wake the device straight away.
140 if (this->interrupt_latched_ && this->interrupt_axes_cfg_ != 0 && !this->is_failed()) {
141 uint8_t src = 0;
142 this->read_byte(LIS3DH_REG_INT1_SRC, &src);
143 }
144}
145
147 if (this->is_failed())
148 return false;
149
150 int16_t raw[3];
151 if (!this->read_bytes_16_le(LIS3DH_REG_OUT_X_L | LIS3DH_AUTO_INCREMENT, raw, 3)) {
152 this->status_set_warning(LOG_STR("Failed to read acceleration data"));
153 return false;
154 }
155
156 float scale;
157 progmem_memcpy(&scale, &SCALE_G[this->range_], sizeof(scale));
158
159 // Data is little-endian and left-justified in a signed 16-bit register.
160 int16_t raw_x = raw[0];
161 int16_t raw_y = raw[1];
162 int16_t raw_z = raw[2];
163 ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z);
164 data.acceleration[motion::X_AXIS] = raw_x * scale;
165 data.acceleration[motion::Y_AXIS] = raw_y * scale;
166 data.acceleration[motion::Z_AXIS] = raw_z * scale;
167
168#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
169 if (this->temperature_publisher_ != nullptr && !this->publish_temperature_())
170 return true;
171#endif
172 this->status_clear_warning();
173 return true;
174}
175
176#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
178 int16_t raw;
179 if (!this->read_bytes_16_le(LIS3DH_REG_OUT_ADC3_L | LIS3DH_AUTO_INCREMENT, &raw, 1)) {
180 this->status_set_warning(LOG_STR("Failed to read temperature data"));
181 return false;
182 }
183 // Signed, left-justified, relative value with 1 °C per count of the high byte; the
184 // low byte carries the fractional bits in the modes that have them.
185 float temperature = raw / 256.0f + LIS3DH_TEMP_REFERENCE_C;
186 this->temperature_publisher_(temperature);
187 return true;
188}
189#endif
190
191} // 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:288
void status_clear_warning()
Definition component.h:318
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
bool update_data(motion::MotionData &data) override
Definition lis3dh.cpp:146
const uint8_t STORE_YAML_DATA[] PROGMEM
The zstd YAML envelope generated by store_yaml, STORE_YAML_DATA_SIZE bytes in flash.
@ 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