-
Notifications
You must be signed in to change notification settings - Fork 212
/
erpc_rpmsg_lite_rtos_transport.cpp
291 lines (253 loc) · 7.66 KB
/
erpc_rpmsg_lite_rtos_transport.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2023 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_rpmsg_lite_rtos_transport.hpp"
#include "erpc_config_internal.h"
extern "C" {
#include "rpmsg_ns.h"
}
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
uint8_t RPMsgBase::s_initialized = 0U;
struct rpmsg_lite_instance *RPMsgBase::s_rpmsg;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
RPMsgRTOSTransport::RPMsgRTOSTransport(void) :
Transport(), RPMsgBase(), m_rdev(NULL), m_app_rp_chnl(NULL), m_dst_addr(0), m_rpmsg_queue(NULL), m_rpmsg_ept(NULL),
m_crcImpl(NULL)
{
}
RPMsgRTOSTransport::~RPMsgRTOSTransport(void)
{
bool skip = false;
if (s_rpmsg != RL_NULL)
{
if (m_rpmsg_ept != RL_NULL)
{
if (RL_SUCCESS != rpmsg_lite_destroy_ept(s_rpmsg, m_rpmsg_ept))
{
skip = true;
}
}
if (!skip && (m_rpmsg_queue != RL_NULL))
{
if (RL_SUCCESS != rpmsg_queue_destroy(s_rpmsg, m_rpmsg_queue))
{
skip = true;
}
}
if (!skip && (RL_SUCCESS != rpmsg_lite_deinit(s_rpmsg)))
{
skip = true;
}
if (!skip)
{
s_initialized = 0U;
}
}
}
erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, void *base_address, uint32_t length,
uint32_t rpmsg_link_id)
{
erpc_status_t status = kErpcStatus_Success;
if (0U == s_initialized)
{
#if RL_USE_STATIC_API
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
#else
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS);
#endif
if (s_rpmsg == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
if (status == kErpcStatus_Success)
{
#if RL_USE_STATIC_API
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg, m_queue_stack, &m_queue_context);
#else
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg);
#endif
if (m_rpmsg_queue == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
#if RL_USE_STATIC_API
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue, &m_ept_context);
#else
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue);
#endif
if (m_rpmsg_ept == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
else
{
m_dst_addr = dst_addr;
s_initialized = 1U;
}
}
if (status != kErpcStatus_Success)
{
if (m_rpmsg_queue != RL_NULL)
{
if (RL_SUCCESS == rpmsg_queue_destroy(s_rpmsg, m_rpmsg_queue))
{
m_rpmsg_queue = NULL;
}
}
if (s_rpmsg != RL_NULL)
{
if (RL_SUCCESS == rpmsg_lite_deinit(s_rpmsg))
{
s_rpmsg = NULL;
}
}
}
}
return status;
}
erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, void *base_address, uint32_t rpmsg_link_id,
void (*ready_cb)(void), char *nameservice_name)
{
erpc_status_t status = kErpcStatus_Success;
if (0U == s_initialized)
{
#if RL_USE_STATIC_API
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
#else
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS);
#endif
if (s_rpmsg == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
if (status == kErpcStatus_Success)
{
/* Signal the other core we are ready */
if (ready_cb != NULL)
{
ready_cb();
}
(void)rpmsg_lite_wait_for_link_up(s_rpmsg, RL_BLOCK);
#if RL_USE_STATIC_API
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg, m_queue_stack, &m_queue_context);
#else
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg);
#endif
if (m_rpmsg_queue == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
#if RL_USE_STATIC_API
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue, &m_ept_context);
#else
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue);
#endif
if (m_rpmsg_ept == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
if (NULL != nameservice_name)
{
if (RL_SUCCESS != rpmsg_ns_announce(s_rpmsg, m_rpmsg_ept, nameservice_name, (uint32_t)RL_NS_CREATE))
{
status = kErpcStatus_InitFailed;
}
}
}
if (status == kErpcStatus_Success)
{
m_dst_addr = dst_addr;
s_initialized = 1U;
}
else
{
if (m_rpmsg_ept != RL_NULL)
{
if (RL_SUCCESS == rpmsg_lite_destroy_ept(s_rpmsg, m_rpmsg_ept))
{
m_rpmsg_ept = NULL;
}
}
if (m_rpmsg_queue != RL_NULL)
{
if (RL_SUCCESS == rpmsg_queue_destroy(s_rpmsg, m_rpmsg_queue))
{
m_rpmsg_queue = NULL;
}
}
if (s_rpmsg != RL_NULL)
{
if (RL_SUCCESS == rpmsg_lite_deinit(s_rpmsg))
{
s_rpmsg = NULL;
}
}
}
}
return status;
}
erpc_status_t RPMsgRTOSTransport::receive(MessageBuffer *message)
{
char *buf = NULL;
uint32_t length = 0;
int32_t ret_val;
ret_val = rpmsg_queue_recv_nocopy(s_rpmsg, m_rpmsg_queue, &m_dst_addr, &buf, &length, RL_BLOCK);
erpc_assert(buf != NULL);
message->set(reinterpret_cast<uint8_t *>(buf), length);
message->setUsed(length);
return (ret_val != RL_SUCCESS) ? kErpcStatus_ReceiveFailed : kErpcStatus_Success;
}
erpc_status_t RPMsgRTOSTransport::send(MessageBuffer *message)
{
erpc_status_t status = kErpcStatus_Success;
uint8_t *buf = message->get();
uint16_t length = message->getLength();
uint16_t used = message->getUsed();
int32_t ret_val;
message->set(NULL, 0);
ret_val = rpmsg_lite_send_nocopy(s_rpmsg, m_rpmsg_ept, m_dst_addr, buf, used);
if (ret_val == RL_SUCCESS)
{
status = kErpcStatus_Success;
}
else
{
message->set(buf, length);
message->setUsed(used);
status = kErpcStatus_SendFailed;
}
return status;
}
bool RPMsgRTOSTransport::hasMessage(void)
{
return ((rpmsg_queue_get_current_size(m_rpmsg_queue) > 0) ? true : false);
}
void RPMsgRTOSTransport::setCrc16(Crc16 *crcImpl)
{
erpc_assert(crcImpl != NULL);
m_crcImpl = crcImpl;
}
Crc16 *RPMsgRTOSTransport::getCrc16(void)
{
return m_crcImpl;
}