3 Copyright 2015, Sinclair R.F., Inc.
5 This program is distributed under the GPL, version 2.
7 Demonstrate how to configure the FT230X USB UART bridge as follows:
9 CBUS3 Drive 1 (accomodate PCB error)
14 # Need to install libftdi for the following to work (see README.txt)
17 # Define class for displaying errors.
18 class ErrorMsg(Exception):
19 def __init__(self,message):
20 self.message = message;
24 # Function to convert CBUSH values to human-readable strings
25 def cbush_string(value):
26 if value == ftdi.CBUSH_AWAKE:
28 if value == ftdi.CBUSH_BAT_DETECT:
30 if value == ftdi.CBUSH_BAT_DETECT_NEG:
31 return 'BAT_DETECT_NEG'
32 if value == ftdi.CBUSH_BB_RD:
34 if value == ftdi.CBUSH_BB_WR:
36 if value == ftdi.CBUSH_CLK15:
38 if value == ftdi.CBUSH_CLK30:
40 if value == ftdi.CBUSH_CLK7_5:
42 if value == ftdi.CBUSH_DRIVE_0:
44 if value == ftdi.CBUSH_DRIVE1:
46 if value == ftdi.CBUSH_I2C_RXF:
48 if value == ftdi.CBUSH_I2C_TXE:
50 if value == ftdi.CBUSH_IOMODE:
52 if value == ftdi.CBUSH_PWREN:
54 if value == ftdi.CBUSH_RXLED:
56 if value == ftdi.CBUSH_SLEEP:
58 if value == ftdi.CBUSH_TIME_STAMP:
60 if value == ftdi.CBUSH_TRISTATE:
62 if value == ftdi.CBUSH_TXDEN:
64 if value == ftdi.CBUSH_TXLED:
66 if value == ftdi.CBUSH_TXRXLED:
68 if value == ftdi.CBUSH_VBUS_SENSE:
72 # Surround the program with a try ... except clause.
75 # Allocate and inialize an ftdi context.
78 raise ErrorMsg('ftdi.new() failed')
80 # List all the FT230X devices.
81 nDevices, devlist = ftdi.usb_find_all(ftdic, 0x0403, 0x6015)
83 raise ErrorMsg('ftdi.usb_find_all error = %s' % ftdi.get_error_string(ftdic));
85 raise ErrorMsg('No FT230X devices found');
87 raise ErrorMsg('More than one FT230X device found');
89 # Display the identified single FT230X device.
90 ret, manufacturer, description, serial = ftdi.usb_get_strings(ftdic, devlist.dev)
92 raise ErrorMsg('ftdi.usb_get_strings error = %s' % ftdi.get_error_string(ftdic))
93 print 'manufacturer="%s" description="%s" serial="%s"' % (manufacturer, description, serial)
95 # Open the identified single FT230X device.
96 ret = ftdi.usb_open_desc(ftdic, 0x0403, 0x6015, description, serial);
98 raise ErrorMsg('ftdi.usb_open_desc error = %s' % ftdi.get_error_string(ftdic))
101 ret, chipid = ftdi.read_chipid(ftdic)
103 raise ErrorMsg('ftdi.read_chipid error = %s' % ftdi.get_error_string(ftdic))
104 print 'chip id=0x%08X' % (chipid % 2**32)
107 ret = ftdi.read_eeprom(ftdic);
109 raise ErrorMsg('ftdi.read_eeprom error = %s' % ftdi.get_error_string(ftdic))
111 # Get a read-only copy of the EEPROM
113 eeprom_size = ftdic.eeprom.size
114 ret, eeprom_buf = ftdi.get_eeprom_buf(ftdic, eeprom_size)
116 raise ErrorMsg('ftdi.get_eeprom_buf error = %s' % ftdi.get_error_string(ftdic))
117 for i in range(0,eeprom_size,16):
118 sys.stdout.write('%04x: ' % i)
120 sys.stdout.write('%02x ' % ord(eeprom_buf[i+j]))
122 sys.stdout.write(' ')
125 if 32 <= ord(x) <= 0x7E:
128 sys.stdout.write('.')
129 sys.stdout.write('\n');
131 # Read and display the EEPROM (in human readable format)
132 ret = ftdi.eeprom_decode(ftdic, 1);
134 raise ErrorMsg('ftdi.eeprom_decode error = %s' % ftdi.get_error_string(ftdic))
136 # Set the maximum power to 500mA.
137 print 'initial max_power = %dmA' % ftdic.eeprom.max_power
138 ftdic.eeprom.max_power = 500
139 print 'new max_power = %dmA' % ftdic.eeprom.max_power
141 # Set CBUS3 to DRIVE_1 (the board needs to be reworked to use PWREN# and BCD#)
142 ret, value = ftdi.get_eeprom_value(ftdic,ftdi.CBUS_FUNCTION_3)
144 raise ErrorMsg('ftdi.get_eeprom_value error = %s' % ftdi.get_error_string(ftdic))
145 print 'initial CBUS3 = %d (%s)' % (value,cbush_string(value),)
146 ret = ftdi.set_eeprom_value(ftdic,ftdi.CBUS_FUNCTION_3,ftdi.CBUSH_DRIVE1)
148 raise ErrorMsg('ftdi.set_eeprom_value error = %s' % ftdi.get_error_string(ftdic))
149 ret, value = ftdi.get_eeprom_value(ftdic,ftdi.CBUS_FUNCTION_3)
151 raise ErrorMsg('ftdi.get_eeprom_value error = %s' % ftdi.get_error_string(ftdic))
152 print 'new CBUS3 = %d (%s)' % (value,cbush_string(value),)
154 # Write the new EEPROM settings.
156 ret = ftdi.eeprom_build(ftdic)
158 raise ErrorMsg('ftdi.eeprom_build error = %s' % ftdi.get_error_string(ftdic))
159 ret = ftdi.write_eeprom(ftdic)
161 raise ErrorMsg('ftdi.write_eeprom error = %s' % ftdi.get_error_string(ftdic))
162 print 'EEPROM write succeeded'
164 print 'EEPROM write not attempted'
166 # Close the ftdi context.
167 ret = ftdi.usb_close(ftdic);
169 raise ErrorMsg('ftdi.usb_close error = %s' % ftdi.get_error_string(ftdic));
171 except ErrorMsg, msg:
172 print >> sys.stderr, 'FATAL ERROR: ' + str(msg);