ukbd.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2006-2021, 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. static struct uprotocal kbd_protocal;
  15. static rt_err_t rt_usbh_hid_kbd_callback(void* arg)
  16. {
  17. int int1, int2;
  18. struct uhid* hid;
  19. hid = (struct uhid*)arg;
  20. int1 = *(rt_uint32_t*)hid->buffer;
  21. int2 = *(rt_uint32_t*)(&hid->buffer[4]);
  22. if(int1 != 0 || int2 != 0)
  23. {
  24. RT_DEBUG_LOG(RT_DEBUG_USB, ("key down 0x%x, 0x%x\n", int1, int2));
  25. }
  26. return RT_EOK;
  27. }
  28. static rt_thread_t kbd_thread;
  29. static void kbd_task(void* param)
  30. {
  31. struct uhintf* intf = (struct uhintf*)param;
  32. while (1)
  33. {
  34. if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
  35. ((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
  36. USB_TIMEOUT_BASIC) == 0)
  37. {
  38. break;
  39. }
  40. rt_usbh_hid_kbd_callback(intf->user_data);
  41. }
  42. }
  43. static rt_err_t rt_usbh_hid_kbd_init(void* arg)
  44. {
  45. struct uhintf* intf = (struct uhintf*)arg;
  46. RT_ASSERT(intf != RT_NULL);
  47. rt_usbh_hid_set_protocal(intf, 0);
  48. rt_usbh_hid_set_idle(intf, 10, 0);
  49. RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb keyboard\n"));
  50. kbd_thread = rt_thread_create("kbd0", kbd_task, intf, 1024, 8, 100);
  51. rt_thread_startup(kbd_thread);
  52. return RT_EOK;
  53. }
  54. /**
  55. * This function will define the hid keyboard protocal, it will be register to the protocal list.
  56. *
  57. * @return the keyboard protocal structure.
  58. */
  59. uprotocal_t rt_usbh_hid_protocal_kbd(void)
  60. {
  61. kbd_protocal.pro_id = USB_HID_KEYBOARD;
  62. kbd_protocal.init = rt_usbh_hid_kbd_init;
  63. kbd_protocal.callback = rt_usbh_hid_kbd_callback;
  64. return &kbd_protocal;
  65. }
  66. #endif