Skip to content

Commit 4e2740f

Browse files
committed
tests/samples: drivers: create test, sample for SENT driver
Create test, sample for SENT driver Signed-off-by: Cong Nguyen Huu <[email protected]>
1 parent dca1ab0 commit 4e2740f

13 files changed

+272
-0
lines changed

samples/drivers/sent/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(sent)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/drivers/sent/README.rst

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. zephyr:code-sample:: sent
2+
:name: SENT interface
3+
:relevant-api: sent_interface
4+
5+
Use the Single Edge Nibble Transmission (SENT) driver.
6+
7+
Overview
8+
********
9+
10+
The sample application shows how to use the Single Edge Nibble Transmission (SENT) driver:
11+
12+
* Receive data
13+
14+
Requirements
15+
************
16+
17+
This sample requires a SENT sensor to be connected.
18+
19+
Building, Flashing and Running
20+
******************************
21+
22+
.. zephyr-app-commands::
23+
:zephyr-app: samples/drivers/sent
24+
:board: s32z2xxdc2/s32z270/rtu0
25+
:goals: build flash
26+
27+
Sample Output:
28+
29+
.. code-block:: console
30+
31+
Rx channel 1 completed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
sent-node = &sent1;
10+
};
11+
};
12+
13+
&pinctrl {
14+
sent1_default: sent1_default {
15+
group1 {
16+
pinmux = <PK2_SENT_1_CH1_I>;
17+
input-enable;
18+
};
19+
};
20+
};
21+
22+
&sent1 {
23+
pinctrl-0 = <&sent1_default>;
24+
pinctrl-names = "default";
25+
status = "okay";
26+
};
27+
28+
&sent1_ch1 {
29+
num-data-nibbles = <6>;
30+
tick-time-prescaler-us = <3>;
31+
bus-timeout-cycles = <256>;
32+
calib-method-low-latency;
33+
status = "okay";
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "s32z2xxdc2_s32z270_rtu0.overlay"

samples/drivers/sent/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_SENT=y
2+
CONFIG_SENT_LOG_LEVEL_DBG=y
3+
CONFIG_LOG=y

samples/drivers/sent/sample.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sample:
2+
name: SENT driver sample
3+
4+
tests:
5+
sample.drivers.sent:
6+
tags:
7+
- drivers
8+
- sent
9+
depends_on: sent
10+
filter: dt_alias_exists("sent-node")
11+
harness: sensor

samples/drivers/sent/src/main.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_REGISTER(sent_sample, LOG_LEVEL_DBG);
9+
10+
#include <zephyr/kernel.h>
11+
12+
#include <zephyr/drivers/sent/sent.h>
13+
14+
#define SENT_NODE DT_ALIAS(sent_node)
15+
#define SENT_CHANNEL 1
16+
17+
void rx_cb(const struct device *dev, uint8_t channel_id, struct sent_frame *frame,
18+
enum sent_status status, void *user_data)
19+
{
20+
LOG_INF("Rx channel %d completed\n", channel_id);
21+
}
22+
23+
int main(void)
24+
{
25+
const struct device *const dev = DEVICE_DT_GET(SENT_NODE);
26+
27+
sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);
28+
29+
sent_start_rx(dev, SENT_CHANNEL);
30+
31+
k_sleep(K_MSEC(100));
32+
33+
sent_stop_rx(dev, SENT_CHANNEL);
34+
35+
return 0;
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(sent)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
sent-node = &sent1;
10+
};
11+
};
12+
13+
&pinctrl {
14+
sent1_default: sent1_default {
15+
group1 {
16+
pinmux = <PK2_SENT_1_CH1_I>;
17+
input-enable;
18+
};
19+
};
20+
};
21+
22+
&sent1 {
23+
pinctrl-0 = <&sent1_default>;
24+
pinctrl-names = "default";
25+
status = "okay";
26+
};
27+
28+
&sent1_ch1 {
29+
num-data-nibbles = <6>;
30+
tick-time-prescaler-us = <3>;
31+
bus-timeout-cycles = <256>;
32+
calib-method-low-latency;
33+
status = "okay";
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "s32z2xxdc2_s32z270_rtu0.overlay"

tests/drivers/sent/sent_api/prj.conf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_SENT=y
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sent/sent.h>
10+
11+
#define SENT_NODE DT_ALIAS(sent_node)
12+
#define SENT_CHANNEL 1
13+
14+
const struct device *dev = DEVICE_DT_GET(SENT_NODE);
15+
16+
static void *sent_setup(void)
17+
{
18+
zassert_true(device_is_ready(dev), "SENT device is not ready");
19+
20+
return NULL;
21+
}
22+
23+
void rx_cb(const struct device *dev, uint8_t channel_id, struct sent_frame *frame,
24+
enum sent_status status, void *user_data)
25+
{
26+
ARG_UNUSED(dev);
27+
ARG_UNUSED(channel_id);
28+
ARG_UNUSED(frame);
29+
ARG_UNUSED(status);
30+
ARG_UNUSED(user_data);
31+
}
32+
33+
/**
34+
* @brief Test starting rx is not allowed while started.
35+
*/
36+
ZTEST_USER(sent_api, test_start_rx_while_started)
37+
{
38+
int err;
39+
40+
err = sent_start_rx(dev, SENT_CHANNEL);
41+
zassert_equal(err, 0, "Failed to start rx (err %d)", err);
42+
43+
err = sent_start_rx(dev, SENT_CHANNEL);
44+
zassert_not_equal(err, 0, "Started rx while started");
45+
zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err);
46+
}
47+
48+
/**
49+
* @brief Test stopping rx is not allowed while stopped.
50+
*/
51+
ZTEST_USER(sent_api, test_stop_rx_while_stopped)
52+
{
53+
int err;
54+
55+
err = sent_stop_rx(dev, SENT_CHANNEL);
56+
zassert_equal(err, 0, "Failed to stop rx (err %d)", err);
57+
58+
err = sent_stop_rx(dev, SENT_CHANNEL);
59+
zassert_not_equal(err, 0, "Stopped rx while stopped");
60+
zassert_equal(err, -EALREADY, "Wrong error return code (err %d)", err);
61+
62+
err = sent_start_rx(dev, SENT_CHANNEL);
63+
zassert_equal(err, 0, "Failed to start rx (err %d)", err);
64+
}
65+
66+
/**
67+
* @brief Test setting the rx callback.
68+
*/
69+
ZTEST(sent_api, test_set_rx_callback)
70+
{
71+
int err;
72+
73+
err = sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);
74+
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);
75+
76+
sent_add_rx_callback(dev, SENT_CHANNEL, NULL, NULL);
77+
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);
78+
79+
err = sent_add_rx_callback(dev, SENT_CHANNEL, rx_cb, NULL);
80+
zassert_equal(err, 0, "Failed to set rx callback (err %d)", err);
81+
}
82+
83+
ZTEST_SUITE(sent_api, NULL, sent_setup, NULL, NULL, NULL);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests:
2+
drivers.sent:
3+
tags:
4+
- drivers
5+
- sent
6+
depends_on: sent
7+
filter: dt_alias_exists("sent-node")

0 commit comments

Comments
 (0)