ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
bmi270.h
Go to the documentation of this file.
1#pragma once
2
8#ifdef USE_BMI270_BMM150
9#include "bmm150_aux.h"
10#endif
11#include <functional>
12
13namespace esphome::bmi270 {
14
15// Register map
16static constexpr uint8_t BMI270_REG_CHIP_ID = 0x00;
17static constexpr uint8_t BMI270_REG_ERR_REG = 0x02;
18static constexpr uint8_t BMI270_REG_STATUS = 0x03;
19static constexpr uint8_t BMI270_REG_DATA_8 = 0x0C; // ACC_X LSB
20static constexpr uint8_t BMI270_REG_DATA_14 = 0x12; // GYR_X LSB
21static constexpr uint8_t BMI270_REG_TEMP_0 = 0x22;
22static constexpr uint8_t BMI270_REG_TEMP_MSB = 0x23; // temperature (2 bytes big-endian ish)
23
24static constexpr uint8_t REG_READ_LEN =
25 BMI270_REG_TEMP_MSB - BMI270_REG_DATA_8 +
26 1; // 0x23 - 0x0C + 1 = 0x18 bytes total for accel(6) + gyro(6) + temp(2) + padding(4)
27
28static constexpr uint8_t BMI270_REG_PWR_CONF = 0x7C;
29static constexpr uint8_t BMI270_REG_PWR_CTRL = 0x7D;
30static constexpr uint8_t BMI270_REG_INIT_CTRL = 0x59;
31static constexpr uint8_t BMI270_REG_INIT_DATA = 0x5E;
32static constexpr uint8_t BMI270_REG_INIT_ADDR_0 = 0x5B;
33static constexpr uint8_t BMI270_REG_INTERNAL_STATUS = 0x21;
34static constexpr uint8_t BMI270_REG_ACC_CONF = 0x40;
35static constexpr uint8_t BMI270_REG_ACC_RANGE = 0x41;
36static constexpr uint8_t BMI270_REG_GYR_CONF = 0x42;
37static constexpr uint8_t BMI270_REG_GYR_RANGE = 0x43;
38
39static constexpr uint8_t BMI270_CHIP_ID_VALUE = 0x24;
40
41// Auxiliary (secondary I2C master) interface registers.
42// Used to optionally drive a BMM150 magnetometer wired to the BMI270's AUX pins
43// instead of the main I2C bus - the BMM150 has no address of its own on that bus.
44static constexpr uint8_t BMI270_REG_AUX_X_LSB = 0x04; // start of aux data: X,Y,Z,R (8 bytes)
45static constexpr uint8_t BMI270_REG_AUX_DEV_ID = 0x4B;
46static constexpr uint8_t BMI270_REG_AUX_IF_CONF = 0x4C;
47static constexpr uint8_t BMI270_REG_AUX_RD_ADDR = 0x4D;
48static constexpr uint8_t BMI270_REG_AUX_WR_ADDR = 0x4E;
49static constexpr uint8_t BMI270_REG_AUX_WR_DATA = 0x4F;
50static constexpr uint8_t BMI270_REG_IF_CONF = 0x6B;
51static constexpr uint8_t BMI270_REG_CMD = 0x7E;
52
53// Register values and bitfields
54static constexpr uint8_t BMI270_CMD_SOFT_RESET = 0xB6;
55static constexpr uint8_t BMI270_PWR_CONF_ADV_POWER_SAVE_OFF = 0x00;
56static constexpr uint8_t BMI270_PWR_CONF_FIFO_SELF_WAKEUP = 0x02;
57static constexpr uint8_t BMI270_INIT_CTRL_LOAD_START = 0x00;
58static constexpr uint8_t BMI270_INIT_CTRL_LOAD_DONE = 0x01;
59static constexpr uint8_t BMI270_INTERNAL_STATUS_MASK = 0x0F;
60static constexpr uint8_t BMI270_INTERNAL_STATUS_INIT_OK = 0x01;
61static constexpr uint8_t BMI270_CONF_BWP_NORMAL = 0x02; // filter bandwidth, bits 5:4
62static constexpr uint8_t BMI270_CONF_BWP_SHIFT = 4;
63static constexpr uint8_t BMI270_ACC_CONF_PERF_MODE = 0x80;
64static constexpr uint8_t BMI270_GYR_CONF_NOISE_PERF = 0x40;
65static constexpr uint8_t BMI270_GYR_CONF_FILTER_PERF = 0x80;
66static constexpr uint8_t BMI270_STATUS_AUX_BUSY = 0x04;
67static constexpr uint8_t BMI270_IF_CONF_AUX_IF_EN = 0x20;
68static constexpr uint8_t BMI270_PWR_CTRL_AUX_EN = 0x01;
69static constexpr uint8_t BMI270_PWR_CTRL_ACC_EN = 0x02;
70static constexpr uint8_t BMI270_PWR_CTRL_GYR_EN = 0x04;
71static constexpr uint8_t BMI270_PWR_CTRL_TEMP_EN = 0x08;
72static constexpr uint8_t BMI270_PWR_CTRL_IMU =
73 BMI270_PWR_CTRL_ACC_EN | BMI270_PWR_CTRL_GYR_EN | BMI270_PWR_CTRL_TEMP_EN;
74static constexpr uint8_t BMI270_AUX_IF_CONF_MANUAL_EN = 0x80;
75static constexpr uint8_t BMI270_AUX_IF_CONF_FCU_WRITE_EN = 0x40;
76static constexpr uint8_t BMI270_AUX_IF_CONF_MAN_RD_BURST_8 = 0x0C; // bits 3:2 = 3
77static constexpr uint8_t BMI270_AUX_IF_CONF_RD_BURST_8 = 0x03; // bits 1:0 = 3
78
79// AUX_X_LSB..AUX_R_MSB (8 bytes) sit directly before the accel data, so one burst can read both.
80static constexpr uint8_t BMI270_AUX_DATA_LEN = 8;
81static_assert(BMI270_REG_AUX_X_LSB + BMI270_AUX_DATA_LEN == BMI270_REG_DATA_8, "aux data must precede accel data");
82
83// Accelerometer range options
90
91// Accelerometer ODR options
102
103// Gyroscope range options
111
112// Gyroscope ODR options
123
124#ifdef USE_BMI270_BMM150
125// What is wired to this instance's auxiliary (secondary I2C master) interface; another
126// instance in the same build may have nothing attached.
131#endif
132
133// Main component class
135 public:
136 // Lifecycle
137 void setup() override;
138 void dump_config() override;
139 float get_setup_priority() const override { return setup_priority::DATA; }
140
141 // Configuration setters
145 void set_gyro_odr(BMI270GyroODR o) { this->gyro_odr_ = o; }
146 template<typename F> void add_temperature_listener(F &&cb) { this->temperature_callback_.add(std::forward<F>(cb)); }
147#ifdef USE_BMI270_BMM150
148 void set_aux_device(BMI270AuxDevice device) { this->aux_device_ = device; }
150 template<typename F> void add_magnetometer_listener(F &&cb) { this->magnetometer_callback_.add(std::forward<F>(cb)); }
151#endif
152
153 protected:
154 bool update_data(motion::MotionData &data) override;
155 bool load_config_file_();
156#ifdef USE_BMI270_BMM150
157 // Brings up the BMM150 over the BMI270's aux interface. Optional: on failure this only
158 // disables magnetometer reporting, it does not fail the whole component (accel/gyro still work).
159 bool setup_magnetometer_();
160 // Blocking poll of the aux-transaction-busy bit; aux transaction time depends on the
161 // attached device, so this is bounded by retries/timeout rather than a fixed delay.
162 bool wait_for_aux_idle_(uint32_t timeout_ms);
163 // Manual-mode aux read of 8 bytes starting at `reg` on the aux device.
164 bool read_aux_block_(uint8_t reg, uint8_t *buf);
165 // Manual-mode aux write of one byte to `reg` on the aux device.
166 bool write_aux_register_(uint8_t reg, uint8_t value);
167 // Reads the BMM150 factory trim values used to compensate its samples.
169#endif
170
171 // Config
177#ifdef USE_BMI270_BMM150
181 uint8_t aux_device_address_{bmm150::BMM150_DEFAULT_I2C_ADDRESS};
183#endif
184};
185
186} // namespace esphome::bmi270
uint8_t address
Definition bl0906.h:4
void set_gyro_range(BMI270GyroRange r)
Definition bmi270.h:144
void set_accel_range(BMI270AccelRange r)
Definition bmi270.h:142
bmm150::BMM150Trim mag_trim_
Definition bmi270.h:179
LazyCallbackManager< void(const bmm150::BMM150Data &)> magnetometer_callback_
Definition bmi270.h:178
LazyCallbackManager< void(float)> temperature_callback_
Definition bmi270.h:176
bool update_data(motion::MotionData &data) override
Definition bmi270.cpp:292
void set_accel_odr(BMI270AccelODR o)
Definition bmi270.h:143
float get_setup_priority() const override
Definition bmi270.h:139
void add_magnetometer_listener(F &&cb)
Definition bmi270.h:150
void set_aux_device(BMI270AuxDevice device)
Definition bmi270.h:148
bool read_aux_block_(uint8_t reg, uint8_t *buf)
Definition bmi270.cpp:189
BMI270AuxDevice aux_device_
Definition bmi270.h:180
void add_temperature_listener(F &&cb)
Definition bmi270.h:146
BMI270AccelRange accel_range_
Definition bmi270.h:172
bool wait_for_aux_idle_(uint32_t timeout_ms)
Definition bmi270.cpp:164
bool write_aux_register_(uint8_t reg, uint8_t value)
Definition bmi270.cpp:181
void set_gyro_odr(BMI270GyroODR o)
Definition bmi270.h:145
void set_aux_device_address(uint8_t address)
Definition bmi270.h:149
BMI270GyroRange gyro_range_
Definition bmi270.h:174
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
@ BMI270_ACCEL_ODR_25
Definition bmi270.h:94
@ BMI270_ACCEL_ODR_800
Definition bmi270.h:99
@ BMI270_ACCEL_ODR_400
Definition bmi270.h:98
@ BMI270_ACCEL_ODR_200
Definition bmi270.h:97
@ BMI270_ACCEL_ODR_100
Definition bmi270.h:96
@ BMI270_ACCEL_ODR_1600
Definition bmi270.h:100
@ BMI270_ACCEL_ODR_12_5
Definition bmi270.h:93
@ BMI270_ACCEL_ODR_50
Definition bmi270.h:95
@ BMI270_GYRO_RANGE_250
Definition bmi270.h:108
@ BMI270_GYRO_RANGE_500
Definition bmi270.h:107
@ BMI270_GYRO_RANGE_125
Definition bmi270.h:109
@ BMI270_GYRO_RANGE_2000
Definition bmi270.h:105
@ BMI270_GYRO_RANGE_1000
Definition bmi270.h:106
@ BMI270_AUX_DEVICE_BMM150
Definition bmi270.h:129
@ BMI270_AUX_DEVICE_NONE
Definition bmi270.h:128
@ BMI270_ACCEL_RANGE_4G
Definition bmi270.h:86
@ BMI270_ACCEL_RANGE_16G
Definition bmi270.h:88
@ BMI270_ACCEL_RANGE_2G
Definition bmi270.h:85
@ BMI270_ACCEL_RANGE_8G
Definition bmi270.h:87
@ BMI270_GYRO_ODR_200
Definition bmi270.h:117
@ BMI270_GYRO_ODR_1600
Definition bmi270.h:120
@ BMI270_GYRO_ODR_100
Definition bmi270.h:116
@ BMI270_GYRO_ODR_800
Definition bmi270.h:119
@ BMI270_GYRO_ODR_400
Definition bmi270.h:118
@ BMI270_GYRO_ODR_3200
Definition bmi270.h:121
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Definition component.h:45
static void uint32_t