2010-06-10 14:55:39 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
use Device::SerialPort;
|
|
|
|
use Term::ReadKey;
|
|
|
|
use Getopt::Long;
|
|
|
|
use Time::HiRes qw(usleep);
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $filename = '';
|
|
|
|
my $second = '';
|
|
|
|
my $term = '/dev/ttyUSB0';
|
|
|
|
my $baud = '115200';
|
|
|
|
my $verbose;
|
|
|
|
my $rts = 'rts';
|
2010-11-07 14:08:50 +00:00
|
|
|
my $command = '';
|
|
|
|
my $do_exit;
|
2010-11-07 14:11:50 +00:00
|
|
|
my $zerolen;
|
2010-06-10 14:55:39 +00:00
|
|
|
|
|
|
|
GetOptions ('file=s' => \$filename,
|
|
|
|
'secondfile=s' => \$second,
|
2010-11-07 14:11:50 +00:00
|
|
|
'zerolen' => \$zerolen,
|
2010-06-10 14:55:39 +00:00
|
|
|
'terminal=s' => \$term,
|
|
|
|
'verbose' => \$verbose,
|
|
|
|
'baud=s' => \$baud,
|
|
|
|
'rts=s' => \$rts,
|
2010-11-07 14:08:50 +00:00
|
|
|
'command=s' => \$command,
|
|
|
|
'exit' => \$do_exit,
|
2010-06-10 14:55:39 +00:00
|
|
|
) or die 'bad options';
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
if($filename eq '') {
|
|
|
|
print "Example usage: mc1322x-load.pl -f foo.bin -t /dev/ttyS0 -b 9600\n";
|
|
|
|
print " or : mc1322x-load.pl -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n";
|
2010-11-07 14:11:50 +00:00
|
|
|
print " or : mc1322x-load.pl -f flasher.bin -z 0x1e000,0x11223344,0x55667788\n";
|
2010-06-10 14:55:39 +00:00
|
|
|
print " -f required: binary file to load\n";
|
|
|
|
print " -s optional: secondary binary file to send\n";
|
2010-11-07 14:11:50 +00:00
|
|
|
print " -z optional: send a zero length file as secondary\n";
|
2010-11-07 14:08:50 +00:00
|
|
|
print " -t terminal default: /dev/ttyUSB0\n";
|
|
|
|
print " -b baud rate default: 115200\n";
|
2010-06-10 14:55:39 +00:00
|
|
|
print " -r [none|rts] flow control default: rts\n";
|
2010-11-07 14:08:50 +00:00
|
|
|
print " -c command to run for autoreset: \n";
|
|
|
|
print " e.g. -c 'bbmc -l redbee-econotag -i 0 reset'\n";
|
|
|
|
print " -e exit instead of dropping to terminal display\n";
|
|
|
|
print "\n";
|
|
|
|
print "anything on the command line is sent\n";
|
|
|
|
print "after all of the files.\n\n";
|
2010-06-10 14:55:39 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(-e $filename)) { die "file $filename not found\n"; }
|
|
|
|
if (($second ne '') && !(-e $second)) { die "secondary file $second not found\n"; }
|
|
|
|
|
|
|
|
my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n";
|
|
|
|
# next test will die at runtime unless $ob
|
|
|
|
|
|
|
|
$ob->baudrate($baud);
|
|
|
|
$ob->parity('none');
|
|
|
|
$ob->databits(8);
|
|
|
|
$ob->stopbits(1);
|
|
|
|
if($rts eq 'rts') {
|
|
|
|
$ob->handshake('rts');
|
|
|
|
} else {
|
|
|
|
$ob->handshake('none');
|
|
|
|
}
|
|
|
|
$ob->read_const_time(1000); # 1 second per unfulfilled "read" call
|
|
|
|
$ob->rts_active(1);
|
|
|
|
|
|
|
|
my $s = 0;
|
2010-11-07 14:08:50 +00:00
|
|
|
my $reset = 0;
|
2010-11-07 14:11:50 +00:00
|
|
|
my $size = 0;
|
2010-06-10 14:55:39 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
|
|
|
my $c; my $count; my $ret = ''; my $test='';
|
|
|
|
|
|
|
|
if($s == 1) { print "secondary send...\n"; }
|
|
|
|
|
|
|
|
$ob->write(pack('C','0'));
|
2010-11-07 14:08:50 +00:00
|
|
|
|
|
|
|
if(($command ne '') &&
|
|
|
|
($reset eq 0)) {
|
|
|
|
$reset++;
|
|
|
|
system($command);
|
|
|
|
}
|
|
|
|
|
2010-06-10 14:55:39 +00:00
|
|
|
if($s == 1) {
|
|
|
|
$test = 'ready';
|
|
|
|
} else {
|
|
|
|
$test = 'CONNECT';
|
|
|
|
}
|
|
|
|
|
|
|
|
until($ret =~ /$test$/) {
|
|
|
|
($count,$c) = $ob->read(1);
|
|
|
|
if ($count == 0) {
|
|
|
|
print '.';
|
|
|
|
$ob->write(pack('C','0'));
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$ret .= $c;
|
|
|
|
}
|
|
|
|
print $ret . "\n";
|
|
|
|
|
2010-11-07 14:11:50 +00:00
|
|
|
if (-e $filename || (defined($zerolen) && ($s == 1))) {
|
2010-06-10 14:55:39 +00:00
|
|
|
|
2010-11-07 14:11:50 +00:00
|
|
|
if(defined($zerolen) && ($s == 1)) {
|
|
|
|
$size = 0;
|
|
|
|
} else {
|
|
|
|
$size = -s $filename;
|
|
|
|
}
|
2010-06-10 14:55:39 +00:00
|
|
|
|
|
|
|
print ("Size: $size bytes\n");
|
|
|
|
$ob->write(pack('V',$size));
|
|
|
|
|
2010-11-07 14:11:50 +00:00
|
|
|
if(($s == 0) ||
|
|
|
|
((!defined($zerolen)) && ($s == 1))) {
|
|
|
|
open(FILE, $filename) or die($!);
|
|
|
|
print "Sending $filename\n";
|
|
|
|
|
|
|
|
my $i = 1;
|
|
|
|
while(read(FILE, $c, 1)) {
|
|
|
|
$i++;
|
|
|
|
usleep(50); # this is as fast is it can go...
|
|
|
|
usleep(50) if ($s==1);
|
|
|
|
$ob->write($c);
|
|
|
|
}
|
2010-06-10 14:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last if ($s==1);
|
2010-11-07 14:11:50 +00:00
|
|
|
if((-e $second) || defined($zerolen)) {
|
2010-06-10 14:55:39 +00:00
|
|
|
$s=1; $filename = $second;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
print "done sending files.\n";
|
|
|
|
|
|
|
|
if(scalar(@ARGV)!=0) {
|
|
|
|
print "sending " ;
|
|
|
|
print @ARGV;
|
|
|
|
print ",\n";
|
|
|
|
|
|
|
|
$ob->write(@ARGV);
|
|
|
|
$ob->write(',');
|
|
|
|
}
|
|
|
|
|
2010-11-07 14:08:50 +00:00
|
|
|
if(defined($do_exit)) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2010-06-10 14:55:39 +00:00
|
|
|
my $c; my $count;
|
|
|
|
while(1) {
|
|
|
|
($count, $c) = $ob->read(1);
|
|
|
|
print $c if (defined($count) && ($count != 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ob -> close or die "Close failed: $!\n";
|
|
|
|
ReadMode 0;
|
|
|
|
undef $ob; # closes port AND frees memory in perl
|
|
|
|
exit;
|
|
|
|
|