Open FFBoard
Open source force feedback firmware
dcd_nuc505.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020 Peter Lawrence
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/*
28 Theory of operation:
29
30 The NUC505 USBD peripheral has twelve "EP"s, where each is simplex, in addition
31 to dedicated support for the control endpoint (EP0). The non-user endpoints
32 are referred to as "user" EPs in this code, and follow the datasheet
33 nomenclature of EPA through EPL.
34*/
35
36#include "tusb_option.h"
37
38#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_NUC505)
39
40#include "device/dcd.h"
41#include "NUC505Series.h"
42
43/*
44 * The DMA functionality of the USBD peripheral does not appear to succeed with
45 * transfer lengths that are longer (> 64 bytes) and are not a multiple of 4.
46 * Keep disabled for now.
47 */
48#define USE_DMA 0
49
50/* rather important info unfortunately not provided by device include files */
51#define USBD_BUF_SIZE 2048 /* how much USB buffer space there is */
52#define USBD_MAX_DMA_LEN 0x1000 /* max bytes that can be DMAed at one time */
53
55{
69};
70
71static const uint8_t epcfg_eptype_table[] =
72{
73 [TUSB_XFER_CONTROL] = 0, /* won't happen, since control EPs have dedicated registers */
74 [TUSB_XFER_ISOCHRONOUS] = 3 << USBD_EPCFG_EPTYPE_Pos,
75 [TUSB_XFER_BULK] = 1 << USBD_EPCFG_EPTYPE_Pos,
76 [TUSB_XFER_INTERRUPT] = 2 << USBD_EPCFG_EPTYPE_Pos,
77};
78
79static const uint8_t eprspctl_eptype_table[] =
80{
81 [TUSB_XFER_CONTROL] = 0, /* won't happen, since control EPs have dedicated registers */
82 [TUSB_XFER_ISOCHRONOUS] = 2 << USBD_EPRSPCTL_MODE_Pos, /* Fly Mode */
83 [TUSB_XFER_BULK] = 0 << USBD_EPRSPCTL_MODE_Pos, /* Auto-Validate Mode */
84 [TUSB_XFER_INTERRUPT] = 1 << USBD_EPRSPCTL_MODE_Pos, /* Manual-Validate Mode */
85};
86
87/* set by dcd_set_address() */
88static volatile uint8_t assigned_address;
89
90/* reset by bus_reset(), this is used by dcd_edpt_open() to assign USBD peripheral buffer addresses */
91static uint32_t bufseg_addr;
92
93/* RAM table needed to track ongoing transfers performed by dcd_edpt_xfer(), dcd_userEP_in_xfer(), and the ISR */
94static struct xfer_ctl_t
95{
96 uint8_t *data_ptr; /* data_ptr tracks where to next copy data to (for OUT) or from (for IN) */
97 // tu_fifo_t* ff; // TODO support dcd_edpt_xfer_fifo API
98 union {
99 uint16_t in_remaining_bytes; /* for IN endpoints, we track how many bytes are left to transfer */
100 uint16_t out_bytes_so_far; /* but for OUT endpoints, we track how many bytes we've transferred so far */
101 };
102 uint16_t max_packet_size; /* needed since device driver only finds out this at runtime */
103 uint16_t total_bytes; /* quantity needed to pass as argument to dcd_event_xfer_complete() (for IN endpoints) */
104 uint8_t ep_addr;
107
108/* in addition to xfer_table, additional bespoke bookkeeping is maintained for control EP0 IN */
109static struct
110{
111 uint8_t *data_ptr;
113 uint16_t total_bytes;
115
116static volatile struct xfer_ctl_t *current_dma_xfer;
117
118
119/*
120 local helper functions
121*/
122
123static void usb_attach(void)
124{
125 USBD->PHYCTL |= USBD_PHYCTL_DPPUEN_Msk;
126}
127
128static void usb_detach(void)
129{
130 USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk;
131}
132
133static void usb_control_send_zlp(void)
134{
135 USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
136 USBD->CEPCTL = 0; /* clear NAKCLR bit */
137 USBD->CEPINTEN = USBD_CEPINTEN_STSDONEIEN_Msk;
138}
139
140/* map 8-bit ep_addr into peripheral endpoint index (PERIPH_EPA...) */
141static USBD_EP_T *ep_entry(uint8_t ep_addr, bool add)
142{
143 USBD_EP_T *ep;
144 enum ep_enum ep_index;
145 struct xfer_ctl_t *xfer;
146
147 for (ep_index = PERIPH_EPA, xfer = &xfer_table[PERIPH_EPA], ep = USBD->EP;
148 ep_index < PERIPH_MAX_EP;
149 ep_index++, xfer++, ep++)
150 {
151 if (add)
152 {
153 /* take first peripheral endpoint that is unused */
154 if (0 == (ep->EPCFG & USBD_EPCFG_EPEN_Msk)) return ep;
155 }
156 else
157 {
158 /* find a peripheral endpoint that matches ep_addr */
159 if (xfer->ep_addr == ep_addr) return ep;
160 }
161 }
162
163 return NULL;
164}
165
166/* perform a non-control IN endpoint transfer; this is called by the ISR */
167static void dcd_userEP_in_xfer(struct xfer_ctl_t *xfer, USBD_EP_T *ep)
168{
169 uint16_t const bytes_now = tu_min16(xfer->in_remaining_bytes, xfer->max_packet_size);
170
171 /* precompute what amount of data will be left */
172 xfer->in_remaining_bytes -= bytes_now;
173
174 /*
175 if there will be no more data to send, we replace the BUFEMPTYIF EP interrupt with TXPKIF;
176 that way, we alert TinyUSB as soon as this last packet has been sent
177 */
178 if (0 == xfer->in_remaining_bytes)
179 {
180 ep->EPINTSTS = USBD_EPINTSTS_TXPKIF_Msk;
181 ep->EPINTEN = USBD_EPINTEN_TXPKIEN_Msk;
182 }
183
184 /* provided buffers are thankfully 32-bit aligned, allowing most data to be transferred as 32-bit */
185#if 0 // TODO support dcd_edpt_xfer_fifo API
186 if (xfer->ff)
187 {
188 tu_fifo_read_n_const_addr_full_words(xfer->ff, (void *) (&ep->EPDAT_BYTE), bytes_now);
189 }
190 else
191#endif
192 {
193 uint16_t countdown = bytes_now;
194 while (countdown > 3)
195 {
196 uint32_t u32;
197 memcpy(&u32, xfer->data_ptr, 4);
198
199 ep->EPDAT = u32;
200 xfer->data_ptr += 4; countdown -= 4;
201 }
202
203 while (countdown--) ep->EPDAT_BYTE = *xfer->data_ptr++;
204 }
205
206 /* for short packets, we must nudge the peripheral to say 'that's all folks' */
207 if (bytes_now != xfer->max_packet_size) ep->EPRSPCTL = USBD_EPRSPCTL_SHORTTXEN_Msk;
208}
209
210/* called by dcd_init() as well as by the ISR during a USB bus reset */
211static void bus_reset(void)
212{
213 for (enum ep_enum ep_index = PERIPH_EPA; ep_index < PERIPH_MAX_EP; ep_index++)
214 {
215 USBD->EP[ep_index].EPCFG = 0;
216 xfer_table[ep_index].dma_requested = false;
217 }
218
219 USBD->DMACNT = 0;
220 USBD->DMACTL = USBD_DMACTL_DMARST_Msk;
221 USBD->DMACTL = 0;
222
223 /* allocate the default EP0 endpoints */
224
225 USBD->CEPBUFSTART = 0;
226 USBD->CEPBUFEND = 0 + CFG_TUD_ENDPOINT0_SIZE - 1;
227
228 /* USB RAM beyond what we've allocated above is available to the user */
229 bufseg_addr = CFG_TUD_ENDPOINT0_SIZE;
230
231 /* Reset USB device address */
232 USBD->FADDR = 0;
233
234 current_dma_xfer = NULL;
235}
236
237#if USE_DMA
238/* this must only be called by the ISR; it does its best to share the single DMA engine across all user EPs (IN and OUT) */
239static void service_dma(void)
240{
242 return;
243
244 enum ep_enum ep_index;
245 struct xfer_ctl_t *xfer;
246 USBD_EP_T *ep;
247
248 for (ep_index = PERIPH_EPA, xfer = &xfer_table[PERIPH_EPA], ep = &USBD->EP[PERIPH_EPA]; ep_index < PERIPH_MAX_EP; ep_index++, xfer++, ep++)
249 {
250 uint16_t const available_bytes = ep->EPDATCNT & USBD_EPDATCNT_DATCNT_Msk;
251
252 if (!xfer->dma_requested || !available_bytes)
253 continue;
254
255 /*
256 instruct DMA to copy the data from the PC to the previously provided buffer
257 when the bus interrupt DMADONEIEN subsequently fires, the transfer will have finished
258 */
259 USBD->DMACTL = xfer->ep_addr & USBD_DMACTL_EPNUM_Msk;
260 USBD->DMAADDR = (uint32_t)xfer->data_ptr;
261 USBD->DMACNT = available_bytes;
262 USBD->BUSINTSTS = USBD_BUSINTSTS_DMADONEIF_Msk;
263 xfer->out_bytes_so_far += available_bytes;
265 USBD->DMACTL |= USBD_DMACTL_DMAEN_Msk;
266
267 return;
268 }
269}
270#endif
271
272/* centralized location for USBD interrupt enable bit masks */
273static const uint32_t enabled_irqs = USBD_GINTEN_USBIEN_Msk | \
274 USBD_GINTEN_EPAIEN_Msk | USBD_GINTEN_EPBIEN_Msk | USBD_GINTEN_EPCIEN_Msk | USBD_GINTEN_EPDIEN_Msk | USBD_GINTEN_EPEIEN_Msk | USBD_GINTEN_EPFIEN_Msk | \
275 USBD_GINTEN_EPGIEN_Msk | USBD_GINTEN_EPHIEN_Msk | USBD_GINTEN_EPIIEN_Msk | USBD_GINTEN_EPJIEN_Msk | USBD_GINTEN_EPKIEN_Msk | USBD_GINTEN_EPLIEN_Msk | \
276 USBD_GINTEN_CEPIEN_Msk;
277
278/*
279 NUC505 TinyUSB API driver implementation
280*/
281
282bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
283 (void) rhport;
284 (void) rh_init;
285
286 /* configure interrupts in their initial state; BUSINTEN and CEPINTEN will be subsequently and dynamically re-written as needed */
287 USBD->GINTEN = enabled_irqs;
288 USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_VBUSDETIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
289 USBD->CEPINTEN = 0;
290
291 bus_reset();
292
293 usb_attach();
294
295 return true;
296}
297
298void dcd_int_enable(uint8_t rhport)
299{
300 (void) rhport;
301 NVIC_EnableIRQ(USBD_IRQn);
302}
303
304void dcd_int_disable(uint8_t rhport)
305{
306 (void) rhport;
307 NVIC_DisableIRQ(USBD_IRQn);
308}
309
310void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
311{
312 (void) rhport;
313 usb_control_send_zlp(); /* SET_ADDRESS is the one exception where TinyUSB doesn't use dcd_edpt_xfer() to generate a ZLP */
315}
316
317void dcd_remote_wakeup(uint8_t rhport)
318{
319 (void) rhport;
320 USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
321}
322
323bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
324{
325 (void) rhport;
326
327 USBD_EP_T *ep = ep_entry(p_endpoint_desc->bEndpointAddress, true);
328 TU_ASSERT(ep);
329
330 /* mine the data for the information we need */
331 int const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
332 int const size = tu_edpt_packet_size(p_endpoint_desc);
333 tusb_xfer_type_t const type = p_endpoint_desc->bmAttributes.xfer;
334 struct xfer_ctl_t *xfer = &xfer_table[ep - USBD->EP];
335
336 /* allocate buffer from USB RAM */
337 ep->EPBUFSTART = bufseg_addr;
338 bufseg_addr += size;
339 ep->EPBUFEND = bufseg_addr - 1;
340 TU_ASSERT(bufseg_addr <= USBD_BUF_SIZE);
341
342 ep->EPMPS = size;
343
344 ep->EPRSPCTL = USB_EP_RSPCTL_FLUSH | eprspctl_eptype_table[type];
345
346 /* construct USB Configuration Register value and then write it */
347 uint32_t cfg = (uint32_t)tu_edpt_number(p_endpoint_desc->bEndpointAddress) << USBD_EPCFG_EPNUM_Pos;
348 if (TUSB_DIR_IN == dir)
349 cfg |= USBD_EPCFG_EPDIR_Msk;
350 cfg |= epcfg_eptype_table[type] | USBD_EPCFG_EPEN_Msk;
351 ep->EPCFG = cfg;
352
353 /* make a note of the endpoint particulars */
354 xfer->max_packet_size = size;
355 xfer->ep_addr = p_endpoint_desc->bEndpointAddress;
356
357 return true;
358}
359
360void dcd_edpt_close_all (uint8_t rhport)
361{
362 (void) rhport;
363 // TODO implement dcd_edpt_close_all()
364}
365
366bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
367{
368 (void) rhport;
369
370 if (0x80 == ep_addr) /* control EP0 IN */
371 {
372 if (total_bytes)
373 {
374 USBD->CEPCTL = USBD_CEPCTL_FLUSH_Msk;
375 ctrl_in_xfer.data_ptr = buffer;
376 ctrl_in_xfer.in_remaining_bytes = total_bytes;
377 ctrl_in_xfer.total_bytes = total_bytes;
378 USBD->CEPINTSTS = USBD_CEPINTSTS_INTKIF_Msk;
379 USBD->CEPINTEN = USBD_CEPINTEN_INTKIEN_Msk;
380 }
381 else
382 {
384 }
385 }
386 else if (0x00 == ep_addr) /* control EP0 OUT */
387 {
388 if (total_bytes)
389 {
390 /* if TinyUSB is asking for EP0 OUT data, it is almost certainly already in the buffer */
391 while (total_bytes < USBD->CEPRXCNT);
392 for (int count = 0; count < total_bytes; count++)
393 *buffer++ = USBD->CEPDAT_BYTE;
394
396 }
397 }
398 else
399 {
400 /* mine the data for the information we need */
402 USBD_EP_T *ep = ep_entry(ep_addr, false);
403 TU_ASSERT(ep);
404 struct xfer_ctl_t *xfer = &xfer_table[ep - USBD->EP];
405
406 /* store away the information we'll needing now and later */
407 xfer->data_ptr = buffer;
408 // xfer->ff = NULL; // TODO support dcd_edpt_xfer_fifo API
409 xfer->in_remaining_bytes = total_bytes;
410 xfer->total_bytes = total_bytes;
411
412 if (TUSB_DIR_IN == dir)
413 {
414 ep->EPINTEN = USBD_EPINTEN_BUFEMPTYIEN_Msk;
415 }
416 else
417 {
418 xfer->out_bytes_so_far = 0;
419 ep->EPINTEN = USBD_EPINTEN_RXPKIEN_Msk;
420 }
421 }
422
423 return true;
424}
425
426#if 0 // TODO support dcd_edpt_xfer_fifo API
427bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes)
428{
429 (void) rhport;
430
431 TU_ASSERT(0x80 != ep_addr && 0x00 != ep_addr); // Must not be used for control stuff
432
433 /* mine the data for the information we need */
435 USBD_EP_T *ep = ep_entry(ep_addr, false);
436 struct xfer_ctl_t *xfer = &xfer_table[ep - USBD->EP];
437
438 /* store away the information we'll needing now and later */
439 xfer->data_ptr = NULL; // Indicates a FIFO shall be used
440 xfer->ff = ff;
441 xfer->in_remaining_bytes = total_bytes;
442 xfer->total_bytes = total_bytes;
443
444 if (TUSB_DIR_IN == dir)
445 {
446 ep->EPINTEN = USBD_EPINTEN_BUFEMPTYIEN_Msk;
447 }
448 else
449 {
450 xfer->out_bytes_so_far = 0;
451 ep->EPINTEN = USBD_EPINTEN_RXPKIEN_Msk;
452 }
453
454 return true;
455}
456#endif
457
458void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
459{
460 (void) rhport;
461
463 {
464 USBD_EP_T *ep = ep_entry(ep_addr, false);
465 TU_ASSERT(ep, );
466 ep->EPRSPCTL = (ep->EPRSPCTL & 0xf7) | USBD_EPRSPCTL_HALT_Msk;
467 }
468 else
469 {
470 USBD->CEPCTL = USBD_CEPCTL_STALLEN_Msk;
471 }
472}
473
474void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
475{
476 (void) rhport;
477
479 {
480 USBD_EP_T *ep = ep_entry(ep_addr, false);
481 TU_ASSERT(ep, );
482 ep->EPRSPCTL = USBD_EPRSPCTL_TOGGLE_Msk;
483 }
484}
485
486void dcd_int_handler(uint8_t rhport)
487{
488 (void) rhport;
489
490 uint32_t status = USBD->GINTSTS;
491
492 /* USB interrupt */
493 if (status & USBD_GINTSTS_USBIF_Msk)
494 {
495 uint32_t bus_state = USBD->BUSINTSTS;
496
497 if (bus_state & USBD_BUSINTSTS_SOFIF_Msk)
498 {
499 /* Start-Of-Frame event */
500 dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
501 }
502
503 if (bus_state & USBD_BUSINTSTS_RSTIF_Msk)
504 {
505 bus_reset();
506
507 USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
508 USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
509 USBD->CEPINTSTS = 0x1ffc;
510
511 tusb_speed_t speed = (USBD->OPER & USBD_OPER_CURSPD_Msk) ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL;
512 dcd_event_bus_reset(0, speed, true);
513 }
514
515 if (bus_state & USBD_BUSINTSTS_RESUMEIF_Msk)
516 {
517 USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
518 dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
519 }
520
521 if (bus_state & USBD_BUSINTSTS_SUSPENDIF_Msk)
522 {
523 USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
524 dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
525 }
526
527 if (bus_state & USBD_BUSINTSTS_HISPDIF_Msk)
528 {
529 USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
530 }
531
532 if (bus_state & USBD_BUSINTSTS_DMADONEIF_Msk)
533 {
534#if USE_DMA
536 {
538
539 uint16_t available_bytes = USBD->DMACNT & USBD_DMACNT_DMACNT_Msk;
540
541 /* if the most recent DMA finishes the transfer, alert TinyUSB; otherwise, the next RXPKIF/INTKIF endpoint interrupt will prompt the next DMA */
542 if ( (current_dma_xfer->total_bytes == current_dma_xfer->out_bytes_so_far) || (available_bytes < current_dma_xfer->max_packet_size) )
543 {
545 }
546
547 current_dma_xfer = NULL;
548 service_dma();
549 }
550#endif
551 }
552
553 if (bus_state & USBD_BUSINTSTS_VBUSDETIF_Msk)
554 {
555 if (USBD->PHYCTL & USBD_PHYCTL_VBUSDET_Msk)
556 {
557 /* USB connect */
558 USBD->PHYCTL |= USBD_PHYCTL_PHYEN_Msk | USBD_PHYCTL_DPPUEN_Msk;
559 }
560 else
561 {
562 /* USB disconnect */
563 USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk;
564 }
565 }
566
567 USBD->BUSINTSTS = bus_state & (USBD_BUSINTSTS_SOFIF_Msk | USBD_BUSINTSTS_RSTIF_Msk | USBD_BUSINTSTS_RESUMEIF_Msk | USBD_BUSINTSTS_SUSPENDIF_Msk | USBD_BUSINTSTS_HISPDIF_Msk | USBD_BUSINTSTS_DMADONEIF_Msk | USBD_BUSINTSTS_PHYCLKVLDIF_Msk | USBD_BUSINTSTS_VBUSDETIF_Msk);
568 }
569
570 if (status & USBD_GINTSTS_CEPIF_Msk)
571 {
572 uint32_t cep_state = USBD->CEPINTSTS & USBD->CEPINTEN;
573
574 if (cep_state & USBD_CEPINTSTS_SETUPPKIF_Msk)
575 {
576 /* get SETUP packet from USB buffer */
577 uint8_t setup_packet[8];
578 setup_packet[0] = (uint8_t)(USBD->SETUP1_0 >> 0);
579 setup_packet[1] = (uint8_t)(USBD->SETUP1_0 >> 8);
580 setup_packet[2] = (uint8_t)(USBD->SETUP3_2 >> 0);
581 setup_packet[3] = (uint8_t)(USBD->SETUP3_2 >> 8);
582 setup_packet[4] = (uint8_t)(USBD->SETUP5_4 >> 0);
583 setup_packet[5] = (uint8_t)(USBD->SETUP5_4 >> 8);
584 setup_packet[6] = (uint8_t)(USBD->SETUP7_6 >> 0);
585 setup_packet[7] = (uint8_t)(USBD->SETUP7_6 >> 8);
586 dcd_event_setup_received(0, setup_packet, true);
587 }
588 else if (cep_state & USBD_CEPINTSTS_INTKIF_Msk)
589 {
590 USBD->CEPINTSTS = USBD_CEPINTSTS_TXPKIF_Msk;
591
592 if (!(cep_state & USBD_CEPINTSTS_STSDONEIF_Msk))
593 {
594 USBD->CEPINTEN = USBD_CEPINTEN_TXPKIEN_Msk;
595 uint16_t bytes_now = tu_min16(ctrl_in_xfer.in_remaining_bytes, CFG_TUD_ENDPOINT0_SIZE);
596 for (int count = 0; count < bytes_now; count++)
597 USBD->CEPDAT_BYTE = *ctrl_in_xfer.data_ptr++;
598 ctrl_in_xfer.in_remaining_bytes -= bytes_now;
599 USBD_START_CEP_IN(bytes_now);
600 }
601 else
602 {
603 USBD->CEPINTEN = USBD_CEPINTEN_TXPKIEN_Msk | USBD_CEPINTEN_STSDONEIEN_Msk;
604 }
605 }
606 else if (cep_state & USBD_CEPINTSTS_TXPKIF_Msk)
607 {
608 USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
609 USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
610
611 /* alert TinyUSB that the EP0 IN transfer has finished */
612 if ( (0 == ctrl_in_xfer.in_remaining_bytes) || (0 == ctrl_in_xfer.total_bytes) )
613 dcd_event_xfer_complete(0, 0x80, ctrl_in_xfer.total_bytes, XFER_RESULT_SUCCESS, true);
614
615 if (ctrl_in_xfer.in_remaining_bytes)
616 {
617 USBD->CEPINTSTS = USBD_CEPINTSTS_INTKIF_Msk;
618 USBD->CEPINTEN = USBD_CEPINTEN_INTKIEN_Msk;
619 }
620 else
621 {
622 /* TinyUSB does its own fragmentation and ZLP for EP0; a transfer of zero means a ZLP */
623 if (0 == ctrl_in_xfer.total_bytes) USBD->CEPCTL = USBD_CEPCTL_ZEROLEN_Msk;
624
625 USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
626 USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk | USBD_CEPINTEN_STSDONEIEN_Msk;
627 }
628 }
629 else if (cep_state & USBD_CEPINTSTS_STSDONEIF_Msk)
630 {
631 /* given ACK from host has happened, we can now set the address (if not already done) */
632 if((USBD->FADDR != assigned_address) && (USBD->FADDR == 0))
633 {
634 USBD->FADDR = assigned_address;
635
636 for (enum ep_enum ep_index = PERIPH_EPA; ep_index < PERIPH_MAX_EP; ep_index++)
637 {
638 if (USBD->EP[ep_index].EPCFG & USBD_EPCFG_EPEN_Msk) USBD->EP[ep_index].EPRSPCTL = USBD_EPRSPCTL_TOGGLE_Msk;
639 }
640 }
641
642 USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
643 }
644
645 USBD->CEPINTSTS = cep_state;
646
647 return;
648 }
649
650 if (status & (USBD_GINTSTS_EPAIF_Msk | USBD_GINTSTS_EPBIF_Msk | USBD_GINTSTS_EPCIF_Msk | USBD_GINTSTS_EPDIF_Msk | USBD_GINTSTS_EPEIF_Msk | USBD_GINTSTS_EPFIF_Msk | USBD_GINTSTS_EPGIF_Msk | USBD_GINTSTS_EPHIF_Msk | USBD_GINTSTS_EPIIF_Msk | USBD_GINTSTS_EPJIF_Msk | USBD_GINTSTS_EPKIF_Msk | USBD_GINTSTS_EPLIF_Msk))
651 {
652 /* service PERIPH_EPA through PERIPH_EPL */
653 enum ep_enum ep_index;
654 uint32_t mask;
655 struct xfer_ctl_t *xfer;
656 USBD_EP_T *ep;
657 for (ep_index = PERIPH_EPA, mask = USBD_GINTSTS_EPAIF_Msk, xfer = &xfer_table[PERIPH_EPA], ep = &USBD->EP[PERIPH_EPA]; ep_index < PERIPH_MAX_EP; ep_index++, mask <<= 1, xfer++, ep++)
658 {
659 if(status & mask)
660 {
661 uint8_t const ep_addr = xfer->ep_addr;
662 bool const out_ep = !(ep_addr & TUSB_DIR_IN_MASK);
663 uint32_t ep_state = ep->EPINTSTS & ep->EPINTEN;
664
665 if (out_ep)
666 {
667#if USE_DMA
668 xfer->dma_requested = true;
669 service_dma();
670#else
671 uint16_t const available_bytes = ep->EPDATCNT & USBD_EPDATCNT_DATCNT_Msk;
672 /* copy the data from the PC to the previously provided buffer */
673#if 0 // TODO support dcd_edpt_xfer_fifo API
674 if (xfer->ff)
675 {
676 tu_fifo_write_n_const_addr_full_words(xfer->ff, (const void *) &ep->EPDAT_BYTE, tu_min16(available_bytes, xfer->total_bytes - xfer->out_bytes_so_far));
677 }
678 else
679#endif
680 {
681 for (int count = 0; (count < available_bytes) && (xfer->out_bytes_so_far < xfer->total_bytes); count++, xfer->out_bytes_so_far++)
682 {
683 *xfer->data_ptr++ = ep->EPDAT_BYTE;
684 }
685 }
686
687 /* when the transfer is finished, alert TinyUSB; otherwise, continue accepting more data */
688 if ( (xfer->total_bytes == xfer->out_bytes_so_far) || (available_bytes < xfer->max_packet_size) )
689 {
690 dcd_event_xfer_complete(0, ep_addr, xfer->out_bytes_so_far, XFER_RESULT_SUCCESS, true);
691 }
692#endif
693
694 }
695 else if (ep_state & USBD_EPINTSTS_BUFEMPTYIF_Msk)
696 {
697 /* send any remaining data */
699 }
700 else if (ep_state & USBD_EPINTSTS_TXPKIF_Msk)
701 {
702 /* alert TinyUSB that we've finished */
704 ep->EPINTEN = 0;
705 }
706
707 ep->EPINTSTS = ep_state;
708 }
709 }
710 }
711}
712
713void dcd_disconnect(uint8_t rhport)
714{
715 (void) rhport;
716 usb_detach();
717}
718
719void dcd_connect(uint8_t rhport)
720{
721 (void) rhport;
722 usb_attach();
723}
724
725void dcd_sof_enable(uint8_t rhport, bool en)
726{
727 (void) rhport;
728 (void) en;
729
730 // TODO implement later
731}
732
733#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
xfer_td_t xfer[EP_CBI_COUNT+1][2]
Definition: dcd_nrf5x.c:119
ep_enum
Definition: dcd_nuc120.c:56
static USBD_EP_T * ep_entry(uint8_t ep_addr, bool add)
Definition: dcd_nuc505.c:141
bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t *ff, uint16_t total_bytes)
Definition: dcd_nuc505.c:427
static struct xfer_ctl_t xfer_table[PERIPH_MAX_EP]
@ PERIPH_EPI
Definition: dcd_nuc505.c:64
@ PERIPH_EPC
Definition: dcd_nuc505.c:58
@ PERIPH_EPA
Definition: dcd_nuc505.c:56
@ PERIPH_EPH
Definition: dcd_nuc505.c:63
@ PERIPH_EPB
Definition: dcd_nuc505.c:57
@ PERIPH_EPL
Definition: dcd_nuc505.c:67
@ PERIPH_EPK
Definition: dcd_nuc505.c:66
@ PERIPH_EPE
Definition: dcd_nuc505.c:60
@ PERIPH_EPF
Definition: dcd_nuc505.c:61
@ PERIPH_EPJ
Definition: dcd_nuc505.c:65
@ PERIPH_EPG
Definition: dcd_nuc505.c:62
@ PERIPH_EPD
Definition: dcd_nuc505.c:59
@ PERIPH_MAX_EP
Definition: dcd_nuc505.c:68
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_nuc505.c:458
static void usb_detach(void)
Definition: dcd_nuc505.c:128
static void service_dma(void)
Definition: dcd_nuc505.c:239
static void bus_reset(void)
Definition: dcd_nuc505.c:211
static void usb_control_send_zlp(void)
Definition: dcd_nuc505.c:133
void dcd_int_handler(uint8_t rhport)
Definition: dcd_nuc505.c:486
void dcd_disconnect(uint8_t rhport)
Definition: dcd_nuc505.c:713
uint8_t * data_ptr
Definition: dcd_nuc505.c:111
void dcd_edpt_close_all(uint8_t rhport)
Definition: dcd_nuc505.c:360
void dcd_int_disable(uint8_t rhport)
Definition: dcd_nuc505.c:304
static volatile struct xfer_ctl_t * current_dma_xfer
Definition: dcd_nuc505.c:116
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *p_endpoint_desc)
Definition: dcd_nuc505.c:323
static void usb_attach(void)
Definition: dcd_nuc505.c:123
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
Definition: dcd_nuc505.c:474
uint16_t total_bytes
Definition: dcd_nuc505.c:113
static const uint8_t epcfg_eptype_table[]
Definition: dcd_nuc505.c:71
void dcd_connect(uint8_t rhport)
Definition: dcd_nuc505.c:719
static void dcd_userEP_in_xfer(struct xfer_ctl_t *xfer, USBD_EP_T *ep)
Definition: dcd_nuc505.c:167
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
Definition: dcd_nuc505.c:366
static const uint32_t enabled_irqs
Definition: dcd_nuc505.c:273
static uint32_t bufseg_addr
Definition: dcd_nuc505.c:91
static volatile uint8_t assigned_address
Definition: dcd_nuc505.c:88
void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
Definition: dcd_nuc505.c:310
static const uint8_t eprspctl_eptype_table[]
Definition: dcd_nuc505.c:79
bool dcd_init(uint8_t rhport, const tusb_rhport_init_t *rh_init)
Definition: dcd_nuc505.c:282
static struct @333 ctrl_in_xfer
uint16_t in_remaining_bytes
Definition: dcd_nuc505.c:112
void dcd_int_enable(uint8_t rhport)
Definition: dcd_nuc505.c:298
void dcd_remote_wakeup(uint8_t rhport)
Definition: dcd_nuc505.c:317
void dcd_sof_enable(uint8_t rhport, bool en)
Definition: dcd_nuc505.c:725
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
uint32_t u32
uint8_t const * buffer
Definition: midi_device.h:100
static void * memcpy(void *dst, const void *src, size_t n)
Definition: ringbuffer.c:8
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
uint8_t * data_ptr
Definition: dcd_nuc120.c:78
uint16_t total_bytes
Definition: dcd_nuc120.c:85
uint16_t out_bytes_so_far
Definition: dcd_nuc120.c:82
uint16_t in_remaining_bytes
Definition: dcd_nuc120.c:81
tu_fifo_t * ff
uint8_t ep_addr
Definition: dcd_da146xx.c:221
uint16_t max_packet_size
Definition: dcd_da146xx.c:216
bool dma_requested
Definition: dcd_nuc505.c:105
static TU_ATTR_ALWAYS_INLINE uint16_t tu_min16(uint16_t x, uint16_t y)
Definition: tusb_common.h:155
uint16_t tu_fifo_write_n_const_addr_full_words(tu_fifo_t *f, const void *data, uint16_t n)
This function will write n elements into the array index specified by the write pointer and increment...
Definition: tusb_fifo.c:882
uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t *f, void *buffer, uint16_t n)
This function will read n elements from the array index specified by the read pointer and increment t...
Definition: tusb_fifo.c:753
tusb_dir_t
Definition: tusb_types.h:65
@ 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
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_type_t
defined base on USB Specs Endpoint's bmAttributes
Definition: tusb_types.h:58
@ TUSB_XFER_CONTROL
Definition: tusb_types.h:59
@ TUSB_XFER_ISOCHRONOUS
Definition: tusb_types.h:60
@ TUSB_XFER_INTERRUPT
Definition: tusb_types.h:62
@ 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