bf7e14a993eb6dacba51c282e2949f9f7d0d34d7
1 /* This is a sample demonstration application that showcases usage of remoteproc
2 and rpmsg APIs on the remote core. This application is meant to run on the remote CPU
3 running baremetal code. This applicationr receives two matrices from the master,
4 multiplies them and returns the result to the master core. */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "openamp/open_amp.h"
10 #include "rsc_table.h"
12 #ifdef ZYNQ7_BAREMETAL
13 #include "baremetal.h"
14 #endif
16 #define MAX_SIZE 6
17 #define NUM_MATRIX 2
18 #define SHUTDOWN_MSG 0xEF56A55A
20 typedef struct _matrix {
21 unsigned int size;
22 unsigned int elements[MAX_SIZE][MAX_SIZE];
23 } matrix;
25 /* Internal functions */
26 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
27 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
28 static void rpmsg_read_cb(struct rpmsg_channel *, void *, int, void *,
29 unsigned long);
30 static void Matrix_Multiply(const matrix * m, const matrix * n, matrix * r);
32 /* Globals */
33 static struct rpmsg_channel *app_rp_chnl;
34 void *mat_mul_lock;
35 int need_to_cal = 0;
36 static struct rpmsg_endpoint *rp_ept;
37 static matrix matrix_array[NUM_MATRIX];
38 static matrix matrix_result;
39 static struct remote_proc *proc = NULL;
40 static struct rsc_table_info rsc_info;
41 extern const struct remote_resource_table resources;
43 /* External functions */
44 extern void init_system();
46 /* Application entry point */
47 int main()
48 {
50 int status = 0;
52 #ifdef ZYNQ7_BAREMETAL
53 SWITCH_TO_SYS_MODE();
54 #endif
56 /* Initialize HW system components */
57 init_system();
59 rsc_info.rsc_tab = (struct resource_table *)&resources;
60 rsc_info.size = sizeof(resources);
62 /* Initialize RPMSG framework */
63 status =
64 remoteproc_resource_init(&rsc_info, rpmsg_channel_created,
65 rpmsg_channel_deleted, rpmsg_read_cb,
66 &proc);
67 if (status < 0) {
68 return -1;
69 }
71 while (1) {
72 __asm__("wfi\n\t");
73 }
75 return 0;
76 }
78 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
79 {
80 app_rp_chnl = rp_chnl;
81 rp_ept = rpmsg_create_ept(rp_chnl, rpmsg_read_cb, RPMSG_NULL,
82 RPMSG_ADDR_ANY);
83 }
85 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
86 {
87 rpmsg_destroy_ept(rp_ept);
88 }
90 static void rpmsg_read_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
91 void *priv, unsigned long src)
92 {
93 if ((*(int *)data) == SHUTDOWN_MSG) {
94 remoteproc_resource_deinit(proc);
95 } else {
96 env_memcpy(matrix_array, data, len);
97 /* Process received data and multiple matrices. */
98 Matrix_Multiply(&matrix_array[0], &matrix_array[1],
99 &matrix_result);
101 /* Send the result of matrix multiplication back to master. */
102 rpmsg_send(app_rp_chnl, &matrix_result, sizeof(matrix));
103 }
104 }
106 static void Matrix_Multiply(const matrix * m, const matrix * n, matrix * r)
107 {
108 int i, j, k;
110 env_memset(r, 0x0, sizeof(matrix));
111 r->size = m->size;
113 for (i = 0; i < m->size; ++i) {
114 for (j = 0; j < n->size; ++j) {
115 for (k = 0; k < r->size; ++k) {
116 r->elements[i][j] +=
117 m->elements[i][k] * n->elements[k][j];
118 }
119 }
120 }
121 }