2009-03-27 20:00:10 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2009-05-08 19:10:21 +00:00
|
|
|
use Device::SerialPort;
|
2009-03-27 20:00:10 +00:00
|
|
|
use Term::ReadKey;
|
|
|
|
use Getopt::Long;
|
2009-05-08 19:10:21 +00:00
|
|
|
use Time::HiRes qw(usleep);
|
2009-03-27 20:00:10 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $filename = '';
|
2009-05-08 16:32:14 +00:00
|
|
|
my $second = '';
|
2009-03-27 20:00:10 +00:00
|
|
|
my $term = '/dev/ttyUSB0';
|
|
|
|
my $baud = '115200';
|
|
|
|
my $verbose;
|
2010-03-03 13:10:35 +00:00
|
|
|
my $rts = 'rts';
|
2010-09-06 15:06:52 +00:00
|
|
|
my $command = '';
|
2010-11-20 16:48:29 +00:00
|
|
|
my $first_delay = 50;
|
|
|
|
my $second_delay = 100;
|
2010-09-06 15:06:52 +00:00
|
|
|
my $do_exit;
|
2010-09-06 16:05:34 +00:00
|
|
|
my $zerolen;
|
2009-03-27 20:00:10 +00:00
|
|
|
|
2009-05-08 16:32:14 +00:00
|
|
|
GetOptions ('file=s' => \$filename,
|
|
|
|
'secondfile=s' => \$second,
|
2010-09-06 16:05:34 +00:00
|
|
|
'zerolen' => \$zerolen,
|
2009-04-13 22:11:48 +00:00
|
|
|
'terminal=s' => \$term,
|
2009-03-27 20:00:10 +00:00
|
|
|
'verbose' => \$verbose,
|
2011-03-15 18:52:39 +00:00
|
|
|
'u|baud=s' => \$baud,
|
2010-03-03 13:10:35 +00:00
|
|
|
'rts=s' => \$rts,
|
2010-09-06 15:06:52 +00:00
|
|
|
'command=s' => \$command,
|
2010-11-20 16:48:29 +00:00
|
|
|
'a=s' => \$first_delay,
|
|
|
|
'b=s' => \$second_delay,
|
2010-09-06 15:06:52 +00:00
|
|
|
'exit' => \$do_exit,
|
2009-08-10 20:52:36 +00:00
|
|
|
) or die 'bad options';
|
2009-03-27 20:00:10 +00:00
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
if($filename eq '') {
|
|
|
|
print "Example usage: mc1322x-load.pl -f foo.bin -t /dev/ttyS0 -b 9600\n";
|
2009-05-08 22:06:06 +00:00
|
|
|
print " or : mc1322x-load.pl -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n";
|
2010-09-06 16:05:34 +00:00
|
|
|
print " or : mc1322x-load.pl -f flasher.bin -z 0x1e000,0x11223344,0x55667788\n";
|
2009-03-27 20:00:10 +00:00
|
|
|
print " -f required: binary file to load\n";
|
2009-05-08 16:32:14 +00:00
|
|
|
print " -s optional: secondary binary file to send\n";
|
2010-09-06 16:05:34 +00:00
|
|
|
print " -z optional: send a zero length file as secondary\n";
|
2011-03-15 18:52:39 +00:00
|
|
|
print " -t, terminal default: /dev/ttyUSB0\n";
|
|
|
|
print " -u, --baud baud rate default: 115200\n";
|
2010-03-14 22:08:13 +00:00
|
|
|
print " -r [none|rts] flow control default: rts\n";
|
2010-09-06 15:06:52 +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";
|
2010-11-20 16:48:29 +00:00
|
|
|
print " -a first intercharacter delay, passed to usleep\n";
|
|
|
|
print " -b second intercharacter delay, passed to usleep\n";
|
2010-09-06 15:06:52 +00:00
|
|
|
print "\n";
|
|
|
|
print "anything on the command line is sent\n";
|
|
|
|
print "after all of the files.\n\n";
|
2009-03-27 20:00:10 +00:00
|
|
|
exit;
|
|
|
|
}
|
2009-05-08 22:06:06 +00:00
|
|
|
|
2009-08-10 20:52:36 +00:00
|
|
|
if (!(-e $filename)) { die "file $filename not found\n"; }
|
|
|
|
if (($second ne '') && !(-e $second)) { die "secondary file $second not found\n"; }
|
|
|
|
|
2009-03-27 20:00:10 +00:00
|
|
|
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);
|
2010-03-03 13:10:35 +00:00
|
|
|
if($rts eq 'rts') {
|
|
|
|
$ob->handshake('rts');
|
|
|
|
} else {
|
|
|
|
$ob->handshake('none');
|
|
|
|
}
|
2009-05-08 19:10:21 +00:00
|
|
|
$ob->read_const_time(1000); # 1 second per unfulfilled "read" call
|
2009-11-02 15:06:29 +00:00
|
|
|
$ob->rts_active(1);
|
2009-03-27 20:00:10 +00:00
|
|
|
|
2009-05-08 16:32:14 +00:00
|
|
|
my $s = 0;
|
2010-09-06 15:06:52 +00:00
|
|
|
my $reset = 0;
|
2010-09-06 16:05:34 +00:00
|
|
|
my $size = 0;
|
2009-03-27 20:00:10 +00:00
|
|
|
|
2009-05-08 21:29:01 +00:00
|
|
|
while(1) {
|
|
|
|
|
|
|
|
my $c; my $count; my $ret = ''; my $test='';
|
|
|
|
|
2009-05-08 22:06:06 +00:00
|
|
|
if($s == 1) { print "secondary send...\n"; }
|
2009-05-08 21:29:01 +00:00
|
|
|
|
|
|
|
$ob->write(pack('C','0'));
|
2010-09-06 15:06:52 +00:00
|
|
|
|
|
|
|
if(($command ne '') &&
|
|
|
|
($reset eq 0)) {
|
|
|
|
$reset++;
|
|
|
|
system($command);
|
|
|
|
}
|
|
|
|
|
2009-05-08 21:29:01 +00:00
|
|
|
if($s == 1) {
|
|
|
|
$test = 'ready';
|
|
|
|
} else {
|
|
|
|
$test = 'CONNECT';
|
|
|
|
}
|
|
|
|
|
2009-05-09 17:22:14 +00:00
|
|
|
until($ret =~ /$test$/) {
|
2009-05-08 21:29:01 +00:00
|
|
|
($count,$c) = $ob->read(1);
|
|
|
|
if ($count == 0) {
|
|
|
|
print '.';
|
|
|
|
$ob->write(pack('C','0'));
|
|
|
|
next;
|
2009-05-08 16:32:14 +00:00
|
|
|
}
|
2009-05-08 21:29:01 +00:00
|
|
|
$ret .= $c;
|
|
|
|
}
|
|
|
|
print $ret . "\n";
|
|
|
|
|
2010-09-06 16:05:34 +00:00
|
|
|
if (-e $filename || (defined($zerolen) && ($s == 1))) {
|
2009-05-08 16:32:14 +00:00
|
|
|
|
2010-09-06 16:05:34 +00:00
|
|
|
if(defined($zerolen) && ($s == 1)) {
|
|
|
|
$size = 0;
|
|
|
|
} else {
|
|
|
|
$size = -s $filename;
|
|
|
|
}
|
2009-05-08 16:32:14 +00:00
|
|
|
|
2009-05-08 21:29:01 +00:00
|
|
|
print ("Size: $size bytes\n");
|
|
|
|
$ob->write(pack('V',$size));
|
|
|
|
|
2010-09-06 16:05:34 +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++;
|
2011-05-06 16:13:34 +00:00
|
|
|
usleep($first_delay) if ( $s == 0 ) && ($first_delay != 0);
|
|
|
|
usleep($second_delay) if ( $s == 1 ) && ($second_delay != 0);
|
2010-09-06 16:05:34 +00:00
|
|
|
$ob->write($c);
|
|
|
|
}
|
2009-05-08 21:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last if ($s==1);
|
2010-09-06 16:05:34 +00:00
|
|
|
if((-e $second) || defined($zerolen)) {
|
2009-05-08 21:29:01 +00:00
|
|
|
$s=1; $filename = $second;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-03-27 20:00:10 +00:00
|
|
|
|
2009-05-08 22:06:06 +00:00
|
|
|
print "done sending files.\n";
|
|
|
|
|
2009-05-09 20:39:42 +00:00
|
|
|
if(scalar(@ARGV)!=0) {
|
|
|
|
print "sending " ;
|
|
|
|
print @ARGV;
|
|
|
|
print ",\n";
|
2009-05-08 22:06:06 +00:00
|
|
|
|
2009-05-09 20:40:43 +00:00
|
|
|
$ob->write(@ARGV);
|
|
|
|
$ob->write(',');
|
|
|
|
}
|
2009-03-27 20:00:10 +00:00
|
|
|
|
2010-09-06 15:06:52 +00:00
|
|
|
if(defined($do_exit)) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2009-05-09 19:43:22 +00:00
|
|
|
my $c; my $count;
|
2009-05-08 19:10:21 +00:00
|
|
|
while(1) {
|
2009-05-09 19:43:22 +00:00
|
|
|
($count, $c) = $ob->read(1);
|
2009-05-18 21:57:21 +00:00
|
|
|
print $c if (defined($count) && ($count != 0));
|
2009-05-08 19:10:21 +00:00
|
|
|
}
|
|
|
|
|
2009-03-27 20:00:10 +00:00
|
|
|
$ob -> close or die "Close failed: $!\n";
|
|
|
|
ReadMode 0;
|
|
|
|
undef $ob; # closes port AND frees memory in perl
|
|
|
|
exit;
|
|
|
|
|