keyboard_desc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /**
  2. **************************************************************************
  3. * @file keyboard_desc.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb hid keyboard 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 "keyboard_desc.h"
  30. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  31. * @{
  32. */
  33. /** @defgroup USB_keyboard_desc
  34. * @brief usb device keyboard descriptor
  35. * @{
  36. */
  37. /** @defgroup USB_keyboard_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 keyboard device descriptor handler structure
  56. */
  57. usbd_desc_handler keyboard_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. 0x01, /* 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, // USAGE_PAGE (Generic Desktop)
  150. 0x09, 0x06, // USAGE (Keyboard)
  151. 0xa1, 0x01, // COLLECTION (Application)
  152. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  153. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  154. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  155. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  156. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  157. 0x75, 0x01, // REPORT_SIZE (1)
  158. 0x95, 0x08, // REPORT_COUNT (8)
  159. 0x81, 0x02, // INPUT (Data,Var,Abs)
  160. 0x95, 0x01, // REPORT_COUNT (1)
  161. 0x75, 0x08, // REPORT_SIZE (8)
  162. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  163. 0x95, 0x05, // REPORT_COUNT (5)
  164. 0x75, 0x01, // REPORT_SIZE (1)
  165. 0x05, 0x08, // USAGE_PAGE (LEDs)
  166. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  167. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  168. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  169. 0x95, 0x01, // REPORT_COUNT (1)
  170. 0x75, 0x03, // REPORT_SIZE (3)
  171. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  172. 0x95, 0x06, // REPORT_COUNT (6)
  173. 0x75, 0x08, // REPORT_SIZE (8)
  174. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  175. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  176. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  177. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  178. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  179. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  180. 0xc0 // END_COLLECTION
  181. };
  182. /**
  183. * @brief usb hid descriptor
  184. */
  185. #if defined ( __ICCARM__ ) /* iar compiler */
  186. #pragma data_alignment=4
  187. #endif
  188. ALIGNED_HEAD uint8_t g_hid_usb_desc[9] ALIGNED_TAIL =
  189. {
  190. 0x09, /* bLength: size of HID descriptor in bytes */
  191. HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
  192. LBYTE(HID_BCD_NUM),
  193. HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
  194. 0x00, /* bCountryCode: hardware target conutry */
  195. 0x01, /* bNumDescriptors: number of HID class descriptor to follow */
  196. HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
  197. LBYTE(sizeof(g_usbd_hid_report)),
  198. HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
  199. };
  200. /**
  201. * @brief usb string lang id
  202. */
  203. #if defined ( __ICCARM__ ) /* iar compiler */
  204. #pragma data_alignment=4
  205. #endif
  206. ALIGNED_HEAD uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] ALIGNED_TAIL =
  207. {
  208. USBD_SIZ_STRING_LANGID,
  209. USB_DESCIPTOR_TYPE_STRING,
  210. 0x09,
  211. 0x04,
  212. };
  213. /**
  214. * @brief usb string serial
  215. */
  216. #if defined ( __ICCARM__ ) /* iar compiler */
  217. #pragma data_alignment=4
  218. #endif
  219. ALIGNED_HEAD uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] ALIGNED_TAIL =
  220. {
  221. USBD_SIZ_STRING_SERIAL,
  222. USB_DESCIPTOR_TYPE_STRING,
  223. };
  224. /* device descriptor */
  225. usbd_desc_t device_descriptor =
  226. {
  227. USB_DEVICE_DESC_LEN,
  228. g_usbd_descriptor
  229. };
  230. /* config descriptor */
  231. usbd_desc_t config_descriptor =
  232. {
  233. USBD_CONFIG_DESC_SIZE,
  234. g_usbd_configuration
  235. };
  236. /* langid descriptor */
  237. usbd_desc_t langid_descriptor =
  238. {
  239. USBD_SIZ_STRING_LANGID,
  240. g_string_lang_id
  241. };
  242. /* serial descriptor */
  243. usbd_desc_t serial_descriptor =
  244. {
  245. USBD_SIZ_STRING_SERIAL,
  246. g_string_serial
  247. };
  248. usbd_desc_t vp_desc;
  249. /**
  250. * @brief standard usb unicode convert
  251. * @param string: source string
  252. * @param unicode_buf: unicode buffer
  253. * @retval length
  254. */
  255. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  256. {
  257. uint16_t str_len = 0, id_pos = 2;
  258. uint8_t *tmp_str = string;
  259. while(*tmp_str != '\0')
  260. {
  261. str_len ++;
  262. unicode_buf[id_pos ++] = *tmp_str ++;
  263. unicode_buf[id_pos ++] = 0x00;
  264. }
  265. str_len = str_len * 2 + 2;
  266. unicode_buf[0] = str_len;
  267. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  268. return str_len;
  269. }
  270. /**
  271. * @brief usb int convert to unicode
  272. * @param value: int value
  273. * @param pbus: unicode buffer
  274. * @param len: length
  275. * @retval none
  276. */
  277. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  278. {
  279. uint8_t idx = 0;
  280. for( idx = 0 ; idx < len ; idx ++)
  281. {
  282. if( ((value >> 28)) < 0xA )
  283. {
  284. pbuf[ 2 * idx] = (value >> 28) + '0';
  285. }
  286. else
  287. {
  288. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  289. }
  290. value = value << 4;
  291. pbuf[2 * idx + 1] = 0;
  292. }
  293. }
  294. /**
  295. * @brief usb get serial number
  296. * @param none
  297. * @retval none
  298. */
  299. static void get_serial_num(void)
  300. {
  301. uint32_t serial0, serial1, serial2;
  302. serial0 = *(uint32_t*)MCU_ID1;
  303. serial1 = *(uint32_t*)MCU_ID2;
  304. serial2 = *(uint32_t*)MCU_ID3;
  305. serial0 += serial2;
  306. if (serial0 != 0)
  307. {
  308. usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
  309. usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
  310. }
  311. }
  312. /**
  313. * @brief get device descriptor
  314. * @param none
  315. * @retval usbd_desc
  316. */
  317. usbd_desc_t *get_device_descriptor(void)
  318. {
  319. return &device_descriptor;
  320. }
  321. /**
  322. * @brief get device qualifier
  323. * @param none
  324. * @retval usbd_desc
  325. */
  326. usbd_desc_t * get_device_qualifier(void)
  327. {
  328. return NULL;
  329. }
  330. /**
  331. * @brief get config descriptor
  332. * @param none
  333. * @retval usbd_desc
  334. */
  335. usbd_desc_t *get_device_configuration(void)
  336. {
  337. return &config_descriptor;
  338. }
  339. /**
  340. * @brief get other speed descriptor
  341. * @param none
  342. * @retval usbd_desc
  343. */
  344. usbd_desc_t *get_device_other_speed(void)
  345. {
  346. return NULL;
  347. }
  348. /**
  349. * @brief get lang id descriptor
  350. * @param none
  351. * @retval usbd_desc
  352. */
  353. usbd_desc_t *get_device_lang_id(void)
  354. {
  355. return &langid_descriptor;
  356. }
  357. /**
  358. * @brief get manufacturer descriptor
  359. * @param none
  360. * @retval usbd_desc
  361. */
  362. usbd_desc_t *get_device_manufacturer_string(void)
  363. {
  364. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
  365. vp_desc.descriptor = g_usbd_desc_buffer;
  366. return &vp_desc;
  367. }
  368. /**
  369. * @brief get product descriptor
  370. * @param none
  371. * @retval usbd_desc
  372. */
  373. usbd_desc_t *get_device_product_string(void)
  374. {
  375. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
  376. vp_desc.descriptor = g_usbd_desc_buffer;
  377. return &vp_desc;
  378. }
  379. /**
  380. * @brief get serial descriptor
  381. * @param none
  382. * @retval usbd_desc
  383. */
  384. usbd_desc_t *get_device_serial_string(void)
  385. {
  386. get_serial_num();
  387. return &serial_descriptor;
  388. }
  389. /**
  390. * @brief get interface descriptor
  391. * @param none
  392. * @retval usbd_desc
  393. */
  394. usbd_desc_t *get_device_interface_string(void)
  395. {
  396. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
  397. vp_desc.descriptor = g_usbd_desc_buffer;
  398. return &vp_desc;
  399. }
  400. /**
  401. * @brief get device config descriptor
  402. * @param none
  403. * @retval usbd_desc
  404. */
  405. usbd_desc_t *get_device_config_string(void)
  406. {
  407. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
  408. vp_desc.descriptor = g_usbd_desc_buffer;
  409. return &vp_desc;
  410. }
  411. /**
  412. * @}
  413. */
  414. /**
  415. * @}
  416. */
  417. /**
  418. * @}
  419. */