|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr.h> |
| 8 | +#include <misc/printk.h> |
| 9 | +#include <device.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | +#include "rpmsg_lite.h" |
| 14 | +#include "rpmsg_queue.h" |
| 15 | +#include "rpmsg_ns.h" |
| 16 | + |
| 17 | +#define APP_TASK_STACK_SIZE (256) |
| 18 | +#define REMOTE_EPT_ADDR (30) |
| 19 | +#define LOCAL_EPT_ADDR (40) |
| 20 | + |
| 21 | +K_THREAD_STACK_DEFINE(thread_stack, APP_TASK_STACK_SIZE); |
| 22 | +static struct k_thread thread_data; |
| 23 | + |
| 24 | +#ifdef CPU_LPC54114J256BD64_cm4 |
| 25 | +#define RPMSG_LITE_LINK_ID (RL_PLATFORM_LPC5411x_M4_M0_LINK_ID) |
| 26 | +#define SH_MEM_TOTAL_SIZE (6144) |
| 27 | +#else |
| 28 | +#error Please define RPMSG_LITE_LINK_ID and SH_MEM_TOTAL_SIZE for the CPU used. |
| 29 | +#endif |
| 30 | + |
| 31 | +#if defined(__ICCARM__) /* IAR Workbench */ |
| 32 | +#pragma location = "rpmsg_sh_mem_section" |
| 33 | +char rpmsg_lite_base[SH_MEM_TOTAL_SIZE]; |
| 34 | +#elif defined(__GNUC__) /* LPCXpresso */ |
| 35 | +char rpmsg_lite_base[SH_MEM_TOTAL_SIZE] __attribute__(( |
| 36 | + section(".noinit.$rpmsg_sh_mem") |
| 37 | + )); |
| 38 | +#elif defined(__CC_ARM) /* Keil MDK */ |
| 39 | +char rpmsg_lite_base[SH_MEM_TOTAL_SIZE] __attribute__(( |
| 40 | + section("rpmsg_sh_mem_section") |
| 41 | + )); |
| 42 | +#else |
| 43 | +#error "RPMsg: Please provide your definition of rpmsg_lite_base[]!" |
| 44 | +#endif |
| 45 | + |
| 46 | +struct the_message { |
| 47 | + u32_t DATA; |
| 48 | +}; |
| 49 | + |
| 50 | +struct the_message volatile msg = {0}; |
| 51 | + |
| 52 | +void app_nameservice_isr_cb(unsigned int new_ept, const char *new_ept_name, |
| 53 | + unsigned long flags, void *user_data) |
| 54 | +{ |
| 55 | + unsigned long *data = (unsigned long *)user_data; |
| 56 | + |
| 57 | + *data = new_ept; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +void app_task(void *arg1, void *arg2, void *arg3) |
| 62 | +{ |
| 63 | + ARG_UNUSED(arg1); |
| 64 | + ARG_UNUSED(arg2); |
| 65 | + ARG_UNUSED(arg3); |
| 66 | + |
| 67 | + volatile unsigned long remote_addr = 0; |
| 68 | + struct rpmsg_lite_endpoint *my_ept; |
| 69 | + rpmsg_queue_handle my_queue; |
| 70 | + struct rpmsg_lite_instance *my_rpmsg; |
| 71 | + rpmsg_ns_handle ns_handle; |
| 72 | + int len; |
| 73 | + |
| 74 | + /* Print the initial banner */ |
| 75 | + printk("\r\nRPMsg demo starts\r\n"); |
| 76 | + |
| 77 | + my_rpmsg = rpmsg_lite_master_init(rpmsg_lite_base, SH_MEM_TOTAL_SIZE, |
| 78 | + RPMSG_LITE_LINK_ID, RL_NO_FLAGS); |
| 79 | + |
| 80 | + my_queue = rpmsg_queue_create(my_rpmsg); |
| 81 | + my_ept = rpmsg_lite_create_ept(my_rpmsg, LOCAL_EPT_ADDR, |
| 82 | + rpmsg_queue_rx_cb, my_queue); |
| 83 | + ns_handle = rpmsg_ns_bind(my_rpmsg, app_nameservice_isr_cb, |
| 84 | + (void *)&remote_addr); |
| 85 | + |
| 86 | + /* Wait until the secondary core application issues the nameservice isr |
| 87 | + * and the remote endpoint address is known. |
| 88 | + */ |
| 89 | + while (remote_addr == 0) { |
| 90 | + } |
| 91 | + |
| 92 | + /* Send the first message to the remoteproc */ |
| 93 | + msg.DATA = 0; |
| 94 | + rpmsg_lite_send(my_rpmsg, my_ept, remote_addr, (char *)&msg, |
| 95 | + sizeof(struct the_message), RL_DONT_BLOCK); |
| 96 | + |
| 97 | + while (msg.DATA <= 100) { |
| 98 | + rpmsg_queue_recv(my_rpmsg, my_queue, |
| 99 | + (unsigned long *)&remote_addr, (char *)&msg, |
| 100 | + sizeof(struct the_message), &len, RL_BLOCK); |
| 101 | + printk("Primary core received a msg\r\n"); |
| 102 | + printk("Message: Size=%x, DATA = %i\r\n", len, msg.DATA); |
| 103 | + msg.DATA++; |
| 104 | + |
| 105 | + rpmsg_lite_send(my_rpmsg, my_ept, remote_addr, (char *)&msg, |
| 106 | + sizeof(struct the_message), RL_BLOCK); |
| 107 | + } |
| 108 | + |
| 109 | + rpmsg_lite_destroy_ept(my_rpmsg, my_ept); |
| 110 | + my_ept = NULL; |
| 111 | + rpmsg_queue_destroy(my_rpmsg, my_queue); |
| 112 | + my_queue = NULL; |
| 113 | + rpmsg_ns_unbind(my_rpmsg, ns_handle); |
| 114 | + rpmsg_lite_deinit(my_rpmsg); |
| 115 | + |
| 116 | + /* Print the ending banner */ |
| 117 | + printk("\r\nRPMsg demo ends\r\n"); |
| 118 | + while (1) { |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void main(void) |
| 123 | +{ |
| 124 | + printk("===== app started ========\n"); |
| 125 | + |
| 126 | + k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE, |
| 127 | + (k_thread_entry_t)app_task, |
| 128 | + NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); |
| 129 | +} |
0 commit comments