10#ifdef USE_BMI270_BMM150
11using namespace bmm150;
19#if defined(USE_ARDUINO) && !defined(USE_ESP32)
20static constexpr size_t MAX_I2C_BUFFER_SIZE = 32;
22static constexpr size_t MAX_I2C_BUFFER_SIZE = 256;
27 if (!this->
write_byte(BMI270_REG_PWR_CONF, BMI270_PWR_CONF_ADV_POWER_SAVE_OFF))
30 if (!this->
write_byte(BMI270_REG_INIT_CTRL, BMI270_INIT_CTRL_LOAD_START))
33 constexpr size_t cfg_len =
sizeof(BMI270_CONFIG_FILE);
35 uint8_t buf[1 + MAX_I2C_BUFFER_SIZE];
36 buf[0] = BMI270_REG_INIT_DATA;
39 while (index != cfg_len) {
41 const uint8_t addr[2] = {
static_cast<uint8_t
>((index / 2) & 0x0F),
static_cast<uint8_t
>((index / 2) >> 4)};
45 size_t burst =
clamp_at_most(cfg_len - index, MAX_I2C_BUFFER_SIZE);
55 if (!this->
write_byte(BMI270_REG_INIT_CTRL, BMI270_INIT_CTRL_LOAD_DONE))
60 if (!this->
read_byte(BMI270_REG_INTERNAL_STATUS, &status))
62 if ((status & BMI270_INTERNAL_STATUS_MASK) != BMI270_INTERNAL_STATUS_INIT_OK) {
63 ESP_LOGE(TAG,
"Config load failed: INTERNAL_STATUS=0x%02X (expected 0x01)", status);
70 MotionComponent::setup();
71 if (!this->
check_chip_id(
this, BMI270_REG_CHIP_ID, BMI270_CHIP_ID_VALUE))
82 const uint8_t acc_conf =
83 this->
accel_odr_ | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) | BMI270_ACC_CONF_PERF_MODE;
84 const uint8_t gyr_conf = this->
gyro_odr_ | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) |
85 BMI270_GYR_CONF_NOISE_PERF | BMI270_GYR_CONF_FILTER_PERF;
88 this->
write_byte(BMI270_REG_GYR_CONF, gyr_conf) &&
90 this->
write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU)))
98#ifdef USE_BMI270_BMM150
104 ESP_LOGW(TAG,
"BMM150 magnetometer setup failed at %s; continuing without it", LOG_STR_ARG(failed_step));
110#ifdef USE_BMI270_BMM150
111static_assert(BMM150_TRIM_BLOCK_LEN == BMI270_AUX_DATA_LEN,
"each trim block is one aux read");
114 static constexpr uint32_t AUX_IDLE_TIMEOUT_MS = 20;
120 if ((
status & BMI270_STATUS_AUX_BUSY) == 0)
122 if (
millis() - start >= AUX_IDLE_TIMEOUT_MS)
135 this->
read_bytes(BMI270_REG_AUX_X_LSB, buf, BMI270_AUX_DATA_LEN);
141 if (!(this->
write_byte(BMI270_REG_IF_CONF, BMI270_IF_CONF_AUX_IF_EN) &&
142 this->
write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_MANUAL_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8) &&
144 return LOG_STR(
"aux interface");
149 return LOG_STR(
"power-on");
150 delay(BMM150_START_UP_TIME_MS);
152 return LOG_STR(
"soft reset");
153 delay(BMM150_SOFT_RESET_TIME_MS);
155 uint8_t chip_id[BMI270_AUX_DATA_LEN];
157 return LOG_STR(
"chip ID read");
158 if (chip_id[0] != BMM150_CHIP_ID_VALUE) {
159 ESP_LOGW(TAG,
"BMM150 chip ID 0x%02X, expected 0x%02X", chip_id[0], BMM150_CHIP_ID_VALUE);
160 return LOG_STR(
"chip ID check");
163 uint8_t trim[BMM150_TRIM_BLOCKS * BMM150_TRIM_BLOCK_LEN];
164 for (uint8_t i = 0; i < BMM150_TRIM_BLOCKS; i++) {
165 if (!this->
read_aux_block_(BMM150_REG_TRIM_START + i * BMM150_TRIM_BLOCK_LEN, trim + i * BMM150_TRIM_BLOCK_LEN))
166 return LOG_STR(
"trim read");
171 return LOG_STR(
"normal mode");
173 if (!(this->
write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_FCU_WRITE_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8 |
174 BMI270_AUX_IF_CONF_RD_BURST_8) &&
175 this->
write_byte(BMI270_REG_AUX_RD_ADDR, BMM150_REG_DATA_X_LSB) &&
176 this->
write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU | BMI270_PWR_CTRL_AUX_EN)))
177 return LOG_STR(
"auto-burst mode");
183 ESP_LOGCONFIG(TAG,
"BMI270 IMU:");
184 LOG_I2C_DEVICE(
this);
189 " Accel range : %s\n"
191 LOG_STR_ARG(AccelRangeStrings::get_log_str(this->
accel_range_)),
192 LOG_STR_ARG(GyroRangeStrings::get_log_str(this->
gyro_range_)));
193#ifdef USE_BMI270_BMM150
195 ESP_LOGCONFIG(TAG,
" Aux device : BMM150 magnetometer (%s)",
199 MotionComponent::dump_config();
209#ifdef USE_BMI270_BMM150
210 const bool read_mag = this->
magnetometer_ready_ && this->magnetometer_publisher_ !=
nullptr;
211 uint8_t buf[BMI270_AUX_DATA_LEN + REG_READ_LEN];
212 const uint8_t *mag_raw = buf;
213 const uint8_t *raw_data = read_mag ? buf + BMI270_AUX_DATA_LEN : buf;
214 const bool read_ok = read_mag ? this->
read_bytes(BMI270_REG_AUX_X_LSB, buf,
sizeof(buf))
215 : this->
read_bytes(BMI270_REG_DATA_8, buf, REG_READ_LEN);
217 uint8_t buf[REG_READ_LEN];
218 const uint8_t *raw_data = buf;
219 const bool read_ok = this->
read_bytes(BMI270_REG_DATA_8, buf, REG_READ_LEN);
222 ESP_LOGW(TAG,
"Failed to read IMU data");
228 alignas(4)
static constexpr float ACCEL_SCALE[] PROGMEM = {
242 alignas(4)
static constexpr float GYRO_SCALE[] PROGMEM = {
243 2000.0f / 32768.0f, 1000.0f / 32768.0f, 500.0f / 32768.0f, 250.0f / 32768.0f, 125.0f / 32768.0f,
245 static constexpr uint8_t GYR_OFFS = BMI270_REG_DATA_14 - BMI270_REG_DATA_8;
252#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
253 if (this->temperature_publisher_ !=
nullptr) {
256 static constexpr uint8_t TEMP_OFFS = BMI270_REG_TEMP_0 - BMI270_REG_DATA_8;
263#ifdef USE_BMI270_BMM150
266 this->magnetometer_publisher_(mag_data);
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.