ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
lsm6ds.cpp
Go to the documentation of this file.
1#include "lsm6ds.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
6
7namespace esphome::lsm6ds {
8
9ESPHOME_LOG_TAG(TAG, "lsm6ds");
10
11// Indexed by the accel range field, which is not in ascending order (datasheet Table 3)
12PROGMEM_STRING_TABLE(AccelRangeStrings, "±2g", "±16g", "±4g", "±8g");
13
14static const LogString *gyro_range_to_str(LSM6DSGyroRange range) {
15 switch (range) {
17 return LOG_STR("±125dps");
19 return LOG_STR("±250dps");
21 return LOG_STR("±500dps");
23 return LOG_STR("±1000dps");
25 return LOG_STR("±2000dps");
26 default:
27 return LOG_STR("unknown");
28 }
29}
30
31const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
32 switch (who_am_i) {
33 case LSM6DS_WHO_AM_I_LSM6DS3:
34 return LOG_STR("LSM6DS3");
35 case LSM6DS_WHO_AM_I_LSM6DS3TR_C:
36 return LOG_STR("LSM6DS3TR-C");
37 case LSM6DS_WHO_AM_I_LSM6DSO:
38 return LOG_STR("LSM6DSO");
39 default:
40 return nullptr;
41 }
42}
43
44uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i) {
45 return who_am_i == LSM6DS_WHO_AM_I_LSM6DS3 ? 16 : 256;
46}
47
49 MotionComponent::setup();
50 uint8_t who_am_i = 0;
51 if (!this->setup_check(this->read_register(LSM6DS_REG_WHO_AM_I, &who_am_i, 1) == i2c::ERROR_OK,
52 LOG_STR("Failed to read WHO_AM_I — check wiring and I2C address")))
53 return;
54 const LogString *chip_name = chip_name_from_who_am_i(who_am_i);
55 if (chip_name == nullptr) {
56 ESP_LOGE(TAG, "Unknown WHO_AM_I: 0x%02X", who_am_i);
57 this->mark_failed(LOG_STR("Unknown WHO_AM_I value"));
58 return;
59 }
60 ESP_LOGD(TAG, "Found %s (WHO_AM_I = 0x%02X)", LOG_STR_ARG(chip_name), who_am_i);
61 this->chip_name_ = chip_name;
63
64 // 2. Software reset — clears all registers to defaults
65 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL3_C, &CTRL3_C_SW_RESET, 1) == i2c::ERROR_OK,
66 LOG_STR("Software reset failed")))
67 return;
68 // Datasheet: reset bit self-clears after boot (typ. 50 µs);
69 delay(2);
70
71 // 3. Enable auto-increment and block data update (BDU).
72 // BDU prevents reading a high-byte from one sample and a low-byte from the next.
73 // IF_INC is set by default after reset but we set it explicitly for clarity.
74 uint8_t ctrl3 = CTRL3_C_IF_INC | CTRL3_C_BDU;
75 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL3_C, &ctrl3, 1) == i2c::ERROR_OK,
76 LOG_STR("Config failed")))
77 return;
78
79 // 4. Configure accelerometer: ODR in bits[7:4], FS in bits[3:2]
80 // Anti-aliasing filter bandwidth is left at power-on default (bits[1:0] = 00 = ODR/2).
81 uint8_t ctrl1_xl = (uint8_t) (this->accel_odr_ << 4) | (uint8_t) (this->accel_range_ << 2);
82 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL1_XL, &ctrl1_xl, 1) == i2c::ERROR_OK,
83 LOG_STR("Failed to configure accelerometer")))
84 return;
85
86 // 5. Configure gyroscope: ODR in bits[7:4], FS_G + FS_125 in bits[3:0]
87 // For ±125 dps: FS_G[2:1]=00 and FS_125(bit1)=1, so gyro_range_ encodes the full nibble.
88 uint8_t ctrl2_g = (uint8_t) (this->gyro_odr_ << 4) | (uint8_t) (this->gyro_range_);
89 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL2_G, &ctrl2_g, 1) == i2c::ERROR_OK,
90 LOG_STR("Failed to configure gyroscope")))
91 return;
92
93 // 6. Ensure accelerometer is in high-performance mode (CTRL6_C bit 4 = XL_HM_MODE = 0)
94 // and gyroscope is in high-performance mode (CTRL7_G bit 7 = G_HM_MODE = 0).
95 // Both default to 0 (high-performance) after reset, but write explicitly.
96 uint8_t zero = 0x00;
97 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL6_C, &zero, 1) == i2c::ERROR_OK))
98 return;
99 if (!this->setup_check(this->write_register(LSM6DS_REG_CTRL7_G, &zero, 1) == i2c::ERROR_OK))
100 return;
101}
102
104 ESP_LOGCONFIG(TAG,
105 "LSM6DS IMU:\n"
106 " Chip type: %s\n",
107 LOG_STR_ARG(this->chip_name_ != nullptr ? this->chip_name_ : LOG_STR("Unknown")));
108 LOG_I2C_DEVICE(this);
109 LOG_UPDATE_INTERVAL(this);
110
111 auto accel_odr = this->accel_odr_ == 0 ? 0 : 13 * (1 << (this->accel_odr_ - 1));
112 auto gyro_odr = this->gyro_odr_ == 0 ? 0 : 13 * (1 << (this->gyro_odr_ - 1));
113 ESP_LOGCONFIG(TAG,
114 " Accel range : %s\n"
115 " Accel data rate : %dHz\n"
116 " Gyro range : %s\n"
117 " Gyro data rate : %dHz",
118 LOG_STR_ARG(AccelRangeStrings::get_log_str(this->accel_range_)), accel_odr,
119 LOG_STR_ARG(gyro_range_to_str(this->gyro_range_)), gyro_odr);
120}
121
122// update_data()
123// Called by MotionComponent::update() on each polling interval.
124// Reads gyro XYZ and accel XYZ in a single 12-byte burst (registers 0x22–0x2D).
125// Values are in g (accel) and °/s (gyro) — MotionComponent handles axis mapping
126// and sensor publishing.
127
129 if (this->is_failed())
130 return false;
131
132 // Single burst: gyro X/Y/Z (0x22–0x27) then accel X/Y/Z (0x28–0x2D)
133 uint8_t raw[LSM6DS_BURST_LEN];
134 if (!this->read_bytes(LSM6DS_REG_OUTX_L_G, raw, LSM6DS_BURST_LEN)) {
135 this->status_set_error(LOG_STR("Failed to read IMU data"));
136 return false;
137 }
138 this->status_clear_error();
139
140 // Gyroscope
141 // Sensitivity (mdps/LSB) from datasheet Table 3.
142 // Multiply by 1e-3 to convert mdps → dps (°/s).
143 static constexpr float GYRO_SCALE[] = {
144 8.75e-3f, // 0x00 — ±250 dps
145 8.75e-3f, // 0x01 — unused (maps to 250 as fallback)
146 4.375e-3f, // 0x02 — ±125 dps (FS_125 bit set)
147 8.75e-3f, // 0x03 — unused
148 17.50e-3f, // 0x04 — ±500 dps
149 17.50e-3f, // 0x05 — unused
150 8.75e-3f, // 0x06 — unused
151 8.75e-3f, // 0x07 — unused
152 35.0e-3f, // 0x08 — ±1000 dps
153 35.0e-3f, // 0x09 — unused
154 17.50e-3f, // 0x0A — unused
155 17.50e-3f, // 0x0B — unused
156 70.0e-3f, // 0x0C — ±2000 dps
157 };
158 float gyro_scale = GYRO_SCALE[this->gyro_range_];
159
160 data.angular_rate[motion::X_AXIS] = encode_value_le<int16_t>(raw) * gyro_scale;
161 data.angular_rate[motion::Y_AXIS] = encode_value_le<int16_t>(&raw[2]) * gyro_scale;
162 data.angular_rate[motion::Z_AXIS] = encode_value_le<int16_t>(&raw[4]) * gyro_scale;
163
164 // Accelerometer
165 // Sensitivity (mg/LSB) from datasheet Table 3.
166 // Multiply by 1e-3 to convert mg → g.
167 // Note: FS_XL register values are non-monotonic (0=2g, 1=16g, 2=4g, 3=8g).
168 static constexpr float ACCEL_SCALE[] = {
169 0.061e-3f, // 0x00 — ±2g
170 0.488e-3f, // 0x01 — ±16g
171 0.122e-3f, // 0x02 — ±4g
172 0.244e-3f, // 0x03 — ±8g
173 };
174 float accel_scale = ACCEL_SCALE[this->accel_range_];
175
176 data.acceleration[motion::X_AXIS] = encode_value_le<int16_t>(&raw[LSM6DS_ACCEL_OFFSET]) * accel_scale;
177 data.acceleration[motion::Y_AXIS] = encode_value_le<int16_t>(&raw[LSM6DS_ACCEL_OFFSET + 2]) * accel_scale;
178 data.acceleration[motion::Z_AXIS] = encode_value_le<int16_t>(&raw[LSM6DS_ACCEL_OFFSET + 4]) * accel_scale;
179
180#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
181 // Temperature (lazy — only read if a sensor is configured)
182 // Kept as a separate 2-byte read to avoid extending the burst to 14 bytes when
183 // temperature is not needed.
184 // Formula: T(°C) = raw / sensitivity + 25.0, sensitivity in LSB/°C per chip (0 LSB at 25 °C)
185 if (this->temperature_publisher_ != nullptr) {
186 uint8_t raw_t[2];
187 if (this->read_bytes(LSM6DS_REG_OUT_TEMP_L, raw_t, 2)) {
188 int16_t temp_raw = encode_value_le<int16_t>(raw_t);
189 float temperature = temp_raw / static_cast<float>(this->temperature_sensitivity_) + 25.0f;
190 this->temperature_publisher_(temperature);
191 }
192 }
193#endif
194
195 return true;
196}
197
198} // namespace esphome::lsm6ds
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_error()
Definition component.h:324
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:79
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:58
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
bool update_data(motion::MotionData &data) override
Definition lsm6ds.cpp:128
LSM6DSGyroRange gyro_range_
Definition lsm6ds.h:112
LSM6DSAccelRange accel_range_
Definition lsm6ds.h:110
const LogString * chip_name_
Definition lsm6ds.h:107
Range range
Definition msa3xx.h:0
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i)
Temperature sensitivity in LSB/°C for a supported WHO_AM_I value, from the chip's datasheet.
Definition lsm6ds.cpp:44
ESPHOME_LOG_TAG(TAG, "lsm6ds")
const LogString * chip_name_from_who_am_i(uint8_t who_am_i)
Chip name for a WHO_AM_I value, or nullptr when the chip is not supported.
Definition lsm6ds.cpp:31
@ LSM6DS_GYRO_RANGE_250
Definition lsm6ds.h:67
@ LSM6DS_GYRO_RANGE_125
Definition lsm6ds.h:66
@ LSM6DS_GYRO_RANGE_2000
Definition lsm6ds.h:70
@ LSM6DS_GYRO_RANGE_1000
Definition lsm6ds.h:69
@ LSM6DS_GYRO_RANGE_500
Definition lsm6ds.h:68
PROGMEM_STRING_TABLE(AccelRangeStrings, "±2g", "±16g", "±4g", "±8g")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint16_t temperature
Definition sun_gtil2.cpp:12