2009-03-27 21:00:10 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2009-05-08 21:10:21 +02:00
|
|
|
use Device::SerialPort;
|
2009-03-27 21:00:10 +01:00
|
|
|
use Term::ReadKey;
|
|
|
|
use Getopt::Long;
|
2009-05-08 21:10:21 +02:00
|
|
|
use Time::HiRes qw(usleep);
|
2009-03-27 21:00:10 +01:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $filename = '';
|
2009-05-08 18:32:14 +02:00
|
|
|
my $second = '';
|
2009-03-27 21:00:10 +01:00
|
|
|
my $term = '/dev/ttyUSB0';
|
|
|
|
my $baud = '115200';
|
|
|
|
my $verbose;
|
|
|
|
|
2009-05-08 18:32:14 +02:00
|
|
|
GetOptions ('file=s' => \$filename,
|
|
|
|
'secondfile=s' => \$second,
|
2009-04-14 00:11:48 +02:00
|
|
|
'terminal=s' => \$term,
|
2009-03-27 21:00:10 +01:00
|
|
|
'verbose' => \$verbose,
|
2009-05-09 00:06:06 +02:00
|
|
|
'baud=s' => \$baud,
|
|
|
|
);
|
2009-03-27 21:00:10 +01:00
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
if($filename eq '') {
|
|
|
|
print "Example usage: mc1322x-load.pl -f foo.bin -t /dev/ttyS0 -b 9600\n";
|
2009-05-09 00:06:06 +02:00
|
|
|
print " or : mc1322x-load.pl -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n";
|
2009-03-27 21:00:10 +01:00
|
|
|
print " -f required: binary file to load\n";
|
2009-05-08 18:32:14 +02:00
|
|
|
print " -s optional: secondary binary file to send\n";
|
2009-03-27 21:00:10 +01:00
|
|
|
print " -t default: /dev/ttyUSB0\n";
|
|
|
|
print " -b default: 115200\n";
|
2009-05-09 00:06:06 +02:00
|
|
|
print " anything on the command line is sent serial device\n";
|
|
|
|
print " after all of the files have been sent\n";
|
2009-03-27 21:00:10 +01:00
|
|
|
exit;
|
|
|
|
}
|
2009-05-09 00:06:06 +02:00
|
|
|
|
2009-03-27 21:00:10 +01:00
|
|
|
my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n";
|
|
|
|
# next test will die at runtime unless $ob
|
|
|
|
|
2009-05-08 21:28:34 +02:00
|
|
|
if ($filename eq '') { die "you must specify a file with -f\n"; }
|
2009-03-27 21:00:10 +01:00
|
|
|
|
|
|
|
$ob->baudrate($baud);
|
|
|
|
$ob->parity('none');
|
|
|
|
$ob->databits(8);
|
|
|
|
$ob->stopbits(1);
|
|
|
|
$ob->handshake("rts");
|
2009-05-08 21:10:21 +02:00
|
|
|
$ob->read_const_time(1000); # 1 second per unfulfilled "read" call
|
2009-03-27 21:00:10 +01:00
|
|
|
|
2009-05-08 18:32:14 +02:00
|
|
|
my $s = 0;
|
2009-03-27 21:00:10 +01:00
|
|
|
|
2009-05-08 23:29:01 +02:00
|
|
|
while(1) {
|
|
|
|
|
|
|
|
my $c; my $count; my $ret = ''; my $test='';
|
|
|
|
|
2009-05-09 00:06:06 +02:00
|
|
|
if($s == 1) { print "secondary send...\n"; }
|
2009-05-08 23:29:01 +02:00
|
|
|
|
|
|
|
$ob->write(pack('C','0'));
|
|
|
|
|
|
|
|
if($s == 1) {
|
|
|
|
$test = 'ready';
|
|
|
|
} else {
|
|
|
|
$test = 'CONNECT';
|
|
|
|
}
|
|
|
|
|
2009-05-09 19:22:14 +02:00
|
|
|
until($ret =~ /$test$/) {
|
2009-05-08 23:29:01 +02:00
|
|
|
($count,$c) = $ob->read(1);
|
|
|
|
if ($count == 0) {
|
|
|
|
print '.';
|
|
|
|
$ob->write(pack('C','0'));
|
|
|
|
next;
|
2009-05-08 18:32:14 +02:00
|
|
|
}
|
2009-05-08 23:29:01 +02:00
|
|
|
$ret .= $c;
|
|
|
|
}
|
|
|
|
print $ret . "\n";
|
|
|
|
|
|
|
|
if (-e $filename) {
|
2009-05-08 18:32:14 +02:00
|
|
|
|
2009-05-08 23:29:01 +02:00
|
|
|
my $size = -s $filename;
|
2009-05-08 18:32:14 +02:00
|
|
|
|
2009-05-08 23:29:01 +02:00
|
|
|
print ("Size: $size bytes\n");
|
|
|
|
$ob->write(pack('V',$size));
|
|
|
|
|
|
|
|
open(FILE, $filename) or die($!);
|
|
|
|
print "Sending $filename\n";
|
2009-05-08 18:32:14 +02:00
|
|
|
|
2009-05-08 23:29:01 +02:00
|
|
|
my $i = 1;
|
|
|
|
while(read(FILE, $c, 1)) {
|
|
|
|
$i++;
|
|
|
|
usleep(50); # this is as fast is it can go...
|
2009-05-08 23:41:24 +02:00
|
|
|
usleep(25) if ($s==1);
|
2009-05-08 23:29:01 +02:00
|
|
|
$ob->write($c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last if ($s==1);
|
|
|
|
if((-e $second)) {
|
|
|
|
$s=1; $filename = $second;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-03-27 21:00:10 +01:00
|
|
|
|
2009-05-09 00:06:06 +02:00
|
|
|
print "done sending files.\n";
|
|
|
|
|
|
|
|
print "sending " ;
|
|
|
|
print @ARGV;
|
2009-05-09 20:53:27 +02:00
|
|
|
print ",\n";
|
2009-05-09 00:06:06 +02:00
|
|
|
|
|
|
|
$ob->write(@ARGV);
|
2009-05-09 20:42:07 +02:00
|
|
|
$ob->write(',');
|
2009-03-27 21:00:10 +01:00
|
|
|
|
2009-05-09 21:43:22 +02:00
|
|
|
my $c; my $count;
|
2009-05-08 21:10:21 +02:00
|
|
|
while(1) {
|
2009-05-09 21:43:22 +02:00
|
|
|
($count, $c) = $ob->read(1);
|
|
|
|
print $c if ($count != 0);
|
2009-05-08 21:10:21 +02:00
|
|
|
}
|
|
|
|
|
2009-03-27 21:00:10 +01:00
|
|
|
$ob -> close or die "Close failed: $!\n";
|
|
|
|
ReadMode 0;
|
|
|
|
undef $ob; # closes port AND frees memory in perl
|
|
|
|
exit;
|
|
|
|
|