Skip to content

Instantly share code, notes, and snippets.

@m1cr0lab
Created June 13, 2020 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1cr0lab/bc71ec9d6096bb503cce3cce87a258f6 to your computer and use it in GitHub Desktop.
Save m1cr0lab/bc71ec9d6096bb503cce3cce87a258f6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ----------------------------------------------------------------------------
# ESP32 Firmware Uploader © 2020 Stéphane Calderoni
# ----------------------------------------------------------------------------
# This program is designed to flash the ESP32 from the following binary files:
# - firmware.bin your application
# - partitions.bin the partition table
# ----------------------------------------------------------------------------
PLATFORMIO_ESP_TOOLS="$HOME/.platformio/packages/framework-arduinoespressif32/tools"
ARDUINO_ESP_TOOLS="$HOME/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/tools"
# Default IDE is PlatformIO
ESP_TOOLS="$PLATFORMIO_ESP_TOOLS"
ARDUINO=0
BPS=(115200 230400 460800 921600)
SPEED=${BPS[0]}
PORT=""
EXEC=1
function join_by { local IFS="$1"; shift; echo "$*"; }
function help() {
cat <<EOF
$(tput bold)ESP32 Firmware Uploader$(tput sgr0)
Description:
This tool is designed to flash the ESP32 from the following binary files:
* firmware.bin -> your application
* partitions.bin -> the partition table
It relies on the following development environments:
* PlatformIO IDE (default)
* Arduino IDE
You must have installed one of them.
Usage:
`basename $0` [options] firmware.bin partitions.bin
Options:
-a uses the Arduino IDE rather than PlatformIO IDE
-p port sets the upload port
-s speed sets the upload speed ($( join_by , ${BPS[@]}))
-n prints the command without executing it
-h prints this help
You must specify the communication port with the ESP32.
The default upload speed is set to $SPEED bps.
EOF
exit 0
}
function error() {
echo "[Error] $1 (use -h flag for help)." >&2 && exit 1
}
while getopts ":hap:s:n" opt; do
case $opt in
h)
help
;;
a)
ESP_TOOLS="$ARDUINO_ESP_TOOLS"
ARDUINO=1
;;
p)
PORT=${OPTARG}
;;
s)
SPEED=${OPTARG}
;;
n)
EXEC=0
;;
:)
error "option -${OPTARG} requires an argument"
;;
\?)
error "invalid option: -${OPTARG}"
;;
esac
done
shift $((OPTIND-1))
if [ ! -d "$ESP_TOOLS" ]; then
error "Looks like the $([[ ARDUINO -eq 1 ]] && echo "Arduino" || echo "PlatformIO") IDE is missing"
fi
UPLOADER="$ESP_TOOLS/esptool.py"
BOOTAPP_BIN="$ESP_TOOLS/partitions/boot_app0.bin"
BOOTLDR_BIN="$ESP_TOOLS/sdk/bin/bootloader_dio_80m.bin"
FIRMWARE="$1"
PARTITION="$2"
[[ -z "$FIRMWARE" ]] && error "firmware is missing"
[[ ! -f "$FIRMWARE" ]] && error "firmware does not exist: $FIRMWARE"
[[ -z "$PARTITION" ]] && error "partition table is missing"
[[ ! -f "$PARTITION" ]] && error "partition table does not exist: $PARTITION"
[[ -z "$PORT" ]] && error "upload port is missing"
[[ ! -c "$PORT" ]] && error "unknown upload port: $PORT"
let within=0
for s in ${BPS[@]}; do
[[ $s = $SPEED ]] && within=1 && break
done
[[ $within -eq 0 ]] && error "invalid upload speed: $SPEED"
COMMAND=$(cat <<EOF
python $UPLOADER
--chip esp32
--port $PORT
--baud $SPEED
--before default_reset
--after hard_reset write_flash
--flash_mode dio
--flash_freq 80m
--flash_size detect
-z
0x1000 $BOOTLDR_BIN
0x8000 $PARTITION
0xe000 $BOOTAPP_BIN
0x10000 $FIRMWARE
EOF
)
[[ $EXEC = 1 ]] && $COMMAND || echo "$COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment