tests: mspi: Add mspi api test
Add a MSPI controller API test and run on native_sim board. The API implementation within mspi_emul.c is the test target. The stub device emulator is added for convenience to test controllers. Signed-off-by: Swift Tian <swift.tian@ambiq.com>
This commit is contained in:
parent
f5554ca762
commit
42d24e5716
7 changed files with 233 additions and 0 deletions
10
tests/drivers/mspi/api/CMakeLists.txt
Normal file
10
tests/drivers/mspi/api/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(mspi_api)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
||||
4
tests/drivers/mspi/api/boards/native_sim.conf
Normal file
4
tests/drivers/mspi/api/boards/native_sim.conf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_EMUL=y
|
||||
22
tests/drivers/mspi/api/boards/native_sim.overlay
Normal file
22
tests/drivers/mspi/api/boards/native_sim.overlay
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
aliases {
|
||||
mspi0 = &mspi0;
|
||||
};
|
||||
};
|
||||
|
||||
&mspi0 {
|
||||
|
||||
mspi_device: mspi_device@0 {
|
||||
status = "okay";
|
||||
compatible = "zephyr,mspi-emul-device";
|
||||
reg = <0x0>;
|
||||
mspi-max-frequency = <48000000>;
|
||||
};
|
||||
|
||||
};
|
||||
5
tests/drivers/mspi/api/prj.conf
Normal file
5
tests/drivers/mspi/api/prj.conf
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_ZTEST=y
|
||||
CONFIG_MSPI=y
|
||||
115
tests/drivers/mspi/api/src/main.c
Normal file
115
tests/drivers/mspi/api/src/main.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/mspi.h>
|
||||
#include <zephyr/drivers/mspi_emul.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
#define TEST_MSPI_REINIT 1
|
||||
|
||||
/* add else if for other SoC platforms */
|
||||
#if defined(CONFIG_SOC_POSIX)
|
||||
typedef struct mspi_timing_cfg mspi_timing_cfg;
|
||||
typedef enum mspi_timing_param mspi_timing_param;
|
||||
#elif defined(CONFIG_SOC_FAMILY_AMBIQ)
|
||||
#include "mspi_ambiq.h"
|
||||
typedef struct mspi_ambiq_timing_cfg mspi_timing_cfg;
|
||||
typedef enum mspi_ambiq_timing_param mspi_timing_param;
|
||||
#endif
|
||||
|
||||
#define MSPI_BUS_NODE DT_ALIAS(mspi0)
|
||||
|
||||
static const struct device *mspi_devices[] = {
|
||||
DT_FOREACH_CHILD_STATUS_OKAY_SEP(MSPI_BUS_NODE, DEVICE_DT_GET, (,))
|
||||
};
|
||||
|
||||
static struct gpio_dt_spec ce_gpios[] = MSPI_CE_GPIOS_DT_SPEC_GET(MSPI_BUS_NODE);
|
||||
|
||||
#if TEST_MSPI_REINIT
|
||||
struct mspi_cfg hardware_cfg = {
|
||||
.channel_num = 0,
|
||||
.op_mode = DT_PROP_OR(MSPI_BUS_NODE, op_mode, MSPI_OP_MODE_CONTROLLER),
|
||||
.duplex = DT_PROP_OR(MSPI_BUS_NODE, duplex, MSPI_HALF_DUPLEX),
|
||||
.dqs_support = DT_PROP_OR(MSPI_BUS_NODE, dqs_support, false),
|
||||
.ce_group = ce_gpios,
|
||||
.num_ce_gpios = ARRAY_SIZE(ce_gpios),
|
||||
.num_periph = DT_CHILD_NUM(MSPI_BUS_NODE),
|
||||
.max_freq = DT_PROP(MSPI_BUS_NODE, clock_frequency),
|
||||
.re_init = true,
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
static struct mspi_dev_id dev_id[] = {
|
||||
DT_FOREACH_CHILD_STATUS_OKAY_SEP(MSPI_BUS_NODE, MSPI_DEVICE_ID_DT, (,))
|
||||
};
|
||||
|
||||
static struct mspi_dev_cfg device_cfg[] = {
|
||||
DT_FOREACH_CHILD_STATUS_OKAY_SEP(MSPI_BUS_NODE, MSPI_DEVICE_CONFIG_DT, (,))
|
||||
};
|
||||
|
||||
static struct mspi_xip_cfg xip_cfg[] = {
|
||||
DT_FOREACH_CHILD_STATUS_OKAY_SEP(MSPI_BUS_NODE, MSPI_XIP_CONFIG_DT, (,))
|
||||
};
|
||||
|
||||
static struct mspi_scramble_cfg scramble_cfg[] = {
|
||||
DT_FOREACH_CHILD_STATUS_OKAY_SEP(MSPI_BUS_NODE, MSPI_SCRAMBLE_CONFIG_DT, (,))
|
||||
};
|
||||
|
||||
ZTEST(mspi_api, test_mspi_api)
|
||||
{
|
||||
int ret = 0;
|
||||
const struct device *mspi_bus = DEVICE_DT_GET(MSPI_BUS_NODE);
|
||||
|
||||
zassert_true(device_is_ready(mspi_bus), "mspi_bus is not ready");
|
||||
|
||||
#if TEST_MSPI_REINIT
|
||||
const struct mspi_dt_spec spec = {
|
||||
.bus = mspi_bus,
|
||||
.config = hardware_cfg,
|
||||
};
|
||||
|
||||
ret = mspi_config(&spec);
|
||||
zassert_equal(ret, 0, "mspi_config failed.");
|
||||
#endif
|
||||
|
||||
for (int dev_idx = 0; dev_idx < ARRAY_SIZE(mspi_devices); ++dev_idx) {
|
||||
|
||||
zassert_true(device_is_ready(mspi_devices[dev_idx]), "mspi_device is not ready");
|
||||
|
||||
ret = mspi_dev_config(mspi_bus, &dev_id[dev_idx],
|
||||
MSPI_DEVICE_CONFIG_ALL, &device_cfg[dev_idx]);
|
||||
zassert_equal(ret, 0, "mspi_dev_config failed.");
|
||||
|
||||
#if CONFIG_MSPI_XIP
|
||||
ret = mspi_xip_config(mspi_bus, &dev_id[dev_idx], &xip_cfg[dev_idx]);
|
||||
zassert_equal(ret, 0, "mspi_xip_config failed.");
|
||||
#endif
|
||||
|
||||
#if CONFIG_MSPI_SCRAMBLE
|
||||
ret = mspi_scramble_config(mspi_bus, &dev_id[dev_idx], &scramble_cfg[dev_idx]);
|
||||
zassert_equal(ret, 0, "mspi_scramble_config failed.");
|
||||
#endif
|
||||
|
||||
#if CONFIG_MSPI_TIMING
|
||||
mspi_timing_cfg timing_cfg;
|
||||
mspi_timing_param timing_cfg_mask = 0;
|
||||
|
||||
ret = mspi_timing_config(mspi_bus, &dev_id[dev_idx], timing_cfg_mask, &timing_cfg);
|
||||
zassert_equal(ret, 0, "mspi_timing_config failed.");
|
||||
#endif
|
||||
|
||||
ret = mspi_register_callback(mspi_bus, &dev_id[dev_idx],
|
||||
MSPI_BUS_XFER_COMPLETE, NULL, NULL);
|
||||
zassert_equal(ret, 0, "mspi_register_callback failed.");
|
||||
|
||||
ret = mspi_get_channel_status(mspi_bus, 0);
|
||||
zassert_equal(ret, 0, "mspi_get_channel_status failed.");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ZTEST_SUITE(mspi_api, NULL, NULL, NULL, NULL, NULL);
|
||||
62
tests/drivers/mspi/api/src/stub_mspi_emul_device.c
Normal file
62
tests/drivers/mspi/api/src/stub_mspi_emul_device.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/emul.h>
|
||||
#include <zephyr/drivers/mspi.h>
|
||||
#include <zephyr/drivers/mspi_emul.h>
|
||||
#define DT_DRV_COMPAT zephyr_mspi_emul_device
|
||||
|
||||
/* Stub out a mspi device struct to use mspi_device emulator. */
|
||||
static int emul_mspi_device_init_stub(const struct device *dev)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emul_mspi_init_stub(const struct emul *stub_emul, const struct device *bus)
|
||||
{
|
||||
ARG_UNUSED(stub_emul);
|
||||
ARG_UNUSED(bus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct emul_mspi_device_stub_dev_data {
|
||||
/* Stub */
|
||||
};
|
||||
struct emul_mspi_device_stub_dev_config {
|
||||
/* Stub */
|
||||
};
|
||||
struct emul_mspi_device_stub_dev_api {
|
||||
/* Stub */
|
||||
};
|
||||
|
||||
#define EMUL_MSPI_DEVICE_DEVICE_STUB(n) \
|
||||
static struct emul_mspi_device_stub_dev_data stub_device_data_##n; \
|
||||
static struct emul_mspi_device_stub_dev_config stub_device_config_##n; \
|
||||
static struct emul_mspi_device_stub_dev_api stub_device_api_##n; \
|
||||
DEVICE_DT_INST_DEFINE(n, \
|
||||
emul_mspi_device_init_stub, \
|
||||
NULL, \
|
||||
&stub_device_data_##n, \
|
||||
&stub_device_config_##n, \
|
||||
POST_KERNEL, \
|
||||
CONFIG_MSPI_INIT_PRIORITY, \
|
||||
&stub_device_api_##n);
|
||||
|
||||
#define EMUL_TEST(n) \
|
||||
EMUL_DT_INST_DEFINE(n, \
|
||||
emul_mspi_init_stub, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
NULL);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(EMUL_TEST)
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(EMUL_MSPI_DEVICE_DEVICE_STUB)
|
||||
15
tests/drivers/mspi/api/testcase.yaml
Normal file
15
tests/drivers/mspi/api/testcase.yaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# Copyright (c) 2024 Ambiq Micro Inc. <www.ambiq.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
tests:
|
||||
drivers.mspi.api:
|
||||
tags:
|
||||
- drivers
|
||||
- mspi
|
||||
- api
|
||||
filter: dt_compat_enabled("zephyr,mspi-emul-controller")
|
||||
harness: ztest
|
||||
platform_allow:
|
||||
- native_sim
|
||||
integration_platforms:
|
||||
- native_sim
|
||||
Loading…
Reference in a new issue