forked from jeelabs/esp-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
megaflash
executable file
·114 lines (97 loc) · 2.79 KB
/
megaflash
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
#! /bin/bash
#
# Flash an Arduino Mega with STK500v2 using the esp-link built-in programmer
# Basically we first reset the AVR and get in sync, and then send the hex file
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# Thorsten von Eicken wrote this file. As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return.
#
# Danny Backx wrote the changes for Arduino Mega.
# ----------------------------------------------------------------------------
show_help() {
cat <<EOT
Usage: ${0##*/} [-options...] hostname sketch.hex
Flash the Mega running optiboot attached to esp-link with the sketch.
Note : this is for stk500v2 MCUs, use avrflash for Arduino Uno etc instead.
-v Be verbose
-h show this help
Example: ${0##*/} -v esp-link mysketch.hex
${0##*/} 192.168.4.1 mysketch.hex
EOT
}
if ! which curl >/dev/null; then
echo "ERROR: Cannot find curl: it is required for this script." >&2
exit 1
fi
start=`date +%s`
# ===== Parse arguments
verbose=
while getopts "hvx:" opt; do
case "$opt" in
h) show_help; exit 0 ;;
v) verbose=1 ;;
x) foo="$OPTARG" ;;
'?') show_help >&2; exit 1 ;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND-1))"
# Get the fixed arguments
if [[ $# != 2 ]]; then
show_help >&2
exit 1
fi
hostname=$1
hex=$2
re='[-A-Za-z0-9.]+'
if [[ ! "$hostname" =~ $re ]]; then
echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2
exit 1
fi
if [[ ! -r "$hex" ]]; then
echo "ERROR: cannot read hex file ($hex)" >&2
exit 1
fi
# ===== Get AVR in sync
[[ -n "$verbose" ]] && echo "Resetting AVR with http://$hostname/pgmmega/sync" >&2
v=; [[ -n "$verbose" ]] && v=-v
sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgmmega/sync"`
if [[ $? != 0 || "$sync" != 204 ]]; then
echo "Error resetting AVR" >&2
exit 1
fi
while true; do
# sync=`curl -m 10 $v -s "http://$hostname/pgmmega/sync"`
sync=`curl $v -s "http://$hostname/pgmmega/sync"`
if [[ $? != 0 ]]; then
echo "Error checking sync" >&2
exit 1
fi
case "$sync" in
SYNC*)
echo "AVR in $sync" >&2
break;;
"NOT READY"*)
[[ -n "$verbose" ]] && echo " Waiting for sync..." >&2
;;
*)
echo "Error checking sync: $sync" >&2
exit 1
;;
esac
sleep 0.1
done
# ===== Send HEX file
[[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2
sync=`curl -m 20 $v -s -g -d "@$hex" "http://$hostname/pgmmega/upload"`
echo $sync
if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then
echo "Error programming AVR" >&2
exit 1
fi
sec=$(( `date +%s` - $start ))
echo "Success, took $sec seconds" >&2
exit 0