rahultheengineer

USB CONFIGURATION

Apr 12th, 2017
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.36 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. USB DESCRIPTOR
  3. -------------------------------------------------------------------------------
  4.  
  5. ********************************************************************/
  6.  
  7. /*********************************************************************
  8.  * Descriptor specific type definitions are defined in:
  9.  * usb_device.h
  10.  *
  11.  * Configuration options are defined in:
  12.  * usb_config.h
  13.  ********************************************************************/
  14. #ifndef __USB_DESCRIPTORS_C
  15. #define __USB_DESCRIPTORS_C
  16.  
  17. /** INCLUDES *******************************************************/
  18. #include "usb.h"
  19.  
  20. /** CONSTANTS ******************************************************/
  21. #if defined(COMPILER_MPLAB_C18)
  22. #pragma romdata
  23. #endif
  24.  
  25. /* Device Descriptor */
  26. const USB_DEVICE_DESCRIPTOR device_dsc=
  27. {
  28.     0x12,                   // Size of this descriptor in bytes
  29.     USB_DESCRIPTOR_DEVICE,  // DEVICE descriptor type
  30.     0x0200,                 // USB Spec Release Number in BCD format        
  31.     0x00,                   // Class Code
  32.     0x00,                   // Subclass code
  33.     0x00,                   // Protocol code
  34.     USB_EP0_BUFF_SIZE,      // Max packet size for EP0, see usb_config.h
  35.     0x04D8,                 // Vendor ID: 0x04D8 is Microchip's Vendor ID
  36.     0x0052,                 // Product ID: 0x0052
  37.     0x0000,                 // Device release number in BCD format
  38.     0x01,                   // Manufacturer string index
  39.     0x02,                   // Product string index
  40.     0x00,                   // Device serial number string index
  41.     0x01                    // Number of possible configurations
  42. };
  43.  
  44. /* Configuration 1 Descriptor */
  45. const uint8_t configDescriptor1[]={
  46.     /* Configuration Descriptor */
  47.     0x09,//sizeof(USB_CFG_DSC),    // Size of this descriptor in bytes
  48.     USB_DESCRIPTOR_CONFIGURATION,                // CONFIGURATION descriptor type
  49.     0x2E,0x00,            // Total length of data for this cfg// 0x19 for 1 EP, 0x20 for 2 EP,0x27 for 3 EP, 0x2E for 4 EP,add 7 for every EP
  50.     1,                      // Number of interfaces in this cfg
  51.     1,                      // Index value of this configuration
  52.     0,                      // Configuration string index
  53.     _DEFAULT | _SELF,               // Attributes, see usb_device.h
  54.     50,                     // Max power consumption (2X mA)
  55.                            
  56.     /* Interface Descriptor */
  57.     0x09,//sizeof(USB_INTF_DSC),   // Size of this descriptor in bytes
  58.     USB_DESCRIPTOR_INTERFACE,               // INTERFACE descriptor type
  59.     0,                      // Interface Number
  60.     0,                      // Alternate Setting Number
  61.     4,                      // Number of endpoints in this intf
  62.     0xFF,                   // Class code
  63.     0xFF,                   // Subclass code
  64.     0xFF,                   // Protocol code
  65.     0,                      // Interface string index
  66.    
  67.     /* Endpoint Descriptor */
  68.     0x07,                       /*sizeof(USB_EP_DSC)*/
  69.     USB_DESCRIPTOR_ENDPOINT,    //Endpoint Descriptor
  70.     _EP01_OUT,                  //EndpointAddress
  71. //    _EP01_IN,
  72.     _BULK,                       //Attributes
  73.     64,0x00,                    //size
  74.     1,                         //Interval
  75.    
  76.     0x07,                       /*sizeof(USB_EP_DSC)*/
  77.     USB_DESCRIPTOR_ENDPOINT,    //Endpoint Descriptor
  78.     _EP02_OUT,                   //EndpointAddress
  79.     _BULK,                       //Attributes
  80.     64,0x00,                    //size
  81.     1,                          //Interval
  82.  
  83.     0x07,                       /*sizeof(USB_EP_DSC)*/
  84.     USB_DESCRIPTOR_ENDPOINT,    //Endpoint Descriptor
  85.     _EP03_OUT,                   //EndpointAddress
  86.     _BULK,                       //Attributes
  87.     64,0x00,                    //size
  88.     1,                          //Interval
  89.  
  90.     0x07,                       /*sizeof(USB_EP_DSC)*/
  91.     USB_DESCRIPTOR_ENDPOINT,    //Endpoint Descriptor
  92.     _EP01_IN,                   //EndpointAddress
  93.     _BULK,                       //Attributes
  94.     64,0x00,                    //size
  95.     1  
  96. };
  97.  
  98.  
  99. //Language code string descriptor
  100. const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[1];}sd000={
  101. sizeof(sd000),USB_DESCRIPTOR_STRING,{0x0409}};
  102.  
  103. //Manufacturer string descriptor
  104. const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[25];}sd001={
  105. sizeof(sd001),USB_DESCRIPTOR_STRING,
  106. {'M','i','c','r','o','c','h','i','p',' ',
  107. 'T','e','c','h','n','o','l','o','g','y',' ','I','n','c','.'
  108. }};
  109.  
  110. //Product string descriptor
  111. const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[31];}sd002={
  112. sizeof(sd002),USB_DESCRIPTOR_STRING,
  113. {'M','i','c','r','o','c','h','i','p',' ','W','i','n','U','S','B',
  114. ' ','E','x','a','m','p','l','e',' ','D','e','v','i','c','e'}};
  115.  
  116. //Serial number string descriptor.  If a serial number string is implemented,
  117. //it should be unique for every single device coming off the production assembly
  118. //line.  Plugging two devices with the same serial number into a computer
  119. //simultaneously will cause problems (in extreme cases BSOD).
  120. //Note: Common OSes put restrictions on the possible values that are allowed.
  121. //For best OS compatibility, the serial number string should only consist
  122. //of UNICODE encoded numbers 0 through 9 and capital letters A through F.
  123. //const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[10];}sd003={
  124. //sizeof(sd003),USB_DESCRIPTOR_STRING,
  125. //{'0','1','2','3','4','5','6','7','8','9'}};
  126.  
  127. //Array of configuration descriptors
  128. const uint8_t *const USB_CD_Ptr[]=
  129. {
  130.     (const uint8_t *const)&configDescriptor1
  131. };
  132. //Array of string descriptors
  133. const uint8_t *const USB_SD_Ptr[]=
  134. {
  135.     (const uint8_t *const)&sd000,
  136.     (const uint8_t *const)&sd001,
  137.     (const uint8_t *const)&sd002,
  138.     //(const uint8_t *const)&sd003
  139. };
  140.  
  141.  
  142.  
  143. #if defined(IMPLEMENT_MICROSOFT_OS_DESCRIPTOR)
  144.     //Microsoft "OS Descriptor" - This descriptor is based on a Microsoft specific
  145.     //specification (not part of the standard USB 2.0 specs or class specs).
  146.     //Implementing this special descriptor allows WinUSB driver package installation
  147.     //to be automatic on Windows 8.  For additional details, see:
  148.     //http://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx
  149.     const MS_OS_DESCRIPTOR MSOSDescriptor =
  150.     {  
  151.         sizeof(MSOSDescriptor),         //bLength - lenght of this descriptor in bytes
  152.         USB_DESCRIPTOR_STRING,          //bDescriptorType - "string"
  153.         {'M','S','F','T','1','0','0'},  //qwSignature - special values that specifies the OS descriptor spec version that this firmware implements
  154.         GET_MS_DESCRIPTOR,              //bMS_VendorCode - defines the "GET_MS_DESCRIPTOR" bRequest literal value
  155.         0x00                            //bPad - always 0x00
  156.     };
  157.    
  158.    
  159.     //Extended Compat ID OS Feature Descriptor
  160.     const MS_COMPAT_ID_FEATURE_DESC CompatIDFeatureDescriptor =
  161.     {
  162.         //----------Header Section--------------
  163.         sizeof(CompatIDFeatureDescriptor),  //dwLength
  164.         0x0100,                             //bcdVersion = 1.00
  165.         EXTENDED_COMPAT_ID,                 //wIndex
  166.         0x01,                               //bCount - 0x01 "Function Section(s)" implemented in this descriptor
  167.         {0,0,0,0,0,0,0},                    //Reserved[7]
  168.         //----------Function Section 1----------
  169.         0x00,                               //bFirstInterfaceNumber: the WinUSB interface in this firmware is interface #0
  170.         0x01,                               //Reserved - fill this reserved byte with 0x01 according to documentation
  171.         {'W','I','N','U','S','B',0x00,0x00},//compatID - "WINUSB" (with two null terminators to fill all 8 bytes)
  172.         {0,0,0,0,0,0,0,0},                  //subCompatID - eight bytes of 0
  173.         {0,0,0,0,0,0}                       //Reserved
  174.     };    
  175.    
  176.    
  177.     //Extended Properties OS Feature Descriptor
  178.     const MS_EXT_PROPERTY_FEATURE_DESC ExtPropertyFeatureDescriptor =
  179.     {
  180.         //----------Header Section--------------
  181.         sizeof(ExtPropertyFeatureDescriptor),   //dwLength
  182.         0x0100,                                 //bcdVersion = 1.00
  183.         EXTENDED_PROPERTIES,                    //wIndex
  184.         0x0001,                                 //wCount - 0x0001 "Property Sections" implemented in this descriptor
  185.         //----------Property Section 1----------
  186.         132,                                    //dwSize - 132 bytes in this Property Section
  187.         0x00000001,                             //dwPropertyDataType (Unicode string)
  188.         40,                                     //wPropertyNameLength - 40 bytes in the bPropertyName field
  189.         {'D','e','v','i','c','e','I','n','t','e','r','f','a','c','e','G','U','I','D', 0x0000},  //bPropertyName - "DeviceInterfaceGUID"
  190.         78,                                     //dwPropertyDataLength - 78 bytes in the bPropertyData field (GUID value in UNICODE formatted string, with braces and dashes)
  191.         //The below value is the Device Interface GUID (a 128-bit long "globally unique identifier")
  192.         //Please modify the GUID value in your application before moving to production.
  193.         //When you change the GUID, you must also change the PC application software
  194.         //that connects to this device, as the software looks for the device based on
  195.         //VID, PID, and GUID.  All three values in the PC application must match
  196.         //the values in this firmware.
  197.         //The GUID value can be a randomly generated 128-bit hexadecimal number,
  198.         //formatted like the below value.  The actual value is not important,
  199.         //so long as it is almost certain to be globally unique, and matches the
  200.         //PC software that communicates with this firmware.
  201.         {'{','5','8','d','0','7','2','1','0','-','2','7','c','1','-','1','1','d','d','-','b','d','0','b','-','0','8','0','0','2','0','0','c','9','a','6','6','}',0x0000}  //bPropertyData - this is the actual GUID value.  Make sure this matches the PC application code trying to connect to the device.
  202.     };    
  203. #endif
  204.  
  205. /** EOF usb_descriptors.c ***************************************************/
  206.  
  207. #endif
  208.  
  209.  
  210. ------------------------------------------------------------------------------------
  211. USB CONFIGURATION
  212. ---------------------------------------------------------------------------------
  213. *******************************************************************************/
  214.  
  215. /*********************************************************************
  216.  * Descriptor specific type definitions are defined in: usbd.h
  217.  ********************************************************************/
  218.  
  219. #ifndef USBCFG_H
  220. #define USBCFG_H
  221.  
  222. #include <stdint.h>
  223.  
  224. /** DEFINITIONS ****************************************************/
  225. #define USB_EP0_BUFF_SIZE       8   // Valid Options: 8, 16, 32, or 64 bytes.
  226.                                 // Using larger options take more SRAM, but
  227.                                 // does not provide much advantage in most types
  228.                                 // of applications.  Exceptions to this, are applications
  229.                                 // that use EP0 IN or OUT for sending large amounts of
  230.                                 // application related data.
  231.                                    
  232. #define USB_MAX_NUM_INT         1   //Set this number to match the maximum interface number used in the descriptors for this firmware project
  233. #define USB_MAX_EP_NUMBER       4   //Set this number to match the maximum endpoint number used in the descriptors for this firmware project
  234.  
  235. //Device descriptor - if these two definitions are not defined then
  236. //  a const USB_DEVICE_DESCRIPTOR variable by the exact name of device_dsc
  237. //  must exist.
  238. #define USB_USER_DEVICE_DESCRIPTOR &device_dsc
  239. #define USB_USER_DEVICE_DESCRIPTOR_INCLUDE extern const USB_DEVICE_DESCRIPTOR device_dsc
  240.  
  241. //Configuration descriptors - if these two definitions do not exist then
  242. //  a const uint8_t *const variable named exactly USB_CD_Ptr[] must exist.
  243. #define USB_USER_CONFIG_DESCRIPTOR USB_CD_Ptr
  244. #define USB_USER_CONFIG_DESCRIPTOR_INCLUDE extern const uint8_t *const USB_CD_Ptr[]
  245.  
  246.  
  247. //------------------------------------------------------------------------------
  248. //Select an endpoint ping-pong bufferring mode.  Some microcontrollers only
  249. //support certain modes.  For most applications, it is recommended to use either
  250. //the USB_PING_PONG__FULL_PING_PONG or USB_PING_PONG__EP0_OUT_ONLY options.  
  251. //The other settings are supported on some devices, but they are not
  252. //recommended, as they offer inferior control transfer timing performance.  
  253. //See inline code comments in usb_device.c for additional details.
  254. //Enabling ping pong bufferring on an endpoint generally increases firmware
  255. //overhead somewhat, but when both buffers are used simultaneously in the
  256. //firmware, can offer better sustained bandwidth, especially for OUT endpoints.
  257. //------------------------------------------------------
  258. //#define USB_PING_PONG_MODE USB_PING_PONG__NO_PING_PONG    //Not recommended
  259. #define USB_PING_PONG_MODE USB_PING_PONG__FULL_PING_PONG    //Required setting for this particular demo
  260. //#define USB_PING_PONG_MODE USB_PING_PONG__EP0_OUT_ONLY    
  261. //#define USB_PING_PONG_MODE USB_PING_PONG__ALL_BUT_EP0     //Not recommended
  262. //------------------------------------------------------------------------------
  263.  
  264.  
  265. //------------------------------------------------------------------------------
  266. //Select a USB stack operating mode.  In the USB_INTERRUPT mode, the USB stack
  267. //main task handler gets called only when necessary as an interrupt handler.
  268. //This can potentially minimize CPU utilization, but adds context saving
  269. //and restoring overhead associated with interrupts, which can potentially
  270. //decrease performance.
  271. //When the USB_POLLING mode is selected, the USB stack main task handler
  272. //(ex: USBDeviceTasks()) must be called periodically by the application firmware
  273. //at a minimum rate as described in the inline code comments in usb_device.c.
  274. //------------------------------------------------------
  275. #define USB_POLLING
  276. //#define USB_INTERRUPT
  277. //------------------------------------------------------------------------------
  278.  
  279. /* Parameter definitions are defined in usb_device.h */
  280. #define USB_PULLUP_OPTION USB_PULLUP_ENABLE
  281. //#define USB_PULLUP_OPTION USB_PULLUP_DISABLED
  282.  
  283. #define USB_TRANSCEIVER_OPTION USB_INTERNAL_TRANSCEIVER
  284. //External Transceiver support is not available on all product families.  Please
  285. //  refer to the product family datasheet for more information if this feature
  286. //  is available on the target processor.
  287. //#define USB_TRANSCEIVER_OPTION USB_EXTERNAL_TRANSCEIVER
  288.  
  289. #define USB_SPEED_OPTION USB_FULL_SPEED
  290. //#define USB_SPEED_OPTION USB_LOW_SPEED //(this mode is only supported on some microcontrollers)
  291.  
  292.  
  293. #define USB_ENABLE_STATUS_STAGE_TIMEOUTS    //Comment this out to disable this feature.  
  294.  
  295.  
  296. #define USB_STATUS_STAGE_TIMEOUT     (uint8_t)45   //Approximate timeout in milliseconds, except when
  297.                                                 //USB_POLLING mode is used, and USBDeviceTasks() is called at < 1kHz
  298.                                                 //In this special case, the timeout becomes approximately:
  299. //Timeout(in milliseconds) = ((1000 * (USB_STATUS_STAGE_TIMEOUT - 1)) / (USBDeviceTasks() polling frequency in Hz))
  300. //------------------------------------------------------------------------------------------------------------------
  301.  
  302.  
  303.  
  304. #define IMPLEMENT_MICROSOFT_OS_DESCRIPTOR
  305.  
  306. //Some definitions, only needed when using the MS OS descriptor.
  307. #if defined(IMPLEMENT_MICROSOFT_OS_DESCRIPTOR)
  308.     #if defined(__XC8)
  309.         #define __attribute__(a)
  310.     #endif
  311.     #define MICROSOFT_OS_DESCRIPTOR_INDEX   (unsigned char)0xEE //Magic string index number for the Microsoft OS descriptor
  312.     #define GET_MS_DESCRIPTOR               (unsigned char)0xEE //(arbitarily assigned, but should not clobber/overlap normal bRequests)
  313.     #define EXTENDED_COMPAT_ID              (uint16_t)0x0004
  314.     #define EXTENDED_PROPERTIES             (uint16_t)0x0005
  315.  
  316.     typedef struct __attribute__ ((packed)) _MS_OS_DESCRIPTOR
  317.     {
  318.         uint8_t bLength;
  319.         uint8_t bDscType;
  320.         uint16_t string[7];
  321.         uint8_t vendorCode;
  322.         uint8_t bPad;
  323.     }MS_OS_DESCRIPTOR;
  324.  
  325.     typedef struct __attribute__ ((packed)) _MS_COMPAT_ID_FEATURE_DESC
  326.     {
  327.         uint32_t dwLength;
  328.         uint16_t bcdVersion;
  329.         uint16_t wIndex;
  330.         uint8_t bCount;
  331.         uint8_t Reserved[7];
  332.         uint8_t bFirstInterfaceNumber;
  333.         uint8_t Reserved1;
  334.         uint8_t compatID[8];
  335.         uint8_t subCompatID[8];
  336.         uint8_t Reserved2[6];
  337.     }MS_COMPAT_ID_FEATURE_DESC;
  338.  
  339.     typedef struct __attribute__ ((packed)) _MS_EXT_PROPERTY_FEATURE_DESC
  340.     {
  341.         uint32_t dwLength;
  342.         uint16_t bcdVersion;
  343.         uint16_t wIndex;
  344.         uint16_t wCount;
  345.         uint32_t dwSize;
  346.         uint32_t dwPropertyDataType;
  347.         uint16_t wPropertyNameLength;
  348.         uint16_t bPropertyName[20];
  349.         uint32_t dwPropertyDataLength;
  350.         uint16_t bPropertyData[39];
  351.     }MS_EXT_PROPERTY_FEATURE_DESC;
  352.    
  353.     extern const MS_OS_DESCRIPTOR MSOSDescriptor;
  354.     extern const MS_COMPAT_ID_FEATURE_DESC CompatIDFeatureDescriptor;
  355.     extern const MS_EXT_PROPERTY_FEATURE_DESC ExtPropertyFeatureDescriptor;
  356. #endif
  357.  
  358.  
  359. #define USB_SUPPORT_DEVICE
  360.  
  361. #define USB_NUM_STRING_DESCRIPTORS 3  //Set this number to match the total number of string descriptors that are implemented in the usb_descriptors.c file
  362.  
  363. /*******************************************************************
  364.  * Event disable options                                          
  365.  *   Enable a definition to suppress a specific event.  By default
  366.  *   all events are sent.                                          
  367.  *******************************************************************/
  368. //#define USB_DISABLE_SUSPEND_HANDLER
  369. //#define USB_DISABLE_WAKEUP_FROM_SUSPEND_HANDLER
  370. //#define USB_DISABLE_SOF_HANDLER
  371. //#define USB_DISABLE_TRANSFER_TERMINATED_HANDLER
  372. //#define USB_DISABLE_ERROR_HANDLER
  373. //#define USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER
  374. //#define USB_DISABLE_SET_DESCRIPTOR_HANDLER
  375. //#define USB_DISABLE_SET_CONFIGURATION_HANDLER
  376. //#define USB_DISABLE_TRANSFER_COMPLETE_HANDLER
  377.  
  378. /** DEVICE CLASS USAGE *********************************************/
  379. #define USB_USE_GEN
  380.  
  381. /** ENDPOINTS ALLOCATION *******************************************/
  382.  
  383. /* Generic */
  384. #define USBGEN_EP_SIZE          64
  385. #define USBGEN_EP_NUM            1
  386.  
  387. /** DEFINITIONS ****************************************************/
  388.  
  389. #endif //USBCFG_H
Add Comment
Please, Sign In to add comment