#!/bin/sh RESET_PIN=90 BOOT0_PIN=67 SYSFSPATH=/sys/class/gpio die() { echo "$*" 1>&2 exit 1 } usage() { echo "Usage: $0 [--reset|--dfu|--disable|--flash ]" echo " --reset - resets the Cortex M0" echo " --dfu - enables DFU loader" echo " --disable - keep Cortex M0 in reset" echo " --flash - flash the given firmware" exit 1 } gpiosetup() { for PIN in $RESET_PIN $BOOT0_PIN do if [ ! -d ${SYSFSPATH}/gpio${PIN} ] ; then echo $PIN > ${SYSFSPATH}/export echo out > ${SYSFSPATH}/gpio${PIN}/direction fi done } reset_stm32() { echo 0 > ${SYSFSPATH}/gpio${RESET_PIN}/value echo 0 > ${SYSFSPATH}/gpio${BOOT0_PIN}/value sleep 0.1 echo 1 > ${SYSFSPATH}/gpio${RESET_PIN}/value } enable_dfu() { gpiosetup echo 0 > ${SYSFSPATH}/gpio${RESET_PIN}/value echo 1 > ${SYSFSPATH}/gpio${BOOT0_PIN}/value sleep 0.1 echo 1 > ${SYSFSPATH}/gpio${RESET_PIN}/value } case "$1" in --reset) echo "reseting Cortex M0" gpiosetup reset_stm32 ;; --dfu) echo "enabling DFU loader" gpiosetup enable_dfu ;; --disable) echo "disabling Cortex M0" gpiosetup echo 0 > ${SYSFSPATH}/gpio${RESET_PIN}/value ;; --flash) [ $# -eq 2 ] || usage command -v dfu-util >/dev/null 2>&1 || die "dfu-util needed but not found" [ -f "$2" ] || die "flash file does not exist" echo "flashing Cortex M0" gpiosetup enable_dfu echo waiting for DFU loader if [ command -v lsusb >/dev/null 2>&1 ]; then while ! lsusb -d 0483:df11 > /dev/null do sleep .1 echo -n . done echo -e "\nfound" else sleep 5 fi dfu-util -d 0483:df11 -a 0 -s 0x08000000 -D $2 reset_stm32 ;; --*) usage ;; *) usage ;; esac exit 0