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 #ifdef CONFIG_RX_ENABLED
callback(const struct device * dev,mbox_channel_id_t channel_id,void * user_data,struct mbox_msg * data)12 static void callback(const struct device *dev, mbox_channel_id_t channel_id,
13 void *user_data, struct mbox_msg *data)
14 {
15 printk("Pong (on channel %d)\n", channel_id);
16 }
17 #endif /* CONFIG_RX_ENABLED */
18
main(void)19 int main(void)
20 {
21 int ret;
22
23 printk("Hello from HOST - %s\n", CONFIG_BOARD_TARGET);
24
25 #ifdef CONFIG_RX_ENABLED
26 const struct mbox_dt_spec rx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), rx);
27
28 printk("Maximum RX channels: %d\n", mbox_max_channels_get_dt(&rx_channel));
29
30 ret = mbox_register_callback_dt(&rx_channel, callback, NULL);
31 if (ret < 0) {
32 printk("Could not register callback (%d)\n", ret);
33 return 0;
34 }
35
36 ret = mbox_set_enabled_dt(&rx_channel, true);
37 if (ret < 0) {
38 printk("Could not enable RX channel %d (%d)\n", rx_channel.channel_id, ret);
39 return 0;
40 }
41 #endif /* CONFIG_RX_ENABLED */
42
43 #ifdef CONFIG_TX_ENABLED
44 const struct mbox_dt_spec tx_channel = MBOX_DT_SPEC_GET(DT_PATH(mbox_consumer), tx);
45
46 printk("Maximum bytes of data in the TX message: %d\n", mbox_mtu_get_dt(&tx_channel));
47 printk("Maximum TX channels: %d\n", mbox_max_channels_get_dt(&tx_channel));
48
49 while (1) {
50 #if defined(CONFIG_MULTITHREADING)
51 k_sleep(K_MSEC(2000));
52 #else
53 k_busy_wait(2000000);
54 #endif
55
56 printk("Ping (on channel %d)\n", tx_channel.channel_id);
57
58 ret = mbox_send_dt(&tx_channel, NULL);
59 if (ret < 0) {
60 printk("Could not send (%d)\n", ret);
61 return 0;
62 }
63 }
64 #endif /* CONFIG_TX_ENABLED */
65 return 0;
66 }
67