|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, NXP |
| 3 | + * Copyright (c) 2018, Nordic Semiconductor ASA |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <zephyr.h> |
| 9 | +#include <misc/printk.h> |
| 10 | +#include <device.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +#include <openamp/open_amp.h> |
| 16 | + |
| 17 | +#include "platform.h" |
| 18 | +#include "resource_table.h" |
| 19 | + |
| 20 | +#define APP_TASK_STACK_SIZE (256) |
| 21 | +K_THREAD_STACK_DEFINE(thread_stack, APP_TASK_STACK_SIZE); |
| 22 | +static struct k_thread thread_data; |
| 23 | + |
| 24 | +static struct rpmsg_channel *rp_channel; |
| 25 | +static struct rpmsg_endpoint *rp_endpoint; |
| 26 | + |
| 27 | +static K_SEM_DEFINE(channel_created, 0, 1); |
| 28 | + |
| 29 | +static K_SEM_DEFINE(message_received, 0, 1); |
| 30 | +static volatile unsigned int received_data; |
| 31 | + |
| 32 | +static struct rsc_table_info rsc_info; |
| 33 | +static struct hil_proc *proc; |
| 34 | + |
| 35 | +static void rpmsg_recv_callback(struct rpmsg_channel *channel, void *data, |
| 36 | + int data_length, void *private, unsigned long src) |
| 37 | +{ |
| 38 | + received_data = *((unsigned int *) data); |
| 39 | + k_sem_give(&message_received); |
| 40 | +} |
| 41 | + |
| 42 | +static void rpmsg_channel_created(struct rpmsg_channel *channel) |
| 43 | +{ |
| 44 | + rp_channel = channel; |
| 45 | + rp_endpoint = rpmsg_create_ept(rp_channel, rpmsg_recv_callback, RPMSG_NULL, RPMSG_ADDR_ANY); |
| 46 | + k_sem_give(&channel_created); |
| 47 | +} |
| 48 | + |
| 49 | +static void rpmsg_channel_deleted(struct rpmsg_channel *channel) |
| 50 | +{ |
| 51 | + rpmsg_destroy_ept(rp_endpoint); |
| 52 | +} |
| 53 | + |
| 54 | +static unsigned int receive_message(void) |
| 55 | +{ |
| 56 | + while (k_sem_take(&message_received, K_NO_WAIT) != 0) |
| 57 | + hil_poll(proc, 0); |
| 58 | + return received_data; |
| 59 | +} |
| 60 | + |
| 61 | +static int send_message(unsigned int message) |
| 62 | +{ |
| 63 | + return rpmsg_send(rp_channel, &message, sizeof(message)); |
| 64 | +} |
| 65 | + |
| 66 | +void app_task(void *arg1, void *arg2, void *arg3) |
| 67 | +{ |
| 68 | + ARG_UNUSED(arg1); |
| 69 | + ARG_UNUSED(arg2); |
| 70 | + ARG_UNUSED(arg3); |
| 71 | + |
| 72 | + printk("\r\nOpenAMP demo started\r\n"); |
| 73 | + |
| 74 | + struct metal_init_params metal_params = METAL_INIT_DEFAULTS; |
| 75 | + metal_init(&metal_params); |
| 76 | + |
| 77 | + proc = platform_init(RPMSG_MASTER); |
| 78 | + if (proc == NULL) { |
| 79 | + printk("platform_init() failed\n"); |
| 80 | + goto _cleanup; |
| 81 | + } |
| 82 | + |
| 83 | + resource_table_init((void **) &rsc_info.rsc_tab, &rsc_info.size); |
| 84 | + |
| 85 | + struct remote_proc *rproc_ptr = NULL; |
| 86 | + int status = remoteproc_resource_init(&rsc_info, proc, |
| 87 | + rpmsg_channel_created, rpmsg_channel_deleted, |
| 88 | + rpmsg_recv_callback, &rproc_ptr, RPMSG_MASTER); |
| 89 | + if (status != 0) { |
| 90 | + printk("remoteproc_resource_init() failed with status %d\n", status); |
| 91 | + goto _cleanup; |
| 92 | + } |
| 93 | + |
| 94 | + while (k_sem_take(&channel_created, K_NO_WAIT) != 0) |
| 95 | + hil_poll(proc, 0); |
| 96 | + |
| 97 | + unsigned int message = 0; |
| 98 | + status = send_message(message); |
| 99 | + if (status < 0) { |
| 100 | + printk("send_message(%d) failed with status %d\n", message, status); |
| 101 | + goto _cleanup; |
| 102 | + } |
| 103 | + |
| 104 | + while (message <= 100) { |
| 105 | + message = receive_message(); |
| 106 | + printk("Primary core received a message: %d\n", message); |
| 107 | + |
| 108 | + message++; |
| 109 | + status = send_message(message); |
| 110 | + if (status < 0) { |
| 111 | + printk("send_message(%d) failed with status %d\n", message, status); |
| 112 | + goto _cleanup; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +_cleanup: |
| 117 | + if (rproc_ptr) { |
| 118 | + remoteproc_resource_deinit(rproc_ptr); |
| 119 | + } |
| 120 | + metal_finish(); |
| 121 | + |
| 122 | + printk("OpenAMP demo ended.\n"); |
| 123 | +} |
| 124 | + |
| 125 | +void main(void) |
| 126 | +{ |
| 127 | + printk("Starting application thread!\n"); |
| 128 | + k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE, |
| 129 | + (k_thread_entry_t)app_task, |
| 130 | + NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); |
| 131 | +} |
0 commit comments