Open FFBoard
Open source force feedback firmware
dcd_ci_hs.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#if CFG_TUD_ENABLED && defined(TUP_USBIP_CHIPIDEA_HS)
30
31#include "device/dcd.h"
32#include "ci_hs_type.h"
33
34#if CFG_TUSB_MCU == OPT_MCU_MIMXRT1XXX
35 #include "ci_hs_imxrt.h"
36
37 void dcd_dcache_clean(void const* addr, uint32_t data_size) {
38 imxrt_dcache_clean(addr, data_size);
39 }
40
41 void dcd_dcache_invalidate(void const* addr, uint32_t data_size) {
42 imxrt_dcache_invalidate(addr, data_size);
43 }
44
45 void dcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) {
46 imxrt_dcache_clean_invalidate(addr, data_size);
47 }
48
49#else
50
51#if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX)
52 #include "ci_hs_lpc18_43.h"
53
54#elif TU_CHECK_MCU(OPT_MCU_MCXN9)
55 // MCX N9 only port 1 use this controller
56 #include "ci_hs_mcx.h"
57#else
58 #error "Unsupported MCUs"
59#endif
60
61 TU_ATTR_WEAK void dcd_dcache_clean(void const* addr, uint32_t data_size) {
62 (void) addr; (void) data_size;
63 }
64
65 TU_ATTR_WEAK void dcd_dcache_invalidate(void const* addr, uint32_t data_size) {
66 (void) addr; (void) data_size;
67 }
68
69 TU_ATTR_WEAK void dcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) {
70 (void) addr; (void) data_size;
71 }
72#endif
73
74//--------------------------------------------------------------------+
75// MACRO CONSTANT TYPEDEF
76//--------------------------------------------------------------------+
77
78// ENDPTCTRL
79enum {
80 ENDPTCTRL_STALL = TU_BIT(0),
81 ENDPTCTRL_TOGGLE_INHIBIT = TU_BIT(5), // used for test only
83 ENDPTCTRL_ENABLE = TU_BIT(7)
84};
85
86enum {
87 ENDPTCTRL_TYPE_POS = 2, // Endpoint type is 2-bit field
88};
89
90// USBSTS, USBINTR
91enum {
92 INTR_USB = TU_BIT(0),
93 INTR_ERROR = TU_BIT(1),
94 INTR_PORT_CHANGE = TU_BIT(2),
95 INTR_RESET = TU_BIT(6),
96 INTR_SOF = TU_BIT(7),
97 INTR_SUSPEND = TU_BIT(8),
98 INTR_NAK = TU_BIT(16)
99};
100
101// Queue Transfer Descriptor
102typedef struct
103{
104 // Word 0: Next QTD Pointer
105 uint32_t next;
106
107 // Word 1: qTQ Token
108 uint32_t : 3 ;
109 volatile uint32_t xact_err : 1 ;
110 uint32_t : 1 ;
111 volatile uint32_t buffer_err : 1 ;
112 volatile uint32_t halted : 1 ;
113 volatile uint32_t active : 1 ;
114 uint32_t : 2 ;
115 uint32_t iso_mult_override : 2 ;
116 uint32_t : 3 ;
117 uint32_t int_on_complete : 1 ;
118 volatile uint32_t total_bytes : 15 ;
119 uint32_t : 1 ;
120
121 // Word 2-6: Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page
122 uint32_t buffer[5];
123
124 //--------------------------------------------------------------------+
125 // TD is 32 bytes aligned but occupies only 28 bytes
126 // Therefore there are 4 bytes padding that we can use.
127 //--------------------------------------------------------------------+
129 uint8_t reserved[2];
130} dcd_qtd_t;
131
132TU_VERIFY_STATIC( sizeof(dcd_qtd_t) == 32, "size is not correct");
133
134// Queue Head
135typedef struct
136{
137 // Word 0: Capabilities and Characteristics
138 uint32_t : 15 ;
139 uint32_t int_on_setup : 1 ;
140 uint32_t max_packet_size : 11 ;
141 uint32_t : 2 ;
143 uint32_t iso_mult : 2 ;
144
145 // Word 1: Current qTD Pointer
146 volatile uint32_t qtd_addr;
147
148 // Word 2-9: Transfer Overlay
150
151 // Word 10-11: Setup request (control OUT only)
153
154 //--------------------------------------------------------------------+
155 // QHD is 64 bytes aligned but occupies only 48 bytes
156 // Therefore there are 16 bytes padding that we can use.
157 //--------------------------------------------------------------------+
159 uint8_t reserved[12];
160} dcd_qhd_t;
161
162TU_VERIFY_STATIC( sizeof(dcd_qhd_t) == 64, "size is not correct");
163
164//--------------------------------------------------------------------+
165// Variables
166//--------------------------------------------------------------------+
167
168#define QTD_NEXT_INVALID 0x01
169
170typedef struct {
171 // Must be at 2K alignment
172 // Each endpoint with direction (IN/OUT) occupies a queue head
173 // for portability, TinyUSB only queue 1 TD for each Qhd
174 dcd_qhd_t qhd[TUP_DCD_ENDPOINT_MAX][2] TU_ATTR_ALIGNED(64);
175 dcd_qtd_t qtd[TUP_DCD_ENDPOINT_MAX][2] TU_ATTR_ALIGNED(32);
177
178CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(2048)
179static dcd_data_t _dcd_data;
180
181//--------------------------------------------------------------------+
182// Prototypes and Helper Functions
183//--------------------------------------------------------------------+
184
185TU_ATTR_ALWAYS_INLINE
186static inline uint8_t ci_ep_count(ci_hs_regs_t const* dcd_reg)
187{
188 return dcd_reg->DCCPARAMS & DCCPARAMS_DEN_MASK;
189}
190
191//--------------------------------------------------------------------+
192// Controller API
193//--------------------------------------------------------------------+
194
196static void bus_reset(uint8_t rhport)
197{
198 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
199
200 // The reset value for all endpoint types is the control endpoint. If one endpoint
201 // direction is enabled and the paired endpoint of opposite direction is disabled, then the
202 // endpoint type of the unused direction must be changed from the control type to any other
203 // type (e.g. bulk). Leaving an un-configured endpoint control will cause undefined behavior
204 // for the data PID tracking on the active endpoint.
205 uint8_t const ep_count = ci_ep_count(dcd_reg);
206 for( uint8_t i=1; i < ep_count; i++)
207 {
209 }
210
211 //------------- Clear All Registers -------------//
212 dcd_reg->ENDPTNAK = dcd_reg->ENDPTNAK;
213 dcd_reg->ENDPTNAKEN = 0;
214 dcd_reg->USBSTS = dcd_reg->USBSTS;
215 dcd_reg->ENDPTSETUPSTAT = dcd_reg->ENDPTSETUPSTAT;
216 dcd_reg->ENDPTCOMPLETE = dcd_reg->ENDPTCOMPLETE;
217
218 while (dcd_reg->ENDPTPRIME) {}
219 dcd_reg->ENDPTFLUSH = 0xFFFFFFFF;
220 while (dcd_reg->ENDPTFLUSH) {}
221
222 // read reset bit in portsc
223
224 //------------- Queue Head & Queue TD -------------//
225 tu_memclr(&_dcd_data, sizeof(dcd_data_t));
226
227 //------------- Set up Control Endpoints (0 OUT, 1 IN) -------------//
228 _dcd_data.qhd[0][0].zero_length_termination = _dcd_data.qhd[0][1].zero_length_termination = 1;
229 _dcd_data.qhd[0][0].max_packet_size = _dcd_data.qhd[0][1].max_packet_size = CFG_TUD_ENDPOINT0_SIZE;
230 _dcd_data.qhd[0][0].qtd_overlay.next = _dcd_data.qhd[0][1].qtd_overlay.next = QTD_NEXT_INVALID;
231
232 _dcd_data.qhd[0][0].int_on_setup = 1; // OUT only
233
234 dcd_dcache_clean_invalidate(&_dcd_data, sizeof(dcd_data_t));
235}
236
237bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
238 (void) rh_init;
239 tu_memclr(&_dcd_data, sizeof(dcd_data_t));
240
241 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
242
243 TU_ASSERT(ci_ep_count(dcd_reg) <= TUP_DCD_ENDPOINT_MAX);
244
245 // Reset controller
246 dcd_reg->USBCMD |= USBCMD_RESET;
247 while( dcd_reg->USBCMD & USBCMD_RESET ) {}
248
249 // Set mode to device, must be set immediately after reset
250 uint32_t usbmode = dcd_reg->USBMODE & ~USBMOD_CM_MASK;
251 usbmode |= USBMODE_CM_DEVICE;
252 dcd_reg->USBMODE = usbmode;
253
255
256#if !TUD_OPT_HIGH_SPEED
258#endif
259
260 dcd_dcache_clean_invalidate(&_dcd_data, sizeof(dcd_data_t));
261
262 dcd_reg->ENDPTLISTADDR = (uint32_t) _dcd_data.qhd; // Endpoint List Address has to be 2K alignment
263 dcd_reg->USBSTS = dcd_reg->USBSTS;
265
266 uint32_t usbcmd = dcd_reg->USBCMD;
267 usbcmd &= ~USBCMD_INTR_THRESHOLD_MASK; // Interrupt Threshold Interval = 0
268 usbcmd |= USBCMD_RUN_STOP; // run
269
270 dcd_reg->USBCMD = usbcmd;
271
272 return true;
273}
274
275void dcd_int_enable(uint8_t rhport)
276{
277 CI_DCD_INT_ENABLE(rhport);
278}
279
280void dcd_int_disable(uint8_t rhport)
281{
282 CI_DCD_INT_DISABLE(rhport);
283}
284
285void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
286{
287 // Response with status first before changing device address
288 dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
289
290 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
291 dcd_reg->DEVICEADDR = (dev_addr << 25) | TU_BIT(24);
292}
293
294void dcd_remote_wakeup(uint8_t rhport)
295{
296 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
298}
299
300void dcd_connect(uint8_t rhport)
301{
302 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
303 dcd_reg->USBCMD |= USBCMD_RUN_STOP;
304}
305
306void dcd_disconnect(uint8_t rhport)
307{
308 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
309 dcd_reg->USBCMD &= ~USBCMD_RUN_STOP;
310}
311
312void dcd_sof_enable(uint8_t rhport, bool en)
313{
314 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
315 if (en) {
316 dcd_reg->USBINTR |= INTR_SOF;
317 } else {
318 dcd_reg->USBINTR &= ~INTR_SOF;
319 }
320}
321
322//--------------------------------------------------------------------+
323// HELPER
324//--------------------------------------------------------------------+
325
326static void qtd_init(dcd_qtd_t* p_qtd, void * data_ptr, uint16_t total_bytes)
327{
328 // Force the CPU to flush the buffer. We increase the size by 31 because the call aligns the
329 // address to 32-byte boundaries. Buffer must be word aligned
330 dcd_dcache_clean_invalidate((uint32_t*) tu_align((uint32_t) data_ptr, 4), total_bytes + 31);
331
332 tu_memclr(p_qtd, sizeof(dcd_qtd_t));
333
334 p_qtd->next = QTD_NEXT_INVALID;
335 p_qtd->active = 1;
336 p_qtd->total_bytes = p_qtd->expected_bytes = total_bytes;
337 p_qtd->int_on_complete = true;
338
339 if (data_ptr != NULL)
340 {
341 p_qtd->buffer[0] = (uint32_t) data_ptr;
342
343 uint32_t const bufend = p_qtd->buffer[0] + total_bytes;
344 for(uint8_t i=1; i<5; i++)
345 {
346 uint32_t const next_page = tu_align4k( p_qtd->buffer[i-1] ) + 4096;
347 if ( bufend <= next_page ) break;
348
349 p_qtd->buffer[i] = next_page;
350
351 // TODO page[1] FRAME_N for ISO transfer
352 }
353 }
354}
355
356//--------------------------------------------------------------------+
357// DCD Endpoint Port
358//--------------------------------------------------------------------+
359void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
360{
361 uint8_t const epnum = tu_edpt_number(ep_addr);
362 uint8_t const dir = tu_edpt_dir(ep_addr);
363
364 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
365 dcd_reg->ENDPTCTRL[epnum] |= ENDPTCTRL_STALL << (dir ? 16 : 0);
366
367 // flush to abort any primed buffer
368 dcd_reg->ENDPTFLUSH = TU_BIT(epnum + (dir ? 16 : 0));
369}
370
371void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
372{
373 uint8_t const epnum = tu_edpt_number(ep_addr);
374 uint8_t const dir = tu_edpt_dir(ep_addr);
375
376 // data toggle also need to be reset
377 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
378 dcd_reg->ENDPTCTRL[epnum] |= ENDPTCTRL_TOGGLE_RESET << ( dir ? 16 : 0 );
379 dcd_reg->ENDPTCTRL[epnum] &= ~(ENDPTCTRL_STALL << ( dir ? 16 : 0));
380}
381
382bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
383{
384 uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress);
385 uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
386
387 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
388
389 // Must not exceed max endpoint number
390 TU_ASSERT(epnum < ci_ep_count(dcd_reg));
391
392 //------------- Prepare Queue Head -------------//
393 dcd_qhd_t * p_qhd = &_dcd_data.qhd[epnum][dir];
394 tu_memclr(p_qhd, sizeof(dcd_qhd_t));
395
396 p_qhd->zero_length_termination = 1;
397 p_qhd->max_packet_size = tu_edpt_packet_size(p_endpoint_desc);
398 if (p_endpoint_desc->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
399 {
400 p_qhd->iso_mult = 1;
401 }
402
403 p_qhd->qtd_overlay.next = QTD_NEXT_INVALID;
404
405 dcd_dcache_clean_invalidate(&_dcd_data, sizeof(dcd_data_t));
406
407 // Enable EP Control
408 uint32_t const epctrl = (p_endpoint_desc->bmAttributes.xfer << ENDPTCTRL_TYPE_POS) | ENDPTCTRL_ENABLE | ENDPTCTRL_TOGGLE_RESET;
409
410 if ( dir == TUSB_DIR_OUT )
411 {
412 dcd_reg->ENDPTCTRL[epnum] = (dcd_reg->ENDPTCTRL[epnum] & 0xFFFF0000u) | epctrl;
413 }else
414 {
415 dcd_reg->ENDPTCTRL[epnum] = (dcd_reg->ENDPTCTRL[epnum] & 0x0000FFFFu) | (epctrl << 16);
416 }
417
418 return true;
419}
420
421void dcd_edpt_close_all (uint8_t rhport)
422{
423 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
424
425 // Disable all non-control endpoints
426 uint8_t const ep_count = ci_ep_count(dcd_reg);
427 for (uint8_t epnum = 1; epnum < ep_count; epnum++)
428 {
429 _dcd_data.qhd[epnum][TUSB_DIR_OUT].qtd_overlay.halted = 1;
430 _dcd_data.qhd[epnum][TUSB_DIR_IN ].qtd_overlay.halted = 1;
431
432 dcd_reg->ENDPTFLUSH = TU_BIT(epnum) | TU_BIT(epnum+16);
434 }
435}
436
437void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
438{
439 uint8_t const epnum = tu_edpt_number(ep_addr);
440 uint8_t const dir = tu_edpt_dir(ep_addr);
441
442 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
443
444 _dcd_data.qhd[epnum][dir].qtd_overlay.halted = 1;
445
446 // Flush EP
447 uint32_t const flush_mask = TU_BIT(epnum + (dir ? 16 : 0));
448 dcd_reg->ENDPTFLUSH = flush_mask;
449 while(dcd_reg->ENDPTFLUSH & flush_mask);
450
451 // Clear EP enable
452 dcd_reg->ENDPTCTRL[epnum] &=~(ENDPTCTRL_ENABLE << (dir ? 16 : 0));
453}
454
455static void qhd_start_xfer(uint8_t rhport, uint8_t epnum, uint8_t dir)
456{
457 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
458 dcd_qhd_t* p_qhd = &_dcd_data.qhd[epnum][dir];
459 dcd_qtd_t* p_qtd = &_dcd_data.qtd[epnum][dir];
460
461 p_qhd->qtd_overlay.halted = false; // clear any previous error
462 p_qhd->qtd_overlay.next = (uint32_t) p_qtd; // link qtd to qhd
463
464 // flush cache
465 dcd_dcache_clean_invalidate(&_dcd_data, sizeof(dcd_data_t));
466
467 if ( epnum == 0 )
468 {
469 // follows UM 24.10.8.1.1 Setup packet handling using setup lockout mechanism
470 // wait until ENDPTSETUPSTAT before priming data/status in response TODO add time out
471 while(dcd_reg->ENDPTSETUPSTAT & TU_BIT(0)) {}
472 }
473
474 // start transfer
475 dcd_reg->ENDPTPRIME = TU_BIT(epnum + (dir ? 16 : 0));
476}
477
478bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
479{
480 uint8_t const epnum = tu_edpt_number(ep_addr);
481 uint8_t const dir = tu_edpt_dir(ep_addr);
482
483 dcd_qhd_t* p_qhd = &_dcd_data.qhd[epnum][dir];
484 dcd_qtd_t* p_qtd = &_dcd_data.qtd[epnum][dir];
485
486 // Prepare qtd
487 qtd_init(p_qtd, buffer, total_bytes);
488
489 // Start qhd transfer
490 p_qhd->ff = NULL;
491 qhd_start_xfer(rhport, epnum, dir);
492
493 return true;
494}
495
496// fifo has to be aligned to 4k boundary
497bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes)
498{
499 uint8_t const epnum = tu_edpt_number(ep_addr);
500 uint8_t const dir = tu_edpt_dir(ep_addr);
501
502 dcd_qhd_t * p_qhd = &_dcd_data.qhd[epnum][dir];
503 dcd_qtd_t * p_qtd = &_dcd_data.qtd[epnum][dir];
504
505 tu_fifo_buffer_info_t fifo_info;
506
507 if (dir)
508 {
509 tu_fifo_get_read_info(ff, &fifo_info);
510 } else
511 {
512 tu_fifo_get_write_info(ff, &fifo_info);
513 }
514
515 if ( fifo_info.len_lin >= total_bytes )
516 {
517 // Linear length is enough for this transfer
518 qtd_init(p_qtd, fifo_info.ptr_lin, total_bytes);
519 }
520 else
521 {
522 // linear part is not enough
523
524 // prepare TD up to linear length
525 qtd_init(p_qtd, fifo_info.ptr_lin, fifo_info.len_lin);
526
527 if ( !tu_offset4k((uint32_t) fifo_info.ptr_wrap) && !tu_offset4k(tu_fifo_depth(ff)) )
528 {
529 // If buffer is aligned to 4K & buffer size is multiple of 4K
530 // We can make use of buffer page array to also combine the linear + wrapped length
531 p_qtd->total_bytes = p_qtd->expected_bytes = total_bytes;
532
533 for(uint8_t i = 1, page = 0; i < 5; i++)
534 {
535 // pick up buffer array where linear ends
536 if (p_qtd->buffer[i] == 0)
537 {
538 p_qtd->buffer[i] = (uint32_t) fifo_info.ptr_wrap + 4096 * page;
539 page++;
540 }
541 }
542
543 dcd_dcache_clean_invalidate((uint32_t*) tu_align((uint32_t) fifo_info.ptr_wrap, 4), total_bytes - fifo_info.len_wrap + 31);
544 }
545 else
546 {
547 // TODO we may need to carry the wrapped length after the linear part complete
548 // for now only transfer up to linear part
549 }
550 }
551
552 // Start qhd transfer
553 p_qhd->ff = ff;
554 qhd_start_xfer(rhport, epnum, dir);
555
556 return true;
557}
558
559//--------------------------------------------------------------------+
560// ISR
561//--------------------------------------------------------------------+
562
563static void process_edpt_complete_isr(uint8_t rhport, uint8_t epnum, uint8_t dir)
564{
565 dcd_qhd_t * p_qhd = &_dcd_data.qhd[epnum][dir];
566 dcd_qtd_t * p_qtd = &_dcd_data.qtd[epnum][dir];
567
568 uint8_t result = p_qtd->halted ? XFER_RESULT_STALLED :
570
571 if ( result != XFER_RESULT_SUCCESS )
572 {
573 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
574 // flush to abort error buffer
575 dcd_reg->ENDPTFLUSH = TU_BIT(epnum + (dir ? 16 : 0));
576 }
577
578 uint16_t const xferred_bytes = p_qtd->expected_bytes - p_qtd->total_bytes;
579
580 if (p_qhd->ff)
581 {
582 if (dir == TUSB_DIR_IN)
583 {
584 tu_fifo_advance_read_pointer(p_qhd->ff, xferred_bytes);
585 } else
586 {
587 tu_fifo_advance_write_pointer(p_qhd->ff, xferred_bytes);
588 }
589 }
590
591 // only number of bytes in the IOC qtd
592 dcd_event_xfer_complete(rhport, tu_edpt_addr(epnum, dir), xferred_bytes, result, true);
593}
594
595void dcd_int_handler(uint8_t rhport)
596{
597 ci_hs_regs_t* dcd_reg = CI_HS_REG(rhport);
598
599 uint32_t const int_enable = dcd_reg->USBINTR;
600 uint32_t const int_status = dcd_reg->USBSTS & int_enable;
601 dcd_reg->USBSTS = int_status; // Acknowledge handled interrupt
602
603 // disabled interrupt sources
604 if (int_status == 0) return;
605
606 // Set if the port controller enters the full or high-speed operational state.
607 // either from Bus Reset or Suspended state
608 if (int_status & INTR_PORT_CHANGE)
609 {
610 // TU_LOG2("PortChange %08lx\r\n", dcd_reg->PORTSC1);
611
612 // Reset interrupt is not enabled, we manually check if Port Change is due
613 // to connection / disconnection
614 if ( dcd_reg->USBSTS & INTR_RESET )
615 {
616 dcd_reg->USBSTS = INTR_RESET;
617
619 {
620 uint32_t const speed = (dcd_reg->PORTSC1 & PORTSC1_PORT_SPEED) >> PORTSC1_PORT_SPEED_POS;
621 bus_reset(rhport);
622 dcd_event_bus_reset(rhport, (tusb_speed_t) speed, true);
623 }else
624 {
625 dcd_event_bus_signal(rhport, DCD_EVENT_UNPLUGGED, true);
626 }
627 }
628 else
629 {
630 // Triggered by resuming from suspended state
631 if ( !(dcd_reg->PORTSC1 & PORTSC1_SUSPEND) )
632 {
633 dcd_event_bus_signal(rhport, DCD_EVENT_RESUME, true);
634 }
635 }
636 }
637
638 if (int_status & INTR_SUSPEND)
639 {
640 // TU_LOG2("Suspend %08lx\r\n", dcd_reg->PORTSC1);
641
642 if (dcd_reg->PORTSC1 & PORTSC1_SUSPEND)
643 {
644 // Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration.
645 // Skip suspend event if we are not addressed
646 if ((dcd_reg->DEVICEADDR >> 25) & 0x0f)
647 {
648 dcd_event_bus_signal(rhport, DCD_EVENT_SUSPEND, true);
649 }
650 }
651 }
652
653 if (int_status & INTR_USB)
654 {
655 // Make sure we read the latest version of _dcd_data.
656 dcd_dcache_clean_invalidate(&_dcd_data, sizeof(dcd_data_t));
657
658 uint32_t const edpt_complete = dcd_reg->ENDPTCOMPLETE;
659 dcd_reg->ENDPTCOMPLETE = edpt_complete; // acknowledge
660
661 // 23.10.12.3 Failed QTD also get ENDPTCOMPLETE set
662 // nothing to do, we will submit xfer as error to usbd
663 // if (int_status & INTR_ERROR) { }
664
665 if ( edpt_complete )
666 {
667 for(uint8_t epnum = 0; epnum < TUP_DCD_ENDPOINT_MAX; epnum++)
668 {
669 if ( tu_bit_test(edpt_complete, epnum) ) process_edpt_complete_isr(rhport, epnum, TUSB_DIR_OUT);
670 if ( tu_bit_test(edpt_complete, epnum+16) ) process_edpt_complete_isr(rhport, epnum, TUSB_DIR_IN);
671 }
672 }
673
674 // Set up Received
675 // 23.10.10.2 Operational model for setup transfers
676 // Must be after normal transfer complete since it is possible to have both previous control status + new setup
677 // in the same frame and we should handle previous status first.
678 if (dcd_reg->ENDPTSETUPSTAT) {
679 dcd_reg->ENDPTSETUPSTAT = dcd_reg->ENDPTSETUPSTAT;
680 dcd_event_setup_received(rhport, (uint8_t *) (uintptr_t) &_dcd_data.qhd[0][0].setup_request, true);
681 }
682 }
683
684 if (int_status & INTR_SOF)
685 {
686 const uint32_t frame = dcd_reg->FRINDEX;
687 dcd_event_sof(rhport, frame, true);
688 }
689}
690
691#endif
static TU_ATTR_ALWAYS_INLINE bool imxrt_dcache_invalidate(void const *addr, uint32_t data_size)
Definition: ci_hs_imxrt.h:80
static TU_ATTR_ALWAYS_INLINE bool imxrt_dcache_clean(void const *addr, uint32_t data_size)
Definition: ci_hs_imxrt.h:71
static TU_ATTR_ALWAYS_INLINE bool imxrt_dcache_clean_invalidate(void const *addr, uint32_t data_size)
Definition: ci_hs_imxrt.h:92
static TU_ATTR_ALWAYS_INLINE ci_hs_regs_t * CI_HS_REG(uint8_t port)
Definition: ci_hs_mcx.h:40
@ PORTSC1_FORCE_FULL_SPEED
Definition: ci_hs_type.h:58
@ PORTSC1_PORT_SPEED
Definition: ci_hs_type.h:59
@ PORTSC1_CURRENT_CONNECT_STATUS
Definition: ci_hs_type.h:55
@ PORTSC1_FORCE_PORT_RESUME
Definition: ci_hs_type.h:56
@ PORTSC1_SUSPEND
Definition: ci_hs_type.h:57
@ USBCMD_RUN_STOP
Definition: ci_hs_type.h:41
@ USBCMD_RESET
Definition: ci_hs_type.h:42
@ USBCMD_INTR_THRESHOLD_MASK
Definition: ci_hs_type.h:48
@ OTGSC_VBUS_DISCHARGE
Definition: ci_hs_type.h:64
@ OTGSC_OTG_TERMINATION
Must set to 1 when OTG go to device mode.
Definition: ci_hs_type.h:67
@ USBMODE_CM_DEVICE
Definition: ci_hs_type.h:84
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_sof(uint8_t rhport, uint32_t frame_count, bool in_isr)
Definition: dcd.h:232
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
@ ENDPTCTRL_TYPE_POS
Definition: dcd_ci_hs.c:87
bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t *ff, uint16_t total_bytes)
Definition: dcd_ci_hs.c:497
CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED(2048)
Definition: dcd_ci_hs.c:178
@ ENDPTCTRL_TOGGLE_RESET
Definition: dcd_ci_hs.c:82
@ ENDPTCTRL_TOGGLE_INHIBIT
Definition: dcd_ci_hs.c:81
@ ENDPTCTRL_ENABLE
Definition: dcd_ci_hs.c:83
@ ENDPTCTRL_STALL
Definition: dcd_ci_hs.c:80
static void qhd_start_xfer(uint8_t rhport, uint8_t epnum, uint8_t dir)
Definition: dcd_ci_hs.c:455
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_ci_hs.c:359
void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_ci_hs.c:437
static void bus_reset(uint8_t rhport)
follows LPC43xx User Manual 23.10.3
Definition: dcd_ci_hs.c:196
void dcd_int_handler(uint8_t rhport)
Definition: dcd_ci_hs.c:595
void dcd_disconnect(uint8_t rhport)
Definition: dcd_ci_hs.c:306
TU_VERIFY_STATIC(sizeof(dcd_qtd_t)==32, "size is not correct")
void dcd_edpt_close_all(uint8_t rhport)
Definition: dcd_ci_hs.c:421
void dcd_int_disable(uint8_t rhport)
Definition: dcd_ci_hs.c:280
void dcd_dcache_invalidate(void const *addr, uint32_t data_size)
Definition: dcd_ci_hs.c:41
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *p_endpoint_desc)
Definition: dcd_ci_hs.c:382
@ INTR_SOF
Definition: dcd_ci_hs.c:96
@ INTR_SUSPEND
Definition: dcd_ci_hs.c:97
@ INTR_RESET
Definition: dcd_ci_hs.c:95
@ INTR_NAK
Definition: dcd_ci_hs.c:98
@ INTR_PORT_CHANGE
Definition: dcd_ci_hs.c:94
@ INTR_ERROR
Definition: dcd_ci_hs.c:93
@ INTR_USB
Definition: dcd_ci_hs.c:92
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_ci_hs.c:371
void dcd_dcache_clean(void const *addr, uint32_t data_size)
Definition: dcd_ci_hs.c:37
void dcd_connect(uint8_t rhport)
Definition: dcd_ci_hs.c:300
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
Definition: dcd_ci_hs.c:478
void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
Definition: dcd_ci_hs.c:285
void dcd_dcache_clean_invalidate(void const *addr, uint32_t data_size)
Definition: dcd_ci_hs.c:45
bool dcd_init(uint8_t rhport, const tusb_rhport_init_t *rh_init)
Definition: dcd_ci_hs.c:237
void dcd_int_enable(uint8_t rhport)
Definition: dcd_ci_hs.c:275
static void qtd_init(dcd_qtd_t *p_qtd, void *data_ptr, uint16_t total_bytes)
Definition: dcd_ci_hs.c:326
void dcd_remote_wakeup(uint8_t rhport)
Definition: dcd_ci_hs.c:294
void dcd_sof_enable(uint8_t rhport, bool en)
Definition: dcd_ci_hs.c:312
static void process_edpt_complete_isr(uint8_t rhport, uint8_t epnum, uint8_t dir)
Definition: dcd_ci_hs.c:563
uint8_t * data_ptr
Definition: dcd_nuc505.c:111
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
uint8_t bmAttributes
See: audio_clock_source_attribute_t.
Definition: audio.h:672
uint8_t bEndpointAddress
Definition: video.h:306
volatile uint32_t PORTSC1
Port Status & Control.
Definition: ci_hs_type.h:129
volatile uint32_t USBINTR
Interrupt Enable Register.
Definition: ci_hs_type.h:117
volatile uint32_t OTGSC
On-The-Go Status & control.
Definition: ci_hs_type.h:131
volatile uint32_t ENDPTCTRL[8]
Endpoint Control 0 - 7.
Definition: ci_hs_type.h:138
volatile uint32_t ENDPTNAKEN
Endpoint NAK Enable.
Definition: ci_hs_type.h:127
volatile uint32_t FRINDEX
USB Frame Index.
Definition: ci_hs_type.h:118
volatile uint32_t USBSTS
USB Status Register.
Definition: ci_hs_type.h:116
volatile uint32_t ENDPTLISTADDR
Endpoint List Address.
Definition: ci_hs_type.h:121
volatile uint32_t ENDPTCOMPLETE
Endpoint Complete.
Definition: ci_hs_type.h:137
volatile uint32_t ENDPTNAK
Endpoint NAK.
Definition: ci_hs_type.h:126
volatile uint32_t ENDPTFLUSH
Endpoint Flush.
Definition: ci_hs_type.h:135
volatile uint32_t ENDPTSETUPSTAT
Endpoint Setup Status.
Definition: ci_hs_type.h:133
volatile uint32_t ENDPTPRIME
Endpoint Prime.
Definition: ci_hs_type.h:134
volatile uint32_t USBCMD
USB Command Register.
Definition: ci_hs_type.h:115
volatile uint32_t USBMODE
USB Device Mode.
Definition: ci_hs_type.h:132
volatile uint32_t DEVICEADDR
Device Address.
Definition: ci_hs_type.h:120
dcd_qhd_t qhd[TUP_DCD_ENDPOINT_MAX][2] TU_ATTR_ALIGNED(64)
dcd_qtd_t qtd[TUP_DCD_ENDPOINT_MAX][2] TU_ATTR_ALIGNED(32)
volatile dcd_qtd_t qtd_overlay
Definition: dcd_ci_hs.c:149
tu_fifo_t * ff
Definition: dcd_ci_hs.c:158
uint32_t max_packet_size
Endpoint's wMaxPacketSize.
Definition: dcd_ci_hs.c:140
uint32_t int_on_setup
Interrupt on setup This bit is used on control type endpoints to indicate if USBINT is set in respons...
Definition: dcd_ci_hs.c:139
uint32_t zero_length_termination
This bit is used for non-isochronous endpoints to indicate when a zero-length packet is received to t...
Definition: dcd_ci_hs.c:142
uint32_t iso_mult
Definition: dcd_ci_hs.c:143
volatile tusb_control_request_t setup_request
Definition: dcd_ci_hs.c:152
volatile uint32_t qtd_addr
Definition: dcd_ci_hs.c:146
volatile uint32_t xact_err
Definition: dcd_ci_hs.c:109
uint16_t expected_bytes
Definition: dcd_ci_hs.c:128
volatile uint32_t active
Definition: dcd_ci_hs.c:113
volatile uint32_t halted
Definition: dcd_ci_hs.c:112
volatile uint32_t total_bytes
Definition: dcd_ci_hs.c:118
uint32_t iso_mult_override
This field can be used for transmit ISOs to override the MULT field in the dQH. This field must be ze...
Definition: dcd_ci_hs.c:115
uint32_t buffer[5]
buffer1 has frame_n for TODO Isochronous
Definition: dcd_ci_hs.c:122
uint32_t next
Next link pointer This field contains the physical memory address of the next dTD to be processed.
Definition: dcd_ci_hs.c:105
volatile uint32_t buffer_err
Definition: dcd_ci_hs.c:111
uint32_t int_on_complete
Definition: dcd_ci_hs.c:117
void * ptr_lin
linear part start pointer
Definition: tusb_fifo.h:128
uint16_t len_lin
linear length in item size
Definition: tusb_fifo.h:126
void * ptr_wrap
wrapped part start pointer
Definition: tusb_fifo.h:129
uint16_t len_wrap
wrapped length in item size
Definition: tusb_fifo.h:127
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align4k(uint32_t value)
Definition: tusb_common.h:172
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
static TU_ATTR_ALWAYS_INLINE uint32_t tu_offset4k(uint32_t value)
Definition: tusb_common.h:173
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
Get linear write info.
Definition: tusb_fifo.c:1057
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
Advance write pointer - intended to be used in combination with DMA. It is possible to fill the FIFO ...
Definition: tusb_fifo.c:948
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
Advance read pointer - intended to be used in combination with DMA. It is possible to read from the F...
Definition: tusb_fifo.c:969
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
Get read info.
Definition: tusb_fifo.c:989
static TU_ATTR_ALWAYS_INLINE uint16_t tu_fifo_depth(tu_fifo_t *f)
Definition: tusb_fifo.h:180
@ TUSB_DIR_IN
Definition: tusb_types.h:67
@ TUSB_DIR_OUT
Definition: tusb_types.h:66
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition: tusb_types.h:49
static TU_ATTR_ALWAYS_INLINE uint8_t tu_edpt_number(uint8_t addr)
Definition: tusb_types.h:507
@ XFER_RESULT_FAILED
Definition: tusb_types.h:238
@ XFER_RESULT_SUCCESS
Definition: tusb_types.h:237
@ XFER_RESULT_STALLED
Definition: tusb_types.h:239
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_ISOCHRONOUS
Definition: tusb_types.h:60
@ TUSB_XFER_BULK
Definition: tusb_types.h:61
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