Skip to content

Commit 4020213

Browse files
committed
fix(stm32CubeProg): use BSD getopt for MacOS
On MacOS getopt is the BSD based one while other using the gnu-based getopt. Then "-o" and long options are not supported Fixes #99 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 354699b commit 4020213

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

stm32CubeProg.sh

+39-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -o nounset # Treat unset variables as an error
33
# set -o xtrace # Print command traces before executing command.
44

5+
UNAME_OS="$(uname -s)"
56
STM32CP_CLI=
67
INTERFACE=
78
PORT=
@@ -23,23 +24,23 @@ usage() {
2324
echo "Usage: $(basename "$0") [OPTIONS]...
2425
2526
Mandatory options:
26-
-i, --interface <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial'
27-
-f, --file <path> file path to be downloaded: bin or hex
27+
-i <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial'
28+
-f <path> file path to be downloaded: bin or hex
2829
Optional options:
29-
-e, --erase erase all sectors before flashing
30-
-o, --offset <hex value> offset from flash base ($ADDRESS) where flashing should start
30+
-e
31+
-o <hex value> offset from flash base ($ADDRESS) where flashing should start
3132
3233
Specific options for Serial protocol:
3334
Mandatory:
34-
-c, --com <name> serial identifier, ex: COM1 or /dev/ttyS0,...
35+
-c <name> serial identifier, ex: COM1 or /dev/ttyS0,...
3536
Optional:
36-
-r, --rts <low/high> polarity of RTS signal ('low' by default)
37-
-d, --dtr <low/high> polarity of DTR signal
37+
-r <low/high> polarity of RTS signal ('low' by default)
38+
-d <low/high> polarity of DTR signal
3839
3940
Specific options for DFU protocol:
4041
Mandatory:
41-
-v, --vid <hex value> vendor id, ex: 0x0483
42-
-p, --pid <hex value> product id, ex: 0xdf11
42+
-v <hex value> vendor id, ex: 0x0483
43+
-p <hex value> product id, ex: 0xdf11
4344
4445
" >&2
4546
exit "$1"
@@ -55,64 +56,76 @@ aborting() {
5556

5657
# parse command line arguments
5758
# 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
61-
fi
62-
59+
case "${UNAME_OS}" in
60+
Darwin*)
61+
if ! options=$(getopt hi:ef:o:c:r:d:v:p: "$@"); then
62+
echo "Terminating..." >&2
63+
exit 1
64+
fi
65+
;;
66+
*)
67+
if ! options=$(getopt -o hi:ef:o:c:r:d:v:p: -- "$@"); then
68+
echo "Terminating..." >&2
69+
exit 1
70+
fi
71+
;;
72+
esac
6373
eval set -- "$options"
6474

6575
while true; do
6676
case "$1" in
67-
-h | --help | -\?)
77+
-h | -\?)
6878
usage 0
6979
;;
70-
-i | --interface)
80+
-i)
7181
INTERFACE=$(echo "$2" | tr '[:upper:]' '[:lower:]')
7282
echo "Selected interface: $INTERFACE"
7383
shift 2
7484
;;
75-
-e | --erase)
85+
-e)
7686
ERASE="--erase all"
7787
shift 1
7888
;;
79-
-f | --file)
89+
-f)
8090
FILEPATH=$2
8191
shift 2
8292
;;
83-
-o | --offset)
93+
-o)
8494
OFFSET=$2
8595
ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET)))
8696
shift 2
8797
;;
88-
-c | --com)
98+
-c)
8999
PORT=$2
90100
shift 2
91101
;;
92-
-r | --rts)
102+
-r)
93103
RTS=$(echo "rts=$2" | tr '[:upper:]' '[:lower:]')
94104
shift 2
95105
;;
96-
-d | --dtr)
106+
-d)
97107
DTR=$(echo "dtr=$2" | tr '[:upper:]' '[:lower:]')
98108
shift 2
99109
;;
100-
-v | --vid)
110+
-v)
101111
VID=$2
102112
shift 2
103113
;;
104-
-p | --pid)
114+
-p)
105115
PID=$2
106116
shift 2
107117
;;
108118
--)
109119
shift
110120
break
111121
;;
122+
*)
123+
echo "Unknown option $1"
124+
usage 1
125+
;;
112126
esac
113127
done
114-
# Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu
115-
UNAME_OS="$(uname -s)"
128+
# Check STM32CubeProgrammer cli availability
116129
case "${UNAME_OS}" in
117130
Linux*)
118131
STM32CP_CLI=STM32_Programmer.sh

0 commit comments

Comments
 (0)