X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=frprog.py;h=b9432a85c2a67f698ced568193561155cb65fb9e;hb=aa7ae00606beffecc4e058868a81c76a6872110d;hp=7ea1596f4d75bb348330f77e062a2c8fb12b76b6;hpb=57f45381d4add85a253804c995ce2af4a67fb0a7;p=pyfrprog.git diff --git a/frprog.py b/frprog.py index 7ea1596..b9432a8 100755 --- a/frprog.py +++ b/frprog.py @@ -6,14 +6,15 @@ 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)) def sendWord(word): @@ -34,7 +35,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): @@ -53,7 +54,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): @@ -71,7 +72,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): @@ -85,7 +86,7 @@ def cmdCALL(address): #return recvByte() # TODO: test this function! -def cmdCHECKSUM(): +def bootromCHECKSUM(): # call CHECKSUM command sendByte(0x01) if (recvByte() != 0xF1): @@ -96,7 +97,7 @@ def cmdCHECKSUM(): # get checksum recvChecksum() -def cmdBAUDRATE(baudrate): +def bootromBAUDRATE(baudrate): # send BAUDRATE command sendByte(0x01) if (recvByte() != 0xF1): @@ -107,13 +108,48 @@ def cmdBAUDRATE(baudrate): # send desired baudrate 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 read_mhx_file(fp): # needs a file handle to the desired mhx file +def readMHXFile(filename): # desired mhx filename + fp = open(filename, "r") retval = [] # returns a list of FlashSequence objects linecount = 0 for line in fp: @@ -142,35 +178,29 @@ def read_mhx_file(fp): # needs a file handle to the desired mhx file # add flash sequence to our list retval.append(FlashSequence(address, data)) + fp.close() return retval # check command line arguments -if len(sys.argv) != 2: - print "Usage: " + sys.argv[0] + " [mhx-file]" +if len(sys.argv) != 3: + print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]" sys.exit(1) -# read in data from mhx-file before starting +# read in data from mhx-files before starting try: - fp = open(sys.argv[1], "r") -except IOError: - print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!" + 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) -# get list of all our address/data pairs to flash -flashseqs = read_mhx_file(fp) -fp.close() - -print "The following flash sequences have been read in:" -for seq in flashseqs: - print hex(seq.address) + ":", seq.data - 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: @@ -180,125 +210,53 @@ while 1: # 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 -cmdBAUDRATE(REAL_BAUDRATE) -tty = SerialPort(DEVICE, 100, REAL_BAUDRATE) +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 flashseqs: +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) + #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 -cmdCALL(0x30000); -sys.exit(0) - - -# some tests here....... -""" -# execute (existing) program in ram -cmdCALL(0x00033ffc) -sys.exit(0) -""" - -# read something from the IRAM -#print cmdREAD(0x00030000, 32) - -#data = [] -#for i in range(0, 32): -# data.append(i) -#cmdWRITE(0x00030000, 32, data) - +# 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) -""" -# 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 "Performing ChipErase..." +pkernCHIPERASE() -print "Calculated checksum:", checksum -print "Writing", data_wr, "to the IRAM..." -cmdWRITE(0x00030000, len(data_wr), data_wr) -print "Received Checksum:", last_checksum +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 "Reading from the IRAM again..." -data_re = cmdREAD(0x00030000, len(data_wr)) -print "Received data:", data_re, "Checksum:", last_checksum -""" - -""" -# see whats in the iram at the moment -data_wr = [] -print "Reading from the IRAM..." -data_re = cmdREAD(0x00030000, 0x10000-4) -print "Received data:", data_re, "Checksum:", last_checksum -""" - -""" -# see whats in the dram at the moment -data_wr = [] -print "Reading from the DRAM..." -data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4) -print "Received data:", data_re, "Checksum:", last_checksum -""" - -""" -# blank the iram -data_wr = [] -for i in range(0, 0x10000-4): - value = 0 - data_wr.append(value) - -print "Writing", data_wr, "to the IRAM..." -cmdWRITE(0x00030000, len(data_wr), data_wr) -print "Received Checksum:", last_checksum -print -""" - -""" -# blank the dram -data_wr = [] -for i in range(0, 0x10000-0xC000-4): - value = 0 - data_wr.append(value) - -print "Writing", data_wr, "to the DRAM..." -cmdWRITE(0x0002C000, len(data_wr), data_wr) -print "Received Checksum:", last_checksum -print -""" - -""" -# write some data in the iram and try to execute it -data_wr =[ - 0x9B,0x00, - 0x0D,0x4e, - 0xcf,0xf1, - 0x16,0x01, - 0x9b,0x05, - 0x04,0xc7, - 0xc1,0x06, - 0x16,0x56, - 0xe0,0xfb, #branch - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, - 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0] -print "Writing", data_wr, "to the IRAM..." -cmdWRITE(0x00030000, len(data_wr), data_wr) -print "Received Checksum:", last_checksum -print -cmdCALL(0x00030000) -""" +duration = time.time() - starttime +print "Procedure complete, took", round(duration, 2), "seconds." + +sendByte(0x97) # exit and restart +print "Program was started. Have fun!"