timeout fix
[pyfrprog.git] / frprog.py
index 03a441dedf9627e76cfb69698fdac765b8a528e8..ce102ee8af94f89f61fe3a9f429d525e9a0cd1d9 100755 (executable)
--- a/frprog.py
+++ b/frprog.py
@@ -2,8 +2,6 @@
 import sys, time
 from SerialPort_linux import *
 
-# serial device to communicate with
-DEVICE="/dev/ttyUSB0"
 # baudrate used for initialization
 INIT_BAUDRATE=9600
 # baudrate used for communication with the internal bootloader after init
@@ -14,6 +12,11 @@ KERNEL_BAUDRATE=115200
 # contains the last received checksum from a READ, WRITE or CHECKSUM command
 last_checksum = 0
 
+class FlashSequence(object):
+       def __init__(self, address, data):
+               self.address = address
+               self.data = data
+
 def sendByte(byte):
        tty.write(chr(byte))
 
@@ -35,7 +38,7 @@ def recvChecksum():
        last_checksum = recvByte()
        last_checksum |= (recvByte() << 8)
 
-def cmdREAD(address, size):
+def bootromREAD(address, size):
        # send READ command
        sendByte(0x01)
        if (recvByte() != 0xF1):
@@ -54,7 +57,7 @@ def cmdREAD(address, size):
        recvChecksum()
        return data
 
-def cmdWRITE(address, size, data):
+def bootromWRITE(address, size, data):
        # send WRITE command
        sendByte(0x01)
        if (recvByte() != 0xF1):
@@ -72,7 +75,7 @@ def cmdWRITE(address, size, data):
        recvChecksum()
 
 # TODO: test this function!
-def cmdCALL(address):
+def bootromCALL(address):
        # send CALL command
        sendByte(0x01)
        if (recvByte() != 0xF1):
@@ -86,7 +89,7 @@ def cmdCALL(address):
        #return recvByte()
 
 # TODO: test this function!
-def cmdCHECKSUM():
+def bootromCHECKSUM():
        # call CHECKSUM command
        sendByte(0x01)
        if (recvByte() != 0xF1):
@@ -97,7 +100,7 @@ def cmdCHECKSUM():
        # get checksum
        recvChecksum()
 
-def cmdBAUDRATE(baudrate):
+def bootromBAUDRATE(baudrate):
        # send BAUDRATE command
        sendByte(0x01)
        if (recvByte() != 0xF1):
@@ -125,7 +128,6 @@ def pkernERASE(address, size):
        if (recvByte() != 0x18):
                raise Exception
 
-
 def pkernWRITE(address, size, data):
        # send WRITE command
        sendByte(0x13)
@@ -142,12 +144,6 @@ def pkernWRITE(address, size, data):
        if (recvByte() != 0x28):
                raise Exception
 
-
-class FlashSequence(object):
-       def __init__(self, address, data):
-               self.address = address
-               self.data = data
-
 def readMHXFile(filename): # desired mhx filename
        fp = open(filename, "r")
        retval = [] # returns a list of FlashSequence objects
@@ -181,80 +177,115 @@ def readMHXFile(filename): # desired mhx filename
        fp.close()
        return retval
 
-
-# check command line arguments
-if len(sys.argv) != 3:
-       print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]"
-       sys.exit(1)
-
-# read in data from mhx-files before starting
-try:
-       bootloaderseqs = readMHXFile(sys.argv[1])
-       pkernelseqs = readMHXFile(sys.argv[2])
-except IOError as error:
-       print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
-       sys.exit(1)
-
-print "Initializing serial port..."
-tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
-
-print "Please press RESET on your board..."
-
-while True:
-       tty.write('V')
-       tty.flush()
-       try: 
-               if tty.read() == 'F':
-                       break
-       except SerialPortException: 
-               # timeout happened, who cares ;-)
-               pass
-
-print "OK, trying to set baudrate..."
-# set baudrate
-cmdBAUDRATE(BOOTLOADER_BAUDRATE)
-time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
-del tty
-tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
-
-print "Transfering pkernel program to IRAM",
-# let the fun begin!
-for seq in bootloaderseqs:
-       if(seq.address <= 0x40000):
-               addr = seq.address
-       else:
-               continue
-       #print "RAMing", len(seq.data), "bytes at address", hex(addr)
-       cmdWRITE(addr, len(seq.data), seq.data)
-       tty.flush()
-       sys.stdout.write(".")
-       sys.stdout.flush()
-print
-
-# execute our pkernel finally and set pkernel conform baudrate
-cmdCALL(0x30000)
-time.sleep(0.5) # just to get sure that the pkernel is really running!
-del tty
-pkernelmode = True
-tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
-
-print "Performing ChipErase..."
-pkernCHIPERASE()
-print "Chip erasing done."
-
-print "Flashing",
-for seq in pkernelseqs:
-       # skip seqs only consisting of 0xffs
-       seqset = list(set(seq.data))
-       if len(seqset) == 1 and seqset[0] == 0xff:
-               continue
-       #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
-       pkernWRITE(seq.address, len(seq.data), seq.data)
-       tty.flush()
-       sys.stdout.write(".")
-       sys.stdout.flush()
-print
-print "Flashing done."
-
-sendByte(0x97) # exit and restart
-print "Program was started. Have fun!"
+def usage(execfile):
+       print "Usage: " + execfile + " <target mhx-file> [-d DEVICE]"
+
+def main(argv=None):
+       # check command line arguments
+       if argv is None:
+               argv = sys.argv
+
+       if len(argv) == 2 and (argv[1] == "-v" or argv[1] == "--version"):
+               print "Version: %VERSION%"
+               return 0
+
+       if len(argv) != 2 and len(argv) != 4:
+               usage(argv[0])
+               return 1
+
+       # standard serial device to communicate with
+       DEVICE="/dev/ttyUSB0"
+
+       # overrule standard device if provided with -d
+       if len(argv) == 4:
+               if argv[2] == "-d":
+                       DEVICE = argv[3]
+               else:
+                       usage(argv[0])
+                       return 1
+
+       # read in data from mhx-files before starting
+       try:
+               try:
+                       bootloaderseqs = readMHXFile("pkernel/pkernel.mhx")
+               except IOError as error1:
+                       bootloaderseqs = readMHXFile("%PREFIX%/share/frprog/pkernel.mhx")
+               pkernelseqs = readMHXFile(argv[1])
+       except IOError as error:
+               print argv[0] + ": Error - couldn't open file " + error.filename + "!"
+               return 1
+
+       print "Initializing serial port..."
+       global tty
+       tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
+
+       print "Please press RESET on your board..."
+
+       while True:
+               tty.write('V')
+               tty.flush()
+               try:
+                       if tty.read() == 'F':
+                               break
+               except SerialPortException:
+                       # timeout happened, who cares ;-)
+                       pass
+
+       starttime = time.time() # save time at this point for evaluating the duration at the end
+
+       print "OK, trying to set baudrate..."
+       # set baudrate
+       try:
+               bootromBAUDRATE(BOOTLOADER_BAUDRATE)
+       except SerialPortException:
+               print "timeout exception: try again ->"
+               bootromBAUDRATE(BOOTLOADER_BAUDRATE)
+       time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
+       del tty
+       tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
+
+       print "Transfering pkernel program to IRAM",
+       # let the fun begin!
+       for seq in bootloaderseqs:
+               if(seq.address <= 0x40000):
+                       addr = seq.address
+               else:
+                       continue
+               #print "RAMing", len(seq.data), "bytes at address", hex(addr)
+               bootromWRITE(addr, len(seq.data), seq.data)
+               tty.flush()
+               sys.stdout.write(".")
+               sys.stdout.flush()
+       print
+
+       # execute our pkernel finally and set pkernel conform baudrate
+       bootromCALL(0x30000)
+       time.sleep(0.1) # just to get sure that the pkernel is really running!
+       del tty
+       tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
+
+       print "Performing ChipErase..."
+       pkernCHIPERASE()
+
+       print "Flashing",
+       for seq in pkernelseqs:
+               # skip seqs only consisting of 0xffs
+               seqset = list(set(seq.data))
+               if len(seqset) == 1 and seqset[0] == 0xff:
+                       continue
+               #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
+               pkernWRITE(seq.address, len(seq.data), seq.data)
+               tty.flush()
+               sys.stdout.write(".")
+               sys.stdout.flush()
+       print
+
+       duration = time.time() - starttime
+       print "Procedure complete, took", round(duration, 2), "seconds."
+
+       sendByte(0x97) # exit and restart
+       print "Program was started. Have fun!"
+
+
+if __name__ == '__main__':
+       sys.exit(main())