Skip to content

Commit

Permalink
improve compatibility with esp32
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach committed Oct 4, 2023
1 parent 4f81112 commit 2c8cb9e
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 71 deletions.
5 changes: 5 additions & 0 deletions src/Adafruit_TinyUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
#endif

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

// Device
Expand Down
40 changes: 33 additions & 7 deletions src/arduino/Adafruit_USBH_Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* THE SOFTWARE.
*/

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#define MSBFIRST SPI_MSBFIRST
#endif

#include "tusb_option.h"

#if CFG_TUH_ENABLED
Expand Down Expand Up @@ -118,7 +124,15 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
//--------------------------------------------------------------------+
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421

static void max3421_isr(void) { tuh_int_handler(1); }
static void max3421_isr(void) {
// ESP32 out-of-sync
#if defined(ARDUINO_ARCH_ESP32)
extern "C" void hcd_int_handler_esp32(uint8_t rhport, bool in_isr);
hcd_int_handler_esp32(1, false);
#else
tuh_int_handler(1, true);
#endif
}

extern "C" {

Expand Down Expand Up @@ -152,21 +166,20 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
SPISettings config(max_clock, MSBFIRST, SPI_MODE0);
host->_spi->beginTransaction(config);

size_t count = 0;
while (count < tx_len || count < rx_len) {
// TODO improve spi speed with burst transfer
for (size_t count = 0; count < tx_len || count < rx_len; count++) {
uint8_t data = 0x00;

// TX
if (count < tx_len) {
data = tx_buf[count];
}

data = host->_spi->transfer(data);

// RX
if (count < rx_len) {
rx_buf[count] = data;
}

count++;
}

host->_spi->endTransaction();
Expand All @@ -179,10 +192,12 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
if (!Adafruit_USBH_Host::_instance) {
return;
}
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
(void)host;

#ifdef ARDUINO_ARCH_SAMD
//--- SAMD51 ---//
#ifdef __SAMD51__
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
const IRQn_Type irq =
(IRQn_Type)(EIC_0_IRQn + g_APinDescription[host->_intr].ulExtInt);

Expand All @@ -192,6 +207,7 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
NVIC_DisableIRQ(irq);
}
#else
//--- SAMD21 ---//
if (enabled) {
NVIC_EnableIRQ(EIC_IRQn);
} else {
Expand All @@ -200,12 +216,22 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
#endif

#elif defined(ARDUINO_NRF52_ADAFRUIT)
//--- nRF52 ---//
if (enabled) {
NVIC_EnableIRQ(GPIOTE_IRQn);
} else {
NVIC_DisableIRQ(GPIOTE_IRQn);
}

#elif defined(ARDUINO_ARCH_ESP32)
//--- ESP32 ---//
if (enabled) {
gpio_intr_enable((gpio_num_t)host->_intr);
} else {
gpio_intr_disable((gpio_num_t)host->_intr);
}
#else
#error "MAX3421e host is not Unsupported by this architecture"
#endif
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/arduino/msc/Adafruit_USBH_MSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* THE SOFTWARE.
*/

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if CFG_TUH_ENABLED && CFG_TUH_MSC
Expand Down
72 changes: 44 additions & 28 deletions src/arduino/ports/esp32/tusb_config_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* THE SOFTWARE.
*/

#ifndef _TUSB_CONFIG_ESP32_H_
#define _TUSB_CONFIG_ESP32_H_
#ifndef TUSB_CONFIG_ESP32_H_
#define TUSB_CONFIG_ESP32_H_

#ifdef __cplusplus
extern "C" {
Expand All @@ -43,11 +43,14 @@ extern "C" {
//--------------------------------------------------------------------+
// ESP32 out-of-sync
//--------------------------------------------------------------------+
#ifndef tu_static
#define tu_static static
#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;
Expand All @@ -63,25 +66,37 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
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

#ifndef CFG_TUD_LOG_LEVEL
#define CFG_TUD_LOG_LEVEL 2
#endif

#if 0
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
#define CFG_TUSB_MCU OPT_MCU_NRF5X

#define CFG_TUSB_OS OPT_OS_FREERTOS
#define CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
// #ifndef CFG_TUH_LOG_LEVEL
// #define CFG_TUH_LOG_LEVEL 2
// #endif

#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0
Expand All @@ -90,27 +105,30 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
// For selectively disable device log (when > CFG_TUSB_DEBUG)
// #define CFG_TUD_LOG_LEVEL 3

#ifdef USE_TINYUSB
// Enable device stack
#define CFG_TUD_ENABLED 1

// Enable host stack with MAX3421E (host shield)
#define CFG_TUH_ENABLED 1
#define CFG_TUH_MAX3421 1

#else
#define CFG_TUD_ENABLED 0
#define CFG_TUH_ENABLED 0
#endif

//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------

// device configuration is configured in BSP
// sdk/include/arduino_tinyusb/include/tusb_config.h

//--------------------------------------------------------------------
// Host Configuration
//--------------------------------------------------------------------

// Enable host stack with MAX3421E (host shield)
#define CFG_TUH_ENABLED 0
#define CFG_TUH_MAX_SPEED OPT_MODE_HIGH_SPEED
#ifndef TUH_OPT_HIGH_SPEED
#define TUH_OPT_HIGH_SPEED 0
#endif

#define CFG_TUH_MAX3421 1

#ifndef CFG_TUH_MAX3421_ENDPOINT_TOTAL
#define CFG_TUH_MAX3421_ENDPOINT_TOTAL (8 + 4 * (CFG_TUH_DEVICE_MAX - 1))
#endif

// Size of buffer to hold descriptors and other data used for enumeration
#define CFG_TUH_ENUMERATION_BUFSIZE 256

Expand Down Expand Up @@ -150,8 +168,6 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM \
{ 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }

#endif

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/class/cdc/cdc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
* This file is part of the TinyUSB stack.
*/

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_CDC)

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_CDC)

#include "device/usbd.h"
#include "device/usbd_pvt.h"

Expand Down
8 changes: 4 additions & 4 deletions src/class/dfu/dfu_rt_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
* This file is part of the TinyUSB stack.
*/

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME)

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME)

#include "device/usbd.h"
#include "device/usbd_pvt.h"

Expand Down
10 changes: 5 additions & 5 deletions src/class/hid/hid_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
* This file is part of the TinyUSB stack.
*/

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_HID)
Expand All @@ -32,11 +37,6 @@
// INCLUDE
//--------------------------------------------------------------------+

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "device/usbd.h"
#include "device/usbd_pvt.h"

Expand Down
10 changes: 5 additions & 5 deletions src/class/midi/midi_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
* This file is part of the TinyUSB stack.
*/

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_MIDI)
Expand All @@ -32,11 +37,6 @@
// INCLUDE
//--------------------------------------------------------------------+

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "device/usbd.h"
#include "device/usbd_pvt.h"

Expand Down
8 changes: 4 additions & 4 deletions src/class/msc/msc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
* This file is part of the TinyUSB stack.
*/

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_MSC)

// ESP32 out-of-sync
#ifdef ARDUINO_ARCH_ESP32
#include "arduino/ports/esp32/tusb_config_esp32.h"
#endif

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_MSC)

#include "device/dcd.h" // for faking dcd_event_xfer_complete
#include "device/usbd.h"
#include "device/usbd_pvt.h"
Expand Down
8 changes: 4 additions & 4 deletions src/class/vendor/vendor_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
* This file is part of the TinyUSB stack.
*/

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR)

// 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

#include "tusb_option.h"

#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR)

#include "device/usbd.h"
#include "device/usbd_pvt.h"

Expand Down
Loading

0 comments on commit 2c8cb9e

Please sign in to comment.