From 775cb473bd5b57543e7ebd5be10b32de721cef25 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 17 Oct 2019 15:57:07 +0200 Subject: [PATCH 1/2] [STM32CubeProg] Wait for serial port availability Signed-off-by: Frederic Pillon --- linux/stm32CubeProg.sh | 75 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 14 deletions(-) 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 From da2ec1129c5e31e04bd95823871ffee23a0fcbc4 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 17 Oct 2019 15:57:07 +0200 Subject: [PATCH 2/2] [STM32CubeProg] Add support to request reset to bootloader mode Signed-off-by: Frederic Pillon --- linux/stm32CubeProg.sh | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/linux/stm32CubeProg.sh b/linux/stm32CubeProg.sh index 4b0d28a04..e03fa618d 100755 --- a/linux/stm32CubeProg.sh +++ b/linux/stm32CubeProg.sh @@ -32,6 +32,7 @@ usage() echo "## Options:" echo "## For SWD: no mandatory options" echo "## For DFU: no mandatory options" + echo "## Use '-serport=' to request reset to bootloader mode" echo "## For Serial: 'serport='" echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0" echo "##" @@ -60,6 +61,26 @@ check_tool() { fi } +bootloaderMode() { + if [ ! -z $SERPORT ]; then + # Try to configure it at 1200 to restart + # in Bootloader mode + if [ -c $SERPORT ]; then + count=0 + res=1 + while [ $res -ne 0 ] && ((count++ < 5)); do + # echo "Try to set $SERPORT at 1200" + stty -F $SERPORT 1200 > /dev/null 2>&1 + res=$? + sleep 0.1 + done + if [ $res -eq 0 ]; then + sleep 0.5 + fi + fi + fi +} + upload() { count=0 STATUS=1 @@ -112,7 +133,8 @@ case $PROTOCOL in fi PORT=$SERPORT;; 2) - PORT='USB1';; + PORT='USB1' + bootloaderMode;; *) echo "Protocol unknown!" usage 4;;