cdc_desc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /**
  2. **************************************************************************
  3. * @file cdc_desc.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb cdc device descriptor
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. #include "usb_std.h"
  27. #include "usbd_sdr.h"
  28. #include "usbd_core.h"
  29. #include "cdc_desc.h"
  30. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  31. * @{
  32. */
  33. /** @defgroup USB_cdc_desc
  34. * @brief usb device cdc descriptor
  35. * @{
  36. */
  37. /** @defgroup USB_cdc_desc_private_functions
  38. * @{
  39. */
  40. usbd_desc_t *get_device_descriptor(void);
  41. usbd_desc_t *get_device_qualifier(void);
  42. usbd_desc_t *get_device_configuration(void);
  43. usbd_desc_t *get_device_other_speed(void);
  44. usbd_desc_t *get_device_lang_id(void);
  45. usbd_desc_t *get_device_manufacturer_string(void);
  46. usbd_desc_t *get_device_product_string(void);
  47. usbd_desc_t *get_device_serial_string(void);
  48. usbd_desc_t *get_device_interface_string(void);
  49. usbd_desc_t *get_device_config_string(void);
  50. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
  51. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  52. static void get_serial_num(void);
  53. static uint8_t g_usbd_desc_buffer[256];
  54. /**
  55. * @brief device descriptor handler structure
  56. */
  57. usbd_desc_handler desc_handler =
  58. {
  59. get_device_descriptor,
  60. get_device_qualifier,
  61. get_device_configuration,
  62. get_device_other_speed,
  63. get_device_lang_id,
  64. get_device_manufacturer_string,
  65. get_device_product_string,
  66. get_device_serial_string,
  67. get_device_interface_string,
  68. get_device_config_string,
  69. };
  70. /**
  71. * @brief usb device standard descriptor
  72. */
  73. #if defined ( __ICCARM__ ) /* iar compiler */
  74. #pragma data_alignment=4
  75. #endif
  76. ALIGNED_HEAD uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
  77. {
  78. USB_DEVICE_DESC_LEN, /* bLength */
  79. USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
  80. 0x00, /* bcdUSB */
  81. 0x02,
  82. 0x02, /* bDeviceClass */
  83. 0x00, /* bDeviceSubClass */
  84. 0x00, /* bDeviceProtocol */
  85. USB_MAX_EP0_SIZE, /* bMaxPacketSize */
  86. LBYTE(USBD_VENDOR_ID), /* idVendor */
  87. HBYTE(USBD_VENDOR_ID), /* idVendor */
  88. LBYTE(USBD_PRODUCT_ID), /* idProduct */
  89. HBYTE(USBD_PRODUCT_ID), /* idProduct */
  90. 0x00, /* bcdDevice rel. 2.00 */
  91. 0x02,
  92. USB_MFC_STRING, /* Index of manufacturer string */
  93. USB_PRODUCT_STRING, /* Index of product string */
  94. USB_SERIAL_STRING, /* Index of serial number string */
  95. 1 /* bNumConfigurations */
  96. };
  97. /**
  98. * @brief usb configuration standard descriptor
  99. */
  100. #if defined ( __ICCARM__ ) /* iar compiler */
  101. #pragma data_alignment=4
  102. #endif
  103. ALIGNED_HEAD uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] ALIGNED_TAIL =
  104. {
  105. USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
  106. USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
  107. LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
  108. HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
  109. 0x02, /* bNumInterfaces: 2 interface */
  110. 0x01, /* bConfigurationValue: configuration value */
  111. 0x00, /* iConfiguration: index of string descriptor describing
  112. the configuration */
  113. 0xC0, /* bmAttributes: self powered */
  114. 0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
  115. USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
  116. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  117. 0x00, /* bInterfaceNumber: number of interface */
  118. 0x00, /* bAlternateSetting: alternate set */
  119. 0x01, /* bNumEndpoints: number of endpoints */
  120. USB_CLASS_CODE_CDC, /* bInterfaceClass: CDC class code */
  121. 0x02, /* bInterfaceSubClass: subclass code, Abstract Control Model*/
  122. 0x01, /* bInterfaceProtocol: protocol code, AT Command */
  123. 0x00, /* iInterface: index of string descriptor */
  124. 0x05, /* bFunctionLength: size of this descriptor in bytes */
  125. USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
  126. USBD_CDC_SUBTYPE_HEADER, /* bDescriptorSubtype: Header function Descriptor 0x00*/
  127. LBYTE(BCD_NUM),
  128. HBYTE(BCD_NUM), /* bcdCDC: USB class definitions for communications */
  129. 0x05, /* bFunctionLength: size of this descriptor in bytes */
  130. USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
  131. USBD_CDC_SUBTYPE_CMF, /* bDescriptorSubtype: Call Management function descriptor subtype 0x01 */
  132. 0x00, /* bmCapabilities: 0x00*/
  133. 0x01, /* bDataInterface: interface number of data class interface optionally used for call management */
  134. 0x04, /* bFunctionLength: size of this descriptor in bytes */
  135. USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
  136. USBD_CDC_SUBTYPE_ACM, /* bDescriptorSubtype: Abstract Control Management functional descriptor subtype 0x02 */
  137. 0x02, /* bmCapabilities: Support Set_Line_Coding and Get_Line_Coding 0x02 */
  138. 0x05, /* bFunctionLength: size of this descriptor in bytes */
  139. USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
  140. USBD_CDC_SUBTYPE_UFD, /* bDescriptorSubtype: Union Function Descriptor subtype 0x06 */
  141. 0x00, /* bControlInterface: The interface number of the communications or data class interface 0x00 */
  142. 0x01, /* bSubordinateInterface0: interface number of first subordinate interface in the union */
  143. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  144. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  145. USBD_CDC_INT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  146. USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
  147. LBYTE(USBD_CMD_MAXPACKET_SIZE),
  148. HBYTE(USBD_CMD_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  149. HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
  150. USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
  151. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  152. 0x01, /* bInterfaceNumber: number of interface */
  153. 0x00, /* bAlternateSetting: alternate set */
  154. 0x02, /* bNumEndpoints: number of endpoints */
  155. USB_CLASS_CODE_CDCDATA, /* bInterfaceClass: CDC-data class code */
  156. 0x00, /* bInterfaceSubClass: Data interface subclass code 0x00*/
  157. 0x00, /* bInterfaceProtocol: data class protocol code 0x00 */
  158. 0x00, /* iInterface: index of string descriptor */
  159. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  160. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  161. USBD_CDC_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  162. USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
  163. LBYTE(USBD_IN_MAXPACKET_SIZE),
  164. HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  165. 0x00, /* bInterval: interval for polling endpoint for data transfers */
  166. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  167. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  168. USBD_CDC_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  169. USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
  170. LBYTE(USBD_OUT_MAXPACKET_SIZE),
  171. HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  172. 0x00, /* bInterval: interval for polling endpoint for data transfers */
  173. };
  174. /**
  175. * @brief usb string lang id
  176. */
  177. #if defined ( __ICCARM__ ) /* iar compiler */
  178. #pragma data_alignment=4
  179. #endif
  180. ALIGNED_HEAD uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] ALIGNED_TAIL =
  181. {
  182. USBD_SIZ_STRING_LANGID,
  183. USB_DESCIPTOR_TYPE_STRING,
  184. 0x09,
  185. 0x04,
  186. };
  187. /**
  188. * @brief usb string serial
  189. */
  190. #if defined ( __ICCARM__ ) /* iar compiler */
  191. #pragma data_alignment=4
  192. #endif
  193. ALIGNED_HEAD uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] ALIGNED_TAIL =
  194. {
  195. USBD_SIZ_STRING_SERIAL,
  196. USB_DESCIPTOR_TYPE_STRING,
  197. };
  198. /* device descriptor */
  199. usbd_desc_t device_descriptor =
  200. {
  201. USB_DEVICE_DESC_LEN,
  202. g_usbd_descriptor
  203. };
  204. /* config descriptor */
  205. usbd_desc_t config_descriptor =
  206. {
  207. USBD_CONFIG_DESC_SIZE,
  208. g_usbd_configuration
  209. };
  210. /* langid descriptor */
  211. usbd_desc_t langid_descriptor =
  212. {
  213. USBD_SIZ_STRING_LANGID,
  214. g_string_lang_id
  215. };
  216. /* serial descriptor */
  217. usbd_desc_t serial_descriptor =
  218. {
  219. USBD_SIZ_STRING_SERIAL,
  220. g_string_serial
  221. };
  222. usbd_desc_t vp_desc;
  223. /**
  224. * @brief standard usb unicode convert
  225. * @param string: source string
  226. * @param unicode_buf: unicode buffer
  227. * @retval length
  228. */
  229. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  230. {
  231. uint16_t str_len = 0, id_pos = 2;
  232. uint8_t *tmp_str = string;
  233. while(*tmp_str != '\0')
  234. {
  235. str_len ++;
  236. unicode_buf[id_pos ++] = *tmp_str ++;
  237. unicode_buf[id_pos ++] = 0x00;
  238. }
  239. str_len = str_len * 2 + 2;
  240. unicode_buf[0] = str_len;
  241. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  242. return str_len;
  243. }
  244. /**
  245. * @brief usb int convert to unicode
  246. * @param value: int value
  247. * @param pbus: unicode buffer
  248. * @param len: length
  249. * @retval none
  250. */
  251. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  252. {
  253. uint8_t idx = 0;
  254. for( idx = 0 ; idx < len ; idx ++)
  255. {
  256. if( ((value >> 28)) < 0xA )
  257. {
  258. pbuf[ 2 * idx] = (value >> 28) + '0';
  259. }
  260. else
  261. {
  262. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  263. }
  264. value = value << 4;
  265. pbuf[2 * idx + 1] = 0;
  266. }
  267. }
  268. /**
  269. * @brief usb get serial number
  270. * @param none
  271. * @retval none
  272. */
  273. static void get_serial_num(void)
  274. {
  275. uint32_t serial0, serial1, serial2;
  276. serial0 = *(uint32_t*)MCU_ID1;
  277. serial1 = *(uint32_t*)MCU_ID2;
  278. serial2 = *(uint32_t*)MCU_ID3;
  279. serial0 += serial2;
  280. if (serial0 != 0)
  281. {
  282. usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
  283. usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
  284. }
  285. }
  286. /**
  287. * @brief get device descriptor
  288. * @param none
  289. * @retval usbd_desc
  290. */
  291. usbd_desc_t *get_device_descriptor(void)
  292. {
  293. return &device_descriptor;
  294. }
  295. /**
  296. * @brief get device qualifier
  297. * @param none
  298. * @retval usbd_desc
  299. */
  300. usbd_desc_t * get_device_qualifier(void)
  301. {
  302. return NULL;
  303. }
  304. /**
  305. * @brief get config descriptor
  306. * @param none
  307. * @retval usbd_desc
  308. */
  309. usbd_desc_t *get_device_configuration(void)
  310. {
  311. return &config_descriptor;
  312. }
  313. /**
  314. * @brief get other speed descriptor
  315. * @param none
  316. * @retval usbd_desc
  317. */
  318. usbd_desc_t *get_device_other_speed(void)
  319. {
  320. return NULL;
  321. }
  322. /**
  323. * @brief get lang id descriptor
  324. * @param none
  325. * @retval usbd_desc
  326. */
  327. usbd_desc_t *get_device_lang_id(void)
  328. {
  329. return &langid_descriptor;
  330. }
  331. /**
  332. * @brief get manufacturer descriptor
  333. * @param none
  334. * @retval usbd_desc
  335. */
  336. usbd_desc_t *get_device_manufacturer_string(void)
  337. {
  338. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
  339. vp_desc.descriptor = g_usbd_desc_buffer;
  340. return &vp_desc;
  341. }
  342. /**
  343. * @brief get product descriptor
  344. * @param none
  345. * @retval usbd_desc
  346. */
  347. usbd_desc_t *get_device_product_string(void)
  348. {
  349. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
  350. vp_desc.descriptor = g_usbd_desc_buffer;
  351. return &vp_desc;
  352. }
  353. /**
  354. * @brief get serial descriptor
  355. * @param none
  356. * @retval usbd_desc
  357. */
  358. usbd_desc_t *get_device_serial_string(void)
  359. {
  360. get_serial_num();
  361. return &serial_descriptor;
  362. }
  363. /**
  364. * @brief get interface descriptor
  365. * @param none
  366. * @retval usbd_desc
  367. */
  368. usbd_desc_t *get_device_interface_string(void)
  369. {
  370. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
  371. vp_desc.descriptor = g_usbd_desc_buffer;
  372. return &vp_desc;
  373. }
  374. /**
  375. * @brief get device config descriptor
  376. * @param none
  377. * @retval usbd_desc
  378. */
  379. usbd_desc_t *get_device_config_string(void)
  380. {
  381. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
  382. vp_desc.descriptor = g_usbd_desc_buffer;
  383. return &vp_desc;
  384. }
  385. /**
  386. * @}
  387. */
  388. /**
  389. * @}
  390. */
  391. /**
  392. * @}
  393. */