Skip to content

Commit

Permalink
tests/samples: drivers: create test, sample for SENT driver
Browse files Browse the repository at this point in the history
Create test, sample for SENT driver

Signed-off-by: Cong Nguyen Huu <cong.nguyenhuu@nxp.com>
  • Loading branch information
congnguyenhuu committed Jan 16, 2025
1 parent 49be922 commit 4fc328c
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 0 deletions.
8 changes: 8 additions & 0 deletions samples/drivers/sent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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(sent)

target_sources(app PRIVATE src/main.c)
31 changes: 31 additions & 0 deletions samples/drivers/sent/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.. zephyr:code-sample:: sent
:name: SENT interface
:relevant-api: sent_interface

Use the Single Edge Nibble Transmission (SENT) driver.

Overview
********

The sample application shows how to use the Single Edge Nibble Transmission (SENT) driver:

* Receive data

Requirements
************

This sample requires a SENT sensor to be connected.

Building, Flashing and Running
******************************

.. zephyr-app-commands::
:zephyr-app: samples/drivers/sent
:board: s32z2xxdc2/s32z270/rtu0
:goals: build flash

Sample Output:

.. code-block:: console
Rx channel 1 completed
34 changes: 34 additions & 0 deletions samples/drivers/sent/boards/s32z2xxdc2_s32z270_rtu0.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
sent-node = &sent1;
};
};

&pinctrl {
sent1_default: sent1_default {
group1 {
pinmux = <PK2_SENT_1_CH1_I>;
input-enable;
};
};
};

&sent1 {
pinctrl-0 = <&sent1_default>;
pinctrl-names = "default";
status = "okay";
};

&sent1_ch1 {
num-data-nibbles = <6>;
tick-time-prescaler-us = <3>;
bus-timeout-cycles = <256>;
calib-method-low-latency;
status = "okay";
};
7 changes: 7 additions & 0 deletions samples/drivers/sent/boards/s32z2xxdc2_s32z270_rtu1.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "s32z2xxdc2_s32z270_rtu0.overlay"
3 changes: 3 additions & 0 deletions samples/drivers/sent/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_SENT=y
CONFIG_SENT_LOG_LEVEL_DBG=y
CONFIG_LOG=y
11 changes: 11 additions & 0 deletions samples/drivers/sent/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sample:
name: SENT driver sample

tests:
sample.drivers.sent:
tags:
- drivers
- sent
depends_on: sent
filter: dt_alias_exists("sent-node")
harness: sensor
36 changes: 36 additions & 0 deletions samples/drivers/sent/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(sent_sample, LOG_LEVEL_DBG);

#include <zephyr/kernel.h>

#include <zephyr/drivers/sent/sent.h>

#define SENT_NODE DT_ALIAS(sent_node)
#define SENT_CHANNEL 1

void rx_cb(const struct device *dev, uint8_t channel_id, struct sent_frame *frame,
enum sent_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(SENT_NODE);

sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);

sent_start_rx(dev, SENT_CHANNEL);

k_sleep(K_MSEC(100));

sent_stop_rx(dev, SENT_CHANNEL);

return 0;
}
9 changes: 9 additions & 0 deletions tests/drivers/sent/sent_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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(sent)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
34 changes: 34 additions & 0 deletions tests/drivers/sent/sent_api/boards/s32z2xxdc2_s32z270_rtu0.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
sent-node = &sent1;
};
};

&pinctrl {
sent1_default: sent1_default {
group1 {
pinmux = <PK2_SENT_1_CH1_I>;
input-enable;
};
};
};

&sent1 {
pinctrl-0 = <&sent1_default>;
pinctrl-names = "default";
status = "okay";
};

&sent1_ch1 {
num-data-nibbles = <6>;
tick-time-prescaler-us = <3>;
bus-timeout-cycles = <256>;
calib-method-low-latency;
status = "okay";
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "s32z2xxdc2_s32z270_rtu0.overlay"
2 changes: 2 additions & 0 deletions tests/drivers/sent/sent_api/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_SENT=y
83 changes: 83 additions & 0 deletions tests/drivers/sent/sent_api/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sent/sent.h>

#define SENT_NODE DT_ALIAS(sent_node)
#define SENT_CHANNEL 1

const struct device *dev = DEVICE_DT_GET(SENT_NODE);

static void *sent_setup(void)
{
zassert_true(device_is_ready(dev), "SENT device is not ready");

return NULL;
}

void rx_cb(const struct device *dev, uint8_t channel_id, struct sent_frame *frame,
enum sent_status status, void *user_data)
{
ARG_UNUSED(dev);
ARG_UNUSED(channel_id);
ARG_UNUSED(frame);
ARG_UNUSED(status);
ARG_UNUSED(user_data);
}

/**
* @brief Test starting rx is not allowed while started.
*/
ZTEST_USER(sent_api, test_start_rx_while_started)
{
int err;

err = sent_start_rx(dev, SENT_CHANNEL);
zassert_equal(err, 0, "Failed to start rx (err %d)", err);

err = sent_start_rx(dev, SENT_CHANNEL);
zassert_not_equal(err, 0, "Started rx while started");
zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err);
}

/**
* @brief Test stopping rx is not allowed while stopped.
*/
ZTEST_USER(sent_api, test_stop_rx_while_stoped)

Check warning on line 51 in tests/drivers/sent/sent_api/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TYPO_SPELLING

tests/drivers/sent/sent_api/src/main.c:51 'stoped' may be misspelled - perhaps 'stopped'?
{
int err;

err = sent_stop_rx(dev, SENT_CHANNEL);
zassert_equal(err, 0, "Failed to stop rx (err %d)", err);

err = sent_stop_rx(dev, SENT_CHANNEL);
zassert_not_equal(err, 0, "Stopped rx while stopped");
zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err);

err = sent_start_rx(dev, SENT_CHANNEL);
zassert_equal(err, 0, "Failed to start rx (err %d)", err);
}

/**
* @brief Test setting the SENT rx callback.
*/
ZTEST(sent_api, test_set_rx_callback)
{
int err;

err = sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);

sent_add_rx_callback(dev, SENT_CHANNEL, NULL, NULL);
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);

err = sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);
}

ZTEST_SUITE(sent_api, NULL, sent_setup, NULL, NULL, NULL);
7 changes: 7 additions & 0 deletions tests/drivers/sent/sent_api/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests:
drivers.sent:
tags:
- drivers
- sent
depends_on: sent
filter: dt_alias_exists("sent-node")

0 comments on commit 4fc328c

Please sign in to comment.