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
|
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 ChrisDeadman
* SPDX-FileCopyrightText: Copyright (c) 2024 Ha Thach (tinyusb.org)
* SPDX-License-Identifier: MIT
*
* This file is part of the TinyUSB stack.
*/
#include "tusb_option.h"
#if CFG_TUH_ENABLED && !(defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421) && \
TU_CHECK_MCU(OPT_MCU_SAMD11, OPT_MCU_SAMD21, OPT_MCU_SAML2X, OPT_MCU_SAMD51, OPT_MCU_SAME5X)
#include "host/hcd.h"
#include "sam.h"
/*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/
#define USB_HOST_PTYPE_DIS 0x0
#define USB_HOST_PTYPE_CTRL 0x1
#define USB_HOST_PTYPE_ISO 0x2
#define USB_HOST_PTYPE_BULK 0x3
#define USB_HOST_PTYPE_INT 0x4
#define USB_HOST_PTYPE_EXT 0x5
#define USB_HOST_PCFG_PTOKEN_SETUP 0x0
#define USB_HOST_PCFG_PTOKEN_IN 0x1
#define USB_HOST_PCFG_PTOKEN_OUT 0x2
#define USB_PCKSIZE_ENUM(size) \
((size) >= 1024 ? 7 \
: (size) >= 1023 ? 7 \
: (size) > 256 ? 6 \
: (size) > 128 ? 5 \
: (size) > 64 ? 4 \
: (size) > 32 ? 3 \
: (size) > 16 ? 2 \
: (size) > 8 ? 1 \
: 0)
// Uncomment to use fake frame number.
// Low-Speed devices stall FNUM during enumeration :/
// #define HCD_SAMD_FAKE_FNUM
typedef struct {
uint8_t dev_addr;
uint8_t ep_addr;
uint16_t max_packet_size;
uint16_t xfer_length;
uint16_t xfer_remaining;
} usb_pipe_status_t;
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static volatile UsbHostDescriptor usb_pipe_table[USB_PIPE_NUM];
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static volatile usb_pipe_status_t usb_pipe_status_table[USB_PIPE_NUM];
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static volatile uint32_t fake_fnum;
static uint8_t samd_configure_pipe(uint8_t dev_addr, uint8_t ep_addr)
{
uint8_t pipe;
uint8_t token;
volatile usb_pipe_status_t* pipe_status;
bool same_addr = false;
bool same_ep_addr = false;
// evaluate pipe token
token = (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) ? USB_HOST_PCFG_PTOKEN_IN
: tu_edpt_number(ep_addr) == 0 ? USB_HOST_PCFG_PTOKEN_SETUP
: USB_HOST_PCFG_PTOKEN_OUT;
TU_LOG3("samd_configure_pipe(token=%02X, dev_addr=%02X, ep_addr=%02X)=", token, dev_addr, ep_addr);
// find already allocated pipe
for (pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
pipe_status = &usb_pipe_status_table[pipe];
same_addr = (pipe_status->dev_addr == dev_addr);
same_ep_addr = (tu_edpt_number(pipe_status->ep_addr) == tu_edpt_number(ep_addr));
if (same_ep_addr && (same_addr || (tu_edpt_number(ep_addr) == 0))) {
break;
}
}
// allocate from pool of free pipes
if (pipe >= USB_PIPE_NUM) {
for (pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
pipe_status = &usb_pipe_status_table[pipe];
// found a free pipe
if (pipe_status->dev_addr >= UINT8_MAX) {
break;
}
}
}
// no pipe available :(
if (pipe >= USB_PIPE_NUM) {
TU_LOG3("ERR_NO_PIPE\r\n");
return pipe;
}
TU_LOG3("%d\r\n", pipe);
// no transfer should be in progress
TU_ASSERT(((USB->HOST.HostPipe[pipe].PCFG.bit.PTYPE == USB_HOST_PTYPE_DIS) ||
USB->HOST.HostPipe[pipe].PSTATUS.bit.PFREEZE == 1),
USB_PIPE_NUM);
// update addr and ep_addr
pipe_status->dev_addr = dev_addr;
pipe_status->ep_addr = ep_addr;
usb_pipe_table[pipe].HostDescBank[0].CTRL_PIPE.bit.PDADDR = dev_addr;
usb_pipe_table[pipe].HostDescBank[0].CTRL_PIPE.bit.PEPNUM = tu_edpt_number(ep_addr);
// token specific configuration
USB->HOST.HostPipe[pipe].PCFG.bit.PTOKEN = token;
USB->HOST.HostPipe[pipe].PINTENCLR.reg = USB_HOST_PINTENCLR_MASK;
if (token == USB_HOST_PCFG_PTOKEN_SETUP) {
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_DTGL;
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_BK0RDY;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TXSTP;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_STALL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TRFAIL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_PERR;
} else if (token == USB_HOST_PCFG_PTOKEN_IN) {
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_BK0RDY;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TRCPT_Msk;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_STALL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TRFAIL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_PERR;
} else {
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_BK0RDY;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TRCPT_Msk;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_STALL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_TRFAIL;
USB->HOST.HostPipe[pipe].PINTENSET.reg = USB_HOST_PINTENSET_PERR;
}
return pipe;
}
static void samd_free_pipe(uint8_t pipe)
{
volatile usb_pipe_status_t* pipe_status = &usb_pipe_status_table[pipe];
pipe_status->dev_addr = UINT8_MAX;
pipe_status->ep_addr = UINT8_MAX;
pipe_status->max_packet_size = 0;
pipe_status->xfer_length = 0;
pipe_status->xfer_remaining = 0;
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
USB->HOST.HostPipe[pipe].PCFG.reg &= ~USB_HOST_PCFG_PTYPE_Msk;
USB->HOST.HostPipe[pipe].PINTENCLR.reg = USB_HOST_PINTENCLR_MASK;
memset((uint8_t*)(uintptr_t) &usb_pipe_table[pipe], 0, sizeof(usb_pipe_table[pipe]));
}
static void samd_free_all_pipes(void)
{
for (uint8_t pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
samd_free_pipe(pipe);
}
}
static bool samd_on_xfer(uint8_t pipe, xfer_result_t xfer_result)
{
uint16_t xfer_delta;
bool xfer_complete;
volatile usb_pipe_status_t* pipe_status = &usb_pipe_status_table[pipe];
// freeze the pipe
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
// get number of transferred bytes
if (xfer_result == XFER_RESULT_SUCCESS) {
xfer_delta = usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT;
} else {
xfer_delta = 0;
}
TU_LOG3("samd_on_xfer(%d, result=%d, xdelta=%d, rem=%d)\r\n", xfer_result, pipe, xfer_delta, pipe_status->xfer_remaining);
// update pipe status
if (xfer_delta > pipe_status->xfer_remaining) {
xfer_delta = pipe_status->xfer_remaining;
}
pipe_status->xfer_remaining -= xfer_delta;
pipe_status->xfer_length += xfer_delta;
// last packet handling
if (xfer_delta < pipe_status->max_packet_size) {
pipe_status->xfer_remaining = 0;
}
// transfer complete
xfer_complete = (xfer_result != XFER_RESULT_SUCCESS) || (pipe_status->xfer_remaining == 0);
if (xfer_complete) {
return true;
}
// continue receiving
if (tu_edpt_dir(pipe_status->ep_addr) == TUSB_DIR_IN) {
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_BK0RDY;
}
// continue sending
else {
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT =
(pipe_status->xfer_remaining < pipe_status->max_packet_size) ? pipe_status->xfer_remaining
: pipe_status->max_packet_size;
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_BK0RDY;
}
// advance packet buffer
usb_pipe_table[pipe].HostDescBank[0].ADDR.reg += xfer_delta;
// start next transfer
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_PFREEZE;
return false;
}
//--------------------------------------------------------------------+
// Controller API
//--------------------------------------------------------------------+
// Interrupt Handler
void hcd_int_handler(uint8_t rhport, bool in_isr)
{
(void) rhport;
uint16_t int_flags;
uint8_t pint_flags;
xfer_result_t xfer_result;
volatile usb_pipe_status_t* pipe_status;
//
// Check INTFLAG
//
int_flags = USB->HOST.INTFLAG.reg;
if (int_flags & USB_HOST_INTFLAG_HSOF) {
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_HSOF;
}
if (int_flags & USB_HOST_INTFLAG_RST) {
TU_LOG2("USB_HOST_INTFLAG_RST\r\n");
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_RST;
}
if (int_flags & USB_HOST_INTFLAG_WAKEUP) {
TU_LOG3("USB_HOST_INTFLAG_WAKEUP\r\n");
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_WAKEUP;
}
if (int_flags & USB_HOST_INTFLAG_DNRSM) {
TU_LOG3("USB_HOST_INTFLAG_DNRSM\r\n");
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_DNRSM;
}
if (int_flags & USB_HOST_INTFLAG_UPRSM) {
TU_LOG3("USB_HOST_INTFLAG_UPRSM\r\n");
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_UPRSM;
}
if (int_flags & USB_HOST_INTFLAG_RAMACER) {
TU_LOG1("USB_HOST_INTFLAG_RAMACER\r\n");
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_RAMACER;
}
if (int_flags & USB_HOST_INTFLAG_DCONN) {
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_DCONN;
hcd_event_device_attach(rhport, in_isr);
}
if (int_flags & USB_HOST_INTFLAG_DDISC) {
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_DDISC;
hcd_event_device_remove(rhport, in_isr);
}
// handle pipe interrupts
for (uint8_t pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
// get pipe handle
pipe_status = &usb_pipe_status_table[pipe];
if (pipe_status->dev_addr >= UINT8_MAX) {
continue;
}
//
// Check PINTFLAG
//
pint_flags = USB->HOST.HostPipe[pipe].PINTFLAG.reg;
xfer_result = XFER_RESULT_INVALID;
if (pint_flags & USB_HOST_PINTFLAG_TRCPT0) {
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_TRCPT0;
xfer_result = XFER_RESULT_SUCCESS;
}
if (pint_flags & USB_HOST_PINTFLAG_TRCPT1) {
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_TRCPT1;
xfer_result = XFER_RESULT_SUCCESS;
}
if (pint_flags & USB_HOST_PINTFLAG_TXSTP) {
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_TXSTP;
xfer_result = XFER_RESULT_SUCCESS;
}
if (pint_flags & USB_HOST_PINTFLAG_STALL) {
TU_LOG2("USB_HOST_PINTFLAG_STALL\r\n");
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_STALL;
xfer_result = XFER_RESULT_STALLED;
}
if (pint_flags & USB_HOST_PINTFLAG_TRFAIL) {
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_TRFAIL;
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_BK.reg & USB_HOST_STATUS_BK_ERRORFLOW) {
TU_LOG1("USB_HOST_STATUS_BK_ERRORFLOW\r\n");
xfer_result = XFER_RESULT_FAILED;
} else if (usb_pipe_table[pipe].HostDescBank[0].STATUS_BK.reg & USB_HOST_STATUS_BK_CRCERR) {
TU_LOG1("USB_HOST_STATUS_BK_CRCERR\r\n");
xfer_result = XFER_RESULT_FAILED;
} else {
// SAMD Quirk #1:
// Likes to report TRFAIL for no apparent reason -> ignore
}
}
if (pint_flags & USB_HOST_PINTFLAG_PERR) {
USB->HOST.HostPipe[pipe].PINTFLAG.reg = USB_HOST_PINTFLAG_PERR;
// Handled by STATUS_PIPE checks below
}
//
// Check STATUS_PIPE
//
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg & USB_HOST_STATUS_PIPE_DTGLER) {
TU_LOG1("USB_HOST_STATUS_PIPE_DTGLER\r\n");
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg &= ~USB_HOST_STATUS_PIPE_DTGLER;
xfer_result = XFER_RESULT_FAILED;
}
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg & USB_HOST_STATUS_PIPE_DAPIDER) {
TU_LOG1("USB_HOST_STATUS_PIPE_DAPIDER\r\n");
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg &= ~USB_HOST_STATUS_PIPE_DAPIDER;
xfer_result = XFER_RESULT_FAILED;
}
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg & USB_HOST_STATUS_PIPE_PIDER) {
TU_LOG1("USB_HOST_STATUS_PIPE_PIDER\r\n");
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg &= ~USB_HOST_STATUS_PIPE_PIDER;
xfer_result = XFER_RESULT_FAILED;
}
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg & USB_HOST_STATUS_PIPE_CRC16ER) {
TU_LOG1("USB_HOST_STATUS_PIPE_CRC16ER\r\n");
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg &= ~USB_HOST_STATUS_PIPE_CRC16ER;
xfer_result = XFER_RESULT_FAILED;
}
if (usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg & USB_HOST_STATUS_PIPE_TOUTER) {
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.reg &= ~USB_HOST_STATUS_PIPE_TOUTER;
if ((USB->HOST.HostPipe[pipe].PCFG.bit.PTYPE == USB_HOST_PTYPE_INT) &&
(tu_edpt_dir(pipe_status->ep_addr) == TUSB_DIR_IN)) {
// ignore timeouts from INT pipes
} else {
if (xfer_result == XFER_RESULT_INVALID) {
xfer_result = XFER_RESULT_TIMEOUT;
}
}
}
// prevent PERR from too high error counts, that is handled by TinyUSB anyways
usb_pipe_table[pipe].HostDescBank[0].STATUS_PIPE.bit.ERCNT = 0;
// no updates
if (xfer_result == XFER_RESULT_INVALID) {
continue;
}
// continue / complete transfer
if (samd_on_xfer(pipe, xfer_result)) {
hcd_event_xfer_complete(pipe_status->dev_addr, pipe_status->ep_addr, pipe_status->xfer_length, xfer_result, true);
}
}
}
// Initialize controller to host mode
bool hcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
TU_ASSERT(rhport == 0);
(void) rh_init;
fake_fnum = 0;
// reset to get in a clean state.
USB->HOST.CTRLA.bit.SWRST = 1;
while (USB->HOST.SYNCBUSY.bit.SWRST == 0)
;
while (USB->HOST.SYNCBUSY.bit.SWRST == 1)
;
// load pad calibration
USB->HOST.PADCAL.bit.TRANSP = (*((uint32_t*) USB_FUSES_TRANSP_ADDR) & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos;
USB->HOST.PADCAL.bit.TRANSN = (*((uint32_t*) USB_FUSES_TRANSN_ADDR) & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos;
USB->HOST.PADCAL.bit.TRIM = (*((uint32_t*) USB_FUSES_TRIM_ADDR) & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos;
USB->HOST.QOSCTRL.bit.CQOS = 3; // High Quality
USB->HOST.QOSCTRL.bit.DQOS = 3; // High Quality
// configure host-mode
samd_free_all_pipes(); // initializes pipe handles and usb_pipe_table
USB->HOST.DESCADD.reg = (uint32_t) (&usb_pipe_table[0]);
USB->HOST.CTRLB.reg = USB_HOST_CTRLB_SPDCONF_NORMAL | USB_HOST_CTRLB_VBUSOK;
USB->HOST.CTRLA.reg = USB_CTRLA_MODE_HOST | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
while (USB->HOST.SYNCBUSY.bit.ENABLE == 1)
;
// enable basic USB interrupts
USB->HOST.INTFLAG.reg |= USB->HOST.INTFLAG.reg; // clear pending
USB->HOST.INTENCLR.reg = USB_HOST_INTENCLR_MASK;
USB->HOST.INTENSET.reg = USB_HOST_INTENSET_DCONN;
USB->HOST.INTENSET.reg = USB_HOST_INTENSET_DDISC;
USB->HOST.INTENSET.reg = USB_HOST_INTENSET_WAKEUP;
USB->HOST.INTENSET.reg = USB_HOST_INTENSET_RST;
return true;
}
#if TU_CHECK_MCU(OPT_MCU_SAMD51, OPT_MCU_SAME5X)
// Enable USB interrupt
void hcd_int_enable(uint8_t rhport)
{
(void) rhport;
NVIC_EnableIRQ(USB_0_IRQn);
NVIC_EnableIRQ(USB_1_IRQn);
NVIC_EnableIRQ(USB_2_IRQn);
NVIC_EnableIRQ(USB_3_IRQn);
}
// Disable USB interrupt
void hcd_int_disable(uint8_t rhport)
{
(void) rhport;
NVIC_DisableIRQ(USB_3_IRQn);
NVIC_DisableIRQ(USB_2_IRQn);
NVIC_DisableIRQ(USB_1_IRQn);
NVIC_DisableIRQ(USB_0_IRQn);
}
#elif TU_CHECK_MCU(OPT_MCU_SAMD11, OPT_MCU_SAMD21, OPT_MCU_SAML2X)
// Enable USB interrupt
void hcd_int_enable(uint8_t rhport)
{
(void) rhport;
NVIC_EnableIRQ(USB_IRQn);
}
// Disable USB interrupt
void hcd_int_disable(uint8_t rhport)
{
(void) rhport;
NVIC_DisableIRQ(USB_IRQn);
}
#else
#error "No implementation available for hcd_int_enable / hcd_int_disable"
#endif
// Get frame number (1ms)
uint32_t hcd_frame_number(uint8_t rhport)
{
(void) rhport;
// SAMD Quirk #2:
// FNUM is stalled before enumeration of Low-Speed devices.
// internal frame counter can be used as workaround (not very accurate)
#ifdef HCD_SAMD_FAKE_FNUM
uint8_t start, current, prev;
uint8_t loop_count = (USB->HOST.STATUS.bit.SPEED == TUSB_SPEED_HIGH) ? 8 : 1;
for (uint8_t i = 0; i < loop_count; i++) {
start = USB->HOST.FLENHIGH.reg;
current = start;
// wait until wrap-around
prev = current;
while (current <= start) {
current = USB->HOST.FLENHIGH.reg;
if (current > prev)
break;
prev = current;
}
// wait until start is reached again
prev = current;
while (current > start) {
current = USB->HOST.FLENHIGH.reg;
if (current > prev)
break;
prev = current;
}
}
fake_fnum += 1;
return fake_fnum;
#else
return USB->HOST.FNUM.bit.FNUM;
#endif // HCD_SAMD_FAKE_FNUM
}
//--------------------------------------------------------------------+
// Port API
//--------------------------------------------------------------------+
// Get the current connect status of roothub port
bool hcd_port_connect_status(uint8_t rhport)
{
TU_ASSERT(rhport == 0);
return USB->HOST.STATUS.bit.LINESTATE != 0;
}
// Reset USB bus on the port. Return immediately, bus reset sequence may not be
// complete. Some port would require hcd_port_reset_end() to be invoked after 10ms to
// complete the reset sequence.
void hcd_port_reset(uint8_t rhport)
{
hcd_int_disable(rhport);
samd_free_all_pipes();
USB->HOST.INTFLAG.reg |= USB->HOST.INTFLAG.reg; // clear pending
USB->HOST.CTRLB.bit.BUSRESET = 1;
fake_fnum = 0;
}
// Complete bus reset sequence, may be required by some controllers
void hcd_port_reset_end(uint8_t rhport)
{
while (USB->HOST.INTFLAG.bit.RST == 0)
;
USB->HOST.INTFLAG.reg = USB_HOST_INTFLAG_RST;
USB->HOST.CTRLB.bit.SOFE = 1;
hcd_int_enable(rhport);
}
// Get port link speed
tusb_speed_t hcd_port_speed_get(uint8_t rhport)
{
(void) rhport;
switch (USB->HOST.STATUS.bit.SPEED) {
case 0:
return TUSB_SPEED_FULL;
case 1:
return TUSB_SPEED_LOW;
case 2:
return TUSB_SPEED_HIGH;
default:
return TUSB_SPEED_INVALID;
}
}
// HCD closes all opened endpoints belong to this device
void hcd_device_close(uint8_t rhport, uint8_t dev_addr)
{
(void) rhport;
for (uint8_t pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
volatile usb_pipe_status_t* pipe_status = &usb_pipe_status_table[pipe];
if (pipe_status->dev_addr == dev_addr) {
samd_free_pipe(pipe);
}
}
}
//--------------------------------------------------------------------+
// Endpoints API
//--------------------------------------------------------------------+
// Open an endpoint
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const* ep_desc)
{
TU_ASSERT(rhport == 0);
uint8_t pipe;
volatile usb_pipe_status_t* pipe_status;
const uint8_t ep_addr = ep_desc->bEndpointAddress;
const uint8_t bmAttributes = (ep_desc->bmAttributes.xfer) |
((ep_desc->bmAttributes.sync) << 2) |
((ep_desc->bmAttributes.usage) << 4);
// configure the pipe
pipe = samd_configure_pipe(dev_addr, ep_addr);
if (pipe >= USB_PIPE_NUM) {
return false;
}
// initial configuration
pipe_status = &usb_pipe_status_table[pipe];
USB->HOST.HostPipe[pipe].PCFG.reg &= ~USB_HOST_PCFG_PTYPE_Msk;
USB->HOST.HostPipe[pipe].PCFG.bit.PTYPE = bmAttributes + 1;
USB->HOST.HostPipe[pipe].BINTERVAL.reg = ep_desc->bInterval;
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_DTGL;
USB->HOST.HostPipe[pipe].PINTENCLR.reg = USB_HOST_PINTENCLR_MASK;
pipe_status->max_packet_size = ep_desc->wMaxPacketSize;
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.SIZE = USB_PCKSIZE_ENUM(pipe_status->max_packet_size);
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.AUTO_ZLP = 0;
return true;
}
// Submit a special transfer to send 8-byte Setup Packet, when complete
// hcd_event_xfer_complete() must be invoked
bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8])
{
TU_ASSERT(rhport == 0);
uint8_t pipe;
volatile usb_pipe_status_t* pipe_status;
// configure the pipe
pipe = samd_configure_pipe(dev_addr, 0);
if (pipe >= USB_PIPE_NUM) {
return false;
}
// prepare transfer
pipe_status = &usb_pipe_status_table[pipe];
usb_pipe_table[pipe].HostDescBank[0].ADDR.reg = (uint32_t) setup_packet;
pipe_status->xfer_remaining = 8;
pipe_status->xfer_length = 0;
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT = 8;
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_BK0RDY;
// clear pending interrupts
USB->HOST.HostPipe[pipe].PINTFLAG.reg |= USB->HOST.HostPipe[pipe].PINTFLAG.reg;
// begin transfer
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_PFREEZE;
return true;
}
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t* buffer, uint16_t buflen)
{
TU_ASSERT(rhport == 0);
uint8_t pipe;
volatile usb_pipe_status_t* pipe_status;
// configure the pipe
pipe = samd_configure_pipe(dev_addr, ep_addr);
if (pipe >= USB_PIPE_NUM) {
return false;
}
// prepare transfer
pipe_status = &usb_pipe_status_table[pipe];
usb_pipe_table[pipe].HostDescBank[0].ADDR.reg = (uint32_t) buffer;
pipe_status->xfer_remaining = buflen;
pipe_status->xfer_length = 0;
// receive data
if (tu_edpt_dir(pipe_status->ep_addr) == TUSB_DIR_IN) {
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = pipe_status->max_packet_size;
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_BK0RDY;
}
// send data
else {
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.BYTE_COUNT =
(pipe_status->xfer_remaining < pipe_status->max_packet_size) ? pipe_status->xfer_remaining
: pipe_status->max_packet_size;
usb_pipe_table[pipe].HostDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_BK0RDY;
}
// clear pending interrupts
USB->HOST.HostPipe[pipe].PINTFLAG.reg |= USB->HOST.HostPipe[pipe].PINTFLAG.reg;
// begin transfer
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_PFREEZE;
return true;
}
// Abort a queued transfer. Note: it can only abort transfer that has not been
// started Return true if a queued transfer is aborted, false if there is no transfer
// to abort
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr)
{
TU_ASSERT(rhport == 0);
uint8_t pipe;
volatile usb_pipe_status_t* pipe_status;
TU_LOG3("hcd_edpt_abort_xfer(dev_addr=%02X, ep_addr=%02X)=", dev_addr, ep_addr);
// find the pipe
for (pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
pipe_status = &usb_pipe_status_table[pipe];
if ((pipe_status->dev_addr == dev_addr) && (pipe_status->ep_addr == ep_addr)) {
break;
}
}
// pipe not found
if (pipe >= USB_PIPE_NUM) {
TU_LOG3("ERR_NO_PIPE\r\n");
return false;
}
TU_LOG3("%d\r\n", pipe);
// no transfer in progress
if (USB->HOST.HostPipe[pipe].PSTATUS.bit.PFREEZE == 1) {
return false;
}
// abort the transfer
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
pipe_status = &usb_pipe_status_table[pipe];
pipe_status->xfer_length = 0;
pipe_status->xfer_remaining = 0;
return true;
}
// clear stall, data toggle is also reset to DATA0
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr)
{
TU_ASSERT(rhport == 0);
uint8_t pipe;
volatile usb_pipe_status_t* pipe_status;
TU_LOG3("hcd_edpt_clear_stall(dev_addr=%02X, ep_addr=%02X)=", dev_addr, ep_addr);
// find the pipe
for (pipe = 0; pipe < USB_PIPE_NUM; pipe++) {
pipe_status = &usb_pipe_status_table[pipe];
if ((pipe_status->dev_addr == dev_addr) && (pipe_status->ep_addr == ep_addr)) {
break;
}
}
// pipe not found
if (pipe >= USB_PIPE_NUM) {
TU_LOG3("ERR_NO_PIPE\r\n");
return false;
}
TU_LOG3("%d\r\n", pipe);
// clear pending interrupts
USB->HOST.HostPipe[pipe].PINTFLAG.reg |= USB->HOST.HostPipe[pipe].PINTFLAG.reg;
// clear stalled state
USB->HOST.HostPipe[pipe].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
USB->HOST.HostPipe[pipe].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_DTGL;
return true;
}
#endif
|