Test the WHO_AM_I table against the datasheet values

This commit is contained in:
J. Nick Koston
2026-10-13 22:15:40 -10:00
parent e1b6e5dc6e
commit 94c4e77163
4 changed files with 55 additions and 3 deletions
+6 -3
View File
@@ -27,7 +27,7 @@ static const LogString *gyro_range_to_str(LSM6DSGyroRange range) {
}
}
static const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
switch (who_am_i) {
case LSM6DS_WHO_AM_I_LSM6DS3:
return LOG_STR("LSM6DS3");
@@ -40,6 +40,10 @@ static const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
}
}
uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i) {
return who_am_i == LSM6DS_WHO_AM_I_LSM6DS3 ? 16 : 256;
}
void LSM6DSComponent::setup() {
MotionComponent::setup();
uint8_t who_am_i = 0;
@@ -56,8 +60,7 @@ void LSM6DSComponent::setup() {
}
ESP_LOGD(TAG, "Found %s (WHO_AM_I = 0x%02X)", LOG_STR_ARG(chip_name), who_am_i);
this->chip_name_ = chip_name;
if (who_am_i == LSM6DS_WHO_AM_I_LSM6DS3)
this->temperature_sensitivity_ = 16;
this->temperature_sensitivity_ = temperature_sensitivity_from_who_am_i(who_am_i);
// 2. Software reset — clears all registers to defaults
if (this->write_register(LSM6DS_REG_CTRL3_C, &CTRL3_C_SW_RESET, 1) != i2c::ERROR_OK) {
+5
View File
@@ -87,6 +87,11 @@ enum LSM6DSGyroODR : uint8_t {
};
// ── Main component class ─────────────────────────────────────────────────────
/// Chip name for a WHO_AM_I value, or nullptr when the chip is not supported.
const LogString *chip_name_from_who_am_i(uint8_t who_am_i);
/// Temperature sensitivity in LSB/°C for a supported WHO_AM_I value, from the chip's datasheet.
uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i);
class LSM6DSComponent final : public motion::MotionComponent, public i2c::I2CDevice {
public:
void setup() override;
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# lsm6ds declares i2c on its motion platform, which the harness does not walk
manifest.dependencies = manifest.dependencies + ["i2c", "motion"]
+38
View File
@@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <cstring>
#include "esphome/components/lsm6ds/lsm6ds.h"
namespace esphome::lsm6ds::testing {
// WHO_AM_I values and temperature sensitivities from ST's datasheets:
// LSM6DS3 DocID026899, LSM6DS3TR-C DocID030071, LSM6DSO DS12140
static const char *name_of(uint8_t who_am_i) {
const LogString *name = chip_name_from_who_am_i(who_am_i);
return name == nullptr ? nullptr : LOG_STR_ARG(name);
}
TEST(LSM6DSChipIds, Lsm6ds3) {
EXPECT_STREQ(name_of(0x69), "LSM6DS3");
EXPECT_EQ(temperature_sensitivity_from_who_am_i(0x69), 16);
}
TEST(LSM6DSChipIds, Lsm6ds3trC) {
EXPECT_STREQ(name_of(0x6A), "LSM6DS3TR-C");
EXPECT_EQ(temperature_sensitivity_from_who_am_i(0x6A), 256);
}
TEST(LSM6DSChipIds, Lsm6dso) {
EXPECT_STREQ(name_of(0x6C), "LSM6DSO");
EXPECT_EQ(temperature_sensitivity_from_who_am_i(0x6C), 256);
}
TEST(LSM6DSChipIds, UnknownIsRejected) {
EXPECT_EQ(name_of(0x00), nullptr);
EXPECT_EQ(name_of(0x6B), nullptr);
EXPECT_EQ(name_of(0xFF), nullptr);
}
} // namespace esphome::lsm6ds::testing