1 /*
2  * Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/drivers/ipm.h>
10 #include <zephyr/device.h>
11 #include <string.h>
12 
13 static const char request[] = {"PRO_CPU: request to APP_CPU"};
14 
15 static const struct device *ipm_dev;
16 static char received_string[64];
17 static struct k_sem sync;
18 
ipm_receive_callback(const struct device * ipmdev,void * user_data,uint32_t id,volatile void * data)19 static void ipm_receive_callback(const struct device *ipmdev, void *user_data, uint32_t id,
20 				 volatile void *data)
21 {
22 	ARG_UNUSED(ipmdev);
23 	ARG_UNUSED(user_data);
24 
25 	strncpy(received_string, (const char *)data, sizeof(received_string));
26 	k_sem_give(&sync);
27 }
28 
main(void)29 int main(void)
30 {
31 	int ret;
32 
33 	k_sem_init(&sync, 0, 1);
34 
35 	ipm_dev = DEVICE_DT_GET(DT_NODELABEL(ipm0));
36 	if (!ipm_dev) {
37 		printk("Failed to get IPM device.\n\r");
38 		return 0;
39 	}
40 
41 	ipm_register_callback(ipm_dev, ipm_receive_callback, NULL);
42 
43 	/* Workaround to catch up with APPCPU */
44 	k_sleep(K_MSEC(50));
45 
46 	while (1) {
47 		printk("PRO_CPU is sending a request, waiting remote response...\n\r");
48 
49 		ipm_send(ipm_dev, -1, sizeof(request), &request, sizeof(request));
50 
51 		ret = k_sem_take(&sync, K_MSEC(5000));
52 
53 		if (ret) {
54 			printk("No response from APP_CPU - trying again.\r\n");
55 		} else {
56 			printk("PRO_CPU received a message from APP_CPU : %s\n\r", received_string);
57 		}
58 
59 		k_sleep(K_MSEC(1000));
60 	}
61 	return 0;
62 }
63