1 /*
2  * Copyright 2024-2025 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/mbox.h>
12 
13 #include <zephyr/ztest.h>
14 
15 static K_SEM_DEFINE(g_mbox_data_rx_sem, 0, 1);
16 
17 static uint32_t g_mbox_received_data;
18 static uint32_t g_mbox_expected_data;
19 static uint32_t g_mbox_received_channel;
20 static uint32_t g_mbox_expected_channel;
21 
22 static bool g_received_size_error;
23 static size_t g_received_size;
24 static int g_max_transfer_size_bytes;
25 
26 #define CHANNELS_TO_TEST 4
27 #define TX_CHANNEL_INDEX 0
28 #define RX_CHANNEL_INDEX 1
29 
30 static const struct mbox_dt_spec channels[CHANNELS_TO_TEST][2] = {
31 	{
32 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx0),
33 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx0),
34 	},
35 	{
36 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx1),
37 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx1),
38 	},
39 	{
40 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx2),
41 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx2),
42 	},
43 	{
44 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx3),
45 		MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx3),
46 	},
47 };
48 
49 static uint32_t current_channel_index;
50 
callback(const struct device * dev,uint32_t channel,void * user_data,struct mbox_msg * data)51 static void callback(const struct device *dev, uint32_t channel, void *user_data,
52 		     struct mbox_msg *data)
53 {
54 	/* Handle the case if received invalid size */
55 	if (data->size > sizeof(g_mbox_received_data)) {
56 		g_received_size_error = true;
57 		g_received_size = data->size;
58 	} else {
59 		memcpy(&g_mbox_received_data, data->data, data->size);
60 	}
61 
62 	g_mbox_received_channel = channel;
63 
64 	k_sem_give(&g_mbox_data_rx_sem);
65 }
66 
mbox_data_tests_before(void * f)67 static void mbox_data_tests_before(void *f)
68 {
69 	zassert_false(current_channel_index >= CHANNELS_TO_TEST, "Channel to test is out of range");
70 
71 	const struct mbox_dt_spec *tx_channel = &channels[current_channel_index][TX_CHANNEL_INDEX];
72 	const struct mbox_dt_spec *rx_channel = &channels[current_channel_index][RX_CHANNEL_INDEX];
73 	int ret_val = 0;
74 
75 	g_max_transfer_size_bytes = mbox_mtu_get_dt(tx_channel);
76 	/* Test currently supports only transfer size up to 4 bytes */
77 	if ((g_max_transfer_size_bytes < 0) || (g_max_transfer_size_bytes > 4)) {
78 		printk("mbox_mtu_get() error\n");
79 		zassert_false(1, "mbox invalid maximum transfer unit: %d",
80 			      g_max_transfer_size_bytes);
81 	}
82 
83 	ret_val = mbox_register_callback_dt(rx_channel, callback, NULL);
84 	zassert_false(ret_val != 0, "mbox failed to register callback. ret_val: %d", ret_val);
85 
86 	ret_val = mbox_set_enabled_dt(rx_channel, 1);
87 	zassert_false(ret_val != 0, "mbox failed to enable mbox. ret_val: %d", ret_val);
88 }
89 
mbox_data_tests_after(void * f)90 static void mbox_data_tests_after(void *f)
91 {
92 	zassert_false(current_channel_index >= CHANNELS_TO_TEST, "Channel to test is out of range");
93 
94 	const struct mbox_dt_spec *rx_channel = &channels[current_channel_index][RX_CHANNEL_INDEX];
95 
96 	/* Disable channel after test end */
97 	int ret_val = mbox_set_enabled_dt(rx_channel, 0);
98 
99 	zassert_false(ret_val != 0, "mbox failed to disable mbox. ret_val: %d", ret_val);
100 
101 	/* Increment current channel index to its prepared for next test */
102 	current_channel_index++;
103 }
104 
mbox_test(const uint32_t data)105 static void mbox_test(const uint32_t data)
106 {
107 	struct mbox_msg msg = {0};
108 	uint32_t test_data = data;
109 	int test_count = 0;
110 	int ret_val = 0;
111 
112 	while (test_count < 100) {
113 		const struct mbox_dt_spec *tx_channel =
114 			&channels[current_channel_index][TX_CHANNEL_INDEX];
115 
116 		/* Main core prepare test data */
117 		msg.data = &test_data;
118 		msg.size = g_max_transfer_size_bytes;
119 
120 		/* Main core send test data */
121 		ret_val = mbox_send_dt(tx_channel, &msg);
122 		zassert_false(ret_val < 0, "mbox failed to send. ret_val: %d", ret_val);
123 
124 		/*
125 		 * Determine expected received data based on the configured Maximum
126 		 * Transfer Unit (MTU). Supported MTU sizes are 1, 2, 3, and 4 bytes.
127 		 * If CONFIG_TEST_SINGLE_CPU is enabled, the received data should match
128 		 * the sent data. Otherwise, it is expected to be incremented by one.
129 		 */
130 		g_mbox_expected_data = test_data & ~(0xFFFFFFFF << (g_max_transfer_size_bytes * 8));
131 #ifndef CONFIG_TEST_SINGLE_CPU
132 		g_mbox_expected_data++;
133 #endif
134 
135 		k_sem_take(&g_mbox_data_rx_sem, K_FOREVER);
136 
137 		if (g_received_size_error) {
138 			zassert_false(1, "mbox received invalid size in callback: %d",
139 				      g_received_size);
140 		}
141 
142 		test_data = g_mbox_received_data;
143 
144 		/* Main core check received data */
145 		zassert_equal(g_mbox_expected_data, test_data,
146 			      "Received test_data does not match!: Expected: %08X, Got: %08X",
147 			      g_mbox_expected_data, test_data);
148 
149 		/* Expect reception of data on current RX channel */
150 		g_mbox_expected_channel =
151 			channels[current_channel_index][RX_CHANNEL_INDEX].channel_id;
152 		zassert_equal(g_mbox_expected_channel, g_mbox_received_channel,
153 			      "Received channel does not match!: Expected: %d, Got: %d",
154 			      g_mbox_expected_channel, g_mbox_received_channel);
155 
156 		/* Increment for next send */
157 		test_data++;
158 		test_count++;
159 	}
160 }
161 
162 /**
163  * @brief MBOX Data transfer by ping pong for first set of channels
164  *
165  * This test verifies that the data transfer via MBOX.
166  * Main core will transfer test data to remote core.
167  * Remote core will increment data by one and transfer it back to Main core.
168  * Main core will check that data it sent to remote core was incremented by one.
169  * Main core will again increment test data by one, send it to remote core and repeat 100 times.
170  */
ZTEST(mbox_data_tests,test_ping_pong_1)171 ZTEST(mbox_data_tests, test_ping_pong_1)
172 {
173 	mbox_test(0xADADADAD);
174 }
175 
176 /**
177  * @brief MBOX Data transfer by ping pong for second set of channels
178  *
179  * Description same as for test_ping_pong_1
180  *
181  */
ZTEST(mbox_data_tests,test_ping_pong_2)182 ZTEST(mbox_data_tests, test_ping_pong_2)
183 {
184 	mbox_test(0xDADADADA);
185 }
186 
187 /**
188  * @brief MBOX Data transfer by ping pong for third set of channels
189  *
190  * Description same as for test_ping_pong_1
191  *
192  */
ZTEST(mbox_data_tests,test_ping_pong_3)193 ZTEST(mbox_data_tests, test_ping_pong_3)
194 {
195 	mbox_test(0xADADADAD);
196 }
197 
198 /**
199  * @brief MBOX Data transfer by ping pong for forth set of channels
200  *
201  * Description same as for test_ping_pong_1
202  *
203  */
ZTEST(mbox_data_tests,test_ping_pong_4)204 ZTEST(mbox_data_tests, test_ping_pong_4)
205 {
206 	mbox_test(0xDADADADA);
207 }
208 
209 ZTEST_SUITE(mbox_data_tests, NULL, NULL, mbox_data_tests_before, mbox_data_tests_after, NULL);
210