okay, here is a programmer (=kernel.py) which communicate to the pkernel
authorBernhard Urban <lewurm@gmx.net>
Mon, 14 Dec 2009 03:25:14 +0000 (04:25 +0100)
committerBernhard Urban <lewurm@gmx.net>
Mon, 14 Dec 2009 03:53:06 +0000 (04:53 +0100)
running on the board. there are some operation done to the flash,
however it isn't correct (nothing happens after a reset)

note: atm you have to program the pkernel with a windows programmer.
note: use simpleprg.mhx to test it. (on larger files it takes *very*
long, due to a built-in debug delay ;))

todo: after pkernel works correctly, we have to reduce the .mhx size.
when the mhx file is small enough we can use the bootrom hack to load it
directly into ram. after that it should be done :)

pkernel/SerialPort_linux.py [new symlink]
pkernel/kernel.py
pkernel/main.c

diff --git a/pkernel/SerialPort_linux.py b/pkernel/SerialPort_linux.py
new file mode 120000 (symlink)
index 0000000..88212d9
--- /dev/null
@@ -0,0 +1 @@
+../SerialPort_linux.py
\ No newline at end of file
index 20b3a36b78e1c2779032d0b29dcc64948c979589..91bbcc41ab0baac967fb34a9d012309af4262b4b 100755 (executable)
@@ -7,25 +7,69 @@ DEVICE="/dev/ttyUSB0"
 # baudrate used for communication with pkernel
 KERNEL_BAUDRATE=38400
 
+def recvByte():
+       i = tty.read()
+       while len(i)==0:
+               time.sleep(0.01)
+               i = tty.read()
+       return ord(i)
+
+def sendByte(byte):
+       time.sleep(0.501) # just to get sure, wait 1ms
+       tty.write(chr(byte))
+       tty.flush()
+
+def sendWord(word):
+       sendByte(word & 0xFF)
+       sendByte((word >> 8) & 0xFF)
+
+def sendDWord(dword):
+       sendByte(dword & 0xFF)
+       sendByte((dword >> 8) & 0xFF)
+       sendByte((dword >> 16) & 0xFF)
+       sendByte((dword >> 24) & 0xFF)
+
 def pkernWRITE(address, size, data):
+       print "address:", hex(address), "size:", size
        # send WRITE command
-       sendByte(0x01)
-       if (recvByte() != 0xF1):
-               raise Exception
-       sendByte(0x03)
-       if (recvByte() != 0x83):
+       sendByte(0x13)
+       if (recvByte() != 0x37):
                raise Exception
        # tell desired address and size
        sendDWord(address)
        sendWord(size)
+       if (recvByte() != 0x04):
+               raise Exception
+       print "Received Metadata."
+
        # write binary stream of data
        for i in range(0, size):
                sendByte(data[i])
-       # get checksum
-       recvChecksum()
+
+       if (recvByte() != 0x08):
+               raise Exception
+       print "Received Data."
+
+       if (recvByte() != 0x18):
+               raise Exception
+       print "Erasing done."
+
+       if (recvByte() != 0x28):
+               raise Exception
+       print "Flashing done."
+
+
+class FlashSequence(object):
+       def __init__(self, address, data):
+               self.address = address
+               self.data = data
+
+# list of all our address/data pairs to flash
+flashseqs = []
+
 
 print "Initializing serial port..."
-tty = SerialPort(DEVICE, 500, KERNEL_BAUDRATE)
+tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
 
 # check command line arguments
 if len(sys.argv) != 2:
@@ -77,4 +121,6 @@ for seq in flashseqs:
        print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
        pkernWRITE(seq.address, len(seq.data), seq.data)
 
+sendByte(0x99);
+
 print "Reset your board now to run code from Flash"
index ff5a4da31e8be6ff7fa8f9e1529b9ff106ab00c3..3676387a43694582dcf5e33e73b4d59a5a15f63e 100644 (file)
 
 #pragma section CODE=IRAM,attr=CODE
 
-void increaseled(void)
+#define BUFSIZE 0x20
+#define cleardata() memset(data,0,BUFSIZE)
+
+static void increaseled(void)
 {
        PDR14 = ~(((~PDR14)+1)%256);
        HWWD_CL = 0;
 }
 
+static unsigned char recvbyte(void)
+{
+       return Getch4();
+}
+
+static unsigned short recvword(void)
+{
+       static unsigned char b1, b2;
+       static unsigned int ret;
+       b1 = recvbyte();
+       b2 = recvbyte();
+       ret = (b2 << 8) | b1;
+       return ret;
+}
+
+static unsigned int recvdword(void)
+{
+       static unsigned char b1, b2, b3, b4;
+       static unsigned int ret;
+       b1 = recvbyte();
+       b2 = recvbyte();
+       b3 = recvbyte();
+       b4 = recvbyte();
+       ret = (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
+       return ret;
+}
+
+static void halt(void)
+{
+       while(1) {
+               HWWD_CL = 0;
+       }
+}
+
+static void panic(void)
+{
+       PDR14 = 0xff;
+       PDR14 = 0x22;
+       halt();
+}
+
 void main(void)
 {
-       unsigned int i, baseaddr;
-       unsigned int toflash[] = {0x9b00, 0x0d4e, 0xcff1, 0x1601, 0x9b05, 0x04c7, 0xc106, 0x1656, 0xe0fb}; //len = 9
+       unsigned int address;
+       unsigned short i, size;
+       unsigned char running = 1, data[BUFSIZE] = {0};
        
        PORTEN = 0x3; /* enable I/O Ports */
 
        /*Enable LEDs*/
        DDR14 = 0xFF;
-       PDR14 = 0x00;
-       increaseled();
+       PDR14 = 0xff;
 
        /*Initialize UART4*/
        InitUart4();
 
-#if 1
+       while(running) {
+               cleardata();
+               increaseled();
+               switch(recvbyte()) {
+                       case 0x13: //receive
+                               Putch4(0x37);
+                               increaseled();
+
+                               address = recvdword();
+                               increaseled();
+
+                               size = recvword();
+                               increaseled();
+
+                               Putch4(0x04); //Received Metadata.
+                               PDR14 = 0xff;
+                               for(i=0; i<size; i++) { /* get data */
+                                       increaseled();
+                                       data[i] = recvbyte();
+                               }
+                               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 */
+                                       increaseled();
+                                       if(FLASH_WriteHalfWord(address + (2*i), data[i]) != 1) {
+                                               panic();
+                                       }
+                               }
+                               Putch4(0x28); //Flashing done.
+                               break;
+
+                       case 0x99: /* exit */
+                               running = 0;
+                               break;
+                       default: break;
+               }
+       }
+
+       PDR14 = 0x55; //signal that we finished now!
+       halt();
+
+#if 0
        i = 0;
        baseaddr = 0xf4000;
        for (; i <0x10; i+=4) {
                (void) FLASH_SectorErase(baseaddr + i);
                increaseled();
        }
-#endif
 
        i = 0;
        baseaddr = 0xf4000;
@@ -40,14 +133,6 @@ void main(void)
                (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
        }
 
-       PDR14 = 0x55; //signal that we finished now!
-
-#if 1
-       while(1) {
-               HWWD_CL = 0;
-       }
-#else
-       //let restart it
 #endif
 }