-
Notifications
You must be signed in to change notification settings - Fork 97
/
utils.sh
executable file
·421 lines (356 loc) · 9.87 KB
/
utils.sh
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
#!/bin/bash
set -e # bail on error
export WILLOW_PATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$WILLOW_PATH"
export PLATFORM="esp32s3" # Current general family
export FLASH_BAUD=2000000 # Optimistic but seems to work for me for now
export CONSOLE_BAUD=2000000 # Subject to change
export DOCKER_IMAGE="willow:latest"
export DOCKER_NAME="willow-build"
export DIST_FILE="build/${dist_filename:-willow-dist.bin}"
# ESP-SR Componenent ver hash
ESP_SR_VER="c8bf7cd118c437b4ab41ccb37e4ad6b84a8a4fc4"
# esptool ver to install
ESPTOOL_VER="4.5.1"
export ADF_PATH="$WILLOW_PATH/deps/esp-adf"
# Number of loops for torture test
TORTURE_LOOPS=300
# Delay in between loops
TORTURE_DELAY=3
# File to play
TORTURE_PLAY="misc/hi_esp_this_is_a_test_command.flac"
# Container or host?
# podman sets container var to podman, make docker act like that
if [ -f /.dockerenv ]; then
export container="docker"
fi
# Get Willow version
export WILLOW_VERSION=$(git describe --always --dirty --tags)
# Test for local environment file and use any overrides
if [ -r .env ]; then
echo "Using configuration overrides from .env file"
. .env
fi
# Always print Willow version
echo "Willow build version: $WILLOW_VERSION"
check_port() {
if [ ! $PORT ]; then
echo "You need to define the PORT environment variable to do serial stuff - exiting"
echo "If using sudo because of permissions, pass PORT with sudo. Example: sudo PORT=/dev/ttyACM0 ./utils.sh [command]"
exit 1
fi
if [ ! -c $PORT ]; then
echo "Cannot find configured port $PORT - exiting"
exit 1
fi
if [ ! -w "$PORT" ]; then
echo "You don't have permission to write to $PORT - exiting"
echo "You need to either run this command with sudo or add yourself to the dialout group"
echo "Example: sudo -E ./utils.sh erase-flash or sudo -E ./utils.sh flash"
exit 1
fi
}
check_esptool() {
if [ ! -x venv/bin/esptool.py ]; then
rm -rf venv
echo "Creating venv for esptool"
python3 -m venv venv
source venv/bin/activate
echo "Installing esptool..."
pip install -U wheel setuptools pip
pip install esptool=="$ESPTOOL_VER"
else
echo "Using venv for esptool"
source venv/bin/activate
fi
}
check_tio() {
if ! command -v tio &> /dev/null
then
echo "tio could not be found in path - you need to install it"
echo "More information: https://github.com/tio/tio"
exit 1
fi
}
check_clang_format() {
if ! command -v clang-format-15 &> /dev/null
then
echo "clang-format-15 could not be found in path - you need to install it"
exit 1
fi
}
fix_term() {
clear
reset
}
do_term() {
tio -b "$CONSOLE_BAUD" "$PORT"
}
check_build_host() {
if [ "$BUILD_HOST_PATH" ]; then
echo "Copying build from defined remote build host and path $BUILD_HOST_PATH"
rsync -r --delete --exclude esp-idf "$BUILD_HOST_PATH/build" .
fi
}
check_container(){
if [ "$container" ]; then
return
fi
echo "You need to run this command inside of the container - you are on the host"
exit 1
}
check_host(){
if [ ! "$container" ]; then
return
fi
echo "You need to run this command from the host - you are in the container"
exit 1
}
check_deps() {
if [ ! -d deps/esp-adf ]; then
echo "You need to run install first"
exit 1
fi
}
generate_speech_commands() {
rm -rf build/srmodels
/usr/bin/python3 speech_commands/generate_commands.py
if [ -r "$WILLOW_PATH"/speech_commands/commands_en.txt ]; then
echo "Linking custom speech commands"
ln -sf "$WILLOW_PATH"/speech_commands/commands_en.txt \
"$WILLOW_PATH"/components/esp-sr/model/multinet_model/fst/commands_en.txt
fi
}
generate_nvs() {
SSID=$(grep CONFIG_WIFI_SSID sdkconfig | cut -d'=' -f2 | tr -d '"')
PASSWORD=$(grep CONFIG_WIFI_PASSWORD sdkconfig | cut -d'=' -f2 | tr -d '"')
WAS_URL=$(grep CONFIG_WILLOW_WAS_URL sdkconfig | cut -d'=' -f2 | tr -d '"')
echo -n "key,type,encoding,value
WAS,namespace,,
URL,data,string,$WAS_URL
WIFI,namespace,,
PSK,data,string,$PASSWORD
SSID,data,string,$SSID" > build/nvs.csv
/opt/esp/idf/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate \
--version 2 build/nvs.csv build/nvs.bin 0x24000
}
install() {
if [ -d deps ]; then
echo "You already have a deps directory - exiting"
exit 1
fi
mkdir -p deps
cd deps
# Setup ADF
git clone https://github.com/toverainc/esp-adf.git
cd $ADF_PATH
git checkout "$ADF_VER"
git submodule update --init components/esp-adf-libs
# Setup esp-sr
cd $WILLOW_PATH/components
git clone https://github.com/espressif/esp-sr.git
cd esp-sr
git checkout "$ESP_SR_VER"
cd $WILLOW_PATH
idf.py set-target "$PLATFORM"
}
destroy() {
sudo rm -rf build serve deps target venv managed_components "$DIST_FILE" components/esp-sr flags/*
}
# Just in case
mkdir -p flags
check_flag() {
FLAG="$1"
if [ ! -r flags/"$FLAG" ]; then
echo "You need to run $FLAG first"
exit 1
fi
}
add_flag() {
FLAG="$1"
date > flags/"$FLAG"
}
remove_flag() {
FLAG="$1"
rm -f flags/"$FLAG"
}
do_dist() {
cd "$WILLOW_PATH"/build
esptool.py --chip "$PLATFORM" merge_bin -o "$WILLOW_PATH/$DIST_FILE" \
@flash_args
echo "Combined firmware image for flashing written"
ls -lh "$WILLOW_PATH/$DIST_FILE"
cd "$WILLOW_PATH"
}
# Some of this may seem redundant but for build, clean, etc we'll probably need to do our own stuff later
case $1 in
config)
check_container
check_deps
idf.py menuconfig
;;
clean)
check_container
check_deps
idf.py clean
;;
fullclean)
check_container
check_deps
idf.py fullclean
;;
build)
check_container
check_deps
[ "$CI" ] || generate_speech_commands
if [ $2 ]; then
echo "Adding timestamp to dev build"
TS=$(date '+%d-%m-%Y_%H:%M:%S')
WILLOW_VERSION+="_$TS"
fi
WILLOW_SDKCONFIG_SANITY_CHECKS=1 idf.py build
;;
build-docker|docker-build)
docker build -t "$DOCKER_IMAGE" .
;;
docker)
docker run --rm -it -v "$PWD":/willow -e TERM --name "$DOCKER_NAME" \
"$DOCKER_IMAGE" /bin/bash
;;
flash)
check_host
check_port
check_tio
check_esptool
check_flag "erase-flash"
check_build_host
cd "$WILLOW_PATH"/build
esptool.py --chip "$PLATFORM" -p "$PORT" -b "$FLASH_BAUD" --before default_reset --after hard_reset write_flash \
@flash_args
do_term
;;
flash-app)
check_host
check_port
check_tio
check_esptool
check_flag "erase-flash"
check_build_host
cd "$WILLOW_PATH"/build
esptool.py --chip "$PLATFORM" -p "$PORT" -b "$FLASH_BAUD" --before=default_reset --after=hard_reset write_flash \
@flash_app_args
do_term
;;
dist)
check_esptool
check_build_host
do_dist
generate_nvs
dd conv=notrunc bs=1 if=build/nvs.bin of="$DIST_FILE" seek=$((0x9000))
;;
flash-dist|dist-flash)
if [ ! -r "$DIST_FILE" ]; then
echo "You need to run dist first"
exit 1
fi
check_port
check_esptool
check_build_host
check_flag "erase-flash"
esptool.py --chip "$PLATFORM" -p "$PORT" -b "$FLASH_BAUD" --before=default_reset --after=hard_reset write_flash \
--flash_mode dio --flash_freq 80m --flash_size 16MB 0x0 "$WILLOW_PATH/$DIST_FILE"
do_term
;;
erase-flash)
check_host
check_port
check_esptool
esptool.py --chip "$PLATFORM" -p "$PORT" erase_flash
echo "Flash erased. You will need to reflash."
add_flag "erase-flash"
;;
monitor)
check_host
check_port
check_tio
do_term
;;
destroy)
echo "YOU ARE ABOUT TO REMOVE THIS ENTIRE ENVIRONMENT AND RESET THE REPO. HIT ENTER TO CONFIRM."
read
echo "SERIOUSLY - YOU WILL LOSE WORK AND I WILL NOT STOP YOU IF YOU HIT ENTER AGAIN!"
read
echo "LAST CHANCE!"
read
destroy
echo "Not a trace left. You will have to run setup again."
;;
install|setup)
check_container
install
echo "You can now run ./utils.sh config and navigate to Willow Configuration for your environment"
;;
reinstall)
check_container
cp sdkconfig sdkconfig.user
destroy
install
mv sdkconfig.user sdkconfig
echo "Reinstalled with your configuration - you can either run ./utils.sh config and/or ./utils.sh build"
;;
torture)
check_host
echo "Running torture test for $TORTURE_LOOPS loops..."
echo "WARNING: If testing against Tovera provided servers you will get rate-limited or blocked"
for i in `seq 1 $TORTURE_LOOPS`; do
echo -n "Running loop $i at" `date +"%H:%M:%S"`
if `grep -q Raspberry /proc/cpuinfo`; then
aplay -q "$TORTURE_PLAY"
else
ffplay -nodisp -hide_banner -loglevel error -autoexit -i "$TORTURE_PLAY"
fi
echo
sleep $TORTURE_DELAY
done
;;
log)
if [ ! $2 ]; then
echo "Need port"
exit 1
else
LOG_PORT="$2"
LOG_DEVICE="/dev/tty$LOG_PORT"
fi
FILE="tt-$LOG_PORT.log"
DIR="build"
echo "Logging device $LOG_DEVICE torture to $DIR/$FILE"
tio -l "$DIR"/"$FILE" -b "$CONSOLE_BAUD" "$LOG_DEVICE"
;;
reset)
check_esptool
if [ ! $2 ]; then
echo "Need port"
exit 1
else
PORT=/dev/tty"$2"
fi
esptool.py --chip esp32s3 --port "$PORT" --after hard_reset --no-stub run
;;
clang-format)
check_clang_format
find main/ -name '*.c' -or -name '*.h' -exec clang-format-15 -i {} +
;;
convert-font)
lv_font_conv --format bin --lcd --bpp 4 --no-compress --size 28 --font "$2" \
-r 0x20-0x20CF -o spiffs/ui/font.bin
;;
addr2line)
shift
xtensa-esp32s3-elf-addr2line -e /willow/build/willow.elf "$@"
;;
*)
echo "Unknown argument - passing directly to idf.py"
check_container
idf.py "$@"
;;
esac