ESPHome 2026.11.0b6
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->write_byte(QMI8658_REG_RESET, QMI8658_RESET_CMD)) {
38 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
39 return;
40 }
41 delay(15); // spec: wait for reset to complete
42
43 // 3. Serial interface: enable register address auto-increment
44 if (!this->write_byte(QMI8658_REG_CTRL1, QMI8658_CTRL1_VALUE)) {
45 this->mark_failed(LOG_STR("Failed to write REG_CTRL1"));
46 return;
47 }
48
49 // 4. Configure accelerometer (CTRL2 = range | ODR)
50 if (!this->write_byte(QMI8658_REG_CTRL2, (uint8_t) (this->accel_range_) | (uint8_t) (this->accel_odr_))) {
51 this->mark_failed(LOG_STR("Failed to write REG_CTRL2"));
52 return;
53 }
54
55 // 5. Configure gyroscope (CTRL3 = range | ODR)
56 if (!this->write_byte(QMI8658_REG_CTRL3, (uint8_t) (this->gyro_range_) | (uint8_t) (this->gyro_odr_))) {
57 this->mark_failed(LOG_STR("Failed to write REG_CTRL3"));
58 return;
59 }
60
61 // 6. Disable the built-in low-pass filters (leave raw data to the motion pipeline)
62 if (!this->write_byte(QMI8658_REG_CTRL5, 0x00)) {
63 this->mark_failed(LOG_STR("Failed to write REG_CTRL5"));
64 return;
65 }
66
67 // 7. Enable accelerometer and gyroscope
68 if (!this->write_byte(QMI8658_REG_CTRL7, QMI8658_CTRL7_ACC_EN | QMI8658_CTRL7_GYR_EN)) {
69 this->mark_failed(LOG_STR("Failed to write REG_CTRL7"));
70 return;
71 }
72
73 ESP_LOGCONFIG(TAG, "QMI8658 initialised successfully");
74}
75
77 ESP_LOGCONFIG(TAG, "QMI8658 IMU:");
78 LOG_I2C_DEVICE(this);
79 if (this->is_failed())
80 return;
81
82 ESP_LOGCONFIG(TAG, " Accel range : %s",
83 LOG_STR_ARG(AccelRangeStrings::get_log_str(this->accel_range_ >> 4, AccelRangeStrings::LAST_INDEX)));
84 ESP_LOGCONFIG(TAG, " Gyro range : %s",
85 LOG_STR_ARG(GyroRangeStrings::get_log_str(this->gyro_range_ >> 4, GyroRangeStrings::LAST_INDEX)));
86 MotionComponent::dump_config();
87}
88
90 if (this->is_failed())
91 return false;
92
93 // Read temperature + accel + gyro in one contiguous block starting at TEMP_L.
94 uint8_t raw_data[REG_READ_LEN];
95 if (!this->read_bytes(QMI8658_REG_TEMP_L, raw_data, REG_READ_LEN)) {
96 ESP_LOGW(TAG, "Failed to read IMU data");
97 return false;
98 }
99
100 // Data is little-endian (low byte first).
101 float scale = ACCEL_SCALE[this->accel_range_ >> 4];
102 int16_t raw_x = encode_value_le<int16_t>(&raw_data[ACC_OFFS]);
103 int16_t raw_y = encode_value_le<int16_t>(&raw_data[ACC_OFFS + 2]);
104 int16_t raw_z = encode_value_le<int16_t>(&raw_data[ACC_OFFS + 4]);
105 ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z);
106 data.acceleration[motion::X_AXIS] = raw_x * scale;
107 data.acceleration[motion::Y_AXIS] = raw_y * scale;
108 data.acceleration[motion::Z_AXIS] = raw_z * scale;
109
110 scale = GYRO_SCALE[this->gyro_range_ >> 4];
111 raw_x = encode_value_le<int16_t>(&raw_data[GYR_OFFS]);
112 raw_y = encode_value_le<int16_t>(&raw_data[GYR_OFFS + 2]);
113 raw_z = encode_value_le<int16_t>(&raw_data[GYR_OFFS + 4]);
114 ESP_LOGV(TAG, "Read raw gyro data: %d, %d, %d", raw_x, raw_y, raw_z);
115 data.angular_rate[motion::X_AXIS] = raw_x * scale;
116 data.angular_rate[motion::Y_AXIS] = raw_y * scale;
117 data.angular_rate[motion::Z_AXIS] = raw_z * scale;
118
119 if (this->temperature_callback_.empty())
120 return true;
121 // Temperature: signed 16-bit, °C = raw / 256
122 int16_t raw_t = encode_value_le<int16_t>(&raw_data[TEMP_OFFS]);
123 this->temperature_callback_.call(raw_t / 256.0f);
124 return true;
125}
126
127} // namespace esphome::qmi8658
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
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:103
bool update_data(motion::MotionData &data) override
Definition qmi8658.cpp:89
LazyCallbackManager< void(float)> temperature_callback_
Definition qmi8658.h:108
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:1084
void HOT delay(uint32_t ms)
Definition hal.cpp:85