-
-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathmain.py
504 lines (436 loc) · 15.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Copyright 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import sys
from os.path import isfile, join
from SCons.Script import (
ARGUMENTS, COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
DefaultEnvironment)
from platformio.util import get_serial_ports
#
# Helpers
#
def BeforeUpload(target, source, env):
upload_options = {}
if "BOARD" in env:
upload_options = env.BoardConfig().get("upload", {})
env.AutodetectUploadPort()
before_ports = get_serial_ports()
if upload_options.get("use_1200bps_touch", False):
env.TouchSerialPort("$UPLOAD_PORT", 1200)
if upload_options.get("wait_for_upload_port", False):
env.Replace(UPLOAD_PORT=env.WaitForNewSerialPort(before_ports))
def _get_board_f_flash(env):
frequency = env.subst("$BOARD_F_FLASH")
frequency = str(frequency).replace("L", "")
return str(int(int(frequency) / 1000000)) + "m"
def _get_board_flash_mode(env):
mode = env.subst("$BOARD_FLASH_MODE")
if mode == "qio":
return "dio"
elif mode == "qout":
return "dout"
return mode
def _parse_size(value):
if isinstance(value, int):
return value
elif value.isdigit():
return int(value)
elif value.startswith("0x"):
return int(value, 16)
elif value[-1].upper() in ("K", "M"):
base = 1024 if value[-1].upper() == "K" else 1024 * 1024
return int(value[:-1]) * base
return value
def _parse_partitions(env):
partitions_csv = env.subst("$PARTITIONS_TABLE_CSV")
if not isfile(partitions_csv):
sys.stderr.write("Could not find the file %s with partitions "
"table.\n" % partitions_csv)
env.Exit(1)
return
result = []
next_offset = 0
bound = 0x10000 # default value
with open(partitions_csv) as fp:
for line in fp.readlines():
line = line.strip()
if not line or line.startswith("#"):
continue
tokens = [t.strip() for t in line.split(",")]
if len(tokens) < 5:
continue
partition = {
"name": tokens[0],
"type": tokens[1],
"subtype": tokens[2],
"offset": tokens[3] or next_offset,
"size": tokens[4],
"flags": tokens[5] if len(tokens) > 5 else None
}
result.append(partition)
next_offset = _parse_size(partition["offset"])
if (partition["subtype"] == "ota_0"):
bound = next_offset
next_offset = (next_offset + bound - 1) & ~(bound - 1)
env["ESP32_APP_OFFSET"] = hex(bound)
return result
def _update_max_upload_size(env):
if not env.get("PARTITIONS_TABLE_CSV"):
return
sizes = [
_parse_size(p['size']) for p in _parse_partitions(env)
if p['type'] in ("0", "app")
]
if sizes:
board.update("upload.maximum_size", max(sizes))
def _to_unix_slashes(path):
return path.replace("\\", "/")
#
# Filesystem helpers
#
def fetch_fs_size(env):
fs = None
for p in _parse_partitions(env):
if p["type"] == "data" and p["subtype"] in ("spiffs", "fat"):
fs = p
if not fs:
sys.stderr.write(
"Could not find the any filesystem section in the partitions "
"table %s\n" % env.subst("$PARTITIONS_TABLE_CSV")
)
env.Exit(1)
return
env["FS_START"] = _parse_size(fs["offset"])
env["FS_SIZE"] = _parse_size(fs["size"])
env["FS_PAGE"] = int("0x100", 16)
env["FS_BLOCK"] = int("0x1000", 16)
# FFat specific offsets, see:
# https://github.com/lorol/arduino-esp32fatfs-plugin#notes-for-fatfs
if filesystem == "fatfs":
env["FS_START"] += 4096
env["FS_SIZE"] -= 4096
def __fetch_fs_size(target, source, env):
fetch_fs_size(env)
return (target, source)
env = DefaultEnvironment()
env.SConscript("compat.py", exports="env")
platform = env.PioPlatform()
board = env.BoardConfig()
mcu = board.get("build.mcu", "esp32")
toolchain_arch = "xtensa-%s" % mcu
filesystem = board.get("build.filesystem", "spiffs")
if mcu == "esp32c3":
toolchain_arch = "riscv32-esp"
env.Replace(
__get_board_f_flash=_get_board_f_flash,
__get_board_flash_mode=_get_board_flash_mode,
AR="%s-elf-ar" % toolchain_arch,
AS="%s-elf-as" % toolchain_arch,
CC="%s-elf-gcc" % toolchain_arch,
CXX="%s-elf-g++" % toolchain_arch,
GDB="%s-elf-gdb" % toolchain_arch,
OBJCOPY=join(
platform.get_package_dir("tool-esptoolpy") or "", "esptool.py"),
RANLIB="%s-elf-ranlib" % toolchain_arch,
SIZETOOL="%s-elf-size" % toolchain_arch,
ARFLAGS=["rc"],
SIZEPROGREGEXP=r"^(?:\.iram0\.text|\.iram0\.vectors|\.dram0\.data|\.flash\.text|\.flash\.rodata|)\s+([0-9]+).*",
SIZEDATAREGEXP=r"^(?:\.dram0\.data|\.dram0\.bss|\.noinit)\s+([0-9]+).*",
SIZECHECKCMD="$SIZETOOL -A -d $SOURCES",
SIZEPRINTCMD="$SIZETOOL -B -d $SOURCES",
ERASEFLAGS=[
"--chip", mcu,
"--port", '"$UPLOAD_PORT"'
],
ERASECMD='"$PYTHONEXE" "$OBJCOPY" $ERASEFLAGS erase_flash',
# mkspiffs package contains two different binaries for IDF and Arduino
MKFSTOOL="mk%s" % filesystem
+ (
(
"_${PIOPLATFORM}_"
+ (
"espidf"
if "espidf" in env.subst("$PIOFRAMEWORK")
else "${PIOFRAMEWORK}"
)
)
if filesystem == "spiffs"
else ""
),
# Legacy `ESP32_SPIFFS_IMAGE_NAME` is used as the second fallback value for
# backward compatibility
ESP32_FS_IMAGE_NAME=env.get(
"ESP32_FS_IMAGE_NAME", env.get("ESP32_SPIFFS_IMAGE_NAME", filesystem)
),
PROGSUFFIX=".elf"
)
# Allow user to override via pre:script
if env.get("PROGNAME", "program") == "program":
env.Replace(PROGNAME="firmware")
env.Append(
# copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode)
ASFLAGS=env.get("CCFLAGS", [])[:],
BUILDERS=dict(
ElfToBin=Builder(
action=env.VerboseAction(" ".join([
'"$PYTHONEXE" "$OBJCOPY"',
"--chip", mcu, "elf2image",
"--flash_mode", "$BOARD_FLASH_MODE",
"--flash_freq", "${__get_board_f_flash(__env__)}",
"--flash_size", board.get("upload.flash_size", "detect"),
"-o", "$TARGET", "$SOURCES"
]), "Building $TARGET"),
suffix=".bin"
),
DataToBin=Builder(
action=env.VerboseAction(
" ".join(
['"$MKFSTOOL"', "-c", "$SOURCES", "-s", "$FS_SIZE"]
+ (
[
"-p",
"$FS_PAGE",
"-b",
"$FS_BLOCK",
]
if filesystem in ("spiffs", "littlefs")
else []
)
+ ["$TARGET"]
),
"Building FS image from '$SOURCES' directory to $TARGET",
),
emitter=__fetch_fs_size,
source_factory=env.Dir,
suffix=".bin",
),
)
)
if not env.get("PIOFRAMEWORK"):
env.SConscript("frameworks/_bare.py", exports="env")
#
# Target: Build executable and linkable firmware or FS image
#
target_elf = None
if "nobuild" in COMMAND_LINE_TARGETS:
target_elf = join("$BUILD_DIR", "${PROGNAME}.elf")
if set(["uploadfs", "uploadfsota"]) & set(COMMAND_LINE_TARGETS):
fetch_fs_size(env)
target_firm = join("$BUILD_DIR", "${ESP32_FS_IMAGE_NAME}.bin")
else:
target_firm = join("$BUILD_DIR", "${PROGNAME}.bin")
else:
target_elf = env.BuildProgram()
if set(["buildfs", "uploadfs", "uploadfsota"]) & set(COMMAND_LINE_TARGETS):
target_firm = env.DataToBin(
join("$BUILD_DIR", "${ESP32_FS_IMAGE_NAME}"), "$PROJECT_DATA_DIR"
)
env.NoCache(target_firm)
AlwaysBuild(target_firm)
else:
target_firm = env.ElfToBin(
join("$BUILD_DIR", "${PROGNAME}"), target_elf)
env.Depends(target_firm, "checkprogsize")
env.AddPlatformTarget("buildfs", target_firm, target_firm, "Build Filesystem Image")
AlwaysBuild(env.Alias("nobuild", target_firm))
target_buildprog = env.Alias("buildprog", target_firm, target_firm)
# update max upload size based on CSV file
if env.get("PIOMAINPROG"):
env.AddPreAction(
"checkprogsize",
env.VerboseAction(
lambda source, target, env: _update_max_upload_size(env),
"Retrieving maximum program size $SOURCES"))
# remove after PIO Core 3.6 release
elif set(["checkprogsize", "upload"]) & set(COMMAND_LINE_TARGETS):
_update_max_upload_size(env)
#
# Target: Print binary size
#
target_size = env.AddPlatformTarget(
"size",
target_elf,
env.VerboseAction("$SIZEPRINTCMD", "Calculating size $SOURCE"),
"Program Size",
"Calculate program size",
)
#
# Target: Upload firmware or FS image
#
upload_protocol = env.subst("$UPLOAD_PROTOCOL")
debug_tools = board.get("debug.tools", {})
upload_actions = []
# Compatibility with old OTA configurations
if (upload_protocol != "espota"
and re.match(r"\"?((([0-9]{1,3}\.){3}[0-9]{1,3})|[^\\/]+\.local)\"?$",
env.get("UPLOAD_PORT", ""))):
upload_protocol = "espota"
sys.stderr.write(
"Warning! We have just detected `upload_port` as IP address or host "
"name of ESP device. `upload_protocol` is switched to `espota`.\n"
"Please specify `upload_protocol = espota` in `platformio.ini` "
"project configuration file.\n")
if upload_protocol == "espota":
if not env.subst("$UPLOAD_PORT"):
sys.stderr.write(
"Error: Please specify IP address or host name of ESP device "
"using `upload_port` for build environment or use "
"global `--upload-port` option.\n"
"See https://docs.platformio.org/page/platforms/"
"espressif32.html#over-the-air-ota-update\n")
env.Replace(
UPLOADER=join(
platform.get_package_dir("framework-arduinoespressif32") or "",
"tools", "espota.py"),
UPLOADERFLAGS=["--debug", "--progress", "-i", "$UPLOAD_PORT"],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS -f $SOURCE'
)
if set(["uploadfs", "uploadfsota"]) & set(COMMAND_LINE_TARGETS):
env.Append(UPLOADERFLAGS=["--spiffs"])
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]
elif upload_protocol == "esptool":
env.Replace(
UPLOADER=join(
platform.get_package_dir("tool-esptoolpy") or "", "esptool.py"),
UPLOADERFLAGS=[
"--chip", mcu,
"--port", '"$UPLOAD_PORT"',
"--baud", "$UPLOAD_SPEED",
"--before", "default_reset",
"--after", "hard_reset",
"write_flash", "-z",
"--flash_mode", "${__get_board_flash_mode(__env__)}",
"--flash_freq", "${__get_board_f_flash(__env__)}",
"--flash_size", board.get("upload.flash_size", "detect")
],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS $ESP32_APP_OFFSET $SOURCE'
)
for image in env.get("FLASH_EXTRA_IMAGES", []):
env.Append(UPLOADERFLAGS=[image[0], env.subst(image[1])])
if "uploadfs" in COMMAND_LINE_TARGETS:
env.Replace(
UPLOADERFLAGS=[
"--chip", mcu,
"--port", '"$UPLOAD_PORT"',
"--baud", "$UPLOAD_SPEED",
"--before", "default_reset",
"--after", "hard_reset",
"write_flash", "-z",
"--flash_mode", "$BOARD_FLASH_MODE",
"--flash_size", "detect",
"$FS_START"
],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS $SOURCE',
)
upload_actions = [
env.VerboseAction(BeforeUpload, "Looking for upload port..."),
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")
]
elif upload_protocol == "mbctool":
env.Replace(
UPLOADER=join(
platform.get_package_dir("tool-mbctool") or "", "bin", "mbctool"),
UPLOADERFLAGS=[
"--device", "esp",
"--speed", "$UPLOAD_SPEED",
"--port", '"$UPLOAD_PORT"',
"--upload",
"0x1000", join(
platform.get_package_dir("framework-arduino-mbcwb"),
"tools", "sdk", "bin", "bootloader_qio_80m.bin"),
"0x8000", join("$BUILD_DIR", "partitions.bin"),
"0xe000", join(
platform.get_package_dir("framework-arduino-mbcwb"),
"tools", "partitions", "boot_app0.bin"),
"0x10000", join("$BUILD_DIR", "${PROGNAME}.bin"),
],
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS'
)
upload_actions = [
env.VerboseAction(env.AutodetectUploadPort,
"Looking for upload port..."),
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")
]
elif upload_protocol in debug_tools:
openocd_args = ["-d%d" % (2 if int(ARGUMENTS.get("PIOVERBOSE", 0)) else 1)]
openocd_args.extend(
debug_tools.get(upload_protocol).get("server").get("arguments", []))
openocd_args.extend(
[
"-c",
"adapter_khz %s" % env.GetProjectOption("debug_speed", "5000"),
"-c",
"program_esp {{$SOURCE}} %s verify"
% (
"$FS_START"
if "uploadfs" in COMMAND_LINE_TARGETS
else "$ESP32_APP_OFFSET"
),
]
)
if "uploadfs" not in COMMAND_LINE_TARGETS:
for image in env.get("FLASH_EXTRA_IMAGES", []):
openocd_args.extend(
[
"-c",
"program_esp {{%s}} %s verify"
% (_to_unix_slashes(image[1]), image[0]),
]
)
openocd_args.extend(["-c", "reset run; shutdown"])
openocd_args = [
f.replace(
"$PACKAGE_DIR",
_to_unix_slashes(
platform.get_package_dir("tool-openocd-esp32") or ""))
for f in openocd_args
]
env.Replace(
UPLOADER="openocd",
UPLOADERFLAGS=openocd_args,
UPLOADCMD="$UPLOADER $UPLOADERFLAGS",
)
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]
# custom upload tool
elif upload_protocol == "custom":
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]
else:
sys.stderr.write("Warning! Unknown upload protocol %s\n" % upload_protocol)
env.AddPlatformTarget("upload", target_firm, upload_actions, "Upload")
env.AddPlatformTarget("uploadfs", target_firm, upload_actions, "Upload Filesystem Image")
env.AddPlatformTarget(
"uploadfsota", target_firm, upload_actions, "Upload Filesystem Image OTA")
#
# Target: Erase Flash
#
env.AddPlatformTarget(
"erase",
None,
[
env.VerboseAction(env.AutodetectUploadPort, "Looking for serial port..."),
env.VerboseAction("$ERASECMD", "Erasing...")
],
"Erase Flash",
)
#
# Information about obsolete method of specifying linker scripts
#
if any("-Wl,-T" in f for f in env.get("LINKFLAGS", [])):
print("Warning! '-Wl,-T' option for specifying linker scripts is deprecated. "
"Please use 'board_build.ldscript' option in your 'platformio.ini' file.")
#
# Default targets
#
Default([target_buildprog, target_size])