-
Notifications
You must be signed in to change notification settings - Fork 20
/
usbd_ecm.c
303 lines (239 loc) · 8.92 KB
/
usbd_ecm.c
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
/*
USB CDC-ECM for STM32F072 microcontroller
Copyright (C) 2015,2016,2018 Peter Lawrence
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/*
Theory of operation:
Each incoming virtual Ethernet packet from the host arrives via ECM_DATA_OUT_EP.
It arrives in 64-byte chunks, and the last chunk will have a length of less than 64 (signifying a whole packet).
These chunks are accumulated in ecm_rx_buffer, and the whole packet is passed to the user's usb_ecm_recv_callback function.
The user indicates that another packet can be received (writing over the existing data) by calling usb_ecm_recv_renew().
Note that ST's stack may refuse to "renew", so OutboundTransferNeedsRenewal exists to mop up when this happens.
Outgoing virtual Ethernet packets are sent to the host via ECM_DATA_IN_IP.
In theory, the ST stack can handle "multi-packet" automatically, but there seem to be issues with this.
So, as with receive packets, transmit packets are sent to the host in 64-byte chunks.
The user should call usb_ecm_can_xmit() to verify whether it is possible to transmit another packet.
The user then calls usb_ecm_xmit_packet() to transmit such a packet.
*/
#include "usbd_ecm.h"
#include "usbd_desc.h"
/* USB handle declared in main.c */
extern USBD_HandleTypeDef USBD_Device;
extern void ECM_ReceivePacket(unsigned index, uint8_t *data, uint16_t length);
/* local function prototyping */
static uint8_t USBD_ECM_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_ECM_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_ECM_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_ECM_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_ECM_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_ECM_EP0_RxReady (USBD_HandleTypeDef *pdev);
static const uint8_t *USBD_ECM_GetFSCfgDesc (uint16_t *length);
static uint8_t USBD_ECM_SOF (USBD_HandleTypeDef *pdev);
static USBD_StatusTypeDef USBD_ECM_ReceivePacket (USBD_HandleTypeDef *pdev, unsigned index);
/* class callbacks structure that is used by main.c */
const USBD_ClassTypeDef USBD_ECM =
{
.Init = USBD_ECM_Init,
.DeInit = USBD_ECM_DeInit,
.Setup = USBD_ECM_Setup,
.EP0_TxSent = NULL,
.EP0_RxReady = USBD_ECM_EP0_RxReady,
.DataIn = USBD_ECM_DataIn,
.DataOut = USBD_ECM_DataOut,
.SOF = USBD_ECM_SOF,
.IsoINIncomplete = NULL,
.IsoOUTIncomplete = NULL,
.GetFSConfigDescriptor = USBD_ECM_GetFSCfgDesc,
};
static USBD_HandleTypeDef *registered_pdev;
__ALIGN_BEGIN static uint8_t ecm_rx_buffer[ECM_MAX_SEGMENT_SIZE] __ALIGN_END;
__ALIGN_BEGIN static uint8_t ecm_tx_buffer[ECM_MAX_SEGMENT_SIZE] __ALIGN_END;
__ALIGN_BEGIN static USBD_SetupReqTypedef notify __ALIGN_END =
{
.bmRequest = 0x21,
.bRequest = 0 /* NETWORK_CONNECTION */,
.wValue = 1 /* Connected */,
.wLength = 0,
};
static int ecm_rx_index;
static bool can_xmit;
static bool OutboundTransferNeedsRenewal;
static uint8_t *ecm_tx_ptr;
static int ecm_tx_remaining;
static int ecm_tx_busy;
static int copy_length;
void usb_ecm_recv_renew(void)
{
USBD_StatusTypeDef outcome;
outcome = USBD_LL_PrepareReceive(registered_pdev, ECM_DATA_OUT_EP, ecm_rx_buffer + ecm_rx_index, ECM_DATA_OUT_SZ);
OutboundTransferNeedsRenewal = (USBD_OK != outcome); /* set if the HAL was busy so that we know to retry it */
}
static uint8_t USBD_ECM_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
registered_pdev = pdev;
/* Open EP IN */
USBD_LL_OpenEP(pdev, ECM_DATA_IN_EP, USBD_EP_TYPE_BULK, ECM_DATA_IN_SZ);
/* Open EP OUT */
USBD_LL_OpenEP(pdev, ECM_DATA_OUT_EP, USBD_EP_TYPE_BULK, ECM_DATA_OUT_SZ);
/* Open Command IN EP */
USBD_LL_OpenEP(pdev, ECM_NOTIFICATION_IN_EP, USBD_EP_TYPE_INTR, ECM_NOTIFICATION_IN_SZ);
usb_ecm_recv_renew();
can_xmit = true;
OutboundTransferNeedsRenewal = false;
ecm_tx_busy = 0;
ecm_tx_remaining = 0;
return USBD_OK;
}
static uint8_t USBD_ECM_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
registered_pdev = NULL;
/* Close EP IN */
USBD_LL_CloseEP(pdev, ECM_DATA_IN_EP);
/* Close EP OUT */
USBD_LL_CloseEP(pdev, ECM_DATA_OUT_EP);
/* Close Command IN EP */
USBD_LL_CloseEP(pdev, ECM_NOTIFICATION_IN_EP);
can_xmit = false;
return USBD_OK;
}
static uint8_t USBD_ECM_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == req->bRequest)
{
notify.wIndex = req->wIndex;
USBD_LL_Transmit(pdev, ECM_NOTIFICATION_IN_EP, (uint8_t *)¬ify, sizeof(notify));
}
return USBD_OK;
}
static void ecm_incoming_attempt(void)
{
int chunk_size;
if (!ecm_tx_remaining || ecm_tx_busy)
return;
chunk_size = ecm_tx_remaining;
if (chunk_size > ECM_DATA_IN_SZ)
chunk_size = ECM_DATA_IN_SZ;
/* ST stack always returns a success code, so reading the return value is pointless */
USBD_LL_Transmit(registered_pdev, ECM_DATA_IN_EP, ecm_tx_ptr, chunk_size);
ecm_tx_ptr += chunk_size;
ecm_tx_remaining -= chunk_size;
ecm_tx_busy = 1;
}
static uint8_t USBD_ECM_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
{
if (ECM_DATA_IN_EP == (epnum | 0x80))
{
ecm_tx_busy = 0;
if (0 == ecm_tx_remaining)
can_xmit = true;
ecm_incoming_attempt();
}
return USBD_OK;
}
static uint8_t USBD_ECM_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum)
{
uint32_t RxLength;
if (ECM_DATA_OUT_EP != epnum)
return USBD_OK;
/* Get the received data length */
RxLength = USBD_LL_GetRxDataSize (pdev, epnum);
ecm_rx_index += RxLength;
if (RxLength < ECM_DATA_OUT_SZ)
{
usb_ecm_recv_callback(ecm_rx_buffer, ecm_rx_index);
ecm_rx_index = 0;
}
else
{
/* Initiate next USB packet transfer */
usb_ecm_recv_renew();
}
return USBD_OK;
}
static uint8_t USBD_ECM_SOF (USBD_HandleTypeDef *pdev)
{
/* mop up for any failed USBD_LL_PrepareReceive() call */
if (OutboundTransferNeedsRenewal)
usb_ecm_recv_renew();
if (ecm_tx_busy)
{
/* ugly hack for ST stack sometimes not providing the DataOut callback */
if (++ecm_tx_busy > 32)
{
ecm_tx_busy = 0;
if (0 == ecm_tx_remaining)
can_xmit = true;
}
}
ecm_incoming_attempt();
return USBD_OK;
}
static uint8_t USBD_ECM_EP0_RxReady (USBD_HandleTypeDef *pdev)
{
return USBD_OK;
}
static const uint8_t *USBD_ECM_GetFSCfgDesc (uint16_t *length)
{
*length = USBD_CfgFSDesc_len;
return USBD_CfgFSDesc_pnt;
}
uint8_t USBD_ECM_RegisterInterface(USBD_HandleTypeDef *pdev)
{
unsigned index;
return USBD_OK;
}
void USBD_ECM_PMAConfig(PCD_HandleTypeDef *hpcd, uint32_t *pma_address)
{
/* allocate PMA memory for all endpoints associated with ECM */
HAL_PCDEx_PMAConfig(hpcd, ECM_DATA_IN_EP, PCD_SNG_BUF, *pma_address);
*pma_address += ECM_DATA_IN_SZ;
HAL_PCDEx_PMAConfig(hpcd, ECM_DATA_OUT_EP, PCD_SNG_BUF, *pma_address);
*pma_address += ECM_DATA_OUT_SZ;
HAL_PCDEx_PMAConfig(hpcd, ECM_NOTIFICATION_IN_EP, PCD_SNG_BUF, *pma_address);
*pma_address += ECM_NOTIFICATION_IN_SZ;
}
bool usb_ecm_can_xmit(void)
{
bool outcome;
__disable_irq();
outcome = can_xmit;
__enable_irq();
return outcome;
}
void usb_ecm_xmit_packet(struct pbuf *p)
{
struct pbuf *q;
int packet_size;
uint8_t *data;
if (!registered_pdev || !can_xmit)
return;
data = ecm_tx_buffer;
packet_size = 0;
for(q = p; q != NULL; q = q->next)
{
memcpy(data, q->payload, q->len);
data += q->len;
packet_size += q->len;
}
__disable_irq();
can_xmit = false;
ecm_tx_ptr = ecm_tx_buffer;
ecm_tx_remaining = packet_size;
copy_length = packet_size;
__enable_irq();
}