1 /*
2  * Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/mbox.h>
9 #include <zephyr/sys/printk.h>
10 
11 #if !defined(CONFIG_RX_ENABLED) && !defined(CONFIG_TX_ENABLED)
12 #error "At least one of CONFIG_RX_ENABLED or CONFIG_TX_ENABLED must be set"
13 #endif
14 
15 #ifdef CONFIG_RX_ENABLED
callback(const struct device * dev,mbox_channel_id_t channel_id,void * user_data,struct mbox_msg * data)16 static void callback(const struct device *dev, mbox_channel_id_t channel_id,
17 		     void *user_data, struct mbox_msg *data)
18 {
19 	printk("Pong (on channel %d)\n", channel_id);
20 }
21 #endif /* CONFIG_RX_ENABLED */
22 
main(void)23 int main(void)
24 {
25 	int ret;
26 
27 	printk("Hello from REMOTE - %s\n", CONFIG_BOARD_TARGET);
28 
29 #ifdef CONFIG_RX_ENABLED
30 	const struct mbox_dt_spec rx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx);
31 
32 	printk("Maximum RX channels: %d\n", mbox_max_channels_get_dt(&rx_channel));
33 
34 	ret = mbox_register_callback_dt(&rx_channel, callback, NULL);
35 	if (ret < 0) {
36 		printk("Could not register callback (%d)\n", ret);
37 		return 0;
38 	}
39 
40 	ret = mbox_set_enabled_dt(&rx_channel, true);
41 	if (ret < 0) {
42 		printk("Could not enable RX channel %d (%d)\n", rx_channel.channel_id, ret);
43 		return 0;
44 	}
45 #endif /* CONFIG_RX_ENABLED */
46 
47 #ifdef CONFIG_TX_ENABLED
48 	const struct mbox_dt_spec tx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx);
49 
50 	printk("Maximum bytes of data in the TX message: %d\n", mbox_mtu_get_dt(&tx_channel));
51 	printk("Maximum TX channels: %d\n", mbox_max_channels_get_dt(&tx_channel));
52 
53 	while (1) {
54 #if defined(CONFIG_MULTITHREADING)
55 		k_sleep(K_MSEC(3000));
56 #else
57 		k_busy_wait(3000000);
58 #endif
59 
60 		printk("Ping (on channel %d)\n", tx_channel.channel_id);
61 
62 		ret = mbox_send_dt(&tx_channel, NULL);
63 		if (ret < 0) {
64 			printk("Could not send (%d)\n", ret);
65 			return 0;
66 		}
67 	}
68 #endif /* CONFIG_TX_ENABLED */
69 	return 0;
70 }
71