ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
qmi8658.cpp
Go to the documentation of this file.
1#include "qmi8658.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
5
6namespace esphome::qmi8658 {
7
8ESPHOME_LOG_TAG(TAG, "qmi8658");
9
10PROGMEM_STRING_TABLE(AccelRangeStrings, "±2g", "±4g", "±8g", "±16g");
11PROGMEM_STRING_TABLE(GyroRangeStrings, "±16°/s", "±32°/s", "±64°/s", "±128°/s", "±256°/s", "±512°/s", "±1024°/s",
12 "±2048°/s");
13
14// Acceleration scale (g per LSB), indexed by accel_range_ >> 4.
15// Full-scale = range_g, mapped over a signed 16-bit value (2^15 counts).
16static constexpr float ACCEL_SCALE[] = {
17 2.0f / 32768.0f,
18 4.0f / 32768.0f,
19 8.0f / 32768.0f,
20 16.0f / 32768.0f,
21};
22
23// Angular rate scale (°/s per LSB), indexed by gyro_range_ >> 4.
24static constexpr float GYRO_SCALE[] = {
25 16.0f / 32768.0f, 32.0f / 32768.0f, 64.0f / 32768.0f, 128.0f / 32768.0f,
26 256.0f / 32768.0f, 512.0f / 32768.0f, 1024.0f / 32768.0f, 2048.0f / 32768.0f,
27};
28
30 MotionComponent::setup();
31
32 // 1. Verify chip ID
33 if (!this->check_chip_id(this, QMI8658_REG_WHO_AM_I, QMI8658_WHO_AM_I_VALUE))
34 return;
35
36 // 2. Soft reset
37 if (!this->setup_check(this->write_byte(QMI8658_REG_RESET, QMI8658_RESET_CMD)))
38 return;
39 delay(15); // spec: wait for reset to complete
40
41 // 3. Serial interface: enable register address auto-increment
42 if (!this->setup_check(this->write_byte(QMI8658_REG_CTRL1, QMI8658_CTRL1_VALUE),
43 LOG_STR("Failed to write REG_CTRL1")))
44 return;
45
46 // 4. Configure accelerometer (CTRL2 = range | ODR)
47 if (!this->setup_check(
48 this->write_byte(QMI8658_REG_CTRL2, (uint8_t) (this->accel_range_) | (uint8_t) (this->accel_odr_)),
49 LOG_STR("Failed to write REG_CTRL2")))
50 return;
51
52 // 5. Configure gyroscope (CTRL3 = range | ODR)
53 if (!this->setup_check(
54 this->write_byte(QMI8658_REG_CTRL3, (uint8_t) (this->gyro_range_) | (uint8_t) (this->gyro_odr_)),
55 LOG_STR("Failed to write REG_CTRL3")))
56 return;
57
58 // 6. Disable the built-in low-pass filters (leave raw data to the motion pipeline)
59 if (!this->setup_check(this->write_byte(QMI8658_REG_CTRL5, 0x00), LOG_STR("Failed to write REG_CTRL5")))
60 return;
61
62 // 7. Enable accelerometer and gyroscope
63 if (!this->setup_check(this->write_byte(QMI8658_REG_CTRL7, QMI8658_CTRL7_ACC_EN | QMI8658_CTRL7_GYR_EN),
64 LOG_STR("Failed to write REG_CTRL7")))
65 return;
66
67 ESP_LOGCONFIG(TAG, "QMI8658 initialised successfully");
68}
69
71 ESP_LOGCONFIG(TAG, "QMI8658 IMU:");
72 LOG_I2C_DEVICE(this);
73 if (this->is_failed())
74 return;
75
76 ESP_LOGCONFIG(TAG, " Accel range : %s", LOG_STR_ARG(AccelRangeStrings::get_log_str(this->accel_range_ >> 4)));
77 ESP_LOGCONFIG(TAG, " Gyro range : %s", LOG_STR_ARG(GyroRangeStrings::get_log_str(this->gyro_range_ >> 4)));
78 MotionComponent::dump_config();
79}
80
82 if (this->is_failed())
83 return false;
84
85 // Read temperature + accel + gyro in one contiguous block starting at TEMP_L.
86 uint8_t raw_data[REG_READ_LEN];
87 if (!this->read_bytes(QMI8658_REG_TEMP_L, raw_data, REG_READ_LEN)) {
88 ESP_LOGW(TAG, "Failed to read IMU data");
89 return false;
90 }
91
92 // Data is little-endian (low byte first).
93 float scale = ACCEL_SCALE[this->accel_range_ >> 4];
94 int16_t raw_x = encode_value_le<int16_t>(&raw_data[ACC_OFFS]);
95 int16_t raw_y = encode_value_le<int16_t>(&raw_data[ACC_OFFS + 2]);
96 int16_t raw_z = encode_value_le<int16_t>(&raw_data[ACC_OFFS + 4]);
97 ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z);
98 data.acceleration[motion::X_AXIS] = raw_x * scale;
99 data.acceleration[motion::Y_AXIS] = raw_y * scale;
100 data.acceleration[motion::Z_AXIS] = raw_z * scale;
101
102 scale = GYRO_SCALE[this->gyro_range_ >> 4];
103 raw_x = encode_value_le<int16_t>(&raw_data[GYR_OFFS]);
104 raw_y = encode_value_le<int16_t>(&raw_data[GYR_OFFS + 2]);
105 raw_z = encode_value_le<int16_t>(&raw_data[GYR_OFFS + 4]);
106 ESP_LOGV(TAG, "Read raw gyro data: %d, %d, %d", raw_x, raw_y, raw_z);
107 data.angular_rate[motion::X_AXIS] = raw_x * scale;
108 data.angular_rate[motion::Y_AXIS] = raw_y * scale;
109 data.angular_rate[motion::Z_AXIS] = raw_z * scale;
110
111#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
112 if (this->temperature_publisher_ == nullptr)
113 return true;
114 // Temperature: signed 16-bit, °C = raw / 256
115 int16_t raw_t = encode_value_le<int16_t>(&raw_data[TEMP_OFFS]);
116 this->temperature_publisher_(raw_t / 256.0f);
117#endif
118 return true;
119}
120
121} // namespace esphome::qmi8658
bool is_failed() const
Definition component.h:288
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
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.
Definition i2c.h:232
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
QMI8658AccelRange accel_range_
Definition qmi8658.h:102
bool update_data(motion::MotionData &data) override
Definition qmi8658.cpp:81
PROGMEM_STRING_TABLE(AccelRangeStrings, "±2g", "±4g", "±8g", "±16g")
ESPHOME_LOG_TAG(TAG, "qmi8658")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
void HOT delay(uint32_t ms)
Definition hal.cpp:78