added further exit function, so the board restarts after reset
[pyfrprog.git] / pkernel / kernel.py
index 91bbcc41ab0baac967fb34a9d012309af4262b4b..8bf955b377dd0ec822f430910f666829292d0684 100755 (executable)
@@ -9,13 +9,10 @@ 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
+       time.sleep(0.005) # just to get sure, wait 5ms
        tty.write(chr(byte))
        tty.flush()
 
@@ -29,6 +26,26 @@ def sendDWord(dword):
        sendByte((dword >> 16) & 0xFF)
        sendByte((dword >> 24) & 0xFF)
 
+def pkernCHIPERASE():
+       sendByte(0x15)
+       if (recvByte() != 0x45):
+               raise Exception
+       print "wait..."
+       if (recvByte() != 0x23):
+               raise Exception
+       print "Chip erasing done."
+
+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,22 +55,11 @@ def pkernWRITE(address, size, data):
        # 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])
 
-       if (recvByte() != 0x08):
-               raise Exception
-       print "Received Data."
-
-       if (recvByte() != 0x18):
-               raise Exception
-       print "Erasing done."
-
        if (recvByte() != 0x28):
                raise Exception
        print "Flashing done."
@@ -69,7 +75,7 @@ flashseqs = []
 
 
 print "Initializing serial port..."
-tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
+tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
 
 # check command line arguments
 if len(sys.argv) != 2:
@@ -113,14 +119,26 @@ 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))
+"""
+print "ChipErase..."
+pkernCHIPERASE()
+
+
 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);
-
+"""
+sendByte(0x99) #exit and wait
 print "Reset your board now to run code from Flash"
+"""
+
+sendByte(0x97) #exit and restart