pipe.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include <stdio.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/semphr.h"
  5. #include "esp_err.h"
  6. #include "esp_attr.h"
  7. #include "hal/usbh_ll.h"
  8. #include "hcd.h"
  9. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  10. #include "esp32-hal-log.h"
  11. #else
  12. #include "esp_log.h"
  13. #endif
  14. #define TRANSFER_DATA_MAX_BYTES 255 //Just assume that will only IN/OUT 64 bytes for now
  15. #define BASE_PIPE_EVENT 0x8000
  16. #define CDC_BASE_PIPE_EVENT 0x1000
  17. #define HID_BASE_PIPE_EVENT 0x2000
  18. #define USBH_WEAK_CB __attribute__((weak))
  19. /**
  20. * @brief Build get string request
  21. */
  22. #define USB_CTRL_REQ_INIT_GET_STRING(ctrl_req_ptr, lang, desc_index, len) ( \
  23. { \
  24. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  25. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR; \
  26. (ctrl_req_ptr)->wValue = (USB_W_VALUE_DT_STRING << 8) | ((desc_index)&0xFF); \
  27. (ctrl_req_ptr)->wIndex = (lang); \
  28. (ctrl_req_ptr)->wLength = (len); \
  29. })
  30. typedef struct
  31. {
  32. hcd_pipe_handle_t handle;
  33. hcd_pipe_event_t event;
  34. } pipe_event_msg_t;
  35. class USBHostPipe;
  36. class USBHostPort;
  37. /**
  38. * @brief Pipe callback definition to pass events to class callback
  39. */
  40. typedef void (*pipe_cb_t)(pipe_event_msg_t msg, usb_irp_t *irp, USBHostPipe *context);
  41. void onSerialString(char* str);
  42. void onProductString(char* str);
  43. void onManufacturerString(char* str);
  44. class USBHostPipe
  45. {
  46. protected:
  47. //
  48. hcd_pipe_handle_t handle;
  49. //
  50. hcd_port_handle_t port_hdl;
  51. //
  52. xTaskHandle taskHandle;
  53. public:
  54. // friend void pipe_event_task(void *p);
  55. //
  56. usb_desc_ep_t endpoint;
  57. //
  58. pipe_cb_t callback = nullptr;
  59. //
  60. QueueHandle_t pipeEvtQueue;
  61. usb_desc_devc_t device_desc;
  62. USBHostPipe(hcd_port_handle_t handle = nullptr);
  63. ~USBHostPipe();
  64. // master port which this pipe belongs to
  65. USBHostPort *port;
  66. /**
  67. * @brief Register pipe event from class space
  68. */
  69. void onPipeEvent(pipe_cb_t cb);
  70. /**
  71. * @brief Initialize pipe from endpoint data
  72. */
  73. void init(usb_desc_ep_t *ep_desc = nullptr, uint8_t addr = 0);
  74. /**
  75. * @brief Free all queues and pipe belongs to this object
  76. */
  77. void freePipe();
  78. /**
  79. * @brief Update address, usually used with control pipe
  80. */
  81. void updateAddress(uint8_t);
  82. /**
  83. * @brief Update port handle, do we need it???
  84. */
  85. void updatePort(hcd_port_handle_t handle);
  86. /**
  87. * @brief Reset pipe
  88. */
  89. void reset();
  90. /**
  91. * @brief Get pipe state
  92. */
  93. hcd_pipe_state_t getState();
  94. /**
  95. * @brief Allocate IRP before enqueue new request
  96. */
  97. usb_irp_t *allocateIRP(size_t size);
  98. /**
  99. * @brief Free IRP and data buffer
  100. */
  101. void freeIRP(usb_irp_t *);
  102. /**
  103. * @brief Get pipe handle
  104. */
  105. hcd_pipe_handle_t getHandle();
  106. /**
  107. * @brief Send data IN request
  108. */
  109. void inData(size_t size = 0);
  110. /**
  111. * @brief Send data OUT request
  112. */
  113. void outData(uint8_t *data, size_t len);
  114. // ------------------- standard USB requests ------------------------ //
  115. /**
  116. * @brief Prepare and enqueue get device descriptor request
  117. */
  118. void getDeviceDescriptor();
  119. /**
  120. * @brief Prepare and enqueue set device address request
  121. */
  122. void setDeviceAddress(uint8_t addr);
  123. /**
  124. * @brief Prepare and enqueue get configuration descriptor request
  125. */
  126. void getCurrentConfiguration();
  127. /**
  128. * @brief Prepare and enqueue set configuration request
  129. */
  130. void setConfiguration(uint8_t);
  131. /**
  132. * @brief Prepare and enqueue get configuration descriptor request
  133. */
  134. void getConfigDescriptor(size_t n = 9);
  135. /**
  136. * @brief Prepare and enqueue get string by id request
  137. */
  138. void getString(uint8_t);
  139. void setDeviceDesc(uint8_t* data);
  140. void getSerialString();
  141. void getProductString();
  142. void getManufacturerString();
  143. };
  144. // TODO add abort IRP