From 4b232a413863ca58ebe8a123e68a78031004a464 Mon Sep 17 00:00:00 2001 From: Tristan Honscheid Date: Thu, 4 Nov 2021 13:21:57 -0600 Subject: zephyr: bmi160: Test offset and calib on unsupported sensor types Try running perform_calib() and set_offset() on sensor types that don't support it. BRANCH=None BUG=b:184856157 TEST=zmake -D configure --test test-drivers; make runhosttests Signed-off-by: Tristan Honscheid Change-Id: I978dfc87bd2915c4da2ccd58d2da9f3f597164d5 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3262105 Reviewed-by: Aaron Massey --- driver/accelgyro_bmi160.c | 13 ++++++++++--- zephyr/test/drivers/src/bmi160.c | 42 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index f92f61d181..9983f4648c 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -247,6 +247,11 @@ static int perform_calib(struct motion_sensor_t *s, int enable) if (!enable) return EC_SUCCESS; + /* We only support accelerometers and gyroscopes */ + if (s->type != MOTIONSENSE_TYPE_ACCEL && + s->type != MOTIONSENSE_TYPE_GYRO) + return EC_RES_INVALID_PARAM; + rate = bmi_get_data_rate(s); /* * Temporary set frequency to 100Hz to get enough data in a short @@ -287,10 +292,12 @@ static int perform_calib(struct motion_sensor_t *s, int enable) /* Timeout for gyroscope calibration */ timeout.val = 800 * MSEC; break; + /* LCOV_EXCL_START */ default: - /* Not supported on Magnetometer */ - ret = EC_RES_INVALID_PARAM; - goto end_perform_calib; + /* Unreachable due to sensor type check above. */ + ASSERT(false); + return EC_RES_INVALID_PARAM; + /* LCOV_EXCL_STOP */ } ret = bmi_write8(s->port, s->i2c_spi_addr_flags, BMI160_FOC_CONF, val); diff --git a/zephyr/test/drivers/src/bmi160.c b/zephyr/test/drivers/src/bmi160.c index 68971028d9..3f45b3c5b4 100644 --- a/zephyr/test/drivers/src/bmi160.c +++ b/zephyr/test/drivers/src/bmi160.c @@ -1914,6 +1914,43 @@ static void test_bmi_sec_raw_write8(void) actual_reg_addr, requested_reg_addr); } +/** Test setting an offset on an invalid sensor type */ +static void test_bmi_set_offset_invalid_type(void) +{ + struct motion_sensor_t ms_fake; + int ret; + + int16_t unused_offset; + int16_t temp = 0; + + /* make a copy of the accel motion sensor so we modify its type */ + memcpy(&ms_fake, &motion_sensors[BMI_ACC_SENSOR_ID], sizeof(ms_fake)); + ms_fake.type = MOTIONSENSE_TYPE_MAX; + + ret = ms_fake.drv->set_offset(&ms_fake, &unused_offset, temp); + + zassert_equal(ret, EC_RES_INVALID_PARAM, + "Expected return code of %d but got %d", + EC_RES_INVALID_PARAM, ret); +} + +/** Test performing a calibration on a magnetometer, which is not supported */ +static void test_bmi_perform_calib_invalid_type(void) +{ + struct motion_sensor_t ms_fake; + int ret; + + /* make a copy of the accel motion sensor so we modify its type */ + memcpy(&ms_fake, &motion_sensors[BMI_ACC_SENSOR_ID], sizeof(ms_fake)); + ms_fake.type = MOTIONSENSE_TYPE_MAG; + + ret = ms_fake.drv->perform_calib(&ms_fake, 1); + + zassert_equal(ret, EC_RES_INVALID_PARAM, + "Expected return code of %d but got %d", + EC_RES_INVALID_PARAM, ret); +} + void test_suite_bmi160(void) { ztest_test_suite(bmi160, @@ -1936,6 +1973,9 @@ void test_suite_bmi160(void) ztest_user_unit_test(test_bmi_acc_fifo), ztest_user_unit_test(test_bmi_gyr_fifo), ztest_user_unit_test(test_bmi_sec_raw_read8), - ztest_user_unit_test(test_bmi_sec_raw_write8)); + ztest_user_unit_test(test_bmi_sec_raw_write8), + ztest_user_unit_test(test_bmi_set_offset_invalid_type), + ztest_user_unit_test( + test_bmi_perform_calib_invalid_type)); ztest_run_test_suite(bmi160); } -- cgit v1.2.1