From e3a27c08b301e7ff8792d3b2a4c711431a42d212 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 28 Mar 2024 08:51:40 +0100 Subject: [PATCH 1/5] refactor(stm32CubeProg): use getopt and extend options Added options: Serial: rts, dtr (optional) DFU: vid, pid (mandatory) Signed-off-by: Frederic Pillon --- stm32CubeProg.sh | 240 ++++++++++++++++++++++++++++++----------------- 1 file changed, 152 insertions(+), 88 deletions(-) diff --git a/stm32CubeProg.sh b/stm32CubeProg.sh index ed5e00c03..403d1a425 100644 --- a/stm32CubeProg.sh +++ b/stm32CubeProg.sh @@ -1,75 +1,143 @@ #!/bin/sh - set -o nounset # Treat unset variables as an error -# set -o xtrace # Print command traces before executing command. +# set -o xtrace # Print command traces before executing command. STM32CP_CLI= +INTERFACE= +PORT= +FILEPATH= ADDRESS=0x8000000 -ERASE="" -MODE="" -PORT="" -OPTS="" +OFFSET=0x0 +# Optional +ERASE= +# Optional for Serial +RTS= +DTR= +# Mandatory for DFU +VID= +PID= ############################################################################### ## Help function usage() { - echo "############################################################" - echo "##" - echo "## $(basename "$0") [OPTIONS]" - echo "##" - echo "## protocol:" - echo "## 0: SWD" - echo "## 1: Serial" - echo "## 2: DFU" - echo "## Note: prefix it by 1 to erase all sectors." - echo "## Ex: 10 erase all sectors using SWD interface." - echo "## file_path: file path name to be downloaded: (bin, hex)" - echo "## offset: offset to add to $ADDRESS" - echo "## Options:" - echo "## For SWD and DFU: no mandatory options" - echo "## For Serial: " - echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0 or COM1" - echo "##" - echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI" - echo "## They have to be valid commands for STM32CubeProgrammer cli" - echo "## Ex: -rst: Reset system" - echo "############################################################" + echo "Usage: $(basename "$0") [OPTIONS]... + + Mandatory options: + -i, --interface <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial' + -f, --file file path to be downloaded: bin or hex + Optional options: + -e, --erase erase all sectors before flashing + -o, --offset offset from flash base ($ADDRESS) where flashing should start + + Specific options for Serial protocol: + Mandatory: + -c, --com serial identifier, ex: COM1 or /dev/ttyS0,... + Optional: + -r, --rts polarity of RTS signal ('low' by default) + -d, --dtr polarity of DTR signal + + Specific options for DFU protocol: + Mandatory: + -v, --vid vendor id, ex: 0x0483 + -p, --pid product id, ex: 0xdf11 + +" >&2 exit "$1" } +aborting() { + echo "STM32CubeProgrammer not found ($STM32CP_CLI). + Please install it or add '/bin' to your PATH environment: + https://www.st.com/en/development-tools/stm32cubeprog.html + Aborting!" >&2 + exit 1 +} + +# parse command line arguments +# options may be followed by one colon to indicate they have a required arg +if ! options=$(getopt -a -o hi:ef:o:c:r:d:v:p: --long help,interface:,erase,file:,offset:,com:,rts:,dtr:,vid:,pid: -- "$@"); then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$options" + +while true; do + case "$1" in + -h | --help | -\?) + usage 0 + ;; + -i | --interface) + INTERFACE=$(echo "$2" | tr '[:upper:]' '[:lower:]') + echo "Selected interface: $INTERFACE" + shift 2 + ;; + -e | --erase) + ERASE="--erase all" + shift 1 + ;; + -f | --file) + FILEPATH=$2 + shift 2 + ;; + -o | --offset) + OFFSET=$2 + ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET))) + shift 2 + ;; + -c | --com) + PORT=$2 + shift 2 + ;; + -r | --rts) + RTS=$(echo "rts=$2" | tr '[:upper:]' '[:lower:]') + shift 2 + ;; + -d | --dtr) + DTR=$(echo "dtr=$2" | tr '[:upper:]' '[:lower:]') + shift 2 + ;; + -v | --vid) + VID=$2 + shift 2 + ;; + -p | --pid) + PID=$2 + shift 2 + ;; + --) + shift + break + ;; + esac +done +# Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu UNAME_OS="$(uname -s)" case "${UNAME_OS}" in Linux*) STM32CP_CLI=STM32_Programmer.sh - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH" fi - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then export PATH="/opt/stm32cubeprog/bin":"$PATH" fi - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then - echo "STM32CubeProgrammer not found ($STM32CP_CLI)." - echo "Please install it or add '/bin' to your PATH environment:" - echo "https://www.st.com/en/development-tools/stm32cubeprog.html" - echo "Aborting!" - exit 1 + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then + aborting fi ;; Darwin*) STM32CP_CLI=STM32_Programmer_CLI - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH" fi - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then - echo "STM32CubeProgrammer not found ($STM32CP_CLI)." - echo "Please install it or add '/bin' to your PATH environment:" - echo "https://www.st.com/en/development-tools/stm32cubeprog.html" - echo "Aborting!" - exit 1 + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then + aborting fi ;; Windows*) STM32CP_CLI=STM32_Programmer_CLI.exe - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then if [ -n "${PROGRAMFILES+x}" ]; then STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin export PATH="${STM32CP86}":"$PATH" @@ -78,69 +146,65 @@ case "${UNAME_OS}" in STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin export PATH="${STM32CP}":"$PATH" fi - if ! command -v $STM32CP_CLI > /dev/null 2>&1; then - echo "STM32CubeProgrammer not found ($STM32CP_CLI)." - echo "Please install it or add '\bin' to your PATH environment:" - echo "https://www.st.com/en/development-tools/stm32cubeprog.html" - echo "Aborting!" + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then + aborting fi fi ;; *) - echo "Unknown host OS: ${UNAME_OS}." + echo "Unknown host OS: ${UNAME_OS}." >&2 exit 1 ;; esac -if [ $# -lt 3 ]; then - echo "Not enough arguments!" - usage 2 +# Check mandatory options +if [ -z "${INTERFACE}" ]; then + echo "Error missing interface!" >&2 + usage 1 fi - -# Parse options -PROTOCOL=$1 -FILEPATH=$2 -OFFSET=$3 -ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET))) - -# Protocol $1 -# 1x: Erase all sectors -if [ "$1" -ge 10 ]; then - ERASE="yes" - PROTOCOL=$(($1 - 10)) +if [ -z "${FILEPATH}" ]; then + echo "Error missing file argmument!" >&2 + usage 1 fi -# Protocol $1 -# 0: SWD -# 1: Serial -# 2: DFU -case $PROTOCOL in - 0) - PORT="SWD" - MODE="mode=UR" - shift 3 +if [ ! -r "${FILEPATH}" ]; then + echo "Error ${FILEPATH} does not exist!" >&2 + usage 1 +fi + +case "${INTERFACE}" in + swd) + ${STM32CP_CLI} --connect port=SWD mode=UR "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}" ;; - 1) - if [ $# -lt 4 ]; then - usage 3 - else - PORT=$4 - shift 4 + dfu) + if [ -z "${VID}" ] || [ -z "${PID}" ]; then + echo "Missing mandatory arguments for DFU mode (VID/PID)!" >&2 + exit 1 fi + ${STM32CP_CLI} --connect port=usb1 VID="${VID}" PID="${PID}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}" ;; - 2) - PORT="USB1" - shift 3 + serial) + if [ -z "${PORT}" ]; then + echo "Missing mandatory arguments for serial mode: serial identifier!" >&2 + exit 1 + fi + if [ -n "${RTS}" ]; then + if [ "${RTS}" != "rts=low" ] && [ "${RTS}" != "rts=high" ]; then + echo "Wrong rts value waiting high or low instead of ${RTS}" >&2 + exit 1 + fi + fi + if [ -n "${DTR}" ]; then + if [ "${DTR}" != "dtr=low" ] && [ "${DTR}" != "dtr=high" ]; then + echo "Wrong dtr value waiting high or low instead of ${DTR}" >&2 + exit 1 + fi + fi + ${STM32CP_CLI} --connect port="${PORT}" "${RTS}" "${DTR}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}" ;; *) - echo "Protocol unknown!" + echo "Protocol unknown!" >&2 usage 4 ;; esac -if [ $# -gt 0 ]; then - OPTS="$*" -fi - -${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" "${ADDRESS}" -s "${ADDRESS}" "${OPTS}" - exit $? From f0c3abe3b7dbf7a0286088d57c1951659c9a6d7f Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 26 Mar 2024 10:46:07 +0100 Subject: [PATCH 2/5] chore(maple_upload): remove useless part Signed-off-by: Frederic Pillon --- maple_upload.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/maple_upload.sh b/maple_upload.sh index 2b10603a9..c391a0af8 100755 --- a/maple_upload.sh +++ b/maple_upload.sh @@ -52,11 +52,7 @@ fi COUNTER=5 while - if [ $# -eq 5 ]; then - "${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" "--dfuse-address $5" -R - else - "${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" -R - fi + "${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" -R ret=$? do if [ $ret -eq 0 ]; then From b84d4ff2a3f70f412e0ea877377d1a0a28681268 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 22 Mar 2024 17:36:19 +0100 Subject: [PATCH 3/5] feat(maple_upload): replace usbID by usb vid and pid Signed-off-by: Frederic Pillon --- maple_upload.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/maple_upload.sh b/maple_upload.sh index c391a0af8..98fee0d81 100755 --- a/maple_upload.sh +++ b/maple_upload.sh @@ -2,13 +2,14 @@ set -e -if [ $# -lt 4 ]; then - echo "Usage: $0 " >&2 +if [ $# -lt 5 ]; then + echo "Usage: $0 " >&2 exit 1 fi altID="$2" -usbID="$3" -binfile="$4" +usbVID=${3#"0x"} +usbPID=${4#"0x"} +binfile="$5" EXT="" UNAME_OS="$(uname -s)" @@ -52,7 +53,7 @@ fi COUNTER=5 while - "${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" -R + "${DIR}/dfu-util.sh" -d "${usbVID}:${usbPID}" -a "${altID}" -D "${binfile}" -R ret=$? do if [ $ret -eq 0 ]; then From 75e067fe69ba6f38d44f64a7d1cfef143eb1531e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 27 Mar 2024 13:46:00 +0100 Subject: [PATCH 4/5] fix(maple_upload): review output redirection Signed-off-by: Frederic Pillon --- maple_upload.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maple_upload.sh b/maple_upload.sh index 98fee0d81..0cc71aea2 100755 --- a/maple_upload.sh +++ b/maple_upload.sh @@ -70,13 +70,13 @@ do fi done -printf "Waiting for %s serial..." "${dummy_port_fullpath}" >&2 +printf "Waiting for %s serial..." "${dummy_port_fullpath}" COUNTER=40 if [ ${OS_DIR} = "win" ]; then while [ $COUNTER -gt 0 ]; do if ! "${DIR}/${OS_DIR}/check_port${EXT}" "${dummy_port_fullpath}"; then COUNTER=$((COUNTER - 1)) - printf "." >&2 + printf "." sleep 0.1 else break @@ -86,7 +86,7 @@ if [ ${OS_DIR} = "win" ]; then else while [ ! -r "${dummy_port_fullpath}" ] && [ $COUNTER -gt 0 ]; do COUNTER=$((COUNTER - 1)) - printf "." >&2 + printf "." sleep 0.1 done fi @@ -95,5 +95,5 @@ if [ $COUNTER -eq -0 ]; then echo " Timed out." >&2 exit 1 else - echo " Done." >&2 + echo " Done." fi From ed7bc74942222a4994d9a7f333209d1da56ea39e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 27 Mar 2024 13:46:47 +0100 Subject: [PATCH 5/5] fix(maple_upload): add a delay after upload else logs are not properly displayed (mixed). Signed-off-by: Frederic Pillon --- maple_upload.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maple_upload.sh b/maple_upload.sh index 0cc71aea2..98a66f5dd 100755 --- a/maple_upload.sh +++ b/maple_upload.sh @@ -70,6 +70,8 @@ do fi done +sleep 1 + printf "Waiting for %s serial..." "${dummy_port_fullpath}" COUNTER=40 if [ ${OS_DIR} = "win" ]; then