1 # Copyright (C) 2009 Ubixum, Inc.
3 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 def get_eeprom(addr,length):
26 while len(prom_val)<length:
27 buf='\x00'*1024 # read 1024 bytes max at a time
28 transfer_len = length-len(prom_val) > 1024 and 1024 or length-len(prom_val)
29 ret=f.do_usb_command ( buf,
32 addr+len(prom_val),0,transfer_len )
36 raise Exception("eeprom read didn't work: %d" % ret )
41 return int(c.encode('hex'),16)
47 This function dynamically determines how much data to read for c2 eeprom data and downloads
54 return prom # c0 blocks are 8 bytes long
55 if prom[0] != '\xc2': raise Exception ( "Envalid eeprom (%s)" % prom[0].encode('hex') )
56 # the length of the 1st data block is bytes 8,9 (0 based)
59 size_read = get_eeprom(read_addr,4) # get the data length and start address
62 # if this is the end 0x80 0x01 0xe6 0x00, then break
63 if size_read == '\x80\x01\xe6\x00': break
64 # else it is a data block
65 size = (hexchartoint(size_read[0]) << 8) + hexchartoint(size_read[1])
66 print "Next eeprom data size %d" % size
67 prom += get_eeprom(read_addr,size)
70 prom += get_eeprom(read_addr,1) # should always be 0
71 assert prom[-1] == '\x00'
78 while bytes_written<len(prom):
79 # attemp 1024 at a time
80 to_write=len(prom)-bytes_written > 1024 and 1024 or len(prom)-bytes_written
81 print "Writing %d Bytes.." % to_write
82 ret=f.do_usb_command(prom[bytes_written:bytes_written+to_write], 0x40,0xb1,bytes_written, 0, to_write, 10000)
86 raise Exception ( "eeprom write didn't work: %d" % ret )
89 if __name__=='__main__':
91 openfx2(0x04b4,0x0083) # vid/pid of eeprom firmware