1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/ipc/ipc_service.h>
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_DECLARE(app);
11 
12 #define STACKSIZE	2048
13 #define PRIORITY	K_PRIO_PREEMPT(2)
14 
15 K_THREAD_STACK_DEFINE(ipc1_stack, STACKSIZE);
16 static volatile uint8_t ipc1_received_data;
17 static K_SEM_DEFINE(ipc1_bound_sem, 0, 1);
18 static K_SEM_DEFINE(ipc1_data_sem, 0, 1);
19 
20 /*
21  * ==> THREAD 1 (IPC instance 1) <==
22  */
23 
ipc1_ept_bound(void * priv)24 static void ipc1_ept_bound(void *priv)
25 {
26 	k_sem_give(&ipc1_bound_sem);
27 }
28 
ipc1_ept_recv(const void * data,size_t len,void * priv)29 static void ipc1_ept_recv(const void *data, size_t len, void *priv)
30 {
31 	ipc1_received_data = *((uint8_t *) data);
32 
33 	k_sem_give(&ipc1_data_sem);
34 }
35 
36 static struct ipc_ept_cfg ipc1_ept_cfg = {
37 	.name = "ipc1",
38 	.cb = {
39 		.bound    = ipc1_ept_bound,
40 		.received = ipc1_ept_recv,
41 	},
42 };
43 
ipc1_entry(void * dummy0,void * dummy1,void * dummy2)44 static void ipc1_entry(void *dummy0, void *dummy1, void *dummy2)
45 {
46 	ARG_UNUSED(dummy0);
47 	ARG_UNUSED(dummy1);
48 	ARG_UNUSED(dummy2);
49 
50 	const struct device *ipc1_instance;
51 	unsigned char message = 0;
52 	struct ipc_ept ipc1_ept;
53 	int ret;
54 
55 	printk("IPC-service REMOTE [INST 1] demo started\n");
56 
57 	ipc1_instance = DEVICE_DT_GET(DT_NODELABEL(ipc1));
58 
59 	/*k_sleep(K_FOREVER);*/
60 	ret = ipc_service_open_instance(ipc1_instance);
61 	if (ret < 0 && ret != -EALREADY) {
62 		printk("ipc_service_open_instance() failure\n");
63 		return;
64 	}
65 
66 	LOG_INF("ipc open %d", ret);
67 
68 	ret = ipc_service_register_endpoint(ipc1_instance, &ipc1_ept, &ipc1_ept_cfg);
69 	if (ret < 0) {
70 		printf("ipc_service_register_endpoint() failure\n");
71 		return;
72 	}
73 
74 	LOG_INF("wait for bound");
75 	k_sem_take(&ipc1_bound_sem, K_FOREVER);
76 	LOG_INF("bounded");
77 
78 	while (message < 99) {
79 		k_sem_take(&ipc1_data_sem, K_FOREVER);
80 		message = ipc1_received_data;
81 
82 		LOG_INF("REMOTE [1]: %d", message);
83 
84 		message++;
85 
86 		ret = ipc_service_send(&ipc1_ept, &message, sizeof(message));
87 		if (ret < 0) {
88 			printk("send_message(%d) failed with ret %d\n", message, ret);
89 			break;
90 		}
91 	}
92 
93 	printk("IPC-service REMOTE [INST 1] demo ended.\n");
94 }
95 K_THREAD_DEFINE(ipc1_thread_id, STACKSIZE, ipc1_entry, NULL, NULL, NULL, PRIORITY, 0, 0);
96