|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (c) 2018, NXP | 
|  | 3 | + * Copyright (c) 2018-2019, Linaro Limited | 
|  | 4 | + * Copyright (c) 2018-2021, Nordic Semiconductor ASA | 
|  | 5 | + * | 
|  | 6 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +#include <zephyr.h> | 
|  | 10 | +#include <drivers/ipm.h> | 
|  | 11 | +#include <sys/printk.h> | 
|  | 12 | +#include <device.h> | 
|  | 13 | +#include <stdio.h> | 
|  | 14 | +#include <stdlib.h> | 
|  | 15 | +#include <string.h> | 
|  | 16 | + | 
|  | 17 | +#include <ipc/rpmsg_service.h> | 
|  | 18 | + | 
|  | 19 | +#define APP_TASK_STACK_SIZE (1024) | 
|  | 20 | +K_THREAD_STACK_DEFINE(thread_stack, APP_TASK_STACK_SIZE); | 
|  | 21 | +static struct k_thread thread_data; | 
|  | 22 | + | 
|  | 23 | +static volatile unsigned int received_data; | 
|  | 24 | + | 
|  | 25 | +static K_SEM_DEFINE(data_sem, 0, 1); | 
|  | 26 | +static K_SEM_DEFINE(data_rx_sem, 0, 1); | 
|  | 27 | + | 
|  | 28 | +int endpoint_cb(struct rpmsg_endpoint *ept, void *data, | 
|  | 29 | +		size_t len, uint32_t src, void *priv) | 
|  | 30 | +{ | 
|  | 31 | +	received_data = *((unsigned int *) data); | 
|  | 32 | + | 
|  | 33 | +	k_sem_give(&data_rx_sem); | 
|  | 34 | + | 
|  | 35 | +	return RPMSG_SUCCESS; | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +static int ep_id; | 
|  | 39 | + | 
|  | 40 | +static unsigned int receive_message(void) | 
|  | 41 | +{ | 
|  | 42 | +	k_sem_take(&data_rx_sem, K_FOREVER); | 
|  | 43 | +	return received_data; | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +static int send_message(unsigned int message) | 
|  | 47 | +{ | 
|  | 48 | +	return rpmsg_service_send(ep_id, &message, sizeof(message)); | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +void app_task(void *arg1, void *arg2, void *arg3) | 
|  | 52 | +{ | 
|  | 53 | +	ARG_UNUSED(arg1); | 
|  | 54 | +	ARG_UNUSED(arg2); | 
|  | 55 | +	ARG_UNUSED(arg3); | 
|  | 56 | +	int status = 0; | 
|  | 57 | +	unsigned int message = 0U; | 
|  | 58 | + | 
|  | 59 | +	printk("\r\nRPMsg Service [remote] demo started\r\n"); | 
|  | 60 | + | 
|  | 61 | +	while (message < 99) { | 
|  | 62 | +		message = receive_message(); | 
|  | 63 | +		printk("Remote core received a message: %d\n", message); | 
|  | 64 | + | 
|  | 65 | +		message++; | 
|  | 66 | +		status = send_message(message); | 
|  | 67 | +		if (status < 0) { | 
|  | 68 | +			printk("send_message(%d) failed with status %d\n", | 
|  | 69 | +			       message, status); | 
|  | 70 | +			break; | 
|  | 71 | +		} | 
|  | 72 | +	} | 
|  | 73 | + | 
|  | 74 | +	printk("RPMsg Service demo ended.\n"); | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +void main(void) | 
|  | 78 | +{ | 
|  | 79 | +	printk("Starting application thread!\n"); | 
|  | 80 | +	k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE, | 
|  | 81 | +			(k_thread_entry_t)app_task, | 
|  | 82 | +			NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT); | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +/* Make sure we register endpoint before RPMsg Service is initialized. */ | 
|  | 86 | +int register_endpoint(const struct device *arg) | 
|  | 87 | +{ | 
|  | 88 | +	int status; | 
|  | 89 | + | 
|  | 90 | +	status = rpmsg_service_register_endpoint("demo", endpoint_cb); | 
|  | 91 | + | 
|  | 92 | +	if (status < 0) { | 
|  | 93 | +		printk("rpmsg_create_ept failed %d\n", status); | 
|  | 94 | +		return status; | 
|  | 95 | +	} | 
|  | 96 | + | 
|  | 97 | +	ep_id = status; | 
|  | 98 | + | 
|  | 99 | +	return 0; | 
|  | 100 | +} | 
|  | 101 | + | 
|  | 102 | +SYS_INIT(register_endpoint, POST_KERNEL, CONFIG_RPMSG_SERVICE_EP_REG_PRIORITY); | 
0 commit comments