tinyusb  0.4
Click here to lend your support to tinyusb donation and make a donation at pledgie.com
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
dcd_lpc43xx.c
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
39 #include "tusb_option.h"
40 
41 #if MODE_DEVICE_SUPPORTED && TUSB_CFG_MCU == MCU_LPC43XX
42 
43 #define _TINY_USB_SOURCE_FILE_
44 //--------------------------------------------------------------------+
45 // INCLUDE
46 //--------------------------------------------------------------------+
47 #include "common/common.h"
48 #include "hal/hal.h"
49 #include "osal/osal.h"
50 #include "common/timeout_timer.h"
51 
52 #include "dcd.h"
53 #include "usbd_dcd.h"
54 #include "dcd_lpc43xx.h"
55 
56 //--------------------------------------------------------------------+
57 // MACRO CONSTANT TYPEDEF
58 //--------------------------------------------------------------------+
59 #define DCD_QHD_MAX 12
60 #define DCD_QTD_MAX 12
61 #define DCD_QTD_PER_QHD_MAX 2 // maximum number of qtd that are linked into one queue head at a time
62 
63 #define QTD_NEXT_INVALID 0x01
64 
65 /*---------- ENDPTCTRL ----------*/
66 enum {
67  ENDPTCTRL_MASK_STALL = BIT_(0),
68  ENDPTCTRL_MASK_TOGGLE_INHIBIT = BIT_(5),
69  ENDPTCTRL_MASK_TOGGLE_RESET = BIT_(6),
70  ENDPTCTRL_MASK_ENABLE = BIT_(7)
71 };
72 
73 /*---------- USBCMD ----------*/
74 enum {
75  USBCMD_MASK_RUN_STOP = BIT_(0),
76  USBCMD_MASK_RESET = BIT_(1),
77  USBCMD_MASK_SETUP_TRIPWIRE = BIT_(13),
78  USBCMD_MASK_ADD_QTD_TRIPWIRE = BIT_(14)
79 };
80 // Interrupt Threshold bit 23:16
81 
82 /*---------- USBSTS, USBINTR ----------*/
83 enum {
84  INT_MASK_USB = BIT_(0),
85  INT_MASK_ERROR = BIT_(1),
86  INT_MASK_PORT_CHANGE = BIT_(2),
87  INT_MASK_RESET = BIT_(6),
88  INT_MASK_SOF = BIT_(7),
89  INT_MASK_SUSPEND = BIT_(8),
90  INT_MASK_NAK = BIT_(16)
91 };
92 
93 //------------- PORTSC -------------//
94 enum {
95  PORTSC_CURRENT_CONNECT_STATUS_MASK = BIT_(0),
96  PORTSC_FORCE_PORT_RESUME_MASK = BIT_(6),
97  PORTSC_SUSPEND_MASK = BIT_(7)
98 
99 };
100 
101 typedef struct {
102  // Word 0: Next QTD Pointer
103  uint32_t next;
104 
105  // Word 1: qTQ Token
106  uint32_t : 3 ;
107  volatile uint32_t xact_err : 1 ;
108  uint32_t : 1 ;
109  volatile uint32_t buffer_err : 1 ;
110  volatile uint32_t halted : 1 ;
111  volatile uint32_t active : 1 ;
112  uint32_t : 2 ;
113  uint32_t iso_mult_override : 2 ;
114  uint32_t : 3 ;
115  uint32_t int_on_complete : 1 ;
116  volatile uint32_t total_bytes : 15 ;
117  uint32_t : 0 ;
118 
119  // 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
120  uint32_t buffer[5];
121 
122  //------------- DCD Area -------------//
123  uint16_t expected_bytes;
124  uint8_t used;
125  uint8_t reserved;
126 } dcd_qtd_t;
127 
128 STATIC_ASSERT( sizeof(dcd_qtd_t) == 32, "size is not correct");
129 
130 typedef struct ATTR_ALIGNED(64) {
131  // Word 0: Capabilities and Characteristics
132  uint32_t : 15 ;
133  uint32_t int_on_setup : 1 ;
134  uint32_t max_package_size : 11 ;
135  uint32_t : 2 ;
136  uint32_t zero_length_termination : 1 ;
137  uint32_t iso_mult : 2 ;
138  uint32_t : 0 ;
139 
140  // Word 1: Current qTD Pointer
141  volatile uint32_t qtd_addr;
142 
143  // Word 2-9: Transfer Overlay
144  volatile dcd_qtd_t qtd_overlay;
145 
146  // Word 10-11: Setup request (control OUT only)
147  volatile tusb_control_request_t setup_request;
148 
149  //--------------------------------------------------------------------+
152  //--------------------------------------------------------------------+
153  uint8_t class_code; // Class code that endpoint belongs to
154  uint8_t list_qtd_idx[DCD_QTD_PER_QHD_MAX];
155 
156  uint8_t reserved[15-DCD_QTD_PER_QHD_MAX];
157 } dcd_qhd_t;
158 
159 STATIC_ASSERT( sizeof(dcd_qhd_t) == 64, "size is not correct");
160 
161 //--------------------------------------------------------------------+
162 // INTERNAL OBJECT & FUNCTION DECLARATION
163 //--------------------------------------------------------------------+
164 typedef struct {
165  dcd_qhd_t qhd[DCD_QHD_MAX];
166  dcd_qtd_t qtd[DCD_QTD_MAX] ATTR_ALIGNED(32);
167 
168 }dcd_data_t;
169 
170 #if (TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE)
171 TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(2048) dcd_data_t dcd_data0;
172 #endif
173 
174 #if (TUSB_CFG_CONTROLLER_1_MODE & TUSB_MODE_DEVICE)
175 TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(2048) dcd_data_t dcd_data1;
176 #endif
177 
178 static LPC_USB0_Type * const LPC_USB[2] = { LPC_USB0, ((LPC_USB0_Type*) LPC_USB1_BASE) };
179 static dcd_data_t* const dcd_data_ptr[2] =
180 {
181  #if (TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE)
182  &dcd_data0,
183  #else
184  NULL,
185  #endif
186 
187  #if (TUSB_CFG_CONTROLLER_1_MODE & TUSB_MODE_DEVICE)
188  &dcd_data1
189  #else
190  NULL
191  #endif
192 };
193 
194 //--------------------------------------------------------------------+
195 // CONTROLLER API
196 //--------------------------------------------------------------------+
197 void dcd_controller_connect(uint8_t coreid)
198 {
199  LPC_USB[coreid]->USBCMD_D |= BIT_(0);
200 }
201 
202 void dcd_controller_set_address(uint8_t coreid, uint8_t dev_addr)
203 {
204  LPC_USB[coreid]->DEVICEADDR = (dev_addr << 25) | BIT_(24);
205 }
206 
207 void dcd_controller_set_configuration(uint8_t coreid)
208 {
209 
210 }
211 
213 static void bus_reset(uint8_t coreid)
214 {
215  LPC_USB0_Type* const lpc_usb = LPC_USB[coreid];
216 
217  // The reset value for all endpoint types is the control endpoint. If one endpoint
218  //direction is enabled and the paired endpoint of opposite direction is disabled, then the
219  //endpoint type of the unused direction must bechanged from the control type to any other
220  //type (e.g. bulk). Leaving an unconfigured endpoint control will cause undefined behavior
221  //for the data PID tracking on the active endpoint.
222  lpc_usb->ENDPTCTRL1 = lpc_usb->ENDPTCTRL2 = lpc_usb->ENDPTCTRL3 =
223  (TUSB_XFER_BULK << 2) | (TUSB_XFER_BULK << 18);
224 
225  // USB1 only has 3 non-control endpoints
226  if ( coreid == 0)
227  {
228  lpc_usb->ENDPTCTRL4 = lpc_usb->ENDPTCTRL5 = (TUSB_XFER_BULK << 2) | (TUSB_XFER_BULK << 18);
229  }
230 
231  //------------- Clear All Registers -------------//
232  lpc_usb->ENDPTNAK = lpc_usb->ENDPTNAK;
233  lpc_usb->ENDPTNAKEN = 0;
234  lpc_usb->USBSTS_D = lpc_usb->USBSTS_D;
235  lpc_usb->ENDPTSETUPSTAT = lpc_usb->ENDPTSETUPSTAT;
236  lpc_usb->ENDPTCOMPLETE = lpc_usb->ENDPTCOMPLETE;
237 
238  while (lpc_usb->ENDPTPRIME);
239  lpc_usb->ENDPTFLUSH = 0xFFFFFFFF;
240  while (lpc_usb->ENDPTFLUSH);
241 
242  // read reset bit in portsc
243 
244  //------------- Queue Head & Queue TD -------------//
245  dcd_data_t* p_dcd = dcd_data_ptr[coreid];
246 
247  memclr_(p_dcd, sizeof(dcd_data_t));
248 
249  //------------- Set up Control Endpoints (0 OUT, 1 IN) -------------//
250  p_dcd->qhd[0].zero_length_termination = p_dcd->qhd[1].zero_length_termination = 1;
251  p_dcd->qhd[0].max_package_size = p_dcd->qhd[1].max_package_size = TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE;
252  p_dcd->qhd[0].qtd_overlay.next = p_dcd->qhd[1].qtd_overlay.next = QTD_NEXT_INVALID;
253 
254  p_dcd->qhd[0].int_on_setup = 1; // OUT only
255 
256 }
257 
258 static void lpc43xx_controller_init(uint8_t coreid)
259 {
260  LPC_USB0_Type* const lpc_usb = LPC_USB[coreid];
261  dcd_data_t* p_dcd = dcd_data_ptr[coreid];
262 
263  memclr_(p_dcd, sizeof(dcd_data_t));
264 
265  lpc_usb->ENDPOINTLISTADDR = (uint32_t) p_dcd->qhd; // Endpoint List Address has to be 2K alignment
266  lpc_usb->USBSTS_D = lpc_usb->USBSTS_D;
267  lpc_usb->USBINTR_D = INT_MASK_USB | INT_MASK_ERROR | INT_MASK_PORT_CHANGE | INT_MASK_RESET | INT_MASK_SUSPEND; // | INT_MASK_SOF;
268 
269  lpc_usb->USBCMD_D &= ~0x00FF0000; // Interrupt Threshold Interval = 0
270  lpc_usb->USBCMD_D |= BIT_(0); // connect
271 }
272 
273 tusb_error_t dcd_init(void)
274 {
275  #if (TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE)
276  lpc43xx_controller_init(0);
277  #endif
278 
279  #if (TUSB_CFG_CONTROLLER_1_MODE & TUSB_MODE_DEVICE)
280  lpc43xx_controller_init(1);
281  #endif
282 
283  return TUSB_ERROR_NONE;
284 }
285 
286 //--------------------------------------------------------------------+
287 // PIPE HELPER
288 //--------------------------------------------------------------------+
289 #if 0
290 static inline uint8_t edpt_pos2phy(uint8_t pos) ATTR_CONST ATTR_ALWAYS_INLINE;
291 static inline uint8_t edpt_pos2phy(uint8_t pos)
292 { // 0-5 --> OUT, 16-21 IN
293  return (pos < DCD_QHD_MAX/2) ? (2*pos) : (2*(pos-16)+1);
294 }
295 #endif
296 
297 static inline uint8_t edpt_phy2pos(uint8_t physical_endpoint) ATTR_CONST ATTR_ALWAYS_INLINE;
298 static inline uint8_t edpt_phy2pos(uint8_t physical_endpoint)
299 {
300  return physical_endpoint/2 + ( (physical_endpoint%2) ? 16 : 0);
301 }
302 
303 static inline uint8_t edpt_addr2phy(uint8_t endpoint_addr) ATTR_CONST ATTR_ALWAYS_INLINE;
304 static inline uint8_t edpt_addr2phy(uint8_t endpoint_addr)
305 {
306  return 2*(endpoint_addr & 0x0F) + ((endpoint_addr & TUSB_DIR_DEV_TO_HOST_MASK) ? 1 : 0);
307 }
308 
309 static inline uint8_t edpt_phy2log(uint8_t physical_endpoint) ATTR_CONST ATTR_ALWAYS_INLINE;
310 static inline uint8_t edpt_phy2log(uint8_t physical_endpoint)
311 {
312  return physical_endpoint/2;
313 }
314 
315 static void qtd_init(dcd_qtd_t* p_qtd, void * data_ptr, uint16_t total_bytes)
316 {
317  memclr_(p_qtd, sizeof(dcd_qtd_t));
318 
319  p_qtd->used = 1;
320 
321  p_qtd->next = QTD_NEXT_INVALID;
322  p_qtd->active = 1;
323  p_qtd->total_bytes = p_qtd->expected_bytes = total_bytes;
324 
325  if (data_ptr != NULL)
326  {
327  p_qtd->buffer[0] = (uint32_t) data_ptr;
328  for(uint8_t i=1; i<5; i++)
329  {
330  p_qtd->buffer[i] |= align4k( p_qtd->buffer[i-1] ) + 4096;
331  }
332  }
333 }
334 
335 // retval 0: invalid
336 static inline uint8_t qtd_find_free(uint8_t coreid) ATTR_PURE ATTR_ALWAYS_INLINE;
337 static inline uint8_t qtd_find_free(uint8_t coreid)
338 {
339  for(uint8_t i=2; i<DCD_QTD_MAX; i++)
340  { // exclude control's qtd
341  if ( dcd_data_ptr[coreid]->qtd[i].used == 0) return i;
342  }
343 
344  return 0;
345 }
346 
347 //--------------------------------------------------------------------+
348 // CONTROL PIPE API
349 //--------------------------------------------------------------------+
350 void dcd_pipe_control_stall(uint8_t coreid)
351 {
352  LPC_USB[coreid]->ENDPTCTRL0 |= (ENDPTCTRL_MASK_STALL << 16); // stall Control IN TODO stall control OUT as well
353 }
354 
355 // control transfer does not need to use qtd find function
356 tusb_error_t dcd_pipe_control_xfer(uint8_t coreid, tusb_direction_t dir, uint8_t * p_buffer, uint16_t length, bool int_on_complete)
357 {
358  LPC_USB0_Type* const lpc_usb = LPC_USB[coreid];
359  dcd_data_t* const p_dcd = dcd_data_ptr[coreid];
360 
361  // determine Endpoint where Data & Status phase occurred (IN or OUT)
362  uint8_t const ep_data = (dir == TUSB_DIR_DEV_TO_HOST) ? 1 : 0;
363  uint8_t const ep_status = 1 - ep_data;
364 
365  ASSERT_FALSE(p_dcd->qhd[0].qtd_overlay.active || p_dcd->qhd[1].qtd_overlay.active, TUSB_ERROR_FAILED);
366 
367  //------------- Data Phase -------------//
368  if (length > 0)
369  {
370  dcd_qtd_t* p_qtd_data = &p_dcd->qtd[0];
371  qtd_init(p_qtd_data, p_buffer, length);
372  p_dcd->qhd[ep_data].qtd_overlay.next = (uint32_t) p_qtd_data;
373 
374  lpc_usb->ENDPTPRIME = BIT_(edpt_phy2pos(ep_data));
375  }
376 
377  //------------- Status Phase -------------//
378  dcd_qtd_t* p_qtd_status = &p_dcd->qtd[1];
379  qtd_init(p_qtd_status, NULL, 0); // zero length xfer
380  p_qtd_status->int_on_complete = int_on_complete ? 1 : 0;
381 
382  p_dcd->qhd[ep_status].qtd_overlay.next = (uint32_t) p_qtd_status;
383 
384  LPC_USB0->ENDPTPRIME = BIT_(edpt_phy2pos(ep_status)); // | (length>0 ? BIT_(edpt_phy2pos(ep_data)) : 0 );
385 
386  return TUSB_ERROR_NONE;
387 }
388 
389 //--------------------------------------------------------------------+
390 // BULK/INTERRUPT/ISOCHRONOUS PIPE API
391 //--------------------------------------------------------------------+
392 static inline volatile uint32_t * get_reg_control_addr(uint8_t coreid, uint8_t physical_endpoint) ATTR_PURE ATTR_ALWAYS_INLINE;
393 static inline volatile uint32_t * get_reg_control_addr(uint8_t coreid, uint8_t physical_endpoint)
394 {
395  return &(LPC_USB[coreid]->ENDPTCTRL0) + edpt_phy2log(physical_endpoint);
396 }
397 
398 tusb_error_t dcd_pipe_stall(endpoint_handle_t edpt_hdl)
399 {
400  volatile uint32_t * reg_control = get_reg_control_addr(edpt_hdl.coreid, edpt_hdl.index);
401 
402  (*reg_control) |= ENDPTCTRL_MASK_STALL << (edpt_hdl.index & 0x01 ? 16 : 0);
403 
404  return TUSB_ERROR_NONE;
405 }
406 
407 tusb_error_t dcd_pipe_clear_stall(uint8_t coreid, uint8_t edpt_addr)
408 {
409  volatile uint32_t * reg_control = get_reg_control_addr(coreid, edpt_addr2phy(edpt_addr));
410 
411  // data toggle also need to be reset
412  (*reg_control) |= ENDPTCTRL_MASK_TOGGLE_RESET << ((edpt_addr & TUSB_DIR_DEV_TO_HOST_MASK) ? 16 : 0);
413  (*reg_control) &= ~(ENDPTCTRL_MASK_STALL << ((edpt_addr & TUSB_DIR_DEV_TO_HOST_MASK) ? 16 : 0));
414 
415  return TUSB_ERROR_NONE;
416 }
417 
418 endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const * p_endpoint_desc, uint8_t class_code)
419 {
420  // TODO USB1 only has 4 non-control enpoint (USB0 has 5)
421  endpoint_handle_t const null_handle = { 0 };
422 
423  if (p_endpoint_desc->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
424  return null_handle; // TODO not support ISO yet
425 
426  tusb_direction_t dir = (p_endpoint_desc->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK) ? TUSB_DIR_DEV_TO_HOST : TUSB_DIR_HOST_TO_DEV;
427 
428  //------------- Prepare Queue Head -------------//
429  uint8_t ep_idx = edpt_addr2phy(p_endpoint_desc->bEndpointAddress);
430  dcd_qhd_t * p_qhd = &dcd_data_ptr[coreid]->qhd[ep_idx];
431 
432  memclr_(p_qhd, sizeof(dcd_qhd_t));
433 
434  p_qhd->class_code = class_code;
435  p_qhd->zero_length_termination = 1;
436  p_qhd->max_package_size = p_endpoint_desc->wMaxPacketSize.size;
437  p_qhd->qtd_overlay.next = QTD_NEXT_INVALID;
438 
439  //------------- Endpoint Control Register -------------//
440  volatile uint32_t * reg_control = get_reg_control_addr(coreid, ep_idx);
441 
442  ASSERT_FALSE( (*reg_control) & (ENDPTCTRL_MASK_ENABLE << (dir ? 16 : 0)), null_handle ); // endpoint must not be already enabled
443  (*reg_control) |= ((p_endpoint_desc->bmAttributes.xfer << 2) | ENDPTCTRL_MASK_ENABLE | ENDPTCTRL_MASK_TOGGLE_RESET) << (dir ? 16 : 0);
444 
445  return (endpoint_handle_t)
446  {
447  .coreid = coreid,
448  .index = ep_idx,
449  .class_code = class_code
450  };
451 }
452 
453 bool dcd_pipe_is_busy(endpoint_handle_t edpt_hdl)
454 {
455  dcd_qhd_t* p_qhd = &dcd_data_ptr[edpt_hdl.coreid]->qhd[edpt_hdl.index];
456 
457  // LPC_USB0->ENDPTSTAT & endpoint_phy2pos(edpt_hdl.index)
458  return !p_qhd->qtd_overlay.halted && p_qhd->qtd_overlay.active;
459 }
460 
461 // add only, controller virtually cannot know
462 static tusb_error_t pipe_add_xfer(endpoint_handle_t edpt_hdl, void * buffer, uint16_t total_bytes, bool int_on_complete)
463 {
464  uint8_t qtd_idx = qtd_find_free(edpt_hdl.coreid);
465  ASSERT(qtd_idx != 0, TUSB_ERROR_DCD_NOT_ENOUGH_QTD);
466 
467  dcd_data_t* p_dcd = dcd_data_ptr[edpt_hdl.coreid];
468  dcd_qhd_t * p_qhd = &p_dcd->qhd[edpt_hdl.index];
469  dcd_qtd_t * p_qtd = &p_dcd->qtd[qtd_idx];
470 
471  //------------- Find free slot in qhd's array list -------------//
472  uint8_t free_slot;
473  for(free_slot=0; free_slot < DCD_QTD_PER_QHD_MAX; free_slot++)
474  {
475  if ( p_qhd->list_qtd_idx[free_slot] == 0 ) break; // found free slot
476  }
477  ASSERT(free_slot < DCD_QTD_PER_QHD_MAX, TUSB_ERROR_DCD_NOT_ENOUGH_QTD);
478 
479  p_qhd->list_qtd_idx[free_slot] = qtd_idx; // add new qtd to qhd's array list
480 
481  //------------- Prepare qtd -------------//
482  qtd_init(p_qtd, buffer, total_bytes);
483  p_qtd->int_on_complete = int_on_complete;
484 
485  if ( free_slot > 0 ) p_dcd->qtd[ p_qhd->list_qtd_idx[free_slot-1] ].next = (uint32_t) p_qtd;
486 
487  return TUSB_ERROR_NONE;
488 }
489 
490 tusb_error_t dcd_pipe_queue_xfer(endpoint_handle_t edpt_hdl, uint8_t * buffer, uint16_t total_bytes)
491 {
492  return pipe_add_xfer( edpt_hdl, buffer, total_bytes, false);
493 }
494 
495 tusb_error_t dcd_pipe_xfer(endpoint_handle_t edpt_hdl, uint8_t * buffer, uint16_t total_bytes, bool int_on_complete)
496 {
497  ASSERT_STATUS ( pipe_add_xfer(edpt_hdl, buffer, total_bytes, int_on_complete) );
498 
499  dcd_qhd_t* p_qhd = &dcd_data_ptr[edpt_hdl.coreid]->qhd[ edpt_hdl.index ];
500  dcd_qtd_t* p_qtd = &dcd_data_ptr[edpt_hdl.coreid]->qtd[ p_qhd->list_qtd_idx[0] ];
501 
502  p_qhd->qtd_overlay.next = (uint32_t) p_qtd; // attach head QTD to QHD start transferring
503 
504  LPC_USB[edpt_hdl.coreid]->ENDPTPRIME = BIT_( edpt_phy2pos(edpt_hdl.index) ) ;
505 
506  return TUSB_ERROR_NONE;
507 }
508 
509 //------------- Device Controller Driver's Interrupt Handler -------------//
510 void xfer_complete_isr(uint8_t coreid, uint32_t reg_complete)
511 {
512  for(uint8_t ep_idx = 2; ep_idx < DCD_QHD_MAX; ep_idx++)
513  {
514  if ( BIT_TEST_(reg_complete, edpt_phy2pos(ep_idx)) )
515  { // 23.10.12.3 Failed QTD also get ENDPTCOMPLETE set
516  dcd_qhd_t * p_qhd = &dcd_data_ptr[coreid]->qhd[ep_idx];
517 
518  endpoint_handle_t edpt_hdl =
519  {
520  .coreid = coreid,
521  .index = ep_idx,
522  .class_code = p_qhd->class_code
523  };
524 
525  // retire all QTDs in array list, up to 1st still-active QTD
526  while( p_qhd->list_qtd_idx[0] != 0 )
527  {
528  dcd_qtd_t * p_qtd = &dcd_data_ptr[coreid]->qtd[ p_qhd->list_qtd_idx[0] ];
529 
530  if (p_qtd->active) break; // stop immediately if found still-active QTD and shift array list
531 
532  //------------- Free QTD and shift array list -------------//
533  p_qtd->used = 0; // free QTD
534  memmove(p_qhd->list_qtd_idx, p_qhd->list_qtd_idx+1, DCD_QTD_PER_QHD_MAX-1);
535  p_qhd->list_qtd_idx[DCD_QTD_PER_QHD_MAX-1]=0;
536 
537  if (p_qtd->int_on_complete)
538  {
539  tusb_event_t event = ( p_qtd->xact_err || p_qtd->halted || p_qtd->buffer_err ) ? TUSB_EVENT_XFER_ERROR : TUSB_EVENT_XFER_COMPLETE;
540  usbd_xfer_isr(edpt_hdl, event, p_qtd->expected_bytes - p_qtd->total_bytes); // only number of bytes in the IOC qtd
541  }
542  }
543  }
544  }
545 }
546 
547 void dcd_isr(uint8_t coreid)
548 {
549  LPC_USB0_Type* const lpc_usb = LPC_USB[coreid];
550 
551  uint32_t const int_enable = lpc_usb->USBINTR_D;
552  uint32_t const int_status = lpc_usb->USBSTS_D & int_enable;
553  lpc_usb->USBSTS_D = int_status; // Acknowledge handled interrupt
554 
555  if (int_status == 0) return;// disabled interrupt sources
556 
557  if (int_status & INT_MASK_RESET)
558  {
559  bus_reset(coreid);
560  usbd_dcd_bus_event_isr(0, USBD_BUS_EVENT_RESET);
561  }
562 
563  if (int_status & INT_MASK_SUSPEND)
564  {
565  if (lpc_usb->PORTSC1_D & PORTSC_SUSPEND_MASK)
566  { // Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration.
567  if ((lpc_usb->DEVICEADDR >> 25) & 0x0f)
568  {
569  usbd_dcd_bus_event_isr(0, USBD_BUS_EVENT_SUSPENDED);
570  }
571  }
572  }
573 
574  // TODO disconnection does not generate interrupt !!!!!!
575 // if (int_status & INT_MASK_PORT_CHANGE)
576 // {
577 // if ( !(lpc_usb->PORTSC1_D & PORTSC_CURRENT_CONNECT_STATUS_MASK) )
578 // {
579 // usbd_dcd_bus_event_isr(0, USBD_BUS_EVENT_UNPLUGGED);
580 // }
581 // }
582 
583  if (int_status & INT_MASK_USB)
584  {
585  uint32_t const edpt_complete = lpc_usb->ENDPTCOMPLETE;
586  lpc_usb->ENDPTCOMPLETE = edpt_complete; // acknowledge
587 
588  dcd_data_t* const p_dcd = dcd_data_ptr[coreid];
589 
590  //------------- Set up Received -------------//
591  if (lpc_usb->ENDPTSETUPSTAT)
592  { // 23.10.10.2 Operational model for setup transfers
593  tusb_control_request_t control_request = p_dcd->qhd[0].setup_request;
594  lpc_usb->ENDPTSETUPSTAT = lpc_usb->ENDPTSETUPSTAT;// acknowledge
595 
596  usbd_setup_received_isr(coreid, &control_request);
597  }
598  //------------- Control Request Completed -------------//
599  else if ( edpt_complete & 0x03 )
600  {
601  for(uint8_t ep_idx = 0; ep_idx < 2; ep_idx++)
602  {
603  if ( BIT_TEST_(edpt_complete, edpt_phy2pos(ep_idx)) )
604  {
605  // TODO use the actual QTD instead of the qhd's overlay to get expected bytes for actual byte xferred
606  dcd_qtd_t volatile * const p_qtd = &p_dcd->qhd[ep_idx].qtd_overlay;
607 
608  if ( p_qtd->int_on_complete )
609  {
610  endpoint_handle_t edpt_hdl =
611  {
612  .coreid = coreid,
613  .index = 0,
614  .class_code = 0
615  };
616  tusb_event_t event = ( p_qtd->xact_err || p_qtd->halted || p_qtd->buffer_err ) ? TUSB_EVENT_XFER_ERROR : TUSB_EVENT_XFER_COMPLETE;
617 
618  usbd_xfer_isr(edpt_hdl, event, 0); // TODO xferred bytes for control xfer is not needed yet !!!!
619  }
620  }
621  }
622  }
623 
624  //------------- Transfer Complete -------------//
625  if ( edpt_complete & ~(0x03UL) )
626  {
627  xfer_complete_isr(coreid, edpt_complete);
628  }
629  }
630 
631  if (int_status & INT_MASK_SOF) {}
632  if (int_status & INT_MASK_NAK) {}
633  if (int_status & INT_MASK_ERROR) ASSERT(false, VOID_RETURN);
634 }
635 
636 //--------------------------------------------------------------------+
637 // HELPER
638 //--------------------------------------------------------------------+
639 #endif
#define BIT_(n)
n-th Bit
Definition: binary.h:54
#define BIT_TEST_(x, n)
check if n-th bit of x is 1
Definition: binary.h:57
#define ATTR_PURE
Many functions have no effects except the return value and their return value depends only on the par...
Definition: compiler_gcc.h:96
struct tusb_descriptor_endpoint_t::@8 bmAttributes
This field describes the endpoint's attributes when it is configured using the bConfigurationValue. Bits 1..0: Transfer Type - 00 = Control - 01 = Isochronous - 10 = Bulk - 11 = Interrupt If not an isochronous endpoint, bits 5..2 are reserved and must be set to zero. If isochronous, they are defined as follows: Bits 3..2: Synchronization Type - 00 = No Synchronization - 01 = Asynchronous - 10 = Adaptive - 11 = Synchronous Bits 5..4: Usage Type - 00 = Data endpoint - 01 = Feedback endpoint - 10 = Implicit feedback Data endpoint - 11 = Reserved Refer to Chapter 5 of USB 2.0 specification for more information. All other bits are reserved and must be reset to zero. Reserved bits must be ignored by the host.
#define TUSB_CFG_ATTR_USBRAM
USB Standard Endpoint Descriptor (section 9.6.1 table 9-13)
tusb_error_t
Error Code returned.
Definition: tusb_errors.h:100
#define ATTR_ALIGNED(Bytes)
This attribute specifies a minimum alignment for the variable or structure field, measured in bytes...
Definition: compiler_gcc.h:72
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
Definition: compiler_gcc.h:89
#define ATTR_CONST
Many functions do not examine any values except their arguments, and have no effects except the retur...
Definition: compiler_gcc.h:100
#define TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE
Max packet size of Cotnrol Endpoint, default is 64.
uint16_t size
Maximum packet size this endpoint is capable of sending or receiving when this configuration is selec...
uint8_t bEndpointAddress
The address of the endpoint on the USB device described by this descriptor. The address is encoded as...