X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=frprog.py;h=03a441dedf9627e76cfb69698fdac765b8a528e8;hb=7b64a4f9de0211a3d903dd4ed6ad2bb9b6186563;hp=66e6d477e30cd49120b5839fa0080cf6b4b343ac;hpb=562594c85205ccdd8f9eaa1c76e57b4d7dce39b3;p=pyfrprog.git diff --git a/frprog.py b/frprog.py index 66e6d47..03a441d 100755 --- a/frprog.py +++ b/frprog.py @@ -6,16 +6,16 @@ from SerialPort_linux import * DEVICE="/dev/ttyUSB0" # baudrate used for initialization INIT_BAUDRATE=9600 -# baudrate used for communication after init -REAL_BAUDRATE=38400 +# baudrate used for communication with the internal bootloader after init +BOOTLOADER_BAUDRATE=38400 +# baudrate used for communication with the pkernel program that does the flashing eventually +KERNEL_BAUDRATE=115200 # contains the last received checksum from a READ, WRITE or CHECKSUM command last_checksum = 0 def sendByte(byte): - time.sleep(0.001) # just to get sure, wait 1ms tty.write(chr(byte)) - tty.flush() def sendWord(word): sendByte(word & 0xFF) @@ -98,8 +98,6 @@ def cmdCHECKSUM(): recvChecksum() def cmdBAUDRATE(baudrate): - global last_checksum - # send BAUDRATE command sendByte(0x01) if (recvByte() != 0xF1): @@ -108,17 +106,101 @@ def cmdBAUDRATE(baudrate): if (recvByte() != 0x86): raise Exception # send desired baudrate - sendByte(baudrate & 0xFF) - sendByte((baudrate >> 8) & 0xFF) - sendByte((baudrate >> 16) & 0xFF) - sendByte((baudrate >> 24) & 0xFF) + sendDWord(baudrate) + +def pkernCHIPERASE(): + sendByte(0x15) + if (recvByte() != 0x45): + raise Exception + # wait till completion... + if (recvByte() != 0x23): + raise Exception + +def pkernERASE(address, size): + sendByte(0x12) + if (recvByte() != 0x11): + raise Exception + sendDWord(address) + sendWord(size) + if (recvByte() != 0x18): + raise Exception + + +def pkernWRITE(address, size, data): + # send WRITE command + sendByte(0x13) + if (recvByte() != 0x37): + raise Exception + # tell desired address and size + sendDWord(address) + sendWord(size) + + # write binary stream of data + for i in range(0, size): + sendByte(data[i]) + + 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 + linecount = 0 + for line in fp: + 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)) + 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 1337 board..." +print "Please press RESET on your board..." -while 1: +while True: tty.write('V') tty.flush() try: @@ -129,34 +211,50 @@ while 1: pass print "OK, trying to set baudrate..." - # set baudrate -cmdBAUDRATE(REAL_BAUDRATE) -tty = SerialPort(DEVICE, 100, REAL_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 -sendByte(0x01) -print recvByte() -sendByte(0x02) -print recvByte() -sys.exit(0) -""" - - -# 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 + +# 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." -print "Reading from the IRAM again..." -data_re = cmdREAD(0x00030000, len(data_wr)) -print "Received data:", data_re, "Checksum:", last_checksum +sendByte(0x97) # exit and restart +print "Program was started. Have fun!"