📄 dongle.py
字号:
except SerialPortException , e:
print "Unable to open port " + name
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
j=don.tty.inWaiting()
#print "Tested in waiting %i needed %i"%(j,byteCount)
return j ## ret two bytes
def getReturn(self,byteCount):
i=0
#while don.tty.inWaiting()<byteCount:
# i=i+1
# time.sleep(0.1)
# if i==100*byteCount:
# print "Dongle not communicating"
# #print "Read in waiting %i needed %i was %i"%(i,byteCount,don.tty.inWaiting())
# sys.exit()
# break
#i=don.tty.inWaiting()
#print "Read in waiting %i needed %i was %i"%(i,byteCount,don.tty.inWaiting())
buf = don.tty.read(byteCount)
#print "Got bytes =%i "%(len(buf))
return buf ## ret two bytes
def write_command(self,command):
lsb = command&0xff
msb = (command>>8)&0xff
self.write_2bytes(msb,lsb)
def write_2bytes(self, msb,lsb):
"""Write one word MSB,LSB to the serial port MSB first"""
s = pack('BB', msb, lsb)
ret = self.tty.write(s)
if(ret<len(s)):
print 'write_2byte: Wrote less then needed %i bytes from %i'%(ret,length(s))
# Wait for the write to complete
#WaitForSingleObject(overlapped.hEvent, INFINITE)
def get_address_buf(self,address): #set word address
lsbyte = address&0xff
byte = (address>>8)&0xff
msbyte = (address>>16)&0xff
buffer = ""
buffer += chr(lsbyte)
buffer += chr(0xA0)
buffer += chr(byte)
buffer += chr(0xA1)
buffer += chr(msbyte)
buffer += chr(0xA2)
evaluate = (address>>24)
if evaluate != 0:
print "Addressign fault. Too large address passed"
sys.exit()
return buffer
def set_address(self,address): #set word address
lsbyte = address&0xff
byte = (address>>8)&0xff
msbyte = (address>>16)&0xff
evaluate = (address>>24)
if evaluate != 0:
print "Addressign fault. Too large address passed"
sys.exit()
self.write_2bytes(lsbyte,0xA0) #set internal address to dongle
self.write_2bytes(byte,0xA1) #set internal address to dongle
self.write_2bytes(msbyte,0xA2) #send query command
def read_data(self,wordCount,address):
command = 0
byteCount = wordCount<<1 #calc byte count
if wordCount>0 :
command = (command|wordCount)<<8;
command = command|0xCD
self.set_address(address) # send read address
self.write_command(command) # send get data command
return self.getReturn(byteCount)
else:
print "Word count can't be under 1"
sys.exit()
def issue_blk_read(self):
command = 0
wordCount = 0
byteCount = wordCount<<1 #calc byte count
command = (command|wordCount)<<8;
command = command|0xCD
self.write_command(command) # send get data command
def read_status(self):
don.write_command(0x0070) # 0x0098 //clear status
command = 0
wordCount= 1 #calc byte count
byteCount = wordCount<<1
command = (command|wordCount)<<8;
command = command|0xCD
self.write_command(command) # send get data command
return self.getReturn(byteCount)
def get_block_no(self,address):
return address >> 16 # 16 bit mode block is 64Kwords
def wait_on_busy(self):
exit=0
while exit==0:
buf=self.read_status()
statReg = ord(buf[0]) #8 bit reg
if statReg>>7 == 1:
exit=1
def parse_status(self): # use only after wait on busy commad to get result of the operation
exit = 0
buf=self.read_status()
statReg = ord(buf[0]) #8 bit reg
if (statReg>>5)&1 == 1:
print "Block erase suspended"
exit = 1
if (statReg>>4)&3 == 3:
print "Error in command order" #if bits 4 and 5 are set then
exit = 1
if (statReg>>4)&3 == 1:
print "Error in setting lock bit"
exit = 1
if (statReg>>3)&1 == 1:
print "Low Programming Voltage Detected, Operation Aborted"
exit = 1
if (statReg>>2)&1 == 1:
print "Programming suspended"
exit = 1
if (statReg>>1)&1 == 1:
print "Block lock bit detected"
exit = 1
if exit == 1:
sys.exit()
def erase_block(self,blockNo):
blockAddress = blockNo << 16
command = 0x0020
self.set_address(blockAddress)
self.write_command(command) #issue block erase
command = 0x00D0
self.write_command(command) #issue block erase confirm
#self.wait_on_busy()
#self.parse_status()
def buffer_write(self,wordCount,startAddress,buffer):
# to speed up buffer writing compose all commands into one buffer
# instead of multiple single writes this is needed as the FTDI chip
# round lag is amazingly large with VCOM drivers
#u = len(buffer)
if len(buffer)<32: #don't ever make unaligned writes
i=len(buffer)
while len(buffer)<32:
buffer += "\xff"
adrBuf = self.get_address_buf(startAddress) #6 bytes total
cmd_e8="" #8 bytes total
cmd_e8+= chr(16) #make it always 16 wordCount
cmd_e8+= chr(0xE8)
cmd_wcnt="" #10 bytes total
cmd_wcnt+= chr(0x00)
cmd_wcnt+= chr(16-1)
cmd_buf="" #12 bytes total
cmd_buf+= chr(0x00)
cmd_buf+= chr(0xD0)
wr_buffer_cmd = adrBuf + cmd_e8 + cmd_wcnt + buffer + cmd_buf #44 bytes total
self.write_buf_cmd(wr_buffer_cmd)
if self.mode.version <5:
n = 0
if sys.platform=='win32':
while (n < 1024):
n += 1;
elif sys.platform=='linux2' or sys.platform=='darwin':
#Linux FTDI VCP driver is way faster and needs longer grace time than windows driver
while (n < 1024*8):
n += 1;
def write_buf_cmd(self, buffer):
"""Write one word MSB,LSB to the serial port MSB first"""
a=0
s=""
if (len(buffer) < 44): # if buffer is shorter than expected then pad with read array mode commands
i=0
while i<len(buffer):
print '0x%02x'%(ord(buffer[i]))
i+=1
while(a < len(buffer)):
if a < 10:
s= pack('2c', buffer[a], buffer[a+1])
self.tty.write(s)
elif a < len(buffer)-2:
s= pack('2c', buffer[a+1], buffer[a])
self.tty.write(s)
elif len(buffer)==2:
s=pack('2c', buffer[a], buffer[a+1])
self.tty.write(s)
else:
s=pack('2c', buffer[a], chr(0xFF))
self.tty.write(s)
a+=2
else:
#first 10 bytes are in correct order + 32 data bytes are in wrong order and + 2 confirm bytes are in correct order
s=pack('44c',
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],
buffer[8], buffer[9], buffer[11], buffer[10], buffer[13], buffer[12], buffer[15], buffer[14],
buffer[17], buffer[16], buffer[19], buffer[18], buffer[21], buffer[20], buffer[23], buffer[22],
buffer[25], buffer[24], buffer[27], buffer[26], buffer[29], buffer[28], buffer[31], buffer[30],
buffer[33], buffer[32], buffer[35], buffer[34], buffer[37], buffer[36], buffer[39], buffer[38],
buffer[41], buffer[40], buffer[42], buffer[43]
)
ret = self.tty.write(s)
################## Main program #########################
last_ops = 0
mode = DongleMode()
# PARSE ARGUMENTS
for arg in sys.argv:
if len(sys.argv) == 1: # if no arguments display help
#usage(sys.argv[0])
usage("dongle.py")
sys.exit()
if arg in ("-h","--help","/help","/h"):
#usage(sys.argv[0])
usage("dongle.py")
sys.exit()
if arg in ("-c"):
last_ops = sys.argv.index(arg) + 1 #if remains last set of options from here start ordered strings
i = sys.argv.index(arg)
print "Opening port: "+sys.argv[i+1]
mode.portname = sys.argv[i+1] # next element after -c open port for usage
if arg[0]=="-" and arg[1]!="c": # if other opptions
# parse all options in this
last_ops = sys.argv.index(arg) #if remains last set of options from here start ordered strings
ops = arg[1:]# get all besides the - sign
for op in ops:
if op=="q":
mode.q = 1
if op=="v":
mode.v = 1
if op=="f":
mode.f = 1
if op=="d":
mode.d = 1
if op=="r":
mode.r = 1
if op=="t":
mode.t = 1
if op=="e":
mode.e = 1
if op=="b":
mode.b = 1
if op=="l":
mode.l = 1
else:
i = sys.argv.index(arg)
if i == last_ops + 1:
if mode.r==1:
mode.offset=mode.convParamStr(arg)
else:
mode.filename=arg
if i == last_ops + 2:
if mode.r==1:
mode.length=mode.convParamStr(arg)
else:
mode.address=mode.convParamStr(arg)
if i == last_ops + 3:
if mode.r==1:
mode.filename=arg
else:
print "Too many parameters provided"
sys.exit()
if i > last_ops + 3:
print "Too many parameters provided"
sys.exit()
# END PARSE ARGUMENTS
if mode.portname=="":
print "No port name given see -h for help"
sys.exit()
else:
# test PC speed to find sutable delay for linux driver
# to get 250 us
mytime = time.clock()
n = 0
while (n < 100000):
n += 1;
k10Time = time.clock() - mytime # time per 10000 while cycles
wait = k10Time/100000.0 # time per while cycle
wait = (0.00025/wait) * 1.20 # count for 250us + safe margin
# ok done
reopened = 0
if sys.platform=='win32':
don = Dongle(mode.portname,256000,6000)
elif sys.platform=='linux2':
don = Dongle(mode.portname,230400,6000)
#don.tty.cts()
elif sys.platform=='darwin':
don = Dongle(mode.portname,230400,6000)
#don.tty.cts()
else:
sys.exit('Sorry, no implementation for this platform yet')
don.tty.wait = wait
while 1:
don.write_command(0x0050) # 0x0098
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
if sys.platform=='win32':
don = Dongle(mode.portname,256000,6000)
elif sys.platform=='linux2':
don = Dongle(mode.portname,230400,6000)
#self.tty.cts()
elif sys.platform=='darwin':
don = Dongle(mode.portname,230400,6000)
#self.tty.cts()
else:
sys.exit('Sorry, no implementation for this platform yet')
don.tty.wait = wait
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -