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 <peress@google.com>
This commit is contained in:
Yuval Peress 2023-02-24 11:57:25 -07:00 committed by Maureen Helm
commit cedeebec59
3 changed files with 35 additions and 0 deletions

View file

@ -5,6 +5,7 @@
&spi0 {
icm42688: icm42688@3 {
compatible = "invensense,icm42688";
int-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
spi-max-frequency = <50000000>;
reg = <3>;
};

View file

@ -4,6 +4,9 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
# Enable GPIO
CONFIG_GPIO=y
# Enable sensors
CONFIG_SENSOR=y

View file

@ -3,14 +3,22 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/fff.h>
#include <zephyr/ztest.h>
#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);
}