Skip to content

Commit 4c78381

Browse files
authored
add possibility to add board specific script (#291)
1 parent 39f9a30 commit 4c78381

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

boards/m5stack-tab5-p4.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"build": {
3+
"core": "esp32",
4+
"extra_flags": [
5+
"-DBOARD_HAS_PSRAM",
6+
"-DARDUINO_USB_CDC_ON_BOOT=1",
7+
"-DARDUINO_USB_MODE=1"
8+
],
9+
"f_cpu": "360000000L",
10+
"f_flash": "80000000L",
11+
"flash_mode": "qio",
12+
"mcu": "esp32p4",
13+
"variant": "m5stack_tab5"
14+
},
15+
"arduino": {
16+
"partitions": "default_16MB.csv"
17+
},
18+
"connectivity": [
19+
"bluetooth",
20+
"openthread"
21+
],
22+
"debug": {
23+
"openocd_target": "esp32p4.cfg"
24+
},
25+
"frameworks": [
26+
"arduino",
27+
"espidf"
28+
],
29+
"name": "M5STACK Tab5 esp32-p4 Board",
30+
"upload": {
31+
"flash_size": "16MB",
32+
"maximum_ram_size": 512000,
33+
"maximum_size": 16777216,
34+
"require_upload_port": true,
35+
"speed": 1500000
36+
},
37+
"url": "https://docs.m5stack.com/en/core/Tab5",
38+
"vendor": "M5STACK"
39+
}

boards/m5stack-tab5-p4.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def configure_board(env):
2+
if "arduino" in env.get("PIOFRAMEWORK", []):
3+
deps = ["https://github.com/M5Stack/M5Unified.git"]
4+
# Install libraries using PlatformIO Library Manager
5+
from pathlib import Path
6+
from platformio.package.manager.library import LibraryPackageManager
7+
8+
lib_dir = Path(env.subst("$PROJECT_DIR")) / ".pio" / "libdeps" / env.subst("$PIOENV")
9+
lm = LibraryPackageManager(package_dir=lib_dir)
10+
11+
for lib in deps:
12+
lm.install(lib)

builder/main.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import sys
2121
from os.path import isfile, join
2222
from pathlib import Path
23+
import importlib.util
2324

2425
from SCons.Script import (
2526
ARGUMENTS,
@@ -40,12 +41,43 @@
4041
platform = env.PioPlatform()
4142
projectconfig = env.GetProjectConfig()
4243
terminal_cp = locale.getpreferredencoding().lower()
43-
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoespressif32")
44-
platformio_dir = projectconfig.get("platformio", "core_dir")
44+
platform_dir = Path(env.PioPlatform().get_dir())
45+
framework_dir = platform.get_package_dir("framework-arduinoespressif32")
46+
core_dir = projectconfig.get("platformio", "core_dir")
47+
build_dir = Path(projectconfig.get("platformio", "build_dir"))
4548

4649
# Setup Python virtual environment and get executable paths
47-
PYTHON_EXE, esptool_binary_path = setup_python_environment(env, platform, platformio_dir)
50+
PYTHON_EXE, esptool_binary_path = setup_python_environment(env, platform, core_dir)
4851

52+
# Initialize board configuration and MCU settings
53+
board = env.BoardConfig()
54+
board_id = env.subst("$BOARD")
55+
mcu = board.get("build.mcu", "esp32")
56+
is_xtensa = mcu in ("esp32", "esp32s2", "esp32s3")
57+
toolchain_arch = "xtensa-%s" % mcu
58+
filesystem = board.get("build.filesystem", "littlefs")
59+
60+
61+
def load_board_script(env):
62+
if not board_id:
63+
return
64+
65+
script_path = platform_dir / "boards" / f"{board_id}.py"
66+
67+
if script_path.exists():
68+
try:
69+
spec = importlib.util.spec_from_file_location(
70+
f"board_{board_id}",
71+
str(script_path)
72+
)
73+
board_module = importlib.util.module_from_spec(spec)
74+
spec.loader.exec_module(board_module)
75+
76+
if hasattr(board_module, 'configure_board'):
77+
board_module.configure_board(env)
78+
79+
except Exception as e:
80+
print(f"Error loading board script {board_id}.py: {e}")
4981

5082
def BeforeUpload(target, source, env):
5183
"""
@@ -412,12 +444,8 @@ def switch_off_ldf():
412444
projectconfig.set(env_section, "lib_ldf_mode", "off")
413445

414446

415-
# Initialize board configuration and MCU settings
416-
board = env.BoardConfig()
417-
mcu = board.get("build.mcu", "esp32")
418-
is_xtensa = mcu in ("esp32", "esp32s2", "esp32s3")
419-
toolchain_arch = "xtensa-%s" % mcu
420-
filesystem = board.get("build.filesystem", "littlefs")
447+
# Board specific script
448+
load_board_script(env)
421449

422450
# Set toolchain architecture for RISC-V based ESP32 variants
423451
if not is_xtensa:
@@ -706,7 +734,7 @@ def firmware_metrics(target, source, env):
706734
"espressif32.html#over-the-air-ota-update\n"
707735
)
708736
env.Replace(
709-
UPLOADER=str(Path(FRAMEWORK_DIR).resolve() / "tools" / "espota.py"),
737+
UPLOADER=str(Path(framework_dir).resolve() / "tools" / "espota.py"),
710738
UPLOADERFLAGS=["--debug", "--progress", "-i", "$UPLOAD_PORT"],
711739
UPLOADCMD=f'"{PYTHON_EXE}" "$UPLOADER" $UPLOADERFLAGS -f $SOURCE',
712740
)

examples/arduino-blink/platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ custom_component_remove = espressif/esp_hosted
206206
espressif/rmaker_common
207207
chmorgan/esp-libhelix-mp3
208208

209-
[env:esp32-p4]
209+
[env:m5stack-tab5-p4]
210210
platform = espressif32
211211
framework = arduino
212-
board = esp32-p4
212+
board = m5stack-tab5-p4
213213
build_flags = -DLED_BUILTIN=2
214214
lib_ignore = wifi
215215
ble

0 commit comments

Comments
 (0)