ESPHome 2026.12.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
12namespace esphome::bmi270 {
13
14static constexpr uint8_t BMI270_REG_CHIP_ID = 0x00;
15static constexpr uint8_t BMI270_REG_STATUS = 0x03;
16static constexpr uint8_t BMI270_REG_DATA_8 = 0x0C; // ACC_X LSB
17static constexpr uint8_t BMI270_REG_DATA_14 = 0x12; // GYR_X LSB
18static constexpr uint8_t BMI270_REG_TEMP_0 = 0x22;
19static constexpr uint8_t BMI270_REG_TEMP_MSB = 0x23;
20
21// Accel, gyro, 10 unused bytes, then temperature: 0x0C to 0x23
22static constexpr uint8_t REG_READ_LEN = BMI270_REG_TEMP_MSB - BMI270_REG_DATA_8 + 1;
23
24static constexpr uint8_t BMI270_REG_PWR_CONF = 0x7C;
25static constexpr uint8_t BMI270_REG_PWR_CTRL = 0x7D;
26static constexpr uint8_t BMI270_REG_INIT_CTRL = 0x59;
27static constexpr uint8_t BMI270_REG_INIT_DATA = 0x5E;
28static constexpr uint8_t BMI270_REG_INIT_ADDR_0 = 0x5B;
29static constexpr uint8_t BMI270_REG_INTERNAL_STATUS = 0x21;
30static constexpr uint8_t BMI270_REG_ACC_CONF = 0x40;
31static constexpr uint8_t BMI270_REG_ACC_RANGE = 0x41;
32static constexpr uint8_t BMI270_REG_GYR_CONF = 0x42;
33static constexpr uint8_t BMI270_REG_GYR_RANGE = 0x43;
34
35static constexpr uint8_t BMI270_CHIP_ID_VALUE = 0x24;
36
37// Auxiliary (secondary I2C master) interface registers.
38// Used to optionally drive a BMM150 magnetometer wired to the BMI270's AUX pins
39// instead of the main I2C bus - the BMM150 has no address of its own on that bus.
40static constexpr uint8_t BMI270_REG_AUX_X_LSB = 0x04; // start of aux data: X,Y,Z,R (8 bytes)
41static constexpr uint8_t BMI270_REG_AUX_DEV_ID = 0x4B;
42static constexpr uint8_t BMI270_REG_AUX_IF_CONF = 0x4C;
43static constexpr uint8_t BMI270_REG_AUX_RD_ADDR = 0x4D;
44static constexpr uint8_t BMI270_REG_AUX_WR_ADDR = 0x4E;
45static constexpr uint8_t BMI270_REG_AUX_WR_DATA = 0x4F;
46static constexpr uint8_t BMI270_REG_IF_CONF = 0x6B;
47static constexpr uint8_t BMI270_REG_CMD = 0x7E;
48
49// Register values and bitfields
50static constexpr uint8_t BMI270_CMD_SOFT_RESET = 0xB6;
51static constexpr uint8_t BMI270_PWR_CONF_ADV_POWER_SAVE_OFF = 0x00;
52static constexpr uint8_t BMI270_PWR_CONF_FIFO_SELF_WAKEUP = 0x02;
53static constexpr uint8_t BMI270_INIT_CTRL_LOAD_START = 0x00;
54static constexpr uint8_t BMI270_INIT_CTRL_LOAD_DONE = 0x01;
55static constexpr uint8_t BMI270_INTERNAL_STATUS_MASK = 0x0F;
56static constexpr uint8_t BMI270_INTERNAL_STATUS_INIT_OK = 0x01;
57static constexpr uint8_t BMI270_CONF_BWP_NORMAL = 0x02; // filter bandwidth, bits 5:4
58static constexpr uint8_t BMI270_CONF_BWP_SHIFT = 4;
59static constexpr uint8_t BMI270_ACC_CONF_PERF_MODE = 0x80;
60static constexpr uint8_t BMI270_GYR_CONF_NOISE_PERF = 0x40;
61static constexpr uint8_t BMI270_GYR_CONF_FILTER_PERF = 0x80;
62static constexpr uint8_t BMI270_STATUS_AUX_BUSY = 0x04;
63static constexpr uint8_t BMI270_IF_CONF_AUX_IF_EN = 0x20;
64static constexpr uint8_t BMI270_PWR_CTRL_AUX_EN = 0x01;
65static constexpr uint8_t BMI270_PWR_CTRL_ACC_EN = 0x02;
66static constexpr uint8_t BMI270_PWR_CTRL_GYR_EN = 0x04;
67static constexpr uint8_t BMI270_PWR_CTRL_TEMP_EN = 0x08;
68static constexpr uint8_t BMI270_PWR_CTRL_IMU =
69 BMI270_PWR_CTRL_ACC_EN | BMI270_PWR_CTRL_GYR_EN | BMI270_PWR_CTRL_TEMP_EN;
70static constexpr uint8_t BMI270_AUX_IF_CONF_MANUAL_EN = 0x80;
71static constexpr uint8_t BMI270_AUX_IF_CONF_FCU_WRITE_EN = 0x40;
72static constexpr uint8_t BMI270_AUX_IF_CONF_MAN_RD_BURST_8 = 0x0C; // bits 3:2 = 3
73static constexpr uint8_t BMI270_AUX_IF_CONF_RD_BURST_8 = 0x03; // bits 1:0 = 3
74
75// AUX_X_LSB..AUX_R_MSB (8 bytes) sit directly before the accel data, so one burst can read both.
76static constexpr uint8_t BMI270_AUX_DATA_LEN = 8;
77static_assert(BMI270_REG_AUX_X_LSB + BMI270_AUX_DATA_LEN == BMI270_REG_DATA_8, "aux data must precede accel data");
78
85
96
104
115
116#ifdef USE_BMI270_BMM150
117// What is wired to this instance's auxiliary (secondary I2C master) interface; another
118// instance in the same build may have nothing attached.
123#endif
124
126 public:
127 void setup() override;
128 void dump_config() override;
129
133 void set_gyro_odr(BMI270GyroODR o) { this->gyro_odr_ = o; }
134#ifdef USE_BMI270_BMM150
135 void set_aux_device(BMI270AuxDevice device) { this->aux_device_ = device; }
137 HUB_PUBLISHER(magnetometer, const bmm150::BMM150Data &)
138#endif
139
140 protected:
141 bool update_data(motion::MotionData &data) override;
143#ifdef USE_BMI270_BMM150
144 // Returns the step that failed, or nullptr once the BMM150 is running
145 const LogString *setup_magnetometer_();
146 // Polls the aux busy bit, as the aux transaction time depends on the attached device
147 bool wait_for_aux_idle_();
148 // Manual-mode aux read of 8 bytes starting at `reg` on the aux device
149 bool read_aux_block_(uint8_t reg, uint8_t *buf);
150 bool write_aux_register_(uint8_t reg, uint8_t value);
151#endif
152
157#ifdef USE_BMI270_BMM150
160 uint8_t aux_device_address_{bmm150::BMM150_DEFAULT_I2C_ADDRESS};
162#endif
163};
164
165} // namespace esphome::bmi270
void set_gyro_range(BMI270GyroRange r)
Definition bmi270.h:132
void set_accel_range(BMI270AccelRange r)
Definition bmi270.h:130
bmm150::BMM150Trim mag_trim_
Definition bmi270.h:158
void set_accel_odr(BMI270AccelODR o)
Definition bmi270.h:131
void set_aux_device(BMI270AuxDevice device)
Definition bmi270.h:135
HUB_PUBLISHER(magnetometer, const bmm150::BMM150Data &) protected boo load_config_file_)()
Definition bmi270.h:142
bool read_aux_block_(uint8_t reg, uint8_t *buf)
Definition bmi270.cpp:133
BMI270AuxDevice aux_device_
Definition bmi270.h:159
BMI270AccelRange accel_range_
Definition bmi270.h:153
bool write_aux_register_(uint8_t reg, uint8_t value)
Definition bmi270.cpp:128
const LogString * setup_magnetometer_()
Definition bmi270.cpp:140
void set_gyro_odr(BMI270GyroODR o)
Definition bmi270.h:133
void set_aux_device_address(uint8_t address)
Definition bmi270.h:136
BMI270GyroRange gyro_range_
Definition bmi270.h:155
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
HUB_PUBLISHER(data, const MotionData &) HUB_PUBLISHER(temperature
virtual bool update_data(MotionData &data)=0
@ BMI270_ACCEL_ODR_25
Definition bmi270.h:88
@ BMI270_ACCEL_ODR_800
Definition bmi270.h:93
@ BMI270_ACCEL_ODR_400
Definition bmi270.h:92
@ BMI270_ACCEL_ODR_200
Definition bmi270.h:91
@ BMI270_ACCEL_ODR_100
Definition bmi270.h:90
@ BMI270_ACCEL_ODR_1600
Definition bmi270.h:94
@ BMI270_ACCEL_ODR_12_5
Definition bmi270.h:87
@ BMI270_ACCEL_ODR_50
Definition bmi270.h:89
@ BMI270_GYRO_RANGE_250
Definition bmi270.h:101
@ BMI270_GYRO_RANGE_500
Definition bmi270.h:100
@ BMI270_GYRO_RANGE_125
Definition bmi270.h:102
@ BMI270_GYRO_RANGE_2000
Definition bmi270.h:98
@ BMI270_GYRO_RANGE_1000
Definition bmi270.h:99
@ BMI270_AUX_DEVICE_BMM150
Definition bmi270.h:121
@ BMI270_AUX_DEVICE_NONE
Definition bmi270.h:120
@ BMI270_ACCEL_RANGE_4G
Definition bmi270.h:81
@ BMI270_ACCEL_RANGE_16G
Definition bmi270.h:83
@ BMI270_ACCEL_RANGE_2G
Definition bmi270.h:80
@ BMI270_ACCEL_RANGE_8G
Definition bmi270.h:82
@ BMI270_GYRO_ODR_200
Definition bmi270.h:109
@ BMI270_GYRO_ODR_1600
Definition bmi270.h:112
@ BMI270_GYRO_ODR_100
Definition bmi270.h:108
@ BMI270_GYRO_ODR_800
Definition bmi270.h:111
@ BMI270_GYRO_ODR_400
Definition bmi270.h:110
@ BMI270_GYRO_ODR_3200
Definition bmi270.h:113
uint32_t value
Definition hal.cpp:59