forked from piyopiyoex/hello_atomvm_scene
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomvm-esp32.sh
More file actions
executable file
·371 lines (312 loc) · 8.71 KB
/
atomvm-esp32.sh
File metadata and controls
executable file
·371 lines (312 loc) · 8.71 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
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
#!/usr/bin/env bash
set -Eeuo pipefail
if [ -z "${BASH_VERSION:-}" ]; then
exec /usr/bin/env bash "$0" "$@"
fi
usage() {
local script_name
script_name="$(basename "$0")"
cat <<EOF
Usage:
${script_name} <command> [options]
Commands:
sync Ensure AtomVM + AtomGL repos exist and patch sdkconfig.defaults
core Build Generic UNIX core libs (boot AVM)
build Build ESP32 + mkimage (delegates to tools/atomvm-esp32-build-image.sh)
erase Erase flash (esptool.py via ESP-IDF)
flash Flash locally-built image (delegates to tools/atomvm-esp32-flash-image.sh)
monitor Serial monitor (delegates to tools/atomvm-esp32-monitor.sh)
configure Configure actions (delegates to tools/atomvm-esp32-configure.sh)
clean Remove build artifacts
build-erase-flash Run: sync -> core -> build -> erase -> flash
Options (used by some commands):
--idf-dir PATH ESP-IDF root (contains export.sh)
--target TARGET Target chip (e.g. esp32, esp32s3) [required for build/build-erase-flash]
--port PORT Serial port (optional; auto-detect if omitted) [erase/flash/monitor/build-erase-flash]
--baud BAUD Baud for erase (default: 921600) [erase/build-erase-flash]
-h, --help Show help
EOF
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
TOOLS_DIR="${SCRIPT_DIR}/tools"
ATOMVM_WRAPPER_DIR="${ROOT}/atomvm"
ATOMVM_DIR="${ATOMVM_WRAPPER_DIR}/AtomVM"
ATOMVM_URL="https://github.com/atomvm/AtomVM.git"
ATOMGL_URL="https://github.com/atomvm/atomgl.git"
ATOMVM_REF="main"
ATOMGL_REF="main"
SDKCFG_WANT_IPV6='CONFIG_LWIP_IPV6=y'
SDKCFG_WANT_PARTITIONS='CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-elixir.csv"'
die() {
printf "✖ %s\n" "$*" >&2
exit 1
}
run() {
printf "+ %s\n" "$*"
"$@"
}
require_cmd() { command -v "$1" >/dev/null 2>&1 || die "Missing dependency: $1"; }
require_executable() { [ -x "$1" ] || die "Missing tool: $1"; }
# Usage: append_if_set array_name flag value
append_if_set() {
local -n ary="$1"
local flag="$2"
local value="$3"
if [ -n "$value" ]; then
ary+=("$flag" "$value")
fi
}
resolve_idf_dir() {
local override="$1"
if [ -n "$override" ]; then
printf "%s" "$override"
elif [ -n "${ESP_IDF_DIR:-}" ]; then
printf "%s" "${ESP_IDF_DIR}"
elif [ -n "${IDF_PATH:-}" ]; then
printf "%s" "${IDF_PATH}"
else
printf "%s" "${HOME}/esp/esp-idf"
fi
}
auto_port() {
local p
if [ -d /dev/serial/by-id ]; then
for p in /dev/serial/by-id/*; do
[ -e "$p" ] && {
printf "%s" "$p"
return 0
}
done
fi
for p in /dev/ttyACM* /dev/ttyUSB*; do
[ -e "$p" ] && {
printf "%s" "$p"
return 0
}
done
return 1
}
ensure_repo_present() {
local name="$1" dir="$2" url="$3" ref="$4"
if [ -d "${dir}/.git" ]; then
return 0
fi
[ ! -e "$dir" ] || die "${name} exists but is not a git repo: ${dir}"
mkdir -p "$(dirname "$dir")"
if [ -n "$ref" ]; then
run git clone --filter=blob:none --depth 1 --branch "$ref" "$url" "$dir"
else
run git clone --filter=blob:none --depth 1 "$url" "$dir"
fi
}
patch_sdkconfig_defaults() {
local esp32_dir="${ATOMVM_DIR}/src/platforms/esp32"
local path="${esp32_dir}/sdkconfig.defaults"
local marker="# Added by hello_atomvm_scene/scripts/atomvm-esp32.sh"
[ -f "$path" ] || die "sdkconfig.defaults not found: ${path}"
local want_lines=("$SDKCFG_WANT_IPV6" "$SDKCFG_WANT_PARTITIONS")
local line needs_patch="0"
for line in "${want_lines[@]}"; do
grep -qF "$line" "$path" || needs_patch="1"
done
[ "$needs_patch" = "1" ] || return 0
{
printf "\n"
printf "%s\n" "$marker"
for line in "${want_lines[@]}"; do
grep -qF "$line" "$path" || printf "%s\n" "$line"
done
} >>"$path"
printf "✔ patched: %s\n" "$path"
}
idf_env_run() {
local idf_dir="$1" workdir="$2"
shift 2
[ -f "${idf_dir}/export.sh" ] || die "ESP-IDF export.sh not found: ${idf_dir}/export.sh"
(
set -Eeuo pipefail
# shellcheck source=/dev/null
source "${idf_dir}/export.sh" >/dev/null 2>&1
cd "$workdir"
"$@"
)
}
require_target_arg() {
while [ "$#" -gt 0 ]; do
case "$1" in
--target)
shift
[ "$#" -gt 0 ] || die "--target requires a value"
return 0
;;
--) break ;;
esac
shift
done
die "--target is required for: build"
}
sync_cmd() {
require_cmd git
ensure_repo_present "AtomVM" "${ATOMVM_DIR}" "${ATOMVM_URL}" "${ATOMVM_REF}"
local atomgl_dir="${ATOMVM_DIR}/src/platforms/esp32/components/atomgl"
ensure_repo_present "AtomGL" "${atomgl_dir}" "${ATOMGL_URL}" "${ATOMGL_REF}"
patch_sdkconfig_defaults
}
core_cmd() {
local boot_avm="${ATOMVM_DIR}/build/libs/esp32boot/elixir_esp32boot.avm"
require_executable "${TOOLS_DIR}/atomvm-build-boot-avm.sh"
if [ -f "$boot_avm" ]; then
printf "✔ core already built: %s\n" "$boot_avm"
else
run "${TOOLS_DIR}/atomvm-build-boot-avm.sh" --atomvm-repo "${ATOMVM_WRAPPER_DIR}"
fi
}
build_cmd() {
require_executable "${TOOLS_DIR}/atomvm-esp32-build-image.sh"
require_target_arg "$@"
run "${TOOLS_DIR}/atomvm-esp32-build-image.sh" --atomvm-repo "${ATOMVM_WRAPPER_DIR}" "$@"
}
flash_cmd() {
require_executable "${TOOLS_DIR}/atomvm-esp32-flash-image.sh"
run "${TOOLS_DIR}/atomvm-esp32-flash-image.sh" --atomvm-repo "${ATOMVM_WRAPPER_DIR}" "$@"
}
monitor_cmd() {
require_executable "${TOOLS_DIR}/atomvm-esp32-monitor.sh"
run "${TOOLS_DIR}/atomvm-esp32-monitor.sh" --atomvm-repo "${ATOMVM_WRAPPER_DIR}" "$@"
}
configure_cmd() {
require_executable "${TOOLS_DIR}/atomvm-esp32-configure.sh"
run "${TOOLS_DIR}/atomvm-esp32-configure.sh" --atomvm-repo "${ATOMVM_WRAPPER_DIR}" "$@"
}
erase_cmd() {
local idf_dir_override="" port="" baud="921600"
while [ "$#" -gt 0 ]; do
case "$1" in
--idf-dir)
shift
[ "$#" -gt 0 ] || die "--idf-dir requires a value"
idf_dir_override="$1"
shift
;;
--port | -p)
shift
[ "$#" -gt 0 ] || die "--port requires a value"
port="$1"
shift
;;
--baud)
shift
[ "$#" -gt 0 ] || die "--baud requires a value"
baud="$1"
shift
;;
--)
shift
break
;;
*) die "Unknown arg for erase: $1" ;;
esac
done
local idf_dir
idf_dir="$(resolve_idf_dir "$idf_dir_override")"
if [ -n "$port" ]; then
[ -e "$port" ] || die "Port not found: ${port}"
else
port="$(auto_port)" || die "Could not auto-detect a serial port. Pass --port."
fi
idf_env_run "$idf_dir" "${ATOMVM_DIR}/src/platforms/esp32" \
esptool.py --chip auto --port "$port" --baud "$baud" erase_flash
}
clean_cmd() {
[ "${1:-}" = "--" ] && shift || [ "$#" -eq 0 ] || die "Unknown arg for clean: $1"
local esp32_build="${ATOMVM_DIR}/src/platforms/esp32/build"
[ -d "$esp32_build" ] && run rm -rf "$esp32_build"
}
build_erase_flash_cmd() {
local target="" idf_dir="" port="" baud="921600" passthru=()
while [ "$#" -gt 0 ]; do
case "$1" in
--target)
shift
[ "$#" -gt 0 ] || die "--target requires a value"
target="$1"
shift
;;
--idf-dir)
shift
[ "$#" -gt 0 ] || die "--idf-dir requires a value"
idf_dir="$1"
shift
;;
--port | -p)
shift
[ "$#" -gt 0 ] || die "--port requires a value"
port="$1"
shift
;;
--baud)
shift
[ "$#" -gt 0 ] || die "--baud requires a value"
baud="$1"
shift
;;
--)
shift
while [ "$#" -gt 0 ]; do
passthru+=("$1")
shift
done
;;
*)
passthru+=("$1")
shift
;;
esac
done
[ -n "$target" ] || die "--target is required for: build-erase-flash"
sync_cmd
core_cmd
local build_args=(--target "$target")
append_if_set build_args --idf-dir "$idf_dir"
build_args+=(-- "${passthru[@]}")
local erase_args=(--baud "$baud")
append_if_set erase_args --idf-dir "$idf_dir"
append_if_set erase_args --port "$port"
erase_args+=(--)
local flash_args=()
append_if_set flash_args --idf-dir "$idf_dir"
append_if_set flash_args --port "$port"
flash_args+=(-- "${passthru[@]}")
build_cmd "${build_args[@]}"
erase_cmd "${erase_args[@]}"
flash_cmd "${flash_args[@]}"
}
main() {
local cmd="${1:-}"
if [ -z "$cmd" ]; then
usage
return 2
fi
shift || true
case "$cmd" in
-h | --help)
usage
return 0
;;
sync) sync_cmd ;;
core) core_cmd ;;
build) build_cmd "$@" ;;
erase) erase_cmd "$@" ;;
flash) flash_cmd "$@" ;;
monitor) monitor_cmd "$@" ;;
configure) configure_cmd "$@" ;;
clean) clean_cmd "$@" ;;
build-erase-flash) build_erase_flash_cmd "$@" ;;
*)
usage
die "Unknown command: ${cmd}"
;;
esac
}
main "$@"