Skip to content

Commit 2e995d7

Browse files
committed
adding support for esp32 for use with max3421e host
1 parent fc91e15 commit 2e995d7

File tree

21 files changed

+94
-49
lines changed

21 files changed

+94
-49
lines changed

examples/device/board_test/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#endif
5050

5151
// Espressif IDF requires "freertos/" prefix in include path
52-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
52+
#if TUP_MCU_ESPRESSIF
5353
#define CFG_TUSB_OS_INC_PATH freertos/
5454
#endif
5555

examples/device/cdc_msc_freertos/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "bsp/board_api.h"
3131
#include "tusb.h"
3232

33-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
33+
#if TUP_MCU_ESPRESSIF
3434
// ESP-IDF need "freertos/" prefix in include path.
3535
// CFG_TUSB_OS_INC_PATH should be defined accordingly.
3636
#include "freertos/FreeRTOS.h"
@@ -111,14 +111,14 @@ int main(void) {
111111
#endif
112112

113113
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
114-
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
114+
#if !TUP_MCU_ESPRESSIF
115115
vTaskStartScheduler();
116116
#endif
117117

118118
return 0;
119119
}
120120

121-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
121+
#if TUP_MCU_ESPRESSIF
122122
void app_main(void) {
123123
main();
124124
}

examples/device/cdc_msc_freertos/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#endif
6060

6161
// Espressif IDF requires "freertos/" prefix in include path
62-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
62+
#if TUP_MCU_ESPRESSIF
6363
#define CFG_TUSB_OS_INC_PATH freertos/
6464
#endif
6565

examples/device/hid_composite_freertos/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "tusb.h"
3232
#include "usb_descriptors.h"
3333

34-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
34+
#if TUP_MCU_ESPRESSIF
3535
// ESP-IDF need "freertos/" prefix in include path.
3636
// CFG_TUSB_OS_INC_PATH should be defined accordingly.
3737
#include "freertos/FreeRTOS.h"
@@ -113,14 +113,14 @@ int main(void)
113113
xTimerStart(blinky_tm, 0);
114114

115115
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
116-
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
116+
#if !TUP_MCU_ESPRESSIF
117117
vTaskStartScheduler();
118118
#endif
119119

120120
return 0;
121121
}
122122

123-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
123+
#if TUP_MCU_ESPRESSIF
124124
void app_main(void)
125125
{
126126
main();

examples/device/hid_composite_freertos/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#endif
6060

6161
// Espressif IDF requires "freertos/" prefix in include path
62-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
62+
#if TUP_MCU_ESPRESSIF
6363
#define CFG_TUSB_OS_INC_PATH freertos/
6464
#endif
6565

examples/device/video_capture/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void led_blinking_task(void* param) {
288288
#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
289289
#define VIDEO_STACK_SIZE (configMINIMAL_STACK_SIZE*4)
290290

291-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
291+
#if TUP_MCU_ESPRESSIF
292292
#define USBD_STACK_SIZE 4096
293293
int main(void);
294294
void app_main(void) {
@@ -344,7 +344,7 @@ void freertos_init_task(void) {
344344
#endif
345345

346346
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
347-
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
347+
#if !TUP_MCU_ESPRESSIF
348348
vTaskStartScheduler();
349349
#endif
350350
}

examples/device/video_capture/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#endif
5959

6060
// Espressif IDF requires "freertos/" prefix in include path
61-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
61+
#if TUP_MCU_ESPRESSIF
6262
#define CFG_TUSB_OS_INC_PATH freertos/
6363
#endif
6464

examples/device/video_capture_2ch/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ void led_blinking_task(void* param) {
296296
#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
297297
#define VIDEO_STACK_SIZE (configMINIMAL_STACK_SIZE*4)
298298

299-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
299+
#if TUP_MCU_ESPRESSIF
300300
#define USBD_STACK_SIZE 4096
301301
int main(void);
302302
void app_main(void) {
@@ -352,7 +352,7 @@ void freertos_init_task(void) {
352352
#endif
353353

354354
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
355-
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
355+
#if !TUP_MCU_ESPRESSIF
356356
vTaskStartScheduler();
357357
#endif
358358
}

examples/device/video_capture_2ch/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#endif
5959

6060
// Espressif IDF requires "freertos/" prefix in include path
61-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
61+
#if TUP_MCU_ESPRESSIF
6262
#define CFG_TUSB_OS_INC_PATH freertos/
6363
#endif
6464

examples/host/cdc_msc_hid_freertos/src/cdc_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "tusb.h"
2828
#include "bsp/board_api.h"
2929

30-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
30+
#if TUP_MCU_ESPRESSIF
3131
// ESP-IDF need "freertos/" prefix in include path.
3232
// CFG_TUSB_OS_INC_PATH should be defined accordingly.
3333
#include "freertos/FreeRTOS.h"

examples/host/cdc_msc_hid_freertos/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "bsp/board_api.h"
3131
#include "tusb.h"
3232

33-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
33+
#if TUP_MCU_ESPRESSIF
3434
// ESP-IDF need "freertos/" prefix in include path.
3535
// CFG_TUSB_OS_INC_PATH should be defined accordingly.
3636
#include "freertos/FreeRTOS.h"
@@ -107,14 +107,14 @@ int main(void) {
107107
xTimerStart(blinky_tm, 0);
108108

109109
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
110-
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
110+
#if !TUP_MCU_ESPRESSIF
111111
vTaskStartScheduler();
112112
#endif
113113

114114
return 0;
115115
}
116116

117-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
117+
#if TUP_MCU_ESPRESSIF
118118
void app_main(void) {
119119
main();
120120
}

examples/host/cdc_msc_hid_freertos/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#endif
4545

4646
// Espressif IDF requires "freertos/" prefix in include path
47-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
47+
#if TUP_MCU_ESPRESSIF
4848
#define CFG_TUSB_OS_INC_PATH freertos/
4949
#endif
5050

hw/bsp/board_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern "C" {
3939
#include "tusb.h"
4040

4141
#if CFG_TUSB_OS == OPT_OS_FREERTOS
42-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
42+
#if TUP_MCU_ESPRESSIF
4343
// ESP-IDF need "freertos/" prefix in include path.
4444
// CFG_TUSB_OS_INC_PATH should be defined accordingly.
4545
#include "freertos/FreeRTOS.h"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Apply board specific content here
2+
set(IDF_TARGET "esp32")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020, Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#ifndef BOARD_H_
28+
#define BOARD_H_
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#define NEOPIXEL_PIN 0
35+
#define NEOPIXEL_POWER_PIN 2
36+
#define NEOPIXEL_POWER_STATE 1
37+
38+
#define BUTTON_PIN 38
39+
#define BUTTON_STATE_ACTIVE 0
40+
41+
// SPI for USB host shield
42+
#define MAX3421_SPI_HOST SPI3_HOST
43+
#define MAX3421_SCK_PIN 5
44+
#define MAX3421_MOSI_PIN 19
45+
#define MAX3421_MISO_PIN 21
46+
#define MAX3421_CS_PIN 33
47+
#define MAX3421_INTR_PIN 15
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
53+
#endif /* BOARD_H_ */

hw/bsp/espressif/boards/family.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
#include "esp_rom_gpio.h"
3131
#include "esp_mac.h"
3232
#include "hal/gpio_ll.h"
33+
34+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
3335
#include "hal/usb_hal.h"
3436
#include "soc/usb_periph.h"
37+
static void configure_pins(usb_hal_context_t* usb);
38+
#endif
3539

3640
#include "driver/gpio.h"
3741
#include "driver/uart.h"
@@ -56,7 +60,6 @@ static led_strip_handle_t led_strip;
5660
static void max3421_init(void);
5761
#endif
5862

59-
static void configure_pins(usb_hal_context_t* usb);
6063

6164
//--------------------------------------------------------------------+
6265
// Implementation
@@ -100,7 +103,6 @@ void board_init(void) {
100103
};
101104

102105
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
103-
104106
led_strip_clear(led_strip); // off
105107
#endif
106108

@@ -109,6 +111,7 @@ void board_init(void) {
109111
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
110112
gpio_set_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN_ONLY : GPIO_PULLUP_ONLY);
111113

114+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
112115
// USB Controller Hal init
113116
periph_module_reset(PERIPH_USB_MODULE);
114117
periph_module_enable(PERIPH_USB_MODULE);
@@ -118,12 +121,14 @@ void board_init(void) {
118121
};
119122
usb_hal_init(&hal);
120123
configure_pins(&hal);
124+
#endif
121125

122126
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
123127
max3421_init();
124128
#endif
125129
}
126130

131+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
127132
static void configure_pins(usb_hal_context_t* usb) {
128133
/* usb_periph_iopins currently configures USB_OTG as USB Device.
129134
* Introduce additional parameters in usb_hal_context_t when adding support
@@ -153,6 +158,7 @@ static void configure_pins(usb_hal_context_t* usb) {
153158
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
154159
}
155160
}
161+
#endif
156162

157163
//--------------------------------------------------------------------+
158164
// Board porting API
@@ -208,15 +214,12 @@ SemaphoreHandle_t max3421_intr_sem;
208214

209215
static void IRAM_ATTR max3421_isr_handler(void* arg) {
210216
(void) arg; // arg is gpio num
211-
gpio_set_level(13, 1);
212217

213218
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
214219
xSemaphoreGiveFromISR(max3421_intr_sem, &xHigherPriorityTaskWoken);
215220
if (xHigherPriorityTaskWoken) {
216221
portYIELD_FROM_ISR();
217222
}
218-
219-
gpio_set_level(13, 0);
220223
}
221224

222225
static void max3421_intr_task(void* param) {
@@ -250,16 +253,12 @@ static void max3421_init(void) {
250253

251254
spi_device_interface_config_t max3421_cfg = {
252255
.mode = 0,
253-
.clock_speed_hz = 26000000,
256+
.clock_speed_hz = 20000000, // S2/S3 can work with 26 Mhz, but esp32 seems only work up to 20 Mhz
254257
.spics_io_num = -1, // manual control CS
255258
.queue_size = 1
256259
};
257260
ESP_ERROR_CHECK(spi_bus_add_device(MAX3421_SPI_HOST, &max3421_cfg, &max3421_spi));
258261

259-
// debug
260-
gpio_set_direction(13, GPIO_MODE_OUTPUT);
261-
gpio_set_level(13, 0);
262-
263262
// Interrupt pin
264263
max3421_intr_sem = xSemaphoreCreateBinary();
265264
xTaskCreate(max3421_intr_task, "max3421 intr", 2048, NULL, configMAX_PRIORITIES - 2, NULL);

hw/bsp/espressif/components/tinyusb_src/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ set(includes_public)
55
set(compile_options)
66
set(tusb_src "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src")
77

8-
if(target STREQUAL "esp32s3")
9-
set(tusb_mcu "OPT_MCU_ESP32S3")
10-
elseif(target STREQUAL "esp32s2")
11-
set(tusb_mcu "OPT_MCU_ESP32S2")
12-
else()
13-
# CONFIG_TINYUSB dependency has been guaranteed by Kconfig logic,
14-
# So it's not possible that cmake goes here
15-
message(FATAL_ERROR "TinyUSB is not support on ${target}.")
16-
return()
17-
endif()
18-
8+
string(TOUPPER OPT_MCU_${target} tusb_mcu)
199
list(APPEND compile_definitions
2010
CFG_TUSB_MCU=${tusb_mcu}
2111
CFG_TUSB_OS=OPT_OS_FREERTOS

hw/bsp/espressif/family.cmake

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ cmake_minimum_required(VERSION 3.5)
33
# Apply board specific content i.e IDF_TARGET must be set before project.cmake is included
44
include("${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake")
55

6-
if(IDF_TARGET STREQUAL "esp32s2")
7-
set(FAMILY_MCUS ESP32S2)
8-
elseif(IDF_TARGET STREQUAL "esp32s3")
9-
set(FAMILY_MCUS ESP32S3)
10-
endif()
6+
string(TOUPPER ${IDF_TARGET} FAMILY_MCUS)
117

128
# Add example src and bsp directories
139
set(EXTRA_COMPONENT_DIRS "src" "${CMAKE_CURRENT_LIST_DIR}/boards" "${CMAKE_CURRENT_LIST_DIR}/components")

src/common/tusb_mcu.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@
329329
#define TUP_USBIP_DWC2
330330
#define TUP_DCD_ENDPOINT_MAX 6
331331

332+
#elif TU_CHECK_MCU(OPT_MCU_ESP32) && (CFG_TUD_ENABLED || !(defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421))
333+
#error "MCUs are only supported with CFG_TUH_MAX3421 enabled"
334+
332335
//--------------------------------------------------------------------+
333336
// Dialog
334337
//--------------------------------------------------------------------+
@@ -426,7 +429,7 @@
426429
#define TUP_MCU_MULTIPLE_CORE 0
427430
#endif
428431

429-
#ifndef TUP_DCD_ENDPOINT_MAX
432+
#if CFG_TUD_ENABLED && !defined(TUP_DCD_ENDPOINT_MAX)
430433
#warning "TUP_DCD_ENDPOINT_MAX is not defined for this MCU, default to 8"
431434
#define TUP_DCD_ENDPOINT_MAX 8
432435
#endif

src/portable/analog/max3421/hcd_max3421.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ bool hcd_init(uint8_t rhport) {
484484

485485
// v1 is 0x01, v2 is 0x12, v3 is 0x13
486486
uint8_t const revision = reg_read(rhport, REVISION_ADDR, false);
487-
TU_ASSERT(revision == 0x01 || revision == 0x12 || revision == 0x13, false);
488487
TU_LOG2_HEX(revision);
488+
TU_ASSERT(revision == 0x01 || revision == 0x12 || revision == 0x13, false);
489489

490490
// reset
491491
reg_write(rhport, USBCTL_ADDR, USBCTL_CHIPRES, false);

0 commit comments

Comments
 (0)