Skip to content

Commit

Permalink
fix(usb_resets): Fix resetting in USB-OTG and USB-Serial/JTAG modes
Browse files Browse the repository at this point in the history
Closes #970
  • Loading branch information
radimkarnis committed Oct 21, 2024
1 parent 1b15738 commit 8298cdc
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ target_esp32s2_usbcdc:
extends: .target_esptool_test
tags:
- esptool_esp32s2_cdc_target
variables:
ESPTOOL_TEST_USB_OTG: "1"
script:
- coverage run --parallel-mode -m pytest ${CI_PROJECT_DIR}/test/test_esptool.py --port /dev/serial_ports/ESP32S2_USBCDC --chip esp32s2 --baud 115200

Expand Down Expand Up @@ -347,6 +349,8 @@ target_esp32s3_usbcdc:
extends: .target_esptool_test
tags:
- esptool_esp32s3_cdc_target
variables:
ESPTOOL_TEST_USB_OTG: "1"
script:
- coverage run --parallel-mode -m pytest ${CI_PROJECT_DIR}/test/test_esptool.py --port /dev/serial_ports/ESP32S3_USBCDC --chip esp32s3 --baud 115200

Expand Down
4 changes: 2 additions & 2 deletions docs/en/esptool/advanced-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ The ``--after`` argument allows you to specify whether the chip should be reset

.. list::

* ``--after hard_reset`` is the default. The DTR serial control line is used to reset the chip into a normal boot sequence.
:esp8266:* ``--after soft_reset`` This runs the user firmware, but any subsequent reset will return to the serial bootloader. This was the reset behaviour in esptool v1.x.
* ``--after hard_reset`` is the default. The RTS serial control line is used to reset the chip into a normal boot sequence.
:esp8266: * ``--after soft_reset`` runs the user firmware, but any subsequent reset will return to the serial bootloader. This was the reset behaviour in esptool v1.x.
* ``--after no_reset`` leaves the chip in the serial bootloader, no reset is performed.
* ``--after no_reset_stub`` leaves the chip in the stub bootloader, no reset is performed.

Expand Down
1 change: 1 addition & 0 deletions esptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"merge_bin",
"read_flash",
"read_flash_status",
"read_flash_sfdp",
"read_mac",
"read_mem",
"run",
Expand Down
11 changes: 10 additions & 1 deletion esptool/targets/esp32c2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2014-2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
Expand Down Expand Up @@ -63,6 +63,12 @@ class ESP32C2ROM(ESP32C3ROM):
[0x4037C000, 0x403C0000, "IRAM"],
]

RTCCNTL_BASE_REG = 0x60008000
RTC_CNTL_WDTCONFIG0_REG = RTCCNTL_BASE_REG + 0x0084
RTC_CNTL_WDTCONFIG1_REG = RTCCNTL_BASE_REG + 0x0088
RTC_CNTL_WDTWPROTECT_REG = RTCCNTL_BASE_REG + 0x009C
RTC_CNTL_WDT_WKEY = 0x50D83AA1

UF2_FAMILY_ID = 0x2B88D29C

KEY_PURPOSES: Dict[int, str] = {}
Expand Down Expand Up @@ -130,6 +136,9 @@ def _post_connect(self):
self.stub_is_disabled = True
self.IS_STUB = False

def hard_reset(self):
ESPLoader.hard_reset(self)

""" Try to read (encryption key) and check if it is valid """

def is_flash_encryption_key_valid(self):
Expand Down
18 changes: 17 additions & 1 deletion esptool/targets/esp32c3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2014-2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
Expand Down Expand Up @@ -83,6 +83,7 @@ class ESP32C3ROM(ESP32ROM):
RTC_CNTL_SWD_WKEY = 0x8F1D312A

RTC_CNTL_WDTCONFIG0_REG = RTCCNTL_BASE_REG + 0x0090
RTC_CNTL_WDTCONFIG1_REG = RTCCNTL_BASE_REG + 0x0094
RTC_CNTL_WDTWPROTECT_REG = RTCCNTL_BASE_REG + 0x00A8
RTC_CNTL_WDT_WKEY = 0x50D83AA1

Expand Down Expand Up @@ -252,6 +253,21 @@ def _post_connect(self):
if not self.sync_stub_detected: # Don't run if stub is reused
self.disable_watchdogs()

def hard_reset(self):
if self.uses_usb_jtag_serial():
self.rtc_wdt_reset()
else:
ESPLoader.hard_reset(self)

def rtc_wdt_reset(self):
print("Hard resetting with RTC WDT...")
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, self.RTC_CNTL_WDT_WKEY) # unlock
self.write_reg(self.RTC_CNTL_WDTCONFIG1_REG, 5000) # set WDT timeout
self.write_reg(
self.RTC_CNTL_WDTCONFIG0_REG, (1 << 31) | (5 << 28) | (1 << 8) | 2
) # enable WDT
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, 0) # lock

def check_spi_connection(self, spi_connection):
if not set(spi_connection).issubset(set(range(0, 22))):
raise FatalError("SPI Pin numbers must be in the range 0-21.")
Expand Down
3 changes: 2 additions & 1 deletion esptool/targets/esp32c6.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
Expand Down Expand Up @@ -72,6 +72,7 @@ class ESP32C6ROM(ESP32C3ROM):

DR_REG_LP_WDT_BASE = 0x600B1C00
RTC_CNTL_WDTCONFIG0_REG = DR_REG_LP_WDT_BASE + 0x0 # LP_WDT_RWDT_CONFIG0_REG
RTC_CNTL_WDTCONFIG1_REG = DR_REG_LP_WDT_BASE + 0x0004 # LP_WDT_RWDT_CONFIG1_REG
RTC_CNTL_WDTWPROTECT_REG = DR_REG_LP_WDT_BASE + 0x0018 # LP_WDT_RWDT_WPROTECT_REG

RTC_CNTL_SWD_CONF_REG = DR_REG_LP_WDT_BASE + 0x001C # LP_WDT_SWD_CONFIG_REG
Expand Down
7 changes: 6 additions & 1 deletion esptool/targets/esp32h2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# SPDX-FileCopyrightText: 2022 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later

from typing import Dict

from .esp32c6 import ESP32C6ROM
from ..loader import ESPLoader
from ..util import FatalError


Expand All @@ -18,6 +19,7 @@ class ESP32H2ROM(ESP32C6ROM):

DR_REG_LP_WDT_BASE = 0x600B1C00
RTC_CNTL_WDTCONFIG0_REG = DR_REG_LP_WDT_BASE + 0x0 # LP_WDT_RWDT_CONFIG0_REG
RTC_CNTL_WDTCONFIG1_REG = DR_REG_LP_WDT_BASE + 0x0004 # LP_WDT_RWDT_CONFIG1_REG
RTC_CNTL_WDTWPROTECT_REG = DR_REG_LP_WDT_BASE + 0x001C # LP_WDT_RWDT_WPROTECT_REG

RTC_CNTL_SWD_CONF_REG = DR_REG_LP_WDT_BASE + 0x0020 # LP_WDT_SWD_CONFIG_REG
Expand Down Expand Up @@ -77,6 +79,9 @@ def get_crystal_freq(self):
# ESP32H2 XTAL is fixed to 32MHz
return 32

def hard_reset(self):
ESPLoader.hard_reset(self)

def check_spi_connection(self, spi_connection):
if not set(spi_connection).issubset(set(range(0, 28))):
raise FatalError("SPI Pin numbers must be in the range 0-27.")
Expand Down
72 changes: 67 additions & 5 deletions esptool/targets/esp32p4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
Expand Down Expand Up @@ -72,6 +72,10 @@ class ESP32P4ROM(ESP32ROM):

FLASH_ENCRYPTED_WRITE_ALIGN = 16

UARTDEV_BUF_NO = 0x4FF3FEC8 # Variable in ROM .bss which indicates the port in use
UARTDEV_BUF_NO_USB_OTG = 5 # The above var when USB-OTG is used
UARTDEV_BUF_NO_USB_JTAG_SERIAL = 6 # The above var when USB-JTAG/Serial is used

MEMORY_MAP = [
[0x00000000, 0x00010000, "PADDING"],
[0x40000000, 0x4C000000, "DROM"],
Expand Down Expand Up @@ -105,6 +109,17 @@ class ESP32P4ROM(ESP32ROM):
12: "KM_INIT_KEY",
}

DR_REG_LP_WDT_BASE = 0x50116000
RTC_CNTL_WDTCONFIG0_REG = DR_REG_LP_WDT_BASE + 0x0 # LP_WDT_CONFIG0_REG
RTC_CNTL_WDTCONFIG1_REG = DR_REG_LP_WDT_BASE + 0x0004 # LP_WDT_CONFIG1_REG
RTC_CNTL_WDTWPROTECT_REG = DR_REG_LP_WDT_BASE + 0x0018 # LP_WDT_WPROTECT_REG
RTC_CNTL_WDT_WKEY = 0x50D83AA1

RTC_CNTL_SWD_CONF_REG = DR_REG_LP_WDT_BASE + 0x001C # RTC_WDT_SWD_CONFIG_REG
RTC_CNTL_SWD_AUTO_FEED_EN = 1 << 18
RTC_CNTL_SWD_WPROTECT_REG = DR_REG_LP_WDT_BASE + 0x0020 # RTC_WDT_SWD_WPROTECT_REG
RTC_CNTL_SWD_WKEY = 0x50D83AA1 # RTC_WDT_SWD_WKEY, same as WDT key in this case

def get_pkg_version(self):
num_word = 2
return (self.read_reg(self.EFUSE_BLOCK1_ADDR + (4 * num_word)) >> 20) & 0x07
Expand Down Expand Up @@ -191,10 +206,42 @@ def change_baud(self, baud):
ESPLoader.change_baud(self, baud)

def _post_connect(self):
pass
# TODO: Disable watchdogs when USB modes are supported in the stub
# if not self.sync_stub_detected: # Don't run if stub is reused
# self.disable_watchdogs()
if not self.sync_stub_detected: # Don't run if stub is reused
self.disable_watchdogs()

def uses_usb_otg(self):
"""
Check the UARTDEV_BUF_NO register to see if USB-OTG console is being used
"""
if self.secure_download_mode:
return False # can't detect native USB in secure download mode
return self.get_uart_no() == self.UARTDEV_BUF_NO_USB_OTG

def uses_usb_jtag_serial(self):
"""
Check the UARTDEV_BUF_NO register to see if USB-JTAG/Serial is being used
"""
if self.secure_download_mode:
return False # can't detect USB-JTAG/Serial in secure download mode
return self.get_uart_no() == self.UARTDEV_BUF_NO_USB_JTAG_SERIAL

def disable_watchdogs(self):
# When USB-JTAG/Serial is used, the RTC WDT and SWD watchdog are not reset
# and can then reset the board during flashing. Disable them.
if self.uses_usb_jtag_serial():
# Disable RTC WDT
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, self.RTC_CNTL_SWD_WKEY)
self.write_reg(self.RTC_CNTL_WDTCONFIG0_REG, 0)
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, 0)

# Automatically feed SWD
self.write_reg(self.RTC_CNTL_SWD_WPROTECT_REG, self.RTC_CNTL_SWD_WKEY)
self.write_reg(
self.RTC_CNTL_SWD_CONF_REG,
self.read_reg(self.RTC_CNTL_SWD_CONF_REG)
| self.RTC_CNTL_SWD_AUTO_FEED_EN,
)
self.write_reg(self.RTC_CNTL_SWD_WPROTECT_REG, 0)

def check_spi_connection(self, spi_connection):
if not set(spi_connection).issubset(set(range(0, 55))):
Expand All @@ -205,6 +252,21 @@ def check_spi_connection(self, spi_connection):
"consider using other pins for SPI flash connection."
)

def rtc_wdt_reset(self):
print("Hard resetting with RTC WDT...")
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, self.RTC_CNTL_WDT_WKEY) # unlock
self.write_reg(self.RTC_CNTL_WDTCONFIG1_REG, 5000) # set WDT timeout
self.write_reg(
self.RTC_CNTL_WDTCONFIG0_REG, (1 << 31) | (5 << 28) | (1 << 8) | 2
) # enable WDT
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, 0) # lock

def hard_reset(self):
if self.uses_usb_jtag_serial():
self.rtc_wdt_reset()
else:
ESPLoader.hard_reset(self)


class ESP32P4StubLoader(ESP32P4ROM):
"""Access class for ESP32P4 stub loader, runs on top of ROM.
Expand Down
50 changes: 25 additions & 25 deletions esptool/targets/esp32s2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# SPDX-FileCopyrightText: 2014-2023 Fredrik Ahlberg, Angus Gratton,
# SPDX-FileCopyrightText: 2014-2024 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later

import os
import struct
from typing import Dict

Expand Down Expand Up @@ -82,11 +81,17 @@ class ESP32S2ROM(ESP32ROM):
USB_RAM_BLOCK = 0x800 # Max block size USB-OTG is used

GPIO_STRAP_REG = 0x3F404038
GPIO_STRAP_SPI_BOOT_MASK = 0x8 # Not download mode
GPIO_STRAP_SPI_BOOT_MASK = 1 << 3 # Not download mode
GPIO_STRAP_VDDSPI_MASK = 1 << 4
RTC_CNTL_OPTION1_REG = 0x3F408128
RTC_CNTL_FORCE_DOWNLOAD_BOOT_MASK = 0x1 # Is download mode forced over USB?

RTCCNTL_BASE_REG = 0x3F408000
RTC_CNTL_WDTCONFIG0_REG = RTCCNTL_BASE_REG + 0x0094
RTC_CNTL_WDTCONFIG1_REG = RTCCNTL_BASE_REG + 0x0098
RTC_CNTL_WDTWPROTECT_REG = RTCCNTL_BASE_REG + 0x00AC
RTC_CNTL_WDT_WKEY = 0x50D83AA1

MEMORY_MAP = [
[0x00000000, 0x00010000, "PADDING"],
[0x3F000000, 0x3FF80000, "DROM"],
Expand Down Expand Up @@ -282,32 +287,27 @@ def _post_connect(self):
if self.uses_usb_otg():
self.ESP_RAM_BLOCK = self.USB_RAM_BLOCK

def _check_if_can_reset(self):
"""
Check the strapping register to see if we can reset out of download mode.
"""
if os.getenv("ESPTOOL_TESTING") is not None:
print("ESPTOOL_TESTING is set, ignoring strapping mode check")
# Esptool tests over USB-OTG run with GPIO0 strapped low,
# don't complain in this case.
return
strap_reg = self.read_reg(self.GPIO_STRAP_REG)
force_dl_reg = self.read_reg(self.RTC_CNTL_OPTION1_REG)
if (
strap_reg & self.GPIO_STRAP_SPI_BOOT_MASK == 0
and force_dl_reg & self.RTC_CNTL_FORCE_DOWNLOAD_BOOT_MASK == 0
):
raise SystemExit(
f"Error: {self.get_chip_description()} chip was placed into download "
"mode using GPIO0.\nesptool.py can not exit the download mode over "
"USB. To run the app, reset the chip manually.\n"
"To suppress this note, set --after option to 'no_reset'."
)
def rtc_wdt_reset(self):
print("Hard resetting with RTC WDT...")
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, self.RTC_CNTL_WDT_WKEY) # unlock
self.write_reg(self.RTC_CNTL_WDTCONFIG1_REG, 5000) # set WDT timeout
self.write_reg(
self.RTC_CNTL_WDTCONFIG0_REG, (1 << 31) | (5 << 28) | (1 << 8) | 2
) # enable WDT
self.write_reg(self.RTC_CNTL_WDTWPROTECT_REG, 0) # lock

def hard_reset(self):
uses_usb_otg = self.uses_usb_otg()
if uses_usb_otg:
self._check_if_can_reset()
# Check the strapping register to see if we can perform RTC WDT reset
strap_reg = self.read_reg(self.GPIO_STRAP_REG)
force_dl_reg = self.read_reg(self.RTC_CNTL_OPTION1_REG)
if (
strap_reg & self.GPIO_STRAP_SPI_BOOT_MASK == 0 # GPIO0 low
and force_dl_reg & self.RTC_CNTL_FORCE_DOWNLOAD_BOOT_MASK == 0
):
self.rtc_wdt_reset()
return

ESPLoader.hard_reset(self, uses_usb_otg)

Expand Down
Loading

0 comments on commit 8298cdc

Please sign in to comment.