From c0b912dca2b21920a0e94f9afaf4e092d68536d3 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 12:32:14 -0400 Subject: [PATCH 01/17] flash and burn completely untested. --- Makefile | 1 + mc1322x-load.pl | 77 ++++++++++++++-------- tests/flasher.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 27 deletions(-) create mode 100644 tests/flasher.c diff --git a/Makefile b/Makefile index 6337154cb..7c3873052 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ ALL = $(TESTS:.c=.srec) $(TESTS:.c=.bin) $(TESTS:.c=.dis) all: src/start.o src/isr.o $(ALL) +tests/flasher.obj: src/maca.o src/nvm.o tests/nvm-read.obj: src/maca.o src/nvm.o tests/nvm-write.obj: src/maca.o src/nvm.o tests/rftest-rx.obj: src/maca.o src/nvm.o diff --git a/mc1322x-load.pl b/mc1322x-load.pl index b5b4a5391..2863a39b3 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -7,11 +7,13 @@ use Getopt::Long; use strict; my $filename = ''; +my $second = ''; my $term = '/dev/ttyUSB0'; my $baud = '115200'; my $verbose; -GetOptions ('file=s' => \$filename, +GetOptions ('file=s' => \$filename, + 'secondfile=s' => \$second, 'terminal=s' => \$term, 'verbose' => \$verbose, 'baud=s' => \$baud); @@ -20,7 +22,9 @@ $| = 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\n"; print " -f required: binary file to load\n"; + print " -s optional: secondary binary file to send\n"; print " -t default: /dev/ttyUSB0\n"; print " -b default: 115200\n"; exit; @@ -29,7 +33,7 @@ if($filename eq '') { my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; # next test will die at runtime unless $ob -$baud = 115200 if (!defined($baud)); +if(($filename eq '')) die "you must specify a file with -f\n"; $ob->baudrate($baud); $ob->parity('none'); @@ -37,36 +41,55 @@ $ob->databits(8); $ob->stopbits(1); $ob->handshake("rts"); -my $c; +my $s = 0; -$ob->write(pack('C','0')); + SEND: + do { + + my $c; -my $ret = ''; -until($ret eq 'CONNECT') { - $c = $ob->input; - $ret .= $c; -} -print $ret . "\n"; + if($s == 1) { print "performing secondary send\n"; } + + $ob->write(pack('C','0')); + + my $ret = ''; + my $test; + if($s == 1) { + $test = 'ready'; + } else { + $test = 'CONNECT'; + } -if (defined $filename) { + until($ret eq $test) { + $c = $ob->input; + $ret .= $c; + } + print $ret . "\n"; + + + if (defined $filename) { + + my $size = -s $filename; + + print ("Size: $size bytes\n"); + $ob->write(pack('V',$size)); + + open(FILE, $filename) or die($!); + print "Sending $filename\n"; + + my $i = 1; + while(read(FILE, $c, 1)) { + print unpack('H',$c) . unpack('h',$c) if $verbose; + print "\n" if ($verbose && ($i%4==0)); + $i++; + select undef, undef, undef, 0.001; + $ob->write($c); + } + } - my $size = -s $filename; - - print ("Size: $size bytes\n"); - $ob->write(pack('V',$size)); - - open(FILE, $filename) or die($!); - print "Sending $filename\n"; - - my $i = 1; - while(read(FILE, $c, 1)) { - print unpack('H',$c) . unpack('h',$c) if $verbose; - print "\n" if ($verbose && ($i%4==0)); - $i++; - select undef, undef, undef, 0.001; - $ob->write($c); - } + if(-e $second) {$s=1; $filename = $second; continue SEND; } + } print "done.\n"; diff --git a/tests/flasher.c b/tests/flasher.c new file mode 100644 index 000000000..1bd01ea77 --- /dev/null +++ b/tests/flasher.c @@ -0,0 +1,165 @@ +#define GPIO_FUNC_SEL0 0x80000018 /* GPIO 15 - 0; 2 bit blocks */ + +#define BASE_UART1 0x80005000 +#define UART1_CON 0x80005000 +#define UART1_STAT 0x80005004 +#define UART1_DATA 0x80005008 +#define UR1CON 0x8000500c +#define UT1CON 0x80005010 +#define UART1_CTS 0x80005014 +#define UART1_BR 0x80005018 + +#define GPIO_PAD_DIR0 0x80000000 +#define GPIO_DATA0 0x80000008 + +#include "embedded_types.h" +#include "nvm.h" +#include "maca.h" + +#define reg(x) (*(volatile uint32_t *)(x)) + +#define DELAY 400000 + +void putc(uint8_t c); +void puts(uint8_t *s); +void put_hex(uint8_t x); +void put_hex16(uint16_t x); +void put_hex32(uint32_t x); +uint8_t getc(); + +const uint8_t hex[16]={'0','1','2','3','4','5','6','7', + '8','9','a','b','c','d','e','f'}; + +#include "isr.h" + +#define NBYTES 8 +#define WRITE_ADDR 0x1e000 +//#define WRITE_ADDR 0x0 +#define WRITEVAL0 0x00000001 +#define WRITEVAL1 0x00000000 + +__attribute__ ((section ("startup"))) +void main(void) { + nvmType_t type=0; + nvmErr_t err; + uint8_t c; + uint32_t buf[NBYTES/4]; + uint32_t i; + uint32_t len=0; + + *(volatile uint32_t *)GPIO_PAD_DIR0 = 0x00000100; + + /* Restore UART regs. to default */ + /* in case there is still bootloader state leftover */ + + reg(UART1_CON) = 0x0000c800; /* mask interrupts, 16 bit sample --- helps explain the baud rate */ + + /* INC = 767; MOD = 9999 works: 115200 @ 24 MHz 16 bit sample */ + #define INC 767 + #define MOD 9999 + reg(UART1_BR) = INC<<16 | MOD; + + /* see Section 11.5.1.2 Alternate Modes */ + /* you must enable the peripheral first BEFORE setting the function in GPIO_FUNC_SEL */ + /* From the datasheet: "The peripheral function will control operation of the pad IF */ + /* THE PERIPHERAL IS ENABLED. */ + reg(UART1_CON) = 0x00000003; /* enable receive and transmit */ + reg(GPIO_FUNC_SEL0) = ( (0x01 << (14*2)) | (0x01 << (15*2)) ); /* set GPIO15-14 to UART (UART1 TX and RX)*/ + + vreg_init(); + +// puts("CRM status: 0x"); +// put_hex32(reg(0x80003018)); +// puts("\n\r"); + + puts("Detecting internal nvm\n\r"); + + err = nvm_detect(gNvmInternalInterface_c, &type); + + puts("nvm_detect returned: 0x"); + put_hex(err); + puts(" type is: 0x"); + put_hex32(type); + puts("\n\r"); + + + /* erase the flash */ + err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); + puts("nvm_erase returned: 0x"); + put_hex(err); + puts("\n\r"); + + /* say we are ready */ +ready: + len = 0; + puts("ready"); + + /* read the length */ + for(i=0; i<4; i++) { + c = getc(); + /* bail if the first byte of the length is zero */ + if((i==0) && (c==0)) goto ready; + len += (len<<(i*8)); + puts("len: "); + put_hex32(len); + puts("\n\r"); + } + + /* write the OKOK magic */ + buf[0] = 'O'; buf[1] = 'K'; buf[2] = 'O'; buf[3] = 'K'; + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); + + /* read a byte, write a byte */ + /* byte at a time will make this work as a contiki process better */ + /* for OTAP */ + for(i=0; i> 4]); + putc(hex[x & 15]); +} + +void put_hex16(uint16_t x) +{ + put_hex((x >> 8) & 0xFF); + put_hex((x) & 0xFF); +} + +void put_hex32(uint32_t x) +{ + put_hex((x >> 24) & 0xFF); + put_hex((x >> 16) & 0xFF); + put_hex((x >> 8) & 0xFF); + put_hex((x) & 0xFF); +} From 4730dc048d49d3ecaa9ff0f9174e3fdb56aa5c2e Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 15:28:34 -0400 Subject: [PATCH 02/17] fixed syntax errors. --- mc1322x-load.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 2863a39b3..f454736bd 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -33,7 +33,7 @@ if($filename eq '') { my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; # next test will die at runtime unless $ob -if(($filename eq '')) die "you must specify a file with -f\n"; +if ($filename eq '') { die "you must specify a file with -f\n"; } $ob->baudrate($baud); $ob->parity('none'); @@ -68,7 +68,7 @@ my $s = 0; print $ret . "\n"; - if (defined $filename) { + if (-e $filename) { my $size = -s $filename; @@ -88,9 +88,9 @@ my $s = 0; } } - if(-e $second) {$s=1; $filename = $second; continue SEND; } + if(-e $second) {$s=1; $filename = $second; next SEND; } -} +}; print "done.\n"; From b7b99c15f2c9a36683d7328d269e8d142d22e763 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 15:10:21 -0400 Subject: [PATCH 03/17] first try at dual stage loading. --- mc1322x-load.pl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index f454736bd..21bdd28cb 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -1,8 +1,9 @@ #!/usr/bin/perl -w -use Device::SerialPort 0.05; +use Device::SerialPort; use Term::ReadKey; use Getopt::Long; +use Time::HiRes qw(usleep); use strict; @@ -40,21 +41,25 @@ $ob->parity('none'); $ob->databits(8); $ob->stopbits(1); $ob->handshake("rts"); +$ob->read_const_time(1000); # 1 second per unfulfilled "read" call +<<<<<<< HEAD:mc1322x-load.pl my $s = 0; SEND: do { my $c; - + my $count; + my $ret = ''; + if($s == 1) { print "performing secondary send\n"; } $ob->write(pack('C','0')); my $ret = ''; my $test; - + if($s == 1) { $test = 'ready'; } else { @@ -62,11 +67,15 @@ my $s = 0; } until($ret eq $test) { - $c = $ob->input; + ($count,$c) = $ob->read(1); + if ($count == 0) { + print '.'; + $ob->write(pack('C','0')); + next; + } $ret .= $c; } - print $ret . "\n"; - + print $ret . "\n"; if (-e $filename) { @@ -81,9 +90,8 @@ my $s = 0; my $i = 1; while(read(FILE, $c, 1)) { print unpack('H',$c) . unpack('h',$c) if $verbose; - print "\n" if ($verbose && ($i%4==0)); $i++; - select undef, undef, undef, 0.001; + usleep(50); # this is as fast is it can go... $ob->write($c); } } @@ -94,6 +102,10 @@ my $s = 0; print "done.\n"; +while(1) { + print $ob->input; +} + $ob -> close or die "Close failed: $!\n"; ReadMode 0; undef $ob; # closes port AND frees memory in perl From 311b2f49c4fa83a08a9d47654bc92a645e4dac3b Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 15:33:05 -0400 Subject: [PATCH 04/17] finish merge of fast with repeat with second stage loading --- mc1322x-load.pl | 1 - 1 file changed, 1 deletion(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 21bdd28cb..76c852a66 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -43,7 +43,6 @@ $ob->stopbits(1); $ob->handshake("rts"); $ob->read_const_time(1000); # 1 second per unfulfilled "read" call -<<<<<<< HEAD:mc1322x-load.pl my $s = 0; SEND: From 3bea9ac2f0cdb4b6b34dc65937abcc83bf222cba Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 17:27:24 -0400 Subject: [PATCH 05/17] this version of flasher works. --- tests/flasher.c | 73 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/tests/flasher.c b/tests/flasher.c index 1bd01ea77..ec60b0ed3 100644 --- a/tests/flasher.c +++ b/tests/flasher.c @@ -26,6 +26,7 @@ void put_hex(uint8_t x); void put_hex16(uint16_t x); void put_hex32(uint32_t x); uint8_t getc(); +void flushrx(); const uint8_t hex[16]={'0','1','2','3','4','5','6','7', '8','9','a','b','c','d','e','f'}; @@ -42,10 +43,10 @@ __attribute__ ((section ("startup"))) void main(void) { nvmType_t type=0; nvmErr_t err; - uint8_t c; - uint32_t buf[NBYTES/4]; - uint32_t i; - uint32_t len=0; + volatile uint8_t c; + volatile uint32_t buf[NBYTES/4]; + volatile uint32_t i; + volatile uint32_t len=0; *(volatile uint32_t *)GPIO_PAD_DIR0 = 0x00000100; @@ -72,49 +73,69 @@ void main(void) { // put_hex32(reg(0x80003018)); // puts("\n\r"); - puts("Detecting internal nvm\n\r"); +// puts("Detecting internal nvm\n\r"); err = nvm_detect(gNvmInternalInterface_c, &type); +/* puts("nvm_detect returned: 0x"); put_hex(err); puts(" type is: 0x"); put_hex32(type); puts("\n\r"); - +*/ /* erase the flash */ - err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); +// err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); + err = nvm_erase(gNvmInternalInterface_c, 1, 0x4fffffff); + +/* puts("nvm_erase returned: 0x"); put_hex(err); puts("\n\r"); +*/ /* say we are ready */ -ready: len = 0; puts("ready"); + flushrx(); /* read the length */ for(i=0; i<4; i++) { c = getc(); /* bail if the first byte of the length is zero */ - if((i==0) && (c==0)) goto ready; - len += (len<<(i*8)); - puts("len: "); - put_hex32(len); - puts("\n\r"); + len += (c<<(i*8)); } +// puts("len: "); +// put_hex32(len); +// puts("\n\r"); + /* write the OKOK magic */ - buf[0] = 'O'; buf[1] = 'K'; buf[2] = 'O'; buf[3] = 'K'; - err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); + ((uint8_t *)buf)[0] = 'O'; ((uint8_t *)buf)[1] = 'K'; ((uint8_t *)buf)[2] = 'O'; ((uint8_t *)buf)[3] = 'K'; +// ((uint8_t *)buf)[3] = 'x'; +// err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); + err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)buf, 0, 4); +// puts("nvm_write returned: 0x"); +// put_hex(err); +// puts("\n\r"); + + /* write the length */ + err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)&len, 4, 4); /* read a byte, write a byte */ /* byte at a time will make this work as a contiki process better */ /* for OTAP */ for(i=0; i Date: Fri, 8 May 2009 17:29:01 -0400 Subject: [PATCH 06/17] this version of mc1322x can flash code with flasher. --- mc1322x-load.pl | 100 +++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 76c852a66..c17c005ad 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -45,59 +45,63 @@ $ob->read_const_time(1000); # 1 second per unfulfilled "read" call my $s = 0; - SEND: - do { - - my $c; - my $count; - my $ret = ''; - - if($s == 1) { print "performing secondary send\n"; } - - $ob->write(pack('C','0')); - - my $ret = ''; - my $test; - - if($s == 1) { - $test = 'ready'; - } else { - $test = 'CONNECT'; - } - - until($ret eq $test) { - ($count,$c) = $ob->read(1); - if ($count == 0) { - print '.'; - $ob->write(pack('C','0')); - next; - } - $ret .= $c; +while(1) { + + my $c; my $count; my $ret = ''; my $test=''; + + if($s == 1) { print "performing secondary send\n"; } + + $ob->write(pack('C','0')); + + if($s == 1) { + $test = 'ready'; + } else { + $test = 'CONNECT'; + } + + until($ret eq $test) { + ($count,$c) = $ob->read(1); + if ($count == 0) { + print '.'; + $ob->write(pack('C','0')); + next; } + $ret .= $c; print $ret . "\n"; + } + print $ret . "\n"; + + if (-e $filename) { - if (-e $filename) { - - my $size = -s $filename; - - print ("Size: $size bytes\n"); - $ob->write(pack('V',$size)); - - open(FILE, $filename) or die($!); - print "Sending $filename\n"; - - my $i = 1; - while(read(FILE, $c, 1)) { - print unpack('H',$c) . unpack('h',$c) if $verbose; - $i++; - usleep(50); # this is as fast is it can go... - $ob->write($c); - } - } + my $size = -s $filename; - if(-e $second) {$s=1; $filename = $second; next SEND; } + print ("Size: $size bytes\n"); + $ob->write(pack('V',$size)); + + 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(500) if ($s==1); + $ob->write($c); +# if($s==1) { +# ($count, $c) = $ob->read(1); +# print $c; +# } + } + } + + last if ($s==1); + if((-e $second)) { + $s=1; $filename = $second; + } else { + last; + } + +} print "done.\n"; From 6b77fe8c3726fa9a138bce77ace5d2e4c0047972 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 17:41:24 -0400 Subject: [PATCH 07/17] flashing needs a little more time. --- mc1322x-load.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index c17c005ad..26c1a1012 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -85,7 +85,7 @@ while(1) { while(read(FILE, $c, 1)) { $i++; usleep(50); # this is as fast is it can go... - usleep(500) if ($s==1); + usleep(25) if ($s==1); $ob->write($c); # if($s==1) { # ($count, $c) = $ob->read(1); From cd973de3350906dc170df6b1e61ef23dca38238c Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 18:04:55 -0400 Subject: [PATCH 08/17] catch the aux. send. still need to parse it for something like: 0x1e000,0x11223344,0x55667788 --- tests/flasher.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/flasher.c b/tests/flasher.c index ec60b0ed3..da974608e 100644 --- a/tests/flasher.c +++ b/tests/flasher.c @@ -140,6 +140,8 @@ void main(void) { puts("flasher done\n\r"); + while(c=getc()) {putc(c);} + while(1) {continue;}; } From 002d63056baf143bf118b8f5432fb92e5aef281b Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Fri, 8 May 2009 18:06:06 -0400 Subject: [PATCH 09/17] send aux data on the command line after writing the files. --- mc1322x-load.pl | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 26c1a1012..666801a68 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -17,19 +17,24 @@ GetOptions ('file=s' => \$filename, 'secondfile=s' => \$second, 'terminal=s' => \$term, 'verbose' => \$verbose, - 'baud=s' => \$baud); + 'baud=s' => \$baud, + ); $| = 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\n"; + print " or : mc1322x-load.pl -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n"; print " -f required: binary file to load\n"; print " -s optional: secondary binary file to send\n"; print " -t default: /dev/ttyUSB0\n"; print " -b default: 115200\n"; + print " anything on the command line is sent serial device\n"; + print " after all of the files have been sent\n"; exit; } + +print @ARGV; my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; # next test will die at runtime unless $ob @@ -49,7 +54,7 @@ while(1) { my $c; my $count; my $ret = ''; my $test=''; - if($s == 1) { print "performing secondary send\n"; } + if($s == 1) { print "secondary send...\n"; } $ob->write(pack('C','0')); @@ -67,7 +72,6 @@ while(1) { next; } $ret .= $c; - print $ret . "\n"; } print $ret . "\n"; @@ -87,10 +91,6 @@ while(1) { usleep(50); # this is as fast is it can go... usleep(25) if ($s==1); $ob->write($c); -# if($s==1) { -# ($count, $c) = $ob->read(1); -# print $c; -# } } } @@ -103,7 +103,13 @@ while(1) { } -print "done.\n"; +print "done sending files.\n"; + +print "sending " ; +print @ARGV; +print "\n"; + +$ob->write(@ARGV); while(1) { print $ob->input; From 60582e33bf8f0bbbcc8c760b4aa796cd2470ec5b Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 13:22:14 -0400 Subject: [PATCH 10/17] changed the test condition to match the end of the captured string. this is better in case there is junk at the beginning or if the first one is incomplete. also removed the debug ARGV print --- mc1322x-load.pl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 666801a68..4034c2191 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -34,8 +34,6 @@ if($filename eq '') { exit; } -print @ARGV; - my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; # next test will die at runtime unless $ob @@ -64,7 +62,7 @@ while(1) { $test = 'CONNECT'; } - until($ret eq $test) { + until($ret =~ /$test$/) { ($count,$c) = $ob->read(1); if ($count == 0) { print '.'; From d60cc72ac411f528a89c0f6d383d0cf24d6a4937 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 14:42:07 -0400 Subject: [PATCH 11/17] terminate extra data string with a comma for flasher.c --- mc1322x-load.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 4034c2191..413d71f91 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -108,6 +108,7 @@ print @ARGV; print "\n"; $ob->write(@ARGV); +$ob->write(','); while(1) { print $ob->input; From a0db791175e7580f37ab89834994fa22665c83b1 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 14:43:17 -0400 Subject: [PATCH 12/17] add support for extra write data string. --- tests/flasher.c | 121 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 110 insertions(+), 11 deletions(-) diff --git a/tests/flasher.c b/tests/flasher.c index da974608e..475cde52b 100644 --- a/tests/flasher.c +++ b/tests/flasher.c @@ -20,24 +20,37 @@ #define DELAY 400000 +#define DEBUG 1 +#if DEBUG void putc(uint8_t c); void puts(uint8_t *s); void put_hex(uint8_t x); void put_hex16(uint16_t x); void put_hex32(uint32_t x); -uint8_t getc(); -void flushrx(); - const uint8_t hex[16]={'0','1','2','3','4','5','6','7', '8','9','a','b','c','d','e','f'}; +#else +#define putc(...) +#define puts(...) +#define put_hex(...) +#define put_hex16(...) +#define put_hex32(...) +#endif + +uint8_t getc(void); +void flushrx(void); +uint32_t to_u32(char *c); #include "isr.h" -#define NBYTES 8 -#define WRITE_ADDR 0x1e000 -//#define WRITE_ADDR 0x0 -#define WRITEVAL0 0x00000001 -#define WRITEVAL1 0x00000000 +#define NBYTES 16 + +enum parse_states { + SCAN_X, + READ_CHARS, + PROCESS, + MAX_STATE, +}; __attribute__ ((section ("startup"))) void main(void) { @@ -47,6 +60,8 @@ void main(void) { volatile uint32_t buf[NBYTES/4]; volatile uint32_t i; volatile uint32_t len=0; + volatile uint32_t state = SCAN_X; + volatile uint32_t addr,data; *(volatile uint32_t *)GPIO_PAD_DIR0 = 0x00000100; @@ -140,12 +155,55 @@ void main(void) { puts("flasher done\n\r"); - while(c=getc()) {putc(c);} + state = SCAN_X; addr=0; + while((c=getc())) { + putc(c); + if(state == SCAN_X) { + puts("scanx\n\r"); + /* read until we see an 'x' */ + if(c==0) { break; } + if(c!='x'){ continue; } + /* go to read_chars once we have an 'x' */ + state = READ_CHARS; + i = 0; + } + if(state == READ_CHARS) { + puts("readchars i "); + put_hex(i); + puts(" c "); + putc(c); + puts("\n\r"); + /* read all the chars up to a ',' */ + ((uint8_t *)buf)[i++] = c; + /* after reading a ',' */ + /* goto PROCESS state */ + if((c == ',') || (c == 0)) { state = PROCESS; } + } + if(state == PROCESS) { + puts("process\n\r"); + if(addr==0) { + /*interpret the string as the starting address */ + addr = to_u32((uint8_t *)buf); + } else { + /* string is data to write */ + data = to_u32((uint8_t *)buf); + puts("writing addr "); + put_hex32(addr); + puts(" data "); + put_hex32(data); + puts("\n\r"); + err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)&data, addr, 4); + addr += 4; + } + /* look for the next 'x' */ + state=SCAN_X; + } + } while(1) {continue;}; } -void flushrx() +void flushrx(void) { volatile uint8_t c; while(reg(UR1CON) !=0) { @@ -153,7 +211,48 @@ void flushrx() } } -uint8_t getc() +/* Convert from ASCII hex. Returns + the value, or 16 if it was space/newline, or + 32 if some other character. */ +uint8_t from_hex(uint8_t ch) +{ + if(ch==' ' || ch=='\r' || ch=='\n') + return 16; + + if(ch < '0') + goto bad; + if(ch <= '9') + return ch - '0'; + ch |= 0x20; + if(ch < 'a') + goto bad; + if(ch <= 'f') + return ch - 'a' + 10; +bad: + return 32; +} + +uint32_t to_u32(char *c) +{ + volatile uint32_t ret=0; + volatile uint32_t i,val; + + /* c should be /x\d+,/ */ + i=1; /* skip x */ + while(c[i] != ',') { + puts("to_u32 on "); + putc(c[i]); + puts(" with i "); + put_hex(i); + puts("\n\r"); + ret = ret<<4; + val = from_hex(c[i++]); + ret += val; + } + return ret; +} + +uint8_t getc(void) { volatile uint8_t c; while(reg(UR1CON) == 0); From c68af8819f245744c169dcbc93f953a851465b60 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 14:53:27 -0400 Subject: [PATCH 13/17] print appended , to the extra data --- mc1322x-load.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 413d71f91..8bd2b8b5b 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -105,7 +105,7 @@ print "done sending files.\n"; print "sending " ; print @ARGV; -print "\n"; +print ",\n"; $ob->write(@ARGV); $ob->write(','); From 2783ad26f0e0b2d020e465cfa42cf27f315d6ab3 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 14:55:48 -0400 Subject: [PATCH 14/17] clean up debug. --- tests/flasher.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/tests/flasher.c b/tests/flasher.c index 475cde52b..74ec3b147 100644 --- a/tests/flasher.c +++ b/tests/flasher.c @@ -22,20 +22,21 @@ #define DEBUG 1 #if DEBUG -void putc(uint8_t c); -void puts(uint8_t *s); -void put_hex(uint8_t x); -void put_hex16(uint16_t x); -void put_hex32(uint32_t x); +#define dbg_putc(...) putc(__VA_ARGS__) +#define dbg_puts(...) puts(__VA_ARGS__) +#define dbg_put_hex(...) put_hex(__VA_ARGS__) +#define dbg_put_hex16(...) put_hex16(__VA_ARGS__) +#define dbg_put_hex32(...) put_hex32(__VA_ARGS__) +#else +#define dbg_putc(...) +#define dbg_puts(...) +#define dbg_put_hex(...) +#define dbg_put_hex16(...) +#define dbg_put_hex32(...) +#endif + const uint8_t hex[16]={'0','1','2','3','4','5','6','7', '8','9','a','b','c','d','e','f'}; -#else -#define putc(...) -#define puts(...) -#define put_hex(...) -#define put_hex16(...) -#define put_hex32(...) -#endif uint8_t getc(void); void flushrx(void); @@ -157,9 +158,7 @@ void main(void) { state = SCAN_X; addr=0; while((c=getc())) { - putc(c); if(state == SCAN_X) { - puts("scanx\n\r"); /* read until we see an 'x' */ if(c==0) { break; } if(c!='x'){ continue; } @@ -168,11 +167,6 @@ void main(void) { i = 0; } if(state == READ_CHARS) { - puts("readchars i "); - put_hex(i); - puts(" c "); - putc(c); - puts("\n\r"); /* read all the chars up to a ',' */ ((uint8_t *)buf)[i++] = c; /* after reading a ',' */ @@ -180,7 +174,6 @@ void main(void) { if((c == ',') || (c == 0)) { state = PROCESS; } } if(state == PROCESS) { - puts("process\n\r"); if(addr==0) { /*interpret the string as the starting address */ addr = to_u32((uint8_t *)buf); @@ -240,11 +233,6 @@ uint32_t to_u32(char *c) /* c should be /x\d+,/ */ i=1; /* skip x */ while(c[i] != ',') { - puts("to_u32 on "); - putc(c[i]); - puts(" with i "); - put_hex(i); - puts("\n\r"); ret = ret<<4; val = from_hex(c[i++]); ret += val; @@ -261,6 +249,7 @@ uint8_t getc(void) return c; } + void putc(uint8_t c) { while(reg(UT1CON)==31); /* wait for there to be room in the buffer */ reg(UART1_DATA) = c; @@ -291,3 +280,4 @@ void put_hex32(uint32_t x) put_hex((x >> 8) & 0xFF); put_hex((x) & 0xFF); } + From 706da178076c7df78259bde6d0eff1e7548990c2 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 15:30:54 -0400 Subject: [PATCH 15/17] clean up and flash type workaround. --- tests/flasher.c | 90 ++++++++++++++++++++++++++++++------------------ tests/nvm-read.c | 4 +-- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/tests/flasher.c b/tests/flasher.c index 74ec3b147..931b335d6 100644 --- a/tests/flasher.c +++ b/tests/flasher.c @@ -20,6 +20,11 @@ #define DELAY 400000 +/* if both BOOT_OK and BOOT_SECURE are 0 then flash image will not be bootable */ +/* if both are 1 then flash image will be secure */ +#define BOOT_OK 1 +#define BOOT_SECURE 0 + #define DEBUG 1 #if DEBUG #define dbg_putc(...) putc(__VA_ARGS__) @@ -85,31 +90,26 @@ void main(void) { vreg_init(); -// puts("CRM status: 0x"); -// put_hex32(reg(0x80003018)); -// puts("\n\r"); - -// puts("Detecting internal nvm\n\r"); + dbg_puts("Detecting internal nvm\n\r"); err = nvm_detect(gNvmInternalInterface_c, &type); -/* - puts("nvm_detect returned: 0x"); - put_hex(err); - puts(" type is: 0x"); - put_hex32(type); - puts("\n\r"); -*/ + dbg_puts("nvm_detect returned: 0x"); + dbg_put_hex(err); + dbg_puts(" type is: 0x"); + dbg_put_hex32(type); + dbg_puts("\n\r"); /* erase the flash */ -// err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); - err = nvm_erase(gNvmInternalInterface_c, 1, 0x4fffffff); + err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); -/* - puts("nvm_erase returned: 0x"); - put_hex(err); - puts("\n\r"); -*/ + dbg_puts("nvm_erase returned: 0x"); + dbg_put_hex(err); + dbg_puts("\n\r"); + + dbg_puts(" type is: 0x"); + dbg_put_hex32(type); + dbg_puts("\n\r"); /* say we are ready */ len = 0; @@ -123,21 +123,32 @@ void main(void) { len += (c<<(i*8)); } -// puts("len: "); -// put_hex32(len); -// puts("\n\r"); + dbg_puts("len: "); + dbg_put_hex32(len); + dbg_puts("\n\r"); /* write the OKOK magic */ - ((uint8_t *)buf)[0] = 'O'; ((uint8_t *)buf)[1] = 'K'; ((uint8_t *)buf)[2] = 'O'; ((uint8_t *)buf)[3] = 'K'; -// ((uint8_t *)buf)[3] = 'x'; -// err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); - err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)buf, 0, 4); -// puts("nvm_write returned: 0x"); -// put_hex(err); -// puts("\n\r"); + +#if BOOT_OK + ((uint8_t *)buf)[0] = 'O'; ((uint8_t *)buf)[1] = 'K'; ((uint8_t *)buf)[2] = 'O'; ((uint8_t *)buf)[3] = 'K'; +#elif BOOT_SECURE + ((uint8_t *)buf)[0] = 'S'; ((uint8_t *)buf)[1] = 'E'; ((uint8_t *)buf)[2] = 'C'; ((uint8_t *)buf)[3] = 'U'; +#else + ((uint8_t *)buf)[0] = 'N'; ((uint8_t *)buf)[1] = 'O'; ((uint8_t *)buf)[2] = 'N'; ((uint8_t *)buf)[3] = 'O'; +#endif + + dbg_puts(" type is: 0x"); + dbg_put_hex32(type); + dbg_puts("\n\r"); + + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); + + dbg_puts("nvm_write returned: 0x"); + dbg_put_hex(err); + dbg_puts("\n\r"); /* write the length */ - err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)&len, 4, 4); + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)&len, 4, 4); /* read a byte, write a byte */ /* byte at a time will make this work as a contiki process better */ @@ -146,9 +157,22 @@ void main(void) { c = getc(); // put_hex(c); // puts(": "); -// err = nvm_write(gNvmInternalInterface_c, type, &c, 4+i, 1); - err = nvm_write(gNvmInternalInterface_c, 1, &c, 8+i, 1); -// if(err==0) { putc('.'); } else { putc('x'); } + + /*for some reason I have to hard code the type here for it to work reliably */ +// err = nvm_write(gNvmInternalInterface_c, type, &c, 4+i, 1); + switch(type) + { + case 1: + err = nvm_write(gNvmInternalInterface_c, 1, &c, 8+i, 1); + break; + case 2: + err = nvm_write(gNvmInternalInterface_c, 2, &c, 8+i, 1); + break; + case 3: + err = nvm_write(gNvmInternalInterface_c, 3, &c, 8+i, 1); + break; + } + // puts("nvm_write returned: 0x"); // put_hex(err); // puts("\n\r"); diff --git a/tests/nvm-read.c b/tests/nvm-read.c index 54a912904..44ed25a1e 100644 --- a/tests/nvm-read.c +++ b/tests/nvm-read.c @@ -31,7 +31,7 @@ const uint8_t hex[16]={'0','1','2','3','4','5','6','7', #include "isr.h" -#define NBYTES 1024 +#define NBYTES 16 __attribute__ ((section ("startup"))) void main(void) { nvmType_t type=0; @@ -76,7 +76,7 @@ void main(void) { nvm_setsvar(0); - err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, 0x1F000, NBYTES); + err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, 0x1e000, NBYTES); puts("nvm_read returned: 0x"); put_hex(err); puts("\n\r"); From 963d7a69de2d76cb0bf66132990723fbc7dfe23e Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 15:43:22 -0400 Subject: [PATCH 16/17] don't hose the cpu by using read instead of input --- mc1322x-load.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mc1322x-load.pl b/mc1322x-load.pl index 8bd2b8b5b..45c13b3a8 100755 --- a/mc1322x-load.pl +++ b/mc1322x-load.pl @@ -110,8 +110,10 @@ print ",\n"; $ob->write(@ARGV); $ob->write(','); +my $c; my $count; while(1) { - print $ob->input; + ($count, $c) = $ob->read(1); + print $c if ($count != 0); } $ob -> close or die "Close failed: $!\n"; From cb4dd0d23efbbaf3df00bde0b470a6890f88fd13 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sat, 9 May 2009 15:48:02 -0400 Subject: [PATCH 17/17] reverting this file back to dumping the calibration section. --- tests/nvm-read.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/nvm-read.c b/tests/nvm-read.c index 44ed25a1e..54a912904 100644 --- a/tests/nvm-read.c +++ b/tests/nvm-read.c @@ -31,7 +31,7 @@ const uint8_t hex[16]={'0','1','2','3','4','5','6','7', #include "isr.h" -#define NBYTES 16 +#define NBYTES 1024 __attribute__ ((section ("startup"))) void main(void) { nvmType_t type=0; @@ -76,7 +76,7 @@ void main(void) { nvm_setsvar(0); - err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, 0x1e000, NBYTES); + err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, 0x1F000, NBYTES); puts("nvm_read returned: 0x"); put_hex(err); puts("\n\r");