-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflash-emontx6.sh
More file actions
executable file
·173 lines (145 loc) · 3.76 KB
/
flash-emontx6.sh
File metadata and controls
executable file
·173 lines (145 loc) · 3.76 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
set -e
# Ensure running on Linux
if [[ "$(uname -s)" != "Linux" ]]; then
echo "ERROR: This script must be run on a Linux system."
echo "Detected OS: $(uname -s)"
exit 1
fi
# Change working directory to the parent directory of this script
cd "$(dirname "$0")/.."
UF2_FILE="build/emon32.uf2"
DO_BUILD=0
echo "=== emonPi3/TX6 UF2 Flash Script ==="
#
# Parse arguments
#
while [[ $# -gt 0 ]]; do
case "$1" in
--build)
DO_BUILD=1
shift
;;
--uf2)
if [[ -z "${2:-}" ]]; then
echo "ERROR: --uf2 requires a path argument"
echo "Usage: $0 [--uf2 <path>]"
exit 1
fi
UF2_FILE="$2"
shift 2
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--build] [--uf2 <path>]"
exit 1
;;
esac
done
#
# Check picocom is installed
#
if ! command -v picocom >/dev/null 2>&1; then
echo "ERROR: picocom is not installed."
echo "Install it with: sudo apt install picocom"
exit 1
fi
#
# Optional build step
#
if [[ $DO_BUILD -eq 1 ]]; then
echo "Running build..."
if ! make -j; then
echo "ERROR: Build failed."
exit 1
fi
fi
#
# Ensure UF2 exists
#
if [ ! -f "$UF2_FILE" ]; then
echo "ERROR: UF2 file not found at $UF2_FILE"
exit 1
fi
echo "Checking device state..."
#
# 1. Check if device is already in BOOTLOADER MODE
#
BOOT_DEV=$(ls /dev/disk/by-id/usb-emonPi3_* 2>/dev/null || true)
if [ -n "$BOOT_DEV" ]; then
echo "Device is already in bootloader mode."
DEV="$BOOT_DEV"
else
echo "Device not in bootloader mode. Checking for normal mode..."
#
# 2. Detect NORMAL MODE via serial device ID (*Pi3*)
#
SERIAL_LINK=$(ls /dev/serial/by-id/*Pi3* 2>/dev/null || true)
if [ -z "$SERIAL_LINK" ]; then
echo "ERROR: Device not detected in normal or bootloader mode."
echo "Is it connected?"
exit 1
fi
echo "Device found in normal mode."
#
# 3. Resolve serial device path
#
REAL_SERIAL=$(readlink -f "$SERIAL_LINK")
echo "Using serial device: $REAL_SERIAL"
#
# 4. Send 'e' + CRLF, wait, then 'y' + CRLF via picocom
#
echo "Sending bootloader command (e + y)..."
(
sleep 0.2
printf "e\r\n"
sleep 0.2
printf "y\r\n"
) | picocom -q -q --baud 115200 --omap crcrlf,crlf "$REAL_SERIAL" 2>/dev/null
echo "Bootloader command sent. Waiting for device to re-enumerate..."
#
# 5. Give USB stack a moment to settle
#
sleep 1
#
# 6. Wait for bootloader device to appear
#
while true; do
DEV=$(ls /dev/disk/by-id/usb-emonPi3_* 2>/dev/null || true)
if [ -n "$DEV" ]; then
echo "Bootloader device detected: $DEV"
break
fi
echo "Waiting for bootloader device..."
sleep 1
done
fi
#
# 7. Check if already mounted
#
echo "Checking mount state..."
INFO=$(udisksctl info -b "$DEV")
if echo "$INFO" | grep -q "Mounted: yes"; then
MOUNT_POINT=$(echo "$INFO" | sed -n 's/ *MountPoints: \(.*\)/\1/p')
echo "Device is already mounted at: $MOUNT_POINT"
else
echo "Mounting device..."
MOUNT_OUTPUT=$(sudo udisksctl mount -b "$DEV")
# Extract mount point robustly
MOUNT_POINT=$(echo "$MOUNT_OUTPUT" | grep -oE "at (/[^ ]+)" | awk '{print $2}')
if [ -z "$MOUNT_POINT" ]; then
echo "ERROR: Could not determine mount point"
echo "udisksctl output was:"
echo "$MOUNT_OUTPUT"
exit 1
fi
echo "Mounted at: $MOUNT_POINT"
fi
#
# 8. Copy the UF2 file
#
echo "Copying UF2 file..."
sudo cp "$UF2_FILE" "$MOUNT_POINT/"
echo "Syncing..."
sync
echo "Flash complete. Device will reboot automatically."