0590ba4be9d9f3445da6f564297d60b3f74e55e1
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 "openamp/rpmsg_retarget.h"
14 #ifdef ZYNQ7_BAREMETAL
15 #include "baremetal.h"
16 #endif
18 /* Internal functions */
19 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
20 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
21 static void rpmsg_read_cb(struct rpmsg_channel *, void *, int, void *,
22 unsigned long);
23 static void shutdown_cb(struct rpmsg_channel *rp_chnl);
25 /* Globals */
26 static struct rpmsg_channel *app_rp_chnl;
27 volatile int chnl_cb_flag = 0;
28 static struct remote_proc *proc = NULL;
29 static struct rsc_table_info rsc_info;
30 extern const struct remote_resource_table resources;
31 extern struct hil_proc proc_table[];
33 /* External functions */
34 extern void init_system();
35 extern void cleanup_system();
37 #define REDEF_O_CREAT 100
38 #define REDEF_O_EXCL 200
39 #define REDEF_O_RDONLY 0
40 #define REDEF_O_WRONLY 1
41 #define REDEF_O_RDWR 2
42 #define REDEF_O_APPEND 2000
43 #define REDEF_O_ACCMODE 3
45 #define RPC_CHANNEL_READY_TO_CLOSE "rpc_channel_ready_to_close"
47 /* Application entry point */
48 int main()
49 {
50 int fd, bytes_written, bytes_read;
51 char fname[] = "remote.file";
52 char wbuff[50];
53 char rbuff[1024];
54 char ubuff[50];
55 float fdata;
56 int idata;
57 int ret;
58 int status;
60 #ifdef ZYNQ7_BAREMETAL
61 SWITCH_TO_SYS_MODE();
62 #endif
64 /* Initialize HW system components */
65 init_system();
67 rsc_info.rsc_tab = (struct resource_table *)&resources;
68 rsc_info.size = sizeof(resources);
70 /* Initialize RPMSG framework */
71 status = remoteproc_resource_init(&rsc_info, &proc_table[0],
72 rpmsg_channel_created,
73 rpmsg_channel_deleted, rpmsg_read_cb,
74 &proc, 0);
75 if (status < 0) {
76 return -1;
77 }
79 while (!chnl_cb_flag) {
80 __asm__("\
81 wfi\n\t");
82 }
84 chnl_cb_flag = 0;
86 rpmsg_retarget_init(app_rp_chnl, shutdown_cb);
88 printf
89 ("\r\nRemote>Baremetal Remote Procedure Call (RPC) Demonstration\r\n");
90 printf
91 ("\r\nRemote>***************************************************\r\n");
92 printf
93 ("\r\nRemote>Rpmsg based retargetting to proxy initialized..\r\n");
95 /* Remote performing file IO on Master */
96 printf("\r\nRemote>FileIO demo ..\r\n");
98 printf("\r\nRemote>Creating a file on master and writing to it..\r\n");
99 fd = open(fname, REDEF_O_CREAT | REDEF_O_WRONLY | REDEF_O_APPEND,
100 S_IRUSR | S_IWUSR);
101 printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
103 sprintf(wbuff, "This is a test string being written to file..");
104 bytes_written = write(fd, wbuff, strlen(wbuff));
105 printf("\r\nRemote>Wrote to fd = %d, size = %d, content = %s\r\n", fd,
106 bytes_written, wbuff);
107 close(fd);
108 printf("\r\nRemote>Closed fd = %d\r\n", fd);
110 /* Remote performing file IO on Master */
111 printf
112 ("\r\nRemote>Reading a file on master and displaying its contents..\r\n");
113 fd = open(fname, REDEF_O_RDONLY, S_IRUSR | S_IWUSR);
114 printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
115 bytes_read = read(fd, rbuff, 1024);
116 *(char *)(&rbuff[0] + bytes_read + 1) = 0;
117 printf
118 ("\r\nRemote>Read from fd = %d, size = %d, printing contents below .. %s\r\n",
119 fd, bytes_read, rbuff);
120 close(fd);
121 printf("\r\nRemote>Closed fd = %d\r\n", fd);
123 while (1) {
124 /* Remote performing STDIO on Master */
125 printf
126 ("\r\nRemote>Remote firmware using scanf and printf ..\r\n");
127 printf("\r\nRemote>Scanning user input from master..\r\n");
128 printf("\r\nRemote>Enter name\r\n");
129 ret = scanf("%s", ubuff);
130 if (ret) {
131 printf("\r\nRemote>Enter age\r\n");
132 ret = scanf("%d", &idata);
133 if (ret) {
134 printf("\r\nRemote>Enter value for pi\r\n");
135 ret = scanf("%f", &fdata);
136 if (ret) {
137 printf
138 ("\r\nRemote>User name = '%s'\r\n",
139 ubuff);
140 printf("\r\nRemote>User age = '%d'\r\n",
141 idata);
142 printf
143 ("\r\nRemote>User entered value of pi = '%f'\r\n",
144 fdata);
145 }
146 }
147 }
148 if (!ret) {
149 scanf("%s", ubuff);
150 printf("Remote> Invalid value. Starting again....");
151 } else {
152 printf
153 ("\r\nRemote>Repeat demo ? (enter yes or no) \r\n");
154 scanf("%s", ubuff);
155 if ((strcmp(ubuff, "no")) && (strcmp(ubuff, "yes"))) {
156 printf
157 ("\r\nRemote>Invalid option. Starting again....\r\n");
158 } else if ((!strcmp(ubuff, "no"))) {
159 printf
160 ("\r\nRemote>RPC retargetting quitting ...\r\n");
161 sprintf(wbuff, RPC_CHANNEL_READY_TO_CLOSE);
162 rpmsg_retarget_send(wbuff,
163 sizeof
164 (RPC_CHANNEL_READY_TO_CLOSE)
165 + 1);
166 break;
167 }
168 }
169 }
170 printf
171 ("\r\nRemote> Firmware's rpmsg-openamp-demo-channel going down! \r\n");
173 while (1) {
174 __asm__("\
175 wfi\n\t");
176 }
178 return 0;
179 }
181 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
182 {
183 app_rp_chnl = rp_chnl;
184 chnl_cb_flag = 1;
185 }
187 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
188 {
189 }
191 static void rpmsg_read_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
192 void *priv, unsigned long src)
193 {
194 }
196 static void shutdown_cb(struct rpmsg_channel *rp_chnl)
197 {
198 rpmsg_retarget_deinit(rp_chnl);
199 remoteproc_resource_deinit(proc);
200 cleanup_system();
201 }