1 /*
2  * Copyright 2024 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", 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 		/* Expect next received data will be incremented by one.
125 		 * And based on Maximum Transfer Unit determine expected data.
126 		 * Currently supported MTU's are 1, 2, 3, and 4 bytes.
127 		 */
128 		g_mbox_expected_data = test_data & ~(0xFFFFFFFF << (g_max_transfer_size_bytes * 8));
129 		g_mbox_expected_data++;
130 
131 		k_sem_take(&g_mbox_data_rx_sem, K_FOREVER);
132 
133 		if (g_received_size_error) {
134 			zassert_false(1, "mbox received invalid size in callback: %d",
135 				      g_received_size);
136 		}
137 
138 		test_data = g_mbox_received_data;
139 
140 		/* Main core check received data */
141 		zassert_equal(g_mbox_expected_data, test_data,
142 			      "Received test_data does not match!: Expected: %08X, Got: %08X",
143 			      g_mbox_expected_data, test_data);
144 
145 		/* Expect reception of data on current RX channel */
146 		g_mbox_expected_channel =
147 			channels[current_channel_index][RX_CHANNEL_INDEX].channel_id;
148 		zassert_equal(g_mbox_expected_channel, g_mbox_received_channel,
149 			      "Received channel does not match!: Expected: %d, Got: %d",
150 			      g_mbox_expected_channel, g_mbox_received_channel);
151 
152 		/* Increment for next send */
153 		test_data++;
154 		test_count++;
155 	}
156 }
157 
158 /**
159  * @brief MBOX Data transfer by ping pong for first set of channels
160  *
161  * This test verifies that the data transfer via MBOX.
162  * Main core will transfer test data to remote core.
163  * Remote core will increment data by one and transfer it back to Main core.
164  * Main core will check that data it sent to remote core was incremented by one.
165  * Main core will again increment test data by one, send it to remote core and repeat 100 times.
166  */
ZTEST(mbox_data_tests,test_ping_pong_1)167 ZTEST(mbox_data_tests, test_ping_pong_1)
168 {
169 	mbox_test(0xADADADAD);
170 }
171 
172 /**
173  * @brief MBOX Data transfer by ping pong for second set of channels
174  *
175  * Description same as for test_ping_pong_1
176  *
177  */
ZTEST(mbox_data_tests,test_ping_pong_2)178 ZTEST(mbox_data_tests, test_ping_pong_2)
179 {
180 	mbox_test(0xDADADADA);
181 }
182 
183 /**
184  * @brief MBOX Data transfer by ping pong for third set of channels
185  *
186  * Description same as for test_ping_pong_1
187  *
188  */
ZTEST(mbox_data_tests,test_ping_pong_3)189 ZTEST(mbox_data_tests, test_ping_pong_3)
190 {
191 	mbox_test(0xADADADAD);
192 }
193 
194 /**
195  * @brief MBOX Data transfer by ping pong for forth set of channels
196  *
197  * Description same as for test_ping_pong_1
198  *
199  */
ZTEST(mbox_data_tests,test_ping_pong_4)200 ZTEST(mbox_data_tests, test_ping_pong_4)
201 {
202 	mbox_test(0xDADADADA);
203 }
204 
205 ZTEST_SUITE(mbox_data_tests, NULL, NULL, mbox_data_tests_before, mbox_data_tests_after, NULL);
206