aboutsummaryrefslogtreecommitdiffstats
path: root/avr-test/avr-fusedump.bash
blob: d6c6e50b30f4f413c712751593310ff8384f6687 (plain)
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
#!/bin/bash
#############################################################################
#Bash script to dump the fuse bits for AVR microcontrollers and open 
#the Engbedded Atmel AVR Fuse Calculator with the current fuse settings.
#
#05-Nov-2009
#Craig Heffner
#http://www.hackingwithgum.com
#############################################################################

LFUSE=lfuse
HFUSE=hfuse
EFUSE=efuse
URL="http://www.engbedded.com/cgi-bin/fcx.cgi?P=MMCU&V_LOW=$LFUSE&V_HIGH=$HFUSE&V_EXTENDED=$EFUSE&O_HEX=Apply+values"
ADSUCCESS=OK
ADOK=0
FUSE=""
DATA=""
ADARGS=""

#Loop through all the arguments to the script and concatenate them into $ADARGS.
#These arguments get passed directly to avrdude.
while [ $# -ne 0 ]
do
	#If this argument is -p, then the next one is the chip name
	if [ "$1" == "-p" ] && [ "$2" != "" ]
	then
		URL=$(echo $URL | sed -e "s/MMCU/$2/")
	fi

        ADARGS="$ADARGS $1"
        shift
done

#Does the user need usage help?
if [ "$ADARGS" == "" ] || [ "$ADARGS" == "-h" ] || [ "$ADARGS" == "--help" ]
then
	echo ""
	echo "Usage: $0 -c <programmer> -p <partno>"
	echo ""
	echo -e "\tExample: $0 -c usbtiny -p ATmega8"
	echo ""
	echo -e "Note that the argument supplied to -p is case-sensitive.\nIt should look exactly as it does in the Engbedded Web site's select box.\n"
	exit
fi

#Dump fuse info with avrdude and loop through the output word by word
for WORD in $(avrdude $ADARGS -q -U lfuse:r:-:i -U hfuse:r:-:i -U efuse:r:-:i 2>&1)
do
	#Check for the success indicator
	if [ "$WORD" == "$ADSUCCESS" ]
	then
		ADOK=1
	fi

	#Look for keywords "lfuse", "hfuse", or "efuse". They appear 
	#just before the corresponding fuse values are displayed.
	if [ $(echo $WORD | grep $LFUSE) ]
	then
		FUSE=$LFUSE
	elif [ $(echo $WORD | grep $HFUSE) ]
	then
		FUSE=$HFUSE
	elif [ $(echo $WORD | grep $EFUSE) ]
	then
		FUSE=$EFUSE
	fi

	#Have we seen one of the fuse keywords?
	if [ "$FUSE" != "" ]
	then
		#Displayed fuse values start with a colon
		DATA=$(echo $WORD | grep -e "^:")

		if [ "$DATA" != "" ]
		then
			#Pull out the fuse byte; 9th and 10th characters in the displayed value
			DATA=${DATA:9:2}
			
			#Display the fuse value and update the Engbedded URL with the value
			echo -e "$FUSE\t0x$DATA"
			URL=$(echo $URL | sed -e "s/$FUSE/$DATA/")

			#Clear these in order to look for the next fuse
			FUSE=""
			DATA=""
		fi
	fi
done

#If there was an error, let the user know.
#Else, open the Engbedded URL.
if [ $ADOK -ne 1 ]
then
	echo "Avrdude failed! Check your programmer and chip arguments."
else
	#Show the URL
	#echo $URL

	#Open the URL automatically
	firefox $URL &
fi