-
Notifications
You must be signed in to change notification settings - Fork 6
/
nanovna_config.sh
executable file
·153 lines (143 loc) · 4.21 KB
/
nanovna_config.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
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Tool to read and write the configuration and calibration data of NanoVNA[-H|-H4]
# The data is stored on top of flash memory
# Address and size depend on device and FW variant (select below)
########################################################################################
#
# BEGIN OF CONFIG AREA
#
# set either "H4" or "H" for DiSlord's FW
# https://github.com/DiSlord/NanoVNA-D
# or "H_HORO" for Ho-Ro's noSD 8 slot FW modification
# https://github.com/Ho-Ro/NanoVNA-D/tree/NanoVNA-noSD
#
#VARIANT="H4"
VARIANT="H"
#VARIANT="H_HORO"
#
# the USB serial port of NanoVNA (default for Linux)
SERIAL_DEVICE="/dev/ttyACM0"
#
# flash start address (equal for -H and -H4)
FLASH=0x08000000
#
#
# END OF CONFIG AREA
#
########################################################################################
# exit at undefined variables
set -u
# debug output
# set -x
# how are we called (i.e. shall we read or write)
CMD_NAME="$(basename $0)"
if [ "$#" -lt 1 ]; then # no command given
CMD="NONE"
else
CMD="$1"
fi
# HACK for Ho-Ro 8 slot FW
if [ "$CMD" = "SAVE8" ]; then
CMD=SAVE
VARIANT="H_HORO"
elif [ "$CMD" = "RESTORE8" ]; then
CMD=RESTORE
VARIANT="H_HORO"
fi
# setup filename, flash address and config size
if [ "$VARIANT" = "H4" ]; then
PROP_MAX=7
DEVICE="NanoVNA-H4_${PROP_MAX}_slots"
FLASH_SIZE=0x40000
CONF_SIZE=0x800
PROP_SIZE=0x4000
SLOT0=0x0800
MAGIC=434f4e55
elif [ "$VARIANT" = "H" ]; then
PROP_MAX=5
DEVICE="NanoVNA-H_${PROP_MAX}_slots"
FLASH_SIZE=0x20000
CONF_SIZE=0x800
PROP_SIZE=0x1800
SLOT0=0x0800
MAGIC=434f4e52
elif [ "$VARIANT" = "H_HORO" ]; then
PROP_MAX=8
DEVICE="NanoVNA-H_${PROP_MAX}_slots"
FLASH_SIZE=0x20000
CONF_SIZE=0x800
PROP_SIZE=0x1800
SLOT0=0xA800
MAGIC=434f4e52
else
echo VARIANT must be one of "H4", "H", or "H_HORO"
exit
fi
# calculate size and address of config
CONFIG_SIZE=$(( $CONF_SIZE + $PROP_MAX * $PROP_SIZE ))
CONFIG_START=$(( $FLASH + $FLASH_SIZE - $CONFIG_SIZE ))
# format as hex for better human readability in debug etc.
START=$( printf "0x%08X" $CONFIG_START )
# prepare the cmd line, --device VID:PID, --alt (@Internal Flash)
DFU_UTIL="dfu-util --device 0483:df11 --alt 0 --dfuse-address $START"
if [ "$CMD" = "SAVE" ]; then # read config block from device
if [ "$#" -lt 2 ]; then # no filename given, create unique name
NAME="${DEVICE}_config_$(date +%Y%m%d_%H%M%S).bin"
else # use 1st argument as file name
NAME="$2"
fi
# if the device is in UART mode then switch to DFU mode
if [ -c "$SERIAL_DEVICE" ]; then
printf "\rreset dfu\r" > "$SERIAL_DEVICE"
sleep 2
fi
EXECUTE="$DFU_UTIL --upload $NAME"
echo $EXECUTE
$EXECUTE
# is this the correct config content (start with magic "RNOC" or "UNOC")
# get hex string of 1st 4 bytes
XXXX=$(od --traditional -N4 -An -tx4 "$NAME" +$SLOT0 | tr A-F a-f | tr -d " ")
if [ $XXXX != $MAGIC ]; then
echo "\n$NAME: no correct config file"
exit
fi
elif [ "$CMD" = "RESTORE" ]; then
if [ "$#" -lt 2 ]; then # no filename given, exit
echo "usage: $CMD_NAME RESTORE <CONFIG_FILE>"
exit
fi
# is this regular file readable?
if [ ! -f "$2" -o ! -r "$2" ]; then
echo "$CMD_NAME: cannot read $2"
exit
fi
# does it have the correct size?
FILESIZE=$(wc -c "$2" | cut -d" " -f1)
if [ "$FILESIZE" -ne "$CONFIG_SIZE" ]; then
echo "$CMD_NAME: wrong config file size ${FILESIZE}, expected ${CONFIG_SIZE}"
exit
fi
# is this the correct config content (start with magic "RNOC" or "UNOC")
# get hex string of 1st 4 bytes
XXXX=$(od --traditional -N4 -An -tx4 "$2" +$SLOT0 | tr A-F a-f | tr -d " ")
if [ $XXXX != $MAGIC ]; then
echo "$CMD_NAME: no correct config file"
exit
fi
# if the device is in UART mode then switch to DFU mode
if [ -c "$SERIAL_DEVICE" ]; then
printf "\rreset dfu\r" > "$SERIAL_DEVICE"
sleep 2
fi
EXECUTE="$DFU_UTIL --download $2"
#echo $EXECUTE
$EXECUTE
else
echo usage:
echo $CMD_NAME SAVE [CONFIG_FILE]
echo or
echo $CMD_NAME RESTORE CONFIG_FILE
exit
fi