mouse_desc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. **************************************************************************
  3. * @file mouse_desc.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb hid mouse 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 "mouse_desc.h"
  30. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  31. * @{
  32. */
  33. /** @defgroup USB_mouse_desc
  34. * @brief usb device mouse descriptor
  35. * @{
  36. */
  37. /** @defgroup USB_mouse_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 mouse device descriptor handler structure
  56. */
  57. usbd_desc_handler mouse_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. 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. 0x01, /* bNumInterfaces: 1 interface */
  110. 0x01, /* bConfigurationValue: configuration value */
  111. 0x00, /* iConfiguration: index of string descriptor describing
  112. the configuration */
  113. 0xE0, /* bmAttributes: self powered ans support remote wakeup*/
  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_HID, /* bInterfaceClass: class code hid */
  121. 0x01, /* bInterfaceSubClass: subclass code */
  122. 0x02, /* bInterfaceProtocol: protocol code */
  123. 0x00, /* iInterface: index of string descriptor */
  124. 0x09, /* bLength: size of HID descriptor in bytes */
  125. HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
  126. LBYTE(HID_BCD_NUM),
  127. HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
  128. 0x00, /* bCountryCode: hardware target conutry */
  129. 0x01, /* bNumDescriptors: number of HID class descriptor to follow */
  130. HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
  131. LBYTE(sizeof(g_usbd_hid_report)),
  132. HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
  133. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  134. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  135. USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  136. USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
  137. LBYTE(USBD_IN_MAXPACKET_SIZE),
  138. HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  139. HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
  140. };
  141. /**
  142. * @brief usb mouse report descriptor
  143. */
  144. #if defined ( __ICCARM__ ) /* iar compiler */
  145. #pragma data_alignment=4
  146. #endif
  147. ALIGNED_HEAD uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] ALIGNED_TAIL =
  148. {
  149. 0x05,0x01,
  150. 0x09,0x02,
  151. 0xA1,0x01,
  152. 0x09,0x01,
  153. 0xA1,0x00,
  154. 0x05,0x09,
  155. 0x19,0x01,
  156. 0x29,0x03,
  157. 0x15,0x00,
  158. 0x25,0x01,
  159. 0x95,0x03,
  160. 0x75,0x01,
  161. 0x81,0x02,
  162. 0x95,0x01,
  163. 0x75,0x05,
  164. 0x81,0x01,
  165. 0x05,0x01,
  166. 0x09,0x30,
  167. 0x09,0x31,
  168. 0x09,0x38,
  169. 0x15,0x81,
  170. 0x25,0x7F,
  171. 0x75,0x08,
  172. 0x95,0x03,
  173. 0x81,0x06,
  174. 0xC0,0x09,
  175. 0x3c,0x05,
  176. 0xff,0x09,
  177. 0x01,0x15,
  178. 0x00,0x25,
  179. 0x01,0x75,
  180. 0x01,0x95,
  181. 0x02,0xb1,
  182. 0x22,0x75,
  183. 0x06,0x95,
  184. 0x01,0xb1,
  185. 0x01,0xc0
  186. };
  187. /**
  188. * @brief usb hid descriptor
  189. */
  190. #if defined ( __ICCARM__ ) /* iar compiler */
  191. #pragma data_alignment=4
  192. #endif
  193. ALIGNED_HEAD uint8_t g_hid_usb_desc[9] ALIGNED_TAIL =
  194. {
  195. 0x09, /* bLength: size of HID descriptor in bytes */
  196. HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
  197. LBYTE(HID_BCD_NUM),
  198. HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
  199. 0x00, /* bCountryCode: hardware target conutry */
  200. 0x01, /* bNumDescriptors: number of HID class descriptor to follow */
  201. HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
  202. LBYTE(sizeof(g_usbd_hid_report)),
  203. HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
  204. };
  205. /**
  206. * @brief usb string lang id
  207. */
  208. #if defined ( __ICCARM__ ) /* iar compiler */
  209. #pragma data_alignment=4
  210. #endif
  211. ALIGNED_HEAD uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] ALIGNED_TAIL =
  212. {
  213. USBD_SIZ_STRING_LANGID,
  214. USB_DESCIPTOR_TYPE_STRING,
  215. 0x09,
  216. 0x04,
  217. };
  218. /**
  219. * @brief usb string serial
  220. */
  221. #if defined ( __ICCARM__ ) /* iar compiler */
  222. #pragma data_alignment=4
  223. #endif
  224. ALIGNED_HEAD uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] ALIGNED_TAIL =
  225. {
  226. USBD_SIZ_STRING_SERIAL,
  227. USB_DESCIPTOR_TYPE_STRING,
  228. };
  229. /* device descriptor */
  230. usbd_desc_t device_descriptor =
  231. {
  232. USB_DEVICE_DESC_LEN,
  233. g_usbd_descriptor
  234. };
  235. /* config descriptor */
  236. usbd_desc_t config_descriptor =
  237. {
  238. USBD_CONFIG_DESC_SIZE,
  239. g_usbd_configuration
  240. };
  241. /* langid descriptor */
  242. usbd_desc_t langid_descriptor =
  243. {
  244. USBD_SIZ_STRING_LANGID,
  245. g_string_lang_id
  246. };
  247. /* serial descriptor */
  248. usbd_desc_t serial_descriptor =
  249. {
  250. USBD_SIZ_STRING_SERIAL,
  251. g_string_serial
  252. };
  253. usbd_desc_t vp_desc;
  254. /**
  255. * @brief standard usb unicode convert
  256. * @param string: source string
  257. * @param unicode_buf: unicode buffer
  258. * @retval length
  259. */
  260. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  261. {
  262. uint16_t str_len = 0, id_pos = 2;
  263. uint8_t *tmp_str = string;
  264. while(*tmp_str != '\0')
  265. {
  266. str_len ++;
  267. unicode_buf[id_pos ++] = *tmp_str ++;
  268. unicode_buf[id_pos ++] = 0x00;
  269. }
  270. str_len = str_len * 2 + 2;
  271. unicode_buf[0] = str_len;
  272. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  273. return str_len;
  274. }
  275. /**
  276. * @brief usb int convert to unicode
  277. * @param value: int value
  278. * @param pbus: unicode buffer
  279. * @param len: length
  280. * @retval none
  281. */
  282. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  283. {
  284. uint8_t idx = 0;
  285. for( idx = 0 ; idx < len ; idx ++)
  286. {
  287. if( ((value >> 28)) < 0xA )
  288. {
  289. pbuf[ 2 * idx] = (value >> 28) + '0';
  290. }
  291. else
  292. {
  293. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  294. }
  295. value = value << 4;
  296. pbuf[2 * idx + 1] = 0;
  297. }
  298. }
  299. /**
  300. * @brief usb get serial number
  301. * @param none
  302. * @retval none
  303. */
  304. static void get_serial_num(void)
  305. {
  306. uint32_t serial0, serial1, serial2;
  307. serial0 = *(uint32_t*)MCU_ID1;
  308. serial1 = *(uint32_t*)MCU_ID2;
  309. serial2 = *(uint32_t*)MCU_ID3;
  310. serial0 += serial2;
  311. if (serial0 != 0)
  312. {
  313. usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
  314. usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
  315. }
  316. }
  317. /**
  318. * @brief get device descriptor
  319. * @param none
  320. * @retval usbd_desc
  321. */
  322. usbd_desc_t *get_device_descriptor(void)
  323. {
  324. return &device_descriptor;
  325. }
  326. /**
  327. * @brief get device qualifier
  328. * @param none
  329. * @retval usbd_desc
  330. */
  331. usbd_desc_t * get_device_qualifier(void)
  332. {
  333. return NULL;
  334. }
  335. /**
  336. * @brief get config descriptor
  337. * @param none
  338. * @retval usbd_desc
  339. */
  340. usbd_desc_t *get_device_configuration(void)
  341. {
  342. return &config_descriptor;
  343. }
  344. /**
  345. * @brief get other speed descriptor
  346. * @param none
  347. * @retval usbd_desc
  348. */
  349. usbd_desc_t *get_device_other_speed(void)
  350. {
  351. return NULL;
  352. }
  353. /**
  354. * @brief get lang id descriptor
  355. * @param none
  356. * @retval usbd_desc
  357. */
  358. usbd_desc_t *get_device_lang_id(void)
  359. {
  360. return &langid_descriptor;
  361. }
  362. /**
  363. * @brief get manufacturer descriptor
  364. * @param none
  365. * @retval usbd_desc
  366. */
  367. usbd_desc_t *get_device_manufacturer_string(void)
  368. {
  369. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
  370. vp_desc.descriptor = g_usbd_desc_buffer;
  371. return &vp_desc;
  372. }
  373. /**
  374. * @brief get product descriptor
  375. * @param none
  376. * @retval usbd_desc
  377. */
  378. usbd_desc_t *get_device_product_string(void)
  379. {
  380. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
  381. vp_desc.descriptor = g_usbd_desc_buffer;
  382. return &vp_desc;
  383. }
  384. /**
  385. * @brief get serial descriptor
  386. * @param none
  387. * @retval usbd_desc
  388. */
  389. usbd_desc_t *get_device_serial_string(void)
  390. {
  391. get_serial_num();
  392. return &serial_descriptor;
  393. }
  394. /**
  395. * @brief get interface descriptor
  396. * @param none
  397. * @retval usbd_desc
  398. */
  399. usbd_desc_t *get_device_interface_string(void)
  400. {
  401. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
  402. vp_desc.descriptor = g_usbd_desc_buffer;
  403. return &vp_desc;
  404. }
  405. /**
  406. * @brief get device config descriptor
  407. * @param none
  408. * @retval usbd_desc
  409. */
  410. usbd_desc_t *get_device_config_string(void)
  411. {
  412. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
  413. vp_desc.descriptor = g_usbd_desc_buffer;
  414. return &vp_desc;
  415. }
  416. /**
  417. * @}
  418. */
  419. /**
  420. * @}
  421. */
  422. /**
  423. * @}
  424. */