-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCH56x_usb20_devbulk.c
767 lines (729 loc) · 22.3 KB
/
CH56x_usb20_devbulk.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/********************************** (C) COPYRIGHT *******************************
* File Name : CH56x_usb20_devbulk.c
* Author : WCH, bvernoux
* Version : V1.1
* Date : 2022/08/20
* Description :
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH56x_common.h"
#include "CH56x_usb20_devbulk.h"
#include "CH56x_usb30_devbulk.h"
#include "CH56x_usb30_devbulk_LIB.h"
#include "CH56x_usb_devbulk_desc_cmd.h"
#include "CH56x_debug_log.h"
//#define DEBUG_USB2_REQ 1 // Debug USB Req (can have impact real-time to correctly enumerate USB2)
//#define DEBUG_USB2_EP0 1 // Debug ON EP0 have timing issue with real-time to enumerate USB2
//#define DEBUG_USB2_EPX 1 // EPX is EP1 to EP7
/*
* This routine is a USB 2.0 device usage routine
* After the host downloads through endpoint 1, it can upload a package through endpoint 1
* The host can directly and continuously fetch data from endpoint 2
*/
/* Global define */
/* Global Variable */
vuint16_t U20_EndpnMaxSize = 512;
vuint16_t SetupReqLen=0; //Host request data length
vuint16_t SetupLen = 0; //The length of data actually sent or received during the data phase
vuint32_t seq_num = 0;
DevInfo_Typedef g_devInfo;
static vuint8_t SetupReqType = 0; //Host request descriptor type
static vuint8_t SetupReq = 0; //Host request descriptor type
static puint8_t pDescr;
extern vuint8_t link_sta;
static puint8_t pUEP1_TX_data;
static puint8_t pUEP1_RX_data;
vuint32_t EP1_OUT_seq_num = 0;
__attribute__ ((aligned(16))) uint8_t vendor_buff[16] __attribute__((section(".DMADATA"))); //Endpoint 0 data transceiver buffer
/* Function declaration */
/*******************************************************************************
* @fn USB2_force
*
* @brief Switch to USB2 even if USB3 is available
*
* @return None
*/
void USB2_force(void)
{
USBSS->LINK_INT_FLAG = LINK_DISABLE_FLAG;
USB30D_init(DISABLE);
PFIC_DisableIRQ(USBSS_IRQn);
R8_TMR0_CTRL_MOD = RB_TMR_ALL_CLEAR;
R8_TMR0_INTER_EN = 0;
PFIC_DisableIRQ(TMR0_IRQn);
PFIC_EnableIRQ(USBHS_IRQn);
USB20_Device_Init(ENABLE);
}
/*******************************************************************************
* @fn USB20_Endp_Init
*
* @brief USB2.0 HS endpoint initialization
*
* @return None
*/
void USB20_Endp_Init(void)
{
R8_UEP4_1_MOD = RB_UEP1_RX_EN | RB_UEP1_TX_EN | RB_UEP4_RX_EN | RB_UEP4_TX_EN;
R8_UEP2_3_MOD = RB_UEP2_RX_EN | RB_UEP2_TX_EN | RB_UEP3_RX_EN | RB_UEP3_TX_EN;
R8_UEP5_6_MOD = RB_UEP5_RX_EN | RB_UEP5_TX_EN | RB_UEP6_RX_EN | RB_UEP6_TX_EN;
R8_UEP7_MOD = RB_UEP7_RX_EN | RB_UEP7_TX_EN; // endpoint send and receive enable
R16_UEP0_MAX_LEN = 64;
R16_UEP1_MAX_LEN = 512;
R16_UEP2_MAX_LEN = 512;
R32_UEP0_RT_DMA = (uint32_t)(uint8_t *)endp0RTbuff;
R32_UEP1_TX_DMA = (uint32_t)(uint8_t *)endp1Tbuff;
R32_UEP1_RX_DMA = (uint32_t)(uint8_t *)endp1Rbuff;
R32_UEP2_TX_DMA = (uint32_t)(uint8_t *)endp2RTbuff;
R32_UEP2_RX_DMA = (uint32_t)(uint8_t *)endp2RTbuff;
R16_UEP0_T_LEN = 0;
R8_UEP0_TX_CTRL = 0;
R8_UEP0_RX_CTRL = 0;
R16_UEP1_T_LEN = U20_MAXPACKET_LEN;
R8_UEP1_TX_CTRL = UEP_T_RES_ACK | RB_UEP_T_TOG_0;
R8_UEP1_RX_CTRL = UEP_R_RES_ACK | RB_UEP_R_TOG_0;
R16_UEP2_T_LEN = U20_MAXPACKET_LEN;
R8_UEP2_TX_CTRL = UEP_T_RES_ACK | RB_UEP_T_TOG_0;
R8_UEP2_RX_CTRL = UEP_R_RES_ACK | RB_UEP_R_TOG_0;
R16_UEP3_T_LEN = 0;
R8_UEP3_TX_CTRL = UEP_T_RES_NAK;
R8_UEP3_RX_CTRL = UEP_R_RES_NAK;
R16_UEP4_T_LEN = 0;
R8_UEP4_TX_CTRL = UEP_T_RES_NAK ;
R8_UEP4_RX_CTRL = UEP_R_RES_NAK;
R16_UEP5_T_LEN = 0;
R8_UEP5_TX_CTRL = UEP_T_RES_NAK;
R8_UEP5_RX_CTRL = UEP_R_RES_NAK;
R16_UEP6_T_LEN = 0;
R8_UEP6_TX_CTRL = UEP_T_RES_NAK;
R8_UEP6_RX_CTRL = UEP_R_RES_NAK;
R16_UEP7_T_LEN = 0;
R8_UEP7_TX_CTRL = UEP_T_RES_NAK;
R8_UEP7_RX_CTRL = UEP_R_RES_NAK;
}
/*******************************************************************************
* @fn USB20_Device_Init
*
* @brief USB2.0 device initialization
*
* @param sta -
* ENABLE
* DISABLE
*
* @return None
*/
void USB20_Device_Init(FunctionalState sta)
{
if(sta)
{
R8_USB_CTRL = 0;
R8_USB_CTRL = UCST_HS | RB_DEV_PU_EN | RB_USB_INT_BUSY |RB_USB_DMA_EN;
R8_USB_INT_EN = RB_USB_IE_SETUPACT | RB_USB_IE_TRANS | RB_USB_IE_SUSPEND |RB_USB_IE_BUSRST ;
USB20_Endp_Init();
}
else
{
R8_USB_CTRL = RB_USB_CLR_ALL | RB_USB_RESET_SIE;
}
}
/*******************************************************************************
* @fn USB20_Device_setaddress
*
* @brief USB2.0 set device address (called from USBHS_IRQHandler())
*
* @param address
*
* @return None
**/
void USB20_Device_Setaddress(uint32_t address)
{
R8_USB_DEV_AD = address; // SET ADDRESS
}
/*******************************************************************************
* @fn U20_NonStandard_Request
*
* @brief USB2.0 non standard request.
*
* @return None
*/
uint16_t U20_NonStandard_Request(void)
{
uint16_t len = 0;
uint8_t endp_dir;
SetupLen = 0;
endp_dir = UsbSetupBuf->bRequestType & 0x80;
switch(SetupReq)
{
case 0x01: // Microsoft OS 2.0 Descriptors
switch(UsbSetupBuf->wIndex.bw.bb1)
{
case 0x04:
#if DEBUG_USB2_REQ
cprintf(" 0104:CompactId\n");
#endif
SetupLen = ( SetupReqLen > sizeof(USB_CompatId) )? sizeof(USB_CompatId):SetupReqLen;
pDescr = (puint8_t)USB_CompatId;
break;
case 0x05:
#if DEBUG_USB2_REQ
cprintf(" 0105:PropertyHeader\n");
#endif
SetupLen = ( SetupReqLen > sizeof(USB_PropertyHeader) )? sizeof(USB_PropertyHeader):SetupReqLen;
pDescr = (puint8_t)USB_PropertyHeader;
break;
case 0x06:
#if DEBUG_USB2_REQ
cprintf(" 0106:Undefined\n");
#endif
break;
case 0x07:
#if DEBUG_USB2_REQ
cprintf(" 0107:MSOS20DescriptorSet\n");
#endif
SetupLen = ( SetupReqLen > sizeof(USB_MSOS20DescriptorSet) )? sizeof(USB_MSOS20DescriptorSet):SetupReqLen;
pDescr = (puint8_t)USB_MSOS20DescriptorSet;
break;
case 0x08:
#if DEBUG_USB2_REQ
cprintf(" 0108:Undefined\n");
#endif
break;
case 0x09://
#if DEBUG_USB2_REQ
cprintf(" 0109:Undefined\n");
#endif
break;
case 0xc0:
#if DEBUG_USB2_REQ
cprintf(" 01C0:Undefined\n");
#endif
break;
default:
#if DEBUG_USB2_REQ
cprintf(" 01D:INVALID_REQ_CODE\n");
#endif
return USB_DESCR_UNSUPPORTED;
break;
}
break;
case 0x02: // user-defined command
switch(UsbSetupBuf->wIndex.bw.bb1)
{
/*
case 0x05:
cprintf(" NS2:PropertyHeader\n");
SetupLen = ( SetupReqLen > sizeof(PropertyHeader) )? sizeof(PropertyHeader):SetupReqLen;
pDescr = (puint8_t)PropertyHeader;
break;
*/
default:
#if DEBUG_USB2_REQ
cprintf(" 02D:INVALID_REQ_CODE\n");
#endif
return USB_DESCR_UNSUPPORTED;
break;
}
break;
default:
#if DEBUG_USB2_REQ
cprintf(" D:INVALID_REQ_CODE\n");
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
/* Determine whether it can be processed normally */
if( (SetupLen != USB_DESCR_UNSUPPORTED) && (SetupLen != 0))
{
len = ( SetupLen >= U20_UEP0_MAXSIZE ) ? U20_UEP0_MAXSIZE : SetupLen; // Return send length
if(endp_dir)
{
memcpy( endp0RTbuff, pDescr, len ); // Data stage prepares data to buffer for IN
pDescr += len;
}
SetupLen -= len; // Remaining unsent data length
}
return len;
}
/*******************************************************************************
* @fn U20_Standard_Request
*
* @brief USB2.0 standard request (called from USBHS_IRQHandler())
*
* @return None
*/
uint16_t U20_Standard_Request()
{
uint16_t len = 0;
uint8_t endp_dir;
SetupLen = 0;
endp_dir = UsbSetupBuf->bRequestType & 0x80;
switch( SetupReq )
{
case USB_GET_DESCRIPTOR:
{
switch(UsbSetupBuf->wValue.bw.bb0)
{
case USB_DESCR_TYP_DEVICE: /* Get device descriptor */
pDescr = (uint8_t *)USB_HS_DeviceDescriptor;
SetupLen = ( SetupReqLen > sizeof(USB_HS_DeviceDescriptor) )? sizeof(USB_HS_DeviceDescriptor):SetupReqLen;
break;
case USB_DESCR_TYP_CONFIG: /* Get configuration descriptor */
pDescr = (uint8_t *)USB_HS_ConfigDescriptor;
SetupLen = ( SetupReqLen > sizeof(USB_HS_ConfigDescriptor) )? sizeof(USB_HS_ConfigDescriptor):SetupReqLen;
break;
case USB_DESCR_TYP_BOS: /* Get BOS Descriptor */
pDescr =(uint8_t *)USB_BOSDescriptor;
SetupLen = ( SetupReqLen > sizeof(USB_BOSDescriptor) )? sizeof(USB_BOSDescriptor):SetupReqLen;
break;
case USB_DESCR_TYP_STRING: /* Get string descriptor */
switch(UsbSetupBuf->wValue.bw.bb1)
{
case USB_DESCR_LANGID_STRING: /* Language string descriptor */
pDescr = (uint8_t *)USB_StringLangID;
SetupLen = ( SetupReqLen > sizeof(USB_StringLangID) )? sizeof(USB_StringLangID):SetupReqLen;
break;
case USB_DESCR_VENDOR_STRING: /* Manufacturer String Descriptor */
pDescr = (uint8_t *)USB_StringVendor;
SetupLen = ( SetupReqLen > sizeof(USB_StringVendor) )? sizeof(USB_StringVendor):SetupReqLen;
break;
case USB_DESCR_PRODUCT_STRING: /* Product String Descriptor */
pDescr =(uint8_t *) USB_HS_StringProduct;
SetupLen = ( SetupReqLen > sizeof(USB_HS_StringProduct) )? sizeof(USB_HS_StringProduct):SetupReqLen;
break;
case USB_DESCR_SERIAL_STRING: /* Serial String Descriptor */
pDescr = (uint8_t *)USB_StringSerial;
SetupLen = ( SetupReqLen > sizeof(USB_StringSerial) )? sizeof(USB_StringSerial):SetupReqLen;
break;
case USB_DESCR_OS_STRING: /* Microsoft OS String Descriptor */
pDescr = (uint8_t *)USB_OSStringDescriptor;
SetupLen = ( SetupReqLen > sizeof(USB_OSStringDescriptor) )? sizeof(USB_OSStringDescriptor):SetupReqLen;
break;
default:
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
break;
default :
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
}
break;
case USB_SET_ADDRESS: /* Set Address */
g_devInfo.dev_addr = UsbSetupBuf->wValue.bw.bb1;
break;
case USB_GET_CONFIGURATION: /* Get configuration value */
endp0RTbuff[ 0 ] = g_devInfo.dev_config_value;
SetupLen = 1;
break;
case USB_SET_CONFIGURATION: /* Set configuration value */
if( (R8_USB_SPD_TYPE & RB_USBSPEED_MASK) == UST_FS )
{
U20_EndpnMaxSize = 64;
}
else if( (R8_USB_SPD_TYPE & RB_USBSPEED_MASK) == UST_LS )
{
U20_EndpnMaxSize = 8;
}
g_devInfo.dev_config_value = UsbSetupBuf->wValue.bw.bb1;
g_devInfo.dev_enum_status = 0x01;
break;
case USB_CLEAR_FEATURE: /* Clear features */
if( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) /* Clear endpoint */
{
switch(UsbSetupBuf->wValue.bw.bb1) /* wIndexL */
{
case 0x81: /* endpoint 1 IN */
R16_UEP1_T_LEN = 0;
R8_UEP1_TX_CTRL = UEP_T_RES_NAK | RB_UEP_T_TOG_0;
break;
case 0x01: /* endpoint 1 OUT */
R8_UEP1_RX_CTRL = UEP_T_RES_ACK | RB_UEP_R_TOG_0;
break;
case 0x82: /* endpoint 2 IN */
R16_UEP2_T_LEN= 0;
R8_UEP2_TX_CTRL = UEP_T_RES_NAK | RB_UEP_T_TOG_0;
break;
case 0x02: /* endpoint 2 OUT */
R8_UEP2_TX_CTRL = UEP_R_RES_ACK | RB_UEP_R_TOG_0;
break;
default:
#if DEBUG_USB2_REQ
cprintf(" USB_CLEAR_FEATURE:USB_REQ_RECIP_ENDP UNSUPPORTED UsbSetupBuf->wValue.bw.bb1=0x%02X\n",
UsbSetupBuf->wValue.bw.bb1);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
}
else if( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE )
{
if( UsbSetupBuf->wValue.bw.bb1 == 1 ) /* wIndexL */
{
g_devInfo.dev_sleep_status &= ~0x01;
}
}
else
{
#if DEBUG_USB2_REQ
cprintf(" USB_CLEAR_FEATURE:UNSUPPORTED UsbSetupBuf->bRequestType=0x%02X\n", UsbSetupBuf->bRequestType);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
}
break;
case USB_SET_FEATURE: /* Set properties */
if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x00 ) /* Set up the device */
{
if( UsbSetupBuf->wValue.w == 0x01 )
{
if( USB_HS_ConfigDescriptor[7] & 0x20 ) /* Set wakeup enable flag */
{
g_devInfo.dev_sleep_status = 0x01;
}
else
{
#if DEBUG_USB2_REQ
cprintf(" USB_SET_FEATURE:UNSUPPORTED USB_HS_ConfigDescriptor[7]=0x%02X\n",
USB_HS_ConfigDescriptor[7]);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
}
}
else
{
#if DEBUG_USB2_REQ
cprintf(" USB_SET_FEATURE:UNSUPPORTED UsbSetupBuf->wValue=0x%04X\n", UsbSetupBuf->wValue.w);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
}
}
else if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x02 ) /* Set endpoint */
{
if( UsbSetupBuf->wValue.w == 0x00 ) /* Set the specified endpoint STALL */
{
switch( ( UsbSetupBuf->wIndex.w ) & 0xff )
{
case 0x81: /* Set endpoint 1 IN STALL */
R8_UEP1_TX_CTRL = ( R8_UEP1_TX_CTRL & ~RB_UEP_TRES_MASK ) | UEP_T_RES_STALL;
break;
case 0x01: /* Set endpoint 1 OUT STALL */
R8_UEP1_RX_CTRL = ( R8_UEP1_RX_CTRL & ~RB_UEP_RRES_MASK ) | UEP_R_RES_STALL;
break;
case 0x82: /* Set endpoint 2 IN STALL */
R8_UEP2_TX_CTRL = ( R8_UEP2_TX_CTRL & ~RB_UEP_TRES_MASK ) | UEP_T_RES_STALL;
break;
case 0x02: /* Set endpoint 2 OUT STALL */
R8_UEP2_RX_CTRL = ( R8_UEP2_RX_CTRL & ~RB_UEP_RRES_MASK ) | UEP_R_RES_STALL;
break;
default:
#if DEBUG_USB2_REQ
cprintf(" USB_SET_FEATURE: UNSUPPORTED Set endpoint X UsbSetupBuf->wIndex=0x%04X\n", UsbSetupBuf->wIndex.w);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
}
else
{
#if DEBUG_USB2_REQ
cprintf(" USB_SET_FEATURE: UNSUPPORTED UsbSetupBuf->wValue(!=0)=0x%04X\n", UsbSetupBuf->wValue.w);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
}
}
else
{
#if DEBUG_USB2_REQ
cprintf(" USB_SET_FEATURE: UNSUPPORTED UsbSetupBuf->bRequestType=0x%02X\n", UsbSetupBuf->bRequestType);
#endif
SetupLen = USB_DESCR_UNSUPPORTED;
}
break;
case USB_GET_INTERFACE:
#if DEBUG_USB2_REQ
cprintf(" USB_GET_INTERFACE: wValue=0x%04X wIndex=0x%04X\n", UsbSetupBuf->wValue.w, UsbSetupBuf->wIndex.w);
#endif
break;
case USB_SET_INTERFACE:
#if DEBUG_USB2_REQ
cprintf(" USB_SET_INTERFACE: wValue=0x%04X wIndex=0x%04X\n", UsbSetupBuf->wValue.w, UsbSetupBuf->wIndex.w);
#endif
break;
case USB_GET_STATUS: /* Reply according to the actual state of the current endpoint */
endp0RTbuff[ 0 ] = 0x00;
endp0RTbuff[ 1 ] = 0x00;
SetupLen = 2;
if( UsbSetupBuf->wIndex.w == 0x81 )
{
if( ( R8_UEP1_TX_CTRL & RB_UEP_TRES_MASK ) == UEP_T_RES_STALL )
{
endp0RTbuff[ 0 ] = 0x01;
SetupLen = 1;
}
}
else if( UsbSetupBuf->wIndex.w == 0x01 )
{
if( ( R8_UEP1_RX_CTRL & RB_UEP_RRES_MASK ) == UEP_R_RES_STALL )
{
endp0RTbuff[ 0 ] = 0x01;
SetupLen = 1;
}
}
else if( UsbSetupBuf->wIndex.w == 0x82 )
{
if( ( R8_UEP2_TX_CTRL & RB_UEP_TRES_MASK ) == UEP_T_RES_STALL )
{
endp0RTbuff[ 0 ] = 0x01;
SetupLen = 1;
}
}
else if( UsbSetupBuf->wIndex.w == 0x02 )
{
if( ( R8_UEP2_RX_CTRL & RB_UEP_RRES_MASK ) == UEP_R_RES_STALL )
{
endp0RTbuff[ 0 ] = 0x01;
SetupLen = 1;
}
}
break;
default:
SetupLen = USB_DESCR_UNSUPPORTED;
break;
}
/* Determine whether it can be processed normally */
if( (SetupLen != USB_DESCR_UNSUPPORTED) && (SetupLen != 0))
{
len = ( SetupLen >= U20_UEP0_MAXSIZE ) ? U20_UEP0_MAXSIZE : SetupLen; // Return send length
if(endp_dir)
{
memcpy( endp0RTbuff, pDescr, len ); // The data stage prepares the data to the buffer for IN
pDescr += len;
}
SetupLen -= len; // Remaining unsent data length
}
return len;
}
/*******************************************************************************
* @fn USBHS_IRQHandler
*
* @brief USB2.0 HS Interrupt Handler.
*
* @return None
*/
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHS_IRQHandler(void)
{
uint32_t end_num;
uint32_t rx_token;
uint16_t ret_len;
uint8_t int_flg;
int_flg = R8_USB_INT_FG;
if( int_flg & RB_USB_IF_SETUOACT ) // SETUP interrupt
{
SetupReqType = UsbSetupBuf->bRequestType;
SetupReq = UsbSetupBuf->bRequest;
SetupReqLen = UsbSetupBuf->wLength; // Data length
/* Analyze host requests */
if((UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) != USB_REQ_TYP_STANDARD) // Non-standard request
{
#if DEBUG_USB2_REQ
cprintf("NSU2 :");
uint8_t *p8 = (uint8_t *)endp0RTbuff;
for(int i = 0; i < 8; i++)
{
cprintf("%02x ", *p8++);
}
cprintf("\n");
#endif
ret_len = U20_NonStandard_Request();
}
else // Standard request
{
#if DEBUG_USB2_REQ
cprintf("SU2 :");
uint8_t *p8 = (uint8_t *)endp0RTbuff;
for(int i=0; i<8; i++)
{
cprintf("%02x ", *p8++);
}
cprintf("\n");
#endif
ret_len = U20_Standard_Request();
}
if(ret_len == 0xFFFF) // Unsupported descriptor
{
R16_UEP0_T_LEN = 0;
R8_UEP0_TX_CTRL = UEP_T_RES_STALL ;
R8_UEP0_RX_CTRL = UEP_R_RES_STALL ;
}
else
{
R16_UEP0_T_LEN = ret_len;
R8_UEP0_TX_CTRL = UEP_T_RES_ACK | RB_UEP_T_TOG_1; // Data process or state process
R8_UEP0_RX_CTRL = UEP_R_RES_ACK | RB_UEP_R_TOG_1;
}
R8_USB_INT_FG = RB_USB_IF_SETUOACT; // Clear int flag
}
else if( int_flg & RB_USB_IF_TRANSFER ) // Transaction transfer complete interrupt
{
end_num = R8_USB_INT_ST & 0xf; // The endpoint currently generating the interrupt
rx_token = ( (R8_USB_INT_ST )>>4) & 0x3; // Interrupt Type 00: OUT, 01: SOF, 10: IN, 11: SETUP
#if 0
if( !(R8_USB_INT_ST & RB_USB_ST_TOGOK) )
{
cprintf(" TOG MATCH FAIL : ENDP %x token %x \n", end_num, rx_token);
}
#endif
switch( end_num ) // endpoint number
{
case 0: /* Endpoint 0 */
#if DEBUG_USB2_EP0
cprintf("EP0\n");
#endif
if( rx_token == PID_IN ) // Endpoint Zero Transmit Complete Interrupt
{
ret_len = U20_Endp0_IN_Callback();
if(ret_len == 0) // Data transmission completed
{
R8_UEP0_RX_CTRL = UEP_R_RES_ACK | RB_UEP_R_TOG_1; // The state process is OUT
R16_UEP0_T_LEN = 0; // Clear send length
R8_UEP0_TX_CTRL = 0; // Clear send controller
}
else
{
R16_UEP0_T_LEN = ret_len;
R8_UEP0_TX_CTRL ^= RB_UEP_T_TOG_1;
R8_UEP0_TX_CTRL = ( R8_UEP0_TX_CTRL &~RB_UEP_TRES_MASK )| UEP_T_RES_ACK ;
}
}
else if( rx_token == PID_OUT ) // Endpoint zero receive complete interrupt
{
SetupLen -= SetupLen > R16_USB_RX_LEN ? R16_USB_RX_LEN :SetupLen;
if( SetupLen > 0 ) // There is still data to be received
{
R8_UEP0_RX_CTRL ^=RB_UEP_R_TOG_1;
R8_UEP0_RX_CTRL = ( R8_UEP0_RX_CTRL &~RB_UEP_RRES_MASK) | UEP_R_RES_ACK;
}
else // No data received
{
R16_UEP0_T_LEN = 0; // Status process sends zero-length packets
R8_UEP0_TX_CTRL = UEP_T_RES_ACK | RB_UEP_T_TOG_1; // State process is IN
R8_UEP0_RX_CTRL = 0 ; // No data received Receive status zero
}
}
break;
case 1: /* Endpoint 1 */
#if DEBUG_USB2_EPX
cprintf("EP1\n");
#endif
if(rx_token == PID_IN)
{
//log_printf("EP1 IN\n");
pUEP1_TX_data += U20_MAXPACKET_LEN;
memcpy(endp1Tbuff, pUEP1_TX_data, U20_MAXPACKET_LEN);
// Flip the synchronization trigger bit to be sent next time bulk transfer data0 data1 flip back and forth
R32_UEP1_TX_DMA = (uint32_t)(uint8_t *)endp1Tbuff;
R8_UEP1_TX_CTRL ^= RB_UEP_T_TOG_1;
// The endpoint status is set to ACK, if the transmission length remains unchanged,
// there is no need to rewrite the R16_UEP1_T_LEN register
R8_UEP1_TX_CTRL = (R8_UEP1_TX_CTRL & ~RB_UEP_TRES_MASK) | UEP_T_RES_ACK;
}
else if(rx_token == PID_OUT)
{
if(EP1_OUT_seq_num == 0)
{
pUEP1_TX_data = (uint8_t *)endp1Tbuff;
pUEP1_RX_data = (uint8_t *)endp1Rbuff;
}
if(EP1_OUT_seq_num < 7)
{
pUEP1_RX_data += U20_MAXPACKET_LEN;
R32_UEP1_RX_DMA = (uint32_t)(uint8_t *)pUEP1_RX_data;
}
EP1_OUT_seq_num++;
if(EP1_OUT_seq_num == 8) // 4096 Bytes Buffer (8x512 Bytes)
{
usb_cmd_rx(USB_TYPE_USB2, endp1Rbuff, endp1Tbuff);
EP1_OUT_seq_num = 0;
pUEP1_RX_data = (uint8_t *)endp1Rbuff;
R32_UEP1_RX_DMA = (uint32_t)(uint8_t *)pUEP1_RX_data;
}
R8_UEP1_RX_CTRL ^= RB_UEP_R_TOG_1;
R8_UEP1_RX_CTRL = (R8_UEP1_RX_CTRL &~RB_UEP_RRES_MASK)|UEP_R_RES_ACK;
}
break;
case 2: /* Endpoint 2 */
#if DEBUG_USB2_EPX
cprintf("EP2\n");
#endif
if(rx_token == PID_IN)
{
// Flip the synchronization trigger bit to be sent next time bulk transfer data0 data1 flip back and forth
R32_UEP2_TX_DMA = (uint32_t)(uint8_t *)endp2RTbuff;
R8_UEP2_TX_CTRL ^= RB_UEP_T_TOG_1;
// The endpoint status is set to ACK, if the transmission length remains unchanged,
// there is no need to rewrite the R16_UEP2_T_LEN register
R8_UEP2_TX_CTRL = (R8_UEP2_TX_CTRL & ~RB_UEP_TRES_MASK) | UEP_T_RES_ACK;
}
else if(rx_token == PID_OUT)
{
R32_UEP2_RX_DMA = (uint32_t)(uint8_t *)endp2RTbuff;
R8_UEP2_RX_CTRL ^= RB_UEP_R_TOG_1;
R8_UEP2_RX_CTRL = (R8_UEP2_RX_CTRL &~RB_UEP_RRES_MASK)|UEP_R_RES_ACK;
}
break;
default:
break;
}
R8_USB_INT_FG = RB_USB_IF_TRANSFER; // Clear USB transaction complete interrupt
}
else if( int_flg & RB_USB_IF_BUSRST ) // Bus reset interrupt
{
g_DeviceConnectstatus = USB_INT_DISCONNECT;
g_DeviceUsbType = 0;
USB20_Endp_Init();
USB20_Device_Setaddress( 0 ); // Bus reset address clear to 0
R8_USB_INT_FG = RB_USB_IF_BUSRST;
if( link_sta == 1 )
{
PFIC_EnableIRQ(USBSS_IRQn);
PFIC_EnableIRQ(LINK_IRQn);
PFIC_EnableIRQ(TMR0_IRQn);
R8_TMR0_INTER_EN = 1;
TMR0_TimerInit( 67000000 ); // Set Timer to about 0.5 seconds
USB30D_init(ENABLE);
}
}
else if( int_flg & RB_USB_IF_SUSPEND ) // Pending interrupt
{
R8_USB_INT_FG = RB_USB_IF_SUSPEND;
}
}
/*******************************************************************************
* @fn U20_Endp0_IN_Callback
*
* @brief U20_Endp0_IN_Callback Handler.
*
* @return None
*/
uint16_t U20_Endp0_IN_Callback(void)
{
uint16_t len = 0;
switch(SetupReq)
{
case USB_GET_DESCRIPTOR:
len = SetupLen >= U20_UEP0_MAXSIZE ? U20_UEP0_MAXSIZE : SetupLen;
memcpy(endp0RTbuff, pDescr, len);
SetupLen -= len;
pDescr += len;
break;
case USB_SET_ADDRESS:
g_DeviceUsbType = USB_U20_SPEED;
g_DeviceConnectstatus = USB_INT_CONNECT_ENUM;
USB20_Device_Setaddress(g_devInfo.dev_addr);
break;
default:
break;
}
return len;
}