Skip to content

Commit 69d7be7

Browse files
authored
esp builtin to top
1 parent 56da2c0 commit 69d7be7

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

platform.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414

1515
import os
1616
import contextlib
17-
import requests
1817
import json
1918
import subprocess
2019
import sys
2120
import shutil
2221
import logging
23-
from functools import lru_cache
2422
from typing import Optional, Dict, List, Any
25-
from os.path import join
2623

2724
from platformio.public import PlatformBase, to_unix_path
2825
from platformio.proc import get_pythonexe_path
@@ -36,8 +33,12 @@
3633
MKLITTLEFS_VERSION_400 = "4.0.0"
3734
DEFAULT_DEBUG_SPEED = "5000"
3835
DEFAULT_APP_OFFSET = "0x10000"
36+
3937
ARDUINO_ESP32_PACKAGE_URL = "https://raw.githubusercontent.com/espressif/arduino-esp32/master/package/package_esp32_index.template.json"
4038

39+
# MCUs that support ESP-builtin debug
40+
ESP_BUILTIN_DEBUG_MCUS = frozenset(["esp32c3", "esp32c5", "esp32c6", "esp32s3", "esp32h2", "esp32p4"])
41+
4142
# MCU configuration
4243
MCU_TOOLCHAIN_CONFIG = {
4344
"xtensa": {
@@ -96,7 +97,7 @@ def wrapper(*args, **kwargs):
9697
return False
9798
except Exception as e:
9899
logger.error(f"Unexpected error in {operation_func.__name__}: {e}")
99-
return False
100+
raise # Re-raise unexpected exceptions
100101
return wrapper
101102

102103

@@ -123,6 +124,7 @@ def __init__(self, *args, **kwargs):
123124
super().__init__(*args, **kwargs)
124125
self._packages_dir = None
125126
self._tools_cache = {}
127+
self._mcu_config_cache = {}
126128

127129
@property
128130
def packages_dir(self) -> str:
@@ -167,13 +169,12 @@ def _run_idf_tools_install(self, tools_json_path: str, idf_tools_path: str) -> b
167169
]
168170

169171
try:
170-
with open(os.devnull, 'w') as devnull:
171-
result = subprocess.run(
172-
cmd,
173-
stdout=subprocess.DEVNULL,
174-
stderr=subprocess.DEVNULL,
175-
timeout=SUBPROCESS_TIMEOUT
176-
)
172+
result = subprocess.run(
173+
cmd,
174+
stdout=subprocess.DEVNULL,
175+
stderr=subprocess.DEVNULL,
176+
timeout=SUBPROCESS_TIMEOUT
177+
)
177178

178179
if result.returncode != 0:
179180
logger.error("idf_tools.py installation failed")
@@ -317,21 +318,25 @@ def _configure_espidf_framework(self, frameworks: List[str], variables: Dict, bo
317318

318319
def _get_mcu_config(self, mcu: str) -> Optional[Dict]:
319320
"""MCU configuration with optimized search"""
320-
for arch_name, config in MCU_TOOLCHAIN_CONFIG.items():
321+
if mcu in self._mcu_config_cache:
322+
return self._mcu_config_cache[mcu]
323+
324+
for _, config in MCU_TOOLCHAIN_CONFIG.items():
321325
if mcu in config["mcus"]:
322326
# Dynamically add ULP toolchain
323327
result = config.copy()
324328
result["ulp_toolchain"] = ["toolchain-esp32ulp"]
325329
if mcu != "esp32":
326330
result["ulp_toolchain"].append("toolchain-riscv32-esp")
331+
self._mcu_config_cache[mcu] = result
327332
return result
328333
return None
329334

330335
def _needs_debug_tools(self, variables: Dict, targets: List[str]) -> bool:
331336
"""Check if debug tools are needed"""
332337
return bool(
333338
variables.get("build_type") or
334-
"debug" in "".join(targets) or
339+
"debug" in targets or
335340
variables.get("upload_protocol")
336341
)
337342

@@ -483,7 +488,7 @@ def configure_default_packages(self, variables: Dict, targets: List[str]) -> Any
483488
logger.info("Package configuration completed successfully")
484489

485490
except Exception as e:
486-
logger.error(f"Error in package configuration: {e}")
491+
logger.error(f"Error in package configuration: {type(e).__name__}: {e}")
487492
# Don't re-raise to maintain compatibility
488493

489494
return super().configure_default_packages(variables, targets)
@@ -512,9 +517,17 @@ def _add_dynamic_options(self, board):
512517
debug = board.manifest.get("debug", {})
513518
non_debug_protocols = ["esptool", "espota"]
514519
supported_debug_tools = [
515-
"cmsis-dap", "esp-prog", "esp-bridge", "iot-bus-jtag", "jlink",
516-
"minimodule", "olimex-arm-usb-tiny-h", "olimex-arm-usb-ocd-h",
517-
"olimex-arm-usb-ocd", "olimex-jtag-tiny", "tumpa"
520+
"cmsis-dap",
521+
"esp-prog",
522+
"esp-bridge",
523+
"iot-bus-jtag",
524+
"jlink",
525+
"minimodule",
526+
"olimex-arm-usb-tiny-h",
527+
"olimex-arm-usb-ocd-h",
528+
"olimex-arm-usb-ocd",
529+
"olimex-jtag-tiny",
530+
"tumpa"
518531
]
519532

520533
# Special configuration for Kaluga board
@@ -523,7 +536,7 @@ def _add_dynamic_options(self, board):
523536

524537
# ESP-builtin for certain MCUs
525538
mcu = board.get("build.mcu", "")
526-
if mcu in ("esp32c3", "esp32c5", "esp32c6", "esp32s3", "esp32h2", "esp32p4"):
539+
if mcu in ESP_BUILTIN_DEBUG_MCUS:
527540
supported_debug_tools.append("esp-builtin")
528541

529542
upload_protocol = board.manifest.get("upload", {}).get("protocol")
@@ -595,10 +608,16 @@ def _get_openocd_interface(self, link: str, board) -> str:
595608

596609
def _get_debug_server_args(self, openocd_interface: str, debug: Dict) -> List[str]:
597610
"""Generate debug server arguments"""
611+
if 'openocd_target' in debug:
612+
config_type = 'target'
613+
config_name = debug.get('openocd_target')
614+
else:
615+
config_type = 'board'
616+
config_name = debug.get('openocd_board')
598617
return [
599618
"-s", "$PACKAGE_DIR/share/openocd/scripts",
600619
"-f", f"interface/{openocd_interface}.cfg",
601-
"-f", f"{('target', debug.get('openocd_target')) if 'openocd_target' in debug else ('board', debug.get('openocd_board'))}"
620+
"-f", f"{config_type}/{config_name}.cfg"
602621
]
603622

604623
def configure_debug_session(self, debug_config):

0 commit comments

Comments
 (0)