usbd_vendor.rst 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. vendor device 驱动编写
  2. ===========================
  3. 本节主要介绍如何编写一个 vendor device 驱动。
  4. - 首先复制一份 class/template/usbd_xxx.c 文件
  5. - 实现以下三个回调函数,通常来说,vendor 驱动只需要实现 vendor_handler
  6. .. code-block:: C
  7. intf->class_interface_handler = xxx_class_interface_request_handler;
  8. intf->class_endpoint_handler = NULL;
  9. intf->vendor_handler = NULL;
  10. intf->notify_handler = xxx_notify_handler;
  11. - 举例如下
  12. case1 演示对于主机 IN 数据的处理,将数据拷贝到 *data 中,并指定*len 的长度。协议栈会自动发送给主机,不需要用户手动调用发送 API。
  13. case2 演示对于主机 OUT 数据的处理,当执行到此函数时,说明数据都已经接收完成,可以直接读取 *data 中的数据,长度为 *len。
  14. .. code-block:: C
  15. static int xxx_vendor_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  16. {
  17. USB_LOG_WRN("XXX Class request: "
  18. "bRequest 0x%02x\r\n",
  19. setup->bRequest);
  20. switch (setup->bRequest) {
  21. case 1:
  22. memcpy(*data, xxx, sizeof(xxx));
  23. *len = sizeof(xxx);
  24. case 2:
  25. hexdump(*data, *len);
  26. default:
  27. USB_LOG_WRN("Unhandled XXX Class bRequest 0x%02x\r\n", setup->bRequest);
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. - 最后使用形如 usbd_add_interface(busid, usbd_xxx_init_intf(&intf)) 注册接口