From 8c5a2c390e298a88bd7f9ff60a71d3c790749e1b Mon Sep 17 00:00:00 2001 From: Cong Nguyen Huu Date: Wed, 25 Sep 2024 14:00:55 +0700 Subject: [PATCH] tests/samples: drivers: create test, sample for psi5 driver Create test, sample for psi5 driver Signed-off-by: Cong Nguyen Huu --- samples/drivers/psi5/CMakeLists.txt | 8 + samples/drivers/psi5/README.rst | 34 ++++ .../boards/s32z2xxdc2_s32z270_rtu0.overlay | 64 ++++++++ .../boards/s32z2xxdc2_s32z270_rtu1.overlay | 7 + samples/drivers/psi5/prj.conf | 3 + samples/drivers/psi5/sample.yaml | 11 ++ samples/drivers/psi5/src/main.c | 54 ++++++ tests/drivers/psi5/psi5_api/CMakeLists.txt | 9 + .../boards/s32z2xxdc2_s32z270_rtu0.overlay | 47 ++++++ .../boards/s32z2xxdc2_s32z270_rtu1.overlay | 7 + tests/drivers/psi5/psi5_api/prj.conf | 2 + tests/drivers/psi5/psi5_api/src/main.c | 154 ++++++++++++++++++ tests/drivers/psi5/psi5_api/testcase.yaml | 7 + 13 files changed, 407 insertions(+) create mode 100644 samples/drivers/psi5/CMakeLists.txt create mode 100644 samples/drivers/psi5/README.rst create mode 100644 samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu0.overlay create mode 100644 samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu1.overlay create mode 100644 samples/drivers/psi5/prj.conf create mode 100644 samples/drivers/psi5/sample.yaml create mode 100644 samples/drivers/psi5/src/main.c create mode 100644 tests/drivers/psi5/psi5_api/CMakeLists.txt create mode 100644 tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu0.overlay create mode 100644 tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu1.overlay create mode 100644 tests/drivers/psi5/psi5_api/prj.conf create mode 100644 tests/drivers/psi5/psi5_api/src/main.c create mode 100644 tests/drivers/psi5/psi5_api/testcase.yaml diff --git a/samples/drivers/psi5/CMakeLists.txt b/samples/drivers/psi5/CMakeLists.txt new file mode 100644 index 000000000000..cc19ea188af9 --- /dev/null +++ b/samples/drivers/psi5/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(psi5) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/psi5/README.rst b/samples/drivers/psi5/README.rst new file mode 100644 index 000000000000..aa4e83ea6497 --- /dev/null +++ b/samples/drivers/psi5/README.rst @@ -0,0 +1,34 @@ +.. zephyr:code-sample:: psi5 + :name: PSI5 interface + :relevant-api: psi5_interface + + Use the Peripheral Sensor Interface (PSI5) driver. + +Overview +******** + +The sample application shows how to use the Peripheral Sensor Interface (PSI5) driver: + +* Receive data +* Transmit data + +Requirements +************ + +This sample requires a PSI5 sensor to be connected. + +Building, Flashing and Running +****************************** + +.. zephyr-app-commands:: + :zephyr-app: samples/drivers/psi5 + :board: s32z2xxdc2/s32z270/rtu0 + :goals: build flash + +Sample Output: + +.. code-block:: console + + Rx channel 1 completed + + Tx channel 2 completed diff --git a/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu0.overlay b/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu0.overlay new file mode 100644 index 000000000000..3edfe82c8ab8 --- /dev/null +++ b/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu0.overlay @@ -0,0 +1,64 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + psi5-node = &psi5_0; + }; +}; + +&pinctrl { + psi5_0_default: psi5_0_default { + group1 { + pinmux = , ; + output-enable; + }; + group2 { + pinmux = , ; + input-enable; + }; + }; +}; + +&psi5_0 { + pinctrl-0 = <&psi5_0_default>; + pinctrl-names = "default"; + status = "okay"; +}; + +&psi5_0_ch1 { + period-sync-pulse-us = <500>; + decoder-start-offset-us = <0>; + pulse-width-0-us = <100>; + pulse-width-1-us = <127>; + tx-mode = "long-frame-31"; + num-rx-buf = <32>; + rx-bitrate-kbps = <189>; + array-slot-duration-us = <150>; + array-slot-start-offset-us = <110>; + array-slot-data-length = <16>; + array-slot-data-msb-first = <0>; + array-slot-has-smc = <0>; + array-slot-has-parity = <0>; + status = "okay"; +}; + +&psi5_0_ch2 { + period-sync-pulse-us = <8>; + decoder-start-offset-us = <0>; + pulse-width-0-us = <2>; + pulse-width-1-us = <6>; + tx-mode = "long-frame-31"; + num-rx-buf = <32>; + rx-bitrate-kbps = <189>; + array-slot-duration-us = <500>; + array-slot-start-offset-us = <0>; + array-slot-data-length = <16>; + array-slot-data-msb-first = <0>; + array-slot-has-smc = <0>; + array-slot-has-parity = <0>; + status = "okay"; +}; diff --git a/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu1.overlay b/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu1.overlay new file mode 100644 index 000000000000..0a3db9994302 --- /dev/null +++ b/samples/drivers/psi5/boards/s32z2xxdc2_s32z270_rtu1.overlay @@ -0,0 +1,7 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "s32z2xxdc2_s32z270_rtu0.overlay" diff --git a/samples/drivers/psi5/prj.conf b/samples/drivers/psi5/prj.conf new file mode 100644 index 000000000000..bac255b3c705 --- /dev/null +++ b/samples/drivers/psi5/prj.conf @@ -0,0 +1,3 @@ +CONFIG_PSI5=y +CONFIG_PSI5_LOG_LEVEL_DBG=y +CONFIG_LOG=y diff --git a/samples/drivers/psi5/sample.yaml b/samples/drivers/psi5/sample.yaml new file mode 100644 index 000000000000..525b1990bbbb --- /dev/null +++ b/samples/drivers/psi5/sample.yaml @@ -0,0 +1,11 @@ +sample: + name: PSI5 driver sample + +tests: + sample.drivers.psi5: + tags: + - drivers + - psi5 + depends_on: psi5 + filter: dt_alias_exists("psi5-node") + harness: sensor diff --git a/samples/drivers/psi5/src/main.c b/samples/drivers/psi5/src/main.c new file mode 100644 index 000000000000..5a4beac2bd2a --- /dev/null +++ b/samples/drivers/psi5/src/main.c @@ -0,0 +1,54 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +LOG_MODULE_REGISTER(psi5_sample, LOG_LEVEL_DBG); + +#include + +#include + +#define PSI5_NODE DT_ALIAS(psi5_node) +#define PSI5_CHANNEL_RX 1 +#define PSI5_CHANNEL_TX 2 + +void tx_cb(const struct device *dev, uint8_t channel_id, enum psi5_status status, void *user_data) +{ + LOG_INF("Tx channel %d completed\n", channel_id); +} + +void rx_cb(const struct device *dev, uint8_t channel_id, struct psi5_frame *frame, + enum psi5_status status, void *user_data) +{ + + LOG_INF("Rx channel %d completed\n", channel_id); +} + +int main(void) +{ + const struct device *const dev = DEVICE_DT_GET(PSI5_NODE); + uint64_t send_data = 0x1234; + + /* Test receive data */ + psi5_add_rx_callback(dev, PSI5_CHANNEL_RX, rx_cb, NULL); + + psi5_start_sync(dev, PSI5_CHANNEL_RX); + + k_sleep(K_MSEC(100)); + + psi5_stop_sync(dev, PSI5_CHANNEL_RX); + + /* Test send data */ + psi5_start_sync(dev, PSI5_CHANNEL_TX); + + psi5_send(dev, PSI5_CHANNEL_TX, send_data, K_MSEC(100), tx_cb, NULL); + + k_sleep(K_MSEC(100)); + + psi5_stop_sync(dev, PSI5_CHANNEL_TX); + + return 0; +} diff --git a/tests/drivers/psi5/psi5_api/CMakeLists.txt b/tests/drivers/psi5/psi5_api/CMakeLists.txt new file mode 100644 index 000000000000..34adf12427ec --- /dev/null +++ b/tests/drivers/psi5/psi5_api/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(psi5) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu0.overlay b/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu0.overlay new file mode 100644 index 000000000000..dfd93afcd84a --- /dev/null +++ b/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu0.overlay @@ -0,0 +1,47 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + psi5-node = &psi5_0; + }; +}; + +&pinctrl { + psi5_0_default: psi5_0_default { + group1 { + pinmux = ; + output-enable; + }; + group2 { + pinmux = ; + input-enable; + }; + }; +}; + +&psi5_0 { + pinctrl-0 = <&psi5_0_default>; + pinctrl-names = "default"; + status = "okay"; +}; + +&psi5_0_ch1 { + period-sync-pulse-us = <500>; + decoder-start-offset-us = <0>; + pulse-width-0-us = <100>; + pulse-width-1-us = <127>; + tx-mode = "long-frame-31"; + num-rx-buf = <32>; + rx-bitrate-kbps = <189>; + array-slot-duration-us = <150>; + array-slot-start-offset-us = <110>; + array-slot-data-length = <16>; + array-slot-data-msb-first = <0>; + array-slot-has-smc = <0>; + array-slot-has-parity = <0>; + status = "okay"; +}; diff --git a/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu1.overlay b/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu1.overlay new file mode 100644 index 000000000000..0a3db9994302 --- /dev/null +++ b/tests/drivers/psi5/psi5_api/boards/s32z2xxdc2_s32z270_rtu1.overlay @@ -0,0 +1,7 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "s32z2xxdc2_s32z270_rtu0.overlay" diff --git a/tests/drivers/psi5/psi5_api/prj.conf b/tests/drivers/psi5/psi5_api/prj.conf new file mode 100644 index 000000000000..5d7b4975ea6b --- /dev/null +++ b/tests/drivers/psi5/psi5_api/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_PSI5=y diff --git a/tests/drivers/psi5/psi5_api/src/main.c b/tests/drivers/psi5/psi5_api/src/main.c new file mode 100644 index 000000000000..07e52f40ee17 --- /dev/null +++ b/tests/drivers/psi5/psi5_api/src/main.c @@ -0,0 +1,154 @@ +/* + * Copyright 2025 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define PSI5_NODE DT_ALIAS(psi5_node) +#define PSI5_CHANNEL 1 + +const struct device *dev = DEVICE_DT_GET(PSI5_NODE); +struct k_sem tx_callback_sem; + +static void *psi5_setup(void) +{ + k_sem_init(&tx_callback_sem, 0, 1); + + zassert_true(device_is_ready(dev), "PSI5 device is not ready"); + + return NULL; +} + +void rx_cb(const struct device *dev, uint8_t channel_id, struct psi5_frame *frame, + enum psi5_status status, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel_id); + ARG_UNUSED(frame); + ARG_UNUSED(status); + ARG_UNUSED(user_data); +} + +void tx_cb(const struct device *dev, uint8_t channel_id, enum psi5_status status, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel_id); + ARG_UNUSED(status); + ARG_UNUSED(user_data); + + k_sem_give(&tx_callback_sem); +} + +/** + * @brief Test starting sync is not allowed while started. + */ +ZTEST_USER(psi5_api, test_start_sync_while_started) +{ + int err; + + err = psi5_start_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to start sync (err %d)", err); + + err = psi5_start_sync(dev, PSI5_CHANNEL); + zassert_not_equal(err, 0, "Started sync while started"); + zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err); +} + +/** + * @brief Test stopping sync is not allowed while stopped. + */ +ZTEST_USER(psi5_api, test_stop_sync_while_stoped) +{ + int err; + + err = psi5_stop_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to stop sync (err %d)", err); + + err = psi5_stop_sync(dev, PSI5_CHANNEL); + zassert_not_equal(err, 0, "Stopped sync while stopped"); + zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err); + + err = psi5_start_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to start sync (err %d)", err); +} + +/** + * @brief Test setting the rx callback. + */ +ZTEST(psi5_api, test_set_rx_callback) +{ + int err; + + err = psi5_add_rx_callback(dev, PSI5_CHANNEL, rx_cb, NULL); + zassert_equal(err, 0, "Failed to set rx callback (err %d)", err); + + psi5_add_rx_callback(dev, PSI5_CHANNEL, NULL, NULL); + zassert_equal(err, 0, "Failed to set rx callback (err %d)", err); + + err = psi5_add_rx_callback(dev, PSI5_CHANNEL, rx_cb, NULL); + zassert_equal(err, 0, "Failed to set rx callback (err %d)", err); +} + +/** + * @brief Test sending data with callback. + */ +ZTEST(psi5_api, test_send_callback) +{ + int err; + uint64_t send_data = 0x1234; + + k_sem_reset(&tx_callback_sem); + + err = psi5_start_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to start sync (err %d)", err); + + err = psi5_send(dev, PSI5_CHANNEL, send_data, K_MSEC(100), tx_cb, NULL); + zassert_equal(err, 0, "Failed to send (err %d)", err); + + k_sleep(K_MSEC(100)); + + err = psi5_stop_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to stop sync (err %d)", err); + + err = k_sem_take(&tx_callback_sem, K_MSEC(100)); + zassert_equal(err, 0, "missing TX callback"); +} + +/** + * @brief Test sending data without callback. + */ +ZTEST(psi5_api, test_send_without_callback) +{ + int err; + uint64_t send_data = 0x1234; + + err = psi5_start_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to start sync (err %d)", err); + + err = psi5_send(dev, PSI5_CHANNEL, send_data, K_MSEC(100), NULL, NULL); + zassert_equal(err, 0, "Failed to send (err %d)", err); + + k_sleep(K_MSEC(100)); + + err = psi5_stop_sync(dev, PSI5_CHANNEL); + zassert_equal(err, 0, "Failed to stop sync (err %d)", err); +} + +/** + * @brief Test sending data is not allowed while stopped sync. + */ +ZTEST(psi5_api, test_send_while_stopped_sync) +{ + int err; + uint64_t send_data = 0x1234; + + err = psi5_send(dev, PSI5_CHANNEL, send_data, K_MSEC(100), NULL, NULL); + zassert_not_equal(err, 0, "Sent data while stopped sync"); + zassert_equal(err, -ENETDOWN, "Wrong error return code (err %d)", err); +} + +ZTEST_SUITE(psi5_api, NULL, psi5_setup, NULL, NULL, NULL); diff --git a/tests/drivers/psi5/psi5_api/testcase.yaml b/tests/drivers/psi5/psi5_api/testcase.yaml new file mode 100644 index 000000000000..587cb77b5c0c --- /dev/null +++ b/tests/drivers/psi5/psi5_api/testcase.yaml @@ -0,0 +1,7 @@ +tests: + drivers.psi5: + tags: + - drivers + - psi5 + depends_on: psi5 + filter: dt_alias_exists("psi5-node")