Skip to content

Commit 819a49b

Browse files
authored
Merge pull request adafruit#406 from adafruit/prevent-s3-jtag-enumerate
prevent S3 enumerated as USB JTAG while waiting for 2nd reset
2 parents 1afb3f6 + 7899657 commit 819a49b

File tree

9 files changed

+64
-45
lines changed

9 files changed

+64
-45
lines changed

.github/actions/setup_toolchain/action.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ inputs:
44
toolchain:
55
description: 'Toolchain name'
66
required: true
7-
toolchain_url:
8-
description: 'Toolchain URL or version'
7+
toolchain_version:
8+
description: 'Toolchain version'
99
required: false
1010

1111
outputs:
@@ -28,7 +28,26 @@ runs:
2828
uses: ./.github/actions/setup_toolchain/espressif
2929
with:
3030
toolchain: ${{ inputs.toolchain }}
31-
toolchain_url: ${{ inputs.toolchain_url }}
31+
toolchain_version: ${{ inputs.toolchain_version }}
32+
33+
- name: Get Toolchain URL
34+
if: >-
35+
inputs.toolchain != 'arm-gcc' &&
36+
inputs.toolchain != 'arm-iar' &&
37+
inputs.toolchain != 'esp-idf'
38+
id: set-toolchain-url
39+
run: |
40+
TOOLCHAIN_JSON='{
41+
"aarch64-gcc": "https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-aarch64-none-elf.tar.xz",
42+
"arm-clang": "https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm/releases/download/release-17.0.1/LLVMEmbeddedToolchainForArm-17.0.1-Linux-x86_64.tar.xz",
43+
"msp430-gcc": "http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSPGCC/9_2_0_0/export/msp430-gcc-9.2.0.50_linux64.tar.bz2",
44+
"riscv-gcc": "https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v13.2.0-2/xpack-riscv-none-elf-gcc-13.2.0-2-linux-x64.tar.gz",
45+
"rx-gcc": "http://gcc-renesas.com/downloads/get.php?f=rx/8.3.0.202004-gnurx/gcc-8.3.0.202004-GNURX-ELF.run"
46+
}'
47+
TOOLCHAIN_URL=$(echo $TOOLCHAIN_JSON | jq -r '.["${{ inputs.toolchain }}"]')
48+
echo "toolchain_url=$TOOLCHAIN_URL"
49+
echo "toolchain_url=$TOOLCHAIN_URL" >> $GITHUB_OUTPUT
50+
shell: bash
3251

3352
- name: Download Toolchain
3453
if: >-
@@ -38,4 +57,4 @@ runs:
3857
uses: ./.github/actions/setup_toolchain/download
3958
with:
4059
toolchain: ${{ inputs.toolchain }}
41-
toolchain_url: ${{ inputs.toolchain_url }}
60+
toolchain_url: ${{ steps.set-toolchain-url.outputs.toolchain_url }}

.github/actions/setup_toolchain/espressif/action.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ inputs:
44
toolchain:
55
description: 'Toolchain name'
66
required: true
7-
toolchain_url:
8-
description: 'Toolchain URL or version'
7+
toolchain_version:
8+
description: 'Toolchain version'
99
required: true
1010

1111
runs:
@@ -22,14 +22,14 @@ runs:
2222
id: cache-toolchain-espressif
2323
with:
2424
path: ${{ env.DOCKER_ESP_IDF }}
25-
key: ${{ inputs.toolchain }}-${{ inputs.toolchain_url }}
25+
key: ${{ inputs.toolchain }}-${{ inputs.toolchain_version }}
2626

2727
- name: Pull and Save Docker Image
2828
if: steps.cache-toolchain-espressif.outputs.cache-hit != 'true'
2929
run: |
30-
docker pull espressif/idf:${{ inputs.toolchain_url }}
30+
docker pull espressif/idf:${{ inputs.toolchain_version }}
3131
mkdir -p $(dirname $DOCKER_ESP_IDF)
32-
docker save -o $DOCKER_ESP_IDF espressif/idf:${{ inputs.toolchain_url }}
32+
docker save -o $DOCKER_ESP_IDF espressif/idf:${{ inputs.toolchain_version }}
3333
du -sh $DOCKER_ESP_IDF
3434
shell: bash
3535

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
boards: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['espressif'].board) }}
8787
build-system: 'make'
8888
toolchain: 'esp-idf'
89-
toolchain_url: 'v5.1.1'
89+
toolchain_version: 'v5.1.4'
9090

9191
# ---------------------------------------
9292
# Build RISC-V
@@ -106,7 +106,6 @@ jobs:
106106
boards: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.port].board) }}
107107
build-system: 'make'
108108
toolchain: 'riscv-gcc'
109-
toolchain_url: 'https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v13.2.0-2/xpack-riscv-none-elf-gcc-13.2.0-2-linux-x64.tar.gz'
110109

111110
# ---------------------------------------
112111
# Unit testing with ghostfat

.github/workflows/build_ghostfat.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
working-directory: ports/test_ghostfat/_build/${{ matrix.board }}
4646

4747
- name: Save newly generated self-test images as CI artifacts
48-
uses: actions/upload-artifact@v3
48+
uses: actions/upload-artifact@v4
4949
with:
50-
name: ghostfat_selftest_images
50+
name: ghostfat_${{ matrix.board }}_image
5151
path: ./ports/test_ghostfat/_build/${{ matrix.board }}/ghostfat_${{ matrix.board }}.img.gz.gz

.github/workflows/build_util.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616
toolchain:
1717
required: true
1818
type: string
19-
toolchain_url:
19+
toolchain_version:
2020
required: false
2121
type: string
2222

@@ -41,7 +41,7 @@ jobs:
4141
uses: ./.github/actions/setup_toolchain
4242
with:
4343
toolchain: ${{ inputs.toolchain }}
44-
toolchain_url: ${{ inputs.toolchain_url }}
44+
toolchain_version: ${{ inputs.toolchain_version }}
4545

4646
- name: Get Dependencies
4747
run: |
@@ -58,7 +58,7 @@ jobs:
5858
5959
- name: Build using ESP-IDF docker
6060
if: inputs.toolchain == 'esp-idf'
61-
run: docker run --rm -v $PWD:/project -w /project espressif/idf:${{ inputs.toolchain_url }} /bin/bash -c "git config --global --add safe.directory /project && make -C ports/espressif/ BOARD=${{ matrix.board }} all self-update copy-artifact"
61+
run: docker run --rm -v $PWD:/project -w /project espressif/idf:${{ inputs.toolchain_version }} /bin/bash -c "git config --global --add safe.directory /project && make -C ports/espressif/ BOARD=${{ matrix.board }} all self-update copy-artifact"
6262

6363
- uses: actions/upload-artifact@v4
6464
with:

ports/espressif/CMakeLists.txt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ add_custom_command(TARGET app POST_BUILD
3939
# Post build: update arduino-esp32 bootloader for debug purpose
4040
# -------------------------------------------------------------
4141
if (0)
42-
#set(ARDUINO_VARIANT_DIR /home/hathach/code/arduino-esp32/variants/${BOARD})
43-
set(ARDUINO_VARIANT_DIR /home/hathach/code/arduino-esp32/variants/adafruit_feather_esp32s3)
42+
set(ARDUINO_VARIANT_DIR $ENV{HOME}/code/arduino-esp32/variants/${BOARD})
43+
#set(ARDUINO_VARIANT_DIR $ENV{HOME}/code/arduino-esp32/variants/adafruit_feather_esp32s3_nopsram)
4444

4545
add_custom_command(TARGET bootloader POST_BUILD
4646
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bootloader/bootloader.bin ${ARDUINO_VARIANT_DIR}/bootloader-tinyuf2.bin
@@ -49,17 +49,4 @@ if (0)
4949
add_custom_command(TARGET app POST_BUILD
5050
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/tinyuf2.bin ${ARDUINO_VARIANT_DIR}/tinyuf2.bin
5151
)
52-
53-
file(STRINGS ${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/sdkconfig SDKCONFIG_CONTENTS)
54-
foreach (line ${SDKCONFIG_CONTENTS})
55-
if (line MATCHES "CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"(.*).csv\"")
56-
set(PARTITION_TABLE_CSV ${CMAKE_MATCH_1})
57-
cmake_print_variables(PARTITION_TABLE_CSV)
58-
break()
59-
endif ()
60-
endforeach ()
61-
62-
add_custom_command(TARGET app POST_BUILD
63-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/${PARTITION_TABLE_CSV}.csv ${ARDUINO_VARIANT_DIR}/${PARTITION_TABLE_CSV}-tinyuf2.csv
64-
)
6552
endif ()

ports/espressif/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The project is composed of customizing the 2nd stage bootloader from IDF and UF2 factory application as 3rd stage bootloader.
44

5-
**Note**: IDF is actively developed and change very often, TinyUF2 is developed and tested with IDF v5.1.1. Should you have a problem please try to change your IDF version.
5+
**Note**: IDF is actively developed and change very often, TinyUF2 is developed and tested with IDF v5.1.4. Should you have a problem please try to change your IDF version.
66

77
Following boards are supported:
88

ports/espressif/boards/boards.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void board_timer_stop(void) {
345345

346346
#if CONFIG_IDF_TARGET_ESP32S3
347347
#include "hal/usb_serial_jtag_ll.h"
348-
#include "hal/usb_phy_ll.h"
348+
#include "hal/usb_fsls_phy_ll.h"
349349

350350
static void hw_cdc_reset_handler(void *arg) {
351351
portBASE_TYPE xTaskWoken = 0;
@@ -389,7 +389,7 @@ static void usb_switch_to_cdc_jtag(void) {
389389
gpio_set_level((gpio_num_t)USBPHY_DP_NUM, 0);
390390

391391
// Initialize CDC+JTAG ISR to listen for BUS_RESET
392-
usb_phy_ll_int_jtag_enable(&USB_SERIAL_JTAG);
392+
usb_fsls_phy_ll_int_jtag_enable(&USB_SERIAL_JTAG);
393393
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
394394
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
395395
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_BUS_RESET);
@@ -446,6 +446,8 @@ void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
446446
// copied from Arduino's usb_persist_restart()
447447
esp_register_shutdown_handler(usb_persist_shutdown_handler);
448448
#if CONFIG_IDF_TARGET_ESP32S3
449+
// Switch to JTAG since S3 bootrom has issue with uploading with USB OTG
450+
// https://github.com/espressif/arduino-esp32/issues/6762#issuecomment-1128621518
449451
usb_switch_to_cdc_jtag();
450452
#endif
451453
esp_restart();

ports/espressif/components/bootloader/subproject/main/bootloader_start.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
185185
}
186186
#endif // CONFIG_BOOTLOADER_APP_TEST
187187

188-
// UF2: check if Application want to load uf2 "bootloader" with reset reason hint.
188+
// TinyUF2: check if Application want to load uf2 "bootloader" with reset reason hint.
189189
if ( boot_index != FACTORY_INDEX ) {
190190
// Application request to enter UF2 with Software Reset with reason hint
191191
if ( reset_reason == RESET_REASON_CORE_SW || reset_reason == RESET_REASON_CPU0_SW ) {
@@ -199,10 +199,10 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
199199
}
200200
}
201201

202-
// UF2: check if GPIO0 is pressed and/or 1-bit RC on specific GPIO detect double reset
202+
// TinyUF2: when reset by EN/nRST pin: check if GPIO0 is pressed and/or 1-bit RC on specific GPIO detect double reset
203203
// during this time. If yes then to load uf2 "bootloader".
204-
if ( boot_index != FACTORY_INDEX ) {
205-
#ifdef PIN_DOUBLE_RESET_RC
204+
if (boot_index != FACTORY_INDEX && reset_reason == RESET_REASON_CHIP_POWER_ON) {
205+
#ifdef PIN_DOUBLE_RESET_RC
206206
// Double reset detect if board implements 1-bit memory with RC components
207207
esp_rom_gpio_pad_select_gpio(PIN_DOUBLE_RESET_RC);
208208
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[PIN_DOUBLE_RESET_RC]);
@@ -215,11 +215,18 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
215215
// Set to high to charge the RC, indicating we are in reset
216216
gpio_ll_output_enable(&GPIO, PIN_DOUBLE_RESET_RC);
217217
gpio_ll_set_level(&GPIO, PIN_DOUBLE_RESET_RC, 1);
218-
#else
219-
{
220-
#endif
221-
// turn led on if there is actually waiting
218+
}
219+
#endif
220+
221+
if (boot_index != FACTORY_INDEX) {
222222
if (UF2_DETECTION_DELAY_MS > 0){
223+
#if CONFIG_IDF_TARGET_ESP32S3
224+
// S3 startup with USB JTAG, while delaying here, USB JTAG will be enumerated which can cause confusion when
225+
// switching to OTG in application. Switch to OTG PHY here to avoid this.
226+
SET_PERI_REG_MASK(RTC_CNTL_USB_CONF_REG,
227+
RTC_CNTL_SW_HW_USB_PHY_SEL | RTC_CNTL_SW_USB_PHY_SEL | RTC_CNTL_USB_PAD_ENABLE);
228+
#endif
229+
223230
board_led_on();
224231
}
225232

@@ -232,14 +239,19 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
232239
do {
233240
if ( gpio_ll_get_level(&GPIO, PIN_BUTTON_UF2) == 0 ) {
234241
ESP_LOGI(TAG, "Detect GPIO %d active to enter UF2 bootloader", PIN_BUTTON_UF2);
235-
236-
// Simply return factory index without erasing any other partition
237242
boot_index = FACTORY_INDEX;
238243
break;
239244
}
240245
} while (UF2_DETECTION_DELAY_MS > (esp_log_early_timestamp() - tm_start) );
241246

242-
board_led_off();
247+
if (UF2_DETECTION_DELAY_MS > 0){
248+
#if CONFIG_IDF_TARGET_ESP32S3
249+
CLEAR_PERI_REG_MASK(RTC_CNTL_USB_CONF_REG,
250+
RTC_CNTL_SW_HW_USB_PHY_SEL | RTC_CNTL_SW_USB_PHY_SEL | RTC_CNTL_USB_PAD_ENABLE);
251+
#endif
252+
253+
board_led_off();
254+
}
243255
}
244256

245257
#if PIN_DOUBLE_RESET_RC

0 commit comments

Comments
 (0)