summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--driver/accelgyro_bmi160.c13
-rw-r--r--zephyr/test/drivers/src/bmi160.c42
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);
}