33b9d9539d27eeea244978b2fdb9342493bdf7ca
1 /* This is a sample demonstration application that showcases usage of proxy from the remote core.
2 This application is meant to run on the remote CPU running baremetal.
3 This applicationr can print to to master console and perform file I/O using proxy mechanism. */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include "openamp/open_amp.h"
11 #include "rsc_table.h"
12 #include "platform_info.h"
13 #include "openamp/rpmsg_retarget.h"
15 /* Internal functions */
16 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
17 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
18 static void rpmsg_read_cb(struct rpmsg_channel *, void *, int, void *,
19 unsigned long);
20 static void shutdown_cb(struct rpmsg_channel *rp_chnl);
22 /* Globals */
23 static struct rpmsg_channel *app_rp_chnl;
24 volatile int chnl_cb_flag = 0;
25 static struct remote_proc *proc = NULL;
26 static struct rsc_table_info rsc_info;
27 extern const struct remote_resource_table resources;
28 extern struct rproc_info_plat_local proc_table;
30 /* External functions */
31 extern void init_system();
32 extern void cleanup_system();
34 #define REDEF_O_CREAT 100
35 #define REDEF_O_EXCL 200
36 #define REDEF_O_RDONLY 0
37 #define REDEF_O_WRONLY 1
38 #define REDEF_O_RDWR 2
39 #define REDEF_O_APPEND 2000
40 #define REDEF_O_ACCMODE 3
42 #define RPC_CHANNEL_READY_TO_CLOSE "rpc_channel_ready_to_close"
44 /* Application entry point */
45 int main()
46 {
47 int fd, bytes_written, bytes_read;
48 char fname[] = "remote.file";
49 char wbuff[50];
50 char rbuff[1024];
51 char ubuff[50];
52 float fdata;
53 int idata;
54 int ret;
55 int status;
57 /* Initialize HW system components */
58 init_system();
60 rsc_info.rsc_tab = (struct resource_table *)&resources;
61 rsc_info.size = sizeof(resources);
63 /* Initialize RPMSG framework */
64 status = remoteproc_resource_init(&rsc_info, &proc_table,
65 rpmsg_channel_created,
66 rpmsg_channel_deleted, rpmsg_read_cb,
67 &proc, 0);
68 if (status < 0) {
69 return -1;
70 }
72 while (!chnl_cb_flag) {
73 hil_poll(proc->proc, 0);
74 }
76 chnl_cb_flag = 0;
78 rpmsg_retarget_init(app_rp_chnl, shutdown_cb);
80 printf
81 ("\r\nRemote>Baremetal Remote Procedure Call (RPC) Demonstration\r\n");
82 printf
83 ("\r\nRemote>***************************************************\r\n");
84 printf
85 ("\r\nRemote>Rpmsg based retargetting to proxy initialized..\r\n");
87 /* Remote performing file IO on Master */
88 printf("\r\nRemote>FileIO demo ..\r\n");
90 printf("\r\nRemote>Creating a file on master and writing to it..\r\n");
91 fd = open(fname, REDEF_O_CREAT | REDEF_O_WRONLY | REDEF_O_APPEND,
92 S_IRUSR | S_IWUSR);
93 printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
95 sprintf(wbuff, "This is a test string being written to file..");
96 bytes_written = write(fd, wbuff, strlen(wbuff));
97 printf("\r\nRemote>Wrote to fd = %d, size = %d, content = %s\r\n", fd,
98 bytes_written, wbuff);
99 close(fd);
100 printf("\r\nRemote>Closed fd = %d\r\n", fd);
102 /* Remote performing file IO on Master */
103 printf
104 ("\r\nRemote>Reading a file on master and displaying its contents..\r\n");
105 fd = open(fname, REDEF_O_RDONLY, S_IRUSR | S_IWUSR);
106 printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
107 bytes_read = read(fd, rbuff, 1024);
108 *(char *)(&rbuff[0] + bytes_read + 1) = 0;
109 printf
110 ("\r\nRemote>Read from fd = %d, size = %d, printing contents below .. %s\r\n",
111 fd, bytes_read, rbuff);
112 close(fd);
113 printf("\r\nRemote>Closed fd = %d\r\n", fd);
115 while (1) {
116 /* Remote performing STDIO on Master */
117 printf
118 ("\r\nRemote>Remote firmware using scanf and printf ..\r\n");
119 printf("\r\nRemote>Scanning user input from master..\r\n");
120 printf("\r\nRemote>Enter name\r\n");
121 ret = scanf("%s", ubuff);
122 if (ret) {
123 printf("\r\nRemote>Enter age\r\n");
124 ret = scanf("%d", &idata);
125 if (ret) {
126 printf("\r\nRemote>Enter value for pi\r\n");
127 ret = scanf("%f", &fdata);
128 if (ret) {
129 printf
130 ("\r\nRemote>User name = '%s'\r\n",
131 ubuff);
132 printf("\r\nRemote>User age = '%d'\r\n",
133 idata);
134 printf
135 ("\r\nRemote>User entered value of pi = '%f'\r\n",
136 fdata);
137 }
138 }
139 }
140 if (!ret) {
141 scanf("%s", ubuff);
142 printf("Remote> Invalid value. Starting again....");
143 } else {
144 printf
145 ("\r\nRemote>Repeat demo ? (enter yes or no) \r\n");
146 scanf("%s", ubuff);
147 if ((strcmp(ubuff, "no")) && (strcmp(ubuff, "yes"))) {
148 printf
149 ("\r\nRemote>Invalid option. Starting again....\r\n");
150 } else if ((!strcmp(ubuff, "no"))) {
151 printf
152 ("\r\nRemote>RPC retargetting quitting ...\r\n");
153 sprintf(wbuff, RPC_CHANNEL_READY_TO_CLOSE);
154 break;
155 }
156 }
157 }
158 printf
159 ("\r\nRemote> Firmware's rpmsg-openamp-demo-channel going down! \r\n");
160 rpmsg_retarget_send(wbuff,
161 sizeof (RPC_CHANNEL_READY_TO_CLOSE) + 1);
163 while (chnl_cb_flag)
164 hil_poll(proc->proc, 0);
166 rpmsg_retarget_deinit(app_rp_chnl);
167 remoteproc_resource_deinit(proc);
168 cleanup_system();
169 return 0;
170 }
172 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
173 {
174 app_rp_chnl = rp_chnl;
175 chnl_cb_flag = 1;
176 }
178 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
179 {
180 app_rp_chnl = NULL;
181 }
183 static void rpmsg_read_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
184 void *priv, unsigned long src)
185 {
186 }
188 static void shutdown_cb(struct rpmsg_channel *rp_chnl)
189 {
190 chnl_cb_flag = 0;
191 }