Compare commits

...
9 changed files with 248 additions and 213 deletions

No files matched your search

+67 -136
View File
@@ -21,48 +21,38 @@ static constexpr size_t MAX_I2C_BUFFER_SIZE = 32;
static constexpr size_t MAX_I2C_BUFFER_SIZE = 256;
#endif
// Configuration blob upload
// The BMI270 requires a firmware config blob to be written to its internal
// memory after every power-on before sensors can be used.
bool BMI270Component::load_config_file_() {
// 1. Disable advanced power-save so the config port is accessible
// The config port is only reachable with advanced power save off
if (!this->write_byte(BMI270_REG_PWR_CONF, BMI270_PWR_CONF_ADV_POWER_SAVE_OFF))
return false;
delay(1);
// 2. Prepare config load: write 0x00 to INIT_CTRL to start
if (!this->write_byte(BMI270_REG_INIT_CTRL, BMI270_INIT_CTRL_LOAD_START))
return false;
// 3. Burst-write the config in pages
const uint8_t *cfg = BMI270_CONFIG_FILE;
constexpr size_t cfg_len = sizeof(BMI270_CONFIG_FILE);
// The register byte goes first, so each burst is copied once and sent with write()
uint8_t buf[1 + MAX_I2C_BUFFER_SIZE];
buf[0] = BMI270_REG_INIT_DATA;
size_t index = 0;
while (index != cfg_len) {
// Set the page address in INIT_ADDR registers
uint8_t addr_lsb = (uint8_t) ((index / 2) & 0x0F);
uint8_t addr_msb = (uint8_t) ((index / 2) >> 4);
if (!this->write_byte(BMI270_REG_INIT_ADDR_0, addr_lsb))
return false;
if (!this->write_byte(BMI270_REG_INIT_ADDR_0 + 1, addr_msb))
// INIT_ADDR_0 takes the word address bits 0-3 and INIT_ADDR_1 bits 4-11
const uint8_t addr[2] = {static_cast<uint8_t>((index / 2) & 0x0F), static_cast<uint8_t>((index / 2) >> 4)};
if (this->write_register(BMI270_REG_INIT_ADDR_0, addr, sizeof(addr)) != i2c::ERROR_OK)
return false;
// Write a burst of up to the maximum allowed size
size_t burst = clamp_at_most(cfg_len - index, MAX_I2C_BUFFER_SIZE);
if (this->write_register(BMI270_REG_INIT_DATA, cfg + index, burst) != i2c::ERROR_OK)
progmem_memcpy(buf + 1, BMI270_CONFIG_FILE + index, burst);
if (this->write(buf, burst + 1) != i2c::ERROR_OK)
return false;
index += burst;
}
// 4. Signal end of config load
if (!this->write_byte(BMI270_REG_INIT_CTRL, BMI270_INIT_CTRL_LOAD_DONE))
return false;
delay(20); // spec: wait ≥20 ms for init to complete
delay(20); // The datasheet asks for at least 20 ms before INTERNAL_STATUS is valid
// 5. Check INTERNAL_STATUS: bit[0:3] should be 0x01 ("initialisation OK")
uint8_t status = 0;
if (!this->read_byte(BMI270_REG_INTERNAL_STATUS, &status))
return false;
@@ -73,74 +63,52 @@ bool BMI270Component::load_config_file_() {
return true;
}
// setup() ─
void BMI270Component::setup() {
MotionComponent::setup();
// 1. Verify chip ID
if (!this->check_chip_id(this, BMI270_REG_CHIP_ID, BMI270_CHIP_ID_VALUE))
return;
// 2. Soft-reset via CMD register (0x7E = 0xB6)
if (!this->setup_check(this->write_byte(BMI270_REG_CMD, BMI270_CMD_SOFT_RESET)))
return;
delay(20);
// 4. Upload the configuration blob
if (!this->setup_check(load_config_file_(), LOG_STR("Config file upload failed")))
return;
ESP_LOGD(TAG, "Config blob uploaded ✓");
// 5. Configure accelerometer
// ACC_CONF: ODR | BWP(0x2 = normal avg4) | perf_mode(1)
uint8_t acc_conf =
(uint8_t) (accel_odr_) | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) | BMI270_ACC_CONF_PERF_MODE;
if (!this->setup_check(this->write_byte(BMI270_REG_ACC_CONF, acc_conf)))
return;
if (!this->setup_check(this->write_byte(BMI270_REG_ACC_RANGE, (uint8_t) accel_range_)))
if (!this->setup_check(this->load_config_file_(), LOG_STR("Config file upload failed")))
return;
// 6. Configure gyroscope
// GYR_CONF: ODR | BWP(0x2 = normal) | noise_perf(1) | filter_perf(1)
uint8_t gyr_conf = (uint8_t) (gyro_odr_) | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) |
BMI270_GYR_CONF_NOISE_PERF | BMI270_GYR_CONF_FILTER_PERF;
if (!this->setup_check(this->write_byte(BMI270_REG_GYR_CONF, gyr_conf)))
return;
if (!this->setup_check(this->write_byte(BMI270_REG_GYR_RANGE, (uint8_t) gyro_range_)))
return;
// 7. Enable accelerometer, gyroscope, and temperature sensor
// PWR_CTRL bits: temp_en[3] | gyr_en[2] | acc_en[1]
if (!this->setup_check(this->write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU)))
// Normal filter bandwidth; performance mode for the accelerometer, noise and filter performance for the gyroscope
const uint8_t acc_conf =
this->accel_odr_ | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) | BMI270_ACC_CONF_PERF_MODE;
const uint8_t gyr_conf = this->gyro_odr_ | (BMI270_CONF_BWP_NORMAL << BMI270_CONF_BWP_SHIFT) |
BMI270_GYR_CONF_NOISE_PERF | BMI270_GYR_CONF_FILTER_PERF;
if (!this->setup_check(this->write_byte(BMI270_REG_ACC_CONF, acc_conf) &&
this->write_byte(BMI270_REG_ACC_RANGE, this->accel_range_) &&
this->write_byte(BMI270_REG_GYR_CONF, gyr_conf) &&
this->write_byte(BMI270_REG_GYR_RANGE, this->gyro_range_) &&
this->write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU)))
return;
delay(5);
// 8. Re-enable advanced power save (optional; keeps current low between reads)
// Disabled here for simplicity – leave in performance mode
// bit1 = fifo_self_wakeup
// Advanced power save stays off; FIFO self-wakeup on
if (!this->setup_check(this->write_byte(BMI270_REG_PWR_CONF, BMI270_PWR_CONF_FIFO_SELF_WAKEUP)))
return;
// 9. Optionally bring up whatever is wired to the aux interface.
// This is entirely additive: it is off by default, and failure only disables
// magnetometer reporting rather than the whole component.
#ifdef USE_BMI270_BMM150
// A missing magnetometer only disables magnetometer reporting
if (this->aux_device_ == BMI270_AUX_DEVICE_BMM150) {
this->magnetometer_ready_ = this->setup_magnetometer_();
const LogString *failed_step = this->setup_magnetometer_();
this->magnetometer_ready_ = failed_step == nullptr;
if (!this->magnetometer_ready_) {
ESP_LOGW(TAG, "BMM150 magnetometer setup failed; continuing without it");
ESP_LOGW(TAG, "BMM150 magnetometer setup failed at %s; continuing without it", LOG_STR_ARG(failed_step));
}
}
#endif
ESP_LOGCONFIG(TAG, "BMI270 initialised successfully");
}
#ifdef USE_BMI270_BMM150
bool BMI270Component::wait_for_aux_idle_(uint32_t timeout_ms) {
// The STATUS aux-busy bit is set while an aux transaction is in flight. Aux transaction
// duration depends on the attached device's own timing, so poll with a bounded
// timeout instead of guessing a fixed delay.
static_assert(BMM150_TRIM_BLOCK_LEN == BMI270_AUX_DATA_LEN, "each trim block is one aux read");
bool BMI270Component::wait_for_aux_idle_() {
static constexpr uint32_t AUX_IDLE_TIMEOUT_MS = 20;
uint32_t start = millis();
while (true) {
uint8_t status = 0;
@@ -148,98 +116,63 @@ bool BMI270Component::wait_for_aux_idle_(uint32_t timeout_ms) {
return false;
if ((status & BMI270_STATUS_AUX_BUSY) == 0)
return true;
if (millis() - start >= timeout_ms)
if (millis() - start >= AUX_IDLE_TIMEOUT_MS)
return false;
delay(1);
}
}
bool BMI270Component::write_aux_register_(uint8_t reg, uint8_t value) {
if (!this->write_byte(BMI270_REG_AUX_WR_DATA, value))
return false;
if (!this->write_byte(BMI270_REG_AUX_WR_ADDR, reg))
return false;
return this->wait_for_aux_idle_(20);
return this->write_byte(BMI270_REG_AUX_WR_DATA, value) && this->write_byte(BMI270_REG_AUX_WR_ADDR, reg) &&
this->wait_for_aux_idle_();
}
bool BMI270Component::read_aux_block_(uint8_t reg, uint8_t *buf) {
if (!this->write_byte(BMI270_REG_AUX_RD_ADDR, reg))
return false;
if (!this->wait_for_aux_idle_(20))
return false;
return this->read_bytes(BMI270_REG_AUX_X_LSB, buf, BMI270_AUX_DATA_LEN);
return this->write_byte(BMI270_REG_AUX_RD_ADDR, reg) && this->wait_for_aux_idle_() &&
this->read_bytes(BMI270_REG_AUX_X_LSB, buf, BMI270_AUX_DATA_LEN);
}
bool BMI270Component::read_magnetometer_trim_() {
uint8_t regs[BMM150_TRIM_BLOCKS * BMM150_TRIM_BLOCK_LEN];
for (uint8_t i = 0; i < BMM150_TRIM_BLOCKS; i++) {
if (!this->read_aux_block_(BMM150_REG_TRIM_START + i * BMM150_TRIM_BLOCK_LEN, regs + i * BMM150_TRIM_BLOCK_LEN))
return false;
}
this->mag_trim_ = bmm150_parse_trim(regs);
return true;
}
bool BMI270Component::setup_magnetometer_() {
// Sequence adapted from the Bosch BMI270 aux-interface protocol: enable the secondary
// I2C master in manual mode, point it at the BMM150's address, power the BMM150 up
// and soft-reset it, verify its chip ID, read its factory trim values, put it in
// normal/30Hz mode, then switch the aux interface to auto-burst mode so BMI270 keeps
// AUX_X/Y/Z/R refreshed from the BMM150's data registers.
ESP_LOGD(TAG, "Setting up BMM150 magnetometer via BMI270 aux interface...");
if (!this->write_byte(BMI270_REG_IF_CONF, BMI270_IF_CONF_AUX_IF_EN))
return false;
if (!this->write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_MANUAL_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8))
return false;
if (!this->write_byte(BMI270_REG_AUX_DEV_ID, (uint8_t) (this->aux_device_address_ << 1)))
return false;
// Follows the Bosch BMI270 aux-interface sequence; ends in auto-burst mode, where the BMI270 keeps
// AUX_X/Y/Z/R filled from the BMM150's data registers
const LogString *BMI270Component::setup_magnetometer_() {
if (!(this->write_byte(BMI270_REG_IF_CONF, BMI270_IF_CONF_AUX_IF_EN) &&
this->write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_MANUAL_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8) &&
this->write_byte(BMI270_REG_AUX_DEV_ID, this->aux_device_address_ << 1)))
return LOG_STR("aux interface");
// Suspend -> sleep, then a soft reset so the BMM150 starts from a known state even if it
// stayed powered across a restart. The chip ID is only readable once the power bit is set.
if (!this->write_aux_register_(BMM150_REG_POWER_CONTROL, BMM150_CMD_POWER_ON)) {
ESP_LOGW(TAG, "BMM150 power-on write timed out");
return false;
}
if (!this->write_aux_register_(BMM150_REG_POWER_CONTROL, BMM150_CMD_POWER_ON))
return LOG_STR("power-on");
delay(BMM150_START_UP_TIME_MS);
if (!this->write_aux_register_(BMM150_REG_POWER_CONTROL, BMM150_CMD_SOFT_RESET)) {
ESP_LOGW(TAG, "BMM150 soft-reset write timed out");
return false;
}
if (!this->write_aux_register_(BMM150_REG_POWER_CONTROL, BMM150_CMD_SOFT_RESET))
return LOG_STR("soft reset");
delay(BMM150_SOFT_RESET_TIME_MS);
uint8_t chip_id[BMI270_AUX_DATA_LEN];
if (!this->read_aux_block_(BMM150_REG_CHIP_ID, chip_id)) {
ESP_LOGW(TAG, "BMM150 chip-ID read failed");
return false;
}
if (!this->read_aux_block_(BMM150_REG_CHIP_ID, chip_id))
return LOG_STR("chip ID read");
if (chip_id[0] != BMM150_CHIP_ID_VALUE) {
ESP_LOGW(TAG, "BMM150 not detected (chip ID 0x%02X, expected 0x%02X)", chip_id[0], BMM150_CHIP_ID_VALUE);
return false;
ESP_LOGW(TAG, "BMM150 chip ID 0x%02X, expected 0x%02X", chip_id[0], BMM150_CHIP_ID_VALUE);
return LOG_STR("chip ID check");
}
if (!this->read_magnetometer_trim_()) {
ESP_LOGW(TAG, "BMM150 trim data read failed");
return false;
uint8_t trim[BMM150_TRIM_BLOCKS * BMM150_TRIM_BLOCK_LEN];
for (uint8_t i = 0; i < BMM150_TRIM_BLOCKS; i++) {
if (!this->read_aux_block_(BMM150_REG_TRIM_START + i * BMM150_TRIM_BLOCK_LEN, trim + i * BMM150_TRIM_BLOCK_LEN))
return LOG_STR("trim read");
}
this->mag_trim_ = bmm150_parse_trim(trim);
if (!this->write_aux_register_(BMM150_REG_OP_MODE, BMM150_CMD_NORMAL_MODE_ODR_30HZ)) {
ESP_LOGW(TAG, "BMM150 mode write timed out");
return false;
}
if (!this->write_aux_register_(BMM150_REG_OP_MODE, BMM150_CMD_NORMAL_MODE_ODR_30HZ))
return LOG_STR("normal mode");
// Switch to auto-burst mode: BMI270 will keep polling 8 bytes from the BMM150's
// data registers (0x42) into its own AUX_X/Y/Z/R registers automatically.
if (!this->write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_FCU_WRITE_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8 |
BMI270_AUX_IF_CONF_RD_BURST_8))
return false;
if (!this->write_byte(BMI270_REG_AUX_RD_ADDR, BMM150_REG_DATA_X_LSB))
return false;
if (!this->write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU | BMI270_PWR_CTRL_AUX_EN))
return false;
ESP_LOGD(TAG, "BMM150 magnetometer ready");
return true;
if (!(this->write_byte(BMI270_REG_AUX_IF_CONF, BMI270_AUX_IF_CONF_FCU_WRITE_EN | BMI270_AUX_IF_CONF_MAN_RD_BURST_8 |
BMI270_AUX_IF_CONF_RD_BURST_8) &&
this->write_byte(BMI270_REG_AUX_RD_ADDR, BMM150_REG_DATA_X_LSB) &&
this->write_byte(BMI270_REG_PWR_CTRL, BMI270_PWR_CTRL_IMU | BMI270_PWR_CTRL_AUX_EN)))
return LOG_STR("auto-burst mode");
return nullptr;
}
#endif // USE_BMI270_BMM150
@@ -249,9 +182,10 @@ void BMI270Component::dump_config() {
if (this->is_failed())
return;
ESP_LOGCONFIG(TAG, " Accel range : %s",
LOG_STR_ARG(AccelRangeStrings::get_log_str(this->accel_range_, AccelRangeStrings::LAST_INDEX)));
ESP_LOGCONFIG(TAG, " Gyro range : %s",
ESP_LOGCONFIG(TAG,
" Accel range : %s\n"
" Gyro range : %s",
LOG_STR_ARG(AccelRangeStrings::get_log_str(this->accel_range_, AccelRangeStrings::LAST_INDEX)),
LOG_STR_ARG(GyroRangeStrings::get_log_str(this->gyro_range_, GyroRangeStrings::LAST_INDEX)));
#ifdef USE_BMI270_BMM150
if (this->aux_device_ == BMI270_AUX_DEVICE_BMM150) {
@@ -262,9 +196,6 @@ void BMI270Component::dump_config() {
MotionComponent::dump_config();
}
// update() ─
// Reads all 6 axes + temperature in one block
bool BMI270Component::update_data(motion::MotionData &data) {
if (this->is_failed())
return false;
+8 -24
View File
@@ -12,18 +12,15 @@
namespace esphome::bmi270 {
// Register map
static constexpr uint8_t BMI270_REG_CHIP_ID = 0x00;
static constexpr uint8_t BMI270_REG_ERR_REG = 0x02;
static constexpr uint8_t BMI270_REG_STATUS = 0x03;
static constexpr uint8_t BMI270_REG_DATA_8 = 0x0C; // ACC_X LSB
static constexpr uint8_t BMI270_REG_DATA_14 = 0x12; // GYR_X LSB
static constexpr uint8_t BMI270_REG_TEMP_0 = 0x22;
static constexpr uint8_t BMI270_REG_TEMP_MSB = 0x23; // temperature (2 bytes big-endian ish)
static constexpr uint8_t BMI270_REG_TEMP_MSB = 0x23;
static constexpr uint8_t REG_READ_LEN =
BMI270_REG_TEMP_MSB - BMI270_REG_DATA_8 +
1; // 0x23 - 0x0C + 1 = 0x18 bytes total for accel(6) + gyro(6) + temp(2) + padding(4)
// Accel, gyro, 10 unused bytes, then temperature: 0x0C to 0x23
static constexpr uint8_t REG_READ_LEN = BMI270_REG_TEMP_MSB - BMI270_REG_DATA_8 + 1;
static constexpr uint8_t BMI270_REG_PWR_CONF = 0x7C;
static constexpr uint8_t BMI270_REG_PWR_CTRL = 0x7D;
@@ -80,7 +77,6 @@ static constexpr uint8_t BMI270_AUX_IF_CONF_RD_BURST_8 = 0x03; // bits 1:0
static constexpr uint8_t BMI270_AUX_DATA_LEN = 8;
static_assert(BMI270_REG_AUX_X_LSB + BMI270_AUX_DATA_LEN == BMI270_REG_DATA_8, "aux data must precede accel data");
// Accelerometer range options
enum BMI270AccelRange : uint8_t {
BMI270_ACCEL_RANGE_2G = 0x00,
BMI270_ACCEL_RANGE_4G = 0x01,
@@ -88,7 +84,6 @@ enum BMI270AccelRange : uint8_t {
BMI270_ACCEL_RANGE_16G = 0x03,
};
// Accelerometer ODR options
enum BMI270AccelODR : uint8_t {
BMI270_ACCEL_ODR_12_5 = 0x05,
BMI270_ACCEL_ODR_25 = 0x06,
@@ -100,7 +95,6 @@ enum BMI270AccelODR : uint8_t {
BMI270_ACCEL_ODR_1600 = 0x0C,
};
// Gyroscope range options
enum BMI270GyroRange : uint8_t {
BMI270_GYRO_RANGE_2000 = 0x00,
BMI270_GYRO_RANGE_1000 = 0x01,
@@ -109,7 +103,6 @@ enum BMI270GyroRange : uint8_t {
BMI270_GYRO_RANGE_125 = 0x04,
};
// Gyroscope ODR options
enum BMI270GyroODR : uint8_t {
BMI270_GYRO_ODR_25 = 0x06,
BMI270_GYRO_ODR_50 = 0x07,
@@ -130,14 +123,11 @@ enum BMI270AuxDevice : uint8_t {
};
#endif
// Main component class
class BMI270Component final : public motion::MotionComponent, public i2c::I2CDevice {
public:
// Lifecycle
void setup() override;
void dump_config() override;
// Configuration setters
void set_accel_range(BMI270AccelRange r) { this->accel_range_ = r; }
void set_accel_odr(BMI270AccelODR o) { this->accel_odr_ = o; }
void set_gyro_range(BMI270GyroRange r) { this->gyro_range_ = r; }
@@ -153,21 +143,15 @@ class BMI270Component final : public motion::MotionComponent, public i2c::I2CDev
bool update_data(motion::MotionData &data) override;
bool load_config_file_();
#ifdef USE_BMI270_BMM150
// Brings up the BMM150 over the BMI270's aux interface. Optional: on failure this only
// disables magnetometer reporting, it does not fail the whole component (accel/gyro still work).
bool setup_magnetometer_();
// Blocking poll of the aux-transaction-busy bit; aux transaction time depends on the
// attached device, so this is bounded by retries/timeout rather than a fixed delay.
bool wait_for_aux_idle_(uint32_t timeout_ms);
// Manual-mode aux read of 8 bytes starting at `reg` on the aux device.
// Returns the step that failed, or nullptr once the BMM150 is running
const LogString *setup_magnetometer_();
// Polls the aux busy bit, as the aux transaction time depends on the attached device
bool wait_for_aux_idle_();
// Manual-mode aux read of 8 bytes starting at `reg` on the aux device
bool read_aux_block_(uint8_t reg, uint8_t *buf);
// Manual-mode aux write of one byte to `reg` on the aux device.
bool write_aux_register_(uint8_t reg, uint8_t value);
// Reads the BMM150 factory trim values used to compensate its samples.
bool read_magnetometer_trim_();
#endif
// Config
BMI270AccelRange accel_range_{BMI270_ACCEL_RANGE_4G};
BMI270AccelODR accel_odr_{BMI270_ACCEL_ODR_100};
BMI270GyroRange gyro_range_{BMI270_GYRO_RANGE_2000};
+2 -1
View File
@@ -1,5 +1,6 @@
#pragma once
#include <cstdint>
#include "esphome/core/hal.h"
namespace esphome::bmi270 {
@@ -44,7 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
after every power cycle, before any sensor data can be read.
--------------------------------------------------------------------------- */
static constexpr uint8_t BMI270_CONFIG_FILE[] = {
static constexpr uint8_t BMI270_CONFIG_FILE[] PROGMEM = {
0xc8, 0x2e, 0x00, 0x2e, 0x80, 0x2e, 0x3d, 0xb1, 0xc8, 0x2e, 0x00, 0x2e, 0x80, 0x2e, 0x91, 0x03, 0x80, 0x2e, 0xbc,
0xb0, 0x80, 0x2e, 0xa3, 0x03, 0xc8, 0x2e, 0x00, 0x2e, 0x80, 0x2e, 0x00, 0xb0, 0x50, 0x30, 0x21, 0x2e, 0x59, 0xf5,
0x10, 0x30, 0x21, 0x2e, 0x6a, 0xf5, 0x80, 0x2e, 0x3b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x08, 0x19, 0x01, 0x00, 0x22,
+16 -16
View File
@@ -3,11 +3,8 @@
#include <cstdint>
#include "esphome/core/helpers.h"
// BMM150 magnetometer definitions - the chip that can optionally be wired to the BMI270's
// auxiliary (secondary I2C master) interface. Lives in its own header/namespace because this
// is BMM150-specific driver knowledge, not BMI270 knowledge - it just can't be a standalone
// ESPHome component, since the BMM150 has no address of its own on the main I2C bus and is only
// reachable by having the BMI270 proxy register reads/writes through its aux interface.
// BMM150 magnetometer behind the BMI270's aux interface; it is not on the main I2C bus, so it is not
// its own component
namespace esphome::bmi270::bmm150 {
static constexpr uint8_t BMM150_DEFAULT_I2C_ADDRESS = 0x10;
@@ -64,20 +61,23 @@ inline uint16_t bmm150_trim_word(const uint8_t *regs, uint8_t reg) {
return encode_value_le<uint16_t>(&regs[offset]);
}
// A trim byte, addressed by its register
inline uint8_t bmm150_trim_byte(const uint8_t *regs, uint8_t reg) { return regs[reg - BMM150_REG_TRIM_START]; }
// Decodes the trim registers; `regs` holds the bytes of registers 0x5D onwards (at least 21).
inline BMM150Trim bmm150_parse_trim(const uint8_t *regs) {
BMM150Trim trim;
trim.x1 = static_cast<int8_t>(regs[BMM150_TRIM_X1 - BMM150_REG_TRIM_START]);
trim.y1 = static_cast<int8_t>(regs[BMM150_TRIM_Y1 - BMM150_REG_TRIM_START]);
trim.x1 = static_cast<int8_t>(bmm150_trim_byte(regs, BMM150_TRIM_X1));
trim.y1 = static_cast<int8_t>(bmm150_trim_byte(regs, BMM150_TRIM_Y1));
trim.z4 = static_cast<int16_t>(bmm150_trim_word(regs, BMM150_TRIM_Z4));
trim.x2 = static_cast<int8_t>(regs[BMM150_TRIM_X2 - BMM150_REG_TRIM_START]);
trim.y2 = static_cast<int8_t>(regs[BMM150_TRIM_Y2 - BMM150_REG_TRIM_START]);
trim.x2 = static_cast<int8_t>(bmm150_trim_byte(regs, BMM150_TRIM_X2));
trim.y2 = static_cast<int8_t>(bmm150_trim_byte(regs, BMM150_TRIM_Y2));
trim.z2 = static_cast<int16_t>(bmm150_trim_word(regs, BMM150_TRIM_Z2));
trim.z1 = bmm150_trim_word(regs, BMM150_TRIM_Z1);
trim.xyz1 = bmm150_trim_word(regs, BMM150_TRIM_XYZ1) & BMM150_TRIM_XYZ1_MASK;
trim.z3 = static_cast<int16_t>(bmm150_trim_word(regs, BMM150_TRIM_Z3));
trim.xy2 = static_cast<int8_t>(regs[BMM150_TRIM_XY2 - BMM150_REG_TRIM_START]);
trim.xy1 = regs[BMM150_TRIM_XY1 - BMM150_REG_TRIM_START];
trim.xy2 = static_cast<int8_t>(bmm150_trim_byte(regs, BMM150_TRIM_XY2));
trim.xy1 = bmm150_trim_byte(regs, BMM150_TRIM_XY1);
return trim;
}
@@ -108,11 +108,11 @@ inline float bmm150_compensate_z(const BMM150Trim &trim, int16_t raw, uint16_t r
// Converts 8 raw bytes from the BMM150 data registers (X, Y, Z, RHALL; 0x42-0x49) to compensated µT.
inline BMM150Data bmm150_convert(const BMM150Trim &trim, const uint8_t *raw) {
// X/Y are 13-bit (LSB byte bits 7:3), Z is 15-bit (bits 7:1), RHALL is 14-bit (bits 7:2).
int16_t x = (int16_t) (((int16_t) (int8_t) raw[1] * 32) | (raw[0] >> 3));
int16_t y = (int16_t) (((int16_t) (int8_t) raw[3] * 32) | (raw[2] >> 3));
int16_t z = (int16_t) (((int16_t) (int8_t) raw[5] * 128) | (raw[4] >> 1));
uint16_t rhall = (uint16_t) (((uint16_t) raw[7] << 6) | (raw[6] >> 2));
// Left-aligned in their 16-bit words: X/Y are 13-bit, Z is 15-bit, RHALL is 14-bit
const int16_t x = encode_value_le<int16_t>(&raw[0]) >> 3;
const int16_t y = encode_value_le<int16_t>(&raw[2]) >> 3;
const int16_t z = encode_value_le<int16_t>(&raw[4]) >> 1;
const uint16_t rhall = encode_value_le<uint16_t>(&raw[6]) >> 2;
return BMM150Data{
.x = bmm150_compensate_xy(trim, trim.x1, trim.x2, x, rhall),
.y = bmm150_compensate_xy(trim, trim.y1, trim.y2, y, rhall),
+28 -21
View File
@@ -17,6 +17,14 @@ DEPENDENCIES = ["i2c"]
CONF_AUX_ADDRESS = "aux_address"
CONF_AUX_DEVICE = "aux_device"
# to_code skips the setters at these values, so each must match its member default in bmi270.h
DEFAULT_ACCELEROMETER_RANGE = "4G"
DEFAULT_ACCELEROMETER_ODR = "100HZ"
DEFAULT_GYROSCOPE_RANGE = "2000DPS"
DEFAULT_GYROSCOPE_ODR = "200HZ"
# The BMM150's fixed address
DEFAULT_AUX_ADDRESS = 0x10
# Enum proxies (must match the C++ enum values exactly)
BMI270AuxDevice = bmi270_ns.enum("BMI270AuxDevice")
BMI270AccelRange = bmi270_ns.enum("BMI270AccelRange")
@@ -57,50 +65,49 @@ GYRO_ODR_OPTIONS = {
"3200HZ": BMI270GyroODR.BMI270_GYRO_ODR_3200,
}
# Top-level CONFIG_SCHEMA
CONFIG_SCHEMA = (
motion_schema(BMI270Component, has_accel=True, has_gyro=True)
.extend(
{
cv.Optional(CONF_ACCELEROMETER_RANGE, default="4G"): cv.enum(
ACCEL_RANGE_OPTIONS, upper=True
),
cv.Optional(CONF_ACCELEROMETER_ODR, default="100HZ"): cv.enum(
ACCEL_ODR_OPTIONS, upper=True
),
cv.Optional(CONF_GYROSCOPE_RANGE, default="2000DPS"): cv.enum(
cv.Optional(
CONF_ACCELEROMETER_RANGE, default=DEFAULT_ACCELEROMETER_RANGE
): cv.enum(ACCEL_RANGE_OPTIONS, upper=True),
cv.Optional(
CONF_ACCELEROMETER_ODR, default=DEFAULT_ACCELEROMETER_ODR
): cv.enum(ACCEL_ODR_OPTIONS, upper=True),
cv.Optional(CONF_GYROSCOPE_RANGE, default=DEFAULT_GYROSCOPE_RANGE): cv.enum(
GYRO_RANGE_OPTIONS, upper=True
),
cv.Optional(CONF_GYROSCOPE_ODR, default="200HZ"): cv.enum(
cv.Optional(CONF_GYROSCOPE_ODR, default=DEFAULT_GYROSCOPE_ODR): cv.enum(
GYRO_ODR_OPTIONS, upper=True
),
# Optional device wired to the BMI270's aux interface (not the main I2C bus).
# Defaults to NONE so existing configs/boards are unaffected.
cv.Optional(CONF_AUX_DEVICE, default="NONE"): cv.one_of(
"NONE", "BMM150", upper=True
),
# Default matches the BMM150's fixed I2C address; irrelevant when aux_device is NONE.
cv.Optional(CONF_AUX_ADDRESS, default=0x10): cv.i2c_address,
cv.Optional(CONF_AUX_ADDRESS, default=DEFAULT_AUX_ADDRESS): cv.i2c_address,
}
)
.extend(i2c.i2c_device_schema(0x68))
)
# Code generation
async def to_code(config: ConfigType) -> None:
var = await new_motion_component(config)
await i2c.register_i2c_device(var, config)
# Accelerometer sensors
# Hardware configuration
cg.add(var.set_accel_range(config[CONF_ACCELEROMETER_RANGE]))
cg.add(var.set_accel_odr(config[CONF_ACCELEROMETER_ODR]))
cg.add(var.set_gyro_range(config[CONF_GYROSCOPE_RANGE]))
cg.add(var.set_gyro_odr(config[CONF_GYROSCOPE_ODR]))
# The C++ members already hold the schema defaults
for key, default, setter in (
(CONF_ACCELEROMETER_RANGE, DEFAULT_ACCELEROMETER_RANGE, var.set_accel_range),
(CONF_ACCELEROMETER_ODR, DEFAULT_ACCELEROMETER_ODR, var.set_accel_odr),
(CONF_GYROSCOPE_RANGE, DEFAULT_GYROSCOPE_RANGE, var.set_gyro_range),
(CONF_GYROSCOPE_ODR, DEFAULT_GYROSCOPE_ODR, var.set_gyro_odr),
):
if config[key] != default:
cg.add(setter(config[key]))
# The aux interface code only compiles when some instance has a BMM150; the
# instance still records its own device, as another one may have none
if config[CONF_AUX_DEVICE] == "BMM150":
cg.add_define("USE_BMI270_BMM150")
cg.add(var.set_aux_device(BMI270AuxDevice.BMI270_AUX_DEVICE_BMM150))
cg.add(var.set_aux_device_address(config[CONF_AUX_ADDRESS]))
if (address := config[CONF_AUX_ADDRESS]) != DEFAULT_AUX_ADDRESS:
cg.add(var.set_aux_device_address(address))
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# bmi270 declares i2c on its motion platform, which the harness does not walk
manifest.dependencies = manifest.dependencies + ["i2c", "motion"]
+4 -15
View File
@@ -36,7 +36,6 @@ sensor:
name: "BMI270 Accel Z"
accuracy_decimals: 4
# Gyroscope axes (unit: °/s)
- platform: motion
motion_id: bmi270_motion
type: gyroscope_x
@@ -76,21 +75,11 @@ motion:
- platform: bmi270
id: bmi270_motion
i2c_id: i2c_bus
# Accelerometer full-scale range: 2G | 4G | 8G | 16G
accelerometer_range: 4G
# Accelerometer output data rate: 12_5HZ | 25HZ | 50HZ | 100HZ |
# 200HZ | 400HZ | 800HZ | 1600HZ
accelerometer_odr: 100HZ
# Gyroscope full-scale range: 125DPS | 250DPS | 500DPS | 1000DPS | 2000DPS
gyroscope_range: 2000DPS
# Gyroscope output data rate: 25HZ | 50HZ | 100HZ | 200HZ |
# 400HZ | 800HZ | 1600HZ | 3200HZ
gyroscope_odr: 200HZ
accelerometer_range: 8G
accelerometer_odr: 200HZ
gyroscope_range: 1000DPS
gyroscope_odr: 400HZ
aux_device: BMM150
aux_address: 0x10
axis_map:
x: y
y: x
+48
View File
@@ -0,0 +1,48 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "esphome/components/bmi270/bmm150_aux.h"
namespace esphome::bmi270::bmm150::testing {
// The axis decode before it used encode_value_le, kept as the reference
static BMM150Data reference_convert(const BMM150Trim &trim, const uint8_t *raw) {
int16_t x = (int16_t) (((int16_t) (int8_t) raw[1] * 32) | (raw[0] >> 3));
int16_t y = (int16_t) (((int16_t) (int8_t) raw[3] * 32) | (raw[2] >> 3));
int16_t z = (int16_t) (((int16_t) (int8_t) raw[5] * 128) | (raw[4] >> 1));
uint16_t rhall = (uint16_t) (((uint16_t) raw[7] << 6) | (raw[6] >> 2));
return BMM150Data{
.x = bmm150_compensate_xy(trim, trim.x1, trim.x2, x, rhall),
.y = bmm150_compensate_xy(trim, trim.y1, trim.y2, y, rhall),
.z = bmm150_compensate_z(trim, z, rhall),
};
}
TEST(BMM150Convert, MatchesTheReferenceForEveryWordOfEachField) {
const BMM150Trim trim{.x1 = 0,
.y1 = 0,
.x2 = 26,
.y2 = 26,
.xy2 = -3,
.xy1 = 29,
.z2 = 760,
.z3 = 0,
.z4 = 0,
.z1 = 24786,
.xyz1 = 7101};
// Sweep one field through every 16-bit value while the others hold mid-range readings
for (int field = 0; field < 4; field++) {
for (uint32_t word = 0; word <= 0xFFFF; word++) {
uint8_t raw[8] = {0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A};
raw[2 * field] = word & 0xFF;
raw[2 * field + 1] = word >> 8;
const BMM150Data got = bmm150_convert(trim, raw);
const BMM150Data want = reference_convert(trim, raw);
ASSERT_THAT(got.x, ::testing::NanSensitiveFloatEq(want.x)) << "field " << field << " word 0x" << std::hex << word;
ASSERT_THAT(got.y, ::testing::NanSensitiveFloatEq(want.y)) << "field " << field << " word 0x" << std::hex << word;
ASSERT_THAT(got.z, ::testing::NanSensitiveFloatEq(want.z)) << "field " << field << " word 0x" << std::hex << word;
}
}
}
} // namespace esphome::bmi270::bmm150::testing
@@ -0,0 +1,69 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <array>
#include <cstring>
#include "esphome/components/bmi270/bmi270.h"
#include "esphome/components/bmi270/bmi270_config.h"
namespace esphome::bmi270::testing {
// Host builds upload in 256-byte bursts
static constexpr size_t MAX_BURST = 256;
// A BMI270 register file: writes go to consecutive registers, except INIT_DATA, which streams into
// the config memory at the word address set in INIT_ADDR_0/1
class Bmi270Bus final : public i2c::I2CBus {
public:
std::array<uint8_t, 128> regs{};
std::array<uint8_t, sizeof(BMI270_CONFIG_FILE)> config_memory{};
unsigned upload_writes{0};
Bmi270Bus() {
this->regs[BMI270_REG_CHIP_ID] = BMI270_CHIP_ID_VALUE;
this->regs[BMI270_REG_INTERNAL_STATUS] = BMI270_INTERNAL_STATUS_INIT_OK;
}
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
size_t out_size) override {
if (size == 0)
return i2c::ERROR_INVALID_ARGUMENT;
uint8_t reg = data[0];
if (size > 1) {
if (reg == BMI270_REG_INIT_ADDR_0 || reg == BMI270_REG_INIT_ADDR_0 + 1 || reg == BMI270_REG_INIT_DATA)
++this->upload_writes;
if (reg == BMI270_REG_INIT_DATA) {
size_t offset =
2 * ((this->regs[BMI270_REG_INIT_ADDR_0] & 0x0F) | (this->regs[BMI270_REG_INIT_ADDR_0 + 1] << 4));
if (offset + size - 1 > this->config_memory.size())
return i2c::ERROR_INVALID_ARGUMENT;
std::memcpy(this->config_memory.data() + offset, data + 1, size - 1);
} else {
for (size_t i = 1; i < size; i++)
this->regs[(reg + i - 1) & 0x7F] = data[i];
}
}
for (size_t i = 0; i < out_size; i++)
out[i] = this->regs[(reg + i) & 0x7F];
return i2c::ERROR_OK;
}
};
TEST(BMI270ConfigUpload, UploadsTheWholeConfigFileInOrder) {
Bmi270Bus bus;
BMI270Component imu;
imu.set_i2c_bus(&bus);
imu.set_i2c_address(0x68);
imu.setup();
EXPECT_FALSE(imu.is_failed());
EXPECT_EQ(0, std::memcmp(bus.config_memory.data(), BMI270_CONFIG_FILE, sizeof(BMI270_CONFIG_FILE)));
EXPECT_EQ(bus.regs[BMI270_REG_INIT_CTRL], BMI270_INIT_CTRL_LOAD_DONE);
// One INIT_ADDR write and one INIT_DATA write per burst
EXPECT_EQ(bus.upload_writes, 2 * ((sizeof(BMI270_CONFIG_FILE) + MAX_BURST - 1) / MAX_BURST));
}
} // namespace esphome::bmi270::testing
#endif