-
Notifications
You must be signed in to change notification settings - Fork 215
/
erpc_mbox_zephyr_transport.cpp
192 lines (156 loc) · 4.63 KB
/
erpc_mbox_zephyr_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
/*
* Copyright 2023 NXP
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_mbox_zephyr_transport.hpp"
#include "erpc_port.h"
#include <zephyr/kernel.h>
#include <cstdio>
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
static MBOXTransport *s_mbox_instance = NULL;
RING_BUF_DECLARE(s_rxRingBuffer, MBOX_BUFFER_SIZE);
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
MBOXTransport::MBOXTransport(struct device *dev, struct mbox_channel *tx_channel, struct mbox_channel *rx_channel) :
m_dev(dev), m_tx_channel(tx_channel), m_rx_channel(rx_channel)
#if !ERPC_THREADS_IS(NONE)
,
m_rxSemaphore(), m_txSemaphore()
#endif
{
s_mbox_instance = this;
}
MBOXTransport::~MBOXTransport(void) {}
void MBOXTransport::rx_cb(struct mbox_msg *data)
{
// Read data to ring buffer
ring_buf_put(&s_rxRingBuffer, static_cast<const uint8_t *>(data->data), data->size);
// message is complete, unblock caller of the receive function
if (ring_buf_size_get(&s_rxRingBuffer) >= m_transferReceiveRequireBytes)
{
m_isTransferReceiveCompleted = true;
#if !ERPC_THREADS_IS(NONE)
// disable MU rx full interrupt in rtos-based blocking implementation
m_rxSemaphore.put();
#endif
m_transferReceiveRequireBytes = MBOX_BUFFER_SIZE;
}
}
/* Transfer callback */
static void mbox_callback(const struct device *dev, uint32_t channel, void *user_data, struct mbox_msg *data)
{
s_mbox_instance->rx_cb(data);
}
erpc_status_t MBOXTransport::init(void)
{
if (mbox_register_callback(m_rx_channel, mbox_callback, NULL))
{
printk("mbox_register_callback() error\n");
return kErpcStatus_InitFailed;
}
if (mbox_set_enabled(m_rx_channel, 1))
{
printk("mbox_set_enable() error\n");
return kErpcStatus_InitFailed;
}
return kErpcStatus_Success;
}
erpc_status_t MBOXTransport::receive(MessageBuffer *message)
{
erpc_status_t status = kErpcStatus_Success;
uint8_t tmp[4];
uint32_t rxMsgSize = 0;
if (message == NULL)
{
status = kErpcStatus_ReceiveFailed;
}
else
{
#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_receiveLock);
#endif
// Wait for size of the message
waitForBytes(sizeof(uint32_t));
ring_buf_get(&s_rxRingBuffer, (uint8_t *)&rxMsgSize, sizeof(uint32_t));
// Wait for message to be transmitted
waitForBytes(rxMsgSize);
message->setUsed((uint16_t)rxMsgSize);
if (ring_buf_get(&s_rxRingBuffer, reinterpret_cast<uint8_t *>(message->get()), rxMsgSize) != rxMsgSize)
{
status = kErpcStatus_ReceiveFailed;
};
// Read remaining bytes from ring buffer
ring_buf_get(&s_rxRingBuffer, tmp, 4 - (rxMsgSize % 4));
rxMsgSize = 0;
}
return status;
}
void MBOXTransport::waitForBytes(uint32_t numOfBytes)
{
if (ring_buf_size_get(&s_rxRingBuffer) < numOfBytes)
{
m_transferReceiveRequireBytes = numOfBytes;
/* wait until the receiving is finished */
#if !ERPC_THREADS_IS(NONE)
(void)m_rxSemaphore.get();
#else
m_isTransferReceiveCompleted = false;
while (!m_isTransferReceiveCompleted)
{
}
#endif
}
}
erpc_status_t MBOXTransport::send(MessageBuffer *message)
{
erpc_status_t status = kErpcStatus_Success;
struct mbox_msg txMsg;
uint32_t txMsgSize = 0;
uint32_t txCntBytes = 0;
uint8_t *txBuffer;
if (message == NULL)
{
status = kErpcStatus_SendFailed;
}
else
{
#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_sendLock);
#endif
txMsgSize = message->getUsed();
txCntBytes = 0;
txBuffer = message->get();
txMsg.data = &txMsgSize;
txMsg.size = sizeof(uint32_t);
mbox_send(m_tx_channel, &txMsg);
while (txCntBytes < txMsgSize)
{
txMsg.data = &txBuffer[txCntBytes];
txMsg.size = sizeof(uint32_t);
int mboxStatus = mbox_send(m_tx_channel, &txMsg);
if (mboxStatus == -EBUSY)
{
continue;
}
if (mboxStatus < 0)
{
return kErpcStatus_SendFailed;
}
txCntBytes += sizeof(uint32_t);
}
txMsgSize = 0;
txCntBytes = 0;
txBuffer = NULL;
}
return status;
}
bool MBOXTransport::hasMessage(void)
{
return !ring_buf_is_empty(&s_rxRingBuffer);
}