diff --git a/src/arduino/Adafruit_USBH_Host.cpp b/src/arduino/Adafruit_USBH_Host.cpp index e8d6c1eb..470998df 100644 --- a/src/arduino/Adafruit_USBH_Host.cpp +++ b/src/arduino/Adafruit_USBH_Host.cpp @@ -25,6 +25,7 @@ // ESP32 out-of-sync #ifdef ARDUINO_ARCH_ESP32 #include "arduino/ports/esp32/tusb_config_esp32.h" +#include "driver/gpio.h" #define MSBFIRST SPI_MSBFIRST #endif @@ -39,8 +40,11 @@ Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL; Adafruit_USBH_Host::Adafruit_USBH_Host(void) { Adafruit_USBH_Host::_instance = this; + +#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 _spi = NULL; _cs = _intr = _sck = _mosi = _miso = -1; +#endif } #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 @@ -161,11 +165,12 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, #if defined(ARDUINO_ARCH_ESP32) -#ifdef PLATFORMIO -#define tuh_int_handler_esp32 tuh_int_handler -#else +#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(2, 0, 14) && \ + !defined(PLATFORMIO) extern "C" void hcd_int_handler_esp32(uint8_t rhport, bool in_isr); #define tuh_int_handler_esp32 hcd_int_handler_esp32 +#else +#define tuh_int_handler_esp32 tuh_int_handler #endif static void max3421_intr_task(void *param) { diff --git a/src/arduino/ports/esp32/tusb_config_esp32.h b/src/arduino/ports/esp32/tusb_config_esp32.h index 45974cef..ede090be 100644 --- a/src/arduino/ports/esp32/tusb_config_esp32.h +++ b/src/arduino/ports/esp32/tusb_config_esp32.h @@ -43,65 +43,20 @@ extern "C" { //--------------------------------------------------------------------+ // ESP32 out-of-sync //--------------------------------------------------------------------+ -#include "esp_idf_version.h" - -// IDF 4.4.4 and prior is using tinyusb 0.14.0 -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5) -#include -#include - -#define tu_static static -static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count) { - if (count > destsz) { - return -1; - } - memset(dest, ch, count); - return 0; -} -static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src, - size_t count) { - if (count > destsz) { - return -1; - } - memcpy(dest, src, count); - return 0; -} - -enum { - TUSB_EPSIZE_BULK_FS = 64, - TUSB_EPSIZE_BULK_HS = 512, - - TUSB_EPSIZE_ISO_FS_MAX = 1023, - TUSB_EPSIZE_ISO_HS_MAX = 1024, -}; - -enum { TUSB_INDEX_INVALID_8 = 0xFFu }; -#endif - -#ifndef CFG_TUD_MEM_SECTION -#define CFG_TUD_MEM_SECTION CFG_TUSB_MEM_SECTION -#endif - -#ifndef CFG_TUH_MEM_SECTION -#define CFG_TUH_MEM_SECTION CFG_TUSB_MEM_SECTION -#endif - -#ifndef CFG_TUH_MEM_ALIGN -#define CFG_TUH_MEM_ALIGN CFG_TUSB_MEM_ALIGN -#endif +#include "esp_arduino_version.h" #ifndef CFG_TUD_LOG_LEVEL #define CFG_TUD_LOG_LEVEL 2 #endif -// #ifndef CFG_TUH_LOG_LEVEL -// #define CFG_TUH_LOG_LEVEL 2 -// #endif - -#ifndef CFG_TUSB_DEBUG -#define CFG_TUSB_DEBUG 0 +#ifndef CFG_TUH_LOG_LEVEL +#define CFG_TUH_LOG_LEVEL 2 #endif +// #ifndef CFG_TUSB_DEBUG +// #define CFG_TUSB_DEBUG 0 +// #endif + // For selectively disable device log (when > CFG_TUSB_DEBUG) // #define CFG_TUD_LOG_LEVEL 3 @@ -118,7 +73,8 @@ enum { TUSB_INDEX_INVALID_8 = 0xFFu }; // Enable host stack with MAX3421E (host shield) #define CFG_TUH_ENABLED 1 -#define CFG_TUH_MAX_SPEED OPT_MODE_HIGH_SPEED +#define CFG_TUH_MAX_SPEED OPT_MODE_FULL_SPEED + #ifndef TUH_OPT_HIGH_SPEED #define TUH_OPT_HIGH_SPEED 0 #endif diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 87b37e26..22751f28 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -36,125 +36,8 @@ #include "host/usbh.h" #include "host/usbh_pvt.h" -//--------------------------------------------------------------------+ -// ESP32 out-of-sync -//--------------------------------------------------------------------+ -#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5) -typedef struct { - uint8_t daddr; - tusb_desc_interface_t desc; -} tuh_itf_info_t; - -typedef struct { - bool is_host; // host or device most - union { - uint8_t daddr; - uint8_t rhport; - uint8_t hwid; - }; - uint8_t ep_addr; - uint8_t ep_speed; - - uint16_t ep_packetsize; - uint16_t ep_bufsize; - - // TODO xfer_fifo can skip this buffer - uint8_t* ep_buf; - - tu_fifo_t ff; - - // mutex: read if ep rx, write if e tx - OSAL_MUTEX_DEF(ff_mutex); - -}tu_edpt_stream_t; - -// Init an stream, should only be called once -bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable, - void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize); - -// Open an stream for an endpoint -// hwid is either device address (host mode) or rhport (device mode) -TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep) { - tu_fifo_clear(&s->ff); - s->hwid = hwid; - s->ep_addr = desc_ep->bEndpointAddress; - s->ep_packetsize = tu_edpt_packet_size(desc_ep); -} - -TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_close(tu_edpt_stream_t* s) { - s->hwid = 0; - s->ep_addr = 0; -} - -// Clear fifo -TU_ATTR_ALWAYS_INLINE static inline -bool tu_edpt_stream_clear(tu_edpt_stream_t* s) { - return tu_fifo_clear(&s->ff); -} - -//--------------------------------------------------------------------+ -// Stream Write -//--------------------------------------------------------------------+ - -// Write to stream -uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize); - -// Start an usb transfer if endpoint is not busy -uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s); - -// Start an zero-length packet if needed -bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes); - -// Get the number of bytes available for writing -TU_ATTR_ALWAYS_INLINE static inline -uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) -{ - return (uint32_t) tu_fifo_remaining(&s->ff); -} - -//--------------------------------------------------------------------+ -// Stream Read -//--------------------------------------------------------------------+ - -// Read from stream -uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize); - -// Start an usb transfer if endpoint is not busy -uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s); - -// Must be called in the transfer complete callback -TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) { - tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes); -} - -// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes -TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) { - if (skip_offset < xferred_bytes) { - tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset)); - } -} - -// Get the number of bytes available for reading -TU_ATTR_ALWAYS_INLINE static inline -uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t* s) { - return (uint32_t) tu_fifo_count(&s->ff); -} - -TU_ATTR_ALWAYS_INLINE static inline -bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch) { - return tu_fifo_peek(&s->ff, ch); -} -#endif -#endif - #include "cdc_host.h" - // Level where CFG_TUSB_DEBUG must be at least for this driver is logged #ifndef CFG_TUH_CDC_LOG_LEVEL #define CFG_TUH_CDC_LOG_LEVEL CFG_TUH_LOG_LEVEL diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 3b1f940d..9c26739f 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -36,16 +36,6 @@ #include "host/usbh.h" #include "host/usbh_pvt.h" -// ESP32 out-of-sync -#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5) -typedef struct { - uint8_t daddr; - tusb_desc_interface_t desc; -} tuh_itf_info_t; -#endif -#endif - #include "hid_host.h" // Level where CFG_TUSB_DEBUG must be at least for this driver is logged diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index ed93edac..374edd51 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -25,11 +25,9 @@ */ // ESP32 out-of-sync -// Somehow we have linking issue: multiple definition of vendor APIs with arduino-esp32 master -// skip this driver entirely and used the pre-compiled libarduino_tinyusb.a instead #ifdef ARDUINO_ARCH_ESP32 #include "arduino/ports/esp32/tusb_config_esp32.h" -#else +#endif #include "tusb_option.h" @@ -292,4 +290,3 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint } #endif -#endif diff --git a/src/device/usbd.c b/src/device/usbd.c index effb2b8d..67ddd83e 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -44,9 +44,6 @@ // ESP32 out-of-sync //--------------------------------------------------------------------+ #if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) -TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size); -TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc); - #ifndef TU_LOG_BUF #if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL static inline void tu_print_buf(uint8_t const* buf, uint32_t bufsize) { diff --git a/src/host/usbh.c b/src/host/usbh.c index 7937d3c4..abed89c1 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -54,6 +54,11 @@ // ESP32 out-of-sync //--------------------------------------------------------------------+ #ifdef ARDUINO_ARCH_ESP32 + +#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(2, 0, 14) && !defined(PLATFORMIO) +extern bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr); +#endif + #ifndef TU_LOG_BUF #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL static inline void tu_print_buf(uint8_t const* buf, uint32_t bufsize) { @@ -65,8 +70,7 @@ #endif #endif -extern bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr); -#endif +#endif // ESP32 #ifndef TU_LOG_USBH #define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__) diff --git a/src/portable/analog/max3421/hcd_max3421.c b/src/portable/analog/max3421/hcd_max3421.c index 37a5b542..807a2dcb 100644 --- a/src/portable/analog/max3421/hcd_max3421.c +++ b/src/portable/analog/max3421/hcd_max3421.c @@ -677,7 +677,7 @@ bool hcd_setup_send(uint8_t rhport, uint8_t daddr, uint8_t const setup_packet[8] } // ESP32 out-of-sync -#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) +#if defined(ARDUINO_ARCH_ESP32) && ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(2, 0, 14) && !defined(PLATFORMIO) bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) { #else // clear stall, data toggle is also reset to DATA0 @@ -875,7 +875,7 @@ void print_hirq(uint8_t hirq) { #endif // ESP32 out-of-sync -#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO) +#if defined(ARDUINO_ARCH_ESP32) && ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(2, 0, 14) && !defined(PLATFORMIO) void hcd_int_handler_esp32(uint8_t rhport, bool in_isr) { #else // Interrupt handler