From 4b1de3354187f03c7e51674c8d3c6584306c221c Mon Sep 17 00:00:00 2001 From: "Najumon B.A" Date: Fri, 26 May 2023 13:33:29 +0530 Subject: [PATCH] tests: lib: devicetree: add test app for dts base init priority add test app for dts base device initialization priority Signed-off-by: Najumon B.A --- .../lib/devicetree/boot_order/CMakeLists.txt | 9 ++ tests/lib/devicetree/boot_order/README | 1 + tests/lib/devicetree/boot_order/app.overlay | 92 +++++++++++++++++++ tests/lib/devicetree/boot_order/prj.conf | 6 ++ tests/lib/devicetree/boot_order/src/main.c | 90 ++++++++++++++++++ tests/lib/devicetree/boot_order/testcase.yaml | 5 + tests/lib/devicetree/devices/testcase.yaml | 14 +-- 7 files changed, 204 insertions(+), 13 deletions(-) create mode 100644 tests/lib/devicetree/boot_order/CMakeLists.txt create mode 100644 tests/lib/devicetree/boot_order/README create mode 100644 tests/lib/devicetree/boot_order/app.overlay create mode 100644 tests/lib/devicetree/boot_order/prj.conf create mode 100644 tests/lib/devicetree/boot_order/src/main.c create mode 100644 tests/lib/devicetree/boot_order/testcase.yaml diff --git a/tests/lib/devicetree/boot_order/CMakeLists.txt b/tests/lib/devicetree/boot_order/CMakeLists.txt new file mode 100644 index 00000000000000..bf5a1867570d99 --- /dev/null +++ b/tests/lib/devicetree/boot_order/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(devicetree_devices) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/lib/devicetree/boot_order/README b/tests/lib/devicetree/boot_order/README new file mode 100644 index 00000000000000..2d3f1cfb830fb6 --- /dev/null +++ b/tests/lib/devicetree/boot_order/README @@ -0,0 +1 @@ +Test cases for the device API devicetree data. diff --git a/tests/lib/devicetree/boot_order/app.overlay b/tests/lib/devicetree/boot_order/app.overlay new file mode 100644 index 00000000000000..5aeb97e5868424 --- /dev/null +++ b/tests/lib/devicetree/boot_order/app.overlay @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + * + * Application overlay for testing the devicetree.h API. + * + * Names in this file should be chosen in a way that won't conflict + * with real-world devicetree nodes, to allow these tests to run on + * (and be extended to test) real hardware. + */ + +/ { + test { + #address-cells = <0x1>; + #size-cells = <0x1>; + + test_gpio_0: gpio@ffff { + gpio-controller; + #gpio-cells = <0x2>; + compatible = "vnd,gpio-device"; + status = "okay"; + reg = <0xffff 0x1000>; + }; + + i2c: i2c@11112222 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "vnd,i2c"; + status = "okay"; + reg = <0x11112222 0x1000>; + + test_i2c_dev_a: test-i2c-dev@10 { + compatible = "vnd,i2c-device"; + status = "okay"; + reg = <0x10>; + supply-gpios = <&test_gpio_0 1 0>; + }; + + test_i2c_dev_b: test-i2c-dev@12 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "vnd,i2c-device"; + status = "okay"; + reg = <0x12>; + }; + + test_i2c_dev_c: test-i2c-dev@13 { + compatible = "vnd,i2c-device"; + reg = <0x13>; + status = "okay"; + }; + + test-i2c-dev@14 { + compatible = "vnd,i2c-device"; + status = "okay"; + reg = <0x14>; + }; + }; + + spi: spi@44442222 { + compatible = "vnd,spi"; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x44442222 0x1000>; + status = "okay"; + + test_spi_dev_a: test-spi-dev@1 { + compatible = "vnd,spi-device"; + status = "okay"; + reg = <0x1>; + spi-max-frequency = <50000000>; + supply-gpios = <&test_gpio_0 1 0>; + }; + + test_spi_dev_b: test-spi-dev@2 { + compatible = "vnd,spi-device"; + status = "okay"; + spi-max-frequency = <50000000>; + reg = <0x2>; + }; + + test_spi_dev_c: test-spi-dev@3 { + compatible = "vnd,spi-device"; + reg = <0x3>; + spi-max-frequency = <50000000>; + status = "okay"; + }; + + }; + }; +}; diff --git a/tests/lib/devicetree/boot_order/prj.conf b/tests/lib/devicetree/boot_order/prj.conf new file mode 100644 index 00000000000000..5016b30efe4630 --- /dev/null +++ b/tests/lib/devicetree/boot_order/prj.conf @@ -0,0 +1,6 @@ +CONFIG_ZTEST=y +CONFIG_I2C=n +CONFIG_SPI=n +CONFIG_ZTEST_NEW_API=y +CONFIG_DTS_BOOT_PRIORITY=y +CONFIG_I2C_STATS=y diff --git a/tests/lib/devicetree/boot_order/src/main.c b/tests/lib/devicetree/boot_order/src/main.c new file mode 100644 index 00000000000000..9d173a88fe82dc --- /dev/null +++ b/tests/lib/devicetree/boot_order/src/main.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include + +#define TEST_GPIO DT_NODELABEL(test_gpio_0) +#define TEST_I2C DT_NODELABEL(i2c) +#define TEST_I2C_DEVA DT_NODELABEL(test_i2c_dev_a) +#define TEST_I2C_DEVB DT_NODELABEL(test_i2c_dev_b) +#define TEST_I2C_DEVC DT_NODELABEL(test_i2c_dev_c) +#define TEST_I2C_NOLABEL DT_PATH(test, i2c_11112222, test_i2c_dev_14) +#define TEST_SPI DT_NODELABEL(spi) +#define TEST_SPI_DEVA DT_NODELABEL(test_spi_dev_a) +#define TEST_SPI_DEVB DT_NODELABEL(test_spi_dev_b) +#define TEST_SPI_DEVC DT_NODELABEL(test_spi_dev_c) + +static const struct device *init_order[20]; +static uint8_t init_idx; + +#define DEF_DRV_INIT(node_id) \ + static int DEV_INIT_##node_id(const struct device *dev) \ + { \ + printk("%s %d\n", __func__, init_idx); \ + __ASSERT_NO_MSG(init_idx < ARRAY_SIZE(init_order)); \ + init_order[init_idx++] = dev; \ + return 0; \ + } + +#define DEFINE_DRV(node_id, level) \ + DEVICE_DT_DEFINE(node_id, DEV_INIT_##node_id, NULL, NULL, NULL, level, 0, NULL) + +DEF_DRV_INIT(TEST_GPIO) +DEF_DRV_INIT(TEST_I2C) +DEF_DRV_INIT(TEST_I2C_DEVA) +DEF_DRV_INIT(TEST_I2C_DEVB) +DEF_DRV_INIT(TEST_I2C_DEVC) +DEF_DRV_INIT(TEST_I2C_NOLABEL) +DEF_DRV_INIT(TEST_SPI) +DEF_DRV_INIT(TEST_SPI_DEVA) +DEF_DRV_INIT(TEST_SPI_DEVB) +DEF_DRV_INIT(TEST_SPI_DEVC) + +DEFINE_DRV(TEST_GPIO, PRE_KERNEL_2); +DEFINE_DRV(TEST_I2C, POST_KERNEL); +DEFINE_DRV(TEST_I2C_DEVB, APPLICATION); +DEFINE_DRV(TEST_I2C_DEVC, POST_KERNEL); +DEFINE_DRV(TEST_I2C_DEVA, POST_KERNEL); +DEFINE_DRV(TEST_I2C_NOLABEL, PRE_KERNEL_1); +DEFINE_DRV(TEST_SPI, PRE_KERNEL_2); +DEFINE_DRV(TEST_SPI_DEVB, PRE_KERNEL_1); +DEFINE_DRV(TEST_SPI_DEVA, APPLICATION); +DEFINE_DRV(TEST_SPI_DEVC, PRE_KERNEL_1); + +#define DEV_HDL(node_id) DEVICE_DT_GET(node_id) +#define DEV_HDL_NAME(name) DEVICE_GET(name) + +ZTEST(devicetree_devices, test_init_order) +{ + zassert_equal(init_order[0], DEV_HDL(TEST_GPIO)); + zassert_equal(init_order[1], DEV_HDL(TEST_SPI)); + zassert_equal(init_order[2], DEV_HDL(TEST_SPI_DEVB)); + zassert_equal(init_order[3], DEV_HDL(TEST_SPI_DEVC)); + zassert_equal(init_order[4], DEV_HDL(TEST_I2C)); + zassert_equal(init_order[5], DEV_HDL(TEST_I2C_DEVA)); + zassert_equal(init_order[6], DEV_HDL(TEST_I2C_DEVC)); + zassert_equal(init_order[7], DEV_HDL(TEST_I2C_NOLABEL)); + zassert_equal(init_order[8], DEV_HDL(TEST_I2C_DEVB)); + zassert_equal(init_order[9], DEV_HDL(TEST_SPI_DEVA)); +} + +ZTEST(devicetree_devices, test_get_or_null) +{ + const struct device *dev; + + dev = DEVICE_DT_GET_OR_NULL(TEST_I2C_DEVA); + zassert_not_equal(dev, NULL, NULL); + + dev = DEVICE_DT_GET_OR_NULL(non_existing_node); + zassert_is_null(dev); +} + +ZTEST_SUITE(devicetree_devices, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/lib/devicetree/boot_order/testcase.yaml b/tests/lib/devicetree/boot_order/testcase.yaml new file mode 100644 index 00000000000000..e1ae943c97ee55 --- /dev/null +++ b/tests/lib/devicetree/boot_order/testcase.yaml @@ -0,0 +1,5 @@ +tests: + + libraries.devicetree.boot_order: + platform_allow: native_posix + tags: devicetree diff --git a/tests/lib/devicetree/devices/testcase.yaml b/tests/lib/devicetree/devices/testcase.yaml index b57489b9a11936..22f5755797641c 100644 --- a/tests/lib/devicetree/devices/testcase.yaml +++ b/tests/lib/devicetree/devices/testcase.yaml @@ -1,17 +1,5 @@ tests: libraries.devicetree.devices: tags: devicetree - # We only need this to run on one platform so use native_posix as it - # will mostly likely be the fastest. - integration_platforms: + platform_allow: - native_posix - platform_exclude: - - hsdk - - hsdk_2cores - - thingy52_nrf52832 - - bbc_microbit - - bbc_microbit_v2 - - bt610 - - bl5340_dvk_cpuapp - - bl5340_dvk_cpuapp_ns - - mimxrt595_evk_cm33