This repository was archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash.pl
More file actions
executable file
·136 lines (107 loc) · 3.68 KB
/
flash.pl
File metadata and controls
executable file
·136 lines (107 loc) · 3.68 KB
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
#!/usr/bin/perl
use Cwd 'abs_path';
use File::Basename;
##############
# Flash-Tool #
##############
sub explainUsageAndExit(@);
sub checkHex ($);
###############################################################################
if(int(@ARGV) < 1) {
explainUsageAndExit();
}
my $hexFile = $ARGV[0];
my $hmType = $ARGV[1];
my $hmID = $ARGV[2];
my $serialNr = $ARGV[3];
if ( !(-e "$hexFile") ) {
explainUsageAndExit("Can't access the given hexfile.");
}
##########################
# Set the default values #
##########################
my $defHmId1 = 'AB';
my $defHmId2 = 'CD';
my $defHmId3 = 'EF';
my $hmId1 = '';
my $hmId2 = '';
my $hmId3 = '';
my $defHmType1 = '12';
my $defHmType2 = '34';
my $defSerialNumber = 'HB0Default';
my $curPath = $dirname = dirname(abs_path($0));
my ($fileBasename, $filePath, $fileExt) = fileparse($hexFile, '\..*');
##########################
# convert hexfile to bin #
##########################
`$curPath/bin/hex2bin -c $hexFile`;
if (defined($hmType) && defined($hmID) && defined($serialNr)) {
###########################
# Check the valid HM-Type #
###########################
($hmType1,$hmType2) = split(/:/, $hmType);
if (!checkHex($hmType1) || !checkHex($hmType2)) {
explainUsageAndExit ("The entered Type $hmType is invalid. Format: XX:XX. Each X must be 0-9 or A-F.");
}
#########################
# Check the valid HM-ID #
#########################
($hmId1, $hmId2, $hmId3) = split(/:/, $hmID);
if (!checkHex($hmId1) || !checkHex($hmId2) || !checkHex($hmId3)) {
explainUsageAndExit ("The entered HM-ID $hmID is invalid. Format: XX:XX:XX. Each X must be 0-9 or A-F.");
} else {
$hmID = $hmId1 . $hmId2 . $hmId3;
}
#################################
# Check the valid serial number #
#################################
if ( !($serialNr =~ /^[0-9a-zA-Z]{10}$/) ) {;
explainUsageAndExit ("The serial number must contains 10 characters 0-9 or A-Z.");
}
##########################################################
# Write user defined HM-ID and serial number to bin-file #
##########################################################
my $sedParams = '"s/\(\x' . $defHmType1 . '\x' . $defHmType2 . '\)\(' . $defSerialNumber . '\)\(\x' .$defHmId1 . '\x' .$defHmId2 . '\x' .$defHmId3 . '\)/\x' . $hmType1. '\x' . $hmType2. '' . $serialNr . '\x' . $hmId1 . '\x' . $hmId2 . '\x' . $hmId3 . '/" ' . $curPath . '/' . $fileBasename . '.bin > ' . $curPath . '/' . $fileBasename . '.tmp';
`sed -b -e $sedParams`;
unlink "$curPath/$fileBasename.bin";
rename "$curPath/$fileBasename.tmp", "$fileBasename.bin";
}
else {
print 'Use default serialNumber hmId and hmType';
}
####################
# Write Bootloader #
####################
print "\nTest Bootloader connection\n";
$result = `$curPath/bin/avrdude -C$curPath/bin/avrdude.conf -p atmega328p -P gpio -c gpio 2>&1`;
print $result;
if (index($result, 'initialization failed') != -1) {
print "Failed to connect to bootloader!\n";
exit(1);
}
print "\nWrite Bootloader\n";
$result = `$curPath/bin/avrdude -C$curPath/bin/avrdude.conf -p atmega328p -P gpio -c gpio -e -Uflash:w:$fileBasename.bin:r -Ulock:w:0x2F:m`;
print $result;
##########################
# Write out param errors #
##########################
sub explainUsageAndExit(@) {
my ($txt) = @_;
print "\n";
if (defined($txt)) {
print "$txt\n"
}
print "Usage: flash.pl <hexfile> [<hmtype> <HM-ID> <Serial>]\n";
exit (0);
}
#######################################
# Check a variable if is a hex number #
#######################################
sub checkHex ($) {
my ($val) = @_;
my $retVal = 0;
if ($val =~ /^[0-9a-fA-F]{2}$/) {;
$retVal = 1;
}
return $retVal;
}