Skip to content

Commit a07f9fb

Browse files
committed
compile with both esp32 master (idf 4.4.3) and platformio (idf 4.4.5)
1 parent 788d47d commit a07f9fb

File tree

4 files changed

+126
-9
lines changed

4 files changed

+126
-9
lines changed

src/arduino/ports/esp32/tusb_config_esp32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ enum { TUSB_INDEX_INVALID_8 = 0xFFu };
117117
//--------------------------------------------------------------------
118118

119119
// Enable host stack with MAX3421E (host shield)
120-
#define CFG_TUH_ENABLED 0
120+
#define CFG_TUH_ENABLED 1
121121
#define CFG_TUH_MAX_SPEED OPT_MODE_HIGH_SPEED
122122
#ifndef TUH_OPT_HIGH_SPEED
123123
#define TUH_OPT_HIGH_SPEED 0

src/class/cdc/cdc_host.c

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,119 @@
3636
#include "host/usbh.h"
3737
#include "host/usbh_pvt.h"
3838

39+
//--------------------------------------------------------------------+
3940
// 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+
41152
#endif
42153

43154
#include "cdc_host.h"

src/class/hid/hid_host.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
#include "host/usbh.h"
3737
#include "host/usbh_pvt.h"
3838

39+
// ESP32 out-of-sync
40+
#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5)
41+
typedef struct {
42+
uint8_t daddr;
43+
tusb_desc_interface_t desc;
44+
} tuh_itf_info_t;
45+
#endif
46+
3947
#include "hid_host.h"
4048

4149
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged

src/tusb_config.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@
3939
#include "arduino/ports/rp2040/tusb_config_rp2040.h"
4040

4141
#elif defined(ARDUINO_ARCH_ESP32)
42-
// Note: won't reach this if compiling with Arduino IDEs since Arduino IDE will include one
43-
// in the the BSP sdk/include/arduino_tinyusb/include/tusb_config.h
44-
45-
#if defined(PLATFORMIO)
46-
// For platformio prioritize this file over the one in BSP, include BSP one is required
47-
// For consistency: include the one in BSP
42+
// Note: when compiling core Arduino IDEs will include tusb_config.h in the BSP
43+
// sdk/include/arduino_tinyusb/include. While compiling .c file in this library this
44+
// file will be used instead. For consistency: include the one in BSP here as well
4845
#include "../../arduino_tinyusb/include/tusb_config.h"
49-
#endif
46+
47+
// Note: For platformio prioritize this file over the one in BSP in all cases
5048

5149
#else
5250
#error TinyUSB Arduino Library does not support your core yet

0 commit comments

Comments
 (0)