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