ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
icm20689.cpp
Go to the documentation of this file.
1#include "icm20689.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5#include <cstddef>
6#include <cstdint>
7
9
10ESPHOME_LOG_TAG(TAG, "icm20689");
11
12namespace {
13
14// Range enum values 0..3 map to full-scale settings of 2x/4x/8x/16x the base range.
15constexpr float accel_scale(uint8_t range) { return (2.0f * (1U << range)) / 32768.0f; }
16
17constexpr float gyro_scale(uint8_t range) { return (250.0f * (1U << range)) / 32768.0f; }
18
19} // namespace
20
21void ICM20689Component::write_register_(uint8_t reg, uint8_t value) { this->write_register(reg & 0x7F, value); }
22
23void ICM20689Component::read_registers_(uint8_t reg, uint8_t *data, size_t len) {
24 this->read_registers(reg | ICM20689_READ_BIT, data, len);
25}
26
28 MotionComponent::setup();
29
30 this->spi_setup();
31
32 if (!this->setup_check(this->spi_is_ready(), LOG_STR("SPI device is not ready")))
33 return;
34
35 // Datasheet 4.20: a soft reset must be the first operation after power-up,
36 // before any register initialization. WHO_AM_I is only valid afterwards.
37 this->write_register_(ICM20689_REG_PWR_MGMT_1, ICM20689_SOFT_RESET);
38 delay(100); // NOLINT
39
40 // The chip comes out of reset in sleep mode: wake it and select the
41 // auto-selected best clock source (required for full gyro performance).
42 this->write_register_(ICM20689_REG_PWR_MGMT_1, ICM20689_CLKSEL_AUTO);
43
44 // USER_CTRL is not writable in sleep mode, so I2C_IF_DIS is set once awake.
45 // Datasheet 6.1: disable the I2C interface when using SPI.
46 this->write_register_(ICM20689_REG_USER_CTRL, ICM20689_I2C_IF_DIS);
47
48 uint8_t who_am_i = 0;
49 this->read_registers_(ICM20689_REG_WHO_AM_I, &who_am_i, 1);
50
51 if (who_am_i != ICM20689_WHO_AM_I_VALUE) {
52 ESP_LOGE(TAG, "Unexpected WHO_AM_I 0x%02X (expected 0x%02X) -- check wiring/CS", who_am_i, ICM20689_WHO_AM_I_VALUE);
53 this->mark_failed(LOG_STR("Wrong chip ID"));
54 return;
55 }
56
57 // Allow the sensor to settle after waking from sleep.
58 delay(50); // NOLINT
59
60 // Configure the accelerometer and gyroscope full-scale ranges.
61 this->write_register_(ICM20689_REG_ACCEL_CONFIG, static_cast<uint8_t>(this->accel_range_) << 3);
62 this->write_register_(ICM20689_REG_GYRO_CONFIG, static_cast<uint8_t>(this->gyro_range_) << 3);
63}
64
66 ESP_LOGCONFIG(TAG, "ICM20689 IMU:");
67 LOG_PIN(" CS Pin: ", this->cs_);
68
69 if (this->is_failed())
70 return;
71
72 ESP_LOGCONFIG(TAG, " Accel range: ±%dg", 2 << static_cast<uint8_t>(this->accel_range_));
73 ESP_LOGCONFIG(TAG, " Gyro range: ±%d°/s", 250 << static_cast<uint8_t>(this->gyro_range_));
74
75 MotionComponent::dump_config();
76}
77
79 if (this->is_failed())
80 return false;
81
82 const uint8_t accel_range = static_cast<uint8_t>(this->accel_range_);
83 const uint8_t gyro_range = static_cast<uint8_t>(this->gyro_range_);
84
85 // Read one contiguous sample:
86 // 0..5 accelerometer X/Y/Z
87 // 6..7 temperature
88 // 8..13 gyroscope X/Y/Z
89 uint8_t raw_data[14] = {};
90 this->read_registers_(ICM20689_REG_ACCEL_XOUT_H, raw_data, sizeof(raw_data));
91
92 const float accel_factor = accel_scale(accel_range);
93
94 data.acceleration[motion::X_AXIS] = encode_value<int16_t>(raw_data) * accel_factor;
95 data.acceleration[motion::Y_AXIS] = encode_value<int16_t>(&raw_data[2]) * accel_factor;
96 data.acceleration[motion::Z_AXIS] = encode_value<int16_t>(&raw_data[4]) * accel_factor;
97
98 const float gyro_factor = gyro_scale(gyro_range);
99
100 data.angular_rate[motion::X_AXIS] = encode_value<int16_t>(&raw_data[8]) * gyro_factor;
101 data.angular_rate[motion::Y_AXIS] = encode_value<int16_t>(&raw_data[10]) * gyro_factor;
102 data.angular_rate[motion::Z_AXIS] = encode_value<int16_t>(&raw_data[12]) * gyro_factor;
103
104#ifdef USE_MOTION_TEMPERATURE_PUBLISHER
105 if (this->temperature_publisher_ != nullptr) {
106 // Datasheet formula: temperature (°C) = raw / 326.8 + 25.
107 const float temperature = encode_value<int16_t>(&raw_data[6]) / 326.8f + 25.0f;
108 this->temperature_publisher_(temperature);
109 }
110#endif
111
112 return true;
113}
114
115} // namespace esphome::icm20689
void mark_failed()
Mark this component as failed.
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
void write_register_(uint8_t reg, uint8_t value)
Definition icm20689.cpp:21
bool update_data(motion::MotionData &data) override
Definition icm20689.cpp:78
void read_registers_(uint8_t reg, uint8_t *data, size_t len)
Definition icm20689.cpp:23
ICM20689AccelRange accel_range_
Definition icm20689.h:56
Range range
Definition msa3xx.h:0
ESPHOME_LOG_TAG(TAG, "icm20689")
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:1248
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint16_t temperature
Definition sun_gtil2.cpp:12