Open FFBoard
Open source force feedback firmware
dcd_lpc_ip3511.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#include "tusb_option.h"
28
29/* Since 2012 starting with LPC11uxx, NXP start to use common USB Device Controller with code name LPC IP3511
30 * for almost their new MCUs. Currently supported and tested families are
31 * - LPC11U68, LPC11U37
32 * - LPC1347
33 * - LPC51U68
34 * - LPC54114
35 * - LPC55s69
36 */
37#if CFG_TUD_ENABLED && defined(TUP_USBIP_IP3511)
38
39//--------------------------------------------------------------------+
40// INCLUDE
41//--------------------------------------------------------------------+
42
43#if TU_CHECK_MCU(OPT_MCU_LPC11UXX, OPT_MCU_LPC13XX, OPT_MCU_LPC15XX)
44 // LPCOpen
45 #ifdef __GNUC__
46 #pragma GCC diagnostic push
47 #pragma GCC diagnostic ignored "-Wunused-parameter"
48 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
49 #endif
50
51 #include "chip.h"
52
53 #ifdef __GNUC__
54 #pragma GCC diagnostic pop
55 #endif
56
57#else
58 // SDK
59 #include "fsl_device_registers.h"
60 #define INCLUDE_FSL_DEVICE_REGISTERS
61#endif
62
63#include "device/dcd.h"
64
65//--------------------------------------------------------------------+
66// IP3511 Registers
67//--------------------------------------------------------------------+
68
69typedef struct {
70 __IO uint32_t DEVCMDSTAT; // Device Command/Status register, offset: 0x0
71 __I uint32_t INFO; // Info register, offset: 0x4
72 __IO uint32_t EPLISTSTART; // EP Command/Status List start address, offset: 0x8
73 __IO uint32_t DATABUFSTART; // Data buffer start address, offset: 0xC
74 __IO uint32_t LPM; // Link Power Management register, offset: 0x10
75 __IO uint32_t EPSKIP; // Endpoint skip, offset: 0x14
76 __IO uint32_t EPINUSE; // Endpoint Buffer in use, offset: 0x18
77 __IO uint32_t EPBUFCFG; // Endpoint Buffer Configuration register, offset: 0x1C
78 __IO uint32_t INTSTAT; // interrupt status register, offset: 0x20
79 __IO uint32_t INTEN; // interrupt enable register, offset: 0x24
80 __IO uint32_t INTSETSTAT; // set interrupt status register, offset: 0x28
81 uint8_t RESERVED_0[8];
82 __I uint32_t EPTOGGLE; // Endpoint toggle register, offset: 0x34
84
85// Max nbytes for each control/bulk/interrupt transfer
86enum {
87 NBYTES_ISO_FS_MAX = 1023, // FS ISO
88 NBYTES_ISO_HS_MAX = 1024, // HS ISO
89 NBYTES_CBI_FS_MAX = 64, // FS control/bulk/interrupt. TODO some FS can do burst with higher size e.g 1024. Need to test
90 NBYTES_CBI_HS_MAX = 32767 // can be up to all 15-bit, but only tested with 4096
91};
92
93enum {
94 INT_SOF_MASK = TU_BIT(30),
95 INT_DEVICE_STATUS_MASK = TU_BIT(31)
96};
97
98enum {
102 DEVCMDSTAT_DEVICE_CONNECT_MASK = TU_BIT(16), // reflect the soft-connect only, does not reflect the actual attached state
104 // 23-22 is link speed (only available for HighSpeed port)
109};
110
111enum {
114
115//--------------------------------------------------------------------+
116// Endpoint Command/Status List
117//--------------------------------------------------------------------+
118
119// EP Command/Status field definition
120enum {
121 EPCS_TYPE = TU_BIT(26),
122 EPCS_RF_TV = TU_BIT(27),
123 EPCS_TOGGLE_RESET = TU_BIT(28),
124 EPCS_STALL = TU_BIT(29),
125 EPCS_DISABLED = TU_BIT(30),
126 EPCS_ACTIVE = TU_BIT(31),
127};
128
129// Endpoint Command/Status
130typedef union TU_ATTR_PACKED
131{
132 // Full and High speed has different bit layout for buffer_offset and nbytes
133 // TODO FS/HS layout depends on the max speed of controller e.g
134 // lpc55s69 PORT0 is only FS but actually has the same layout as HS on port1
135
136 // Buffer (aligned 64) = DATABUFSTART [31:22] | buffer_offset [21:6]
137 volatile struct {
138 uint32_t offset : 16;
139 uint32_t nbytes : 10;
140 uint32_t TU_RESERVED : 6;
142
143 // Buffer (aligned 64) = USB_RAM [31:17] | buffer_offset [16:6]
144 volatile struct {
145 uint32_t offset : 11 ;
146 uint32_t nbytes : 15 ;
147 uint32_t TU_RESERVED : 6 ;
149
150 volatile struct {
151 uint32_t TU_RESERVED : 26;
152 uint32_t type : 1 ;
153 uint32_t rf_tv : 1 ; // rate feedback or toggle value
154 uint32_t toggle_reset : 1 ;
155 uint32_t stall : 1 ;
156 uint32_t disable : 1 ;
157 uint32_t active : 1 ;
160
161TU_VERIFY_STATIC( sizeof(ep_cmd_sts_t) == 4, "size is not correct" );
162
163// Software transfer management
164typedef struct
165{
166 uint16_t total_bytes;
168
169 uint16_t nbytes;
170
171 // prevent unaligned access on Highspeed port on USB_SRAM
172 uint16_t TU_RESERVED;
174
175// Absolute max of endpoints pairs for all port
176// - 11 13 15 51 54 has 5x2 endpoints
177// - 55 usb0 (FS) has 5x2 endpoints, usb1 (HS) has 6x2 endpoints
178#define MAX_EP_PAIRS 6
179
180// NOTE data will be transferred as soon as dcd get request by dcd_pipe(_queue)_xfer using double buffering.
181// current_td is used to keep track of number of remaining & xferred bytes of the current request.
182typedef struct
183{
184 // 256 byte aligned, 2 for double buffer (not used)
185 // Each cmd_sts can only transfer up to DMA_NBYTES_MAX bytes each
186 ep_cmd_sts_t ep[2*MAX_EP_PAIRS][2];
187 xfer_dma_t dma[2*MAX_EP_PAIRS];
188
189 TU_ATTR_ALIGNED(64) uint8_t setup_packet[8];
191
192// EP list must be 256-byte aligned
193// Some MCU controller may require this variable to be placed in specific SRAM region.
194// For example: LPC55s69 port1 Highspeed must be USB_RAM (0x40100000)
195// Use CFG_TUD_MEM_SECTION to place it accordingly.
196CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(256) static dcd_data_t _dcd;
197
198// Dummy buffer to fix ZLPs overwriting the buffer (probably an USB/DMA controller bug)
199// TODO find way to save memory
200CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(64) static uint8_t dummy[8];
201
202//--------------------------------------------------------------------+
203// Multiple Controllers
204//--------------------------------------------------------------------+
205
206typedef struct
207{
208 dcd_registers_t* regs; // registers
209 const bool is_highspeed; // max link speed
210 const IRQn_Type irqnum; // IRQ number
211 const uint8_t ep_pairs; // Max bi-directional Endpoints
213
214#ifdef INCLUDE_FSL_DEVICE_REGISTERS
215
217 { .regs = (dcd_registers_t*) USB0_BASE , .is_highspeed = false, .irqnum = USB0_IRQn, .ep_pairs = FSL_FEATURE_USB_EP_NUM },
218 #if defined(FSL_FEATURE_SOC_USBHSD_COUNT) && FSL_FEATURE_SOC_USBHSD_COUNT
219 { .regs = (dcd_registers_t*) USBHSD_BASE, .is_highspeed = true, .irqnum = USB1_IRQn, .ep_pairs = FSL_FEATURE_USBHSD_EP_NUM }
220 #endif
221};
222
223#else
224
225static const dcd_controller_t _dcd_controller[] = {
226 { .regs = (dcd_registers_t*) LPC_USB0_BASE, .is_highspeed = false, .irqnum = USB0_IRQn, .ep_pairs = 5 },
227};
228
229#endif
230
231#if defined(FSL_FEATURE_SOC_USBHSD_COUNT) && FSL_FEATURE_SOC_USBHSD_COUNT
232 #define IP3511_HAS_HIGHSPEED
233#endif
234
235//--------------------------------------------------------------------+
236// INTERNAL OBJECT & FUNCTION DECLARATION
237//--------------------------------------------------------------------+
238
239TU_ATTR_ALWAYS_INLINE static inline uint16_t get_buf_offset(void const * buffer) {
240 uint32_t addr = (uint32_t) buffer;
241 TU_ASSERT( (addr & 0x3f) == 0, 0 );
242 return ( (addr >> 6) & 0xFFFFUL ) ;
243}
244
245TU_ATTR_ALWAYS_INLINE static inline uint8_t ep_addr2id(uint8_t ep_addr) {
246 return 2*(ep_addr & 0x0F) + ((ep_addr & TUSB_DIR_IN_MASK) ? 1 : 0);
247}
248
249TU_ATTR_ALWAYS_INLINE static inline bool ep_is_iso(ep_cmd_sts_t* ep_cs, bool is_highspeed) {
250 return is_highspeed ? (ep_cs[0].cmd_sts.type && !ep_cs[0].cmd_sts.rf_tv) : ep_cs->cmd_sts.type;
251}
252
253TU_ATTR_ALWAYS_INLINE TU_ATTR_UNUSED static inline bool ep_is_bulk(ep_cmd_sts_t* ep_cs) {
254 return (ep_cs[0].cmd_sts.type == 0) && (ep_cs[0].cmd_sts.rf_tv == 0);
255}
256
257TU_ATTR_ALWAYS_INLINE static inline ep_cmd_sts_t* get_ep_cs(uint8_t ep_id) {
258 return _dcd.ep[ep_id];
259}
260
261TU_ATTR_ALWAYS_INLINE static inline bool rhport_is_highspeed(uint8_t rhport) {
262 return _dcd_controller[rhport].is_highspeed;
263}
264
265//--------------------------------------------------------------------+
266// CONTROLLER API
267//--------------------------------------------------------------------+
268
269static void prepare_setup_packet(uint8_t rhport) {
270 uint16_t const buf_offset = get_buf_offset(_dcd.setup_packet);
271 if ( _dcd_controller[rhport].is_highspeed ) {
272 _dcd.ep[0][1].buffer_hs.offset = buf_offset;
273 } else {
274 _dcd.ep[0][1].buffer_fs.offset = buf_offset;
275 }
276}
277
278static void edpt_reset(uint8_t rhport, uint8_t ep_id)
279{
280 (void) rhport;
281 tu_memclr(&_dcd.ep[ep_id], sizeof(_dcd.ep[ep_id]));
282}
283
284static void edpt_reset_all(uint8_t rhport)
285{
286 for (uint8_t ep_id = 0; ep_id < 2*_dcd_controller[rhport].ep_pairs; ++ep_id)
287 {
288 edpt_reset(rhport, ep_id);
289 }
290 prepare_setup_packet(rhport);
291}
292bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
293 (void) rh_init;
294 edpt_reset_all(rhport);
295
296 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
297
298 dcd_reg->EPLISTSTART = (uint32_t) _dcd.ep;
299 dcd_reg->DATABUFSTART = tu_align((uint32_t) &_dcd, TU_BIT(22)); // 22-bit alignment
300 dcd_reg->INTSTAT |= dcd_reg->INTSTAT; // clear all pending interrupt
301 dcd_reg->INTEN = INT_DEVICE_STATUS_MASK;
304
305 NVIC_ClearPendingIRQ(_dcd_controller[rhport].irqnum);
306
307 return true;
308}
309
310void dcd_int_enable(uint8_t rhport)
311{
312 NVIC_EnableIRQ(_dcd_controller[rhport].irqnum);
313}
314
315void dcd_int_disable(uint8_t rhport)
316{
317 NVIC_DisableIRQ(_dcd_controller[rhport].irqnum);
318}
319
320void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
321{
322 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
323
324 // Response with status first before changing device address
325 dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
326
327 dcd_reg->DEVCMDSTAT &= ~DEVCMDSTAT_DEVICE_ADDR_MASK;
328 dcd_reg->DEVCMDSTAT |= dev_addr;
329}
330
331void dcd_remote_wakeup(uint8_t rhport)
332{
333 (void) rhport;
334}
335
336void dcd_connect(uint8_t rhport)
337{
338 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
340}
341
342void dcd_disconnect(uint8_t rhport)
343{
344 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
345 dcd_reg->DEVCMDSTAT &= ~DEVCMDSTAT_DEVICE_CONNECT_MASK;
346}
347
348void dcd_sof_enable(uint8_t rhport, bool en)
349{
350 (void) rhport;
351 (void) en;
352
353 // TODO implement later
354}
355
356//--------------------------------------------------------------------+
357// DCD Endpoint Port
358//--------------------------------------------------------------------+
359void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
360{
361 (void) rhport;
362
363 // TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around
364 uint8_t const ep_id = ep_addr2id(ep_addr);
365 _dcd.ep[ep_id][0].cmd_sts.stall = 1;
366}
367
368void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
369{
370 (void) rhport;
371
372 uint8_t const ep_id = ep_addr2id(ep_addr);
373
374 _dcd.ep[ep_id][0].cmd_sts.stall = 0;
375 _dcd.ep[ep_id][0].cmd_sts.toggle_reset = 1;
376 _dcd.ep[ep_id][0].cmd_sts.rf_tv = 0;
377}
378
379bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
380{
381 //------------- Prepare Queue Head -------------//
382 uint8_t ep_id = ep_addr2id(p_endpoint_desc->bEndpointAddress);
383 ep_cmd_sts_t* ep_cs = get_ep_cs(ep_id);
384
385 // Check if endpoint is available
386 TU_ASSERT( ep_cs[0].cmd_sts.disable && ep_cs[1].cmd_sts.disable );
387
388 edpt_reset(rhport, ep_id);
389
390 switch (p_endpoint_desc->bmAttributes.xfer) {
392 ep_cs[0].cmd_sts.type = 1;
393 break;
394
396 // What is interrupt endpoint in rate feedback mode ?
397 if ( rhport_is_highspeed(rhport) ) {
398 ep_cs[0].cmd_sts.type = 1;
399 ep_cs[0].cmd_sts.rf_tv = 1;
400 }
401 break;
402
403 case TUSB_XFER_BULK:
404 // nothing to do both type and rf_tv are 0
405 break;
406
407 default: break;
408 }
409
410 // Enable EP interrupt
411 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
412 dcd_reg->INTEN |= TU_BIT(ep_id);
413
414 return true;
415}
416
417void dcd_edpt_close_all (uint8_t rhport)
418{
419 for (uint8_t ep_id = 0; ep_id < 2*_dcd_controller[rhport].ep_pairs; ++ep_id)
420 {
421 _dcd.ep[ep_id][0].cmd_sts.active = _dcd.ep[ep_id][0].cmd_sts.active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
422 _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
423 }
424}
425
426void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
427{
428 (void) rhport;
429
430 uint8_t ep_id = ep_addr2id(ep_addr);
431 _dcd.ep[ep_id][0].cmd_sts.active = _dcd.ep[ep_id][0].cmd_sts.active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
432 _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
433}
434
435static void prepare_ep_xfer(uint8_t rhport, uint8_t ep_id, uint16_t buf_offset, uint16_t total_bytes) {
436 uint16_t nbytes;
437 ep_cmd_sts_t* ep_cs = get_ep_cs(ep_id);
438
439 const bool is_iso = ep_is_iso(ep_cs, _dcd_controller[rhport].is_highspeed);
440
441 if ( rhport_is_highspeed(rhport) ) {
443 #if TU_CHECK_MCU(OPT_MCU_LPC54)
444 // LPC54 Errata USB.1: In USB high-speed device mode, the NBytes field does not decrement after BULK OUT transfer.
445 // Suggested Work-around: Program the NByte to the max packet size (512)
446 // Actual Work-around: round up NByte to multiple of 4.
447 // Note: this can cause buffer overflowed and corrupt data if host send more data than total_bytes
448 if ( (ep_id > 1) && (ep_id & 0x01) == 0 && ep_is_bulk(ep_cs) ) {
449 if ( nbytes & 0x03 ) {
450 nbytes = tu_align4(nbytes) + 4;
451 }
452 }
453 #endif
454
455 ep_cs[0].buffer_hs.offset = buf_offset;
456 ep_cs[0].buffer_hs.nbytes = nbytes;
457 }else {
459 ep_cs[0].buffer_fs.offset = buf_offset;
460 ep_cs[0].buffer_fs.nbytes = nbytes;
461 }
462
463 _dcd.dma[ep_id].nbytes = nbytes;
464 ep_cs[0].cmd_sts.active = 1;
465}
466
467bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) {
468 uint8_t const ep_id = ep_addr2id(ep_addr);
469
470 if (!buffer || total_bytes == 0) {
471 // Although having no data, ZLPs can cause buffer overwritten to zeroes. Probably due to USB/DMA controller side
472 // effect/bug. Assigned buffer offset to (valid) dummy to prevent overwriting to DATABUFSTART
473 buffer = (uint8_t *) (uint32_t) dummy;
474 }
475
476 tu_memclr(&_dcd.dma[ep_id], sizeof(xfer_dma_t));
477 _dcd.dma[ep_id].total_bytes = total_bytes;
478
480
481 return true;
482}
483
484//--------------------------------------------------------------------+
485// IRQ
486//--------------------------------------------------------------------+
487static void bus_reset(uint8_t rhport)
488{
489 tu_memclr(&_dcd, sizeof(dcd_data_t));
490 edpt_reset_all(rhport);
491
492 // disable all endpoints as specified by LPC55S69 UM Table 778
493 for(uint8_t ep_id = 0; ep_id < 2*MAX_EP_PAIRS; ep_id++)
494 {
495 _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
496 }
497
498 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
499
500 dcd_reg->EPINUSE = 0;
501 dcd_reg->EPBUFCFG = 0;
502 dcd_reg->EPSKIP = 0xFFFFFFFF;
503
504 dcd_reg->INTSTAT = dcd_reg->INTSTAT; // clear all pending interrupt
505 dcd_reg->DEVCMDSTAT |= DEVCMDSTAT_SETUP_RECEIVED_MASK; // clear setup received interrupt
506 dcd_reg->INTEN = INT_DEVICE_STATUS_MASK | TU_BIT(0) | TU_BIT(1); // enable device status & control endpoints
507}
508
509static void process_xfer_isr(uint8_t rhport, uint32_t int_status) {
510 uint8_t const max_ep = 2*_dcd_controller[rhport].ep_pairs;
511
512 for(uint8_t ep_id = 0; ep_id < max_ep; ep_id++ ) {
513 if ( tu_bit_test(int_status, ep_id) ) {
514 ep_cmd_sts_t * ep_cs = &_dcd.ep[ep_id][0];
515 xfer_dma_t* xfer_dma = &_dcd.dma[ep_id];
516
517 if ( ep_id <= 1 ) {
518 // For control endpoint, we need to manually clear Active bit
519 ep_cs->cmd_sts.active = 0;
520 }
521
522 uint16_t buf_offset;
523 uint16_t buf_nbytes;
524
525 if ( rhport_is_highspeed(rhport) ) {
526 buf_offset = ep_cs->buffer_hs.offset;
527 buf_nbytes = ep_cs->buffer_hs.nbytes;
528
529 #if TU_CHECK_MCU(OPT_MCU_LPC54)
530 // LPC54 Errata USB.2: In USB high-speed device mode, the NBytes field is not correct after BULK IN transfer
531 // There is no work-around. For EP in transfer, the NByte value can be ignored after a packet is transmitted.
532 if ( (ep_id > 1) && (ep_id & 0x01) == 1 && ep_is_bulk(ep_cs) ) {
533 buf_nbytes = 0;
534 }
535 #endif
536 } else {
537 buf_offset = ep_cs->buffer_fs.offset;
538 buf_nbytes = ep_cs->buffer_fs.nbytes;
539 }
540
541 xfer_dma->xferred_bytes += xfer_dma->nbytes - buf_nbytes;
542
543 if ( (buf_nbytes == 0) && (xfer_dma->total_bytes > xfer_dma->xferred_bytes) ) {
544 // There is more data to transfer
545 // buff_offset has been already increased by hw to correct value for next transfer
546 prepare_ep_xfer(rhport, ep_id, buf_offset, xfer_dma->total_bytes - xfer_dma->xferred_bytes);
547 } else {
548 // for detecting ZLP
549 xfer_dma->total_bytes = xfer_dma->xferred_bytes;
550
551 uint8_t const ep_addr = tu_edpt_addr(ep_id / 2, ep_id & 0x01);
552
553 // TODO no way determine if the transfer is failed or not
554 dcd_event_xfer_complete(rhport, ep_addr, xfer_dma->xferred_bytes, XFER_RESULT_SUCCESS, true);
555 }
556 }
557 }
558}
559
560void dcd_int_handler(uint8_t rhport)
561{
562 dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
563
564 uint32_t const cmd_stat = dcd_reg->DEVCMDSTAT;
565
566 uint32_t int_status = dcd_reg->INTSTAT & dcd_reg->INTEN;
567 dcd_reg->INTSTAT = int_status; // Acknowledge handled interrupt
568
569 if (int_status == 0) return;
570
571 //------------- Device Status -------------//
572 if ( int_status & INT_DEVICE_STATUS_MASK )
573 {
575
576 if ( cmd_stat & DEVCMDSTAT_RESET_CHANGE_MASK) // bus reset
577 {
578 bus_reset(rhport);
579
581 if ( _dcd_controller[rhport].is_highspeed ) {
582 // 0 : reserved, 1 : full, 2 : high, 3: super
583 if ( 2 == ((cmd_stat >> DEVCMDSTAT_SPEED_SHIFT) & 0x3UL) ) {
584 speed= TUSB_SPEED_HIGH;
585 }
586 }
587
588 dcd_event_bus_reset(rhport, speed, true);
589 }
590
591 if (cmd_stat & DEVCMDSTAT_CONNECT_CHANGE_MASK)
592 {
593 // device disconnect
594 if (cmd_stat & DEVCMDSTAT_DEVICE_ADDR_MASK)
595 {
596 // debouncing as this can be set when device is powering
597 dcd_event_bus_signal(rhport, DCD_EVENT_UNPLUGGED, true);
598 }
599 }
600
601 if (cmd_stat & DEVCMDSTAT_SUSPEND_CHANGE_MASK)
602 {
603 // suspend signal, bus idle for more than 3ms
604 // Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration.
605 if (cmd_stat & DEVCMDSTAT_DEVICE_ADDR_MASK)
606 {
607 dcd_event_bus_signal(rhport, (cmd_stat & DEVCMDSTAT_DEVICE_SUSPEND_MASK) ? DCD_EVENT_SUSPEND : DCD_EVENT_RESUME, true);
608 }
609 }
610 }
611
612 // Setup Receive
613 if ( tu_bit_test(int_status, 0) && (cmd_stat & DEVCMDSTAT_SETUP_RECEIVED_MASK) )
614 {
615 // Follow UM flowchart to clear Active & Stall on both Control IN/OUT endpoints
616 _dcd.ep[0][0].cmd_sts.active = _dcd.ep[1][0].cmd_sts.active = 0;
617 _dcd.ep[0][0].cmd_sts.stall = _dcd.ep[1][0].cmd_sts.stall = 0;
618
620
621 dcd_event_setup_received(rhport, _dcd.setup_packet, true);
622
623 // keep waiting for next setup
624 prepare_setup_packet(rhport);
625
626 // clear bit0
627 int_status = tu_bit_clear(int_status, 0);
628 }
629
630 // Endpoint transfer complete interrupt
631 process_xfer_isr(rhport, int_status);
632}
633
634#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
static TU_ATTR_ALWAYS_INLINE bool rhport_is_highspeed(uint8_t rhport)
@ DEVCMDSTAT_SUSPEND_CHANGE_MASK
@ DEVCMDSTAT_DEVICE_SUSPEND_MASK
@ DEVCMDSTAT_DEVICE_ADDR_MASK
@ DEVCMDSTAT_DEVICE_ENABLE_MASK
@ DEVCMDSTAT_DEVICE_CONNECT_MASK
@ DEVCMDSTAT_VBUS_DEBOUNCED_MASK
@ DEVCMDSTAT_RESET_CHANGE_MASK
@ DEVCMDSTAT_CONNECT_CHANGE_MASK
@ DEVCMDSTAT_SETUP_RECEIVED_MASK
static void edpt_reset_all(uint8_t rhport)
static TU_ATTR_ALWAYS_INLINE uint16_t get_buf_offset(void const *buffer)
@ EPCS_DISABLED
@ EPCS_STALL
@ EPCS_ACTIVE
@ EPCS_TYPE
@ EPCS_RF_TV
@ EPCS_TOGGLE_RESET
static TU_ATTR_ALWAYS_INLINE ep_cmd_sts_t * get_ep_cs(uint8_t ep_id)
@ INT_SOF_MASK
@ INT_DEVICE_STATUS_MASK
TU_VERIFY_STATIC(sizeof(ep_cmd_sts_t)==4, "size is not correct")
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
static void bus_reset(uint8_t rhport)
void dcd_int_handler(uint8_t rhport)
void dcd_disconnect(uint8_t rhport)
static void edpt_reset(uint8_t rhport, uint8_t ep_id)
static void prepare_setup_packet(uint8_t rhport)
void dcd_edpt_close_all(uint8_t rhport)
void dcd_int_disable(uint8_t rhport)
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *p_endpoint_desc)
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
static void prepare_ep_xfer(uint8_t rhport, uint8_t ep_id, uint16_t buf_offset, uint16_t total_bytes)
void dcd_connect(uint8_t rhport)
CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(256)
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
TU_ATTR_ALWAYS_INLINE static TU_ATTR_UNUSED bool ep_is_bulk(ep_cmd_sts_t *ep_cs)
static const dcd_controller_t _dcd_controller[]
void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
static void process_xfer_isr(uint8_t rhport, uint32_t int_status)
@ DEVCMDSTAT_SPEED_SHIFT
static TU_ATTR_ALWAYS_INLINE bool ep_is_iso(ep_cmd_sts_t *ep_cs, bool is_highspeed)
bool dcd_init(uint8_t rhport, const tusb_rhport_init_t *rh_init)
@ NBYTES_CBI_HS_MAX
@ NBYTES_ISO_FS_MAX
@ NBYTES_CBI_FS_MAX
@ NBYTES_ISO_HS_MAX
static TU_ATTR_ALWAYS_INLINE uint8_t ep_addr2id(uint8_t ep_addr)
union TU_ATTR_PACKED ep_cmd_sts_t
void dcd_int_enable(uint8_t rhport)
void dcd_remote_wakeup(uint8_t rhport)
void dcd_sof_enable(uint8_t rhport, bool en)
dcd_controller_t
uint16_t total_bytes
Definition: dcd_nuc505.c:113
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
uint8_t const * buffer
Definition: midi_device.h:100
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
volatile struct TU_ATTR_PACKED::@419 cmd_sts
uint8_t bmAttributes
See: audio_clock_source_attribute_t.
Definition: audio.h:672
uint32_t offset
Definition: video_device.c:113
volatile struct TU_ATTR_PACKED::@418 buffer_hs
uint32_t toggle_reset
uint8_t bEndpointAddress
Definition: video.h:306
uint8_t TU_RESERVED
Definition: cdc.h:295
uint32_t TU_RESERVED
volatile struct TU_ATTR_PACKED::@417 buffer_fs
USBHS hardware registers.
__IO uint32_t DATABUFSTART
__IO uint32_t LPM
__IO uint32_t INTSETSTAT
__IO uint32_t INTSTAT
__IO uint32_t INTEN
__IO uint32_t EPLISTSTART
__IO uint32_t EPINUSE
__IO uint32_t DEVCMDSTAT
__I uint32_t EPTOGGLE
__I uint32_t INFO
__IO uint32_t EPSKIP
__IO uint32_t EPBUFCFG
uint16_t TU_RESERVED
uint16_t xferred_bytes
uint16_t nbytes
uint16_t total_bytes
static TU_ATTR_ALWAYS_INLINE uint32_t tu_bit_clear(uint32_t value, uint8_t pos)
Definition: tusb_common.h:150
static TU_ATTR_ALWAYS_INLINE uint16_t tu_min16(uint16_t x, uint16_t y)
Definition: tusb_common.h:155
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align4(uint32_t value)
Definition: tusb_common.h:168
static TU_ATTR_ALWAYS_INLINE bool tu_bit_test(uint32_t value, uint8_t pos)
Definition: tusb_common.h:151
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align(uint32_t value, uint32_t alignment)
Definition: tusb_common.h:164
@ TUSB_DIR_IN
Definition: tusb_types.h:67
@ TUSB_DIR_IN_MASK
Definition: tusb_types.h:69
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition: tusb_types.h:49
@ TUSB_SPEED_FULL
Definition: tusb_types.h:50
@ TUSB_SPEED_HIGH
Definition: tusb_types.h:52
@ XFER_RESULT_SUCCESS
Definition: tusb_types.h:237
@ TUSB_XFER_ISOCHRONOUS
Definition: tusb_types.h:60
@ TUSB_XFER_INTERRUPT
Definition: tusb_types.h:62
@ TUSB_XFER_BULK
Definition: tusb_types.h:61
static TU_ATTR_ALWAYS_INLINE uint8_t tu_edpt_addr(uint8_t num, uint8_t dir)
Definition: tusb_types.h:511