printer_class.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. **************************************************************************
  3. * @file printer_class.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb printer class type
  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 "usbd_core.h"
  27. #include "printer_class.h"
  28. #include "printer_desc.h"
  29. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  30. * @{
  31. */
  32. /** @defgroup USB_printer_class
  33. * @brief usb device class printer demo
  34. * @{
  35. */
  36. /** @defgroup USB_printer_class_private_functions
  37. * @{
  38. */
  39. usb_sts_type class_init_handler(void *udev);
  40. usb_sts_type class_clear_handler(void *udev);
  41. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
  42. usb_sts_type class_ept0_tx_handler(void *udev);
  43. usb_sts_type class_ept0_rx_handler(void *udev);
  44. usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
  45. usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
  46. usb_sts_type class_sof_handler(void *udev);
  47. usb_sts_type class_event_handler(void *udev, usbd_event_type event);
  48. /* usb rx and tx buffer */
  49. static uint32_t alt_setting = 0;
  50. static uint8_t g_rx_buff[USBD_OUT_MAXPACKET_SIZE];
  51. static uint16_t g_rxlen;
  52. __IO uint8_t g_tx_completed = 1, g_rx_completed = 0;
  53. uint8_t PRINTER_DEVICE_ID[PRINTER_DEVICE_ID_LEN] =
  54. {
  55. 0x00, 0x16,
  56. 'M', 'F', 'G',':','A','r','t','e', 'r', 'y' ,' ',
  57. 'C','M', 'D', ':', 'E', 'S', 'C', 'P', 'O', 'S',' ',
  58. };
  59. static uint32_t g_printer_port_status = 0x18;
  60. uint8_t g_printer_data[USBD_OUT_MAXPACKET_SIZE];
  61. /* usb device class handler */
  62. usbd_class_handler printer_class_handler =
  63. {
  64. class_init_handler,
  65. class_clear_handler,
  66. class_setup_handler,
  67. class_ept0_tx_handler,
  68. class_ept0_rx_handler,
  69. class_in_handler,
  70. class_out_handler,
  71. class_sof_handler,
  72. class_event_handler,
  73. };
  74. /**
  75. * @brief initialize usb custom hid endpoint
  76. * @param udev: to the structure of usbd_core_type
  77. * @retval status of usb_sts_type
  78. */
  79. usb_sts_type class_init_handler(void *udev)
  80. {
  81. usb_sts_type status = USB_OK;
  82. usbd_core_type *pudev = (usbd_core_type *)udev;
  83. /* open in endpoint */
  84. usbd_ept_open(pudev, USBD_PRINTER_BULK_IN_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
  85. /* open out endpoint */
  86. usbd_ept_open(pudev, USBD_PRINTER_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
  87. /* set out endpoint to receive status */
  88. usbd_ept_recv(pudev, USBD_PRINTER_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
  89. g_tx_completed = 1;
  90. return status;
  91. }
  92. /**
  93. * @brief clear endpoint or other state
  94. * @param udev: to the structure of usbd_core_type
  95. * @retval status of usb_sts_type
  96. */
  97. usb_sts_type class_clear_handler(void *udev)
  98. {
  99. usb_sts_type status = USB_OK;
  100. usbd_core_type *pudev = (usbd_core_type *)udev;
  101. /* close in endpoint */
  102. usbd_ept_close(pudev, USBD_PRINTER_BULK_IN_EPT);
  103. /* close out endpoint */
  104. usbd_ept_close(pudev, USBD_PRINTER_BULK_OUT_EPT);
  105. return status;
  106. }
  107. /**
  108. * @brief usb device class setup request handler
  109. * @param udev: to the structure of usbd_core_type
  110. * @param setup: setup packet
  111. * @retval status of usb_sts_type
  112. */
  113. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
  114. {
  115. usb_sts_type status = USB_OK;
  116. usbd_core_type *pudev = (usbd_core_type *)udev;
  117. switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
  118. {
  119. /* class request */
  120. case USB_REQ_TYPE_CLASS:
  121. switch(setup->bRequest)
  122. {
  123. case PRINTER_REQ_GET_DEVICE_ID:
  124. usbd_ctrl_send(pudev, PRINTER_DEVICE_ID, PRINTER_DEVICE_ID_LEN);
  125. break;
  126. case PRINTER_REQ_GET_PORT_STATUS:
  127. usbd_ctrl_send(pudev, (uint8_t *)&g_printer_port_status, 1);
  128. break;
  129. case PRINTER_REQ_GET_SOFT_RESET:
  130. usbd_ctrl_recv(pudev, g_printer_data, USBD_OUT_MAXPACKET_SIZE);
  131. break;
  132. default:
  133. usbd_ctrl_unsupport(pudev);
  134. break;
  135. }
  136. break;
  137. /* standard request */
  138. case USB_REQ_TYPE_STANDARD:
  139. switch(setup->bRequest)
  140. {
  141. case USB_STD_REQ_GET_DESCRIPTOR:
  142. usbd_ctrl_unsupport(pudev);
  143. break;
  144. case USB_STD_REQ_GET_INTERFACE:
  145. usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
  146. break;
  147. case USB_STD_REQ_SET_INTERFACE:
  148. alt_setting = setup->wValue;
  149. break;
  150. default:
  151. break;
  152. }
  153. break;
  154. default:
  155. usbd_ctrl_unsupport(pudev);
  156. break;
  157. }
  158. return status;
  159. }
  160. /**
  161. * @brief usb device endpoint 0 in status stage complete
  162. * @param udev: to the structure of usbd_core_type
  163. * @retval status of usb_sts_type
  164. */
  165. usb_sts_type class_ept0_tx_handler(void *udev)
  166. {
  167. usb_sts_type status = USB_OK;
  168. /* ...user code... */
  169. return status;
  170. }
  171. /**
  172. * @brief usb device endpoint 0 out status stage complete
  173. * @param udev: usb device core handler type
  174. * @retval status of usb_sts_type
  175. */
  176. usb_sts_type class_ept0_rx_handler(void *udev)
  177. {
  178. usb_sts_type status = USB_OK;
  179. /* ...user code... */
  180. return status;
  181. }
  182. /**
  183. * @brief usb device transmision complete handler
  184. * @param udev: to the structure of usbd_core_type
  185. * @param ept_num: endpoint number
  186. * @retval status of usb_sts_type
  187. */
  188. usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
  189. {
  190. usb_sts_type status = USB_OK;
  191. /* ...user code...
  192. trans next packet data
  193. */
  194. g_tx_completed = 1;
  195. return status;
  196. }
  197. /**
  198. * @brief usb device endpoint receive data
  199. * @param udev: to the structure of usbd_core_type
  200. * @param ept_num: endpoint number
  201. * @retval status of usb_sts_type
  202. */
  203. usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
  204. {
  205. usb_sts_type status = USB_OK;
  206. usbd_core_type *pudev = (usbd_core_type *)udev;
  207. /* get endpoint receive data length */
  208. g_rxlen = usbd_get_recv_len(pudev, ept_num);
  209. /*set recv flag*/
  210. g_rx_completed = 1;
  211. return status;
  212. }
  213. /**
  214. * @brief usb device sof handler
  215. * @param udev: to the structure of usbd_core_type
  216. * @retval status of usb_sts_type
  217. */
  218. usb_sts_type class_sof_handler(void *udev)
  219. {
  220. usb_sts_type status = USB_OK;
  221. /* ...user code... */
  222. return status;
  223. }
  224. /**
  225. * @brief usb device event handler
  226. * @param udev: to the structure of usbd_core_type
  227. * @param event: usb device event
  228. * @retval status of usb_sts_type
  229. */
  230. usb_sts_type class_event_handler(void *udev, usbd_event_type event)
  231. {
  232. usb_sts_type status = USB_OK;
  233. switch(event)
  234. {
  235. case USBD_RESET_EVENT:
  236. /* ...user code... */
  237. break;
  238. case USBD_SUSPEND_EVENT:
  239. /* ...user code... */
  240. break;
  241. case USBD_WAKEUP_EVENT:
  242. /* ...user code... */
  243. break;
  244. default:
  245. break;
  246. }
  247. return status;
  248. }
  249. /**
  250. * @brief usb device class rx data process
  251. * @param udev: to the structure of usbd_core_type
  252. * @param recv_data: receive buffer
  253. * @retval receive data len
  254. */
  255. uint16_t usb_printer_get_rxdata(void *udev, uint8_t *recv_data)
  256. {
  257. uint16_t i_index = 0;
  258. usbd_core_type *pudev = (usbd_core_type *)udev;
  259. uint16_t tmp_len = g_rxlen;
  260. if(g_rx_completed == 0)
  261. {
  262. return 0;
  263. }
  264. g_rx_completed = 0;
  265. tmp_len = g_rxlen;
  266. for(i_index = 0; i_index < g_rxlen; i_index ++)
  267. {
  268. recv_data[i_index] = g_rx_buff[i_index];
  269. }
  270. usbd_ept_recv(pudev, USBD_PRINTER_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
  271. return tmp_len;
  272. }
  273. /**
  274. * @brief usb device class send data
  275. * @param udev: to the structure of usbd_core_type
  276. * @param send_data: send data buffer
  277. * @param len: send length
  278. * @retval error status
  279. */
  280. error_status usb_printer_send_data(void *udev, uint8_t *send_data, uint16_t len)
  281. {
  282. error_status status = SUCCESS;
  283. usbd_core_type *pudev = (usbd_core_type *)udev;
  284. if(g_tx_completed)
  285. {
  286. g_tx_completed = 0;
  287. usbd_ept_send(pudev, USBD_PRINTER_BULK_IN_EPT, send_data, len);
  288. }
  289. else
  290. {
  291. status = ERROR;
  292. }
  293. return status;
  294. }
  295. /**
  296. * @}
  297. */
  298. /**
  299. * @}
  300. */
  301. /**
  302. * @}
  303. */