ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
lsm6ds.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/log.h"
7
8namespace esphome::lsm6ds {
9
10// ── Register map (datasheet DocID030071 Rev 3, Table 19) ────────────────────
11static const uint8_t LSM6DS_REG_WHO_AM_I = 0x0F;
12// WHO_AM_I values; the LSM6DS3 (DocID026899) and LSM6DSO (DS12140) use the same registers as the TR-C here
13static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DS3 = 0x69;
14static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DS3TR_C = 0x6A;
15static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DSO = 0x6C; // also the LSM6DSO32, whose accel ranges differ (unsupported)
16static const uint8_t LSM6DS_REG_CTRL1_XL = 0x10; // Accel ODR + FS
17static const uint8_t LSM6DS_REG_CTRL2_G = 0x11; // Gyro ODR + FS
18static const uint8_t LSM6DS_REG_CTRL3_C = 0x12; // SW_RESET, BDU, IF_INC
19static const uint8_t LSM6DS_REG_CTRL6_C = 0x15; // Accel HP disable, Gyro LPF1
20static const uint8_t LSM6DS_REG_CTRL7_G = 0x16; // Gyro HP disable
21static const uint8_t LSM6DS_REG_STATUS = 0x1E; // XLDA, GDA, TDA
22static const uint8_t LSM6DS_REG_OUT_TEMP_L = 0x20; // Temperature LSB
23static const uint8_t LSM6DS_REG_OUTX_L_G = 0x22; // Gyro X LSB (burst start)
24static const uint8_t LSM6DS_REG_OUTX_L_XL = 0x28; // Accel X LSB
25
26// Burst read from 0x22 to 0x2D inclusive: gyro XYZ (6 bytes) + accel XYZ (6 bytes)
27static const uint8_t LSM6DS_BURST_LEN = 12;
28static const uint8_t LSM6DS_ACCEL_OFFSET = 6; // 0x28 - 0x22
29
30// ── CTRL3_C bit fields ───────────────────────────────────────────────────────
31static const uint8_t CTRL3_C_SW_RESET = (1 << 0);
32static const uint8_t CTRL3_C_IF_INC = (1 << 2); // auto-increment address on burst (default 1)
33static const uint8_t CTRL3_C_BDU = (1 << 6); // block data update
34
35// ── Accelerometer full-scale range ──────────────────────────────────────────
36// CTRL1_XL bits [3:2] — FS_XL[1:0]
37// Note: 0x01 = ±16g is intentional per Table 52 — the mapping is non-monotonic
38enum LSM6DSAccelRange : uint8_t {
39 LSM6DS_ACCEL_RANGE_2G = 0x00, // ±2 g, 0.061 mg/LSB
40 LSM6DS_ACCEL_RANGE_16G = 0x01, // ±16 g, 0.488 mg/LSB
41 LSM6DS_ACCEL_RANGE_4G = 0x02, // ±4 g, 0.122 mg/LSB
42 LSM6DS_ACCEL_RANGE_8G = 0x03, // ±8 g, 0.244 mg/LSB
43};
44
45// ── Accelerometer output data rate ──────────────────────────────────────────
46// CTRL1_XL bits [7:4] — ODR_XL[3:0]
47enum LSM6DSAccelODR : uint8_t {
49 LSM6DS_ACCEL_ODR_12_5 = 0x01, // 12.5 Hz
50 LSM6DS_ACCEL_ODR_26 = 0x02, // 26 Hz
51 LSM6DS_ACCEL_ODR_52 = 0x03, // 52 Hz
52 LSM6DS_ACCEL_ODR_104 = 0x04, // 104 Hz
53 LSM6DS_ACCEL_ODR_208 = 0x05, // 208 Hz
54 LSM6DS_ACCEL_ODR_416 = 0x06, // 416 Hz
55 LSM6DS_ACCEL_ODR_833 = 0x07, // 833 Hz
56 LSM6DS_ACCEL_ODR_1666 = 0x08, // 1666 Hz
57 LSM6DS_ACCEL_ODR_3332 = 0x09, // 3332 Hz
58 LSM6DS_ACCEL_ODR_6664 = 0x0A, // 6664 Hz
59};
60
61// ── Gyroscope full-scale range ───────────────────────────────────────────────
62// CTRL2_G bits [3:0] — FS_G[2:1] and FS_125 (bit 1)
63// The FS_125 bit (bit 1) enables the ±125 dps range independently of FS_G.
64// For all other ranges, bits [3:2] select the range and bit 1 = 0.
65enum LSM6DSGyroRange : uint8_t {
66 LSM6DS_GYRO_RANGE_125 = 0x02, // ±125 dps, 4.375 mdps/LSB (FS_125=1)
67 LSM6DS_GYRO_RANGE_250 = 0x00, // ±250 dps, 8.75 mdps/LSB
68 LSM6DS_GYRO_RANGE_500 = 0x04, // ±500 dps, 17.50 mdps/LSB
69 LSM6DS_GYRO_RANGE_1000 = 0x08, // ±1000 dps, 35 mdps/LSB
70 LSM6DS_GYRO_RANGE_2000 = 0x0C, // ±2000 dps, 70 mdps/LSB
71};
72
73// ── Gyroscope output data rate ───────────────────────────────────────────────
74// CTRL2_G bits [7:4] — ODR_G[3:0]
75enum LSM6DSGyroODR : uint8_t {
77 LSM6DS_GYRO_ODR_12_5 = 0x01, // 12.5 Hz
78 LSM6DS_GYRO_ODR_26 = 0x02, // 26 Hz
79 LSM6DS_GYRO_ODR_52 = 0x03, // 52 Hz
80 LSM6DS_GYRO_ODR_104 = 0x04, // 104 Hz
81 LSM6DS_GYRO_ODR_208 = 0x05, // 208 Hz
82 LSM6DS_GYRO_ODR_416 = 0x06, // 416 Hz
83 LSM6DS_GYRO_ODR_833 = 0x07, // 833 Hz
84 LSM6DS_GYRO_ODR_1666 = 0x08, // 1666 Hz
85 LSM6DS_GYRO_ODR_3332 = 0x09, // 3332 Hz
86 LSM6DS_GYRO_ODR_6664 = 0x0A, // 6664 Hz
87};
88
89// ── Main component class ─────────────────────────────────────────────────────
91const LogString *chip_name_from_who_am_i(uint8_t who_am_i);
93uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i);
94
96 public:
97 void setup() override;
98 void dump_config() override;
99 float get_setup_priority() const override { return setup_priority::DATA; }
100
101 // Configuration setters (called from Python codegen)
105 void set_gyro_odr(LSM6DSGyroODR o) { this->gyro_odr_ = o; }
106
107 template<typename F> void add_temperature_listener(F &&cb) { this->temperature_callback_.add(std::forward<F>(cb)); }
108
109 protected:
110 const LogString *chip_name_{nullptr};
111 bool update_data(motion::MotionData &data) override;
112
117 uint16_t temperature_sensitivity_{256}; // LSB/°C: 16 on the LSM6DS3, 256 on the others
118
120};
121
122} // namespace esphome::lsm6ds
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
void set_accel_odr(LSM6DSAccelODR o)
Definition lsm6ds.h:103
void add_temperature_listener(F &&cb)
Definition lsm6ds.h:107
LazyCallbackManager< void(float)> temperature_callback_
Definition lsm6ds.h:119
void set_gyro_odr(LSM6DSGyroODR o)
Definition lsm6ds.h:105
float get_setup_priority() const override
Definition lsm6ds.h:99
bool update_data(motion::MotionData &data) override
Definition lsm6ds.cpp:137
LSM6DSGyroRange gyro_range_
Definition lsm6ds.h:115
LSM6DSAccelRange accel_range_
Definition lsm6ds.h:113
void set_accel_range(LSM6DSAccelRange r)
Definition lsm6ds.h:102
const LogString * chip_name_
Definition lsm6ds.h:110
void set_gyro_range(LSM6DSGyroRange r)
Definition lsm6ds.h:104
@ LSM6DS_ACCEL_RANGE_16G
Definition lsm6ds.h:40
@ LSM6DS_ACCEL_RANGE_4G
Definition lsm6ds.h:41
@ LSM6DS_ACCEL_RANGE_2G
Definition lsm6ds.h:39
@ LSM6DS_ACCEL_RANGE_8G
Definition lsm6ds.h:42
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:43
@ LSM6DS_GYRO_ODR_104
Definition lsm6ds.h:80
@ LSM6DS_GYRO_ODR_833
Definition lsm6ds.h:83
@ LSM6DS_GYRO_ODR_3332
Definition lsm6ds.h:85
@ LSM6DS_GYRO_ODR_26
Definition lsm6ds.h:78
@ LSM6DS_GYRO_ODR_1666
Definition lsm6ds.h:84
@ LSM6DS_GYRO_ODR_416
Definition lsm6ds.h:82
@ LSM6DS_GYRO_ODR_52
Definition lsm6ds.h:79
@ LSM6DS_GYRO_ODR_6664
Definition lsm6ds.h:86
@ LSM6DS_GYRO_ODR_12_5
Definition lsm6ds.h:77
@ LSM6DS_GYRO_ODR_OFF
Definition lsm6ds.h:76
@ LSM6DS_GYRO_ODR_208
Definition lsm6ds.h:81
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:30
@ LSM6DS_ACCEL_ODR_833
Definition lsm6ds.h:55
@ LSM6DS_ACCEL_ODR_3332
Definition lsm6ds.h:57
@ LSM6DS_ACCEL_ODR_6664
Definition lsm6ds.h:58
@ LSM6DS_ACCEL_ODR_416
Definition lsm6ds.h:54
@ LSM6DS_ACCEL_ODR_52
Definition lsm6ds.h:51
@ LSM6DS_ACCEL_ODR_OFF
Definition lsm6ds.h:48
@ LSM6DS_ACCEL_ODR_104
Definition lsm6ds.h:52
@ LSM6DS_ACCEL_ODR_12_5
Definition lsm6ds.h:49
@ LSM6DS_ACCEL_ODR_26
Definition lsm6ds.h:50
@ LSM6DS_ACCEL_ODR_1666
Definition lsm6ds.h:56
@ LSM6DS_ACCEL_ODR_208
Definition lsm6ds.h:53
@ 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
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Definition component.h:45