From cedeebec5935a352edd5aa1b985d0066ebb41e4b Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Fri, 24 Feb 2023 11:57:25 -0700 Subject: [PATCH] sensor: add unit tests for icm42688 interrupt Add a test that verifies that when the INPUT GPIO fires, we get the trigger callback. Signed-off-by: Yuval Peress --- .../icm42688/boards/native_posix.overlay | 1 + tests/drivers/sensor/icm42688/prj.conf | 3 ++ tests/drivers/sensor/icm42688/src/main.c | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/tests/drivers/sensor/icm42688/boards/native_posix.overlay b/tests/drivers/sensor/icm42688/boards/native_posix.overlay index 916123ce1dc..9247b6365bb 100644 --- a/tests/drivers/sensor/icm42688/boards/native_posix.overlay +++ b/tests/drivers/sensor/icm42688/boards/native_posix.overlay @@ -5,6 +5,7 @@ &spi0 { icm42688: icm42688@3 { compatible = "invensense,icm42688"; + int-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>; spi-max-frequency = <50000000>; reg = <3>; }; diff --git a/tests/drivers/sensor/icm42688/prj.conf b/tests/drivers/sensor/icm42688/prj.conf index cdcf3144034..94239648124 100644 --- a/tests/drivers/sensor/icm42688/prj.conf +++ b/tests/drivers/sensor/icm42688/prj.conf @@ -4,6 +4,9 @@ CONFIG_ZTEST=y CONFIG_ZTEST_NEW_API=y +# Enable GPIO +CONFIG_GPIO=y + # Enable sensors CONFIG_SENSOR=y diff --git a/tests/drivers/sensor/icm42688/src/main.c b/tests/drivers/sensor/icm42688/src/main.c index 6dd406867ee..a4d314dd08a 100644 --- a/tests/drivers/sensor/icm42688/src/main.c +++ b/tests/drivers/sensor/icm42688/src/main.c @@ -3,14 +3,22 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include +#include +#include #include +#include #include #include "icm42688_emul.h" #include "icm42688_reg.h" +#define NODE DT_NODELABEL(icm42688) + +DEFINE_FFF_GLOBALS; + struct icm42688_fixture { const struct device *dev; const struct emul *target; @@ -209,3 +217,26 @@ ZTEST_F(icm42688, test_fetch_gyro) test_fetch_gyro_with_range(fixture, 31250, gyro_percent); test_fetch_gyro_with_range(fixture, 15625, gyro_percent); } + +FAKE_VOID_FUNC(test_interrupt_trigger_handler, const struct device*, const struct sensor_trigger*); + +ZTEST_F(icm42688, test_interrupt) +{ + const struct gpio_dt_spec spec = GPIO_DT_SPEC_GET(NODE, int_gpios); + const struct sensor_trigger trigger = { + .type = SENSOR_TRIG_DATA_READY, + .chan = SENSOR_CHAN_ALL, + }; + + RESET_FAKE(test_interrupt_trigger_handler); + sensor_trigger_set(fixture->dev, &trigger, test_interrupt_trigger_handler); + + /* Toggle the GPIO */ + gpio_emul_input_set(spec.port, spec.pin, 0); + k_msleep(5); + gpio_emul_input_set(spec.port, spec.pin, 1); + k_msleep(5); + + /* Verify the handler was called */ + zassert_equal(test_interrupt_trigger_handler_fake.call_count, 1); +}