Repository navigation
Expand file tree
/
Copy pathupload.py
More file actions
executable file
·126 lines (96 loc) · 3.25 KB
/
Copy pathupload.py
File metadata and controls
executable file
·126 lines (96 loc) · 3.25 KB
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
#!/usr/bin/env python3
# Wrapper for Arduino core / others that can call esptool.py possibly multiple times
# Adds pyserial & esptool that are in the same directory as the script to sys.path
import os
import atexit
import pathlib
import sys
import tempfile
import traceback
from typing import List
# Add neighbouring pyserial & esptool to search path
MODULES = [
"pyserial",
"esptool",
]
PWD = pathlib.Path(__file__).resolve().parent
for m in MODULES:
sys.path.insert(0, (PWD / m).as_posix())
# If this fails, we can't continue and will bomb below
try:
import esptool
except ImportError:
sys.stderr.write(
"\n*** pyserial or esptool directories not found next to upload.py tool (this script) ***\n"
)
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
sys.exit(1)
def make_erase_pair(addr: str, dest_size: int, block_size=2**16):
dest, path = tempfile.mkstemp()
buffer = bytearray(b"\xff" * block_size)
while dest_size:
remainder = dest_size % block_size
if remainder:
src = buffer[:remainder]
src_size = remainder
else:
src = buffer
src_size = block_size
os.write(dest, src)
dest_size -= src_size
os.close(dest)
def maybe_remove(path):
try:
os.remove(path)
except Exception:
pass
atexit.register(maybe_remove, path)
return [addr, path]
argv = sys.argv[1:] # Remove executable name
cmdline: List[str] = []
write_options: List[str] = ["--flash_size", "detect"]
erase_options: List[str] = []
thisarg = ""
lastarg = ""
while argv:
lastarg = thisarg
thisarg = argv.pop(0)
# We silently replace the high-speed setting with 460k to enable backward
# compatibility with the old esptool-ck.exe. Esptool.py doesn't seem
# work reliably, but 460kbaud is still plenty fast.
if lastarg == "--baud" and thisarg in ("921600", "3000000"):
thisarg = "460800"
# 'erase_flash' command is translated to the write_flash --erase-all option
# https://github.com/esp8266/Arduino/issues/6755#issuecomment-553208688
if thisarg == "erase_flash":
write_options.append("--erase-all")
# instead of providing esptool with separate targets,
# everything below becomes 'write_flash' [<addr> <path>] pairs
# 'erase_region' becomes a temporary file filled with 0xff
# this pair is appended *after* 'write_flash' pairs
elif thisarg == "erase_region":
addr = argv.pop(0)
size = int(argv.pop(0), 0)
erase_options.extend(make_erase_pair(addr, size))
# 'write_flash' pair taken in order it was specified
elif thisarg == "write_flash":
addr = argv.pop(0)
path = argv.pop(0)
write_options.extend([addr, path])
# everything else is used as-is
elif thisarg:
cmdline.append(thisarg)
cmdline.append("write_flash")
for opts in (write_options, erase_options):
if opts:
cmdline.extend(opts)
try:
esptool.main(cmdline)
except Exception:
etype, evalue, _ = sys.exc_info()
estring = "\n".join(traceback.format_exception_only(etype, value=evalue))
sys.stderr.write("\n*** upload.py fatal error ***\n")
sys.stderr.write(estring)
sys.stderr.flush()
sys.exit(2)