ukbd.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-01-03 Yi Qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/usb_host.h>
  12. #include "hid.h"
  13. #if defined(RT_USBH_HID) && defined(RT_USBH_HID_KEYBOARD)
  14. #define DBG_TAG "usbhost.ukbd"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. static struct uprotocal kbd_protocal;
  18. static rt_err_t rt_usbh_hid_kbd_callback(void* arg)
  19. {
  20. int int1, int2;
  21. struct uhid* hid;
  22. hid = (struct uhid*)arg;
  23. int1 = *(rt_uint32_t*)hid->buffer;
  24. int2 = *(rt_uint32_t*)(&hid->buffer[4]);
  25. if(int1 != 0 || int2 != 0)
  26. {
  27. LOG_D("key down 0x%x, 0x%x", int1, int2);
  28. }
  29. return RT_EOK;
  30. }
  31. static rt_thread_t kbd_thread;
  32. static void kbd_task(void* param)
  33. {
  34. struct uhintf* intf = (struct uhintf*)param;
  35. while (1)
  36. {
  37. if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
  38. ((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
  39. USB_TIMEOUT_BASIC) == 0)
  40. {
  41. break;
  42. }
  43. rt_usbh_hid_kbd_callback(intf->user_data);
  44. }
  45. }
  46. static rt_err_t rt_usbh_hid_kbd_init(void* arg)
  47. {
  48. struct uhintf* intf = (struct uhintf*)arg;
  49. RT_ASSERT(intf != RT_NULL);
  50. rt_usbh_hid_set_protocal(intf, 0);
  51. rt_usbh_hid_set_idle(intf, 10, 0);
  52. LOG_D("start usb keyboard");
  53. kbd_thread = rt_thread_create("kbd0", kbd_task, intf, 1024, 8, 100);
  54. rt_thread_startup(kbd_thread);
  55. return RT_EOK;
  56. }
  57. /**
  58. * This function will define the hid keyboard protocal, it will be register to the protocal list.
  59. *
  60. * @return the keyboard protocal structure.
  61. */
  62. uprotocal_t rt_usbh_hid_protocal_kbd(void)
  63. {
  64. kbd_protocal.pro_id = USB_HID_KEYBOARD;
  65. kbd_protocal.init = rt_usbh_hid_kbd_init;
  66. kbd_protocal.callback = rt_usbh_hid_kbd_callback;
  67. return &kbd_protocal;
  68. }
  69. #endif