w00t :)
authorBernhard Urban <lewurm@gmx.net>
Mon, 14 Dec 2009 05:23:17 +0000 (06:23 +0100)
committerBernhard Urban <lewurm@gmx.net>
Mon, 14 Dec 2009 05:23:17 +0000 (06:23 +0100)
the trick: erase first *all* sectors and then flash it, not erase,
flash, erase, flash, ...

pkernel/kernel.py
pkernel/main.c

index 91bbcc41ab0baac967fb34a9d012309af4262b4b..54a9fd3f351ba07dd2915831ec341a90cdfc79b6 100755 (executable)
@@ -10,12 +10,12 @@ KERNEL_BAUDRATE=38400
 def recvByte():
        i = tty.read()
        while len(i)==0:
-               time.sleep(0.01)
+               time.sleep(0.03)
                i = tty.read()
        return ord(i)
 
 def sendByte(byte):
-       time.sleep(0.501) # just to get sure, wait 1ms
+       time.sleep(0.005) # just to get sure, wait 5ms
        tty.write(chr(byte))
        tty.flush()
 
@@ -29,6 +29,17 @@ def sendDWord(dword):
        sendByte((dword >> 16) & 0xFF)
        sendByte((dword >> 24) & 0xFF)
 
+def pkernERASE(address, size):
+       sendByte(0x12)
+       if (recvByte() != 0x11):
+               raise Exception
+       sendDWord(address)
+       sendWord(size)
+       if (recvByte() != 0x18):
+               raise Exception
+       print "Erasing done."
+
+
 def pkernWRITE(address, size, data):
        print "address:", hex(address), "size:", size
        # send WRITE command
@@ -38,6 +49,7 @@ def pkernWRITE(address, size, data):
        # tell desired address and size
        sendDWord(address)
        sendWord(size)
+
        if (recvByte() != 0x04):
                raise Exception
        print "Received Metadata."
@@ -50,10 +62,6 @@ def pkernWRITE(address, size, data):
                raise Exception
        print "Received Data."
 
-       if (recvByte() != 0x18):
-               raise Exception
-       print "Erasing done."
-
        if (recvByte() != 0x28):
                raise Exception
        print "Flashing done."
@@ -113,10 +121,14 @@ for line in fp:
 
 print "The following flash sequences have been read in:"
 for seq in flashseqs:
-       print hex(seq.address) + ":", seq.data
+       print hex(seq.address) + ":", [hex(x) for x in seq.data]
 
 
 # let the fun begin!
+for seq in flashseqs:
+       print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
+       pkernERASE(seq.address, len(seq.data))
+
 for seq in flashseqs:
        print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
        pkernWRITE(seq.address, len(seq.data), seq.data)
index 3676387a43694582dcf5e33e73b4d59a5a15f63e..452c3881c7d0040d8b66371be48cf2c1b3bd0313 100644 (file)
@@ -20,10 +20,10 @@ static unsigned char recvbyte(void)
 static unsigned short recvword(void)
 {
        static unsigned char b1, b2;
-       static unsigned int ret;
+       static unsigned short ret;
        b1 = recvbyte();
        b2 = recvbyte();
-       ret = (b2 << 8) | b1;
+       ret = (unsigned short)(b2 << 8) | b1;
        return ret;
 }
 
@@ -35,7 +35,7 @@ static unsigned int recvdword(void)
        b2 = recvbyte();
        b3 = recvbyte();
        b4 = recvbyte();
-       ret = (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
+       ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
        return ret;
 }
 
@@ -48,7 +48,6 @@ static void halt(void)
 
 static void panic(void)
 {
-       PDR14 = 0xff;
        PDR14 = 0x22;
        halt();
 }
@@ -56,7 +55,7 @@ static void panic(void)
 void main(void)
 {
        unsigned int address;
-       unsigned short i, size;
+       unsigned short i, size, next;
        unsigned char running = 1, data[BUFSIZE] = {0};
        
        PORTEN = 0x3; /* enable I/O Ports */
@@ -72,6 +71,24 @@ void main(void)
                cleardata();
                increaseled();
                switch(recvbyte()) {
+                       case 0x12: //erase
+                               Putch4(0x11);
+                               address = recvdword();
+                               increaseled();
+
+                               size = recvword();
+                               increaseled();
+
+                               PDR14 = 0xff;
+                               for(i=0; i<(size+4); i+=4) { /* erase */
+                                       if(FLASH_SectorErase(address + i) != 1) {
+                                               panic();
+                                       }
+                                       increaseled();
+                               }
+                               Putch4(0x18); //Erasing done.
+                               break;
+
                        case 0x13: //receive
                                Putch4(0x37);
                                increaseled();
@@ -91,17 +108,10 @@ void main(void)
                                Putch4(0x08); //Received Data.
 
                                PDR14 = 0xff;
-                               for(i=0; i<size; i+=4) { /* erase */
-                                       if(FLASH_SectorErase(address + i) != 1) {
-                                               panic();
-                                       }
-                                       increaseled();
-                               }
-
-                               Putch4(0x18); //Erasing done.
-                               for(i=0; i<size; i++) { /* flash the data */
+                               for(i=0; i<size; i+=2) { /* flash the data */
                                        increaseled();
-                                       if(FLASH_WriteHalfWord(address + (2*i), data[i]) != 1) {
+                                       next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
+                                       if(FLASH_WriteHalfWord(address + i, next) != 1) {
                                                panic();
                                        }
                                }