Skip to content

Instantly share code, notes, and snippets.

@zvecr
Last active April 6, 2021 16:58
Show Gist options
  • Save zvecr/c69b9bbe97f4f97035bf9b883914e3f0 to your computer and use it in GitHub Desktop.
Save zvecr/c69b9bbe97f4f97035bf9b883914e3f0 to your computer and use it in GitHub Desktop.
Utility script to flash a bluepill with the STM32duino bootloader and test QMK firmware
#!/bin/bash
function error()
{
local RED='\033[0;31m'
local NC='\033[0m' # No Color
echo -e "${RED}$1${NC}"
}
function info()
{
local GREEN='\033[1;34m'
local NC='\033[0m' # No Color
echo -e "${GREEN}$1${NC}"
}
PILL="bluepill"
while getopts p:hu option
do
case "${option}"
in
p)
PILL=${OPTARG}
;;
u)
UNLOCK=true
;;
h)
echo "Usage:"
exit 1
;;
esac
done
declare -A BOOTMAP
BOOTMAP[bluepill]="generic_boot20_pc13.bin"
BOOTMAP[blackpill]="generic_boot20_pb12.bin"
BOOTMAP[weact]="STM32duino-bootloader-PB2.bin"
BOOTLOADER=${BOOTMAP[$PILL]}
if [ -z "$BOOTLOADER" ];
then
error "invalid pill type"
exit 1
fi
BOOT_URL="https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/master/binaries/$BOOTLOADER"
if [ "$PILL" == "weact" ];
then
BOOT_URL="https://github.com/WeActTC/BluePill-Plus/blob/master/SDK/STM32F103C8T6/Arduino/Bootloader/STM32duino-bootloader-PB2.bin?raw=true"
fi
# Attempt to download bootloader if we dont have it
download1=$(wget -P ~/Downloads -cq $BOOT_URL)
if [ $? != 0 ];
then
error "failed to download bootloader"
echo $download1
exit 1
fi
# Attempt to download some basic firmware if we dont have it
download2=$(wget -P ~/Downloads -cq https://qmk.fm/compiled/handwired_onekey_bluepill_default.bin)
if [ $? != 0 ];
then
error "failed to download firmware"
echo $download2
exit 1
fi
info "Please connect device to stlink"
until st-info --chipid | grep -q "0x0410"
do
echo "."
sleep 1
done
# Unlock
if [ "$UNLOCK" == "true" ];
then
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c "init; reset halt; stm32f1x unlock 0; reset; exit"
fi
info "Flashing $PILL with $BOOTLOADER"
sudo st-flash --reset --format binary write ~/Downloads/$BOOTLOADER 0x8000000
if [ $? != 0 ];
then
error "failed to flash bootloader"
exit 1
fi
#sleep 2
info "Please connect device in dfu mode"
until sudo dfu-util -l | grep -q "1eaf:0003"
do
echo "."
sleep 1
done
info "Flashing $PILL with handwired_onekey_bluepill_default.bin"
sudo dfu-util -d 1eaf:0003 -a 2 -R -D ~/Downloads/handwired_onekey_bluepill_default.bin
if [ $? != 0 ];
then
error "failed to flash firmware"
exit 1
fi
info "DONE!!!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment