]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lib/examples/eeprom/client.py
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / examples / eeprom / client.py
CommitLineData
3608c106
UH
1# Copyright (C) 2009 Ubixum, Inc.
2#
3# This library is free software; you can redistribute it and/or
4#
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.
8#
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.
13#
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
17
18import sys
19from fx2load import *
20
21
22def get_eeprom(addr,length):
23 assert f.isopen()
24
25 prom_val = '';
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,
30 0xc0,
31 0xb1,
32 addr+len(prom_val),0,transfer_len )
33 if (ret>=0):
34 prom_val += buf[:ret]
35 else:
36 raise Exception("eeprom read didn't work: %d" % ret )
37 return prom_val
38
39
40def hexchartoint(c):
41 return int(c.encode('hex'),16)
42
43
44def fetch_eeprom():
45 """
46 See TRM 3.4.2, 3.4,3.
47 This function dynamically determines how much data to read for c2 eeprom data and downloads
48 the eeprom iic file.
49 """
50 assert f.isopen()
51 # fetch 1st 8 bytes
52 prom=get_eeprom(0,8)
53 if prom[0] == '\xc0':
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)
57 read_addr=8
58 while True:
59 size_read = get_eeprom(read_addr,4) # get the data length and start address
60 prom += size_read
61 read_addr+=4
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)
68 read_addr+=size
69 # one last byte
70 prom += get_eeprom(read_addr,1) # should always be 0
71 assert prom[-1] == '\x00'
72 return prom
73
74
75def set_eeprom(prom):
76 assert f.isopen()
77 bytes_written=0;
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)
83 if ret>0:
84 bytes_written += ret;
85 else:
86 raise Exception ( "eeprom write didn't work: %d" % ret )
87
88
89if __name__=='__main__':
90
91 openfx2(0x04b4,0x0083) # vid/pid of eeprom firmware
92