mouse_class.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. **************************************************************************
  3. * @file mouse_class.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb hid mouse 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 "mouse_class.h"
  28. #include "mouse_desc.h"
  29. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  30. * @{
  31. */
  32. /** @defgroup USB_mouse_class
  33. * @brief usb device mouse demo
  34. * @{
  35. */
  36. /** @defgroup USB_mouse_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. /* hid static variable */
  49. static uint32_t hid_protocol = 0;
  50. static uint32_t hid_set_idle = 0;
  51. static uint32_t alt_setting = 0;
  52. static uint8_t hid_state;
  53. __IO uint8_t hid_suspend_flag = 0;
  54. uint8_t hid_set_report[64];
  55. /* usb device class handler */
  56. usbd_class_handler mouse_class_handler =
  57. {
  58. class_init_handler,
  59. class_clear_handler,
  60. class_setup_handler,
  61. class_ept0_tx_handler,
  62. class_ept0_rx_handler,
  63. class_in_handler,
  64. class_out_handler,
  65. class_sof_handler,
  66. class_event_handler,
  67. };
  68. /**
  69. * @brief initialize usb endpoint
  70. * @param udev: to the structure of usbd_core_type
  71. * @retval status of usb_sts_type
  72. */
  73. usb_sts_type class_init_handler(void *udev)
  74. {
  75. usb_sts_type status = USB_OK;
  76. usbd_core_type *pudev = (usbd_core_type *)udev;
  77. #ifndef USB_EPT_AUTO_MALLOC_BUFFER
  78. /* use user define buffer address */
  79. usbd_ept_buf_custom_define(pudev, USBD_HID_IN_EPT, EPT1_TX_ADDR);
  80. usbd_ept_buf_custom_define(pudev, USBD_HID_OUT_EPT, EPT1_RX_ADDR);
  81. #endif
  82. /* open hid in endpoint */
  83. usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
  84. return status;
  85. }
  86. /**
  87. * @brief clear endpoint or other state
  88. * @param udev: to the structure of usbd_core_type
  89. * @retval status of usb_sts_type
  90. */
  91. usb_sts_type class_clear_handler(void *udev)
  92. {
  93. usb_sts_type status = USB_OK;
  94. usbd_core_type *pudev = (usbd_core_type *)udev;
  95. /* close hid in endpoint */
  96. usbd_ept_close(pudev, USBD_HID_IN_EPT);
  97. return status;
  98. }
  99. /**
  100. * @brief usb device class setup request handler
  101. * @param udev: to the structure of usbd_core_type
  102. * @param setup: setup packet
  103. * @retval status of usb_sts_type
  104. */
  105. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
  106. {
  107. usb_sts_type status = USB_OK;
  108. usbd_core_type *pudev = (usbd_core_type *)udev;
  109. uint16_t len;
  110. uint8_t *buf;
  111. switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
  112. {
  113. /* class request */
  114. case USB_REQ_TYPE_CLASS:
  115. switch(setup->bRequest)
  116. {
  117. case HID_REQ_SET_PROTOCOL:
  118. hid_protocol = (uint8_t)setup->wValue;
  119. break;
  120. case HID_REQ_GET_PROTOCOL:
  121. usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
  122. break;
  123. case HID_REQ_SET_IDLE:
  124. hid_set_idle = (uint8_t)(setup->wValue >> 8);
  125. break;
  126. case HID_REQ_GET_IDLE:
  127. usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
  128. break;
  129. case HID_REQ_SET_REPORT:
  130. hid_state = HID_REQ_SET_REPORT;
  131. usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
  132. break;
  133. default:
  134. usbd_ctrl_unsupport(pudev);
  135. break;
  136. }
  137. break;
  138. /* standard request */
  139. case USB_REQ_TYPE_STANDARD:
  140. switch(setup->bRequest)
  141. {
  142. case USB_STD_REQ_GET_DESCRIPTOR:
  143. if(setup->wValue >> 8 == HID_REPORT_DESC)
  144. {
  145. len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
  146. buf = (uint8_t *)g_usbd_hid_report;
  147. }
  148. else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
  149. {
  150. len = MIN(9, setup->wLength);
  151. buf = (uint8_t *)g_hid_usb_desc;
  152. }
  153. usbd_ctrl_send(pudev, (uint8_t *)buf, len);
  154. break;
  155. case USB_STD_REQ_GET_INTERFACE:
  156. usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
  157. break;
  158. case USB_STD_REQ_SET_INTERFACE:
  159. alt_setting = setup->wValue;
  160. break;
  161. default:
  162. usbd_ctrl_unsupport(pudev);
  163. break;
  164. }
  165. break;
  166. default:
  167. usbd_ctrl_unsupport(pudev);
  168. break;
  169. }
  170. return status;
  171. }
  172. /**
  173. * @brief usb device class endpoint 0 in status stage complete
  174. * @param udev: to the structure of usbd_core_type
  175. * @retval status of usb_sts_type
  176. */
  177. usb_sts_type class_ept0_tx_handler(void *udev)
  178. {
  179. usb_sts_type status = USB_OK;
  180. /* ...user code... */
  181. return status;
  182. }
  183. /**
  184. * @brief usb device class endpoint 0 out status stage complete
  185. * @param udev: to the structure of usbd_core_type
  186. * @retval status of usb_sts_type
  187. */
  188. usb_sts_type class_ept0_rx_handler(void *udev)
  189. {
  190. usb_sts_type status = USB_OK;
  191. usbd_core_type *pudev = (usbd_core_type *)udev;
  192. uint32_t recv_len = usbd_get_recv_len(pudev, 0);
  193. /* ...user code... */
  194. if( hid_state == HID_REQ_SET_REPORT)
  195. {
  196. /* hid buffer process */
  197. hid_state = 0;
  198. }
  199. return status;
  200. }
  201. /**
  202. * @brief usb device class transmision complete handler
  203. * @param udev: to the structure of usbd_core_type
  204. * @param ept_num: endpoint number
  205. * @retval status of usb_sts_type
  206. */
  207. usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
  208. {
  209. usb_sts_type status = USB_OK;
  210. /* ...user code...
  211. trans next packet data
  212. */
  213. return status;
  214. }
  215. /**
  216. * @brief usb device class endpoint receive data
  217. * @param udev: to the structure of usbd_core_type
  218. * @param ept_num: endpoint number
  219. * @retval status of usb_sts_type
  220. */
  221. usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
  222. {
  223. usb_sts_type status = USB_OK;
  224. return status;
  225. }
  226. /**
  227. * @brief usb device class sof handler
  228. * @param udev: to the structure of usbd_core_type
  229. * @retval status of usb_sts_type
  230. */
  231. usb_sts_type class_sof_handler(void *udev)
  232. {
  233. usb_sts_type status = USB_OK;
  234. /* ...user code... */
  235. return status;
  236. }
  237. /**
  238. * @brief usb device class event handler
  239. * @param udev: to the structure of usbd_core_type
  240. * @param event: usb device event
  241. * @retval status of usb_sts_type
  242. */
  243. usb_sts_type class_event_handler(void *udev, usbd_event_type event)
  244. {
  245. usb_sts_type status = USB_OK;
  246. switch(event)
  247. {
  248. case USBD_RESET_EVENT:
  249. /* ...user code... */
  250. break;
  251. case USBD_SUSPEND_EVENT:
  252. hid_suspend_flag = 1;
  253. /* ...user code... */
  254. break;
  255. case USBD_WAKEUP_EVENT:
  256. /* ...user code... */
  257. break;
  258. default:
  259. break;
  260. }
  261. return status;
  262. }
  263. /**
  264. * @brief usb device class send report
  265. * @param udev: to the structure of usbd_core_type
  266. * @param report: report buffer
  267. * @param len: report length
  268. * @retval status of usb_sts_type
  269. */
  270. usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
  271. {
  272. usb_sts_type status = USB_OK;
  273. usbd_core_type *pudev = (usbd_core_type *)udev;
  274. if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
  275. usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
  276. return status;
  277. }
  278. /**
  279. * @brief usb device class report function
  280. * @param udev: to the structure of usbd_core_type
  281. * @param op: operation
  282. * @retval none
  283. */
  284. void usb_hid_mouse_send(void *udev, uint8_t op)
  285. {
  286. static uint8_t mouse_buffer[4] = {0, 0, 0, 0};
  287. int8_t posx = 0, posy = 0, button = 0;
  288. switch(op)
  289. {
  290. case LEFT_BUTTON:
  291. button = 0x01;
  292. break;
  293. case RIGHT_BUTTON:
  294. button = 0x2;
  295. break;
  296. case UP_MOVE:
  297. posy -= MOVE_STEP;
  298. break;
  299. case DOWN_MOVE:
  300. posy += MOVE_STEP;
  301. break;
  302. case LEFT_MOVE:
  303. posx -= MOVE_STEP;
  304. break;
  305. case RIGHT_MOVE:
  306. posx += MOVE_STEP;
  307. break;
  308. default:
  309. break;
  310. }
  311. mouse_buffer[0] = button;
  312. mouse_buffer[1] = posx;
  313. mouse_buffer[2] = posy;
  314. class_send_report(udev, mouse_buffer, 4);
  315. }
  316. /**
  317. * @}
  318. */
  319. /**
  320. * @}
  321. */
  322. /**
  323. * @}
  324. */