Skip to content

Commit 4cf05d0

Browse files
committed
samples: boards: nxp: s32: create sample mbox
The sample supports ping-pong loopback data on a MBOX channel within one core. The expected received data must match the sent data when running the sample. Signed-off-by: Cong Nguyen Huu <cong.nguyenhuu@nxp.com>
1 parent af46f62 commit 4cf05d0

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(mbox)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. zephyr:code-sample:: nxp_s32_mbox
2+
:name: NXP S32 MBOX
3+
4+
Perform NXP S32 mailbox communication using the MBOX API with data.
5+
6+
Overview
7+
********
8+
9+
The sample supports ping-pong loopback data on a mbox channel within one core.
10+
11+
Building and Running
12+
********************
13+
14+
Building and running the sample, example for s32z2xxdc2/s32z270/rtu0:
15+
16+
.. zephyr-app-commands::
17+
:zephyr-app: samples/boards/nxp/s32/mbox
18+
:board: s32z2xxdc2/s32z270/rtu0
19+
:goals: build flash
20+
21+
Results from running the sample, example for s32z2xxdc2/s32z270/rtu0:
22+
23+
.. code-block:: console
24+
25+
*** Booting Zephyr OS build v4.1.0-3644-ga4ee89a96597 ***
26+
mbox_data demo started
27+
send (on channel 0) value: 0
28+
received (on channel 0) value: 0
29+
send (on channel 0) value: 1
30+
received (on channel 0) value: 1
31+
...
32+
send (on channel 0) value: 98
33+
received (on channel 0) value: 98
34+
send (on channel 0) value: 99
35+
received (on channel 0) value: 99
36+
mbox_data demo ended
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
mbox-consumer {
9+
compatible = "vnd,mbox-consumer";
10+
mboxes = <&mru0 0>, <&mru0 0>;
11+
mbox-names = "tx", "rx";
12+
};
13+
};
14+
15+
&mru0 {
16+
rx-channels = <1>;
17+
status = "okay";
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
mbox-consumer {
9+
compatible = "vnd,mbox-consumer";
10+
mboxes = <&mru4 0>, <&mru4 0>;
11+
mbox-names = "tx", "rx";
12+
};
13+
};
14+
15+
&mru4 {
16+
rx-channels = <1>;
17+
status = "okay";
18+
};

samples/boards/nxp/s32/mbox/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_PRINTK=y
2+
CONFIG_MBOX=y
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
sample:
2+
name: NXP S32 MBOX sample
3+
tests:
4+
sample.boards.nxp_s32.mbox:
5+
tags: mbox
6+
filter: dt_compat_enabled("vnd,mbox-consumer")
7+
platform_allow:
8+
- s32z2xxdc2/s32z270/rtu0
9+
- s32z2xxdc2/s32z270/rtu1
10+
- s32z2xxdc2@D/s32z270/rtu0
11+
- s32z2xxdc2@D/s32z270/rtu1
12+
harness: console
13+
harness_config:
14+
type: multi_line
15+
ordered: false
16+
regex:
17+
- "send .+ value: 0"
18+
- "received .+ value: 0"
19+
- "send .+ value: 1"
20+
- "received .+ value: 1"
21+
- "send .+ value: 41"
22+
- "received .+ value: 41"
23+
- "send .+ value: 42"
24+
- "received .+ value: 42"
25+
- "send .+ value: 98"
26+
- "received .+ value: 98"
27+
- "send .+ value: 99"
28+
- "received .+ value: 99"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/drivers/mbox.h>
9+
10+
static K_SEM_DEFINE(g_mbox_data_rx_sem, 0, 1);
11+
12+
static mbox_channel_id_t g_mbox_received_data;
13+
static mbox_channel_id_t g_mbox_received_channel;
14+
15+
static void callback(const struct device *dev, mbox_channel_id_t channel_id, void *user_data,
16+
struct mbox_msg *data)
17+
{
18+
memcpy(&g_mbox_received_data, data->data, data->size);
19+
g_mbox_received_channel = channel_id;
20+
21+
k_sem_give(&g_mbox_data_rx_sem);
22+
}
23+
24+
int main(void)
25+
{
26+
const struct mbox_dt_spec tx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx);
27+
const struct mbox_dt_spec rx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx);
28+
struct mbox_msg msg = {0};
29+
uint32_t message = 0;
30+
31+
printk("mbox_data demo started\n");
32+
33+
const int max_transfer_size_bytes = mbox_mtu_get_dt(&tx_channel);
34+
/* Sample currently supports only transfer size up to 4 bytes */
35+
if ((max_transfer_size_bytes < 0) || (max_transfer_size_bytes > 4)) {
36+
printk("mbox_mtu_get() error\n");
37+
return 0;
38+
}
39+
40+
if (mbox_register_callback_dt(&rx_channel, callback, NULL)) {
41+
printk("mbox_register_callback() error\n");
42+
return 0;
43+
}
44+
45+
if (mbox_set_enabled_dt(&rx_channel, 1)) {
46+
printk("mbox_set_enable() error\n");
47+
return 0;
48+
}
49+
50+
while (message < 100) {
51+
msg.data = &message;
52+
msg.size = max_transfer_size_bytes;
53+
54+
printk("send (on channel %d) value: %d\n", tx_channel.channel_id, message);
55+
if (mbox_send_dt(&tx_channel, &msg) < 0) {
56+
printk("mbox_send() error\n");
57+
return 0;
58+
}
59+
60+
k_sem_take(&g_mbox_data_rx_sem, K_FOREVER);
61+
message = g_mbox_received_data;
62+
63+
printk("received (on channel %d) value: %d\n", g_mbox_received_channel,
64+
message);
65+
message++;
66+
}
67+
68+
printk("mbox_data demo ended\n");
69+
return 0;
70+
}

0 commit comments

Comments
 (0)