forked from tropicsquare/tropic01-stm32u5-usb-devkit-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash_tool.sh
More file actions
executable file
·142 lines (115 loc) · 3.5 KB
/
flash_tool.sh
File metadata and controls
executable file
·142 lines (115 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
#
# Script Name: flash_tool.sh
# Description: Does automatic flashing and communication testing of TS13xx board.
# Author: Tropic Square
# Date: 2025-06-10
# Version: 1.0.0
#
# Usage: Run script ./flash_tool.sh and then connect TS13xx device with pressed button and follow instructions.
#
# Notes: Make sure no other /dev/ttyACM* devices connected to this computer.
#
# Dependencies: coreutils usbutils dfu-util
#
# License: BSD, see LICENSE.txt
FIRMWARE="app/build/app.bin"
ACM_DEVICE="/dev/ttyACM0"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
err() {
echo -e "${RED}ERROR: $1 ${NC}"
}
clean() {
echo -n "$1" | tr -d '\r\n'
}
parse_version() {
local hex="$1"
# Convert hex string to array of bytes
local -a s
for ((i=0; i<${#hex}; i+=2)); do
byte="0x${hex:$i:2}"
s+=($byte)
done
# get version from TS_L2_GET_INFO_REQ response
local major=$((s[6]))
local minor=$((s[5]))
local patch=$((s[4]))
printf "v%d.%d.%d\n" "$major" "$minor" "$patch"
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
err "Required command '$1' not found." >&2
exit 1
fi
}
echo "STM32 DFU Auto-Flasher"
require_command stty
require_command lsusb
require_command dfu-util
if [ ! -e $FIRMWARE ]; then
err "Firmware $FIRMWARE not found, you need to compile it"
exit 1
fi
if [ -e $ACM_DEVICE ]; then
err "Device $ACM_DEVICE already connected, cant continue. \n Disconnect the device or update this script."
exit 2
fi
while true; do
echo ""
echo "Waiting for DFU device (connect TS13xx board with pressed button) ..."
# Wait for DFU device to appear
while ! lsusb | grep -i "STM.*DFU" >/dev/null; do
sleep 1
done
echo "DFU device detected, release button if still pressed, flashing ... "
# Flash firmware using dfu-util
FLASH_OUTPUT=$(dfu-util -a 0 -s 0x08000000:leave -D "$FIRMWARE" 2>&1)
if echo "$FLASH_OUTPUT" | grep -q "File downloaded successfully"; then
echo "Flash successful."
else
echo "$FLASH_OUTPUT"
err "Flashing failed !"
sleep 5
echo "continue ... "
continue
fi
echo "Waiting for device to reset and enumerate as $ACM_DEVICE ..."
# Wait for serial device to appear
for i in {1..5}; do
if [ -e $ACM_DEVICE ]; then
break
fi
sleep 1
done
if [ ! -e $ACM_DEVICE ]; then
err "$ACM_DEVICE not found after flashing."
continue
fi
echo "Device found, testing ... "
sleep 1
stty -F $ACM_DEVICE 115200 time 5 cs8 -hupcl -icrnl -ixon -ixoff -isig -icanon -iexten -echo
echo "SN" > $ACM_DEVICE &
read -t 0.1 -r RSP < $ACM_DEVICE
SN=$(echo -n "$RSP" | grep 'SN')
SN=$(clean "$SN")
echo -e "${YELLOW}Board $SN ${NC}"
read -t 0.1 -r RSP < $ACM_DEVICE # fetch and discard "OK" response
# send TS_L2_GET_INFO_REQ (see tropic01_l2_api.h)
echo "010202002b98" > $ACM_DEVICE &
read -t 0.1 -r RSP < $ACM_DEVICE
RSP=$(clean "$RSP")
if [[ "$RSP" == "01FFFFFFFFFF" ]]; then
echo -e "${GREEN}Request send OK ${NC}"
sleep 0.1
echo "aaffffffffffffffff" > $ACM_DEVICE &
read -t 0.1 -r RSP < $ACM_DEVICE
VER=$(clean "$RSP")
HVER=$(parse_version "$VER")
echo -e "${GREEN}Response: $VER == Chip version: ${YELLOW} $HVER ${NC}"
else
err "Unexpected chip response $RSP"
fi
done