Skip to content

Commit 749d1ad

Browse files
Jan Beranradimkarnis
Jan Beran
authored andcommitted
feat: print usb mode when output chip info
1 parent 23f11f0 commit 749d1ad

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

esptool/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,14 @@ def add_spi_flash_subparsers(
829829
)
830830

831831
if esp.secure_download_mode:
832-
print("Chip is %s in Secure Download Mode" % esp.CHIP_NAME)
832+
print(f"Chip is {esp.CHIP_NAME} in Secure Download Mode")
833833
else:
834-
print("Chip is %s" % (esp.get_chip_description()))
835-
print("Features: %s" % ", ".join(esp.get_chip_features()))
836-
print("Crystal is %dMHz" % esp.get_crystal_freq())
834+
print(f"Chip is {esp.get_chip_description()}")
835+
print(f"Features: {', '.join(esp.get_chip_features())}")
836+
print(f"Crystal is {esp.get_crystal_freq()}MHz")
837+
usb_mode = esp.get_usb_mode()
838+
if usb_mode is not None:
839+
print(f"USB mode: {usb_mode}")
837840
read_mac(esp, args)
838841

839842
if not args.no_stub:

esptool/loader.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,34 @@ def get_uart_no(self):
10471047
"""
10481048
Read the UARTDEV_BUF_NO register to get the number of the currently used console
10491049
"""
1050-
if self.cache["uart_no"] is None:
1051-
self.cache["uart_no"] = self.read_reg(self.UARTDEV_BUF_NO) & 0xFF
1052-
return self.cache["uart_no"]
1050+
# Some ESP chips do not have this register
1051+
try:
1052+
if self.cache["uart_no"] is None:
1053+
self.cache["uart_no"] = self.read_reg(self.UARTDEV_BUF_NO) & 0xFF
1054+
return self.cache["uart_no"]
1055+
except AttributeError:
1056+
return None
1057+
1058+
def uses_usb_jtag_serial(self):
1059+
"""
1060+
Check if the chip uses USB JTAG/SERIAL mode.
1061+
"""
1062+
return False
1063+
1064+
def uses_usb_otg(self):
1065+
"""
1066+
Check if the chip uses USB OTG mode.
1067+
"""
1068+
return False
1069+
1070+
def get_usb_mode(self):
1071+
"""
1072+
Get the USB mode of the chip: USB-Serial/JTAG or USB-OTG. If the usb_mode is None, external USB-UART is used.
1073+
"""
1074+
usb_jtag_serial = self.uses_usb_jtag_serial()
1075+
usb_otg = self.uses_usb_otg()
1076+
1077+
return "USB-Serial/JTAG" if usb_jtag_serial else "USB-OTG" if usb_otg else None
10531078

10541079
@classmethod
10551080
def parse_flash_size_arg(cls, arg):

esptool/targets/esp32h2.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class ESP32H2ROM(ESP32C6ROM):
2424
RTC_CNTL_SWD_WPROTECT_REG = DR_REG_LP_WDT_BASE + 0x0024 # LP_WDT_SWD_WPROTECT_REG
2525
RTC_CNTL_SWD_WKEY = 0x50D83AA1 # LP_WDT_SWD_WKEY, same as WDT key in this case
2626

27+
UARTDEV_BUF_NO = 0x4084FEFC # Variable in ROM .bss which indicates the port in use
28+
UARTDEV_BUF_NO_USB_JTAG_SERIAL = 3 # The above var when USB-JTAG/Serial is used
29+
2730
FLASH_FREQUENCY = {
2831
"48m": 0xF,
2932
"24m": 0x0,

test/test_esptool.py

+16
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,22 @@ def test_auto_detect(self):
13531353
self._check_output(output)
13541354

13551355

1356+
class TestUSBMode(EsptoolTestCase):
1357+
@pytest.mark.quick_test
1358+
def test_usb_mode(self):
1359+
output = self.run_esptool("chip_id")
1360+
expected_usb_mode = (
1361+
"USB-OTG"
1362+
if os.environ.get("ESPTOOL_TEST_USB_OTG") == "1"
1363+
else "USB-Serial/JTAG"
1364+
if arg_preload_port
1365+
else None
1366+
)
1367+
1368+
if expected_usb_mode:
1369+
assert f"USB mode: {expected_usb_mode}" in output
1370+
1371+
13561372
@pytest.mark.flaky(reruns=5)
13571373
@pytest.mark.skipif(arg_preload_port is not False, reason="USB-to-UART bridge only")
13581374
@pytest.mark.skipif(os.name == "nt", reason="Linux/MacOS only")

0 commit comments

Comments
 (0)