Skip to content

Commit

Permalink
only attempt to wait for 2nd reset if reset by by by power on (En/nRS…
Browse files Browse the repository at this point in the history
…T pin)

temporarily change USB phy to OTG while waiting for 2nd reset, otherwise S3 can be enumerated with USB JTAG.
  • Loading branch information
hathach committed Aug 30, 2024
1 parent 1afb3f6 commit 43e5ee3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ports/espressif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ add_custom_command(TARGET app POST_BUILD
# Post build: update arduino-esp32 bootloader for debug purpose
# -------------------------------------------------------------
if (0)
#set(ARDUINO_VARIANT_DIR /home/hathach/code/arduino-esp32/variants/${BOARD})
set(ARDUINO_VARIANT_DIR /home/hathach/code/arduino-esp32/variants/adafruit_feather_esp32s3)
set(ARDUINO_VARIANT_DIR $ENV{HOME}/code/arduino-esp32/variants/${BOARD})
#set(ARDUINO_VARIANT_DIR $ENV{HOME}/code/arduino-esp32/variants/adafruit_feather_esp32s3)

add_custom_command(TARGET bootloader POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bootloader/bootloader.bin ${ARDUINO_VARIANT_DIR}/bootloader-tinyuf2.bin
Expand Down
2 changes: 2 additions & 0 deletions ports/espressif/boards/boards.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
// copied from Arduino's usb_persist_restart()
esp_register_shutdown_handler(usb_persist_shutdown_handler);
#if CONFIG_IDF_TARGET_ESP32S3
// Switch to JTAG since S3 bootrom has issue with uploading with USB OTG
// https://github.com/espressif/arduino-esp32/issues/6762#issuecomment-1128621518
usb_switch_to_cdc_jtag();
#endif
esp_restart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
}
#endif // CONFIG_BOOTLOADER_APP_TEST

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

// UF2: check if GPIO0 is pressed and/or 1-bit RC on specific GPIO detect double reset
// TinyUF2: when reset by EN/nRST pin: check if GPIO0 is pressed and/or 1-bit RC on specific GPIO detect double reset
// during this time. If yes then to load uf2 "bootloader".
if ( boot_index != FACTORY_INDEX ) {
#ifdef PIN_DOUBLE_RESET_RC
if (boot_index != FACTORY_INDEX && reset_reason == RESET_REASON_CHIP_POWER_ON) {
#ifdef PIN_DOUBLE_RESET_RC
// Double reset detect if board implements 1-bit memory with RC components
esp_rom_gpio_pad_select_gpio(PIN_DOUBLE_RESET_RC);
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[PIN_DOUBLE_RESET_RC]);
Expand All @@ -215,11 +215,18 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
// Set to high to charge the RC, indicating we are in reset
gpio_ll_output_enable(&GPIO, PIN_DOUBLE_RESET_RC);
gpio_ll_set_level(&GPIO, PIN_DOUBLE_RESET_RC, 1);
#else
{
#endif
// turn led on if there is actually waiting
}
#endif

if (boot_index != FACTORY_INDEX) {
if (UF2_DETECTION_DELAY_MS > 0){
#if CONFIG_IDF_TARGET_ESP32S3
// S3 startup with USB JTAG, while delaying here, USB JTAG will be enumerated which can cause confusion when
// switching to OTG in application. Switch to OTG PHY here to avoid this.
SET_PERI_REG_MASK(RTC_CNTL_USB_CONF_REG,
RTC_CNTL_SW_HW_USB_PHY_SEL | RTC_CNTL_SW_USB_PHY_SEL | RTC_CNTL_USB_PAD_ENABLE);
#endif

board_led_on();
}

Expand All @@ -232,14 +239,19 @@ static int selected_boot_partition(const bootloader_state_t *bs) {
do {
if ( gpio_ll_get_level(&GPIO, PIN_BUTTON_UF2) == 0 ) {
ESP_LOGI(TAG, "Detect GPIO %d active to enter UF2 bootloader", PIN_BUTTON_UF2);

// Simply return factory index without erasing any other partition
boot_index = FACTORY_INDEX;
break;
}
} while (UF2_DETECTION_DELAY_MS > (esp_log_early_timestamp() - tm_start) );

board_led_off();
if (UF2_DETECTION_DELAY_MS > 0){
#if CONFIG_IDF_TARGET_ESP32S3
CLEAR_PERI_REG_MASK(RTC_CNTL_USB_CONF_REG,
RTC_CNTL_SW_HW_USB_PHY_SEL | RTC_CNTL_SW_USB_PHY_SEL | RTC_CNTL_USB_PAD_ENABLE);
#endif

board_led_off();
}
}

#if PIN_DOUBLE_RESET_RC
Expand Down

0 comments on commit 43e5ee3

Please sign in to comment.