From ac67c629e211278567a63f8d3400652139b856c1 Mon Sep 17 00:00:00 2001 From: Ting Shen Date: Tue, 2 May 2023 15:04:21 +0800 Subject: zephyr: implement icm42607 test Add unittest for ICM42607 accel+gyro sensor. Note that we've already hit the hard limit of motion sensor count in zephyr/test/drivers/default/, so this one is implemented as a standalone test. Also fixed an endian bug in driver/accelgyro_icm42607.c. BUG=b:272665228 TEST=twister Change-Id: Ia52a81e671a981fc0f003d07c7f59ca3d0c24233 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4497324 Tested-by: Ting Shen Reviewed-by: Eric Yilun Lin Commit-Queue: Ting Shen --- driver/accelgyro_icm42607.c | 4 + zephyr/test/drivers/CMakeLists.txt | 1 + zephyr/test/drivers/Kconfig | 3 + zephyr/test/drivers/icm42607/CMakeLists.txt | 6 + zephyr/test/drivers/icm42607/prj.conf | 6 + zephyr/test/drivers/icm42607/sensor.dts | 74 +++++ zephyr/test/drivers/icm42607/src/icm42607.c | 466 ++++++++++++++++++++++++++++ zephyr/test/drivers/testcase.yaml | 9 + 8 files changed, 569 insertions(+) create mode 100644 zephyr/test/drivers/icm42607/CMakeLists.txt create mode 100644 zephyr/test/drivers/icm42607/prj.conf create mode 100644 zephyr/test/drivers/icm42607/sensor.dts create mode 100644 zephyr/test/drivers/icm42607/src/icm42607.c diff --git a/driver/accelgyro_icm42607.c b/driver/accelgyro_icm42607.c index 48518fa315..07582ba88e 100644 --- a/driver/accelgyro_icm42607.c +++ b/driver/accelgyro_icm42607.c @@ -9,6 +9,7 @@ */ #include "accelgyro.h" +#include "builtin/endian.h" #include "console.h" #include "driver/accelgyro_icm42607.h" #include "driver/accelgyro_icm_common.h" @@ -960,6 +961,9 @@ static int icm42607_read_temp(const struct motion_sensor_t *s, int *temp_ptr) if (ret != EC_SUCCESS) return ret; + /* This register is big-endian and not configurable */ + val = be16toh(val); + /* ensure correct propagation of 16 bits sign bit */ val = sign_extend(val, 15); diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt index 417d96bf36..b995d9ff95 100644 --- a/zephyr/test/drivers/CMakeLists.txt +++ b/zephyr/test/drivers/CMakeLists.txt @@ -26,6 +26,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_COMMON_CBI_GPIO common_cbi_gpio) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_COMMON_CHARGER common_charger) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_CHARGESPLASH chargesplash) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_FLASH flash) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ICM42607 icm42607) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ISL923X isl923x) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ISL9241 isl9241) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_I2C_CONTROLLER i2c_controller) diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig index c3aaac274b..0f846c003f 100644 --- a/zephyr/test/drivers/Kconfig +++ b/zephyr/test/drivers/Kconfig @@ -65,6 +65,9 @@ config LINK_TEST_SUITE_HOST_COMMANDS config LINK_TEST_SUITE_HOST_COMMAND_READ_MEMMAP bool "Link and test the host command read memmap tests" +config LINK_TEST_SUITE_ICM42607 + bool "Link and test the icm42607 tests" + config LINK_TEST_SUITE_ISL923X bool "Link and test the isl923x tests" diff --git a/zephyr/test/drivers/icm42607/CMakeLists.txt b/zephyr/test/drivers/icm42607/CMakeLists.txt new file mode 100644 index 0000000000..6b4b8a083b --- /dev/null +++ b/zephyr/test/drivers/icm42607/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2023 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Add source files +target_sources(app PRIVATE src/icm42607.c) diff --git a/zephyr/test/drivers/icm42607/prj.conf b/zephyr/test/drivers/icm42607/prj.conf new file mode 100644 index 0000000000..16c1ae7c03 --- /dev/null +++ b/zephyr/test/drivers/icm42607/prj.conf @@ -0,0 +1,6 @@ +# Copyright 2023 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_PLATFORM_EC_ACCELGYRO_ICM42607=y +CONFIG_PLATFORM_EC_ACCELGYRO_ICM_COMM_I2C=y diff --git a/zephyr/test/drivers/icm42607/sensor.dts b/zephyr/test/drivers/icm42607/sensor.dts new file mode 100644 index 0000000000..8734ce001a --- /dev/null +++ b/zephyr/test/drivers/icm42607/sensor.dts @@ -0,0 +1,74 @@ +/ { + aliases { + icm42607-int = &ms_icm42607_accel; + }; + + motionsense-mutex { + mutex_icm42607: icm42607-mutex { + }; + }; + + motionsense-sensor-data { + icm42607_data: icm42607-drv-data { + compatible = "cros-ec,drvdata-icm42607"; + status = "okay"; + }; + }; + + motionsense-sensor { + ms_icm42607_accel: ms-icm42607-accel { + compatible = "cros-ec,icm42607-accel"; + status = "okay"; + + location = "MOTIONSENSE_LOC_BASE"; + mutex = <&mutex_icm42607>; + port = <&named_i2c1>; + drv-data = <&icm42607_data>; + i2c-spi-addr-flags = "ICM42607_ADDR1_FLAGS"; + active-mask = "SENSOR_ACTIVE_S0_S3_S5"; + configs { + compatible = + "cros-ec,motionsense-sensor-config"; + ec-s0 { + odr = <10000>; + }; + ec-s3 { + odr = <10000>; + }; + ec-s5 { + odr = <10000>; + }; + }; + }; + + ms_icm42607_gyro: ms-icm42607-gryo { + compatible = "cros-ec,icm42607-gyro"; + status = "okay"; + + location = "MOTIONSENSE_LOC_BASE"; + mutex = <&mutex_icm42607>; + port = <&named_i2c1>; + drv-data = <&icm42607_data>; + i2c-spi-addr-flags = "ICM42607_ADDR1_FLAGS"; + active-mask = "SENSOR_ACTIVE_S0_S3_S5"; + }; + }; +}; + +&i2c1 { + icm42607_emul: icm42607@69 { + compatible = "zephyr,icm42607-emul"; + status = "okay"; + reg = <0x69>; + }; +}; + +/* motionsense framework supports at most 8 sensors, remove two so we can add + * our test target. + */ +&ms_bmi160_accel { + status = "disabled"; +}; +&ms_bmi160_gyro { + status = "disabled"; +}; diff --git a/zephyr/test/drivers/icm42607/src/icm42607.c b/zephyr/test/drivers/icm42607/src/icm42607.c new file mode 100644 index 0000000000..20d2736063 --- /dev/null +++ b/zephyr/test/drivers/icm42607/src/icm42607.c @@ -0,0 +1,466 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include "accelgyro.h" +#include "driver/accelgyro_icm42607.h" +#include "driver/accelgyro_icm_common.h" +#include "emul/emul_icm42607.h" +#include "motion_sense.h" +#include "motion_sense_fifo.h" +#include "test/drivers/test_state.h" + +#include +#include + +#define ICM42607_NODE DT_NODELABEL(icm42607_emul) +#define ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_icm42607_accel)) +#define GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_icm42607_gyro)) + +static const struct emul *emul = EMUL_DT_GET(ICM42607_NODE); +static struct motion_sensor_t *acc = &motion_sensors[ACC_SENSOR_ID]; +static struct motion_sensor_t *gyr = &motion_sensors[GYR_SENSOR_ID]; + +static void icm42607_set_temp(uint16_t val) +{ + icm42607_emul_write_reg(emul, ICM42607_REG_TEMP_DATA, val >> 8); + icm42607_emul_write_reg(emul, ICM42607_REG_TEMP_DATA + 1, val & 0xFF); +} + +static bool check_sensor_enabled(enum motionsensor_type type) +{ + int reg = icm42607_emul_peek_reg(emul, ICM42607_REG_PWR_MGMT0); + + if (type == MOTIONSENSE_TYPE_ACCEL) { + int mode = reg & 3; + + return mode == 2; + } else if (type == MOTIONSENSE_TYPE_GYRO) { + int mode = (reg >> 2) & 3; + + return mode == 3; + } + + return false; +} + +static void icm42607_push_packet(const int16_t *acc, const int16_t *gyr) +{ + uint8_t buf[16]; + int packet_size = 0; + + if (acc && gyr) { + buf[0] = BIT(6) | BIT(5); /* acc + gyr */ + memcpy(buf + 1, acc, 6); + memcpy(buf + 7, gyr, 6); + packet_size = 16; + } else if (acc) { + buf[0] = BIT(6); /* acc */ + memcpy(buf + 1, acc, 6); + packet_size = 8; + } else if (gyr) { + buf[0] = BIT(5); /* gyr */ + memcpy(buf + 1, gyr, 6); + packet_size = 8; + } + + icm42607_emul_write_reg(emul, ICM42607_REG_INT_STATUS, BIT(2)); + icm42607_emul_push_fifo(emul, buf, packet_size); +} + +static int motion_sense_fifo_pop(int *sensor_num, int16_t *data) +{ + struct ec_response_motion_sensor_data resp = {}; + uint16_t resp_size; + + while (motion_sense_fifo_read(sizeof(resp), 1, &resp, &resp_size)) { + if (resp.flags & MOTIONSENSE_SENSOR_FLAG_TIMESTAMP) { + continue; + } + + *sensor_num = resp.sensor_num; + memcpy(data, resp.data, sizeof(resp.data)); + + return 0; + } + + return -1; +} + +static void test_fifo(const int16_t *acc_expected, const int16_t *gyr_expected) +{ + int sensor_num = -1; + int16_t data[3] = {}; + + motion_sense_fifo_reset(); + acc->oversampling_ratio = 1; + gyr->oversampling_ratio = 1; + + icm42607_push_packet(acc_expected, gyr_expected); + icm42607_interrupt(0); + k_sleep(K_SECONDS(1)); + + if (acc_expected) { + zassert_equal(motion_sense_fifo_pop(&sensor_num, data), 0); + zassert_equal(sensor_num, ACC_SENSOR_ID); + zassert_equal(acc_expected[0], data[0]); + zassert_equal(acc_expected[1], data[1]); + zassert_equal(acc_expected[2], data[2]); + } + + if (gyr_expected) { + zassert_equal(motion_sense_fifo_pop(&sensor_num, data), 0); + zassert_equal(sensor_num, GYR_SENSOR_ID); + zassert_equal(gyr_expected[0], data[0]); + zassert_equal(gyr_expected[1], data[1]); + zassert_equal(gyr_expected[2], data[2]); + } +} + +/* verify that icm42607 driver returns error when too many data in fifo */ +ZTEST_USER(icm42607, test_fifo_full) +{ + const uint8_t junk[512] = {}; + int sensor_num = -1; + int16_t data[3] = {}; + + icm42607_push_packet((int16_t[]){ -32768, -32768, -32768 }, NULL); + icm42607_emul_push_fifo(emul, junk, sizeof(junk)); + icm42607_interrupt(0); + k_sleep(K_SECONDS(1)); + + zassert_not_equal(motion_sense_fifo_pop(&sensor_num, data), 0); +} + +ZTEST_USER(icm42607, test_invalid_packet) +{ + const uint8_t junk[16] = { + 0x80, + }; /* bad packet header */ + int sensor_num = -1; + int16_t data[3] = {}; + + icm42607_emul_push_fifo(emul, junk, sizeof(junk)); + icm42607_interrupt(0); + k_sleep(K_SECONDS(1)); + + zassert_not_equal(motion_sense_fifo_pop(&sensor_num, data), 0); +} + +/* verify that icm42607 driver doesn't send bad data to motionsense fifo */ +ZTEST_USER(icm42607, test_invalid_sensor_data) +{ + int sensor_num = -1; + int16_t data[3] = {}; + + icm42607_push_packet((int16_t[]){ -32768, -32768, -32768 }, NULL); + icm42607_interrupt(0); + k_sleep(K_SECONDS(1)); + + zassert_not_equal(motion_sense_fifo_pop(&sensor_num, data), 0); +} + +ZTEST_USER(icm42607, test_fifo_read) +{ + /* 2 sensor packet */ + test_fifo((int16_t[]){ 1111, 2222, 3333 }, + (int16_t[]){ 4444, 5555, 6666 }); + + /* acc only */ + test_fifo((int16_t[]){ 1111, 2222, 3333 }, NULL); + + /* gyr only */ + test_fifo(NULL, (int16_t[]){ 4444, 5555, 6666 }); +} + +ZTEST_USER(icm42607, test_resolution) +{ + zassert_equal(acc->drv->get_resolution(acc), 16); +} + +/* verify that set_data_rate enables or disables the sensor */ +ZTEST_USER(icm42607, test_sensor_enable) +{ + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_ACCEL)); + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_GYRO)); + + zassert_ok(acc->drv->set_data_rate(acc, 12500, 1)); + zassert_true(check_sensor_enabled(MOTIONSENSE_TYPE_ACCEL)); + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_GYRO)); + + zassert_ok(acc->drv->set_data_rate(gyr, 12500, 1)); + zassert_true(check_sensor_enabled(MOTIONSENSE_TYPE_ACCEL)); + zassert_true(check_sensor_enabled(MOTIONSENSE_TYPE_GYRO)); + + zassert_ok(acc->drv->set_data_rate(gyr, 0, 1)); + zassert_true(check_sensor_enabled(MOTIONSENSE_TYPE_ACCEL)); + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_GYRO)); + + zassert_ok(acc->drv->set_data_rate(acc, 0, 1)); + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_ACCEL)); + zassert_false(check_sensor_enabled(MOTIONSENSE_TYPE_GYRO)); +} + +ZTEST_USER(icm42607, test_data_rate) +{ + zassert_ok(acc->drv->set_data_rate(acc, 12500, 1)); + zassert_equal(acc->drv->get_data_rate(acc), 12500); + zassert_equal(icm42607_emul_peek_reg(emul, ICM42607_REG_ACCEL_CONFIG0) & + 0xF, + 0xC); + + /* 24Hz should round up to 25Hz */ + zassert_ok(gyr->drv->set_data_rate(gyr, 24000, 1)); + zassert_equal(gyr->drv->get_data_rate(gyr), 25000); + zassert_equal(icm42607_emul_peek_reg(emul, ICM42607_REG_GYRO_CONFIG0) & + 0xF, + 0xB); + + /* return error if data rate is out of supported range */ + zassert_not_equal(gyr->drv->set_data_rate(gyr, 6250, 0), 0); + zassert_not_equal(acc->drv->set_data_rate(acc, 1600000, 0), 0); +} + +ZTEST_USER(icm42607, test_offset_out_of_range) +{ + int16_t acc_offset[3]; + int16_t acc_offset_input[3] = { 10000, -10000, 0 }; + int16_t acc_temp; + + zassert_ok(acc->drv->init(acc)); + + zassert_ok(acc->drv->set_offset(acc, acc_offset_input, 40)); + zassert_ok(acc->drv->get_offset(acc, acc_offset, &acc_temp)); + + /* icm42607 internally stores offset in 12bit signed integer, + * input is clamped into range [2047, -2048], and then scaled to + * ec unit, so the result is [1023.5, -1024] => [1024, -1024] + */ + zassert_equal(acc_offset[0], 1024); + zassert_equal(acc_offset[1], -1024, "got %d", acc_offset[1]); + zassert_equal(acc_offset[2], 0); + zassert_equal(acc_temp, EC_MOTION_SENSE_INVALID_CALIB_TEMP); +} + +ZTEST_USER(icm42607, test_offset) +{ + int16_t acc_offset[3], gyr_offset[3]; + /* use multiplies of 32 to avoid rounding error */ + int16_t acc_offset_expected[3] = { 32, 32 * 2, 32 * 3 }; + int16_t gyr_offset_expected[3] = { 32 * 4, 32 * 5, 32 * 6 }; + int16_t acc_temp, gyr_temp; + + zassert_ok(acc->drv->init(acc)); + zassert_ok(gyr->drv->init(gyr)); + + zassert_ok(acc->drv->set_offset(acc, acc_offset_expected, 40)); + zassert_ok(gyr->drv->set_offset(gyr, gyr_offset_expected, 80)); + zassert_ok(acc->drv->get_offset(acc, acc_offset, &acc_temp)); + zassert_ok(gyr->drv->get_offset(gyr, gyr_offset, &gyr_temp)); + + zassert_equal(acc_offset[0], acc_offset_expected[0]); + zassert_equal(acc_offset[1], acc_offset_expected[1]); + zassert_equal(acc_offset[2], acc_offset_expected[2]); + zassert_equal(acc_temp, EC_MOTION_SENSE_INVALID_CALIB_TEMP); + zassert_equal(gyr_offset[0], gyr_offset_expected[0]); + zassert_equal(gyr_offset[1], gyr_offset_expected[1]); + zassert_equal(gyr_offset[2], gyr_offset_expected[2]); + zassert_equal(gyr_temp, EC_MOTION_SENSE_INVALID_CALIB_TEMP); +} + +ZTEST_USER(icm42607, test_scale) +{ + uint16_t acc_scale[3], gyr_scale[3]; + int16_t acc_temp, gyr_temp; + + zassert_ok(acc->drv->init(acc)); + zassert_ok(gyr->drv->init(gyr)); + + zassert_ok(acc->drv->set_scale(acc, (uint16_t[3]){ 1, 2, 3 }, 4)); + zassert_ok(gyr->drv->set_scale(gyr, (uint16_t[3]){ 5, 6, 7 }, 8)); + zassert_ok(gyr->drv->get_scale(acc, acc_scale, &acc_temp)); + zassert_ok(gyr->drv->get_scale(gyr, gyr_scale, &gyr_temp)); + + zassert_equal(acc_scale[0], 1); + zassert_equal(acc_scale[1], 2); + zassert_equal(acc_scale[2], 3); + zassert_equal(acc_temp, EC_MOTION_SENSE_INVALID_CALIB_TEMP); + zassert_equal(gyr_scale[0], 5); + zassert_equal(gyr_scale[1], 6); + zassert_equal(gyr_scale[2], 7); + zassert_equal(gyr_temp, EC_MOTION_SENSE_INVALID_CALIB_TEMP); +} + +ZTEST_USER(icm42607, test_invalid_read) +{ + intv3_t v; + + zassert_ok(acc->drv->init(acc)); + zassert_ok(gyr->drv->init(gyr)); + + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ, 0x00); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 1, 0x80); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 2, 0x00); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 3, 0x80); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 4, 0x00); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 5, 0x80); + + /* return cached value (raw_xyz) on invalid data */ + zassert_ok(acc->drv->read(acc, v)); + zassert_equal(v[0], 0); + zassert_equal(v[1], 0); + zassert_equal(v[2], 0); +} + +/* verify that read() works correctly, and scale is applied */ +ZTEST_USER(icm42607, test_read) +{ + intv3_t v; + const uint16_t scale[3] = { 16384, 16384, 16384 }; /* 0.5x scale */ + + zassert_ok(acc->drv->init(acc)); + zassert_ok(gyr->drv->init(gyr)); + + /* verify that sensor data format is configured to little endian */ + int intf_config0 = + icm42607_emul_peek_reg(emul, ICM42607_REG_INTF_CONFIG0); + zassert_equal(intf_config0 & ICM42607_SENSOR_DATA_ENDIAN, 0); + zassert_ok(acc->drv->set_scale(acc, scale, 0)); + + /* test accel read, 16bit LE */ + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ, 0x01); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 1, 0x02); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 2, 0x03); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 3, 0x04); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 4, 0x05); + icm42607_emul_write_reg(emul, ICM42607_REG_ACCEL_DATA_XYZ + 5, 0x06); + + zassert_ok(acc->drv->read(acc, v)); + zassert_equal(v[0], 0x0201 / 2); + zassert_equal(v[1], 0x0403 / 2); + zassert_equal(v[2], 0x0605 / 2); + + /* test gyro read, 16bit LE */ + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ, 0x0A); + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ + 1, 0x0B); + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ + 2, 0x0C); + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ + 3, 0x0D); + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ + 4, 0x0E); + icm42607_emul_write_reg(emul, ICM42607_REG_GYRO_DATA_XYZ + 5, 0x0F); + + zassert_ok(gyr->drv->read(gyr, v)); + zassert_equal(v[0], 0x0B0A); + zassert_equal(v[1], 0x0D0C); + zassert_equal(v[2], 0x0F0E); +} + +/* read() immediately after sensor enabled should fail */ +ZTEST_USER(icm42607, test_read_not_stabilized) +{ + intv3_t v; + + zassert_ok(acc->drv->set_data_rate(acc, 0, 1)); + zassert_ok(acc->drv->set_data_rate(acc, 10000, 1)); + zassert_not_equal(acc->drv->read(acc, v), 0); + + sleep(1); + zassert_equal(acc->drv->read(acc, v), 0); +} + +ZTEST_USER(icm42607, test_set_range) +{ + int reg_val; + + /* set 5G, round down to 4G, expect reg val = 2 */ + zassert_ok(acc->drv->set_range(acc, 5, 0)); + reg_val = (icm42607_emul_peek_reg(emul, ICM42607_REG_ACCEL_CONFIG0) >> + 5) & + 3; + zassert_equal(reg_val, 2); + + /* set 5G, round up to 8G, expect reg val = 1 */ + zassert_ok(acc->drv->set_range(acc, 5, 1)); + reg_val = (icm42607_emul_peek_reg(emul, ICM42607_REG_ACCEL_CONFIG0) >> + 5) & + 3; + zassert_equal(reg_val, 1); + + /* set 1500dps, round down to 1000dps, expect reg val = 1 */ + zassert_ok(gyr->drv->set_range(gyr, 1500, 0)); + reg_val = + (icm42607_emul_peek_reg(emul, ICM42607_REG_GYRO_CONFIG0) >> 5) & + 3; + zassert_equal(reg_val, 1); + + /* set 1500dps, round down to 2000dps, expect reg val = 0 */ + zassert_ok(gyr->drv->set_range(gyr, 1500, 1)); + reg_val = + (icm42607_emul_peek_reg(emul, ICM42607_REG_GYRO_CONFIG0) >> 5) & + 3; + zassert_equal(reg_val, 0); +} + +/* Verify the temperature matches following formula: + * Temperature in C = (REG_DATA / 128) + 25 + */ +ZTEST_USER(icm42607, test_read_temp) +{ + int temp; + + /* expect 0C = 273.15K */ + icm42607_set_temp(-25 * 128); + zassert_ok(acc->drv->read_temp(acc, &temp)); + zassert_equal(temp, 273); + + /* expect 100C = 373.15K */ + icm42607_set_temp(75 * 128); + zassert_ok(acc->drv->read_temp(acc, &temp)); + zassert_equal(temp, 373); + + /* expect 25C = 298K */ + icm42607_set_temp(0); + zassert_ok(acc->drv->read_temp(acc, &temp)); + zassert_equal(temp, 298); + + /* reset value */ + icm42607_set_temp(-32768); + zassert_not_equal(acc->drv->read_temp(acc, &temp), 0); +} + +ZTEST_USER(icm42607, test_init) +{ + struct i2c_common_emul_data *common_data = + emul_icm42607_get_i2c_common_data(emul); + + icm42607_emul_write_reg(emul, ICM42607_REG_WHO_AM_I, + ICM42607_CHIP_ICM42607P); + zassert_ok(acc->drv->init(acc)); + + icm42607_emul_write_reg(emul, ICM42607_REG_WHO_AM_I, 0x87); + zassert_not_equal(acc->drv->init(acc), 0); + + i2c_common_emul_set_read_fail_reg(common_data, ICM42607_REG_WHO_AM_I); + zassert_not_equal(acc->drv->init(acc), 0); +} + +static void icm42607_before(void *fixture) +{ + struct i2c_common_emul_data *common_data = + emul_icm42607_get_i2c_common_data(emul); + + ARG_UNUSED(fixture); + + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + icm42607_emul_reset(emul); + icm_reset_stabilize_ts(acc); + icm_reset_stabilize_ts(gyr); + memset(acc->raw_xyz, 0, sizeof(intv3_t)); + memset(gyr->raw_xyz, 0, sizeof(intv3_t)); + motion_sense_fifo_reset(); + acc->oversampling_ratio = 1; + gyr->oversampling_ratio = 1; +} + +ZTEST_SUITE(icm42607, drivers_predicate_post_main, NULL, icm42607_before, NULL, + NULL); diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml index 695a2bcc66..9e182db74c 100644 --- a/zephyr/test/drivers/testcase.yaml +++ b/zephyr/test/drivers/testcase.yaml @@ -202,6 +202,15 @@ tests: - CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI=n - CONFIG_PLATFORM_EC_HOST_INTERFACE_SHI=y - CONFIG_PLATFORM_EC_SWITCH=n + drivers.icm42607: + extra_configs: + - CONFIG_PLATFORM_EC_ACCELGYRO_BMI160=n + - CONFIG_LINK_TEST_SUITE_ICM42607=y + - CONFIG_PLATFORM_EC_ACCELGYRO_ICM42607=y + - CONFIG_PLATFORM_EC_ACCELGYRO_ICM_COMM_I2C=y + extra_dtc_overlay_files: + - ./boards/native_posix.overlay + - ./icm42607/sensor.dts drivers.isl923x: extra_configs: - CONFIG_LINK_TEST_SUITE_ISL923X=y -- cgit v1.2.1