keyboard_class.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /**
  2. **************************************************************************
  3. * @file keyboard_class.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb hid keyboard 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 "keyboard_class.h"
  28. #include "keyboard_desc.h"
  29. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  30. * @{
  31. */
  32. /** @defgroup USB_keyboard_class
  33. * @brief usb device keyboard demo
  34. * @{
  35. */
  36. /** @defgroup USB_keyboard_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. __IO uint8_t g_u8tx_completed = 0;
  56. #define SHIFT 0x80
  57. const unsigned char _asciimap[128] =
  58. {
  59. 0x00,// NUL
  60. 0x00,// SOH
  61. 0x00,// STX
  62. 0x00,// ETX
  63. 0x00,// EOT
  64. 0x00,// ENQ
  65. 0x00,// ACK
  66. 0x00,// BEL
  67. 0x2A,// BS Backspace
  68. 0x2B,// TAB Tab
  69. 0x28,// LF Enter
  70. 0x00,// VT
  71. 0x00,// FF
  72. 0x00,// CR
  73. 0x00,// SO
  74. 0x00,// SI
  75. 0x00,// DEL
  76. 0x00,// DC1
  77. 0x00,// DC2
  78. 0x00,// DC3
  79. 0x00,// DC4
  80. 0x00,// NAK
  81. 0x00,// SYN
  82. 0x00,// ETB
  83. 0x00,// CAN
  84. 0x00,// EM
  85. 0x00,// SUB
  86. 0x00,// ESC
  87. 0x00,// FS
  88. 0x00,// GS
  89. 0x00,// RS
  90. 0x00,// US
  91. 0x2C,// ' '
  92. 0x1E|SHIFT,// !
  93. 0x34|SHIFT,// "
  94. 0x20|SHIFT,// #
  95. 0x21|SHIFT,// $
  96. 0x22|SHIFT,// %
  97. 0x24|SHIFT,// &
  98. 0x34, // '
  99. 0x26|SHIFT,// (
  100. 0x27|SHIFT,// )
  101. 0x25|SHIFT,// *
  102. 0x2E|SHIFT,// +
  103. 0x36,// ,
  104. 0x2D,// -
  105. 0x37,// .
  106. 0x38,// /
  107. 0x27,// 0
  108. 0x1E,// 1
  109. 0x1F,// 2
  110. 0x20,// 3
  111. 0x21,// 4
  112. 0x22,// 5
  113. 0x23,// 6
  114. 0x24,// 7
  115. 0x25,// 8
  116. 0x26,// 9
  117. 0x33|SHIFT,// :
  118. 0x33, // ;
  119. 0x36|SHIFT,// <
  120. 0x2E, // =
  121. 0x37|SHIFT,// >
  122. 0x38|SHIFT,// ?
  123. 0x1F|SHIFT,// @
  124. 0x04|SHIFT,// A
  125. 0x05|SHIFT,// B
  126. 0x06|SHIFT,// C
  127. 0x07|SHIFT,// D
  128. 0x08|SHIFT,// E
  129. 0x09|SHIFT,// F
  130. 0x0A|SHIFT,// G
  131. 0x0B|SHIFT,// H
  132. 0x0C|SHIFT,// I
  133. 0x0D|SHIFT,// J
  134. 0x0E|SHIFT,// K
  135. 0x0F|SHIFT,// L
  136. 0x10|SHIFT,// M
  137. 0x11|SHIFT,// N
  138. 0x12|SHIFT,// O
  139. 0x13|SHIFT,// P
  140. 0x14|SHIFT,// Q
  141. 0x15|SHIFT,// R
  142. 0x16|SHIFT,// S
  143. 0x17|SHIFT,// T
  144. 0x18|SHIFT,// U
  145. 0x19|SHIFT,// V
  146. 0x1A|SHIFT,// W
  147. 0x1B|SHIFT,// X
  148. 0x1C|SHIFT,// Y
  149. 0x1D|SHIFT,// Z
  150. 0x2F, // [
  151. 0x31, // bslash
  152. 0x30, // ]
  153. 0x23|SHIFT,// ^
  154. 0x2D|SHIFT,// _
  155. 0x35, // `
  156. 0x04, // a
  157. 0x05, // b
  158. 0x06, // c
  159. 0x07, // d
  160. 0x08, // e
  161. 0x09, // f
  162. 0x0A, // g
  163. 0x0B, // h
  164. 0x0C, // i
  165. 0x0D, // j
  166. 0x0E, // k
  167. 0x0F, // l
  168. 0x10, // m
  169. 0x11, // n
  170. 0x12, // o
  171. 0x13, // p
  172. 0x14, // q
  173. 0x15, // r
  174. 0x16, // s
  175. 0x17, // t
  176. 0x18, // u
  177. 0x19, // v
  178. 0x1A, // w
  179. 0x1B, // x
  180. 0x1C, // y
  181. 0x1D, // z
  182. 0x2f|SHIFT,//
  183. 0x31|SHIFT,// |
  184. 0x30|SHIFT,// }
  185. 0x35|SHIFT,// ~
  186. 0 // DEL
  187. };
  188. /* usb device class handler */
  189. usbd_class_handler keyboard_class_handler =
  190. {
  191. class_init_handler,
  192. class_clear_handler,
  193. class_setup_handler,
  194. class_ept0_tx_handler,
  195. class_ept0_rx_handler,
  196. class_in_handler,
  197. class_out_handler,
  198. class_sof_handler,
  199. class_event_handler,
  200. };
  201. /**
  202. * @brief initialize usb endpoint
  203. * @param udev: to the structure of usbd_core_type
  204. * @retval status of usb_sts_type
  205. */
  206. usb_sts_type class_init_handler(void *udev)
  207. {
  208. usb_sts_type status = USB_OK;
  209. usbd_core_type *pudev = (usbd_core_type *)udev;
  210. #ifndef USB_EPT_AUTO_MALLOC_BUFFER
  211. /* use user define buffer address */
  212. usbd_ept_buf_custom_define(pudev, USBD_HID_IN_EPT, EPT1_TX_ADDR);
  213. #endif
  214. /* open hid in endpoint */
  215. usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
  216. g_u8tx_completed = 1;
  217. return status;
  218. }
  219. /**
  220. * @brief clear endpoint or other state
  221. * @param udev: to the structure of usbd_core_type
  222. * @retval status of usb_sts_type
  223. */
  224. usb_sts_type class_clear_handler(void *udev)
  225. {
  226. usb_sts_type status = USB_OK;
  227. usbd_core_type *pudev = (usbd_core_type *)udev;
  228. /* close hid in endpoint */
  229. usbd_ept_close(pudev, USBD_HID_IN_EPT);
  230. return status;
  231. }
  232. /**
  233. * @brief usb device class setup request handler
  234. * @param udev: to the structure of usbd_core_type
  235. * @param setup: setup packet
  236. * @retval status of usb_sts_type
  237. */
  238. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
  239. {
  240. usb_sts_type status = USB_OK;
  241. usbd_core_type *pudev = (usbd_core_type *)udev;
  242. uint16_t len;
  243. uint8_t *buf;
  244. switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
  245. {
  246. /* class request */
  247. case USB_REQ_TYPE_CLASS:
  248. switch(setup->bRequest)
  249. {
  250. case HID_REQ_SET_PROTOCOL:
  251. hid_protocol = (uint8_t)setup->wValue;
  252. break;
  253. case HID_REQ_GET_PROTOCOL:
  254. usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
  255. break;
  256. case HID_REQ_SET_IDLE:
  257. hid_set_idle = (uint8_t)(setup->wValue >> 8);
  258. break;
  259. case HID_REQ_GET_IDLE:
  260. usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
  261. break;
  262. case HID_REQ_SET_REPORT:
  263. hid_state = HID_REQ_SET_REPORT;
  264. usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
  265. break;
  266. default:
  267. usbd_ctrl_unsupport(pudev);
  268. break;
  269. }
  270. break;
  271. /* standard request */
  272. case USB_REQ_TYPE_STANDARD:
  273. switch(setup->bRequest)
  274. {
  275. case USB_STD_REQ_GET_DESCRIPTOR:
  276. if(setup->wValue >> 8 == HID_REPORT_DESC)
  277. {
  278. len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
  279. buf = (uint8_t *)g_usbd_hid_report;
  280. }
  281. else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
  282. {
  283. len = MIN(9, setup->wLength);
  284. buf = (uint8_t *)g_hid_usb_desc;
  285. }
  286. usbd_ctrl_send(pudev, (uint8_t *)buf, len);
  287. break;
  288. case USB_STD_REQ_GET_INTERFACE:
  289. usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
  290. break;
  291. case USB_STD_REQ_SET_INTERFACE:
  292. alt_setting = setup->wValue;
  293. break;
  294. default:
  295. break;
  296. }
  297. break;
  298. default:
  299. usbd_ctrl_unsupport(pudev);
  300. break;
  301. }
  302. return status;
  303. }
  304. /**
  305. * @brief usb device class endpoint 0 in status stage complete
  306. * @param udev: to the structure of usbd_core_type
  307. * @retval status of usb_sts_type
  308. */
  309. usb_sts_type class_ept0_tx_handler(void *udev)
  310. {
  311. usb_sts_type status = USB_OK;
  312. /* ...user code... */
  313. return status;
  314. }
  315. /**
  316. * @brief usb device class endpoint 0 out status stage complete
  317. * @param udev: to the structure of usbd_core_type
  318. * @retval status of usb_sts_type
  319. */
  320. usb_sts_type class_ept0_rx_handler(void *udev)
  321. {
  322. usb_sts_type status = USB_OK;
  323. usbd_core_type *pudev = (usbd_core_type *)udev;
  324. uint32_t recv_len = usbd_get_recv_len(pudev, 0);
  325. /* ...user code... */
  326. if( hid_state == HID_REQ_SET_REPORT)
  327. {
  328. /* hid buffer process */
  329. hid_state = 0;
  330. }
  331. return status;
  332. }
  333. /**
  334. * @brief usb device class transmision complete handler
  335. * @param udev: to the structure of usbd_core_type
  336. * @param ept_num: endpoint number
  337. * @retval status of usb_sts_type
  338. */
  339. usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
  340. {
  341. usb_sts_type status = USB_OK;
  342. /* ...user code...
  343. trans next packet data
  344. */
  345. g_u8tx_completed = 1;
  346. return status;
  347. }
  348. /**
  349. * @brief usb device class endpoint receive data
  350. * @param udev: to the structure of usbd_core_type
  351. * @param ept_num: endpoint number
  352. * @retval status of usb_sts_type
  353. */
  354. usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
  355. {
  356. usb_sts_type status = USB_OK;
  357. return status;
  358. }
  359. /**
  360. * @brief usb device class sof handler
  361. * @param udev: to the structure of usbd_core_type
  362. * @retval status of usb_sts_type
  363. */
  364. usb_sts_type class_sof_handler(void *udev)
  365. {
  366. usb_sts_type status = USB_OK;
  367. /* ...user code... */
  368. return status;
  369. }
  370. /**
  371. * @brief usb device class event handler
  372. * @param udev: to the structure of usbd_core_type
  373. * @param event: usb device event
  374. * @retval status of usb_sts_type
  375. */
  376. usb_sts_type class_event_handler(void *udev, usbd_event_type event)
  377. {
  378. usb_sts_type status = USB_OK;
  379. switch(event)
  380. {
  381. case USBD_RESET_EVENT:
  382. /* ...user code... */
  383. break;
  384. case USBD_SUSPEND_EVENT:
  385. hid_suspend_flag = 1;
  386. /* ...user code... */
  387. break;
  388. case USBD_WAKEUP_EVENT:
  389. /* ...user code... */
  390. break;
  391. default:
  392. break;
  393. }
  394. return status;
  395. }
  396. /**
  397. * @brief usb device class send report
  398. * @param udev: to the structure of usbd_core_type
  399. * @param report: report buffer
  400. * @param len: report length
  401. * @retval status of usb_sts_type
  402. */
  403. usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
  404. {
  405. usb_sts_type status = USB_OK;
  406. usbd_core_type *pudev = (usbd_core_type *)udev;
  407. if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
  408. usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
  409. return status;
  410. }
  411. /**
  412. * @brief usb device class report function
  413. * @param udev: to the structure of usbd_core_type
  414. * @param op: operation
  415. * @retval none
  416. */
  417. void usb_hid_keyboard_send_char(void *udev, uint8_t ascii_code)
  418. {
  419. uint8_t key_data = 0;
  420. static uint8_t temp = 0;
  421. static uint8_t keyboard_buf[8] = {0, 0, 6, 0, 0, 0, 0, 0};
  422. if(ascii_code > 128)
  423. {
  424. ascii_code = 0;
  425. }
  426. else
  427. {
  428. ascii_code = _asciimap[ascii_code];
  429. if(!ascii_code)
  430. {
  431. ascii_code = 0;
  432. }
  433. if(ascii_code & SHIFT)
  434. {
  435. key_data = 0x2;
  436. ascii_code &= 0x7F;
  437. }
  438. }
  439. if((temp == ascii_code) && (ascii_code != 0))
  440. {
  441. keyboard_buf[0] = 0;
  442. keyboard_buf[2] = 0;
  443. class_send_report(udev, keyboard_buf, 8);
  444. }
  445. else
  446. {
  447. keyboard_buf[0] = key_data;
  448. keyboard_buf[2] = ascii_code;
  449. class_send_report(udev, keyboard_buf, 8);
  450. }
  451. }
  452. /**
  453. * @}
  454. */
  455. /**
  456. * @}
  457. */
  458. /**
  459. * @}
  460. */