Open FFBoard
Open source force feedback firmware
dcd_pic.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020 Koji Kitayama
5 * Copyright (c) 2022 Reimu NotMoe <reimu@sudomaker.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#include "tusb_option.h"
29
30#if CFG_TUD_ENABLED && \
31 (CFG_TUSB_MCU == OPT_MCU_PIC32MX || CFG_TUSB_MCU == OPT_MCU_PIC32MM || \
32 CFG_TUSB_MCU == OPT_MCU_PIC32MK || CFG_TUSB_MCU == OPT_MCU_PIC24 || \
33 CFG_TUSB_MCU == OPT_MCU_DSPIC33)
34
35#include <xc.h>
36
37#include "device/dcd.h"
38
39
40#if (CFG_TUSB_MCU == OPT_MCU_PIC32MX || CFG_TUSB_MCU == OPT_MCU_PIC32MM || CFG_TUSB_MCU == OPT_MCU_PIC32MK)
41
42#define TU_PIC_INT_SIZE 4
43
44#elif (CFG_TUSB_MCU == OPT_MCU_PIC24 || CFG_TUSB_MCU == OPT_MCU_DSPIC33)
45
46#define TU_PIC_INT_SIZE 2
47
48#else
49
50#error Unsupportd PIC MCU
51
52#endif
53
54
55#if TU_PIC_INT_SIZE == 4
56
57#ifndef KVA_TO_PA
58#define KVA_TO_PA(kva) ((uint32_t)(kva) & 0x1fffffff)
59#endif
60
61#ifndef PA_TO_KVA1
62#define PA_TO_KVA1(pa) ((uint32_t)(pa) | 0xA0000000)
63#endif
64
65#else
66
67#ifndef KVA_TO_PA
68#define KVA_TO_PA(kva) (kva)
69#endif
70
71#ifndef PA_TO_KVA1
72#define PA_TO_KVA1(pa) (pa)
73#endif
74
75#endif
76
77
78//--------------------------------------------------------------------+
79// MACRO TYPEDEF CONSTANT ENUM DECLARATION
80//--------------------------------------------------------------------+
81
82enum {
84 TOK_PID_IN = 0x9u,
86};
87
88// The BDT is 8 bytes on 32bit PICs and 4 bytes on 8/16bit PICs
89#if TU_PIC_INT_SIZE == 4
90typedef struct TU_ATTR_PACKED
91{
92 union {
93 uint32_t head;
94 struct {
95 union {
96 struct {
97 uint16_t : 2;
99 uint16_t data : 1;
101 uint16_t : 8;
102 };
103 struct {
104 uint16_t : 2;
106 uint16_t dts : 1;
107 uint16_t ninc : 1;
108 uint16_t keep : 1;
109 uint16_t : 10;
110 };
111 };
113 uint16_t : 6;
114 };
115 };
116 uint8_t *addr;
118
119TU_VERIFY_STATIC( sizeof(buffer_descriptor_t) == 8, "size is not correct" );
120#else
121typedef struct TU_ATTR_PACKED
122{
123 union {
125
126 struct {
127 uint16_t : 10;
128 uint16_t tok_pid : 4;
129 uint16_t data : 1;
130 uint16_t own : 1;
131 };
132 struct {
133 uint16_t : 10;
135 uint16_t dts : 1;
136 uint16_t ninc : 1;
137 uint16_t keep : 1;
138 };
139
140 struct {
141 uint16_t bc : 10;
142 uint16_t : 6;
143 };
144 };
145 uint8_t *addr;
147
148TU_VERIFY_STATIC( sizeof(buffer_descriptor_t) == 4, "size is not correct" );
149#endif
150
151
152typedef struct TU_ATTR_PACKED
153{
154 union {
155 uint32_t state;
156 struct {
157 uint32_t max_packet_size :11;
158 uint32_t : 5;
159 uint32_t odd : 1;
160 uint32_t :15;
161 };
162 };
166
167TU_VERIFY_STATIC( sizeof(endpoint_state_t) == 8, "size is not correct" );
168
169typedef struct
170{
171 union {
172 /* [#EP][OUT,IN][EVEN,ODD] */
173 buffer_descriptor_t bdt[16][2][2];
174#if TU_PIC_INT_SIZE == 4
175 uint16_t bda[256];
176#else
177 uint8_t bda[256];
178#endif
179 };
181 endpoint_state_t endpoint[16][2];
182 endpoint_state_t endpoint_unified[16 * 2];
183 };
184 uint8_t setup_packet[8];
185 uint8_t addr;
186} dcd_data_t;
187
188//--------------------------------------------------------------------+
189// INTERNAL OBJECT & FUNCTION DECLARATION
190//--------------------------------------------------------------------+
191// BDT(Buffer Descriptor Table) must be 256-byte aligned
192CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(512) volatile static dcd_data_t _dcd;
193
194#if TU_PIC_INT_SIZE == 4
195TU_VERIFY_STATIC( sizeof(_dcd.bdt) == 512, "size is not correct" );
196#else
197TU_VERIFY_STATIC( sizeof(_dcd.bdt) == 256, "size is not correct" );
198#endif
199
200#if TU_PIC_INT_SIZE == 4
201typedef uint32_t ep_reg_t;
202#elif TU_PIC_INT_SIZE == 2
203typedef uint16_t ep_reg_t;
204#endif
205
206static inline volatile void *ep_addr(uint8_t rhport, uint8_t ep_num) {
207#if CFG_TUSB_MCU == OPT_MCU_PIC32MK
208 volatile void *ep_reg_base = rhport ? (&U2EP0) : (&U1EP0);
209#else
210 volatile void *ep_reg_base = &U1EP0;
211#endif
212#if TU_PIC_INT_SIZE == 4
213 const size_t offset = 0x10;
214#else
215 const size_t offset = 0x2;
216#endif
217 return ep_reg_base + offset * ep_num;
218}
219
220static inline ep_reg_t ep_read(uint8_t rhport, uint8_t ep_num) {
221 volatile ep_reg_t *ep = ep_addr(rhport, ep_num);
222 return *ep;
223}
224
225static inline void ep_write(uint8_t rhport, uint8_t ep_num, ep_reg_t val) {
226 volatile ep_reg_t *ep = ep_addr(rhport, ep_num);
227 *ep = val;
228}
229
230static inline void ep_clear(uint8_t rhport, uint8_t ep_num, ep_reg_t val) {
231#if TU_PIC_INT_SIZE == 4
232 volatile ep_reg_t *ep_clr = (ep_addr(rhport, ep_num) + 0x4);
233 *ep_clr = val;
234#else
235 ep_reg_t v = ep_read(rhport, ep_num);
236 v &= ~val;
237 ep_write(rhport, ep_num, v);
238#endif
239}
240
241static inline void ep_set(uint8_t rhport, uint8_t ep_num, ep_reg_t val) {
242#if TU_PIC_INT_SIZE == 4
243 volatile ep_reg_t *ep_s = (ep_addr(rhport, ep_num) + 0x8);
244 *ep_s = val;
245#else
246 ep_reg_t v = ep_read(rhport, ep_num);
247 v |= val;
248 ep_write(rhport, ep_num, v);
249#endif
250}
251
252static inline void intr_enable(uint8_t rhport) {
253#if CFG_TUSB_MCU == OPT_MCU_PIC32MM
254 IEC0SET = _IEC0_USBIE_MASK;
255#elif CFG_TUSB_MCU == OPT_MCU_PIC32MX
256 IEC1SET = _IEC1_USBIE_MASK;
257#elif CFG_TUSB_MCU == OPT_MCU_PIC32MK
258 if (rhport == 0)
259 IEC1SET = _IEC1_USB1IE_MASK;
260 else
261 IEC7SET = _IEC7_USB2IE_MASK;
262#elif (CFG_TUSB_MCU == OPT_MCU_PIC24) || (CFG_TUSB_MCU == OPT_MCU_DSPIC33)
263 IEC5bits.USB1IE = 1;
264#endif
265}
266
267static inline void intr_disable(uint8_t rhport) {
268#if CFG_TUSB_MCU == OPT_MCU_PIC32MM
269 IEC0CLR = _IEC0_USBIE_MASK;
270#elif CFG_TUSB_MCU == OPT_MCU_PIC32MX
271 IEC1CLR = _IEC1_USBIE_MASK;
272#elif CFG_TUSB_MCU == OPT_MCU_PIC32MK
273 if (rhport == 0)
274 IEC1CLR = _IEC1_USB1IE_MASK;
275 else
276 IEC7CLR = _IEC7_USB2IE_MASK;
277#elif (CFG_TUSB_MCU == OPT_MCU_PIC24) || (CFG_TUSB_MCU == OPT_MCU_DSPIC33)
278 IEC5bits.USB1IE = 0;
279#endif
280}
281
282static inline int intr_is_enabled(uint8_t rhport) {
283#if CFG_TUSB_MCU == OPT_MCU_PIC32MM
284 return IEC0bits.USBIE;
285#elif CFG_TUSB_MCU == OPT_MCU_PIC32MX
286 return IEC1bits.USBIE;
287#elif CFG_TUSB_MCU == OPT_MCU_PIC32MK
288 if (rhport == 0)
289 return IEC1bits.USB1IE;
290 else
291 return IEC7bits.USB2IE;
292#elif (CFG_TUSB_MCU == OPT_MCU_PIC24) || (CFG_TUSB_MCU == OPT_MCU_DSPIC33)
293 return IEC5bits.USB1IE;
294#endif
295}
296
297static inline void intr_clear(uint8_t rhport) {
298#if CFG_TUSB_MCU == OPT_MCU_PIC32MM
299 IFS0CLR = _IFS0_USBIF_MASK;
300#elif CFG_TUSB_MCU == OPT_MCU_PIC32MX
301 IFS1CLR = _IFS1_USBIF_MASK;
302#elif CFG_TUSB_MCU == OPT_MCU_PIC32MK
303 if (rhport == 0)
304 IFS1CLR = _IFS1_USB1IF_MASK;
305 else
306 IFS7CLR = _IFS7_USB2IF_MASK;
307#elif (CFG_TUSB_MCU == OPT_MCU_PIC24) || (CFG_TUSB_MCU == OPT_MCU_DSPIC33)
308 IFS5bits.USB1IF = 0;
309#endif
310}
311
312static void prepare_next_setup_packet(uint8_t rhport)
313{
314 const unsigned out_odd = _dcd.endpoint[0][0].odd;
315 const unsigned in_odd = _dcd.endpoint[0][1].odd;
316 TU_ASSERT(0 == _dcd.bdt[0][0][out_odd].own, );
317
318 _dcd.bdt[0][0][out_odd].data = 0;
319 _dcd.bdt[0][0][out_odd ^ 1].data = 1;
320 _dcd.bdt[0][1][in_odd].data = 1;
321 _dcd.bdt[0][1][in_odd ^ 1].data = 0;
323 _dcd.setup_packet, sizeof(_dcd.setup_packet));
324}
325
326static void process_stall(uint8_t rhport)
327{
328 for (int i = 0; i < 16; ++i) {
329 unsigned const endpt = ep_read(rhport, i);
330
331 if (endpt & _U1EP0_EPSTALL_MASK) {
332 // prepare next setup if endpoint0
333 if ( i == 0 ) prepare_next_setup_packet(rhport);
334
335 // clear stall bit
336 ep_clear(rhport, i, _U1EP0_EPSTALL_MASK);
337 }
338 }
339}
340
341static void process_tokdne(uint8_t rhport)
342{
343 ep_reg_t s = U1STAT;
344
345 U1IR = _U1IR_TRNIF_MASK;
346
347 uint8_t epnum = (s >> _U1STAT_ENDPT0_POSITION);
348 uint8_t dir = (s & _U1STAT_DIR_MASK) >> _U1STAT_DIR_POSITION;
349 unsigned odd = (s & _U1STAT_PPBI_MASK) ? 1 : 0;
350
352 endpoint_state_t *ep = &_dcd.endpoint_unified[s >> 3];
353
354 /* fetch pid before discarded by the next steps */
355 const unsigned pid = bd->tok_pid;
356
357 /* reset values for a next transfer */
358 bd->bdt_stall = 0;
359 bd->dts = 1;
360 bd->ninc = 0;
361 bd->keep = 0;
362 /* update the odd variable to prepare for the next transfer */
363 ep->odd = odd ^ 1;
364 if (pid == TOK_PID_SETUP) {
365 dcd_event_setup_received(rhport, (uint8_t *)PA_TO_KVA1(bd->addr), true);
366#if TU_PIC_INT_SIZE == 4
367 U1CONCLR = _U1CON_PKTDIS_TOKBUSY_MASK;
368#else
369 U1CONbits.PKTDIS = 0;
370#endif
371 return;
372 }
373
374 const unsigned bc = bd->bc;
375 const unsigned remaining = ep->remaining - bc;
376 if (remaining && bc == ep->max_packet_size) {
377 /* continue the transferring consecutive data */
378 ep->remaining = remaining;
379 const int next_remaining = remaining - ep->max_packet_size;
380 if (next_remaining > 0) {
381 /* prepare to the after next transfer */
382 bd->addr += ep->max_packet_size * 2;
383 bd->bc = next_remaining > ep->max_packet_size ? ep->max_packet_size: next_remaining;
384 bd->own = 1; /* the own bit must set after addr */
385 }
386 return;
387 }
388 const unsigned length = ep->length;
390 tu_edpt_addr(epnum, dir),
391 length - remaining, XFER_RESULT_SUCCESS, true);
392 if (0 == epnum && 0 == length) {
393 /* After completion a ZLP of control transfer,
394 * it prepares for the next steup transfer. */
395 if (_dcd.addr) {
396 /* When the transfer was the SetAddress,
397 * the device address should be updated here. */
398 U1ADDR = _dcd.addr;
399 _dcd.addr = 0;
400 }
402 }
403}
404
405static void process_bus_reset(uint8_t rhport)
406{
407#if TU_PIC_INT_SIZE == 4
408 U1PWRCCLR = _U1PWRC_USUSPEND_MASK;
409 U1CONSET = _U1CON_PPBRST_MASK;
410#else
411 U1PWRCbits.USUSPND = 0;
412 U1CONbits.PPBRST = 1;
413#endif
414 U1ADDR = 0;
415
416 U1IE = _U1IE_URSTIE_MASK | _U1IE_TRNIE_MASK | _U1IE_IDLEIE_MASK |
417 _U1IE_UERRIE_MASK | _U1IE_STALLIE_MASK;
418
419 U1EP0 = _U1EP0_EPHSHK_MASK | _U1EP0_EPRXEN_MASK | _U1EP0_EPTXEN_MASK;
420
421 for (unsigned i = 1; i < 16; ++i) {
422 ep_write(rhport, i, 0);
423 }
424
425 buffer_descriptor_t *bd = _dcd.bdt[0][0];
426 for (unsigned i = 0; i < sizeof(_dcd.bdt)/sizeof(*bd); ++i, ++bd) {
427 bd->head = 0;
428 }
429 const endpoint_state_t ep0 = {
430 .max_packet_size = CFG_TUD_ENDPOINT0_SIZE,
431 .odd = 0,
432 .length = 0,
433 .remaining = 0,
434 };
435 _dcd.endpoint[0][0] = ep0;
436 _dcd.endpoint[0][1] = ep0;
437 tu_memclr(_dcd.endpoint[1], sizeof(_dcd.endpoint) - sizeof(_dcd.endpoint[0]));
438 _dcd.addr = 0;
440#if TU_PIC_INT_SIZE == 4
441 U1CONCLR = _U1CON_PPBRST_MASK;
442#else
443 U1CONbits.PPBRST = 0;
444#endif
446}
447
448static void process_bus_sleep(uint8_t rhport)
449{
450 // Enable resume & disable suspend interrupt
451 dcd_event_bus_signal(rhport, DCD_EVENT_SUSPEND, true);
452}
453
454static void process_bus_resume(uint8_t rhport)
455{
456 // Enable suspend & disable resume interrupt
457#if TU_PIC_INT_SIZE == 4
458 U1PWRCCLR = _U1PWRC_USUSPEND_MASK;
459 U1IECLR = _U1IE_RESUMEIE_MASK;
460 U1IESET = _U1IE_IDLEIE_MASK;
461#else
462 U1PWRCbits.USUSPND = 0;
463 U1IEbits.RESUMEIE = 0;
464 U1IEbits.IDLEIE = 1;
465#endif
466
467 dcd_event_bus_signal(rhport, DCD_EVENT_RESUME, true);
468}
469
470/*------------------------------------------------------------------*/
471/* Device API
472 *------------------------------------------------------------------*/
473bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
474 (void) rh_init;
475 intr_disable(rhport);
476 intr_clear(rhport);
477
478#if CFG_TUSB_MCU == OPT_MCU_PIC32MM
479 TRISBbits.TRISB6 = 1;
480#endif
481
482 tu_memclr(&_dcd, sizeof(_dcd));
483
484#if TU_PIC_INT_SIZE == 4
485 U1PWRCSET = _U1PWRC_USBPWR_MASK;
486#else
487 U1PWRCbits.USBPWR = 1;
488#endif
489
490#if TU_PIC_INT_SIZE == 4
491 uint32_t bdt_phys = KVA_TO_PA((uintptr_t)_dcd.bdt);
492
493 U1BDTP1 = (uint8_t)(bdt_phys >> 8);
494 U1BDTP2 = (uint8_t)(bdt_phys >> 16);
495 U1BDTP3 = (uint8_t)(bdt_phys >> 24);
496#else
497 U1BDTP1 = (uint8_t)((uint16_t)(void *)_dcd.bdt >> 8);
498
499 U1CNFG1bits.PPB = 2;
500#endif
501
502 U1IE = _U1IE_URSTIE_MASK;
503
504 dcd_connect(rhport);
505 return true;
506}
507
508void dcd_int_enable(uint8_t rhport)
509{
510 intr_enable(rhport);
511}
512
513void dcd_int_disable(uint8_t rhport)
514{
515 intr_disable(rhport);
516}
517
518void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
519{
520 _dcd.addr = dev_addr & 0x7F;
521 /* Response with status first before changing device address */
522 dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
523}
524
525void dcd_remote_wakeup(uint8_t rhport)
526{
527#if TU_PIC_INT_SIZE == 4
528 U1CONSET = _U1CON_RESUME_MASK;
529#else
530 U1CONbits.RESUME = 1;
531#endif
532 unsigned cnt = 25000000 / 1000;
533 while (cnt--) asm volatile("nop");
534
535#if TU_PIC_INT_SIZE == 4
536 U1CONCLR = _U1CON_RESUME_MASK;
537#else
538 U1CONbits.RESUME = 0;
539#endif
540}
541
542void dcd_connect(uint8_t rhport)
543{
544 while (!U1CONbits.USBEN) {
545#if TU_PIC_INT_SIZE == 4
546 U1CONSET = _U1CON_USBEN_SOFEN_MASK;
547#else
548 U1CONbits.USBEN = 1;
549#endif
550 }
551}
552
553void dcd_disconnect(uint8_t rhport)
554{
555 U1CON = 0;
556}
557
558void dcd_sof_enable(uint8_t rhport, bool en)
559{
560 (void) rhport;
561 (void) en;
562}
563
564//--------------------------------------------------------------------+
565// Endpoint API
566//--------------------------------------------------------------------+
567bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * ep_desc)
568{
569 const unsigned ep_addr = ep_desc->bEndpointAddress;
570 const unsigned epn = tu_edpt_number(ep_addr);
571 const unsigned dir = tu_edpt_dir(ep_addr);
572 const unsigned xfer = ep_desc->bmAttributes.xfer;
573 endpoint_state_t *ep = &_dcd.endpoint[epn][dir];
574 const unsigned odd = ep->odd;
575 buffer_descriptor_t *bd = _dcd.bdt[epn][dir];
576
577 /* No support for control transfer */
578 TU_ASSERT(epn && (xfer != TUSB_XFER_CONTROL));
579
581
582
583 unsigned val = _U1EP0_EPCONDIS_MASK;
584 val |= (xfer != TUSB_XFER_ISOCHRONOUS) ? _U1EP0_EPHSHK_MASK : 0;
585 val |= dir ? _U1EP0_EPTXEN_MASK : _U1EP0_EPRXEN_MASK;
586
587 ep_reg_t tmp = ep_read(rhport, epn);
588 tmp |= val;
589 ep_write(rhport, epn, tmp);
590
592 bd[odd].dts = 1;
593 bd[odd].data = 0;
594 bd[odd ^ 1].dts = 1;
595 bd[odd ^ 1].data = 1;
596 }
597
598 return true;
599}
600
601void dcd_edpt_close_all(uint8_t rhport)
602{
603 const unsigned ie = intr_is_enabled(rhport);
604 intr_disable(rhport);
605
606 for (unsigned i = 1; i < 16; ++i) {
607 ep_write(rhport, i, 0);
608 }
609
610 if (ie) intr_enable(rhport);
611
612 buffer_descriptor_t *bd = _dcd.bdt[1][0];
613 for (unsigned i = 2; i < sizeof(_dcd.bdt)/sizeof(*bd); ++i, ++bd) {
614 bd->head = 0;
615 }
616 endpoint_state_t *ep = &_dcd.endpoint[1][0];
617 for (unsigned i = 2; i < sizeof(_dcd.endpoint)/sizeof(*ep); ++i, ++ep) {
618 /* Clear except the odd */
619 ep->max_packet_size = 0;
620 ep->length = 0;
621 ep->remaining = 0;
622 }
623}
624
625void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
626{
627 const unsigned epn = tu_edpt_number(ep_addr);
628 const unsigned dir = tu_edpt_dir(ep_addr);
629 endpoint_state_t *ep = &_dcd.endpoint[epn][dir];
630 buffer_descriptor_t *bd = _dcd.bdt[epn][dir];
631 const unsigned msk = dir ? _U1EP0_EPTXEN_MASK : _U1EP0_EPRXEN_MASK;
632 const unsigned ie = intr_is_enabled(rhport);
633
634 intr_disable(rhport);
635
636 ep_clear(rhport, epn, msk);
637
638 ep->max_packet_size = 0;
639 ep->length = 0;
640 ep->remaining = 0;
641 bd[0].head = 0;
642 bd[1].head = 0;
643
644 if (ie) intr_enable(rhport);
645}
646
647bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes)
648{
649
650 const unsigned epn = tu_edpt_number(ep_addr);
651 const unsigned dir = tu_edpt_dir(ep_addr);
652 endpoint_state_t *ep = &_dcd.endpoint[epn][dir];
653 buffer_descriptor_t *bd = &_dcd.bdt[epn][dir][ep->odd];
654 TU_ASSERT(0 == bd->own);
655
656 const unsigned ie = intr_is_enabled(rhport);
657
658 intr_disable(rhport);
659
660 ep->length = total_bytes;
662
663 const unsigned mps = ep->max_packet_size;
664 if (total_bytes > mps) {
665 buffer_descriptor_t *next = ep->odd ? bd - 1: bd + 1;
666 /* When total_bytes is greater than the max packet size,
667 * it prepares to the next transfer to avoid NAK in advance. */
668 next->bc = total_bytes >= 2 * mps ? mps: total_bytes - mps;
669 next->addr = (uint8_t *)KVA_TO_PA(buffer + mps);
670 next->own = 1;
671 }
672 bd->bc = total_bytes >= mps ? mps: total_bytes;
673 bd->addr = (uint8_t *)KVA_TO_PA(buffer);
674 bd->own = 1; /* This bit must be set last */
675
676 if (ie) intr_enable(rhport);
677
678 return true;
679}
680
681void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
682{
683 (void) rhport;
684 const unsigned epn = tu_edpt_number(ep_addr);
685
686 if (0 == epn) {
687 ep_set(rhport, epn, _U1EP0_EPSTALL_MASK);
688 } else {
689 const unsigned dir = tu_edpt_dir(ep_addr);
690 const unsigned odd = _dcd.endpoint[epn][dir].odd;
691 buffer_descriptor_t *bd = &_dcd.bdt[epn][dir][odd];
692 TU_ASSERT(0 == bd->own,);
693
694 const unsigned ie = intr_is_enabled(rhport);
695
696 intr_disable(rhport);
697
698 bd->bdt_stall = 1;
699 bd->own = 1; /* This bit must be set last */
700
701 if (ie) intr_enable(rhport);
702 }
703}
704
705void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
706{
707 const unsigned epn = tu_edpt_number(ep_addr);
708 TU_VERIFY(epn,);
709 const unsigned dir = tu_edpt_dir(ep_addr);
710 const unsigned odd = _dcd.endpoint[epn][dir].odd;
711 buffer_descriptor_t *bd = _dcd.bdt[epn][dir];
712 TU_VERIFY(bd[odd].own,);
713
714 const unsigned ie = intr_is_enabled(rhport);
715
716 intr_disable(rhport);
717
718 bd[odd].own = 0;
719
720 // clear stall
721 bd[odd].bdt_stall = 0;
722
723 // Reset data toggle
724 bd[odd ].data = 0;
725 bd[odd ^ 1].data = 1;
726
727 // We already cleared this in ISR, but just clear it here to be safe
728 const unsigned endpt = ep_read(rhport, epn);
729 if (endpt & _U1EP0_EPSTALL_MASK) {
730 ep_clear(rhport, endpt, _U1EP0_EPSTALL_MASK);
731 }
732
733 if (ie) intr_enable(rhport);
734}
735
736//--------------------------------------------------------------------+
737// ISR
738//--------------------------------------------------------------------+
739void dcd_int_handler(uint8_t rhport)
740{
741 uint32_t is = U1IR;
742 uint32_t msk = U1IE;
743
744 U1IR = is & ~msk;
745 is &= msk;
746
747 if (is & _U1IR_UERRIF_MASK) {
748 uint32_t es = U1EIR;
749 U1EIR = es;
750 U1IR = is; /* discard any pending events */
751 }
752
753 if (is & _U1IR_URSTIF_MASK) {
754 U1IR = is; /* discard any pending events */
755 process_bus_reset(rhport);
756 }
757
758 if (is & _U1IR_IDLEIF_MASK) {
759 // Note Host usually has extra delay after bus reset (without SOF), which could falsely
760 // detected as Sleep event. Though usbd has debouncing logic so we are good
761 U1IR = _U1IR_IDLEIF_MASK;
762 process_bus_sleep(rhport);
763 }
764
765 if (is & _U1IR_RESUMEIF_MASK) {
766 U1IR = _U1IR_RESUMEIF_MASK;
767 process_bus_resume(rhport);
768 }
769
770 if (is & _U1IR_SOFIF_MASK) {
771 U1IR = _U1IR_SOFIF_MASK;
772 dcd_event_bus_signal(rhport, DCD_EVENT_SOF, true);
773 }
774
775 if (is & _U1IR_STALLIF_MASK) {
776 U1IR = _U1IR_STALLIF_MASK;
777 process_stall(rhport);
778 }
779
780 if (is & _U1IR_TRNIF_MASK) {
781 process_tokdne(rhport);
782 }
783
784 intr_clear(rhport);
785}
786
787#endif
static TU_ATTR_ALWAYS_INLINE void dcd_event_bus_signal(uint8_t rhport, dcd_eventid_t eid, bool in_isr)
Definition: dcd.h:196
static TU_ATTR_ALWAYS_INLINE void dcd_event_xfer_complete(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
Definition: dcd.h:222
static TU_ATTR_ALWAYS_INLINE void dcd_event_setup_received(uint8_t rhport, uint8_t const *setup, bool in_isr)
Definition: dcd.h:213
static TU_ATTR_ALWAYS_INLINE void dcd_event_bus_reset(uint8_t rhport, tusb_speed_t speed, bool in_isr)
Definition: dcd.h:204
static struct @149 _dcd
xfer_td_t xfer[EP_CBI_COUNT+1][2]
Definition: dcd_nrf5x.c:119
uint16_t total_bytes
Definition: dcd_nuc505.c:113
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
static ep_reg_t ep_read(uint8_t rhport, uint8_t ep_num)
Definition: dcd_pic.c:220
static void intr_enable(uint8_t rhport)
Definition: dcd_pic.c:252
static void ep_clear(uint8_t rhport, uint8_t ep_num, ep_reg_t val)
Definition: dcd_pic.c:230
static void intr_clear(uint8_t rhport)
Definition: dcd_pic.c:297
static void process_bus_reset(uint8_t rhport)
Definition: dcd_pic.c:405
struct TU_ATTR_PACKED endpoint_state_t
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_pic.c:681
void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_pic.c:625
void dcd_int_handler(uint8_t rhport)
Definition: dcd_pic.c:739
static void ep_write(uint8_t rhport, uint8_t ep_num, ep_reg_t val)
Definition: dcd_pic.c:225
void dcd_disconnect(uint8_t rhport)
Definition: dcd_pic.c:553
static void process_tokdne(uint8_t rhport)
Definition: dcd_pic.c:341
void dcd_edpt_close_all(uint8_t rhport)
Definition: dcd_pic.c:601
void dcd_int_disable(uint8_t rhport)
Definition: dcd_pic.c:513
static int intr_is_enabled(uint8_t rhport)
Definition: dcd_pic.c:282
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *ep_desc)
Definition: dcd_pic.c:567
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_pic.c:705
TU_VERIFY_STATIC(sizeof(buffer_descriptor_t)==8, "size is not correct")
static void process_stall(uint8_t rhport)
Definition: dcd_pic.c:326
void dcd_connect(uint8_t rhport)
Definition: dcd_pic.c:542
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
Definition: dcd_pic.c:647
static void intr_disable(uint8_t rhport)
Definition: dcd_pic.c:267
static void prepare_next_setup_packet(uint8_t rhport)
Definition: dcd_pic.c:312
void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
Definition: dcd_pic.c:518
struct TU_ATTR_PACKED buffer_descriptor_t
bool dcd_init(uint8_t rhport, const tusb_rhport_init_t *rh_init)
Definition: dcd_pic.c:473
static void ep_set(uint8_t rhport, uint8_t ep_num, ep_reg_t val)
Definition: dcd_pic.c:241
@ TOK_PID_SETUP
Definition: dcd_pic.c:85
@ TOK_PID_IN
Definition: dcd_pic.c:84
@ TOK_PID_OUT
Definition: dcd_pic.c:83
CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(512) volatile
Definition: dcd_pic.c:192
static void process_bus_sleep(uint8_t rhport)
Definition: dcd_pic.c:448
void dcd_int_enable(uint8_t rhport)
Definition: dcd_pic.c:508
void dcd_remote_wakeup(uint8_t rhport)
Definition: dcd_pic.c:525
static void process_bus_resume(uint8_t rhport)
Definition: dcd_pic.c:454
void dcd_sof_enable(uint8_t rhport, bool en)
Definition: dcd_pic.c:558
uint8_t const * buffer
Definition: midi_device.h:100
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
__IO uint16_t tok_pid
Definition: dcd_ci_fs.c:60
uint8_t * addr
Definition: dcd_ci_fs.c:78
uint16_t head
Definition: dcd_pic.c:124
uint16_t ninc
Definition: dcd_ci_fs.c:69
uint16_t own
Definition: dcd_pic.c:100
uint16_t bdt_stall
Definition: dcd_ci_fs.c:67
uint16_t dts
Definition: dcd_ci_fs.c:68
__IO uint16_t bc
Definition: dcd_ci_fs.c:74
uint8_t data[CFG_TUD_NCM_IN_NTB_MAX_SIZE]
Definition: ncm.h:147
uint32_t max_packet_size
Definition: dcd_ci_fs.c:88
uint8_t bmAttributes
See: audio_clock_source_attribute_t.
Definition: audio.h:672
uint16_t length
Definition: dcd_ci_fs.c:94
uint16_t bc
Definition: dcd_pic.c:112
volatile uint16_t
Definition: hcd_rusb2.c:58
uint32_t odd
Definition: dcd_ci_fs.c:90
uint16_t keep
Definition: dcd_ci_fs.c:70
uint16_t remaining
Definition: dcd_ci_fs.c:95
__IO uint16_t own
Definition: dcd_ci_fs.c:62
uint8_t bEndpointAddress
Definition: video.h:306
uint32_t head
Definition: dcd_ci_fs.c:55
uint16_t tok_pid
Definition: dcd_pic.c:98
TU_ATTR_ALIGNED(4)
Definition: dcd_pic.c:180
@ TUSB_DIR_IN
Definition: tusb_types.h:67
@ TUSB_DIR_OUT
Definition: tusb_types.h:66
@ TUSB_SPEED_FULL
Definition: tusb_types.h:50
static TU_ATTR_ALWAYS_INLINE uint8_t tu_edpt_number(uint8_t addr)
Definition: tusb_types.h:507
@ XFER_RESULT_SUCCESS
Definition: tusb_types.h:237
static TU_ATTR_ALWAYS_INLINE uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const *desc_ep)
Definition: tusb_types.h:515
@ TUSB_XFER_CONTROL
Definition: tusb_types.h:59
@ TUSB_XFER_ISOCHRONOUS
Definition: tusb_types.h:60
TU_ATTR_PACKED_END TU_ATTR_BIT_FIELD_ORDER_END static TU_ATTR_ALWAYS_INLINE tusb_dir_t tu_edpt_dir(uint8_t addr)
Definition: tusb_types.h:502
static TU_ATTR_ALWAYS_INLINE uint8_t tu_edpt_addr(uint8_t num, uint8_t dir)
Definition: tusb_types.h:511