RT-AICHIP-sample
usbcore.c
[詳解]
1 /*----------------------------------------------------------------------------
2  * U S B - K e r n e l
3  *----------------------------------------------------------------------------
4  * Name: usbcore.c
5  * Purpose: USB Core Module
6  * Version: V1.20
7  *----------------------------------------------------------------------------
8  * This software is supplied "AS IS" without any warranties, express,
9  * implied or statutory, including but not limited to the implied
10  * warranties of fitness for purpose, satisfactory quality and
11  * noninfringement. Keil extends you a royalty-free right to reproduce
12  * and distribute executable files created using this software for use
13  * on NXP Semiconductors LPC microcontroller devices only. Nothing else
14  * gives you the right to use this software.
15  *
16  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17  *----------------------------------------------------------------------------
18  * History:
19  * V1.20 Added vendor specific requests
20  * Changed string descriptor handling
21  * Reworked Endpoint0
22  * V1.00 Initial Version
23  *----------------------------------------------------------------------------*/
24 #include "type.h"
25 
26 #include "usb.h"
27 #include "usbcfg.h"
28 #include "usbhw.h"
29 #include "usbcore.h"
30 #include "usbdesc.h"
31 #include "usbuser.h"
32 
33 #if (USB_CLASS)
34 
35 #if (USB_AUDIO)
36 #include "audio.h"
37 #include "adcuser.h"
38 #endif
39 
40 #if (USB_HID)
41 #include "hid.h"
42 #include "hiduser.h"
43 #endif
44 
45 #if (USB_MSC)
46 #include "msc.h"
47 #include "mscuser.h"
48 extern MSC_CSW CSW;
49 #endif
50 
51 #if (USB_CDC)
52 #include "cdc.h"
53 #include "cdcuser.h"
54 #endif
55 
56 #endif
57 
58 #if (USB_VENDOR)
59 #include "vendor.h"
60 #endif
61 
67 uint32_t USB_EndPointStall; /* EP must stay stalled */
70 
72 
73 
75 
76 USB_SETUP_PACKET SetupPacket;
77 
78 
79 /*
80  * Reset USB Core
81  * Parameters: None
82  * Return Value: None
83  */
84 
85 void USB_ResetCore (void) {
86 
90  USB_EndPointMask = 0x00010001;
91  USB_EndPointHalt = 0x00000000;
92  USB_EndPointStall = 0x00000000;
93 }
94 
95 
96 /*
97  * USB Request - Setup Stage
98  * Parameters: None (global SetupPacket)
99  * Return Value: None
100  */
101 
102 void USB_SetupStage (void) {
103  USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
104 }
105 
106 
107 /*
108  * USB Request - Data In Stage
109  * Parameters: None (global EP0Data)
110  * Return Value: None
111  */
112 
113 void USB_DataInStage (void) {
114  uint32_t cnt;
115 
116  if (EP0Data.Count > USB_MAX_PACKET0) {
117  cnt = USB_MAX_PACKET0;
118  } else {
119  cnt = EP0Data.Count;
120  }
121  cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
122  EP0Data.pData += cnt;
123  EP0Data.Count -= cnt;
124 }
125 
126 
127 /*
128  * USB Request - Data Out Stage
129  * Parameters: None (global EP0Data)
130  * Return Value: None
131  */
132 
133 void USB_DataOutStage (void) {
134  uint32_t cnt;
135 
136  cnt = USB_ReadEP(0x00, EP0Data.pData);
137  EP0Data.pData += cnt;
138  EP0Data.Count -= cnt;
139 }
140 
141 
142 /*
143  * USB Request - Status In Stage
144  * Parameters: None
145  * Return Value: None
146  */
147 
148 void USB_StatusInStage (void) {
149  USB_WriteEP(0x80, NULL, 0);
150 }
151 
152 
153 /*
154  * USB Request - Status Out Stage
155  * Parameters: None
156  * Return Value: None
157  */
158 
159 void USB_StatusOutStage (void) {
160  USB_ReadEP(0x00, EP0Buf);
161 }
162 
163 
164 /*
165  * Get Status USB Request
166  * Parameters: None (global SetupPacket)
167  * Return Value: TRUE - Success, FALSE - Error
168  */
169 
170 __inline uint32_t USB_ReqGetStatus (void) {
171  uint32_t n, m;
172 
173  switch (SetupPacket.bmRequestType.BM.Recipient) {
174  case REQUEST_TO_DEVICE:
175  EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
176  break;
178  if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
179  *((uint16_t __attribute__((packed)) *)EP0Buf) = 0;
180  EP0Data.pData = EP0Buf;
181  } else {
182  return (FALSE);
183  }
184  break;
185  case REQUEST_TO_ENDPOINT:
186  n = SetupPacket.wIndex.WB.L & 0x8F;
187  m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
188  if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
189  *((uint16_t __attribute__((packed)) *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
190  EP0Data.pData = EP0Buf;
191  } else {
192  return (FALSE);
193  }
194  break;
195  default:
196  return (FALSE);
197  }
198  return (TRUE);
199 }
200 
201 
202 /*
203  * Set/Clear Feature USB Request
204  * Parameters: sc: 0 - Clear, 1 - Set
205  * (global SetupPacket)
206  * Return Value: TRUE - Success, FALSE - Error
207  */
208 
210  uint32_t n, m;
211 
212  switch (SetupPacket.bmRequestType.BM.Recipient) {
213  case REQUEST_TO_DEVICE:
214  if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
215  if (sc) {
218  } else {
221  }
222  } else {
223  return (FALSE);
224  }
225  break;
227  return (FALSE);
228  case REQUEST_TO_ENDPOINT:
229  n = SetupPacket.wIndex.WB.L & 0x8F;
230  m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
231  if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
232  if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
233  if (sc) {
234  USB_SetStallEP(n);
235  USB_EndPointHalt |= m;
236  } else {
237  if ((USB_EndPointStall & m) != 0) {
238  return (TRUE);
239  }
240  USB_ClrStallEP(n);
241 #if (USB_MSC)
242  if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
243  /* Compliance Test: rewrite CSW after unstall */
244  if (CSW.dSignature == MSC_CSW_Signature) {
245  USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
246  }
247  }
248 #endif
249  USB_EndPointHalt &= ~m;
250  }
251  } else {
252  return (FALSE);
253  }
254  } else {
255  return (FALSE);
256  }
257  break;
258  default:
259  return (FALSE);
260  }
261  return (TRUE);
262 }
263 
264 
265 /*
266  * Set Address USB Request
267  * Parameters: None (global SetupPacket)
268  * Return Value: TRUE - Success, FALSE - Error
269  */
270 
271 __inline uint32_t USB_ReqSetAddress (void) {
272 
273  switch (SetupPacket.bmRequestType.BM.Recipient) {
274  case REQUEST_TO_DEVICE:
275  USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
276  break;
277  default:
278  return (FALSE);
279  }
280  return (TRUE);
281 }
282 
283 
284 /*
285  * Get Descriptor USB Request
286  * Parameters: None (global SetupPacket)
287  * Return Value: TRUE - Success, FALSE - Error
288  */
289 
291  uint8_t *pD;
292  uint32_t len, n;
293 
294  switch (SetupPacket.bmRequestType.BM.Recipient) {
295  case REQUEST_TO_DEVICE:
296  switch (SetupPacket.wValue.WB.H) {
298  EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
299  len = USB_DEVICE_DESC_SIZE;
300  break;
303  for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
304  if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
305  pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
306  }
307  }
308  if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
309  return (FALSE);
310  }
311  EP0Data.pData = pD;
312  len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
313  break;
316  for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
317  if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
318  pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
319  }
320  }
321  if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
322  return (FALSE);
323  }
324  EP0Data.pData = pD;
325  len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
326  break;
327  default:
328  return (FALSE);
329  }
330  break;
332  switch (SetupPacket.wValue.WB.H) {
333 #if USB_HID
334  case HID_HID_DESCRIPTOR_TYPE:
335  if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
336  return (FALSE); /* Only Single HID Interface is supported */
337  }
338  EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
339  len = HID_DESC_SIZE;
340  break;
341  case HID_REPORT_DESCRIPTOR_TYPE:
342  if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
343  return (FALSE); /* Only Single HID Interface is supported */
344  }
345  EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
346  len = HID_ReportDescSize;
347  break;
348  case HID_PHYSICAL_DESCRIPTOR_TYPE:
349  return (FALSE); /* HID Physical Descriptor is not supported */
350 #endif
351  default:
352  return (FALSE);
353  }
354  break;
355  default:
356  return (FALSE);
357  }
358 
359  if (EP0Data.Count > len) {
360  EP0Data.Count = len;
361  }
362 
363  return (TRUE);
364 }
365 
366 
367 /*
368  * Get Configuration USB Request
369  * Parameters: None (global SetupPacket)
370  * Return Value: TRUE - Success, FALSE - Error
371  */
372 
374 
375  switch (SetupPacket.bmRequestType.BM.Recipient) {
376  case REQUEST_TO_DEVICE:
377  EP0Data.pData = &USB_Configuration;
378  break;
379  default:
380  return (FALSE);
381  }
382  return (TRUE);
383 }
384 
385 
386 /*
387  * Add a number of bytes to a pointer's address
388  * Harder than you might think. Some compilers say:
389  * Expected an lvalue -- Assignment expects its first operand to be
390  * an lvalue. Please note that a cast removes the lvaluedness of an
391  * expression.
392  *
393  * vpptr = void pointer to pointer
394  * n = number of bytes to add to pointer
395  * Call looks like: AddPtr((void **)&myPointer, 8);
396  */
397 
398 __inline void UsbAddPtr(void **vpptr, uint32_t n)
399 {
400  /* Declare a pointer to a pointer to a byte. Only a byte pointer
401  * can be incremented by a number of bytes. Other pointers will
402  * increment by a multiple of what they point to.
403  */
404  uint8_t **bpptr;
405 
406  /* Convert our void pointer to a pointer to a byte pointer to a pointer */
407  bpptr = (uint8_t **)vpptr;
408 
409  /* Add 'n' bytes to our pointer value */
410  (*bpptr) += n;
411 }
412 /*
413  * Set Configuration USB Request
414  * Parameters: None (global SetupPacket)
415  * Return Value: TRUE - Success, FALSE - Error
416  */
417 
419  USB_COMMON_DESCRIPTOR *pD;
420  uint32_t alt = 0;
421  uint32_t n, m;
422 
423  switch (SetupPacket.bmRequestType.BM.Recipient) {
424  case REQUEST_TO_DEVICE:
425 
426  if (SetupPacket.wValue.WB.L) {
427  pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
428  while (pD->bLength) {
429  switch (pD->bDescriptorType) {
431  if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
432  USB_Configuration = SetupPacket.wValue.WB.L;
433  USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
434  for (n = 0; n < USB_IF_NUM; n++) {
435  USB_AltSetting[n] = 0;
436  }
437  for (n = 1; n < 16; n++) {
438  if (USB_EndPointMask & (1 << n)) {
439  USB_DisableEP(n);
440  }
441  if (USB_EndPointMask & ((1 << 16) << n)) {
442  USB_DisableEP(n | 0x80);
443  }
444  }
445  USB_EndPointMask = 0x00010001;
446  USB_EndPointHalt = 0x00000000;
447  USB_EndPointStall= 0x00000000;
449  if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
451  } else {
453  }
454  } else {
455  UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
456  continue;
457  }
458  break;
460  alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
461  break;
463  if (alt == 0) {
464  n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
465  m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
466  USB_EndPointMask |= m;
467  USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
468  USB_EnableEP(n);
469  USB_ResetEP(n);
470  }
471  break;
472  }
473  UsbAddPtr((void **)&pD, pD->bLength);
474  }
475  }
476  else {
477  USB_Configuration = 0;
478  for (n = 1; n < 16; n++) {
479  if (USB_EndPointMask & (1 << n)) {
480  USB_DisableEP(n);
481  }
482  if (USB_EndPointMask & ((1 << 16) << n)) {
483  USB_DisableEP(n | 0x80);
484  }
485  }
486  USB_EndPointMask = 0x00010001;
487  USB_EndPointHalt = 0x00000000;
488  USB_EndPointStall = 0x00000000;
490  }
491 
492  if (USB_Configuration != SetupPacket.wValue.WB.L) {
493  return (FALSE);
494  }
495  break;
496  default:
497  return (FALSE);
498  }
499  return (TRUE);
500 }
501 
502 
503 /*
504  * Get Interface USB Request
505  * Parameters: None (global SetupPacket)
506  * Return Value: TRUE - Success, FALSE - Error
507  */
508 
509 __inline uint32_t USB_ReqGetInterface (void) {
510 
511  switch (SetupPacket.bmRequestType.BM.Recipient) {
513  if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
514  EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
515  } else {
516  return (FALSE);
517  }
518  break;
519  default:
520  return (FALSE);
521  }
522  return (TRUE);
523 }
524 
525 
526 /*
527  * Set Interface USB Request
528  * Parameters: None (global SetupPacket)
529  * Return Value: TRUE - Success, FALSE - Error
530  */
531 
532 __inline uint32_t USB_ReqSetInterface (void) {
533  USB_COMMON_DESCRIPTOR *pD;
534  uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
535  uint32_t n, m;
536  uint32_t set;
537 
538  switch (SetupPacket.bmRequestType.BM.Recipient) {
540  if (USB_Configuration == 0) return (FALSE);
541  set = FALSE;
542  pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
543  while (pD->bLength) {
544  switch (pD->bDescriptorType) {
546  if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
547  UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
548  continue;
549  }
550  break;
552  ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
553  alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
554  msk = 0;
555  if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
556  set = TRUE;
557  old = USB_AltSetting[ifn];
558  USB_AltSetting[ifn] = (uint8_t)alt;
559  }
560  break;
562  if (ifn == SetupPacket.wIndex.WB.L) {
563  n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
564  m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
565  if (alt == SetupPacket.wValue.WB.L) {
566  USB_EndPointMask |= m;
567  USB_EndPointHalt &= ~m;
568  USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
569  USB_EnableEP(n);
570  USB_ResetEP(n);
571  msk |= m;
572  }
573  else if ((alt == old) && ((msk & m) == 0)) {
574  USB_EndPointMask &= ~m;
575  USB_EndPointHalt &= ~m;
576  USB_DisableEP(n);
577  }
578  }
579  break;
580  }
581  UsbAddPtr((void **)&pD, pD->bLength);
582  }
583  break;
584  default:
585  return (FALSE);
586  }
587 
588  return (set);
589 }
590 
591 
592 /*
593  * USB Endpoint 0 Event Callback
594  * Parameters: event
595  * Return Value: none
596  */
597 
598 void USB_EndPoint0 (uint32_t event) {
599 
600  switch (event) {
601  case USB_EVT_SETUP:
602  USB_SetupStage();
603  USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
604  EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
605  switch (SetupPacket.bmRequestType.BM.Type) {
606 
607  case REQUEST_STANDARD:
608  switch (SetupPacket.bRequest) {
610  if (!USB_ReqGetStatus()) {
611  goto stall_i;
612  }
613  USB_DataInStage();
614  break;
615 
617  if (!USB_ReqSetClrFeature(0)) {
618  goto stall_i;
619  }
621 #if USB_FEATURE_EVENT
623 #endif
624  break;
625 
627  if (!USB_ReqSetClrFeature(1)) {
628  goto stall_i;
629  }
631 #if USB_FEATURE_EVENT
633 #endif
634  break;
635 
637  if (!USB_ReqSetAddress()) {
638  goto stall_i;
639  }
641  break;
642 
644  if (!USB_ReqGetDescriptor()) {
645  goto stall_i;
646  }
647  USB_DataInStage();
648  break;
649 
651 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
652  EP0Data.Count = 0;
653  break;
654 
656  if (!USB_ReqGetConfiguration()) {
657  goto stall_i;
658  }
659  USB_DataInStage();
660  break;
661 
663  if (!USB_ReqSetConfiguration()) {
664  goto stall_i;
665  }
667 #if USB_CONFIGURE_EVENT
669 #endif
670  break;
671 
673  if (!USB_ReqGetInterface()) {
674  goto stall_i;
675  }
676  USB_DataInStage();
677  break;
678 
680  if (!USB_ReqSetInterface()) {
681  goto stall_i;
682  }
684 #if USB_INTERFACE_EVENT
686 #endif
687  break;
688 
689  default:
690  goto stall_i;
691  }
692  break; /* end case REQUEST_STANDARD */
693 
694 #if USB_CLASS
695  case REQUEST_CLASS:
696  switch (SetupPacket.bmRequestType.BM.Recipient) {
697 
698  case REQUEST_TO_DEVICE:
699  goto stall_i; /* not supported */
700 
702 #if USB_HID
703  if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
704  switch (SetupPacket.bRequest) {
705  case HID_REQUEST_GET_REPORT:
706  if (HID_GetReport()) {
707  EP0Data.pData = EP0Buf; /* point to data to be sent */
708  USB_DataInStage(); /* send requested data */
709  goto setup_class_ok;
710  }
711  break;
712  case HID_REQUEST_SET_REPORT:
713  EP0Data.pData = EP0Buf; /* data to be received */
714  goto setup_class_ok;
715  case HID_REQUEST_GET_IDLE:
716  if (HID_GetIdle()) {
717  EP0Data.pData = EP0Buf; /* point to data to be sent */
718  USB_DataInStage(); /* send requested data */
719  goto setup_class_ok;
720  }
721  break;
722  case HID_REQUEST_SET_IDLE:
723  if (HID_SetIdle()) {
724  USB_StatusInStage(); /* send Acknowledge */
725  goto setup_class_ok;
726  }
727  break;
728  case HID_REQUEST_GET_PROTOCOL:
729  if (HID_GetProtocol()) {
730  EP0Data.pData = EP0Buf; /* point to data to be sent */
731  USB_DataInStage(); /* send requested data */
732  goto setup_class_ok;
733  }
734  break;
735  case HID_REQUEST_SET_PROTOCOL:
736  if (HID_SetProtocol()) {
737  USB_StatusInStage(); /* send Acknowledge */
738  goto setup_class_ok;
739  }
740  break;
741  }
742  }
743 #endif /* USB_HID */
744 #if USB_MSC
745  if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
746  switch (SetupPacket.bRequest) {
747  case MSC_REQUEST_RESET:
748  if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
749  (SetupPacket.wLength == 0)) {
750  if (MSC_Reset()) {
752  goto setup_class_ok;
753  }
754  }
755  break;
756  case MSC_REQUEST_GET_MAX_LUN:
757  if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
758  (SetupPacket.wLength == 1)) {
759  if (MSC_GetMaxLUN()) {
760  EP0Data.pData = EP0Buf;
761  USB_DataInStage();
762  goto setup_class_ok;
763  }
764  }
765  break;
766  }
767  }
768 #endif /* USB_MSC */
769 #if USB_AUDIO
770  if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
771  (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
772  (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
773  switch (SetupPacket.bRequest) {
774  case AUDIO_REQUEST_GET_CUR:
775  case AUDIO_REQUEST_GET_MIN:
776  case AUDIO_REQUEST_GET_MAX:
777  case AUDIO_REQUEST_GET_RES:
778  if (ADC_IF_GetRequest()) {
779  EP0Data.pData = EP0Buf; /* point to data to be sent */
780  USB_DataInStage(); /* send requested data */
781  goto setup_class_ok;
782  }
783  break;
784  case AUDIO_REQUEST_SET_CUR:
785 // case AUDIO_REQUEST_SET_MIN:
786 // case AUDIO_REQUEST_SET_MAX:
787 // case AUDIO_REQUEST_SET_RES:
788  EP0Data.pData = EP0Buf; /* data to be received */
789  goto setup_class_ok;
790  }
791  }
792 #endif /* USB_AUDIO */
793 #if USB_CDC
794  if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
795  (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
796  switch (SetupPacket.bRequest) {
798  EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
799  goto setup_class_ok;
802  EP0Data.pData = EP0Buf; /* point to data to be sent */
803  USB_DataInStage(); /* send requested data */
804  goto setup_class_ok;
805  }
806  break;
808  EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
809  goto setup_class_ok;
811  if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
812  EP0Data.pData = EP0Buf; /* point to data to be sent */
813  USB_DataInStage(); /* send requested data */
814  goto setup_class_ok;
815  }
816  break;
818  if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
819  USB_StatusInStage(); /* send Acknowledge */
820  goto setup_class_ok;
821  }
822  break;
823  case CDC_SET_LINE_CODING:
824  EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
825  goto setup_class_ok;
826  case CDC_GET_LINE_CODING:
827  if (CDC_GetLineCoding()) {
828  EP0Data.pData = EP0Buf; /* point to data to be sent */
829  USB_DataInStage(); /* send requested data */
830  goto setup_class_ok;
831  }
832  break;
834  if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
835  USB_StatusInStage(); /* send Acknowledge */
836  goto setup_class_ok;
837  }
838  break;
839  case CDC_SEND_BREAK:
840  if (CDC_SendBreak(SetupPacket.wValue.W)) {
841  USB_StatusInStage(); /* send Acknowledge */
842  goto setup_class_ok;
843  }
844  break;
845  }
846  }
847 #endif /* USB_CDC */
848  goto stall_i; /* not supported */
849  /* end case REQUEST_TO_INTERFACE */
850 
851  case REQUEST_TO_ENDPOINT:
852 #if USB_AUDIO
853  switch (SetupPacket.bRequest) {
854  case AUDIO_REQUEST_GET_CUR:
855  case AUDIO_REQUEST_GET_MIN:
856  case AUDIO_REQUEST_GET_MAX:
857  case AUDIO_REQUEST_GET_RES:
858  if (ADC_EP_GetRequest()) {
859  EP0Data.pData = EP0Buf; /* point to data to be sent */
860  USB_DataInStage(); /* send requested data */
861  goto setup_class_ok;
862  }
863  break;
864  case AUDIO_REQUEST_SET_CUR:
865 // case AUDIO_REQUEST_SET_MIN:
866 // case AUDIO_REQUEST_SET_MAX:
867 // case AUDIO_REQUEST_SET_RES:
868  EP0Data.pData = EP0Buf; /* data to be received */
869  goto setup_class_ok;
870  }
871 #endif /* USB_AUDIO */
872  goto stall_i;
873  /* end case REQUEST_TO_ENDPOINT */
874 
875  default:
876  goto stall_i;
877  }
878 setup_class_ok: /* request finished successfully */
879  break; /* end case REQUEST_CLASS */
880 #endif /* USB_CLASS */
881 
882 #if USB_VENDOR
883  case REQUEST_VENDOR:
884  switch (SetupPacket.bmRequestType.BM.Recipient) {
885 
886  case REQUEST_TO_DEVICE:
887  if (!USB_ReqVendorDev(TRUE)) {
888  goto stall_i; /* not supported */
889  }
890  break;
891 
893  if (!USB_ReqVendorIF(TRUE)) {
894  goto stall_i; /* not supported */
895  }
896  break;
897 
898  case REQUEST_TO_ENDPOINT:
899  if (!USB_ReqVendorEP(TRUE)) {
900  goto stall_i; /* not supported */
901  }
902  break;
903 
904  default:
905  goto stall_i;
906  }
907 
908  if (SetupPacket.wLength) {
909  if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
910  USB_DataInStage();
911  }
912  } else {
914  }
915 
916  break; /* end case REQUEST_VENDOR */
917 #endif /* USB_VENDOR */
918 
919  default:
920 stall_i: USB_SetStallEP(0x80);
921  EP0Data.Count = 0;
922  break;
923  }
924  break; /* end case USB_EVT_SETUP */
925 
926  case USB_EVT_OUT:
927  if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
928  if (EP0Data.Count) { /* still data to receive ? */
929  USB_DataOutStage(); /* receive data */
930  if (EP0Data.Count == 0) { /* data complete ? */
931  switch (SetupPacket.bmRequestType.BM.Type) {
932 
933  case REQUEST_STANDARD:
934  goto stall_i; /* not supported */
935 
936 #if (USB_CLASS)
937  case REQUEST_CLASS:
938  switch (SetupPacket.bmRequestType.BM.Recipient) {
939  case REQUEST_TO_DEVICE:
940  goto stall_i; /* not supported */
941 
943 #if USB_HID
944  if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
945  switch (SetupPacket.bRequest) {
946  case HID_REQUEST_SET_REPORT:
947  if (HID_SetReport()) {
948  USB_StatusInStage(); /* send Acknowledge */
949  goto out_class_ok;
950  }
951  break;
952  }
953  }
954 #endif /* USB_HID */
955 #if USB_AUDIO
956  if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
957  (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
958  (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
959  switch (SetupPacket.bRequest) {
960  case AUDIO_REQUEST_SET_CUR:
961 // case AUDIO_REQUEST_SET_MIN:
962 // case AUDIO_REQUEST_SET_MAX:
963 // case AUDIO_REQUEST_SET_RES:
964  if (ADC_IF_SetRequest()) {
965  USB_StatusInStage(); /* send Acknowledge */
966  goto out_class_ok;
967  }
968  break;
969  }
970  }
971 #endif /* USB_AUDIO */
972 #if USB_CDC
973  if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
974  (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
975  switch (SetupPacket.bRequest) {
978  USB_StatusInStage(); /* send Acknowledge */
979  goto out_class_ok;
980  }
981  break;
983  if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
984  USB_StatusInStage(); /* send Acknowledge */
985  goto out_class_ok;
986  }
987  break;
988  case CDC_SET_LINE_CODING:
989  if (CDC_SetLineCoding()) {
990  USB_StatusInStage(); /* send Acknowledge */
991  goto out_class_ok;
992  }
993  break;
994  }
995  }
996 #endif /* USB_CDC */
997  goto stall_i;
998  /* end case REQUEST_TO_INTERFACE */
999 
1000  case REQUEST_TO_ENDPOINT:
1001 #if USB_AUDIO
1002  switch (SetupPacket.bRequest) {
1003  case AUDIO_REQUEST_SET_CUR:
1004 // case AUDIO_REQUEST_SET_MIN:
1005 // case AUDIO_REQUEST_SET_MAX:
1006 // case AUDIO_REQUEST_SET_RES:
1007  if (ADC_EP_SetRequest()) {
1008  USB_StatusInStage(); /* send Acknowledge */
1009  goto out_class_ok;
1010  }
1011  break;
1012  }
1013 #endif /* USB_AUDIO */
1014  goto stall_i;
1015  /* end case REQUEST_TO_ENDPOINT */
1016 
1017  default:
1018  goto stall_i;
1019  }
1020 out_class_ok: /* request finished successfully */
1021  break; /* end case REQUEST_CLASS */
1022 #endif /* USB_CLASS */
1023 
1024 #if USB_VENDOR
1025  case REQUEST_VENDOR:
1026  switch (SetupPacket.bmRequestType.BM.Recipient) {
1027 
1028  case REQUEST_TO_DEVICE:
1029  if (!USB_ReqVendorDev(FALSE)) {
1030  goto stall_i; /* not supported */
1031  }
1032  break;
1033 
1034  case REQUEST_TO_INTERFACE:
1035  if (!USB_ReqVendorIF(FALSE)) {
1036  goto stall_i; /* not supported */
1037  }
1038  break;
1039 
1040  case REQUEST_TO_ENDPOINT:
1041  if (!USB_ReqVendorEP(FALSE)) {
1042  goto stall_i; /* not supported */
1043  }
1044  break;
1045 
1046  default:
1047  goto stall_i;
1048  }
1049 
1051 
1052  break; /* end case REQUEST_VENDOR */
1053 #endif /* USB_VENDOR */
1054 
1055  default:
1056  goto stall_i;
1057  }
1058  }
1059  }
1060  } else {
1061  USB_StatusOutStage(); /* receive Acknowledge */
1062  }
1063  break; /* end case USB_EVT_OUT */
1064 
1065  case USB_EVT_IN :
1066  if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1067  USB_DataInStage(); /* send data */
1068  } else {
1069  if (USB_DeviceAddress & 0x80) {
1070  USB_DeviceAddress &= 0x7F;
1072  }
1073  }
1074  break; /* end case USB_EVT_IN */
1075 
1076  case USB_EVT_OUT_STALL:
1077  USB_ClrStallEP(0x00);
1078  break;
1079 
1080  case USB_EVT_IN_STALL:
1081  USB_ClrStallEP(0x80);
1082  break;
1083 
1084  }
1085 }
uint32_t CDC_SendBreak(unsigned short wDurationOfBreak)
Definition: cdcuser.c:270
void USB_Feature_Event(void)
#define USB_FEATURE_ENDPOINT_STALL
Definition: usb.h:77
#define USB_REQUEST_SET_FEATURE
Definition: usb.h:61
uint16_t USB_DeviceStatus
Definition: usbcore.c:62
uint16_t wTotalLength
Definition: usb.h:63
const uint8_t USB_DeviceDescriptor[]
Definition: usbdesc.c:31
void USB_DisableEP(uint32_t EPNum)
Definition: usbhw.c:325
void USB_Configure_Event(void)
#define REQUEST_DEVICE_TO_HOST
Definition: usb.h:34
__inline uint32_t USB_ReqSetInterface(void)
Definition: usbcore.c:532
#define USB_STRING_DESCRIPTOR_TYPE
Definition: usb.h:93
__inline void UsbAddPtr(void **vpptr, uint32_t n)
Definition: usbcore.c:398
uint8_t bLength
Definition: usb.h:61
#define USB_ENDPOINT_DESCRIPTOR_TYPE
Definition: usb.h:95
uint16_t Count
Definition: usbcore.h:26
#define REQUEST_TO_INTERFACE
Definition: usb.h:44
uint32_t USB_ReadEP(uint32_t EPNum, uint8_t *pData)
Definition: usbhw.c:391
#define USB_REQUEST_SET_CONFIGURATION
Definition: usb.h:66
#define USB_REQUEST_GET_DESCRIPTOR
Definition: usb.h:63
#define CDC_SET_LINE_CODING
Definition: cdc.h:104
uint32_t USB_EndPointMask
Definition: usbcore.c:65
void USB_DataInStage(void)
Definition: usbcore.c:113
void USB_SetAddress(uint32_t adr)
Definition: usbhw.c:263
void USB_ResetEP(uint32_t EPNum)
Definition: usbhw.c:338
__inline uint32_t USB_ReqSetAddress(void)
Definition: usbcore.c:271
#define USB_EVT_OUT_STALL
Definition: usbuser.h:38
uint32_t CDC_SetControlLineState(unsigned short wControlSignalBitmap)
Definition: cdcuser.c:255
#define USB_CDC_CIF_NUM
Definition: usbcfg.h:145
#define REQUEST_CLASS
Definition: usb.h:38
#define USB_REQUEST_SET_DESCRIPTOR
Definition: usb.h:64
#define USB_MSC_IF_NUM
Definition: usbcfg.h:139
uint8_t EP0Buf[USB_MAX_PACKET0]
Definition: usbcore.c:71
uint32_t USB_EndPointHalt
Definition: usbcore.c:66
#define FALSE
Definition: type.h:39
#define USB_FEATURE_REMOTE_WAKEUP
Definition: usb.h:78
void USB_EndPoint0(uint32_t event)
Definition: usbcore.c:598
uint32_t CDC_GetCommFeature(unsigned short wFeatureSelector)
Definition: cdcuser.c:183
#define TRUE
Definition: type.h:43
unsigned char uint8_t
Definition: type.h:27
#define USB_REQUEST_SET_INTERFACE
Definition: usb.h:68
#define USB_EVT_IN_STALL
Definition: usbuser.h:39
void USB_Interface_Event(void)
#define CDC_SET_COMM_FEATURE
Definition: cdc.h:95
void USB_DataOutStage(void)
Definition: usbcore.c:133
#define CDC_GET_LINE_CODING
Definition: cdc.h:105
#define USB_INTERFACE_DESCRIPTOR_TYPE
Definition: usb.h:94
__inline uint32_t USB_ReqSetClrFeature(uint32_t sc)
Definition: usbcore.c:209
#define REQUEST_TO_DEVICE
Definition: usb.h:43
#define USB_MAX_PACKET0
Definition: usbcfg.h:60
#define NULL
Definition: type.h:35
uint32_t CDC_GetEncapsulatedResponse(void)
Definition: cdcuser.c:157
uint32_t USB_EndPointStall
Definition: usbcore.c:67
#define REQUEST_VENDOR
Definition: usb.h:39
__inline uint32_t USB_ReqGetConfiguration(void)
Definition: usbcore.c:373
#define USB_DEVICE_DESCRIPTOR_TYPE
Definition: usb.h:91
void USB_StatusInStage(void)
Definition: usbcore.c:148
#define USB_IF_NUM
Definition: usbcfg.h:57
#define USB_REQUEST_SET_ADDRESS
Definition: usb.h:62
uint32_t CDC_SetLineCoding(void)
Definition: cdcuser.c:209
#define REQUEST_TO_ENDPOINT
Definition: usb.h:45
#define USB_EVT_SETUP
Definition: usbuser.h:33
uint32_t CDC_GetLineCoding(void)
Definition: cdcuser.c:235
#define USB_CONFIGURATION_DESCRIPTOR_TYPE
Definition: usb.h:92
#define USB_ADC_CIF_NUM
Definition: usbcfg.h:141
uint8_t USB_NumInterfaces
Definition: usbcore.c:68
#define CDC_CLEAR_COMM_FEATURE
Definition: cdc.h:97
#define CDC_SEND_BREAK
Definition: cdc.h:107
uint32_t CDC_SetCommFeature(unsigned short wFeatureSelector)
Definition: cdcuser.c:170
#define CDC_GET_COMM_FEATURE
Definition: cdc.h:96
void USB_StatusOutStage(void)
Definition: usbcore.c:159
uint8_t USB_Configuration
Definition: usbcore.c:64
#define USB_REQUEST_GET_STATUS
Definition: usb.h:59
const uint8_t USB_StringDescriptor[]
Definition: usbdesc.c:139
void USB_WakeUpCfg(uint32_t cfg)
Definition: usbhw.c:252
uint8_t USB_DeviceAddress
Definition: usbcore.c:63
#define CDC_SEND_ENCAPSULATED_COMMAND
Definition: cdc.h:93
#define USB_ADC_SIF1_NUM
Definition: usbcfg.h:142
const uint8_t USB_ConfigDescriptor[]
Definition: usbdesc.c:50
unsigned short int uint16_t
Definition: type.h:28
void USB_ResetCore(void)
Definition: usbcore.c:85
USB_SETUP_PACKET SetupPacket
Definition: usbcore.c:76
__inline uint32_t USB_ReqGetStatus(void)
Definition: usbcore.c:170
uint32_t USB_WriteEP(uint32_t EPNum, uint8_t *pData, uint32_t cnt)
Definition: usbhw.c:428
void USB_ConfigEP(USB_ENDPOINT_DESCRIPTOR *pEPD)
Definition: usbhw.c:288
#define CDC_SET_CONTROL_LINE_STATE
Definition: cdc.h:106
#define USB_HID_IF_NUM
Definition: usbcfg.h:137
__inline uint32_t USB_ReqSetConfiguration(void)
Definition: usbcore.c:418
void USB_ClrStallEP(uint32_t EPNum)
Definition: usbhw.c:364
#define REQUEST_STANDARD
Definition: usb.h:37
void USB_SetupStage(void)
Definition: usbcore.c:102
#define USB_REQUEST_GET_CONFIGURATION
Definition: usb.h:65
void USB_Configure(uint32_t cfg)
Definition: usbhw.c:275
void USB_DirCtrlEP(uint32_t dir)
Definition: usbhw.c:299
uint8_t USB_AltSetting[USB_IF_NUM]
Definition: usbcore.c:69
#define USB_EVT_IN
Definition: usbuser.h:35
typedef __attribute__
void USB_SetStallEP(uint32_t EPNum)
Definition: usbhw.c:351
#define USB_CONFIG_POWERED_MASK
Definition: usb.h:118
#define USB_CDC_DIF_NUM
Definition: usbcfg.h:146
__inline uint32_t USB_ReqGetInterface(void)
Definition: usbcore.c:509
uint8_t bmAttributes
Definition: usb.h:67
USB_EP_DATA EP0Data
Definition: usbcore.c:74
uint32_t CDC_SendEncapsulatedCommand(void)
Definition: cdcuser.c:145
#define USB_REQUEST_GET_INTERFACE
Definition: usb.h:67
#define USB_GETSTATUS_SELF_POWERED
Definition: usb.h:72
#define REQUEST_HOST_TO_DEVICE
Definition: usb.h:33
#define USB_POWER
Definition: usbcfg.h:56
uint32_t CDC_ClearCommFeature(unsigned short wFeatureSelector)
Definition: cdcuser.c:196
void USB_EnableEP(uint32_t EPNum)
Definition: usbhw.c:312
#define USB_DEVICE_DESC_SIZE
Definition: usbdesc.h:25
uint8_t * pData
Definition: usbcore.h:25
#define USB_ADC_SIF2_NUM
Definition: usbcfg.h:143
#define USB_REQUEST_CLEAR_FEATURE
Definition: usb.h:60
__inline uint32_t USB_ReqGetDescriptor(void)
Definition: usbcore.c:290
#define USB_GETSTATUS_REMOTE_WAKEUP
Definition: usb.h:73
#define USB_EVT_OUT
Definition: usbuser.h:34
unsigned int uint32_t
Definition: type.h:29
#define CDC_GET_ENCAPSULATED_RESPONSE
Definition: cdc.h:94