ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
bmi160.cpp
Go to the documentation of this file.
1#include "bmi160.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace bmi160 {
7
8static const char *const TAG = "bmi160";
9
10const uint8_t BMI160_REGISTER_CHIPID = 0x00;
11
12const uint8_t BMI160_REGISTER_CMD = 0x7E;
13enum class Cmd : uint8_t {
14 START_FOC = 0x03,
15 ACCL_SET_PMU_MODE = 0b00010000, // last 2 bits are mode
16 GYRO_SET_PMU_MODE = 0b00010100, // last 2 bits are mode
17 MAG_SET_PMU_MODE = 0b00011000, // last 2 bits are mode
18 PROG_NVM = 0xA0,
19 FIFO_FLUSH = 0xB0,
20 INT_RESET = 0xB1,
21 SOFT_RESET = 0xB6,
22 STEP_CNT_CLR = 0xB2,
23};
24enum class GyroPmuMode : uint8_t {
25 SUSPEND = 0b00,
26 NORMAL = 0b01,
27 LOW_POWER = 0b10,
28};
29enum class AcclPmuMode : uint8_t {
30 SUSPEND = 0b00,
31 NORMAL = 0b01,
32 FAST_STARTUP = 0b11,
33};
34enum class MagPmuMode : uint8_t {
35 SUSPEND = 0b00,
36 NORMAL = 0b01,
37 LOW_POWER = 0b10,
38};
39
40const uint8_t BMI160_REGISTER_ACCEL_CONFIG = 0x40;
41enum class AcclFilterMode : uint8_t {
42 POWER_SAVING = 0b00000000,
43 PERF = 0b10000000,
44};
45enum class AcclBandwidth : uint8_t {
46 OSR4_AVG1 = 0b00000000,
47 OSR2_AVG2 = 0b00010000,
48 NORMAL_AVG4 = 0b00100000,
49 RES_AVG8 = 0b00110000,
50 RES_AVG16 = 0b01000000,
51 RES_AVG32 = 0b01010000,
52 RES_AVG64 = 0b01100000,
53 RES_AVG128 = 0b01110000,
54};
55enum class AccelOutputDataRate : uint8_t {
56 HZ_25_32 = 0b0001, // 25/32 Hz
57 HZ_25_16 = 0b0010, // 25/16 Hz
58 HZ_25_8 = 0b0011, // 25/8 Hz
59 HZ_25_4 = 0b0100, // 25/4 Hz
60 HZ_25_2 = 0b0101, // 25/2 Hz
61 HZ_25 = 0b0110, // 25 Hz
62 HZ_50 = 0b0111, // 50 Hz
63 HZ_100 = 0b1000, // 100 Hz
64 HZ_200 = 0b1001, // 200 Hz
65 HZ_400 = 0b1010, // 400 Hz
66 HZ_800 = 0b1011, // 800 Hz
67 HZ_1600 = 0b1100, // 1600 Hz
68};
69const uint8_t BMI160_REGISTER_ACCEL_RANGE = 0x41;
70enum class AccelRange : uint8_t {
71 RANGE_2G = 0b0011,
72 RANGE_4G = 0b0101,
73 RANGE_8G = 0b1000,
74 RANGE_16G = 0b1100,
75};
76
77const uint8_t BMI160_REGISTER_GYRO_CONFIG = 0x42;
78enum class GyroBandwidth : uint8_t {
79 OSR4 = 0x00,
80 OSR2 = 0x10,
81 NORMAL = 0x20,
82};
83enum class GyroOuputDataRate : uint8_t {
84 HZ_25 = 0x06,
85 HZ_50 = 0x07,
86 HZ_100 = 0x08,
87 HZ_200 = 0x09,
88 HZ_400 = 0x0A,
89 HZ_800 = 0x0B,
90 HZ_1600 = 0x0C,
91 HZ_3200 = 0x0D,
92};
93const uint8_t BMI160_REGISTER_GYRO_RANGE = 0x43;
94enum class GyroRange : uint8_t {
95 RANGE_2000_DPS = 0x0, // ±2000 °/s
96 RANGE_1000_DPS = 0x1,
97 RANGE_500_DPS = 0x2,
98 RANGE_250_DPS = 0x3,
99 RANGE_125_DPS = 0x4,
100};
101
114const uint8_t BMI160_REGISTER_DATA_TEMP_LSB = 0x20;
115const uint8_t BMI160_REGISTER_DATA_TEMP_MSB = 0x21;
116
117const float GRAVITY_EARTH = 9.80665f;
118
120 switch (stage) {
121 case 0:
122 uint8_t chipid;
123 if (!this->read_byte(BMI160_REGISTER_CHIPID, &chipid) || (chipid != 0b11010001)) {
124 this->mark_failed();
125 return;
126 }
127
128 ESP_LOGV(TAG, " Bringing accelerometer out of sleep");
129 if (!this->write_byte(BMI160_REGISTER_CMD, (uint8_t) Cmd::ACCL_SET_PMU_MODE | (uint8_t) AcclPmuMode::NORMAL)) {
130 this->mark_failed();
131 return;
132 }
133 ESP_LOGV(TAG, " Waiting for accelerometer to wake up");
134 // need to wait (max delay in datasheet) because we can't send commands while another is in progress
135 // min 5ms, 10ms
136 this->set_timeout(10, [this]() { this->internal_setup_(1); });
137 break;
138
139 case 1:
140 ESP_LOGV(TAG, " Bringing gyroscope out of sleep");
141 if (!this->write_byte(BMI160_REGISTER_CMD, (uint8_t) Cmd::GYRO_SET_PMU_MODE | (uint8_t) GyroPmuMode::NORMAL)) {
142 this->mark_failed();
143 return;
144 }
145 ESP_LOGV(TAG, " Waiting for gyroscope to wake up");
146 // wait between 51 & 81ms, doing 100 to be safe
147 this->set_timeout(10, [this]() { this->internal_setup_(2); });
148 break;
149
150 case 2:
151 ESP_LOGV(TAG, " Setting up Gyro Config");
152 uint8_t gyro_config = (uint8_t) GyroBandwidth::OSR4 | (uint8_t) GyroOuputDataRate::HZ_25;
153 ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
154 if (!this->write_byte(BMI160_REGISTER_GYRO_CONFIG, gyro_config)) {
155 this->mark_failed();
156 return;
157 }
158 ESP_LOGV(TAG, " Setting up Gyro Range");
159 uint8_t gyro_range = (uint8_t) GyroRange::RANGE_2000_DPS;
160 ESP_LOGV(TAG, " Output gyro_range: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_range));
161 if (!this->write_byte(BMI160_REGISTER_GYRO_RANGE, gyro_range)) {
162 this->mark_failed();
163 return;
164 }
165
166 ESP_LOGV(TAG, " Setting up Accel Config");
167 uint8_t accel_config =
169 ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
170 if (!this->write_byte(BMI160_REGISTER_ACCEL_CONFIG, accel_config)) {
171 this->mark_failed();
172 return;
173 }
174 ESP_LOGV(TAG, " Setting up Accel Range");
175 uint8_t accel_range = (uint8_t) AccelRange::RANGE_16G;
176 ESP_LOGV(TAG, " Output accel_range: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_range));
177 if (!this->write_byte(BMI160_REGISTER_ACCEL_RANGE, accel_range)) {
178 this->mark_failed();
179 return;
180 }
181
182 this->setup_complete_ = true;
183 }
184}
185
188 ESP_LOGCONFIG(TAG, "BMI160:");
189 LOG_I2C_DEVICE(this);
190 if (this->is_failed()) {
191 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
192 }
193 LOG_UPDATE_INTERVAL(this);
194 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
195 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
196 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
197 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
198 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
199 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
200 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
201}
202
203i2c::ErrorCode BMI160Component::read_le_int16_(uint8_t reg, int16_t *value, uint8_t len) {
204 uint8_t raw_data[len * 2];
205 // read using read_register because we have little-endian data, and read_bytes_16 will swap it
206 i2c::ErrorCode err = this->read_register(reg, raw_data, len * 2, true);
207 if (err != i2c::ERROR_OK) {
208 return err;
209 }
210 for (int i = 0; i < len; i++) {
211 value[i] = (int16_t) ((uint16_t) raw_data[i * 2] | ((uint16_t) raw_data[i * 2 + 1] << 8));
212 }
213 return err;
214}
215
217 if (!this->setup_complete_) {
218 return;
219 }
220
221 ESP_LOGV(TAG, " Updating BMI160");
222 int16_t data[6];
223 if (this->read_le_int16_(BMI160_REGISTER_DATA_GYRO_X_LSB, data, 6) != i2c::ERROR_OK) {
224 this->status_set_warning();
225 return;
226 }
227
228 float gyro_x = (float) data[0] / (float) INT16_MAX * 2000.f;
229 float gyro_y = (float) data[1] / (float) INT16_MAX * 2000.f;
230 float gyro_z = (float) data[2] / (float) INT16_MAX * 2000.f;
231 float accel_x = (float) data[3] / (float) INT16_MAX * 16 * GRAVITY_EARTH;
232 float accel_y = (float) data[4] / (float) INT16_MAX * 16 * GRAVITY_EARTH;
233 float accel_z = (float) data[5] / (float) INT16_MAX * 16 * GRAVITY_EARTH;
234
235 int16_t raw_temperature;
236 if (this->read_le_int16_(BMI160_REGISTER_DATA_TEMP_LSB, &raw_temperature, 1) != i2c::ERROR_OK) {
237 this->status_set_warning();
238 return;
239 }
240 float temperature = (float) raw_temperature / (float) INT16_MAX * 64.5f + 23.f;
241
242 ESP_LOGD(TAG,
243 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
244 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
245 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
246
247 if (this->accel_x_sensor_ != nullptr)
248 this->accel_x_sensor_->publish_state(accel_x);
249 if (this->accel_y_sensor_ != nullptr)
250 this->accel_y_sensor_->publish_state(accel_y);
251 if (this->accel_z_sensor_ != nullptr)
252 this->accel_z_sensor_->publish_state(accel_z);
253
254 if (this->temperature_sensor_ != nullptr)
255 this->temperature_sensor_->publish_state(temperature);
256
257 if (this->gyro_x_sensor_ != nullptr)
258 this->gyro_x_sensor_->publish_state(gyro_x);
259 if (this->gyro_y_sensor_ != nullptr)
260 this->gyro_y_sensor_->publish_state(gyro_y);
261 if (this->gyro_z_sensor_ != nullptr)
262 this->gyro_z_sensor_->publish_state(gyro_z);
263
264 this->status_clear_warning();
265}
267
268} // namespace bmi160
269} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
sensor::Sensor * accel_z_sensor_
Definition bmi160.h:30
i2c::ErrorCode read_le_int16_(uint8_t reg, int16_t *value, uint8_t len)
reads len 16-bit little-endian integers from the given i2c register
Definition bmi160.cpp:203
sensor::Sensor * gyro_y_sensor_
Definition bmi160.h:33
sensor::Sensor * gyro_x_sensor_
Definition bmi160.h:32
sensor::Sensor * accel_x_sensor_
Definition bmi160.h:28
sensor::Sensor * gyro_z_sensor_
Definition bmi160.h:34
void internal_setup_(int stage)
Definition bmi160.cpp:119
sensor::Sensor * temperature_sensor_
Definition bmi160.h:31
sensor::Sensor * accel_y_sensor_
Definition bmi160.h:29
float get_setup_priority() const override
Definition bmi160.cpp:266
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
const uint8_t BMI160_REGISTER_DATA_ACCEL_X_LSB
Definition bmi160.cpp:108
const uint8_t BMI160_REGISTER_DATA_ACCEL_Z_MSB
Definition bmi160.cpp:113
const uint8_t BMI160_REGISTER_DATA_TEMP_MSB
Definition bmi160.cpp:115
const uint8_t BMI160_REGISTER_DATA_ACCEL_X_MSB
Definition bmi160.cpp:109
const uint8_t BMI160_REGISTER_GYRO_RANGE
Definition bmi160.cpp:93
const uint8_t BMI160_REGISTER_DATA_ACCEL_Z_LSB
Definition bmi160.cpp:112
const uint8_t BMI160_REGISTER_DATA_GYRO_Y_LSB
Definition bmi160.cpp:104
const uint8_t BMI160_REGISTER_DATA_GYRO_Z_LSB
Definition bmi160.cpp:106
const uint8_t BMI160_REGISTER_ACCEL_RANGE
Definition bmi160.cpp:69
const uint8_t BMI160_REGISTER_DATA_TEMP_LSB
Definition bmi160.cpp:114
const uint8_t BMI160_REGISTER_DATA_GYRO_X_LSB
Definition bmi160.cpp:102
const uint8_t BMI160_REGISTER_GYRO_CONFIG
Definition bmi160.cpp:77
const uint8_t BMI160_REGISTER_DATA_GYRO_Z_MSB
Definition bmi160.cpp:107
const uint8_t BMI160_REGISTER_DATA_ACCEL_Y_LSB
Definition bmi160.cpp:110
const uint8_t BMI160_REGISTER_DATA_GYRO_Y_MSB
Definition bmi160.cpp:105
const uint8_t BMI160_REGISTER_CMD
Definition bmi160.cpp:12
const uint8_t BMI160_REGISTER_CHIPID
Definition bmi160.cpp:10
const uint8_t BMI160_REGISTER_DATA_ACCEL_Y_MSB
Definition bmi160.cpp:111
const uint8_t BMI160_REGISTER_DATA_GYRO_X_MSB
Definition bmi160.cpp:103
const float GRAVITY_EARTH
Definition bmi160.cpp:117
const uint8_t BMI160_REGISTER_ACCEL_CONFIG
Definition bmi160.cpp:40
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
uint16_t temperature
Definition sun_gtil2.cpp:12