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