diff --git a/linux/stm32CubeProg.sh b/linux/stm32CubeProg.sh index 1d2adeacf..4b0d28a04 100755 --- a/linux/stm32CubeProg.sh +++ b/linux/stm32CubeProg.sh @@ -1,6 +1,8 @@ #!/bin/bash set -o nounset # Treat unset variables as an error #set -x + +# STM32 Cube programmer variables STM32CP_CLI=STM32_Programmer.sh ADDRESS=0x8000000 ERASE= @@ -8,6 +10,10 @@ MODE= PORT= OPTS= +# Script variables +SERPORT= +STATUS= + ############################################################################### ## Help function usage() @@ -24,10 +30,12 @@ usage() echo "## Ex: 10 erase all sectors using SWD interface." echo "## file_path: file path name to be downloaded: (bin, hex)" echo "## Options:" - echo "## For SWD and DFU: no mandatory options" - echo "## For Serial: " + echo "## For SWD: no mandatory options" + echo "## For DFU: no mandatory options" + echo "## For Serial: 'serport='" echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0" echo "##" + echo "## '-serport=' is also used to wait the serial port availability" echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI" echo "## They have to be valid commands for STM32 MCU" echo "## Ex: -g: Run the code at the specified address" @@ -37,7 +45,6 @@ usage() exit $1 } - check_tool() { command -v $STM32CP_CLI >/dev/null 2>&1 if [ $? != 0 ]; then @@ -53,6 +60,18 @@ check_tool() { fi } +upload() { + count=0 + STATUS=1 + while [ $STATUS -ne 0 ] && ((count++ < 5)); do + # echo "Try upload $count " + ${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS} + STATUS=$? + sleep 0.5 + done +} + +# Main check_tool if [ $# -lt 2 ]; then @@ -69,6 +88,15 @@ if [ $1 -ge 10 ]; then ERASE='-e all' PROTOCOL=$(($1 - 10)) fi + +# Check if serial port option available +if [ $# -gt 2 ] && [[ $3 == "-serport="* ]]; then + SERPORT=`echo $3 | cut -d'=' -f2` + if [ ! -z $SERPORT ] && [[ $SERPORT != "/dev/"* ]]; then + SERPORT="/dev/"${SERPORT} + fi +fi + # Protocol $1 # 0: SWD # 1: Serial @@ -76,28 +104,47 @@ fi case $PROTOCOL in 0) PORT='SWD' - MODE='mode=UR' - shift 2;; + MODE='mode=UR';; 1) - if [ $# -lt 3 ]; then + if [ -z $SERPORT ]; then + echo "Missing Serial port!" usage 3 - else - PORT=$3 - shift 3 - fi;; + fi + PORT=$SERPORT;; 2) - PORT='USB1' - shift 2;; + PORT='USB1';; *) echo "Protocol unknown!" usage 4;; esac +if [ -z $SERPORT ]; then + shift 2 +else + shift 3 +fi + if [ $# -gt 0 ]; then OPTS="$@" fi -${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS} +upload + +if [ ! -z $SERPORT ] && [ $STATUS -eq 0 ]; then + echo -n "Waiting for $SERPORT serial..." + count=0 + while [ ! -c $SERPORT ] && ((count++ < 40)); do + sleep 0.1 + done + count=0 + res=1 + while [ $res -ne 0 ] && ((count++ < 20)); do + stty -F $SERPORT > /dev/null 2>&1 + res=$? + sleep 1 + done + echo "done" +fi -exit 0 +exit $STATUS