printer_desc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. **************************************************************************
  3. * @file printer_desc.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb printer 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 "stdio.h"
  27. #include "usb_std.h"
  28. #include "usbd_sdr.h"
  29. #include "usbd_core.h"
  30. #include "printer_desc.h"
  31. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  32. * @{
  33. */
  34. /** @defgroup USB_printer_desc
  35. * @brief usb device printer descriptor
  36. * @{
  37. */
  38. /** @defgroup USB_printer_desc_private_functions
  39. * @{
  40. */
  41. usbd_desc_t *get_device_descriptor(void);
  42. usbd_desc_t *get_device_qualifier(void);
  43. usbd_desc_t *get_device_configuration(void);
  44. usbd_desc_t *get_device_other_speed(void);
  45. usbd_desc_t *get_device_lang_id(void);
  46. usbd_desc_t *get_device_manufacturer_string(void);
  47. usbd_desc_t *get_device_product_string(void);
  48. usbd_desc_t *get_device_serial_string(void);
  49. usbd_desc_t *get_device_interface_string(void);
  50. usbd_desc_t *get_device_config_string(void);
  51. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
  52. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  53. static void get_serial_num(void);
  54. static uint8_t g_usbd_desc_buffer[256];
  55. /**
  56. * @brief device descriptor handler structure
  57. */
  58. usbd_desc_handler printer_desc_handler =
  59. {
  60. get_device_descriptor,
  61. get_device_qualifier,
  62. get_device_configuration,
  63. get_device_other_speed,
  64. get_device_lang_id,
  65. get_device_manufacturer_string,
  66. get_device_product_string,
  67. get_device_serial_string,
  68. get_device_interface_string,
  69. get_device_config_string,
  70. };
  71. /**
  72. * @brief usb device standard descriptor
  73. */
  74. #if defined ( __ICCARM__ ) /* iar compiler */
  75. #pragma data_alignment=4
  76. #endif
  77. ALIGNED_HEAD uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
  78. {
  79. USB_DEVICE_DESC_LEN, /* bLength */
  80. USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
  81. 0x00, /* bcdUSB */
  82. 0x02,
  83. 0x00, /* bDeviceClass */
  84. 0x00, /* bDeviceSubClass */
  85. 0x00, /* bDeviceProtocol */
  86. USB_MAX_EP0_SIZE, /* bMaxPacketSize */
  87. LBYTE(USBD_VENDOR_ID), /* idVendor */
  88. HBYTE(USBD_VENDOR_ID), /* idVendor */
  89. LBYTE(USBD_PRODUCT_ID), /* idProduct */
  90. HBYTE(USBD_PRODUCT_ID), /* idProduct */
  91. 0x00, /* bcdDevice rel. 2.00 */
  92. 0x02,
  93. USB_MFC_STRING, /* Index of manufacturer string */
  94. USB_PRODUCT_STRING, /* Index of product string */
  95. USB_SERIAL_STRING, /* Index of serial number string */
  96. 1 /* bNumConfigurations */
  97. };
  98. /**
  99. * @brief usb configuration standard descriptor
  100. */
  101. #if defined ( __ICCARM__ ) /* iar compiler */
  102. #pragma data_alignment=4
  103. #endif
  104. ALIGNED_HEAD uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] ALIGNED_TAIL =
  105. {
  106. USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
  107. USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
  108. LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
  109. HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
  110. 0x01, /* bNumInterfaces: 1 interface */
  111. 0x01, /* bConfigurationValue: configuration value */
  112. 0x00, /* iConfiguration: index of string descriptor describing
  113. the configuration */
  114. 0xC0, /* bmAttributes: self powered */
  115. 0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
  116. USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
  117. USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
  118. 0x00, /* bInterfaceNumber: number of interface */
  119. 0x00, /* bAlternateSetting: alternate set */
  120. 0x02, /* bNumEndpoints: number of endpoints */
  121. USB_CLASS_CODE_PRINTER, /* bInterfaceClass: printer class code */
  122. 0x01, /* bInterfaceSubClass: subclass code*/
  123. PRINTER_PROTOCOL_BI_DIRECTIONAL, /* bInterfaceProtocol: printer interface type */
  124. 0x00, /* iInterface: index of string descriptor */
  125. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  126. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  127. USBD_PRINTER_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  128. USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
  129. LBYTE(USBD_IN_MAXPACKET_SIZE),
  130. HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  131. 0x00, /* bInterval: interval for polling endpoint for data transfers */
  132. USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
  133. USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
  134. USBD_PRINTER_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
  135. USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
  136. LBYTE(USBD_OUT_MAXPACKET_SIZE),
  137. HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
  138. 0x00, /* bInterval: interval for polling endpoint for data transfers */
  139. };
  140. /**
  141. * @brief usb string lang id
  142. */
  143. #if defined ( __ICCARM__ ) /* iar compiler */
  144. #pragma data_alignment=4
  145. #endif
  146. ALIGNED_HEAD uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] ALIGNED_TAIL =
  147. {
  148. USBD_SIZ_STRING_LANGID,
  149. USB_DESCIPTOR_TYPE_STRING,
  150. 0x09,
  151. 0x04,
  152. };
  153. /**
  154. * @brief usb string serial
  155. */
  156. #if defined ( __ICCARM__ ) /* iar compiler */
  157. #pragma data_alignment=4
  158. #endif
  159. ALIGNED_HEAD uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] ALIGNED_TAIL =
  160. {
  161. USBD_SIZ_STRING_SERIAL,
  162. USB_DESCIPTOR_TYPE_STRING,
  163. };
  164. /* device descriptor */
  165. usbd_desc_t device_descriptor =
  166. {
  167. USB_DEVICE_DESC_LEN,
  168. g_usbd_descriptor
  169. };
  170. /* config descriptor */
  171. usbd_desc_t config_descriptor =
  172. {
  173. USBD_CONFIG_DESC_SIZE,
  174. g_usbd_configuration
  175. };
  176. /* langid descriptor */
  177. usbd_desc_t langid_descriptor =
  178. {
  179. USBD_SIZ_STRING_LANGID,
  180. g_string_lang_id
  181. };
  182. /* serial descriptor */
  183. usbd_desc_t serial_descriptor =
  184. {
  185. USBD_SIZ_STRING_SERIAL,
  186. g_string_serial
  187. };
  188. usbd_desc_t vp_desc;
  189. /**
  190. * @brief standard usb unicode convert
  191. * @param string: source string
  192. * @param unicode_buf: unicode buffer
  193. * @retval length
  194. */
  195. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  196. {
  197. uint16_t str_len = 0, id_pos = 2;
  198. uint8_t *tmp_str = string;
  199. while(*tmp_str != '\0')
  200. {
  201. str_len ++;
  202. unicode_buf[id_pos ++] = *tmp_str ++;
  203. unicode_buf[id_pos ++] = 0x00;
  204. }
  205. str_len = str_len * 2 + 2;
  206. unicode_buf[0] = (uint8_t)str_len;
  207. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  208. return str_len;
  209. }
  210. /**
  211. * @brief usb int convert to unicode
  212. * @param value: int value
  213. * @param pbus: unicode buffer
  214. * @param len: length
  215. * @retval none
  216. */
  217. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  218. {
  219. uint8_t idx = 0;
  220. for( idx = 0 ; idx < len ; idx ++)
  221. {
  222. if( ((value >> 28)) < 0xA )
  223. {
  224. pbuf[ 2 * idx] = (value >> 28) + '0';
  225. }
  226. else
  227. {
  228. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  229. }
  230. value = value << 4;
  231. pbuf[2 * idx + 1] = 0;
  232. }
  233. }
  234. /**
  235. * @brief usb get serial number
  236. * @param none
  237. * @retval none
  238. */
  239. static void get_serial_num(void)
  240. {
  241. uint32_t serial0, serial1, serial2;
  242. serial0 = *(uint32_t*)MCU_ID1;
  243. serial1 = *(uint32_t*)MCU_ID2;
  244. serial2 = *(uint32_t*)MCU_ID3;
  245. serial0 += serial2;
  246. if (serial0 != 0)
  247. {
  248. usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
  249. usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
  250. }
  251. }
  252. /**
  253. * @brief get device descriptor
  254. * @param none
  255. * @retval usbd_desc
  256. */
  257. usbd_desc_t *get_device_descriptor(void)
  258. {
  259. return &device_descriptor;
  260. }
  261. /**
  262. * @brief get device qualifier
  263. * @param none
  264. * @retval usbd_desc
  265. */
  266. usbd_desc_t * get_device_qualifier(void)
  267. {
  268. return NULL;
  269. }
  270. /**
  271. * @brief get config descriptor
  272. * @param none
  273. * @retval usbd_desc
  274. */
  275. usbd_desc_t *get_device_configuration(void)
  276. {
  277. return &config_descriptor;
  278. }
  279. /**
  280. * @brief get other speed descriptor
  281. * @param none
  282. * @retval usbd_desc
  283. */
  284. usbd_desc_t *get_device_other_speed(void)
  285. {
  286. return NULL;
  287. }
  288. /**
  289. * @brief get lang id descriptor
  290. * @param none
  291. * @retval usbd_desc
  292. */
  293. usbd_desc_t *get_device_lang_id(void)
  294. {
  295. return &langid_descriptor;
  296. }
  297. /**
  298. * @brief get manufacturer descriptor
  299. * @param none
  300. * @retval usbd_desc
  301. */
  302. usbd_desc_t *get_device_manufacturer_string(void)
  303. {
  304. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
  305. vp_desc.descriptor = g_usbd_desc_buffer;
  306. return &vp_desc;
  307. }
  308. /**
  309. * @brief get product descriptor
  310. * @param none
  311. * @retval usbd_desc
  312. */
  313. usbd_desc_t *get_device_product_string(void)
  314. {
  315. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
  316. vp_desc.descriptor = g_usbd_desc_buffer;
  317. return &vp_desc;
  318. }
  319. /**
  320. * @brief get serial descriptor
  321. * @param none
  322. * @retval usbd_desc
  323. */
  324. usbd_desc_t *get_device_serial_string(void)
  325. {
  326. get_serial_num();
  327. return &serial_descriptor;
  328. }
  329. /**
  330. * @brief get interface descriptor
  331. * @param none
  332. * @retval usbd_desc
  333. */
  334. usbd_desc_t *get_device_interface_string(void)
  335. {
  336. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
  337. vp_desc.descriptor = g_usbd_desc_buffer;
  338. return &vp_desc;
  339. }
  340. /**
  341. * @brief get device config descriptor
  342. * @param none
  343. * @retval usbd_desc
  344. */
  345. usbd_desc_t *get_device_config_string(void)
  346. {
  347. vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
  348. vp_desc.descriptor = g_usbd_desc_buffer;
  349. return &vp_desc;
  350. }
  351. /**
  352. * @}
  353. */
  354. /**
  355. * @}
  356. */
  357. /**
  358. * @}
  359. */