usb_descriptors.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /********************************************************************
  2. FileName: usb_descriptors.c
  3. Dependencies: See INCLUDES section
  4. Processor: PIC18 or PIC24 USB Microcontrollers
  5. Hardware: The code is natively intended to be used on the following
  6. hardware platforms: PICDEM™ FS USB Demo Board,
  7. PIC18F87J50 FS USB Plug-In Module, or
  8. Explorer 16 + PIC24 USB PIM. The firmware may be
  9. modified for use on other USB platforms by editing the
  10. HardwareProfile.h file.
  11. Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
  12. Company: Microchip Technology, Inc.
  13. Software License Agreement:
  14. The software supplied herewith by Microchip Technology Incorporated
  15. (the “Company”) for its PIC® Microcontroller is intended and
  16. supplied to you, the Company’s customer, for use solely and
  17. exclusively on Microchip PIC Microcontroller products. The
  18. software is owned by the Company and/or its supplier, and is
  19. protected under applicable copyright laws. All rights are reserved.
  20. Any use in violation of the foregoing restrictions may subject the
  21. user to criminal sanctions under applicable laws, as well as to
  22. civil liability for the breach of the terms and conditions of this
  23. license.
  24. THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
  25. WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  26. TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27. PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
  28. IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
  29. CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  30. *********************************************************************
  31. -usb_descriptors.c-
  32. -------------------------------------------------------------------
  33. Filling in the descriptor values in the usb_descriptors.c file:
  34. -------------------------------------------------------------------
  35. [Device Descriptors]
  36. The device descriptor is defined as a USB_DEVICE_DESCRIPTOR type.
  37. This type is defined in usb_ch9.h Each entry into this structure
  38. needs to be the correct length for the data type of the entry.
  39. [Configuration Descriptors]
  40. The configuration descriptor was changed in v2.x from a structure
  41. to a BYTE array. Given that the configuration is now a byte array
  42. each byte of multi-byte fields must be listed individually. This
  43. means that for fields like the total size of the configuration where
  44. the field is a 16-bit value "64,0," is the correct entry for a
  45. configuration that is only 64 bytes long and not "64," which is one
  46. too few bytes.
  47. The configuration attribute must always have the _DEFAULT
  48. definition at the minimum. Additional options can be ORed
  49. to the _DEFAULT attribute. Available options are _SELF and _RWU.
  50. These definitions are defined in the usb_device.h file. The
  51. _SELF tells the USB host that this device is self-powered. The
  52. _RWU tells the USB host that this device supports Remote Wakeup.
  53. [Endpoint Descriptors]
  54. Like the configuration descriptor, the endpoint descriptors were
  55. changed in v2.x of the stack from a structure to a BYTE array. As
  56. endpoint descriptors also has a field that are multi-byte entities,
  57. please be sure to specify both bytes of the field. For example, for
  58. the endpoint size an endpoint that is 64 bytes needs to have the size
  59. defined as "64,0," instead of "64,"
  60. Take the following example:
  61. // Endpoint Descriptor //
  62. 0x07, //the size of this descriptor //
  63. USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
  64. _EP02_IN, //EndpointAddress
  65. _INT, //Attributes
  66. 0x08,0x00, //size (note: 2 bytes)
  67. 0x02, //Interval
  68. The first two parameters are self-explanatory. They specify the
  69. length of this endpoint descriptor (7) and the descriptor type.
  70. The next parameter identifies the endpoint, the definitions are
  71. defined in usb_device.h and has the following naming
  72. convention:
  73. _EP<##>_<dir>
  74. where ## is the endpoint number and dir is the direction of
  75. transfer. The dir has the value of either 'OUT' or 'IN'.
  76. The next parameter identifies the type of the endpoint. Available
  77. options are _BULK, _INT, _ISO, and _CTRL. The _CTRL is not
  78. typically used because the default control transfer endpoint is
  79. not defined in the USB descriptors. When _ISO option is used,
  80. addition options can be ORed to _ISO. Example:
  81. _ISO|_AD|_FE
  82. This describes the endpoint as an isochronous pipe with adaptive
  83. and feedback attributes. See usb_device.h and the USB
  84. specification for details. The next parameter defines the size of
  85. the endpoint. The last parameter in the polling interval.
  86. -------------------------------------------------------------------
  87. Adding a USB String
  88. -------------------------------------------------------------------
  89. A string descriptor array should have the following format:
  90. rom struct{byte bLength;byte bDscType;word string[size];}sdxxx={
  91. sizeof(sdxxx),DSC_STR,<text>};
  92. The above structure provides a means for the C compiler to
  93. calculate the length of string descriptor sdxxx, where xxx is the
  94. index number. The first two bytes of the descriptor are descriptor
  95. length and type. The rest <text> are string texts which must be
  96. in the unicode format. The unicode format is achieved by declaring
  97. each character as a word type. The whole text string is declared
  98. as a word array with the number of characters equals to <size>.
  99. <size> has to be manually counted and entered into the array
  100. declaration. Let's study this through an example:
  101. if the string is "USB" , then the string descriptor should be:
  102. (Using index 02)
  103. rom struct{byte bLength;byte bDscType;word string[3];}sd002={
  104. sizeof(sd002),DSC_STR,'U','S','B'};
  105. A USB project may have multiple strings and the firmware supports
  106. the management of multiple strings through a look-up table.
  107. The look-up table is defined as:
  108. rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
  109. The above declaration has 3 strings, sd000, sd001, and sd002.
  110. Strings can be removed or added. sd000 is a specialized string
  111. descriptor. It defines the language code, usually this is
  112. US English (0x0409). The index of the string must match the index
  113. position of the USB_SD_Ptr array, &sd000 must be in position
  114. USB_SD_Ptr[0], &sd001 must be in position USB_SD_Ptr[1] and so on.
  115. The look-up table USB_SD_Ptr is used by the get string handler
  116. function.
  117. -------------------------------------------------------------------
  118. The look-up table scheme also applies to the configuration
  119. descriptor. A USB device may have multiple configuration
  120. descriptors, i.e. CFG01, CFG02, etc. To add a configuration
  121. descriptor, user must implement a structure similar to CFG01.
  122. The next step is to add the configuration descriptor name, i.e.
  123. cfg01, cfg02,.., to the look-up table USB_CD_Ptr. USB_CD_Ptr[0]
  124. is a dummy place holder since configuration 0 is the un-configured
  125. state according to the definition in the USB specification.
  126. ********************************************************************/
  127. /*********************************************************************
  128. * Descriptor specific type definitions are defined in:
  129. * usb_device.h
  130. *
  131. * Configuration options are defined in:
  132. * usb_config.h
  133. ********************************************************************/
  134. #ifndef __USB_DESCRIPTORS_C
  135. #define __USB_DESCRIPTORS_C
  136. /** INCLUDES *******************************************************/
  137. #include "USB/usb.h"
  138. #include "USB/usb_function_generic.h"
  139. #include "HardwareProfile.h"
  140. #if VENDOR_ID == 1234
  141. #error hardware id not set. see usb_config.h
  142. #endif
  143. /** CONSTANTS ******************************************************/
  144. #if defined(__18CXX)
  145. #pragma romdata
  146. #endif
  147. /* Device Descriptor */
  148. ROM USB_DEVICE_DESCRIPTOR device_dsc=
  149. {
  150. 0x12, // Size of this descriptor in bytes
  151. USB_DESCRIPTOR_DEVICE, // DEVICE descriptor type
  152. 0x0200, // USB Spec Release Number in BCD format
  153. 0x00, // Class Code
  154. 0x00, // Subclass code
  155. 0x00, // Protocol code
  156. USB_EP0_BUFF_SIZE, // Max packet size for EP0, (see usb_config.h)
  157. VENDOR_ID, // Vendor ID (see usb_config.h)
  158. PRODUCT_ID, // Product ID (see usb_config.h)
  159. BCD_RELEASE_NUMBER, // Device release number in BCD format (see usb_config.h)
  160. 0x01, // Manufacturer string index
  161. 0x02, // Product string index
  162. 0x03, // Device serial number string index
  163. 0x01 // Number of possible configurations
  164. };
  165. #if !defined(DUAL_INTERFACE)
  166. /* Configuration 1 Descriptor */
  167. ROM BYTE configDescriptor1[]={
  168. /* Configuration Descriptor */
  169. 0x09,//sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
  170. USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
  171. 0x2E,0x00, // Total length of data for this cfg
  172. 1, // Number of interfaces in this cfg
  173. 1, // Index value of this configuration
  174. 0, // Configuration string index
  175. _DEFAULT | _SELF, // Attributes, see usb_device.h
  176. 50, // Max power consumption (2X mA)
  177. /* Interface Descriptor */
  178. 0x09,//sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
  179. USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type
  180. 0, // Interface Number
  181. 0, // Alternate Setting Number
  182. 4, // Number of endpoints in this intf
  183. 0xFF, // Class code
  184. 0xFF, // Subclass code
  185. 0xFF, // Protocol code
  186. 0, // Interface string index
  187. 0x07, // Endpoint descriptor size
  188. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  189. USBGEN_EP_NUM_INTF0_1, // Endpoint address
  190. USBGEN_EP_ATTRIBUTES_INTF0_1, // Endpoint Attributes
  191. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Endpoint Size
  192. 32, // Endpoint Interval
  193. 0x07, // Endpoint descriptor size
  194. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  195. USBGEN_EP_NUM_INTF0_1|0x80, // Endpoint address
  196. USBGEN_EP_ATTRIBUTES_INTF0_1, // Endpoint Attributes
  197. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Endpoint Size
  198. 32, // Endpoint Interval
  199. 0x07, // Endpoint descriptor size
  200. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  201. USBGEN_EP_NUM_INTF0_2, // Endpoint address
  202. USBGEN_EP_ATTRIBUTES_INTF0_2, // Endpoint Attributes
  203. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Endpoint Size
  204. 32, // Endpoint Interval
  205. 0x07, // Endpoint descriptor size
  206. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  207. USBGEN_EP_NUM_INTF0_2|0x80, // Endpoint address
  208. USBGEN_EP_ATTRIBUTES_INTF0_2, // Endpoint Attributes
  209. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Endpoint Size
  210. 32, // Endpoint Interval
  211. };
  212. #else // Dual interface config
  213. /* Configuration 1 Descriptor */
  214. ROM BYTE configDescriptor1[]={
  215. //// CONFIG ////
  216. 0x09, // Size of this descriptor in bytes
  217. USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
  218. DESC_CONFIG_WORD(0x0037), // Total length of data for this cfg
  219. 2, // Number of interfaces in this cfg
  220. 1, // Index value of this configuration
  221. 0, // Configuration string index
  222. _DEFAULT | _SELF, // Attributes, see usb_device.h
  223. 50, // Max power consumption (2X mA)
  224. //// INTERFACE ////
  225. 0x09, // Interface descriptor size
  226. USB_DESCRIPTOR_INTERFACE, // Interface descriptor
  227. 0, // Interface Number
  228. 0, // Alternate Setting Number
  229. 2, // Number of endpoints in this intf
  230. 0x00, // Class code
  231. 0x00, // Subclass code
  232. 0x00, // Protocol code
  233. 4, // Interface string index
  234. //// ENDPOINT ////
  235. 0x07, // Endpoint descriptor size
  236. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  237. USBGEN_EP_NUM_INTF0, // Endpoint address
  238. USBGEN_EP_ATTRIBUTES_INTF0, // Attributes
  239. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Size
  240. USBGEN_EP_INTERVAL_INTF0, // Interval
  241. //// ENDPOINT ////
  242. 0x07, // Endpoint descriptor size
  243. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  244. USBGEN_EP_NUM_INTF0|0x80, // Endpoint address
  245. USBGEN_EP_ATTRIBUTES_INTF0, // Attributes
  246. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF0), // Size
  247. USBGEN_EP_INTERVAL_INTF0, // Interval
  248. //// INTERFACE ////
  249. 0x09, // Interface descriptor size
  250. USB_DESCRIPTOR_INTERFACE, // Interface descriptor
  251. 1, // Interface Number
  252. 0, // Alternate Setting Number
  253. 2, // Number of endpoints in this intf
  254. 0x00, // Class code
  255. 0x00, // Subclass code
  256. 0x00, // Protocol code
  257. 5, // Interface string index
  258. //// ENDPOINT ////
  259. 0x07, // Endpoint descriptor size
  260. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  261. USBGEN_EP_NUM_INTF1, // Endpoint address
  262. USBGEN_EP_ATTRIBUTES_INTF1, // Attributes
  263. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF1), // Size
  264. USBGEN_EP_INTERVAL_INTF1, // Interval
  265. //// ENDPOINT ////
  266. 0x07, // Endpoint descriptor size
  267. USB_DESCRIPTOR_ENDPOINT, // Endpoint descriptor
  268. USBGEN_EP_NUM_INTF1|0x80, // Endpoint address
  269. USBGEN_EP_ATTRIBUTES_INTF1, // Attributes
  270. DESC_CONFIG_WORD(USBGEN_EP_SIZE_INTF1), // Size
  271. USBGEN_EP_INTERVAL_INTF1, // Interval
  272. };
  273. // Interface #0 String
  274. ROM struct
  275. {
  276. BYTE bLength;
  277. BYTE bDscType;
  278. WORD string[INTF0_STRING_LENGTH];
  279. } sd4={sizeof(sd4),USB_DESCRIPTOR_STRING, {INTF0_STRING}};
  280. // Interface #1 String
  281. ROM struct
  282. {
  283. BYTE bLength;
  284. BYTE bDscType;
  285. WORD string[INTF1_STRING_LENGTH];
  286. } sd5={sizeof(sd5),USB_DESCRIPTOR_STRING, {INTF1_STRING}};
  287. #endif
  288. //Array of configuration descriptors
  289. ROM BYTE *ROM USB_CD_Ptr[]=
  290. {
  291. (ROM BYTE *ROM)&configDescriptor1
  292. };
  293. // Language Code
  294. ROM struct
  295. {
  296. BYTE bLength;
  297. BYTE bDscType;
  298. WORD string[1];
  299. } sd000={sizeof(sd000),USB_DESCRIPTOR_STRING,{0x0409}};
  300. // Manufacturer String
  301. ROM struct
  302. {
  303. BYTE bLength;
  304. BYTE bDscType;
  305. WORD string[MANUFACTURER_STRING_LENGTH];
  306. } sd1={sizeof(sd1),USB_DESCRIPTOR_STRING, {MANUFACTURER_STRING}};
  307. // Product String
  308. ROM struct
  309. {
  310. BYTE bLength;
  311. BYTE bDscType;
  312. WORD string[PRODUCT_STRING_LENGTH];
  313. } sd2={sizeof(sd2),USB_DESCRIPTOR_STRING, {PRODUCT_STRING}};
  314. // Serial Number String
  315. ROM struct
  316. {
  317. BYTE bLength;
  318. BYTE bDscType;
  319. WORD string[SERIAL_NUMBER_LENGTH];
  320. } sd3={sizeof(sd3),USB_DESCRIPTOR_STRING, {SERIAL_NUMBER}};
  321. //Array of string descriptors
  322. ROM BYTE *ROM USB_SD_Ptr[]=
  323. {
  324. (ROM BYTE *ROM)&sd000,
  325. (ROM BYTE *ROM)&sd1,
  326. (ROM BYTE *ROM)&sd2,
  327. (ROM BYTE *ROM)&sd3,
  328. #if defined(DUAL_INTERFACE)
  329. (ROM BYTE *ROM)&sd4,
  330. (ROM BYTE *ROM)&sd5,
  331. #endif
  332. };
  333. #endif
  334. /** EOF usb_descriptors.c ***************************************************/