This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
avrflash
executable file
·146 lines (129 loc) · 3.63 KB
/
avrflash
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
#! /bin/bash
#
# Flash an AVR with optiboot 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.
# ----------------------------------------------------------------------------
show_help() {
cat <<EOT
Usage: ${0##*/} [-options...] hostname sketch.hex
Flash the AVR running optiboot attached to esp-link with the sketch.
-v Be verbose
-h show this help
Example: ${0##*/} -v esp-link mysketch.hex
${0##*/} 192.168.4.1 mysketch.hex
EOT
}
case "$OSTYPE" in
solaris* | darwin* | linux* | bsd*)
if ! which curl >/dev/null; then
echo "ERROR: Cannot find curl: it is required for this script." >&2
exit 1
fi
;;
msys | cygwin)
if [ ! -e $PWD/bin/curl ]; then
echo "ERROR: Cannot find curl: it is required for this script." >&2
exit 1
fi
;;
*)
echo ""
echo "Your system is not supported....aborting !"
echo ""
exit 0
;;
esac
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/pgm/sync" >&2
v=; [[ -n "$verbose" ]] && v=-v
sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgm/sync"`
if [[ $? != 0 || "$sync" != 204 ]]; then
echo "Error resetting AVR" >&2
echo ""
echo "ESP is restarting, please wait"
curl -m 10 -s "http://$hostname/log/reset"
sleep 2
echo "Restart done ! Please try again. Thank you"
exit 1
fi
while true; do
sync=`curl -m 10 $v -s "http://$hostname/pgm/sync"`
if [[ $? != 0 ]]; then
echo "Error checking sync" >&2
echo ""
echo "ESP is restarting, please wait"
curl -m 10 -s "http://$hostname/log/reset"
sleep 2
echo "Restart done ! Please try again. Thank you"
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
echo ""
echo "ESP is restarting, please wait"
curl -m 10 -s "http://$hostname/log/reset"
sleep 2
echo "Restart done ! Please try again. Thank you"
exit 1
;;
esac
sleep 0.1
done
# ===== Send HEX file
[[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2
sync=`curl -m 10 $v -s -g -d "@$hex" "http://$hostname/pgm/upload"`
echo $sync
if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then
echo "Error programming AVR" >&2
echo ""
echo "ESP is restarting, please wait"
curl -m 10 -s "http://$hostname/log/reset"
sleep 2
echo "Restart done ! Please try again. Thank you"
exit 1
fi
sec=$(( `date +%s` - $start ))
echo "Success, took $sec seconds" >&2
exit 0