audio_desc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /**
  2. **************************************************************************
  3. * @file audio_desc.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb audio 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 "audio_desc.h"
  30. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  31. * @{
  32. */
  33. /** @defgroup USB_audio_desc
  34. * @brief usb device audio descriptor
  35. * @{
  36. */
  37. /** @defgroup USB_audio_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 audio_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. 0x00, /* 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. 0x01 /* 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. 0x1 + AUDIO_INTERFACE_NUM, /* bNumInterfaces: n 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. 0x00, /* bNumEndpoints: number of endpoints */
  120. USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio class code */
  121. AUDIO_SUBCLASS_AUDIOCONTROL, /* bInterfaceSubClass: audio control */
  122. AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol: undefined */
  123. 0x00, /* iInterface: index of string descriptor */
  124. 0x08+AUDIO_INTERFACE_NUM, /* bLength: size of this descriptor, in bytes 8+n */
  125. AUDIO_CS_INTERFACE, /* bDescriptorType: cs interface descriptor type */
  126. AUDIO_AC_HEADER, /* bDescriptorSubtype: Header function Descriptor*/
  127. LBYTE(BCD_NUM),
  128. HBYTE(BCD_NUM), /* bcdCDC: audio device class specification release number */
  129. LBYTE(AUDIO_INTERFACE_LEN),
  130. HBYTE(AUDIO_INTERFACE_LEN), /* wTotalLength: total number of bytes returned for the class-specific audio control interface */
  131. AUDIO_INTERFACE_NUM, /* bInClollection: the number of audio streaming */
  132. #if (AUDIO_INTERFACE_NUM == 2) /* two interface */
  133. 0x02,
  134. 0x01,
  135. #else
  136. 0x01,
  137. #endif
  138. /* usb microphone config */
  139. #if (AUDIO_SUPPORT_MIC == 1)
  140. AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
  141. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  142. AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
  143. AUDIO_MIC_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
  144. LBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),
  145. HBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),/* wTerminalType: terminal is microphone */
  146. 0x00, /* bAssocTerminal: no association */
  147. AUDIO_MIC_CHR, /* bNrChannels: two channel */
  148. #if (AUDIO_MIC_CHR == 2)
  149. 0x03, /* wChannelConfig: left front and right front */
  150. #endif
  151. #if (AUDIO_MIC_CHR == 1)
  152. 0x00, /* wChannelConfig */
  153. #endif
  154. 0x00, /* wChannelConfig */
  155. 0x00, /* iChannelNames: unused */
  156. 0x00, /* iTerminal: unused */
  157. AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
  158. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  159. AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
  160. AUDIO_MIC_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
  161. AUDIO_MIC_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
  162. 0x01, /* bControlSize: 1 byte */
  163. 0x01, /* bmaControls0: mute */
  164. 0x02, /* bmaControls1: volume */
  165. 0x00, /* iFeature: unused */
  166. AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
  167. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  168. AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
  169. AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
  170. LBYTE(AUDIO_TERMINAL_TYPE_STREAMING),
  171. HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
  172. 0x00, /* bAssocTerminal: unused */
  173. AUDIO_MIC_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
  174. 0x00, /* iTerminal: unused */
  175. #endif
  176. #if (AUDIO_SUPPORT_SPK == 1)
  177. /* speaker config */
  178. AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
  179. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  180. AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
  181. AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
  182. LBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
  183. HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
  184. 0x00, /* bAssocTerminal: no association */
  185. AUDIO_SPK_CHR, /* bNrChannels: two channel */
  186. #if (AUDIO_SPK_CHR == 2)
  187. 0x03, /* wChannelConfig: left front and right front */
  188. #endif
  189. #if (AUDIO_SPK_CHR == 1)
  190. 0x00, /* wChannelConfig */
  191. #endif
  192. 0x00, /* wChannelConfig */
  193. 0x00, /* iChannelNames: unused */
  194. 0x00, /* iTerminal: unused */
  195. AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
  196. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  197. AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
  198. AUDIO_SPK_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
  199. AUDIO_SPK_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
  200. 0x01, /* bControlSize: 1 byte */
  201. 0x01, /* bmaControls0: mute*/
  202. 0x02, /* bmaControls1: volume */
  203. 0x00, /* iFeature: unused */
  204. AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
  205. AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
  206. AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
  207. AUDIO_SPK_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
  208. LBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
  209. HBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
  210. 0x00, /* bAssocTerminal: unused */
  211. AUDIO_SPK_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
  212. 0x00, /* iTerminal: unused */
  213. #endif
  214. #if (AUDIO_SUPPORT_MIC == 1)
  215. /* microphone interface */
  216. 0x09, /* bLength: descriptor size */
  217. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  218. AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
  219. 0x00, /* bAlternateSetting: index of this setting */
  220. 0x00, /* bNumEndpoints: 0 endpoints */
  221. USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
  222. AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
  223. 0x00, /* bInterfaceProtocol: unused */
  224. 0x00, /* iInterface: unused */
  225. 0x09, /* bLength: descriptor size */
  226. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  227. AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
  228. 0x01, /* bAlternateSetting: index of this setting */
  229. 0x01, /* bNumEndpoints: 1 endpoints */
  230. USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
  231. AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
  232. 0x00, /* bInterfaceProtocol: unused */
  233. 0x00, /* iInterface: unused */
  234. 0x07, /* bLength: configuration descriptor size */
  235. AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
  236. AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
  237. AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalLink: unit id of the output terminal */
  238. 0x01, /* bDelay: interface delay */
  239. 0x01, /* wFormatTag: pcm format*/
  240. 0x00, /* wFormatTag: pcm format*/
  241. 0x08 + AUDIO_MIC_FREQ_SIZE * 3, /* bLength: descriptor size */
  242. AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
  243. AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
  244. AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
  245. AUDIO_MIC_CHR, /* bNrChannels: channel number */
  246. AUDIO_MIC_BITW / 8, /* bSubFrameSize: per audio subframe */
  247. AUDIO_MIC_BITW, /* bBitResolution: n bits per sample */
  248. AUDIO_MIC_FREQ_SIZE, /* bSamFreqType: n frequency supported */
  249. #if (AUDIO_SUPPORT_FREQ_16K == 1)
  250. SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
  251. #endif
  252. #if (AUDIO_SUPPORT_FREQ_48K == 1)
  253. SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
  254. #endif
  255. 0x09, /* bLength: size of endpoint descriptor in bytes */
  256. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  257. USBD_AUDIO_MIC_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  258. USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
  259. LBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE),
  260. HBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  261. HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
  262. 0x00, /* bRefresh: unused */
  263. 0x00, /* bSynchAddress: unused */
  264. 0x07, /* bLength: size of endpoint descriptor in bytes */
  265. AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
  266. 0x01, /* bDescriptorSubtype: general subtype */
  267. 0x01, /* bmAttributes */
  268. 0x00, /* bLockDelayUnits: unused */
  269. 0x00, /* wLockDelay: unused */
  270. 0x00, /* wLockDelay: unused */
  271. #endif
  272. #if (AUDIO_SUPPORT_SPK == 1)
  273. /* speaker interface */
  274. 0x09, /* bLength: descriptor size */
  275. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  276. AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
  277. 0x00, /* bAlternateSetting: index of this setting */
  278. 0x00, /* bNumEndpoints: 0 endpoints */
  279. USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
  280. AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
  281. 0x00, /* bInterfaceProtocol: unused */
  282. 0x00, /* iInterface: unused */
  283. 0x09, /* bLength: descriptor size */
  284. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  285. AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
  286. 0x01, /* bAlternateSetting: index of this setting */
  287. 0x01 + AUDIO_SUPPORT_FEEDBACK, /* bNumEndpoints: endpoints */
  288. USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
  289. AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
  290. 0x00, /* bInterfaceProtocol: unused */
  291. 0x00, /* iInterface: unused */
  292. 0x07, /* bLength: configuration descriptor size */
  293. AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
  294. AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
  295. AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalLink: unit id of the input terminal */
  296. 0x01, /* bDelay: interface delay */
  297. 0x01, /* wFormatTag: pcm format*/
  298. 0x00, /* wFormatTag: pcm format*/
  299. 0x08 + AUDIO_SPK_FREQ_SIZE * 3, /* bLength: descriptor size */
  300. AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
  301. AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
  302. AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
  303. AUDIO_SPK_CHR, /* bNrChannels: channel number */
  304. AUDIO_SPK_BITW / 8, /* bSubFrameSize: per audio subframe */
  305. AUDIO_SPK_BITW, /* bBitResolution: n bits per sample */
  306. AUDIO_SPK_FREQ_SIZE, /* bSamFreqType: n frequency supported */
  307. #if (AUDIO_SUPPORT_FREQ_16K == 1)
  308. SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
  309. #endif
  310. #if (AUDIO_SUPPORT_FREQ_48K == 1)
  311. SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
  312. #endif
  313. 0x09, /* bLength: size of endpoint descriptor in bytes */
  314. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  315. USBD_AUDIO_SPK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  316. USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
  317. LBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE),
  318. HBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  319. HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
  320. 0x00, /* bRefresh: unused */
  321. #if (AUDIO_SUPPORT_FEEDBACK == 1)
  322. USBD_AUDIO_FEEDBACK_EPT, /* bSynchAddress: feedback endpoint */
  323. #else
  324. 0x00, /* bSynchAddress: unused */
  325. #endif
  326. 0x07, /* bLength: size of endpoint descriptor in bytes */
  327. AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
  328. 0x01, /* bDescriptorSubtype: general subtype */
  329. 0x01, /* bmAttributes */
  330. 0x00, /* bLockDelayUnits: unused */
  331. 0x00, /* wLockDelay: unused */
  332. 0x00, /* wLockDelay: unused */
  333. #if (AUDIO_SUPPORT_FEEDBACK == 1)
  334. 0x09, /* bLength: size of endpoint descriptor in bytes */
  335. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  336. USBD_AUDIO_FEEDBACK_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  337. 0x11, /* bmAttributes: endpoint attributes */
  338. LBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  339. HBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  340. 1, /* bInterval: interval for polling endpoint for data transfers */
  341. FEEDBACK_REFRESH_TIME, /* bRefresh: this field indicates the rate at which an iso syncronization
  342. pipe provides new syncronization feedback data. this rate must be a power of
  343. 2, therefore only the power is reported back and the range of this field is from
  344. 1(2ms) to 9(512ms) */
  345. 0x00 /* bSynchAddress: 0x00*/
  346. #endif
  347. #endif
  348. };
  349. /**
  350. * @brief usb string lang id
  351. */
  352. #if defined ( __ICCARM__ ) /* iar compiler */
  353. #pragma data_alignment=4
  354. #endif
  355. ALIGNED_HEAD uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] ALIGNED_TAIL =
  356. {
  357. USBD_SIZ_STRING_LANGID,
  358. USB_DESCIPTOR_TYPE_STRING,
  359. 0x09,
  360. 0x04,
  361. };
  362. /**
  363. * @brief usb string serial
  364. */
  365. #if defined ( __ICCARM__ ) /* iar compiler */
  366. #pragma data_alignment=4
  367. #endif
  368. ALIGNED_HEAD uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] ALIGNED_TAIL =
  369. {
  370. USBD_SIZ_STRING_SERIAL,
  371. USB_DESCIPTOR_TYPE_STRING,
  372. };
  373. /* device descriptor */
  374. usbd_desc_t device_descriptor =
  375. {
  376. USB_DEVICE_DESC_LEN,
  377. g_usbd_descriptor
  378. };
  379. /* config descriptor */
  380. usbd_desc_t config_descriptor =
  381. {
  382. USBD_CONFIG_DESC_SIZE,
  383. g_usbd_configuration
  384. };
  385. /* langid descriptor */
  386. usbd_desc_t langid_descriptor =
  387. {
  388. USBD_SIZ_STRING_LANGID,
  389. g_string_lang_id
  390. };
  391. /* serial descriptor */
  392. usbd_desc_t serial_descriptor =
  393. {
  394. USBD_SIZ_STRING_SERIAL,
  395. g_string_serial
  396. };
  397. usbd_desc_t vp_desc;
  398. /**
  399. * @brief standard usb unicode convert
  400. * @param string: source string
  401. * @param unicode_buf: unicode buffer
  402. * @retval length
  403. */
  404. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  405. {
  406. uint16_t str_len = 0, id_pos = 2;
  407. uint8_t *tmp_str = string;
  408. while(*tmp_str != '\0')
  409. {
  410. str_len ++;
  411. unicode_buf[id_pos ++] = *tmp_str ++;
  412. unicode_buf[id_pos ++] = 0x00;
  413. }
  414. str_len = str_len * 2 + 2;
  415. unicode_buf[0] = str_len;
  416. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  417. return str_len;
  418. }
  419. /**
  420. * @brief usb int convert to unicode
  421. * @param value: int value
  422. * @param pbus: unicode buffer
  423. * @param len: length
  424. * @retval none
  425. */
  426. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  427. {
  428. uint8_t idx = 0;
  429. for( idx = 0 ; idx < len ; idx ++)
  430. {
  431. if( ((value >> 28)) < 0xA )
  432. {
  433. pbuf[2 * idx] = (value >> 28) + '0';
  434. }
  435. else
  436. {
  437. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  438. }
  439. value = value << 4;
  440. pbuf[2 * idx + 1] = 0;
  441. }
  442. }
  443. /**
  444. * @brief usb get serial number
  445. * @param none
  446. * @retval none
  447. */
  448. static void get_serial_num(void)
  449. {
  450. uint32_t serial0, serial1, serial2;
  451. serial0 = *(uint32_t*)MCU_ID1;
  452. serial1 = *(uint32_t*)MCU_ID2;
  453. serial2 = *(uint32_t*)MCU_ID3;
  454. serial0 += serial2;
  455. if (serial0 != 0)
  456. {
  457. usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
  458. usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
  459. }
  460. }
  461. /**
  462. * @brief get device descriptor
  463. * @param none
  464. * @retval usbd_desc
  465. */
  466. usbd_desc_t *get_device_descriptor(void)
  467. {
  468. return &device_descriptor;
  469. }
  470. /**
  471. * @brief get device qualifier
  472. * @param none
  473. * @retval usbd_desc
  474. */
  475. usbd_desc_t * get_device_qualifier(void)
  476. {
  477. return NULL;
  478. }
  479. /**
  480. * @brief get config descriptor
  481. * @param none
  482. * @retval usbd_desc
  483. */
  484. usbd_desc_t *get_device_configuration(void)
  485. {
  486. return &config_descriptor;
  487. }
  488. /**
  489. * @brief get other speed descriptor
  490. * @param none
  491. * @retval usbd_desc
  492. */
  493. usbd_desc_t *get_device_other_speed(void)
  494. {
  495. return NULL;
  496. }
  497. /**
  498. * @brief get lang id descriptor
  499. * @param none
  500. * @retval usbd_desc
  501. */
  502. usbd_desc_t *get_device_lang_id(void)
  503. {
  504. return &langid_descriptor;
  505. }
  506. /**
  507. * @brief get manufacturer descriptor
  508. * @param none
  509. * @retval usbd_desc
  510. */
  511. usbd_desc_t *get_device_manufacturer_string(void)
  512. {
  513. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
  514. vp_desc.descriptor = g_usbd_desc_buffer;
  515. return &vp_desc;
  516. }
  517. /**
  518. * @brief get product descriptor
  519. * @param none
  520. * @retval usbd_desc
  521. */
  522. usbd_desc_t *get_device_product_string(void)
  523. {
  524. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
  525. vp_desc.descriptor = g_usbd_desc_buffer;
  526. return &vp_desc;
  527. }
  528. /**
  529. * @brief get serial descriptor
  530. * @param none
  531. * @retval usbd_desc
  532. */
  533. usbd_desc_t *get_device_serial_string(void)
  534. {
  535. get_serial_num();
  536. return &serial_descriptor;
  537. }
  538. /**
  539. * @brief get interface descriptor
  540. * @param none
  541. * @retval usbd_desc
  542. */
  543. usbd_desc_t *get_device_interface_string(void)
  544. {
  545. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
  546. vp_desc.descriptor = g_usbd_desc_buffer;
  547. return &vp_desc;
  548. }
  549. /**
  550. * @brief get device config descriptor
  551. * @param none
  552. * @retval usbd_desc
  553. */
  554. usbd_desc_t *get_device_config_string(void)
  555. {
  556. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
  557. vp_desc.descriptor = g_usbd_desc_buffer;
  558. return &vp_desc;
  559. }
  560. /**
  561. * @}
  562. */
  563. /**
  564. * @}
  565. */
  566. /**
  567. * @}
  568. */