Skip to content

Commit

Permalink
tests/kernel/device: Add tests for device deferred init
Browse files Browse the repository at this point in the history
Ensure that devices are not ready before calling `device_init()`, but
are after the call.

Signed-off-by: Ederson de Souza <[email protected]>
  • Loading branch information
edersondisouza committed Mar 6, 2024
1 parent 660cae3 commit 3e054b7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
14 changes: 14 additions & 0 deletions tests/kernel/device/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@
status = "okay";
};

fakedeferdriver@E7000000 {
compatible = "fakedeferdriver";
reg = <0xE7000000 0x2000>;
status = "okay";
zephyr,deferred-init;
};

fakedeferdriver@E8000000 {
compatible = "fakedeferdriver";
reg = <0xE8000000 0x2000>;
status = "okay";
zephyr,deferred-init;
};

fakedomain_0: fakedomain_0 {
compatible = "fakedomain";
status = "okay";
Expand Down
14 changes: 14 additions & 0 deletions tests/kernel/device/boards/hifive_unmatched.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
status = "okay";
};

fakedeferdriver@E7000000 {
compatible = "fakedeferdriver";
reg = <0x0 0xE7000000 0x0 0x2000>;
status = "okay";
zephyr,deferred-init;
};

fakedeferdriver@E8000000 {
compatible = "fakedeferdriver";
reg = <0x0 0xE8000000 0x0 0x2000>;
status = "okay";
zephyr,deferred-init;
};

fakedomain_0: fakedomain_0 {
compatible = "fakedomain";
status = "okay";
Expand Down
8 changes: 8 additions & 0 deletions tests/kernel/device/dts/bindings/fakedeferdriver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

description: Properties for fake deferred driver.

compatible: "fakedeferdriver"

include: base.yaml
48 changes: 47 additions & 1 deletion tests/kernel/device/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/init.h>
#include <zephyr/ztest.h>
#include <zephyr/sys/printk.h>
Expand All @@ -21,10 +22,23 @@
#define MY_DRIVER_A "my_driver_A"
#define MY_DRIVER_B "my_driver_B"

#define FAKEDEFERDRIVER0 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e7000000))
#define FAKEDEFERDRIVER1 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e8000000))

/* A device without init call */
DEVICE_DEFINE(dummy_noinit, DUMMY_NOINIT, NULL, NULL, NULL, NULL,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);

/* To access from userspace, the device needs an API. Use a dummy GPIO one */
static const struct gpio_driver_api fakedeferdriverapi;

/* Fake deferred devices */
DEVICE_DT_DEFINE(DT_INST(0, fakedeferdriver), NULL, NULL, NULL, NULL,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
DEVICE_DT_DEFINE(DT_INST(1, fakedeferdriver), NULL, NULL, NULL, NULL,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&fakedeferdriverapi);

/**
* @brief Test cases to verify device objects
*
Expand Down Expand Up @@ -387,9 +401,41 @@ ZTEST(device, test_abstraction_driver_common)
zassert_true(baz == 2, "common API do_that fail");
}

ZTEST(device, test_deferred_init)
{
int ret;

zassert_false(device_is_ready(FAKEDEFERDRIVER0));

ret = device_init(FAKEDEFERDRIVER0);
zassert_true(ret == 0);

zassert_true(device_is_ready(FAKEDEFERDRIVER0));
}

ZTEST_USER(device, test_deferred_init_user)
{
int ret;

zassert_false(device_is_ready(FAKEDEFERDRIVER1));

ret = device_init(FAKEDEFERDRIVER1);
zassert_true(ret == 0);

zassert_true(device_is_ready(FAKEDEFERDRIVER1));
}

void *user_setup(void)
{
#ifdef CONFIG_USERSPACE
k_object_access_grant(FAKEDEFERDRIVER1, k_current_get());
#endif

return NULL;
}

/**
* @}
*/

ZTEST_SUITE(device, NULL, NULL, NULL, NULL, NULL);
ZTEST_SUITE(device, NULL, user_setup, NULL, NULL, NULL);

0 comments on commit 3e054b7

Please sign in to comment.