forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherpc_mu_transport.cpp
317 lines (273 loc) · 7.64 KB
/
erpc_mu_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2017-2023 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_mu_transport.hpp"
#include "erpc_config_internal.h"
extern "C" {
#include "board.h"
}
using namespace erpc;
/* IMPLEMNENTATION COMMENTS */
/*
Implementation for baremetal (#if !ERPC_THREADS_IS(NONE) conditional section)
is nonblocking, the code flow is following: enable interrupt, wait for interrupt, once interrupt appears disable the
interrupt and set hasMessage==true, recv start, enable interrupt, get data, receive end (keep interrupt enabled for
the same reason as the first time).
Implementation for an RTOS (FREERTOS) is blocking, the code flow is following: enable interrupt on recv start,
get data once ready, disable interrupt on receive end.
*/
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
static MUTransport *s_mu_instance = NULL;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
#if ERPC_TRANSPORT_MU_USE_MCMGR
void MUTransport::mu_tx_empty_irq_callback(void)
{
MUTransport *transport = s_mu_instance;
if ((transport != NULL) && MU_IS_TX_EMPTY_FLAG_SET)
{
transport->tx_cb();
}
}
void MUTransport::mu_rx_full_irq_callback(void)
{
MUTransport *transport = s_mu_instance;
if ((transport != NULL) && MU_IS_RX_FULL_FLAG_SET)
{
transport->rx_cb();
}
}
#else
void MUTransport::mu_irq_callback(void)
{
MUTransport *transport = s_mu_instance;
uint32_t flags;
flags = MU_GetStatusFlags(transport->m_muBase);
// RECEIVING - rx full flag and rx full irq enabled
if ((flags & MU_SR_RX_MASK) && MU_IS_RX_FULL_FLAG_SET)
{
transport->rx_cb();
}
// TRANSMITTING - tx empty flag and tx empty irq enabled
if ((flags & MU_SR_TX_MASK) && MU_IS_TX_EMPTY_FLAG_SET)
{
transport->tx_cb();
}
}
#endif /* ERPC_TRANSPORT_MU_USE_MCMGR */
MUTransport::MUTransport(void) :
Transport(), m_newMessage(false), m_rxMsgSize(0), m_rxCntBytes(0), m_rxBuffer(NULL), m_txMsgSize(0), m_txCntBytes(0),
m_txBuffer(NULL)
#if !ERPC_THREADS_IS(NONE)
,
m_rxSemaphore(), m_txSemaphore(), m_sendLock(), m_receiveLock()
#endif
,
m_muBase(NULL)
{
s_mu_instance = this;
}
MUTransport::~MUTransport(void) {}
erpc_status_t MUTransport::init(MU_Type *muBase)
{
m_muBase = muBase;
#if !ERPC_TRANSPORT_MU_USE_MCMGR
MU_Init(muBase);
#endif
#if !ERPC_THREADS
// enabling the MU rx full irq is necessary only for BM app
MU_EnableInterrupts(m_muBase, MU_RX_INTR_MASK);
#endif
NVIC_SetPriority(MU_IRQ, MU_IRQ_PRIORITY);
(void)EnableIRQ(MU_IRQ);
return kErpcStatus_Success;
}
void MUTransport::rx_cb(void)
{
uint32_t i = 0;
if (m_rxBuffer == NULL)
{
// the receive function has not been called yet
// disable MU rx full interrupt
MU_DisableInterrupts(m_muBase, MU_RX_INTR_MASK);
m_newMessage = true;
}
else
{
// read data from the MU rx registers
if (m_rxMsgSize == 0U)
{
m_rxMsgSize = MU_ReceiveMsgNonBlocking(m_muBase, i++);
}
for (; i < MU_REG_COUNT; i++)
{
if (m_rxCntBytes < m_rxMsgSize)
{
m_rxBuffer[m_rxCntBytes >> 2] = MU_ReceiveMsgNonBlocking(m_muBase, i);
m_rxCntBytes += 4U;
}
else
{
// read MU rx reg to clear the rx full flag
(void)MU_ReceiveMsgNonBlocking(m_muBase, i);
}
}
// message is complete, unblock caller of the receive function
if (m_rxCntBytes >= m_rxMsgSize)
{
m_rxBuffer = NULL;
#if !ERPC_THREADS_IS(NONE)
// disable MU rx full interrupt in rtos-based blocking implementation
MU_DisableInterrupts(m_muBase, MU_RX_INTR_MASK);
m_rxSemaphore.putFromISR();
#endif
}
}
}
void MUTransport::tx_cb(void)
{
// fill MU tx regs
uint32_t i = 0;
uint32_t tx;
for (i = 0; i < MU_REG_COUNT; i++)
{
// prepare uint32_t and write it to next MU tx reg
tx = 0;
if (m_txCntBytes < m_txMsgSize)
{
tx = m_txBuffer[m_txCntBytes >> 2];
}
MU_SendMsg(m_muBase, i, tx);
m_txCntBytes += 4U;
}
// if the message was sent whole, disable the MU tx empty irq
if (m_txCntBytes >= m_txMsgSize)
{
// disable MU tx empty irq from the last tx reg
MU_DisableInterrupts(m_muBase, MU_TX_INTR_MASK);
// unblock caller of the send function
m_txBuffer = NULL;
#if !ERPC_THREADS_IS(NONE)
m_txSemaphore.putFromISR();
#endif
}
}
erpc_status_t MUTransport::receive(MessageBuffer *message)
{
erpc_status_t status;
if (message == NULL)
{
status = kErpcStatus_ReceiveFailed;
}
else
{
#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_receiveLock);
#endif
m_rxMsgSize = 0;
m_rxCntBytes = 0;
m_rxBuffer = reinterpret_cast<uint32_t *>(message->get());
// enable the MU rx full irq
MU_EnableInterrupts(m_muBase, MU_RX_INTR_MASK);
// wait until the receiving is not complete
#if !ERPC_THREADS_IS(NONE)
(void)m_rxSemaphore.get();
#else
while (m_rxBuffer != NULL)
{
}
#endif
message->setUsed((uint16_t)m_rxMsgSize);
m_newMessage = false;
status = kErpcStatus_Success;
}
return status;
}
erpc_status_t MUTransport::send(MessageBuffer *message)
{
erpc_status_t status;
uint8_t i;
uint32_t tx;
if (message == NULL)
{
status = kErpcStatus_SendFailed;
}
else
{
#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_sendLock);
#endif
m_txMsgSize = message->getUsed();
m_txCntBytes = 0;
m_txBuffer = reinterpret_cast<uint32_t *>(message->get());
MU_SendMsg(m_muBase, 0, m_txMsgSize);
// write to next MU tx registers
for (i = 1; i < MU_REG_COUNT; i++)
{
// prepare next uint32_t and write it to next MU tx reg
tx = 0;
if (m_txCntBytes < m_txMsgSize)
{
tx = m_txBuffer[m_txCntBytes >> 2];
}
MU_SendMsg(m_muBase, i, tx);
m_txCntBytes += 4U;
}
// if the message wasn't sent whole, enable the MU tx empty irq
if (m_txCntBytes < m_txMsgSize)
{
// enable MU tx empty irq from the last mu tx reg
MU_EnableInterrupts(m_muBase, MU_TX_INTR_MASK);
// wait until the sending is not complete
#if !ERPC_THREADS_IS(NONE)
(void)m_txSemaphore.get();
#else
while (m_txBuffer != NULL)
{
}
#endif
}
status = kErpcStatus_Success;
}
return status;
}
bool MUTransport::hasMessage(void)
{
return m_newMessage;
}
extern "C" {
#if ERPC_TRANSPORT_MU_USE_MCMGR
/*!
* @brief Messaging Unit TxEmptyFlag ISR handler
*
* This function overloads the weak handler defined in MCMGR MU_interrupts[] MU ISR table
*/
void MU_TxEmptyFlagISRCallback(void)
{
MUTransport::mu_tx_empty_irq_callback();
}
/*!
* @brief Messaging Unit RxFullFlag ISR handler
*
* This function overloads the weak handler defined in MCMGR MU_interrupts[] MU ISR table
*/
void MU_RxFullFlagISRCallback(void)
{
MUTransport::mu_rx_full_irq_callback();
}
#else
int MU_IRQ_HANDLER(void)
{
MUTransport::mu_irq_callback();
return 0;
}
#endif /* ERPC_TRANSPORT_MU_USE_MCMGR */
}