forked from lexxvir/esp32-hello
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·184 lines (152 loc) · 4.81 KB
/
build.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
#!/usr/bin/env bash
# Workaround until https://github.com/espressif/esp-idf/pull/6587 is merged.
export ZSH_VERSION=''
set -euo pipefail
cd "$(dirname "${0}")"
CHIP=esp32
PACKAGE=app
EXAMPLE=
FLASH_BAUDRATE=460800
MONITOR_BAUDRATE=115200
PROFILE=
ERASE_FLASH=false
while (( ${#@} )); do
case "${1}" in
--chip)
shift
CHIP="${1}"
;;
-p|--package)
shift
PACKAGE="${1}"
;;
--example)
shift
EXAMPLE="${1}"
;;
--release)
PROFILE=release
;;
--flash-baudrate)
shift
FLASH_BAUDRATE="${1}"
;;
--erase-flash)
ERASE_FLASH=true
;;
*)
echo "Invalid argument: '${1}'" >&2
exit 1
;;
esac
shift
done
SERIAL_PORT="$(find /dev -name 'tty.usbserial-*' 2>/dev/null | head -n 1 || true)"
TARGET="xtensa-${CHIP}-none-elf"
if [[ "${CHIP}" == esp32 ]]; then
IDF_PATH="$(pwd)/esp-idf"
else
IDF_PATH="$(pwd)/ESP8266_RTOS_SDK"
fi
export IDF_PATH
IDF_TOOLS_PATH="$(pwd)/esp-idf-tools"
export IDF_TOOLS_PATH
mkdir -p "${IDF_TOOLS_PATH}"
idf_version="$(git -C "${IDF_PATH}" rev-parse HEAD 2>/dev/null || true)"
version_file="${IDF_TOOLS_PATH}/installed_version"
if ! [[ -f "${version_file}" ]] || [[ "${idf_version}" != "$(cat "${version_file}")" ]]; then
case "${CHIP}" in
esp32)
"${IDF_PATH}/install.sh"
;;
esp8266)
python -m pip install --user -r "${IDF_PATH}/requirements.txt"
;;
esac
if [[ -n "${idf_version:-}" ]]; then
echo "${idf_version}" > "${version_file}"
fi
fi
export PATH="$(brew --prefix rust-xtensa)/bin:$(brew --prefix llvm-xtensa)/bin:${PATH}"
source "${IDF_PATH}/export.sh" >/dev/null
if [[ "${CHIP}" = 'esp32' ]]; then
ln -sfn sdkconfig.esp32 app/sdkconfig
else
ln -sfn sdkconfig.esp8266 app/sdkconfig
fi
export CARGO_TARGET_DIR="$(pwd)/target"
cargo build ${PROFILE:+"--${PROFILE}"} --target "${TARGET}" ${PACKAGE:+--package "${PACKAGE}"} ${EXAMPLE:+--example "${EXAMPLE}"}
cargo doc ${PROFILE:+"--${PROFILE}"} --target "${TARGET}" --no-deps
if [[ -z "${SERIAL_PORT}" ]]; then
exit
fi
esptool() {
"${IDF_PATH}/components/esptool_py/esptool/esptool.py" --chip "${CHIP}" --port "${SERIAL_PORT}" ${FLASH_BAUDRATE:+--baud "${FLASH_BAUDRATE}"} "${@}" | \
grep -E -v 'esptool.py|Serial port|Changing baud rate|Changed.|Uploading stub|Running stub|Stub running|Configuring flash size|Leaving'
}
mapfile -t binary_targets < <(
cargo metadata --format-version 1 \
| jq -c '
.workspace_members as $members | .packages
| map(select(.id as $id | $members[] | contains($id) ))
| map(.targets)[] | map(select(.kind[] | contains("bin") or contains("example")))[]
| .name
' -r
)
for binary_target in "${binary_targets[@]}"; do
for t in "${CARGO_TARGET_DIR}"/${TARGET}/{release,debug}/{,examples/}"${binary_target}"; do
if [[ -f "${t}" ]]; then
"${IDF_PATH}/components/esptool_py/esptool/esptool.py" \
--chip "${CHIP}" \
elf2image \
--version "${ELF2IMAGE_VERSION}" \
-o "${t}.bin" \
"${t}" | tail -n +2
fi
done
done
FLASH_ARGS=( -z --flash_mode dio --flash_freq 80m --flash_size detect )
if [[ "${CHIP}" = 'esp32' ]]; then
BOOTLOADER_OFFSET=0x1000
else
BOOTLOADER_OFFSET=0x0000
fi
BOOTLOADER_BINARY="target/${TARGET}/esp-build/bootloader/bootloader.bin"
PARTITION_TABLE_OFFSET=0x8000
PARTITION_TABLE_BINARY="target/${TARGET}/esp-build/partitions.bin"
APPLICATION_OFFSET=0x10000
if [[ -n "${EXAMPLE-}" ]]; then
BINARY_PATH="examples/${EXAMPLE}"
else
BINARY_PATH="${PACKAGE}"
fi
APPLICATION_BINARY="target/${TARGET}/${PROFILE:-debug}/${BINARY_PATH}.bin"
if "${ERASE_FLASH}"; then
echo 'Erasing flash …'
esptool --after no_reset erase_flash
fi
echo "Verifying bootloader …"
if esptool --no-stub --after no_reset verify_flash "${BOOTLOADER_OFFSET}" "${BOOTLOADER_BINARY}" &>/dev/null; then
echo 'Bootloader is up to date.'
else
echo 'Flashing new bootloader …'
esptool --after no_reset write_flash "${FLASH_ARGS[@]}" \
"${BOOTLOADER_OFFSET}" "${BOOTLOADER_BINARY}"
fi
echo "Verifying partition table …"
if esptool --no-stub --after no_reset verify_flash "${PARTITION_TABLE_OFFSET}" "${PARTITION_TABLE_BINARY}" &>/dev/null; then
echo 'Partition table is up to date.'
else
echo 'Flashing new partition table …'
esptool --after no_reset write_flash "${FLASH_ARGS[@]}" \
"${PARTITION_TABLE_OFFSET}" "${PARTITION_TABLE_BINARY}"
fi
echo "Verifying application …"
if esptool --no-stub --after no_reset verify_flash "${APPLICATION_OFFSET}" "${APPLICATION_BINARY}" &>/dev/null; then
echo 'Application table is up to date.'
else
echo 'Flashing new application …'
esptool --after no_reset write_flash "${FLASH_ARGS[@]}" \
"${APPLICATION_OFFSET}" "${APPLICATION_BINARY}"
fi
python -m serial.tools.miniterm --raw --exit-char=3 --rts=0 --dtr=0 "${SERIAL_PORT}" "${MONITOR_BAUDRATE}"