forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erpc_usb_cdc_transport.cpp
182 lines (157 loc) · 5.65 KB
/
erpc_usb_cdc_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
/*
* Copyright 2020-2021 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_usb_cdc_transport.hpp"
#include <cstdio>
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
static volatile bool s_isTransferReceiveCompleted = false;
static volatile bool s_isTransferSendCompleted = false;
static UsbCdcTransport *s_usbcdc_instance = NULL;
SDK_ALIGN(static uint8_t s_serialWriteHandleBuffer[SERIAL_MANAGER_WRITE_HANDLE_SIZE], 4);
static serial_write_handle_t s_serialWriteHandle = &s_serialWriteHandleBuffer[0]; /*!< serial manager write handle */
SDK_ALIGN(static uint8_t s_serialReadHandleBuffer[SERIAL_MANAGER_READ_HANDLE_SIZE], 4);
static serial_read_handle_t s_serialReadHandle = &s_serialReadHandleBuffer[0]; /*!< serial manager read handle */
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
static void ERPC_SerialManagerTxCallback(const void *callbackParam, const serial_manager_callback_message_t *message,
serial_manager_status_t status)
{
UsbCdcTransport *transport = s_usbcdc_instance;
if ((NULL != callbackParam) && (NULL != message))
{
if (kStatus_SerialManager_Success == status)
{
transport->tx_cb();
}
else
{
/* Handle other status if needed */
}
}
}
static void ERPC_SerialManagerRxCallback(const void *callbackParam, const serial_manager_callback_message_t *message,
serial_manager_status_t status)
{
UsbCdcTransport *transport = s_usbcdc_instance;
if ((NULL != callbackParam) && (NULL != message))
{
if (kStatus_SerialManager_Success == status)
{
transport->rx_cb();
}
else
{
/* Handle other status if needed */
}
}
}
void UsbCdcTransport::tx_cb(void)
{
#if !ERPC_THREADS_IS(NONE)
m_txSemaphore.putFromISR();
#else
s_isTransferSendCompleted = true;
#endif
}
void UsbCdcTransport::rx_cb(void)
{
#if !ERPC_THREADS_IS(NONE)
m_rxSemaphore.putFromISR();
#else
s_isTransferReceiveCompleted = true;
#endif
}
UsbCdcTransport::UsbCdcTransport(serial_handle_t serialHandle, serial_manager_config_t *serialConfig,
serial_port_usb_cdc_config_t *usbCdcConfig, uint8_t *usbRingBuffer,
uint32_t usbRingBufferLength) :
m_serialHandle(serialHandle),
m_serialConfig(serialConfig), m_usbCdcConfig(usbCdcConfig), m_usbRingBuffer(usbRingBuffer),
m_usbRingBufferLength(usbRingBufferLength)
#if !ERPC_THREADS_IS(NONE)
,
m_rxSemaphore(), m_txSemaphore()
#endif
{
s_usbcdc_instance = this;
}
UsbCdcTransport::~UsbCdcTransport(void)
{
/* Cleanup */
SerialManager_CloseWriteHandle(s_serialWriteHandle);
SerialManager_CloseReadHandle(s_serialReadHandle);
SerialManager_Deinit(m_serialHandle);
}
erpc_status_t UsbCdcTransport::init(void)
{
erpc_status_t status = kErpcStatus_InitFailed;
/* Init Serial Manager for USB CDC */
m_serialConfig->type = kSerialPort_UsbCdc;
m_serialConfig->ringBuffer = m_usbRingBuffer;
m_serialConfig->ringBufferSize = m_usbRingBufferLength;
m_serialConfig->portConfig = m_usbCdcConfig;
if (kStatus_SerialManager_Success == SerialManager_Init(m_serialHandle, m_serialConfig))
{
if (kStatus_SerialManager_Success == SerialManager_OpenWriteHandle(m_serialHandle, s_serialWriteHandle))
{
if (kStatus_SerialManager_Success == SerialManager_OpenReadHandle(m_serialHandle, s_serialReadHandle))
{
if (kStatus_SerialManager_Success == SerialManager_InstallTxCallback(s_serialWriteHandle,
ERPC_SerialManagerTxCallback,
s_serialWriteHandle))
{
if (kStatus_SerialManager_Success == SerialManager_InstallRxCallback(s_serialReadHandle,
ERPC_SerialManagerRxCallback,
s_serialReadHandle))
{
status = kErpcStatus_Success;
}
}
}
}
}
return status;
}
erpc_status_t UsbCdcTransport::underlyingReceive(uint8_t *data, uint32_t size)
{
erpc_status_t status = kErpcStatus_ReceiveFailed;
s_isTransferReceiveCompleted = false;
if (kStatus_SerialManager_Success == SerialManager_ReadNonBlocking(s_serialReadHandle, data, size))
{
/* wait until the receiving is finished */
#if !ERPC_THREADS_IS(NONE)
(void)m_rxSemaphore.get();
#else
while (!s_isTransferReceiveCompleted)
{
}
#endif
status = kErpcStatus_Success;
}
return status;
}
erpc_status_t UsbCdcTransport::underlyingSend(const uint8_t *data, uint32_t size)
{
erpc_status_t status = kErpcStatus_SendFailed;
s_isTransferSendCompleted = false;
if (kStatus_SerialManager_Success == SerialManager_WriteNonBlocking(s_serialWriteHandle, data, size))
{
/* wait until the sending is finished */
#if !ERPC_THREADS_IS(NONE)
(void)m_txSemaphore.get();
#else
while (!s_isTransferSendCompleted)
{
}
#endif
status = kErpcStatus_Success;
}
return status;
}