Adafruit_TinyUSB.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #define CFG_TUD_MEM_ALIGN __attribute__((aligned(4)))
  5. #define CFG_TUSB_DEBUG 0
  6. enum xfer_result_t {
  7. XFER_RESULT_SUCCESS,
  8. XFER_RESULT_FAILED,
  9. XFER_RESULT_STALLED,
  10. XFER_RESULT_TIMEOUT,
  11. XFER_RESULT_INVALID,
  12. };
  13. enum {
  14. TUSB_DESC_STRING = 3,
  15. TUSB_DESC_ENDPOINT = 5,
  16. TUSB_DIR_OUT = 0,
  17. TUSB_DIR_IN = 1,
  18. };
  19. struct __attribute__((packed)) tusb_desc_interface_t {
  20. uint8_t bLength, bDescriptorType, bInterfaceNumber, bAlternateSetting;
  21. uint8_t bNumEndpoints, bInterfaceClass, bInterfaceSubClass;
  22. uint8_t bInterfaceProtocol, iInterface;
  23. };
  24. struct __attribute__((packed)) tusb_desc_endpoint_t {
  25. uint8_t bLength, bDescriptorType, bEndpointAddress, bmAttributes;
  26. uint16_t wMaxPacketSize;
  27. uint8_t bInterval;
  28. };
  29. struct __attribute__((packed)) tusb_desc_device_t {
  30. uint8_t bLength, bDescriptorType;
  31. uint16_t bcdUSB;
  32. uint8_t bDeviceClass, bDeviceSubClass, bDeviceProtocol, bMaxPacketSize0;
  33. uint16_t idVendor, idProduct, bcdDevice;
  34. uint8_t iManufacturer, iProduct, iSerialNumber, bNumConfigurations;
  35. };
  36. struct tusb_control_request_t {
  37. uint8_t bmRequestType;
  38. uint8_t bRequest;
  39. uint16_t wValue;
  40. uint16_t wIndex;
  41. uint16_t wLength;
  42. };
  43. enum {
  44. CONTROL_STAGE_SETUP,
  45. CONTROL_STAGE_DATA,
  46. CONTROL_STAGE_ACK,
  47. };
  48. extern "C" {
  49. bool tud_control_xfer(uint8_t rhport, const tusb_control_request_t *request,
  50. void *buffer, uint16_t length);
  51. bool tud_mounted();
  52. const uint8_t *tud_descriptor_device_cb();
  53. }
  54. class Adafruit_USBD_Interface {
  55. protected:
  56. uint8_t _strid = 0;
  57. public:
  58. virtual ~Adafruit_USBD_Interface() = default;
  59. virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
  60. uint16_t bufsize) = 0;
  61. void setStringDescriptor(const char *str);
  62. };
  63. class Adafruit_USBD_Device {
  64. public:
  65. uint8_t allocInterface(uint8_t count);
  66. uint8_t allocEndpoint(uint8_t direction);
  67. bool addInterface(Adafruit_USBD_Interface &interface);
  68. void setID(uint16_t vendor, uint16_t product);
  69. void setVersion(uint16_t version);
  70. void setDeviceVersion(uint16_t version);
  71. void setManufacturerDescriptor(const char *str);
  72. void setProductDescriptor(const char *str);
  73. void setSerialDescriptor(const char *str);
  74. void setConfigurationAttribute(uint8_t attributes);
  75. void setConfigurationMaxPower(uint16_t milliamps);
  76. };
  77. extern Adafruit_USBD_Device TinyUSBDevice;
  78. #define USBDevice TinyUSBDevice