@@ -12,6 +12,9 @@ arduino::RPCClass RPC;
12
12
osThreadId eventHandlerThreadId;
13
13
static rtos::Mutex mutex;
14
14
static struct rpmsg_endpoint endpoints[2 ];
15
+ #ifdef CORE_CM4
16
+ static bool endpoints_init[2 ] = { 0 };
17
+ #endif
15
18
16
19
void RPCClass::new_service_cb (struct rpmsg_device *rdev, const char *name, uint32_t dest) {
17
20
uint8_t buffer[1 ] = {0 };
@@ -31,20 +34,23 @@ void RPCClass::new_service_cb(struct rpmsg_device *rdev, const char *name, uint3
31
34
32
35
int RPCClass::rpmsg_recv_callback (struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) {
33
36
#ifdef CORE_CM4
34
- static bool ep_init_msg[2 ] = { 0 };
35
- if (!ep_init_msg[ENDPOINT_ID_RPC] && ept == &endpoints[ENDPOINT_ID_RPC]) {
36
- ep_init_msg[ENDPOINT_ID_RPC] = true ;
37
+ if (!endpoints_init[ENDPOINT_ID_RPC] && ept == &endpoints[ENDPOINT_ID_RPC]) {
38
+ endpoints_init[ENDPOINT_ID_RPC] = true ;
37
39
return 0 ;
38
- } else if (!ep_init_msg [ENDPOINT_ID_RAW] && ept == &endpoints[ENDPOINT_ID_RAW]) {
39
- ep_init_msg [ENDPOINT_ID_RAW] = true ;
40
+ } else if (!endpoints_init [ENDPOINT_ID_RAW] && ept == &endpoints[ENDPOINT_ID_RAW]) {
41
+ endpoints_init [ENDPOINT_ID_RAW] = true ;
40
42
return 0 ;
41
43
}
42
44
#endif
43
45
44
46
if (ept == &endpoints[ENDPOINT_ID_RAW]) {
45
47
// data on raw endpoint
46
48
if (RPC.raw_callback ) {
47
- RPC.raw_callback .call ((const uint8_t *) data, len);
49
+ RPC.raw_callback .call ((uint8_t *) data, len);
50
+ } else {
51
+ for (size_t i=0 ; i<len; i++) {
52
+ RPC.rx_buffer .store_char (((uint8_t *) data)[i]);
53
+ }
48
54
}
49
55
return 0 ;
50
56
}
@@ -176,6 +182,16 @@ int RPCClass::begin() {
176
182
return 0 ;
177
183
}
178
184
185
+ // Wait for endpoints to be initialized first by the host before allowing
186
+ // the remote to use the endpoints.
187
+ uint32_t millis_start = millis ();
188
+ while (!endpoints_init[ENDPOINT_ID_RPC] || !endpoints_init[ENDPOINT_ID_RAW]) {
189
+ if ((millis () - millis_start) >= 5000 ) {
190
+ return 0 ;
191
+ }
192
+ osDelay (10 );
193
+ }
194
+
179
195
return 1 ;
180
196
}
181
197
#endif
@@ -217,53 +233,25 @@ void RPCClass::request(uint8_t *buf, size_t len) {
217
233
RPCLIB_MSGPACK::unpacked result;
218
234
while (unpacker.next (result)) {
219
235
auto msg = result.get ();
220
- if (msg.via .array .size == 1 ) {
221
- // raw array
222
- std::tuple<RPCLIB_MSGPACK::object> arr;
223
- msg.convert (arr);
224
-
225
- std::vector<uint8_t > buf;
226
- std::get<0 >(arr).convert (buf);
227
-
228
- for (size_t i = 0 ; i < buf.size (); i++) {
229
- rx_buffer.store_char (buf[i]);
230
- }
231
- }
232
-
233
- if (msg.via .array .size > 2 ) {
234
- auto resp = rpc::detail::dispatcher::dispatch (msg, false );
235
- auto data = resp.get_data ();
236
- if (!resp.is_empty ()) {
237
- OPENAMP_send (&endpoints[ENDPOINT_ID_RPC], data.data (), data.size ());
238
- }
236
+ auto resp = rpc::detail::dispatcher::dispatch (msg, false );
237
+ auto data = resp.get_data ();
238
+ if (!resp.is_empty ()) {
239
+ OPENAMP_send (&endpoints[ENDPOINT_ID_RPC], data.data (), data.size ());
239
240
}
240
241
}
241
242
}
242
243
243
244
size_t RPCClass::write (uint8_t c) {
244
- return write (&c, 1 , true , false );
245
+ return write (&c, 1 , true );
245
246
}
246
247
247
248
void rpc::client::write (RPCLIB_MSGPACK::sbuffer *buffer) {
248
- RPC.write ((const uint8_t *) buffer->data (), buffer->size (), false , false );
249
+ RPC.write ((const uint8_t *) buffer->data (), buffer->size (), false );
249
250
}
250
251
251
- size_t RPCClass::write (const uint8_t *buf, size_t len, bool pack, bool raw) {
252
- if (pack) {
253
- std::vector<uint8_t > tx_buffer;
254
- for (size_t i = 0 ; i < len; i++) {
255
- tx_buffer.push_back (buf[i]);
256
- }
257
- auto call_obj = std::make_tuple (tx_buffer);
258
-
259
- RPCLIB_MSGPACK::sbuffer buffer;
260
- RPCLIB_MSGPACK::pack (buffer, call_obj);
261
- len = buffer.size ();
262
- buf = (const uint8_t *) buffer.data ();
263
- }
264
-
252
+ size_t RPCClass::write (const uint8_t *buf, size_t len, bool raw) {
265
253
mutex.lock ();
266
254
OPENAMP_send (&endpoints[raw ? ENDPOINT_ID_RAW : ENDPOINT_ID_RPC], buf, len);
267
255
mutex.unlock ();
268
256
return len;
269
- }
257
+ }
0 commit comments