OpenCores
URL https://opencores.org/ocsvn/usb_dongle_fpga/usb_dongle_fpga/trunk

Subversion Repositories usb_dongle_fpga

[/] [usb_dongle_fpga/] [tags/] [version_1_5/] [sw/] [dongle.py] - Diff between revs 8 and 9

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 8 Rev 9
Line 55... Line 55...
    print " -v        Verbose"
    print " -v        Verbose"
    print " -f        Forced"
    print " -f        Forced"
    print " -d        Debug"
    print " -d        Debug"
    print " -q        Query"
    print " -q        Query"
    print " -r        Readback "
    print " -r        Readback "
 
    print " -e        Erase device "
    print ""
    print ""
    print "Board test options: "
    print "Board test options: "
    print " -t        Marching one and zero test address + data (Device must be empty)"
    print " -t        Marching one and zero test address + data (Device must be empty)"
    print " -e        Erase before test "
 
    print " -b        Leave flash blanc after test "
    print " -b        Leave flash blanc after test "
    print ""
    print ""
    print "Examples:"
    print "Examples:"
    print ""
    print ""
    print " ",s," -c COM3 loader.bin 0"
    print " ",s," -c COM3 loader.bin 0"
Line 117... Line 117...
            self.tty = SerialPort(name,timeout, baud)
            self.tty = SerialPort(name,timeout, baud)
        except:
        except:
            print "Unable to open port"
            print "Unable to open port"
            sys.exit();
            sys.exit();
 
 
 
    def testReturn(self,byteCount):
 
        i=0
 
        while don.tty.inWaiting()<byteCount:
 
            i=i+1
 
            if i==10000*byteCount:
 
                break
 
        if i==10000*byteCount:
 
            return 0
 
        return don.tty.inWaiting()  ## ret two bytes            
 
 
    def getReturn(self,byteCount):
    def getReturn(self,byteCount):
        i=0
        i=0
        while don.tty.inWaiting()<byteCount:
        while don.tty.inWaiting()<byteCount:
            i=i+1
            i=i+1
            if i==10000*byteCount:
            if i==10000*byteCount:
                break
                break
        if i==10000*byteCount:
        if i==10000*byteCount:
            print "Dongle not connected to port or not communicating"
            print "Dongle not communicating"
            sys.exit()
            sys.exit()
        return don.tty.read(byteCount)  ## ret two bytes
        return don.tty.read(byteCount)  ## ret two bytes
 
 
 
 
    def write_command(self,command):
    def write_command(self,command):
        lsb = command&0xff
        lsb = command&0xff
        msb = (command>>8)&0xff
        msb = (command>>8)&0xff
        self.tty.write_2bytes(msb,lsb)
        self.tty.write_2bytes(msb,lsb)
 
 
Line 336... Line 347...
    while (n < 100000):
    while (n < 100000):
        n += 1;
        n += 1;
    k10Time = time.clock() - mytime   # time per 10000 while cycles
    k10Time = time.clock() - mytime   # time per 10000 while cycles
    wait = k10Time/100000.0     # time per while cycle
    wait = k10Time/100000.0     # time per while cycle
    wait = (0.00025/wait) * 1.20   # count for 250us + safe margin
    wait = (0.00025/wait) * 1.20   # count for 250us + safe margin
 
 
    # ok done
    # ok done
 
    reopened = 0
 
 
    don = Dongle(mode.portname,115200,100)
    don = Dongle(mode.portname,115200,100)
    print wait
 
    don.tty.wait = wait
    don.tty.wait = wait
 
    while 1:
    don.write_command(0x0050) # 0x0098
    don.write_command(0x0050) # 0x0098
    don.write_command(0x00C5)            #send dongle check internal command
    don.write_command(0x00C5)            #send dongle check internal command
 
        don_ret=don.testReturn(2)
 
        if don_ret==2:
 
            break
 
        if reopened == 3:
 
             print 'Dongle connected, but does not communicate'
 
             sys.exit()
 
        reopened = reopened + 1
 
        # reopen and do new cycle
 
        don = Dongle(mode.portname,115200,100)
 
        don.tty.wait = wait
 
 
    buf=don.getReturn(2)  # two bytes expected to this command
    buf=don.getReturn(2)  # two bytes expected to this command
 
 
    if ord(buf[1])==0x32 and  ord(buf[0])==0x10:
    if ord(buf[1])==0x32 and  ord(buf[0])==0x10:
        print "Dongle OK"
        print "Dongle OK"
    else:
    else:
        print 'Dongle returned on open: %02x %02x '%(ord(buf[1]), ord(buf[0]))
        print 'Dongle returned on open: %02x %02x '%(ord(buf[1]), ord(buf[0]))
 
 
 
 
if mode.q == 1:   # perform a query from dongle
if mode.q == 1:   # perform a query from dongle
 
 
 
    buf=don.read_data(4,0x0)  # word count and word address
 
    print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[1]),ord(buf[0]),ord(buf[3]),ord(buf[2]),ord(buf[5]),ord(buf[4]),ord(buf[7]),ord(buf[6]) )
 
 
 
 
    don.write_command(0x0050) # 0x0098
    don.write_command(0x0050) # 0x0098
    don.write_command(0x0098) # 0x0098
    don.write_command(0x0098) # 0x0098
    buf=don.read_data(3,0x000010)  # word count and word address
    buf=don.read_data(3,0x000010)  # word count and word address
    if ord(buf[0])==0x51 and  ord(buf[2])==0x52 and  ord(buf[4])==0x59:
    if ord(buf[0])==0x51 and  ord(buf[2])==0x52 and  ord(buf[4])==0x59:
        buf=don.read_data(2,0x000000)  # word count and word address
        buf=don.read_data(2,0x000000)  # word count and word address
Line 362... Line 391...
        buf=don.read_data(2,0x000002)
        buf=don.read_data(2,0x000002)
        print 'lock bit is 0x%02x 0x%02x'%(ord(buf[0]),ord(buf[1]))
        print 'lock bit is 0x%02x 0x%02x'%(ord(buf[0]),ord(buf[1]))
    else:
    else:
        print "Got bad query data:"
        print "Got bad query data:"
        print 'Query address 0x10 = 0x%02x%02x '%(ord(buf[1]),ord(buf[0]))
        print 'Query address 0x10 = 0x%02x%02x '%(ord(buf[1]),ord(buf[0]))
        print 'Query address 0x10 = 0x%02x%02x '%(ord(buf[3]),ord(buf[2]))
        print 'Query address 0x12 = 0x%02x%02x '%(ord(buf[3]),ord(buf[2]))
        print 'Query address 0x10 = 0x%02x%02x '%(ord(buf[5]),ord(buf[4]))
        print 'Query address 0x14 = 0x%02x%02x '%(ord(buf[5]),ord(buf[4]))
        print "Read byte count:",len(buf)
        print "Read byte count:",len(buf)
 
 
    don.write_command(0x00FF) # 0x0098
    don.write_command(0x00FF) # 0x0098
    buf=don.read_data(4,0xff57c0>>1)  # word count and word address     
    buf=don.read_data(4,0xff57c0>>1)  # word count and word address     
 
 
    print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[1]),ord(buf[0]),ord(buf[3]),ord(buf[2]),ord(buf[5]),ord(buf[4]),ord(buf[7]),ord(buf[6]) )
    print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[1]),ord(buf[0]),ord(buf[3]),ord(buf[2]),ord(buf[5]),ord(buf[4]),ord(buf[7]),ord(buf[6]) )
 
 
 
 
 
 
if mode.filename!="" and mode.address!=-1:
if mode.filename!="" and mode.address!=-1:
Line 395... Line 425...
    don.parse_status()
    don.parse_status()
    wordSize = (size+ (size&1))>> 1    # round byte count up and make word address
    wordSize = (size+ (size&1))>> 1    # round byte count up and make word address
    endBlock = don.get_block_no(mode.address+wordSize - 1)
    endBlock = don.get_block_no(mode.address+wordSize - 1)
    startBlock = don.get_block_no(mode.address)
    startBlock = don.get_block_no(mode.address)
    i=startBlock
    i=startBlock
 
    print 'Erasing from block %i to %i '%(i,endBlock)
    while i <= endBlock:
    while i <= endBlock:
 
        if mode.v == 1:
        print 'Erasing block %i '%(i)
        print 'Erasing block %i '%(i)
 
        else:
 
            sys.stdout.write(".")
 
            sys.stdout.flush()
        don.erase_block(i)
        don.erase_block(i)
        don.wait_on_busy()
        don.wait_on_busy()
        don.parse_status()   #do this after programming all but uneaven ending
        don.parse_status()   #do this after programming all but uneaven ending
        i=i+1
        i=i+1
 
    if mode.v == 0:
 
        print " "
    #don.write_command(0x00FF) # 0x0098
    #don.write_command(0x00FF) # 0x0098
    #buf=don.read_data(4,0x000000)  # word count and word address     
    #buf=don.read_data(4,0x000000)  # word count and word address     
    #print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[0]),ord(buf[1]),ord(buf[2]),ord(buf[3]),ord(buf[4]),ord(buf[5]),ord(buf[6]),ord(buf[7]) )
    #print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[0]),ord(buf[1]),ord(buf[2]),ord(buf[3]),ord(buf[4]),ord(buf[5]),ord(buf[6]),ord(buf[7]) )
 
 
    f=open(mode.filename,"rb")
    f=open(mode.filename,"rb")
    f.seek(0) #seek to start
    f.seek(0) #seek to start
    address= mode.address
    address= mode.address
    #don.set_address(address)
    #don.set_address(address)
 
    print 'Writing %iK'%(size/1024)
    while 1:
    while 1:
        if address/(1024*16) != (address-16)/(1024*16):  # get bytes from words if 512
        if (address/(1024*64) != (address-16)/(1024*64)) and address != mode.address:  # get bytes from words if 512
 
            if mode.v == 1:
            print 'Progress: %iK of %iK at 0x%06x'%((address-mode.address)/512,size/1024,address)
            print 'Progress: %iK of %iK at 0x%06x'%((address-mode.address)/512,size/1024,address)
 
            else:
 
                sys.stdout.write(".")
 
                sys.stdout.flush()
        buf = f.read(32)  #16 words is maximum write here bytes are read
        buf = f.read(32)  #16 words is maximum write here bytes are read
        if len(buf)==32:
        if len(buf)==32:
            don.buffer_write(16,address,buf)
            don.buffer_write(16,address,buf)
            address = address + 16
            address = address + 16
        elif len(buf)>0:
        elif len(buf)>0:
Line 427... Line 469...
            don.buffer_write(len,address,buf)
            don.buffer_write(len,address,buf)
            address = address + 16     #inc word address
            address = address + 16     #inc word address
            break
            break
        else:
        else:
            break
            break
 
    if mode.v == 0:
 
        print " "
    print "Write DONE!"
    print "Write DONE!"
    don.parse_status()   #do this after programming all but uneaven ending
    don.parse_status()   #do this after programming all but uneaven ending
    f.close()
    f.close()
 
 
if mode.r == 1:   # perform a readback
if mode.r == 1:   # perform a readback
    if mode.offset!=-1 and mode.length!=-1 and mode.filename!="":
    if mode.offset!=-1 and mode.length!=-1 and mode.filename!="":
        mode.offset=mode.offset>>1    #make word offset
        mode.offset=mode.offset>>1    #make word offset
        mode.length= mode.length>>1   #make word length
        mode.length= mode.length>>1   #make word length
 
        print 'Reading %iK'%(mode.length/512)
        try:
        try:
            f=open(mode.filename,"wb")
            f=open(mode.filename,"wb")
            don.write_command(0x00FF) #  put flash to data read mode
            don.write_command(0x00FF) #  put flash to data read mode
            address = mode.offset    # set word address
            address = mode.offset    # set word address
            while 1:
            while 1:
                if address/(1024*32) != (address-128)/(1024*32):  # get K bytes from words if 512
                if address/(1024*32) != (address-128)/(1024*32):  # get K bytes from words if 512
 
                    if mode.v == 1:
                    print 'Progress: %iK of %iK'%((address-mode.offset)/512,mode.length/512)
                    print 'Progress: %iK of %iK'%((address-mode.offset)/512,mode.length/512)
 
                    else:
 
                        sys.stdout.write(".")
 
                        sys.stdout.flush()
                buf=don.read_data(128,address)  # word count and byte address read 64 words to speed up
                buf=don.read_data(128,address)  # word count and byte address read 64 words to speed up
                f.write(buf)
                f.write(buf)
                #print "from address:",address<<1," ", len(buf)
                #print "from address:",address<<1," ", len(buf)
                if address+128 >= (mode.offset + mode.length):  # 2+64 estimates the end to end in right place
                if address+128 >= (mode.offset + mode.length):  # 2+64 estimates the end to end in right place
                    break
                    break
                address = address + 128    #this is word address
                address = address + 128    #this is word address
            f.close()
            f.close()
 
            if mode.v == 0:
 
                print " "
            print "Readback done!"
            print "Readback done!"
        except IOError:
        except IOError:
            print "IO Error on file open"
            print "IO Error on file open"
            sys.exit()
            sys.exit()
 
 
    else:
    else:
       print "Some of readback parameters missing..."
       print "Some of readback parameters missing..."
       print mode.offset,mode.length, mode.filename
       print mode.offset,mode.length, mode.filename
       sys.exit()
       sys.exit()
 
 
if mode.t == 1:   # perform dongle test
if mode.t == 1:   # perform dongle test
        print "Dongle TEST"
        print "Dongle TEST"
        if mode.e == 1:
        if mode.e == 1:
            #Erase Dongle
            #Erase Dongle
 
            print "Erasing"
            don.write_command(0x0060) # 0x0098
            don.write_command(0x0060) # 0x0098
            don.write_command(0x00D0) # 0x0098
            don.write_command(0x00D0) # 0x0098
            don.wait_on_busy()
            don.wait_on_busy()
            don.parse_status()
            don.parse_status()
            endBlock = 31
            endBlock = 31
            startBlock = 0
            startBlock = 0
            i=startBlock
            i=startBlock
            while i <= endBlock:
            while i <= endBlock:
 
                if mode.v == 1:
                print 'Erasing block %i '%(i)
                print 'Erasing block %i '%(i)
 
                else:
 
                     sys.stdout.write(".")
 
                     sys.stdout.flush()
                don.erase_block(i)
                don.erase_block(i)
                don.wait_on_busy()
                don.wait_on_busy()
                don.parse_status()   #do this after programming all but uneaven ending
                don.parse_status()   #do this after programming all but uneaven ending
                i=i+1
                i=i+1
 
            if mode.v == 0: # add CRTL return to dots
 
                print ""
        #Do marching one test on data and address
        #Do marching one test on data and address
        mode.length= 0   #make word length
        mode.length= 0   #make word length
        try:
        try:
            #Marching one test
            #Marching one test
            #---------------------------------------------------------------------------
            #---------------------------------------------------------------------------
Line 501... Line 560...
                data = data >> 1
                data = data >> 1
                mode.length =  mode.length + 1
                mode.length =  mode.length + 1
                buf2=don.read_data(1,0)  #read first byte
                buf2=don.read_data(1,0)  #read first byte
                if ord(buf2[0]) != 0xFF:
                if ord(buf2[0]) != 0xFF:
                    print "Test FAIL (At least one address line const. 0)!!!!!"
                    print "Test FAIL (At least one address line const. 0)!!!!!"
 
                    sys.exit()
            #-----------------------------------------------------------------------
            #-----------------------------------------------------------------------
            #Marching zero test
            #Marching zero test
            address = 0xFFEFFFFF    # set word address
            address = 0xFFEFFFFF    # set word address
            data = 0x100000
            data = 0x100000
            while mode.length<18: # last address to test 0x20 0000  
            while mode.length<18: # last address to test 0x20 0000  
Line 522... Line 582...
                data = data >> 1
                data = data >> 1
                mode.length =  mode.length + 1
                mode.length =  mode.length + 1
                buf2=don.read_data(1,0x1FFFFF)  #read first byte
                buf2=don.read_data(1,0x1FFFFF)  #read first byte
                if ord(buf2[0]) != 0xFF:
                if ord(buf2[0]) != 0xFF:
                    print "Test FAIL (At least two address lines bonded)!!!!!"
                    print "Test FAIL (At least two address lines bonded)!!!!!"
 
                    sys.exit()
            if mode.b == 1:
            if mode.b == 1:
                #Erase Dongle
                #Erase Dongle
 
                print "Erasing"
                don.write_command(0x0060) # 0x0098
                don.write_command(0x0060) # 0x0098
                don.write_command(0x00D0) # 0x0098
                don.write_command(0x00D0) # 0x0098
                don.wait_on_busy()
                don.wait_on_busy()
                don.parse_status()
                don.parse_status()
                endBlock = 31
                endBlock = 31
                startBlock = 0
                startBlock = 0
                i=startBlock
                i=startBlock
                while i <= endBlock:
                while i <= endBlock:
 
                    if mode.v == 1:
                    print 'Blanking block %i '%(i)
                    print 'Blanking block %i '%(i)
 
                    else:
 
                        sys.stdout.write(".")
 
                        sys.stdout.flush()
                    don.erase_block(i)
                    don.erase_block(i)
                    don.wait_on_busy()
                    don.wait_on_busy()
                    don.parse_status()   #do this after programming all but uneaven ending
                    don.parse_status()   #do this after programming all but uneaven ending
                    i=i+1
                    i=i+1
 
                if mode.v == 0:
 
                    print " "
            print "Test SUCCESSFUL!"
            print "Test SUCCESSFUL!"
 
            sys.exit()
        except IOError:
        except IOError:
            print "IO Error on file open"
            print "IO Error on file open"
            sys.exit()
            sys.exit()
 
 
 
if mode.e == 1:   # perform dongle test
 
            #Erase Dongle
 
            print "Erasing all"
 
            don.write_command(0x0060) # 0x0098
 
            don.write_command(0x00D0) # 0x0098
 
            don.wait_on_busy()
 
            don.parse_status()
 
            endBlock = 31
 
            startBlock = 0
 
            i=startBlock
 
            while i <= endBlock:
 
                if mode.v == 1:
 
                    print 'Erasing block %i '%(i)
 
                else:
 
                     sys.stdout.write(".")
 
                     sys.stdout.flush()
 
                don.erase_block(i)
 
                don.wait_on_busy()
 
                don.parse_status()   #do this after programming all but uneaven ending
 
                i=i+1
 
            if mode.v == 0: # add CRTL return to dots
 
                print ""
 
            print "Erase done."
 
 
##########################################################
##########################################################
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.