-"""
-# write something to the begin of the IRAM
-data_wr = []
-checksum = 0
-for i in range(0, 0x400):
- value = i%256
- data_wr.append(value)
- checksum = (checksum + value) % (2**16)
-
-print "Calculated checksum:", checksum
-print "Writing", data_wr, "to the IRAM..."
-cmdWRITE(0x00030000, len(data_wr), data_wr)
-print "Received Checksum:", last_checksum
-print
-
-print "Reading from the IRAM again..."
-data_re = cmdREAD(0x00030000, len(data_wr))
-print "Received data:", data_re, "Checksum:", last_checksum
-"""
+def readmhxfile(filename): # desired mhx filename
+ """
+ proceeds a MHX-File
+ """
+ filep = open(filename, "r")
+ retval = [] # returns a list of FlashSequence objects
+ linecount = 0
+ for line in filep:
+ linecount += 1
+ # get rid of newline characters
+ line = line.strip()
+
+ # we're only interested in S2 (data sequence with 3 address bytes)
+ # records by now
+ if line[0:2] == "S2":
+ byte_count = int(line[2:4], 16)
+ # just to get sure, check if byte count field is valid
+ if (len(line)-4) != (byte_count*2):
+ print sys.argv[0] + ": Warning - inavlid byte count field in " + \
+ sys.argv[1] + ":" + str(linecount) + ", skipping line!"
+ continue
+
+ # address and checksum bytes are not needed
+ byte_count -= 4
+ address = int(line[4:10], 16)
+ datastr = line[10:10+byte_count*2]
+
+ # convert data hex-byte-string to real byte data list
+ data = []
+ for i in range(0, len(datastr)/2):
+ data.append(int(datastr[2*i:2*i+2], 16))
+
+ # add flash sequence to our list
+ retval.append(FlashSequence(address, data))
+ filep.close()
+ return retval
+
+def usage(execf):
+ """
+ print usage of frprog
+ """
+ print "Usage: " + execf + " <target mhx-file> [-d DEVICE]"
+
+def main(argv=None):
+ """
+ main function of frprog
+ """
+ # 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 _:
+ 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
+
+ # save time at this point for evaluating the duration at the end
+ starttime = time.time()
+
+ print "OK, trying to set baudrate..."
+ # set baudrate
+ try:
+ bootrombaudrate(BOOTLOADER_BAUDRATE)
+ except SerialPortException:
+ print "timeout exception: try again ->"
+ bootrombaudrate(BOOTLOADER_BAUDRATE)
+ # just to get sure that the bootloader is really running in new baudrate mode!
+ time.sleep(0.1)
+ del tty
+ tty = SerialPort(device, 100, BOOTLOADER_BAUDRATE)
+
+ sdots = SPLIT
+ 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()
+
+ sdots = sdots - 1
+ if sdots == 0:
+ sys.stdout.write(".")
+ sys.stdout.flush()
+ sdots = SPLIT
+ 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()
+
+ sdots = SPLIT
+ 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()