-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.sh
executable file
·230 lines (192 loc) · 7.4 KB
/
test.sh
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bash
## Auto Flash and Test BL602 with GPIO Control on Linux SBC.
## Pins to be connected:
## | SBC | BL602 | Function
## | -------|----------|----------
## | GPIO 2 | GPIO 8 | Flashing Mode
## | GPIO 3 | RST | Reset
## | GND | GND | Ground
## | USB | USB | USB UART
set -e ## Exit when any command fails
set -x ## Echo commands
## Default Build Prefix is "upstream"
if [ "$BUILD_PREFIX" == '' ]; then
export BUILD_PREFIX=upstream
fi
## Default Build Date is today (YYYY-MM-DD)
if [ "$BUILD_DATE" == '' ]; then
export BUILD_DATE=$(date +'%Y-%m-%d')
fi
## Default USB Device is /dev/ttyUSB0
if [ "$USB_DEVICE" == '' ]; then
export USB_DEVICE=/dev/ttyUSB0
fi
## Add Rust to the PATH
source $HOME/.cargo/env
set +x ## Disable echo
echo "----- Download the latest $BUILD_PREFIX NuttX build for $BUILD_DATE"
set -x ## Enable echo
wget -q https://github.com/lupyuen/incubator-nuttx/releases/download/$BUILD_PREFIX-$BUILD_DATE/nuttx.zip -O /tmp/nuttx.zip
pushd /tmp
unzip -o nuttx.zip
popd
set +x ## Disable echo
## Print the Commit Hashes
if [ -f /tmp/nuttx.hash ]; then
cat /tmp/nuttx.hash
fi
## Write the Release Tag for populating the Release Log later
echo "$BUILD_PREFIX-$BUILD_DATE" >/tmp/release.tag
echo "----- Enable GPIO 2 and 3"
if [ ! -d /sys/class/gpio/gpio2 ]; then
echo 2 >/sys/class/gpio/export ; sleep 1 ## Must sleep or next GPIO command will fail with "Permission Denied"
fi
if [ ! -d /sys/class/gpio/gpio3 ]; then
echo 3 >/sys/class/gpio/export ; sleep 1 ## Must sleep or next GPIO command will fail with "Permission Denied"
fi
echo "----- Set GPIO 2 and 3 as output"
echo out >/sys/class/gpio/gpio2/direction
echo out >/sys/class/gpio/gpio3/direction
echo "----- Set GPIO 2 to High (BL602 Flashing Mode)"
echo 1 >/sys/class/gpio/gpio2/value ; sleep 1
echo "----- Toggle GPIO 3 High-Low-High (Reset BL602)"
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo 0 >/sys/class/gpio/gpio3/value ; sleep 1
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo "----- Toggle GPIO 3 High-Low-High (Reset BL602 again)"
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo 0 >/sys/class/gpio/gpio3/value ; sleep 1
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo "----- BL602 is now in Flashing Mode"
echo "----- Flash BL602 over USB UART with blflash"
set -x ## Enable echo
blflash flash /tmp/nuttx.bin --port $USB_DEVICE
set +x ## Disable echo
sleep 1
echo "----- Set GPIO 2 to Low (BL602 Normal Mode)"
echo 0 >/sys/class/gpio/gpio2/value ; sleep 1
echo "----- Toggle GPIO 3 High-Low-High (Reset BL602)"
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo 0 >/sys/class/gpio/gpio3/value ; sleep 1
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo "----- BL602 is now in Normal Mode"
## Set USB UART to 2 Mbps
stty -F $USB_DEVICE raw 2000000
## Show the BL602 output and capture to /tmp/test.log.
## Run this in the background so we can kill it later.
cat $USB_DEVICE | tee /tmp/test.log &
echo "----- Toggle GPIO 3 High-Low-High (Reset BL602)"
echo "----- Here is the BL602 Output..."
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
echo 0 >/sys/class/gpio/gpio3/value ; sleep 1
echo 1 >/sys/class/gpio/gpio3/value ; sleep 1
## Wait a while for BL602 to finish booting
sleep 1
## Check whether BL602 has crashed
set +e ## Don't exit when any command fails
match=$(grep "registerdump" /tmp/test.log)
set -e ## Exit when any command fails
if [ "$match" == "" ]; then
## If BL602 has not crashed, send the test command to BL602
echo "uname -a" >$USB_DEVICE ; sleep 1
echo "ls /dev" >$USB_DEVICE ; sleep 1
echo "spi_test2" >$USB_DEVICE ; sleep 2
## Check whether NuttX has booted properly
set +e ## Don't exit when any command fails
match=$(grep "command not found" /tmp/test.log)
set -e ## Exit when any command fails
## If NuttX has booted properly, show the status
if [ "$match" != "" ]; then
echo; echo "===== Boot OK"
else
## Check whether SX1262 is OK
set +e ## Don't exit when any command fails
match=$(grep "SX1262 Register 8 is 0x80" /tmp/test.log)
set +e ## Don't exit when any command fails
## If SX1262 is not OK, quit
if [ "$match" == "" ]; then
echo; echo "===== Error: SX1262 is NOT OK. Check the SPI connection"
test_status=unknown
else
echo ; echo "----- Send command to BL602: lorawan_test" ; sleep 2
echo "" >$USB_DEVICE
echo "lorawan_test" >$USB_DEVICE
## Wait a while for the test command to run
sleep 40
## Check whether BL602 has joined the LoRaWAN Network
set +e ## Don't exit when any command fails
match=$(grep "JOINED" /tmp/test.log)
set -e ## Exit when any command fails
## If BL602 has joined the LoRaWAN Network, then everything is super hunky dory!
if [ "$match" != "" ]; then
echo; echo "===== All OK! BL602 has successfully joined the LoRaWAN Network"
else
echo; echo "===== Unknown Status"
test_status=unknown
fi
fi
fi
else
## If BL602 has crashed, do the Crash Analysis
echo; echo "===== Boot FAILED. Below is the Crash Analysis"; echo
## Don't exit when any command fails (grep)
set +e
## Find all code addresses 23?????? in the Output Log, remove duplicates, skip 23007000.
## Returns a newline-delimited list of addresses: "23011000\n230053a0\n..."
grep --extended-regexp \
--only-matching \
"23[0-9a-f]{6}" \
/tmp/test.log \
| grep -v "23007000" \
| uniq \
>/tmp/test.addr
## For every address, show the corresponding line in the disassembly
for addr in $(cat /tmp/test.addr); do
## Skip addresses that don't match
match=$(grep "$addr:" /tmp/nuttx.S)
if [ "$match" != "" ]; then
echo "----- Code Address $addr"
grep \
--context=5 \
--color=auto \
"$addr:" \
/tmp/nuttx.S
echo
fi
done
## Find all data addresses 42?????? in the Output Log, remove duplicates.
## Returns a newline-delimited list of addresses: "23011000\n230053a0\n..."
grep --extended-regexp \
--only-matching \
"42[0-9a-f]{6}" \
/tmp/test.log \
| uniq \
>/tmp/test.addr
## For every address, show the corresponding line in the disassembly
for addr in $(cat /tmp/test.addr); do
## Skip addresses that don't match
match=$(grep "^$addr" /tmp/nuttx.S)
if [ "$match" != "" ]; then
echo "----- Data Address $addr"
grep \
--color=auto \
"^$addr" \
/tmp/nuttx.S \
| grep -v "noinit"
echo
fi
done
## Exit when any command fails
set -e
fi
## Kill the background task that captures the BL602 output
kill %1
## We don't disable GPIO 2 and 3 because otherwise BL602 might keep rebooting
echo
## If status is unknown, start the second script
if [ "$test_status" == "unknown" ]; then
read -p "Possible LoRa Interference. Close the windows, pull the shades, draw the drapes. Press Enter to retest..."
SCRIPT_PATH="${BASH_SOURCE}"
SCRIPT_DIR="$(cd -P "$(dirname -- "${SCRIPT_PATH}")" >/dev/null 2>&1 && pwd)"
$SCRIPT_DIR/test2.sh
fi