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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
/**
* USBPERIPHERAL Peripheral Source File
* @file usb_peripheral.c
* @ingroup usb_peripheral
* @brief Interface for a usb_peripheral module that needs to be implemented by a device specific USB module driver.
* @version USB Device Stack HAL Driver Version 1.0.0
*/
/*
(c) 2021 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <usb_common_elements.h>
#include <usb_config.h>
#include <usb_peripheral.h>
#include <usb_peripheral_avr_du.h>
#include <usb_protocol_headers.h>
STATIC USB_CONTROL_TRANSFER_t controlTransfer __attribute__((aligned(2))) = { .transferDataPtr = controlTransfer.buffer };
bool USB_SetupIsReceived(void)
{
return USB_SetupInterruptIs();
}
bool USB_EventSOFIsReceived(void)
{
return USB_SOFInterruptIs();
}
void USB_EventSOFClear(void)
{
USB_SOFInterruptClear();
}
bool USB_EventResetIsReceived(void)
{
return USB_ResetInterruptIs();
}
void USB_EventResetClear(void)
{
USB_ResetInterruptClear();
}
uint8_t USB_EventOverUnderflowIsReceived(void)
{
uint8_t eventOverUnderflow = 0;
if (USB_OverflowInterruptIs() == true)
{
eventOverUnderflow |= (uint8_t)OVERFLOW_EVENT;
}
if (USB_UnderflowInterruptIs() == true)
{
eventOverUnderflow |= (uint8_t)UNDERFLOW_EVENT;
}
return eventOverUnderflow;
}
uint8_t USB_ControlOverUnderflowIsReceived(void)
{
uint8_t eventOverUnderflow = 0;
if (USB_EndpointOutOverUnderflowIsSet(0) == true)
{
eventOverUnderflow |= (uint8_t)OVERFLOW_EVENT;
USB_EndpointOutOverUnderflowAck(0);
}
if (USB_EndpointInOverUnderflowIsSet(0) == true)
{
eventOverUnderflow |= (uint8_t)UNDERFLOW_EVENT;
USB_EndpointInOverUnderflowAck(0);
}
return eventOverUnderflow;
}
void USB_EventOverUnderflowClear(void)
{
USB_OverflowInterruptClear();
USB_UnderflowInterruptClear();
}
bool USB_EventSuspendIsReceived(void)
{
return USB_SuspendInterruptIs();
}
void USB_EventSuspendClear(void)
{
USB_SuspendInterruptClear();
}
bool USB_EventResumeIsReceived(void)
{
return USB_ResumeInterruptIs();
}
void USB_EventResumeClear(void)
{
USB_ResumeInterruptClear();
}
bool USB_EventStalledIsReceived(void)
{
return USB_StalledInterruptIs();
}
void USB_EventStalledClear(void)
{
USB_StalledInterruptClear();
}
void USB_BusAttach(void)
{
USB_ConnectionAttach();
}
void USB_BusDetach(void)
{
USB_ConnectionDetach();
}
bool USB_IsBusAttached(void)
{
return USB_ConnectionIsAttach();
}
void USB_DeviceAddressConfigure(uint8_t deviceAddress)
{
USB_DeviceAddressSet(deviceAddress);
}
uint16_t USB_FrameNumberGet(void)
{
return (USB_FrameNumGet());
}
RETURN_CODE_t USB_ControlEndpointsInit(void)
{
RETURN_CODE_t status = UNINITIALIZED;
USB_PIPE_t controlPipeOut = { .address = 0, .direction = USB_EP_DIR_OUT };
USB_PIPE_t controlPipeIn = { .address = 0, .direction = USB_EP_DIR_IN };
status = USB_EndpointConfigure(controlPipeOut, USB_EP0_SIZE, CONTROL);
if (SUCCESS == status)
{
status = USB_EndpointConfigure(controlPipeIn, USB_EP0_SIZE, CONTROL);
}
if (SUCCESS == status)
{
status = EndpointBufferSet(controlPipeOut, controlTransfer.buffer);
if (SUCCESS == status)
{
status = EndpointBufferSet(controlPipeIn, controlTransfer.buffer);
}
}
if (SUCCESS == status)
{
status = USB_DataToggleClear(controlPipeOut);
if (SUCCESS == status)
{
status = USB_DataToggleSet(controlPipeIn);
}
}
if (SUCCESS == status)
{
controlTransfer.status = USB_CONTROL_SETUP;
}
return status;
}
RETURN_CODE_t USB_ControlSetupReceived(void)
{
RETURN_CODE_t status = UNINITIALIZED;
USB_SetupInterruptClear();
if (USB_CONTROL_STALL_REQ == controlTransfer.status)
{
// Stall events are handled by the EventHandler.
status = SUCCESS;
}
else
{
// Acks Setup Received on the control endpoints.
USB_EndpointOutSetupReceivedAck(0u);
USB_EndpointInSetupCompleteAck(0u);
// Clears bytes received and sent
USB_NumberBytesToSendReset(0u);
USB_NumberBytesSentReset(0u);
USB_NumberBytesToReceiveReset(0u);
USB_NumberBytesReceivedReset(0u);
// Copies setup packet out of buffer to make it available for a data stage.
(void)memcpy((uint8_t *)(&controlTransfer.setupRequest), controlTransfer.buffer, sizeof(USB_SETUP_REQUEST_t));
// The processSetupCallback is in most cases the USB_SetupProcess function in usb_core.c.
if (controlTransfer.processSetupCallback != NULL)
{
RETURN_CODE_t setup_status = controlTransfer.processSetupCallback(&controlTransfer.setupRequest);
if (UNSUPPORTED == setup_status)
{
// Setup Request unknown or rejected, stalls the next control transaction.
controlTransfer.status = USB_CONTROL_STALL_REQ;
USB_EndpointInStall(0u);
USB_EndpointOutStall(0u);
status = SUCCESS;
}
else if (SUCCESS == setup_status)
{
if (0u == controlTransfer.transferDataSize)
{
// Request did not contain a data stage, sends ZLP directly.
status = USB_ControlTransferZLP(USB_EP_DIR_IN);
}
else
{
// Sends or Receives data in next stage of request.
controlTransfer.totalBytesTransferred = 0;
USB_PIPE_t controlPipe = { .address = 0u, .direction = USB_EP_DIR_IN };
if ((controlTransfer.setupRequest.bmRequestType.dataPhaseTransferDirection) == USB_REQUEST_DIR_IN)
{
controlTransfer.status = USB_CONTROL_DATA_IN;
}
else
{
// Control OUT data transactions are controlled by the IN.DATAPTR so set specifically here.
status = EndpointBufferSet(controlPipe, controlTransfer.transferDataPtr);
controlPipe.direction = USB_EP_DIR_OUT;
controlTransfer.status = USB_CONTROL_DATA_OUT;
}
// Sets up the pipe variables.
USB_PipeDataTransferredSizeReset(controlPipe);
USB_PipeDataPtrSet(controlPipe, controlTransfer.transferDataPtr);
USB_PipeDataToTransferSizeSet(controlPipe, controlTransfer.transferDataSize);
// Start data stage transaction.
status = USB_ControlTransactionComplete(controlPipe);
}
}
else
{
// Forward error from setup.
status = setup_status;
}
}
else
{
// processSetupCallback missing, return error.
status = CONTROL_SETUP_CALLBACK_ERROR;
}
}
return status;
}
RETURN_CODE_t USB_ControlTransactionComplete(USB_PIPE_t pipe)
{
RETURN_CODE_t status = UNINITIALIZED;
if (pipe.address != 0)
{
status = ENDPOINT_ADDRESS_ERROR;
}
else
{
// The data stage and the status stage must always have the data toggle bit set.
status = USB_DataToggleSet(pipe);
}
if (SUCCESS == status)
{
switch (controlTransfer.status)
{
case USB_CONTROL_DATA_IN:
{
pipe.direction = USB_EP_DIR_IN;
// Updates bytes sent and to be sent.
uint16_t bytesSent = USB_PipeDataTransferredSizeGet(pipe);
bytesSent += USB_NumberBytesSentGet(pipe.address);
USB_PipeDataTransferredSizeSet(pipe, bytesSent);
uint16_t transferDataSize = USB_PipeDataToTransferSizeGet(pipe);
// Checks remaining data to send.
if (0U == (transferDataSize - bytesSent))
{
controlTransfer.totalBytesTransferred += bytesSent;
if (controlTransfer.transferDataSize == controlTransfer.totalBytesTransferred)
{
// Data stage is complete, sends an OUT ZLP for status stage.
status = USB_ControlTransferZLP(USB_REQUEST_DIR_OUT);
}
else
{
// Data stage is not complete, checks if we need a new buffer.
if ((controlTransfer.overUnderRunCallback != NULL) && (SUCCESS == controlTransfer.overUnderRunCallback()))
{
USB_PipeDataTransferredSizeReset(pipe);
USB_PipeDataToTransferSizeSet(pipe, controlTransfer.transferDataSize);
}
// If no new buffer a normal in ZLP will get sent for the data stage.
status = USB_InTransactionRun(pipe);
}
}
else
{
// Starts next transaction in data stage.
status = USB_InTransactionRun(pipe);
}
USB_EndpointInOverUnderflowAck(0);
break;
}
case USB_CONTROL_DATA_OUT:
{
pipe.direction = USB_EP_DIR_OUT;
// Updates bytes received and to be received.
uint16_t bytesReceived = USB_PipeDataTransferredSizeGet(pipe);
bytesReceived += USB_NumberBytesReceivedGet(pipe.address);
USB_PipeDataTransferredSizeSet(pipe, bytesReceived);
uint16_t transferDataSize = USB_PipeDataToTransferSizeGet(pipe);
if (0U == (transferDataSize - bytesReceived))
{
controlTransfer.totalBytesTransferred += bytesReceived;
if (controlTransfer.transferDataSize == controlTransfer.totalBytesTransferred)
{
// Data stage is complete, sends an IN ZLP for status stage.
status = USB_ControlTransferZLP(USB_REQUEST_DIR_IN);
}
else
{
// Data stage is not complete, checks if we need a new buffer.
if ((controlTransfer.overUnderRunCallback != NULL) && (SUCCESS == controlTransfer.overUnderRunCallback()))
{
USB_PipeDataTransferredSizeReset(pipe);
USB_PipeDataToTransferSizeSet(pipe, controlTransfer.transferDataSize);
status = USB_InTransactionRun(pipe);
}
else
{
// If no new buffer the next transaction will be stalled.
controlTransfer.status = USB_CONTROL_STALL_REQ;
USB_EndpointInStall(0);
USB_EndpointOutStall(0);
status = SUCCESS;
}
}
}
else
{
// Starts next transaction in data stage.
status = USB_OutTransactionRun(pipe);
}
USB_EndpointOutOverUnderflowAck(0);
break;
}
case USB_CONTROL_ZLP:
{
// Valid end of setup request.
if (controlTransfer.endOfRequestCallback != NULL)
{
controlTransfer.endOfRequestCallback();
}
// Reinitializes control endpoint management.
status = USB_ControlTransferReset();
break;
}
case USB_CONTROL_SETUP:
{
status = USB_ControlTransferReset();
break;
}
default:
{
status = CONTROL_TRANSACTION_STATUS_ERROR;
break;
}
}
}
return status;
}
RETURN_CODE_t USB_ControlTransferZLP(uint8_t direction)
{
RETURN_CODE_t status = UNINITIALIZED;
USB_NumberBytesToSendReset(0);
USB_NumberBytesSentReset(0);
USB_NumberBytesToReceiveReset(0u);
USB_NumberBytesReceivedReset(0u);
// Prepare to receive a new setup package in case the host decides to ignore the ZLP stage
USB_PipeDataPtrSet((USB_PIPE_t){ .address = 0, .direction = USB_REQUEST_DIR_OUT }, controlTransfer.buffer);
status = EndpointBufferSet((USB_PIPE_t){ .address = 0, .direction = USB_REQUEST_DIR_OUT }, controlTransfer.buffer);
if (SUCCESS == status)
{
controlTransfer.status = USB_CONTROL_ZLP;
// Starts the ZLP transaction
if (direction == USB_REQUEST_DIR_IN)
{
USB_EndpointInNAKClear(0);
USB_EndpointInOverUnderflowAck(0);
}
else
{
USB_EndpointOutNAKClear(0);
USB_EndpointOutOverUnderflowAck(0);
}
}
return status;
}
RETURN_CODE_t USB_ControlTransferReset(void)
{
RETURN_CODE_t status = UNINITIALIZED;
USB_PIPE_t controlPipeOut = { .address = 0u, .direction = USB_EP_DIR_OUT };
// Aborts any ongoing transaction and resets the endpoint statuses
status = USB_TransactionAbort(controlPipeOut);
if (SUCCESS == status)
{
USB_PIPE_t controlPipeIn = { .address = 0u, .direction = USB_EP_DIR_IN };
status = USB_TransactionAbort(controlPipeIn);
}
if (SUCCESS == status)
{
USB_EndpointOutStatusClear(0u);
USB_EndpointInStatusClear(0u);
// Prepare for receiving a new request packet
USB_PipeDataPtrSet(controlPipeOut, controlTransfer.buffer);
USB_PipeDataToTransferSizeSet(controlPipeOut, sizeof(USB_SETUP_REQUEST_t));
USB_PipeDataTransferredSizeReset(controlPipeOut);
USB_PipeTransferEndCallbackRegister(controlPipeOut, NULL);
// Clears the endpoint count registers
USB_NumberBytesToSendReset(0u);
USB_NumberBytesSentReset(0u);
USB_NumberBytesToReceiveReset(0u);
USB_NumberBytesReceivedReset(0u);
// Resets the control transfer variables
controlTransfer.endOfRequestCallback = NULL;
controlTransfer.overUnderRunCallback = NULL;
controlTransfer.transferDataSize = 0u;
controlTransfer.status = USB_CONTROL_SETUP;
}
return status;
}
RETURN_CODE_t USB_ControlTransferDataSet(uint8_t *dataPtr, uint16_t dataSize)
{
RETURN_CODE_t status = UNINITIALIZED;
if (dataPtr == NULL)
{
controlTransfer.transferDataPtr = controlTransfer.buffer;
if (0u != dataSize)
{
status = CONTROL_SIZE_ERROR;
}
else
{
status = SUCCESS;
}
}
else
{
controlTransfer.transferDataPtr = dataPtr;
status = SUCCESS;
}
controlTransfer.transferDataSize = dataSize;
return status;
}
RETURN_CODE_t USB_ControlTransferDataWriteBuffer(uint8_t *dataPtr, uint8_t dataSize)
{
RETURN_CODE_t status = UNINITIALIZED;
if (USB_EP0_SIZE < dataSize)
{
status = CONTROL_SIZE_ERROR;
}
else
{
(void)memcpy(controlTransfer.buffer, dataPtr, dataSize);
controlTransfer.transferDataPtr = controlTransfer.buffer;
controlTransfer.transferDataSize = dataSize;
status = SUCCESS;
}
return status;
}
void USB_ControlEndOfRequestCallbackRegister(USB_SETUP_ENDOFREQUEST_CALLBACK_t callback)
{
controlTransfer.endOfRequestCallback = callback;
}
void USB_ControlProcessSetupCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback)
{
controlTransfer.processSetupCallback = callback;
}
void USB_ControlOverUnderRunCallbackRegister(USB_SETUP_OVERUNDERRUN_CALLBACK_t callback)
{
controlTransfer.overUnderRunCallback = callback;
}
RETURN_CODE_t USB_ControlProcessOverUnderflow(uint8_t overunderflow)
{
RETURN_CODE_t status = UNINITIALIZED;
if (USB_CONTROL_DATA_IN == controlTransfer.status)
{
if (OVERFLOW_EVENT == overunderflow)
{
// Host is done with the data stage and expects an OUT ZLP
status = USB_ControlTransferZLP(USB_REQUEST_DIR_OUT);
}
else
{
// Host is too eager, let this be handled by the transfer handler
status = SUCCESS;
}
}
else if (USB_CONTROL_DATA_OUT == controlTransfer.status)
{
if (UNDERFLOW_EVENT == overunderflow)
{
// Host is done with the data stage and expects an IN ZLP
status = USB_ControlTransferZLP(USB_REQUEST_DIR_IN);
}
else
{
// Host is too eager, let this be handled by the transfer handler
status = SUCCESS;
}
}
else
{
// Remaining control statuses ignores overflow and underflow events
status = SUCCESS;
}
return status;
}
RETURN_CODE_t USB_HandleEventStalled(USB_PIPE_t pipe)
{
RETURN_CODE_t status = UNINITIALIZED;
USB_EndpointInStallAck(pipe.address);
USB_EndpointOutStallAck(pipe.address);
USB_EndpointInStallClear(pipe.address);
USB_EndpointOutStallClear(pipe.address);
if (0u == pipe.address)
{
// Reinitializes control endpoint management.
status = USB_ControlTransferReset();
}
else
{
status = USB_TransactionAbort(pipe);
}
return status;
}
void USB_PeripheralInitialize(void)
{
USB_Enable();
USB_FrameNumEnable();
USB_FifoEnable();
USB_FifoReadPointerReset();
USB_FifoWritePointerReset();
USB_EndpointTableAddressSet(endpointTable.EP);
USB_MaxEndpointsSet(USB_EP_NUM - 1u);
USB_InterruptFlagsClear();
// Reset endpoints table
for (uint8_t endpoint = 0; endpoint < (uint8_t)USB_EP_NUM; endpoint++)
{
endpointTable.EP[endpoint].OUT.CTRL = 0;
endpointTable.EP[endpoint].OUT.STATUS = 0;
endpointTable.EP[endpoint].IN.CTRL = 0;
endpointTable.EP[endpoint].IN.STATUS = 0;
}
}
void USB_PeripheralDisable(void)
{
USB_Disable();
USB_DeviceAddressReset();
}
|