|
2 | 2 | set -o nounset # Treat unset variables as an error |
3 | 3 | # set -o xtrace # Print command traces before executing command. |
4 | 4 |
|
| 5 | +UNAME_OS="$(uname -s)" |
| 6 | +GNU_GETOPT= |
5 | 7 | STM32CP_CLI= |
6 | 8 | INTERFACE= |
7 | 9 | PORT= |
@@ -53,11 +55,73 @@ aborting() { |
53 | 55 | exit 1 |
54 | 56 | } |
55 | 57 |
|
| 58 | +# Check STM32CubeProgrammer cli availability and getopt version |
| 59 | +case "${UNAME_OS}" in |
| 60 | + Linux*) |
| 61 | + STM32CP_CLI=STM32_Programmer.sh |
| 62 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 63 | + export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH" |
| 64 | + fi |
| 65 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 66 | + export PATH="/opt/stm32cubeprog/bin":"$PATH" |
| 67 | + fi |
| 68 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 69 | + aborting |
| 70 | + fi |
| 71 | + ;; |
| 72 | + Darwin*) |
| 73 | + STM32CP_CLI=STM32_Programmer_CLI |
| 74 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 75 | + export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH" |
| 76 | + fi |
| 77 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 78 | + aborting |
| 79 | + fi |
| 80 | + if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then |
| 81 | + if ! command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then |
| 82 | + echo "Warning: long options not supported due to getopt from FreeBSD usage." |
| 83 | + GNU_GETOPT=n |
| 84 | + else |
| 85 | + export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH" |
| 86 | + fi |
| 87 | + else |
| 88 | + export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH" |
| 89 | + fi |
| 90 | + ;; |
| 91 | + Windows*) |
| 92 | + STM32CP_CLI=STM32_Programmer_CLI.exe |
| 93 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 94 | + if [ -n "${PROGRAMFILES+x}" ]; then |
| 95 | + STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
| 96 | + export PATH="${STM32CP86}":"$PATH" |
| 97 | + fi |
| 98 | + if [ -n "${PROGRAMW6432+x}" ]; then |
| 99 | + STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
| 100 | + export PATH="${STM32CP}":"$PATH" |
| 101 | + fi |
| 102 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 103 | + aborting |
| 104 | + fi |
| 105 | + fi |
| 106 | + ;; |
| 107 | + *) |
| 108 | + echo "Unknown host OS: ${UNAME_OS}." >&2 |
| 109 | + exit 1 |
| 110 | + ;; |
| 111 | +esac |
| 112 | + |
56 | 113 | # parse command line arguments |
57 | 114 | # options may be followed by one colon to indicate they have a required arg |
58 | | -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 |
59 | | - echo "Terminating..." >&2 |
60 | | - exit 1 |
| 115 | +if [ -n "${GNU_GETOPT}" ]; then |
| 116 | + if ! options=$(getopt hi:ef:o:c:r:d:v:p: "$@"); then |
| 117 | + echo "Terminating..." >&2 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +else |
| 121 | + 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 |
| 122 | + echo "Terminating..." >&2 |
| 123 | + exit 1 |
| 124 | + fi |
61 | 125 | fi |
62 | 126 |
|
63 | 127 | eval set -- "$options" |
@@ -109,53 +173,12 @@ while true; do |
109 | 173 | shift |
110 | 174 | break |
111 | 175 | ;; |
| 176 | + *) |
| 177 | + echo "Unknown option $1" |
| 178 | + usage 1 |
| 179 | + ;; |
112 | 180 | esac |
113 | 181 | done |
114 | | -# Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu |
115 | | -UNAME_OS="$(uname -s)" |
116 | | -case "${UNAME_OS}" in |
117 | | - Linux*) |
118 | | - STM32CP_CLI=STM32_Programmer.sh |
119 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
120 | | - export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH" |
121 | | - fi |
122 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
123 | | - export PATH="/opt/stm32cubeprog/bin":"$PATH" |
124 | | - fi |
125 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
126 | | - aborting |
127 | | - fi |
128 | | - ;; |
129 | | - Darwin*) |
130 | | - STM32CP_CLI=STM32_Programmer_CLI |
131 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
132 | | - export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH" |
133 | | - fi |
134 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
135 | | - aborting |
136 | | - fi |
137 | | - ;; |
138 | | - Windows*) |
139 | | - STM32CP_CLI=STM32_Programmer_CLI.exe |
140 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
141 | | - if [ -n "${PROGRAMFILES+x}" ]; then |
142 | | - STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
143 | | - export PATH="${STM32CP86}":"$PATH" |
144 | | - fi |
145 | | - if [ -n "${PROGRAMW6432+x}" ]; then |
146 | | - STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
147 | | - export PATH="${STM32CP}":"$PATH" |
148 | | - fi |
149 | | - if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
150 | | - aborting |
151 | | - fi |
152 | | - fi |
153 | | - ;; |
154 | | - *) |
155 | | - echo "Unknown host OS: ${UNAME_OS}." >&2 |
156 | | - exit 1 |
157 | | - ;; |
158 | | -esac |
159 | 182 |
|
160 | 183 | # Check mandatory options |
161 | 184 | if [ -z "${INTERFACE}" ]; then |
|
0 commit comments