|
36 | 36 | #include "host/usbh.h"
|
37 | 37 | #include "host/usbh_pvt.h"
|
38 | 38 |
|
| 39 | +//--------------------------------------------------------------------+ |
39 | 40 | // ESP32 out-of-sync
|
40 |
| -#ifdef ARDUINO_ARCH_ESP32 |
| 41 | +//--------------------------------------------------------------------+ |
| 42 | +#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5) |
| 43 | +typedef struct { |
| 44 | + uint8_t daddr; |
| 45 | + tusb_desc_interface_t desc; |
| 46 | +} tuh_itf_info_t; |
| 47 | + |
| 48 | +typedef struct { |
| 49 | + bool is_host; // host or device most |
| 50 | + union { |
| 51 | + uint8_t daddr; |
| 52 | + uint8_t rhport; |
| 53 | + uint8_t hwid; |
| 54 | + }; |
| 55 | + uint8_t ep_addr; |
| 56 | + uint8_t ep_speed; |
| 57 | + |
| 58 | + uint16_t ep_packetsize; |
| 59 | + uint16_t ep_bufsize; |
| 60 | + |
| 61 | + // TODO xfer_fifo can skip this buffer |
| 62 | + uint8_t* ep_buf; |
| 63 | + |
| 64 | + tu_fifo_t ff; |
| 65 | + |
| 66 | + // mutex: read if ep rx, write if e tx |
| 67 | + OSAL_MUTEX_DEF(ff_mutex); |
| 68 | + |
| 69 | +}tu_edpt_stream_t; |
| 70 | + |
| 71 | +// Init an stream, should only be called once |
| 72 | +bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable, |
| 73 | + void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize); |
| 74 | + |
| 75 | +// Open an stream for an endpoint |
| 76 | +// hwid is either device address (host mode) or rhport (device mode) |
| 77 | +TU_ATTR_ALWAYS_INLINE static inline |
| 78 | +void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep) { |
| 79 | + tu_fifo_clear(&s->ff); |
| 80 | + s->hwid = hwid; |
| 81 | + s->ep_addr = desc_ep->bEndpointAddress; |
| 82 | + s->ep_packetsize = tu_edpt_packet_size(desc_ep); |
| 83 | +} |
| 84 | + |
| 85 | +TU_ATTR_ALWAYS_INLINE static inline |
| 86 | +void tu_edpt_stream_close(tu_edpt_stream_t* s) { |
| 87 | + s->hwid = 0; |
| 88 | + s->ep_addr = 0; |
| 89 | +} |
| 90 | + |
| 91 | +// Clear fifo |
| 92 | +TU_ATTR_ALWAYS_INLINE static inline |
| 93 | +bool tu_edpt_stream_clear(tu_edpt_stream_t* s) { |
| 94 | + return tu_fifo_clear(&s->ff); |
| 95 | +} |
| 96 | + |
| 97 | +//--------------------------------------------------------------------+ |
| 98 | +// Stream Write |
| 99 | +//--------------------------------------------------------------------+ |
| 100 | + |
| 101 | +// Write to stream |
| 102 | +uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize); |
| 103 | + |
| 104 | +// Start an usb transfer if endpoint is not busy |
| 105 | +uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s); |
| 106 | + |
| 107 | +// Start an zero-length packet if needed |
| 108 | +bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes); |
| 109 | + |
| 110 | +// Get the number of bytes available for writing |
| 111 | +TU_ATTR_ALWAYS_INLINE static inline |
| 112 | +uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) |
| 113 | +{ |
| 114 | + return (uint32_t) tu_fifo_remaining(&s->ff); |
| 115 | +} |
| 116 | + |
| 117 | +//--------------------------------------------------------------------+ |
| 118 | +// Stream Read |
| 119 | +//--------------------------------------------------------------------+ |
| 120 | + |
| 121 | +// Read from stream |
| 122 | +uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize); |
| 123 | + |
| 124 | +// Start an usb transfer if endpoint is not busy |
| 125 | +uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s); |
| 126 | + |
| 127 | +// Must be called in the transfer complete callback |
| 128 | +TU_ATTR_ALWAYS_INLINE static inline |
| 129 | +void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) { |
| 130 | + tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes); |
| 131 | +} |
| 132 | + |
| 133 | +// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes |
| 134 | +TU_ATTR_ALWAYS_INLINE static inline |
| 135 | +void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) { |
| 136 | + if (skip_offset < xferred_bytes) { |
| 137 | + tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset)); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// Get the number of bytes available for reading |
| 142 | +TU_ATTR_ALWAYS_INLINE static inline |
| 143 | +uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t* s) { |
| 144 | + return (uint32_t) tu_fifo_count(&s->ff); |
| 145 | +} |
| 146 | + |
| 147 | +TU_ATTR_ALWAYS_INLINE static inline |
| 148 | +bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch) { |
| 149 | + return tu_fifo_peek(&s->ff, ch); |
| 150 | +} |
| 151 | + |
41 | 152 | #endif
|
42 | 153 |
|
43 | 154 | #include "cdc_host.h"
|
|
0 commit comments