Skip to content

Commit

Permalink
update to work better with arduino-esp32 master and latest release 2.…
Browse files Browse the repository at this point in the history
…0.14
  • Loading branch information
hathach committed Oct 6, 2023
1 parent 33c2a08 commit 4d04364
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 194 deletions.
11 changes: 8 additions & 3 deletions src/arduino/Adafruit_USBH_Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 9 additions & 53 deletions src/arduino/ports/esp32/tusb_config_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h>
#include <string.h>

#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

Expand All @@ -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
Expand Down
117 changes: 0 additions & 117 deletions src/class/cdc/cdc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions src/class/hid/hid_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/class/vendor/vendor_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -292,4 +290,3 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
}

#endif
#endif
3 changes: 0 additions & 3 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/host/usbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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__)
Expand Down
4 changes: 2 additions & 2 deletions src/portable/analog/max3421/hcd_max3421.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d04364

Please sign in to comment.