-
Notifications
You must be signed in to change notification settings - Fork 278
/
echo_test.c
210 lines (175 loc) · 5.18 KB
/
echo_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* This is a sample demonstration application that showcases usage of rpmsg
This application is meant to run on the remote CPU running baremetal code.
This application echoes back data that was sent to it by the master core. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "openamp/open_amp.h"
#include "metal/alloc.h"
#include "rsc_table.h"
#define SHUTDOWN_MSG 0xEF56A55A
#define LPRINTF(format, ...) printf(format, ##__VA_ARGS__)
#define LPERROR(format, ...) LPRINTF("ERROR: " format, ##__VA_ARGS__)
struct _payload {
unsigned long num;
unsigned long size;
unsigned char data[];
};
static int err_cnt;
#define RPMSG_GET_KFIFO_SIZE 1
#define RPMSG_GET_AVAIL_DATA_SIZE 2
#define RPMSG_GET_FREE_SPACE 3
#define RPMSG_HEADER_LEN sizeof(struct rpmsg_hdr)
#define MAX_RPMSG_BUFF_SIZE (RPMSG_BUFFER_SIZE - RPMSG_HEADER_LEN)
#define PAYLOAD_MIN_SIZE 1
#define PAYLOAD_MAX_SIZE (MAX_RPMSG_BUFF_SIZE - 24)
#define NUM_PAYLOADS (PAYLOAD_MAX_SIZE/PAYLOAD_MIN_SIZE)
/* Internal functions */
static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
static void rpmsg_read_cb(struct rpmsg_channel *, void *, int, void *,
unsigned long);
/* Globals */
static struct rpmsg_channel *app_rp_chnl;
static struct rpmsg_endpoint *rp_ept;
static struct remote_proc *proc = NULL;
static struct rsc_table_info rsc_info;
static struct _payload *i_payload;
static int rnum = 0;
static int err_cnt = 0;
/* External functions */
extern void init_system();
extern void cleanup_system();
extern struct hil_proc *platform_create_proc(int proc_index);
extern void *get_resource_table (int rsc_id, int *len);
/* Application entry point */
int app (struct hil_proc *hproc)
{
int status = 0;
int shutdown_msg = SHUTDOWN_MSG;
int i;
int size;
int expect_rnum = 0;
LPRINTF(" 1 - Send data to remote core, retrieve the echo");
LPRINTF(" and validate its integrity ..\n");
i_payload =
(struct _payload *)metal_allocate_memory(2 * sizeof(unsigned long) +
PAYLOAD_MAX_SIZE);
if (!i_payload) {
LPERROR("memory allocation failed.\n");
return -1;
}
/* Initialize RPMSG framework */
status =
remoteproc_resource_init(&rsc_info, hproc,
rpmsg_channel_created,
rpmsg_channel_deleted, rpmsg_read_cb,
&proc, 1);
if (status) {
LPERROR("Failed to initialize remoteproc resource.\n");
return -1;
}
LPRINTF("Remote proc resource initialized.\n");
while (!app_rp_chnl) {
hil_poll(proc->proc, 0);
}
LPRINTF("RPMSG channel has created.\n");
for (i = 0, size = PAYLOAD_MIN_SIZE; i < (int)NUM_PAYLOADS; i++, size++) {
i_payload->num = i;
i_payload->size = size;
/* Mark the data buffer. */
memset(&(i_payload->data[0]), 0xA5, size);
LPRINTF("sending payload number %lu of size %d\n",
i_payload->num, (2 * sizeof(unsigned long)) + size);
status = rpmsg_send(app_rp_chnl, i_payload,
(2 * sizeof(unsigned long)) + size);
if (status) {
LPRINTF("Error sending data...\n");
break;
}
LPRINTF("echo test: sent : %d\n",
(2 * sizeof(unsigned long)) + size);
expect_rnum++;
do {
hil_poll(proc->proc, 0);
} while ((rnum < expect_rnum) && !err_cnt && app_rp_chnl);
}
LPRINTF("**********************************\n");
LPRINTF(" Test Results: Error count = %d \n", err_cnt);
LPRINTF("**********************************\n");
/* Send shutdown message to remote */
rpmsg_send(app_rp_chnl, &shutdown_msg, sizeof(int));
sleep(1);
LPRINTF("Quitting application .. Echo test end\n");
remoteproc_resource_deinit(proc);
cleanup_system();
metal_free_memory(i_payload);
return 0;
}
static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
{
app_rp_chnl = rp_chnl;
rp_ept = rpmsg_create_ept(rp_chnl, rpmsg_read_cb, RPMSG_NULL,
RPMSG_ADDR_ANY);
}
static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
{
(void)rp_chnl;
rpmsg_destroy_ept(rp_ept);
LPRINTF("%s\n", __func__);
app_rp_chnl = NULL;
rp_ept = NULL;
}
static void rpmsg_read_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
void *priv, unsigned long src)
{
(void)rp_chnl;
(void)src;
(void)priv;
int i;
struct _payload *r_payload = (struct _payload *)data;
LPRINTF(" received payload number %lu of size %d \r\n",
r_payload->num, len);
if (r_payload->size == 0) {
LPERROR(" Invalid size of package is received.\n");
err_cnt++;
return;
}
/* Validate data buffer integrity. */
for (i = 0; i < (int)r_payload->size; i++) {
if (r_payload->data[i] != 0xA5) {
LPRINTF("Data corruption at index %d\n", i);
err_cnt++;
break;
}
}
rnum = r_payload->num + 1;
}
int main(int argc, char *argv[])
{
unsigned long proc_id = 0;
unsigned long rsc_id = 0;
struct hil_proc *hproc;
/* Initialize HW system components */
init_system();
if (argc >= 2) {
proc_id = strtoul(argv[1], NULL, 0);
}
if (argc >= 3) {
rsc_id = strtoul(argv[2], NULL, 0);
}
/* Create HIL proc */
hproc = platform_create_proc(proc_id);
if (!hproc) {
LPERROR("Failed to create hil proc.\n");
return -1;
}
rsc_info.rsc_tab = get_resource_table(
(int)rsc_id, &rsc_info.size);
if (!rsc_info.rsc_tab) {
LPRINTF("Failed to get resource table data.\n");
return -1;
}
return app(hproc);
}