tests: drivers: sensor: Add an BMI160 accelerometer test

Using the SPI emulator we can create a simple test for the BMI160
driver. Add this an enable it for native_posix.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2020-09-10 13:49:05 -06:00 committed by Anas Nashif
commit 6f0c8f0d2c
8 changed files with 108 additions and 3 deletions

View file

@ -25,6 +25,7 @@
aliases {
eeprom-0 = &eeprom0;
i2c-0 = &i2c0;
spi-0 = &spi0;
};
flashcontroller0: flash-controller@0 {
@ -91,6 +92,16 @@
label = "I2C_0";
};
spi0: spi@200 {
status = "okay";
compatible = "zephyr,spi-emul-controller";
clock-frequency = <50000000>;
#address-cells = <1>;
#size-cells = <0>;
reg = <0x200 4>;
label = "SPI_0";
};
uart0: uart {
status = "okay";
compatible = "zephyr,native-posix-uart";

View file

@ -3,6 +3,4 @@ tests:
depends_on: i2c
tags: drivers i2c
harness: sensor
filter: dt_alias_exists("i2c-0") or
dt_alias_exists("i2c-1") or
dt_alias_exists("i2c-2")
filter: dt_alias_exists("accel-0")

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(device)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,10 @@
# Copyright (c) 2020 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
CONFIG_EMUL=y
CONFIG_SPI=y
CONFIG_SPI_EMUL=y
CONFIG_BMI160=y
CONFIG_EMUL_BMI160=y
CONFIG_SENSOR=y
CONFIG_BMI160_TRIGGER_NONE=y

View file

@ -0,0 +1,18 @@
/* Copyright (c) 2020 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
/ {
aliases {
accel-0 = &bmi;
};
};
&spi0 {
bmi: bmi@3 {
compatible = "bosch,bmi160";
spi-max-frequency = <50000000>;
reg = <3>;
label = "accel";
};
};

View file

@ -0,0 +1 @@
CONFIG_ZTEST=y

View file

@ -0,0 +1,55 @@
/*
* Copyright 2020 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @defgroup driver_sensor_subsys_tests sensor_subsys
* @ingroup all_tests
* @{
* @}
*/
#include <ztest.h>
#include <drivers/sensor.h>
#define ACCEL_LABEL DT_LABEL(DT_ALIAS(accel_0))
static enum sensor_channel channel[] = {
SENSOR_CHAN_ACCEL_X,
SENSOR_CHAN_ACCEL_Y,
SENSOR_CHAN_ACCEL_Z,
SENSOR_CHAN_GYRO_X,
SENSOR_CHAN_GYRO_Y,
SENSOR_CHAN_GYRO_Z,
};
void test_sensor_accel_basic(void)
{
const struct device *dev;
dev = device_get_binding(ACCEL_LABEL);
zassert_not_null(dev, "failed: dev '%s' is null", ACCEL_LABEL);
zassert_equal(sensor_sample_fetch(dev), 0, "fail to fetch sample");
for (int i = 0; i < ARRAY_SIZE(channel); i++) {
struct sensor_value val;
zassert_ok(sensor_channel_get(dev, channel[i], &val),
"fail to get channel");
zassert_equal(i, val.val1, "expected %d, got %d", i, val.val1);
zassert_true(val.val2 < 1000, "error %d is too large",
val.val2);
}
}
/* test case main entry */
void test_main(void)
{
ztest_test_suite(test_sensor_accel,
ztest_user_unit_test(test_sensor_accel_basic));
ztest_run_test_suite(test_sensor_accel);
}

View file

@ -0,0 +1,4 @@
tests:
driver.sensor:
tags: driver sensor subsys
platform_allow: native_posix