-
Notifications
You must be signed in to change notification settings - Fork 376
/
usb.c
1114 lines (1037 loc) · 33.7 KB
/
usb.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "usb_dev.h"
#define USB_DESC_LIST_DEFINE
#include "usb_desc.h"
#include "usb_serial.h"
#include "usb_seremu.h"
#include "usb_rawhid.h"
#include "usb_keyboard.h"
#include "usb_mouse.h"
#include "usb_joystick.h"
#include "usb_flightsim.h"
#include "usb_touch.h"
#include "usb_midi.h"
#include "usb_audio.h"
#include "usb_mtp.h"
#include "core_pins.h" // for delay()
#include "avr/pgmspace.h"
#include <string.h>
#include "debug/printf.h"
//#define LOG_SIZE 20
//uint32_t transfer_log_head=0;
//uint32_t transfer_log_count=0;
//uint32_t transfer_log[LOG_SIZE];
// device mode, page 3155
#if defined(NUM_ENDPOINTS)
typedef struct endpoint_struct endpoint_t;
struct endpoint_struct {
uint32_t config;
uint32_t current;
uint32_t next;
uint32_t status;
uint32_t pointer0;
uint32_t pointer1;
uint32_t pointer2;
uint32_t pointer3;
uint32_t pointer4;
uint32_t reserved;
uint32_t setup0;
uint32_t setup1;
transfer_t *first_transfer;
transfer_t *last_transfer;
void (*callback_function)(transfer_t *completed_transfer);
uint32_t unused1;
};
/*struct transfer_struct {
uint32_t next;
uint32_t status;
uint32_t pointer0;
uint32_t pointer1;
uint32_t pointer2;
uint32_t pointer3;
uint32_t pointer4;
uint32_t callback_param;
};*/
endpoint_t endpoint_queue_head[(NUM_ENDPOINTS+1)*2] __attribute__ ((used, aligned(4096), section(".endpoint_queue") ));
transfer_t endpoint0_transfer_data __attribute__ ((used, aligned(32)));
transfer_t endpoint0_transfer_ack __attribute__ ((used, aligned(32)));
typedef union {
struct {
union {
struct {
uint8_t bmRequestType;
uint8_t bRequest;
};
uint16_t wRequestAndType;
};
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
};
struct {
uint32_t word1;
uint32_t word2;
};
uint64_t bothwords;
} setup_t;
static setup_t endpoint0_setupdata;
static uint32_t endpoint0_notify_mask=0;
static uint32_t endpointN_notify_mask=0;
//static int reset_count=0;
volatile uint8_t usb_configuration = 0; // non-zero when USB host as configured device
volatile uint8_t usb_high_speed = 0; // non-zero if running at 480 Mbit/sec speed
static uint8_t endpoint0_buffer[8];
static uint8_t sof_usage = 0;
static uint8_t usb_reboot_timer = 0;
extern uint8_t usb_descriptor_buffer[]; // defined in usb_desc.c
extern const uint8_t usb_config_descriptor_480[];
extern const uint8_t usb_config_descriptor_12[];
void (*usb_timer0_callback)(void) = NULL;
void (*usb_timer1_callback)(void) = NULL;
void usb_isr(void);
static void endpoint0_setup(uint64_t setupdata);
static void endpoint0_transmit(const void *data, uint32_t len, int notify);
static void endpoint0_receive(void *data, uint32_t len, int notify);
static void endpoint0_complete(void);
static void run_callbacks(endpoint_t *ep);
FLASHMEM void usb_init(void)
{
// TODO: only enable when VBUS detected
// TODO: return to low power mode when VBUS removed
// TODO: protect PMU access with MPU
PMU_REG_3P0 = PMU_REG_3P0_OUTPUT_TRG(0x0F) | PMU_REG_3P0_BO_OFFSET(6)
| PMU_REG_3P0_ENABLE_LINREG;
usb_init_serialnumber();
// assume PLL3 is already running - already done by usb_pll_start() in main.c
CCM_CCGR6 |= CCM_CCGR6_USBOH3(CCM_CCGR_ON); // turn on clocks to USB peripheral
printf("BURSTSIZE=%08lX\n", USB1_BURSTSIZE);
//USB1_BURSTSIZE = USB_BURSTSIZE_TXPBURST(4) | USB_BURSTSIZE_RXPBURST(4);
USB1_BURSTSIZE = 0x0404;
printf("BURSTSIZE=%08lX\n", USB1_BURSTSIZE);
printf("USB1_TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
// Before programming this register, the PHY clocks must be enabled in registers
// USBPHYx_CTRLn and CCM_ANALOG_USBPHYx_PLL_480_CTRLn.
//printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
//printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
//printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
//printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
//printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
// turn on PLL3, wait for 480 MHz lock?
// turn on CCM clock gates? CCGR6[CG0]
#if 1
if ((USBPHY1_PWD & (USBPHY_PWD_RXPWDRX | USBPHY_PWD_RXPWDDIFF | USBPHY_PWD_RXPWD1PT1
| USBPHY_PWD_RXPWDENV | USBPHY_PWD_TXPWDV2I | USBPHY_PWD_TXPWDIBIAS
| USBPHY_PWD_TXPWDFS)) || (USB1_USBMODE & USB_USBMODE_CM_MASK)) {
// USB controller is turned on from previous use
// reset needed to turn it off & start from clean slate
USBPHY1_CTRL_SET = USBPHY_CTRL_SFTRST; // USBPHY1_CTRL page 3292
USB1_USBCMD |= USB_USBCMD_RST; // reset controller
int count=0;
while (USB1_USBCMD & USB_USBCMD_RST) count++;
NVIC_CLEAR_PENDING(IRQ_USB1);
USBPHY1_CTRL_CLR = USBPHY_CTRL_SFTRST; // reset PHY
//USB1_USBSTS = USB1_USBSTS; // TODO: is this needed?
printf("USB reset took %d loops\n", count);
//delay(10);
//printf("\n");
//printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
//printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
//printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
//printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
//printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
delay(25);
}
#endif
// Device Controller Initialization, page 2351 (Rev 2, 12/2019)
// USBCMD pg 3216
// USBSTS pg 3220
// USBINTR pg 3224
// DEVICEADDR pg 3227
// ENDPTLISTADDR 3229
// USBMODE pg 3244
// ENDPTSETUPSTAT 3245
// ENDPTPRIME pg 3246
// ENDPTFLUSH pg 3247
// ENDPTSTAT pg 3247
// ENDPTCOMPLETE 3248
// ENDPTCTRL0 pg 3249
USBPHY1_CTRL_CLR = USBPHY_CTRL_CLKGATE;
USBPHY1_PWD = 0;
//printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
//printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
USB1_USBMODE = USB_USBMODE_CM(2) | USB_USBMODE_SLOM;
memset(endpoint_queue_head, 0, sizeof(endpoint_queue_head));
endpoint_queue_head[0].config = (64 << 16) | (1 << 15);
endpoint_queue_head[1].config = (64 << 16);
USB1_ENDPOINTLISTADDR = (uint32_t)&endpoint_queue_head;
// Recommended: enable all device interrupts including: USBINT, USBERRINT,
// Port Change Detect, USB Reset Received, DCSuspend.
USB1_USBINTR = USB_USBINTR_UE | USB_USBINTR_UEE | /* USB_USBINTR_PCE | */
USB_USBINTR_URE | USB_USBINTR_SLE;
//_VectorsRam[IRQ_USB1+16] = &usb_isr;
attachInterruptVector(IRQ_USB1, &usb_isr);
NVIC_ENABLE_IRQ(IRQ_USB1);
//printf("USB1_ENDPTCTRL0=%08lX\n", USB1_ENDPTCTRL0);
//printf("USB1_ENDPTCTRL1=%08lX\n", USB1_ENDPTCTRL1);
//printf("USB1_ENDPTCTRL2=%08lX\n", USB1_ENDPTCTRL2);
//printf("USB1_ENDPTCTRL3=%08lX\n", USB1_ENDPTCTRL3);
USB1_USBCMD = USB_USBCMD_RS;
//transfer_log_head = 0;
//transfer_log_count = 0;
//USB1_PORTSC1 |= USB_PORTSC1_PFSC; // force 12 Mbit/sec
}
FLASHMEM __attribute__((noinline)) void _reboot_Teensyduino_(void)
{
if (!(HW_OCOTP_CFG5 & 0x02)) {
asm("bkpt #251"); // run bootloader
} else {
__disable_irq(); // secure mode NXP ROM reboot
USB1_USBCMD = 0;
IOMUXC_GPR_GPR16 = 0x00200003;
// TODO: wipe all RAM for security
__asm__ volatile("mov sp, %0" : : "r" (0x20201000) : );
__asm__ volatile("dsb":::"memory");
volatile uint32_t * const p = (uint32_t *)0x20208000;
*p = 0xEB120000;
((void (*)(volatile void *))(*(uint32_t *)(*(uint32_t *)0x0020001C + 8)))(p);
}
__builtin_unreachable();
}
void usb_isr(void)
{
//printf("*");
// Port control in device mode is only used for
// status port reset, suspend, and current connect status.
uint32_t status = USB1_USBSTS;
USB1_USBSTS = status;
// USB_USBSTS_SLI - set to 1 when enters a suspend state from an active state
// USB_USBSTS_SRI - set at start of frame
// USB_USBSTS_SRI - set when USB reset detected
if (status & USB_USBSTS_UI) {
//printf("data\n");
uint32_t setupstatus = USB1_ENDPTSETUPSTAT;
//printf("USB1_ENDPTSETUPSTAT=%X\n", setupstatus);
while (setupstatus) {
USB1_ENDPTSETUPSTAT = setupstatus;
setup_t s;
do {
USB1_USBCMD |= USB_USBCMD_SUTW;
s.word1 = endpoint_queue_head[0].setup0;
s.word2 = endpoint_queue_head[0].setup1;
} while (!(USB1_USBCMD & USB_USBCMD_SUTW));
USB1_USBCMD &= ~USB_USBCMD_SUTW;
//printf("setup %08lX %08lX\n", s.word1, s.word2);
USB1_ENDPTFLUSH = (1<<16) | (1<<0); // page 3174
while (USB1_ENDPTFLUSH & ((1<<16) | (1<<0))) ;
endpoint0_notify_mask = 0;
endpoint0_setup(s.bothwords);
setupstatus = USB1_ENDPTSETUPSTAT; // page 3175
}
uint32_t completestatus = USB1_ENDPTCOMPLETE;
if (completestatus) {
USB1_ENDPTCOMPLETE = completestatus;
//printf("USB1_ENDPTCOMPLETE=%lX\n", completestatus);
if (completestatus & endpoint0_notify_mask) {
endpoint0_notify_mask = 0;
endpoint0_complete();
}
completestatus &= endpointN_notify_mask;
#if 1
if (completestatus) {
// transmit:
uint32_t tx = completestatus >> 16;
while (tx) {
int p=__builtin_ctz(tx);
run_callbacks(endpoint_queue_head + p * 2 + 1);
tx &= ~(1<<p);
}
// receive:
uint32_t rx = completestatus & 0xffff;
while(rx) {
int p=__builtin_ctz(rx);
run_callbacks(endpoint_queue_head + p * 2);
rx &= ~(1<<p);
};
}
#else
if (completestatus) {
int i; // TODO: optimize with __builtin_ctz()
for (i=2; i <= NUM_ENDPOINTS; i++) {
if (completestatus & (1 << i)) { // receive
run_callbacks(endpoint_queue_head + i * 2);
}
if (completestatus & (1 << (i + 16))) { // transmit
run_callbacks(endpoint_queue_head + i * 2 + 1);
}
}
}
#endif
}
}
if (status & USB_USBSTS_URI) { // page 3164
USB1_ENDPTSETUPSTAT = USB1_ENDPTSETUPSTAT; // Clear all setup token semaphores
USB1_ENDPTCOMPLETE = USB1_ENDPTCOMPLETE; // Clear all the endpoint complete status
while (USB1_ENDPTPRIME != 0) ; // Wait for any endpoint priming
USB1_ENDPTFLUSH = 0xFFFFFFFF; // Cancel all endpoint primed status
if ((USB1_PORTSC1 & USB_PORTSC1_PR)) {
//printf("reset\n");
} else {
// we took too long to respond :(
// TODO; is this ever really a problem?
//printf("reset too slow\n");
}
#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
usb_serial_reset();
#endif
endpointN_notify_mask = 0;
// TODO: Free all allocated dTDs
//if (++reset_count >= 3) {
// shut off USB - easier to see results in protocol analyzer
//USB1_USBCMD &= ~USB_USBCMD_RS;
//printf("shut off USB\n");
//}
}
if (status & USB_USBSTS_TI0) {
if (usb_timer0_callback != NULL) usb_timer0_callback();
}
if (status & USB_USBSTS_TI1) {
if (usb_timer1_callback != NULL) usb_timer1_callback();
}
if (status & USB_USBSTS_PCI) {
if (USB1_PORTSC1 & USB_PORTSC1_HSP) {
//printf("port at 480 Mbit\n");
usb_high_speed = 1;
} else {
//printf("port at 12 Mbit\n");
usb_high_speed = 0;
}
}
if (status & USB_USBSTS_SLI) { // page 3165
//printf("suspend\n");
}
if (status & USB_USBSTS_UEI) {
//printf("error\n");
}
if ((USB1_USBINTR & USB_USBINTR_SRE) && (status & USB_USBSTS_SRI)) {
//printf("sof %d\n", usb_reboot_timer);
if (usb_reboot_timer) {
if (--usb_reboot_timer == 0) {
usb_stop_sof_interrupts(NUM_INTERFACE);
_reboot_Teensyduino_();
}
}
#ifdef MIDI_INTERFACE
usb_midi_flush_output();
#endif
#ifdef MULTITOUCH_INTERFACE
usb_touchscreen_update_callback();
#endif
#ifdef FLIGHTSIM_INTERFACE
usb_flightsim_flush_output();
#endif
}
}
void usb_start_sof_interrupts(int interface)
{
__disable_irq();
sof_usage |= (1 << interface);
uint32_t intr = USB1_USBINTR;
if (!(intr & USB_USBINTR_SRE)) {
USB1_USBSTS = USB_USBSTS_SRI; // clear prior SOF before SOF IRQ enable
USB1_USBINTR = intr | USB_USBINTR_SRE;
}
__enable_irq();
}
void usb_stop_sof_interrupts(int interface)
{
sof_usage &= ~(1 << interface);
if (sof_usage == 0) {
USB1_USBINTR &= ~USB_USBINTR_SRE;
}
}
/*
struct transfer_struct { // table 55-60, pg 3159
uint32_t next;
uint32_t status;
uint32_t pointer0;
uint32_t pointer1;
uint32_t pointer2;
uint32_t pointer3;
uint32_t pointer4;
uint32_t unused1;
};
transfer_t endpoint0_transfer_data __attribute__ ((aligned(32)));;
transfer_t endpoint0_transfer_ack __attribute__ ((aligned(32)));;
*/
static uint8_t reply_buffer[8];
static void endpoint0_setup(uint64_t setupdata)
{
setup_t setup;
uint32_t endpoint, dir, ctrl;
const usb_descriptor_list_t *list;
setup.bothwords = setupdata;
switch (setup.wRequestAndType) {
case 0x0500: // SET_ADDRESS
endpoint0_receive(NULL, 0, 0);
USB1_DEVICEADDR = USB_DEVICEADDR_USBADR(setup.wValue) | USB_DEVICEADDR_USBADRA;
return;
case 0x0900: // SET_CONFIGURATION
usb_configuration = setup.wValue;
// configure all other endpoints
#if defined(ENDPOINT2_CONFIG)
USB1_ENDPTCTRL2 = ENDPOINT2_CONFIG;
#endif
#if defined(ENDPOINT3_CONFIG)
USB1_ENDPTCTRL3 = ENDPOINT3_CONFIG;
#endif
#if defined(ENDPOINT4_CONFIG)
USB1_ENDPTCTRL4 = ENDPOINT4_CONFIG;
#endif
#if defined(ENDPOINT5_CONFIG)
USB1_ENDPTCTRL5 = ENDPOINT5_CONFIG;
#endif
#if defined(ENDPOINT6_CONFIG)
USB1_ENDPTCTRL6 = ENDPOINT6_CONFIG;
#endif
#if defined(ENDPOINT7_CONFIG)
USB1_ENDPTCTRL7 = ENDPOINT7_CONFIG;
#endif
#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
usb_serial_configure();
#elif defined(SEREMU_INTERFACE)
usb_seremu_configure();
#endif
#if defined(CDC2_STATUS_INTERFACE) && defined(CDC2_DATA_INTERFACE)
usb_serial2_configure();
#endif
#if defined(CDC3_STATUS_INTERFACE) && defined(CDC3_DATA_INTERFACE)
usb_serial3_configure();
#endif
#if defined(RAWHID_INTERFACE)
usb_rawhid_configure();
#endif
#if defined(KEYBOARD_INTERFACE)
usb_keyboard_configure();
#endif
#if defined(MOUSE_INTERFACE)
usb_mouse_configure();
#endif
#if defined(FLIGHTSIM_INTERFACE)
usb_flightsim_configure();
#endif
#if defined(JOYSTICK_INTERFACE)
usb_joystick_configure();
#endif
#if defined(MULTITOUCH_INTERFACE)
usb_touchscreen_configure();
#endif
#if defined(MIDI_INTERFACE)
usb_midi_configure();
#endif
#if defined(AUDIO_INTERFACE)
usb_audio_configure();
#endif
#if defined(MTP_INTERFACE)
usb_mtp_configure();
#endif
#if defined(EXPERIMENTAL_INTERFACE)
memset(endpoint_queue_head + 2, 0, sizeof(endpoint_t) * 2);
endpoint_queue_head[2].pointer4 = 0xB8C6CF5D;
endpoint_queue_head[3].pointer4 = 0x74D59319;
#endif
endpoint0_receive(NULL, 0, 0);
return;
case 0x0880: // GET_CONFIGURATION
reply_buffer[0] = usb_configuration;
endpoint0_transmit(reply_buffer, 1, 0);
return;
case 0x0080: // GET_STATUS (device)
reply_buffer[0] = 0;
reply_buffer[1] = 0;
endpoint0_transmit(reply_buffer, 2, 0);
return;
case 0x0082: // GET_STATUS (endpoint)
endpoint = setup.wIndex & 0x7F;
if (endpoint > 7) break;
dir = setup.wIndex & 0x80;
ctrl = *((uint32_t *)&USB1_ENDPTCTRL0 + endpoint);
reply_buffer[0] = 0;
reply_buffer[1] = 0;
if ((dir && (ctrl & USB_ENDPTCTRL_TXS)) || (!dir && (ctrl & USB_ENDPTCTRL_RXS))) {
reply_buffer[0] = 1;
}
endpoint0_transmit(reply_buffer, 2, 0);
return;
case 0x0302: // SET_FEATURE (endpoint)
endpoint = setup.wIndex & 0x7F;
if (endpoint > 7) break;
dir = setup.wIndex & 0x80;
if (dir) {
*((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_TXS;
} else {
*((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_RXS;
}
endpoint0_receive(NULL, 0, 0);
return;
case 0x0102: // CLEAR_FEATURE (endpoint)
endpoint = setup.wIndex & 0x7F;
if (endpoint > 7) break;
dir = setup.wIndex & 0x80;
if (dir) {
*((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_TXS;
} else {
*((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_RXS;
}
endpoint0_receive(NULL, 0, 0);
return;
#ifdef EXPERIMENTAL_INTERFACE
case 0xF8C0: // GET_MS_DESCRIPTOR (bRequest=0xF8 because microsoft_os_string_desc)
if ((setup.wIndex & 0xFF00) != 0) break; // 1=Genre, 4=Compat ID, 5=Properties
setup.wIndex |= 0xEE00; // alter wIndex and treat as normal USB descriptor
__attribute__((fallthrough));
#endif
case 0x0680: // GET_DESCRIPTOR
case 0x0681:
for (list = usb_descriptor_list; list->addr != NULL; list++) {
if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
uint32_t datalen;
if ((setup.wValue >> 8) == 3) {
// for string descriptors, use the descriptor's
// length field, allowing runtime configured length.
datalen = *(list->addr);
} else {
datalen = list->length;
}
if (datalen > setup.wLength) datalen = setup.wLength;
// copy the descriptor, from PROGMEM to DMAMEM
if (setup.wValue == 0x200) {
// config descriptor needs to adapt to speed
const uint8_t *src = usb_config_descriptor_12;
if (usb_high_speed) src = usb_config_descriptor_480;
memcpy(usb_descriptor_buffer, src, datalen);
} else if (setup.wValue == 0x700) {
// other speed config also needs to adapt
const uint8_t *src = usb_config_descriptor_480;
if (usb_high_speed) src = usb_config_descriptor_12;
memcpy(usb_descriptor_buffer, src, datalen);
usb_descriptor_buffer[1] = 7;
} else {
memcpy(usb_descriptor_buffer, list->addr, datalen);
}
// prep transmit
arm_dcache_flush_delete(usb_descriptor_buffer, datalen);
endpoint0_transmit(usb_descriptor_buffer, datalen, 0);
return;
}
}
break;
#if defined(CDC_STATUS_INTERFACE)
case 0x2221: // CDC_SET_CONTROL_LINE_STATE
#ifdef CDC_STATUS_INTERFACE
if (setup.wIndex == CDC_STATUS_INTERFACE) {
usb_cdc_line_rtsdtr_millis = systick_millis_count;
usb_cdc_line_rtsdtr = setup.wValue;
}
#endif
#ifdef CDC2_STATUS_INTERFACE
if (setup.wIndex == CDC2_STATUS_INTERFACE) {
usb_cdc2_line_rtsdtr_millis = systick_millis_count;
usb_cdc2_line_rtsdtr = setup.wValue;
}
#endif
#ifdef CDC3_STATUS_INTERFACE
if (setup.wIndex == CDC3_STATUS_INTERFACE) {
usb_cdc3_line_rtsdtr_millis = systick_millis_count;
usb_cdc3_line_rtsdtr = setup.wValue;
}
#endif
__attribute__((fallthrough));
// fall through to next case, to always send ZLP ACK
case 0x2321: // CDC_SEND_BREAK
endpoint0_receive(NULL, 0, 0);
return;
case 0x2021: // CDC_SET_LINE_CODING
if (setup.wLength != 7) break;
endpoint0_setupdata.bothwords = setupdata;
endpoint0_receive(endpoint0_buffer, 7, 1);
return;
#endif
#if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
case 0x0921: // HID SET_REPORT
if (setup.wLength <= sizeof(endpoint0_buffer)) {
//printf("hid set report %x %x\n", setup.word1, setup.word2);
endpoint0_setupdata.bothwords = setup.bothwords;
endpoint0_buffer[0] = 0xE9;
endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
return;
}
break;
#endif
#if defined(AUDIO_INTERFACE)
case 0x0B01: // SET_INTERFACE (alternate setting)
if (setup.wIndex == AUDIO_INTERFACE+1) {
usb_audio_transmit_setting = setup.wValue;
if (usb_audio_transmit_setting > 0) {
// TODO: set up AUDIO_TX_ENDPOINT to transmit
}
endpoint0_receive(NULL, 0, 0);
return;
} else if (setup.wIndex == AUDIO_INTERFACE+2) {
usb_audio_receive_setting = setup.wValue;
endpoint0_receive(NULL, 0, 0);
return;
}
break;
case 0x0A81: // GET_INTERFACE (alternate setting)
if (setup.wIndex == AUDIO_INTERFACE+1) {
endpoint0_buffer[0] = usb_audio_transmit_setting;
endpoint0_transmit(endpoint0_buffer, 1, 0);
return;
} else if (setup.wIndex == AUDIO_INTERFACE+2) {
endpoint0_buffer[0] = usb_audio_receive_setting;
endpoint0_transmit(endpoint0_buffer, 1, 0);
return;
}
break;
case 0x0121: // SET FEATURE
case 0x0221:
case 0x0321:
case 0x0421:
//printf("set_feature, word1=%x, len=%d\n", setup.word1, setup.wLength);
if (setup.wLength <= sizeof(endpoint0_buffer)) {
endpoint0_setupdata.bothwords = setupdata;
endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
return; // handle these after ACK
}
break;
case 0x81A1: // GET FEATURE
case 0x82A1:
case 0x83A1:
case 0x84A1:
if (setup.wLength <= sizeof(endpoint0_buffer)) {
uint32_t len;
if (usb_audio_get_feature(&setup, endpoint0_buffer, &len)) {
//printf("GET feature, len=%d\n", len);
endpoint0_transmit(endpoint0_buffer, len, 0);
return;
}
}
break;
case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
if (setup.wLength >= 3) {
endpoint0_buffer[0] = 44100 & 255;
endpoint0_buffer[1] = 44100 >> 8;
endpoint0_buffer[2] = 0;
endpoint0_transmit(endpoint0_buffer, 3, 0);
return;
}
break;
#endif
#if defined(MULTITOUCH_INTERFACE)
case 0x01A1:
if (setup.wValue == 0x0300 && setup.wIndex == MULTITOUCH_INTERFACE) {
endpoint0_buffer[0] = MULTITOUCH_FINGERS;
endpoint0_transmit(endpoint0_buffer, 1, 0);
return;
} else if (setup.wValue == 0x0100 && setup.wIndex == MULTITOUCH_INTERFACE) {
memset(endpoint0_buffer, 0, 8);
endpoint0_transmit(endpoint0_buffer, 8, 0);
return;
}
break;
#endif
#if defined(MTP_INTERFACE)
case 0x6421: // Cancel Request, Still Image Class 1.0, 5.2.1, page 8
if (setup.wLength == 6) {
endpoint0_setupdata.bothwords = setupdata;
endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
return;
}
break;
case 0x65A1: // Get Extended Event Data, Still Image Class 1.0, 5.2.2, page 9
break;
case 0x6621: // Device Reset, Still Image Class 1.0, 5.2.3 page 10
break;
case 0x67A1: // Get Device Status, Still Image Class 1.0, 5.2.4, page 10
if (setup.wLength >= 4) {
endpoint0_buffer[0] = 4;
endpoint0_buffer[1] = 0;
endpoint0_buffer[2] = usb_mtp_status;
endpoint0_buffer[3] = 0x20;
endpoint0_transmit(endpoint0_buffer, 4, 0);
//if (usb_mtp_status == 0x19) usb_mtp_status = 0x01; // testing only
return;
}
break;
#endif
}
printf("endpoint 0 stall\n");
USB1_ENDPTCTRL0 = 0x000010001; // stall
}
static void endpoint0_transmit(const void *data, uint32_t len, int notify)
{
//printf("tx %lu\n", len);
if (len > 0) {
// Executing A Transfer Descriptor, page 3182
endpoint0_transfer_data.next = 1;
endpoint0_transfer_data.status = (len << 16) | (1<<7);
uint32_t addr = (uint32_t)data;
endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
endpoint0_transfer_data.pointer1 = addr + 4096;
endpoint0_transfer_data.pointer2 = addr + 8192;
endpoint0_transfer_data.pointer3 = addr + 12288;
endpoint0_transfer_data.pointer4 = addr + 16384;
// Case 1: Link list is empty, page 3182
endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
endpoint_queue_head[1].status = 0;
USB1_ENDPTPRIME |= (1<<16);
while (USB1_ENDPTPRIME) ;
}
endpoint0_transfer_ack.next = 1;
endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
endpoint0_transfer_ack.pointer0 = 0;
endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
endpoint_queue_head[0].status = 0;
USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
USB1_ENDPTPRIME |= (1<<0);
endpoint0_notify_mask = (notify ? (1 << 0) : 0);
while (USB1_ENDPTPRIME) ;
}
static void endpoint0_receive(void *data, uint32_t len, int notify)
{
//printf("rx %lu\n", len);
if (len > 0) {
// Executing A Transfer Descriptor, page 3182
endpoint0_transfer_data.next = 1;
endpoint0_transfer_data.status = (len << 16) | (1<<7);
uint32_t addr = (uint32_t)data;
endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
endpoint0_transfer_data.pointer1 = addr + 4096;
endpoint0_transfer_data.pointer2 = addr + 8192;
endpoint0_transfer_data.pointer3 = addr + 12288;
endpoint0_transfer_data.pointer4 = addr + 16384;
// Case 1: Link list is empty, page 3182
endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
endpoint_queue_head[0].status = 0;
USB1_ENDPTPRIME |= (1<<0);
while (USB1_ENDPTPRIME) ;
}
endpoint0_transfer_ack.next = 1;
endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
endpoint0_transfer_ack.pointer0 = 0;
endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
endpoint_queue_head[1].status = 0;
USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
USB1_ENDPTPRIME |= (1<<16);
endpoint0_notify_mask = (notify ? (1 << 16) : 0);
while (USB1_ENDPTPRIME) ;
}
/*typedef union {
struct {
union {
struct {
uint8_t bmRequestType;
uint8_t bRequest;
};
uint16_t wRequestAndType;
};
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
};
struct {
uint32_t word1;
uint32_t word2;
};
uint64_t bothwords;
} setup_t; */
static void endpoint0_complete(void)
{
setup_t setup;
setup.bothwords = endpoint0_setupdata.bothwords;
//printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
#ifdef CDC_STATUS_INTERFACE
// 0x2021 is CDC_SET_LINE_CODING
if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC_STATUS_INTERFACE) {
memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
if (usb_cdc_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
#endif
#ifdef CDC2_STATUS_INTERFACE
if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC2_STATUS_INTERFACE) {
memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
if (usb_cdc2_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
#endif
#ifdef CDC3_STATUS_INTERFACE
if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC3_STATUS_INTERFACE) {
memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
if (usb_cdc3_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
#endif
#ifdef KEYBOARD_INTERFACE
if (setup.word1 == 0x02000921 && setup.word2 == ((1 << 16) | KEYBOARD_INTERFACE)) {
keyboard_leds = endpoint0_buffer[0];
endpoint0_transmit(NULL, 0, 0);
}
#endif
#ifdef SEREMU_INTERFACE
if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)) {
if (endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
&& endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
printf("seremu reboot request\n");
usb_start_sof_interrupts(NUM_INTERFACE);
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
} else {
// any other feature report means Arduino Serial Monitor is open
usb_seremu_online = 1;
}
}
#endif
#ifdef AUDIO_INTERFACE
if (setup.word1 == 0x02010121 || setup.word1 == 0x01000121 /* TODO: check setup.word2 */) {
usb_audio_set_feature(&endpoint0_setupdata, endpoint0_buffer);
}
#endif
#ifdef MTP_INTERFACE
if (setup.wRequestAndType == 0x6421) {
if (endpoint0_buffer[0] == 0x01 && endpoint0_buffer[1] == 0x40) {
printf("MTP cancel, transaction ID=%08X\n",
endpoint0_buffer[2] | (endpoint0_buffer[3] << 8) |
(endpoint0_buffer[4] << 16) | (endpoint0_buffer[5] << 24));
usb_mtp_status = 0x19; // 0x19 = host initiated cancel
}
}
#endif
}
static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
{
memset(qh, 0, sizeof(endpoint_t));
qh->config = config;
qh->next = 1; // Terminate bit = 1
qh->callback_function = callback;
}
void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
{
uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
if (ep < 2 || ep > NUM_ENDPOINTS) return;
usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
if (cb) endpointN_notify_mask |= (1 << ep);
}
void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
{
uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
if (ep < 2 || ep > NUM_ENDPOINTS) return;
usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
if (cb) endpointN_notify_mask |= (1 << (ep + 16));
}
void usb_config_rx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
{
if (mult < 1 || mult > 3) return;
uint32_t config = (packet_size << 16) | (mult << 30);
if (ep < 2 || ep > NUM_ENDPOINTS) return;
usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
if (cb) endpointN_notify_mask |= (1 << ep);
}
void usb_config_tx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
{
if (mult < 1 || mult > 3) return;
uint32_t config = (packet_size << 16) | (mult << 30);
if (ep < 2 || ep > NUM_ENDPOINTS) return;
usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
if (cb) endpointN_notify_mask |= (1 << (ep + 16));
}
void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
{
transfer->next = 1;
transfer->status = (len << 16) | (1<<7);
uint32_t addr = (uint32_t)data;
transfer->pointer0 = addr;
transfer->pointer1 = addr + 4096;
transfer->pointer2 = addr + 8192;
transfer->pointer3 = addr + 12288;
transfer->pointer4 = addr + 16384;
transfer->callback_param = param;
}
#if 0
void usb_print_transfer_log(void)
{
uint32_t i, count;
printf("log %d transfers\n", transfer_log_count);
count = transfer_log_count;
if (count > LOG_SIZE) count = LOG_SIZE;
for (i=0; i < count; i++) {
if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
transfer_log_head--;
uint32_t log = transfer_log[transfer_log_head];
printf(" %c %X\n", log >> 8, (int)(log & 255));
}
}
#endif
static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
{
// when we stop at 6, why is the last transfer missing from the USB output?
//if (transfer_log_count >= 6) return;
//uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
if (endpoint->callback_function) {
transfer->status |= (1<<15);
}
__disable_irq();
//digitalWriteFast(1, HIGH);
// Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
transfer_t *last = endpoint->last_transfer;
if (last) {
last->next = (uint32_t)transfer;
if (USB1_ENDPTPRIME & epmask) goto end;
//digitalWriteFast(2, HIGH);
//ret |= 0x01;
uint32_t status, cyccnt=ARM_DWT_CYCCNT;
do {
USB1_USBCMD |= USB_USBCMD_ATDTW;
status = USB1_ENDPTSTATUS;
} while (!(USB1_USBCMD & USB_USBCMD_ATDTW) && (ARM_DWT_CYCCNT - cyccnt < 2400));
//USB1_USBCMD &= ~USB_USBCMD_ATDTW;
if (status & epmask) goto end;
//ret |= 0x02;
endpoint->next = (uint32_t)transfer;
endpoint->status = 0;
USB1_ENDPTPRIME |= epmask;
goto end;
}
//digitalWriteFast(4, HIGH);
endpoint->next = (uint32_t)transfer;
endpoint->status = 0;
USB1_ENDPTPRIME |= epmask;
endpoint->first_transfer = transfer;
end:
endpoint->last_transfer = transfer;
__enable_irq();
//digitalWriteFast(4, LOW);
//digitalWriteFast(3, LOW);
//digitalWriteFast(2, LOW);
//digitalWriteFast(1, LOW);
//if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
//transfer_log[transfer_log_head++] = ret;
//transfer_log_count++;
}
// ENDPTPRIME - Software should write a one to the corresponding bit when
// posting a new transfer descriptor to an endpoint queue head.
// Hardware automatically uses this bit to begin parsing for a
// new transfer descriptor from the queue head and prepare a
// transmit buffer. Hardware clears this bit when the associated
// endpoint(s) is (are) successfully primed.
// Momentarily set by hardware during hardware re-priming