@@ -94,7 +94,13 @@ def use_FT232H():
9494 atexit .register (enable_FTDI_driver )
9595
9696
97- class FT232H (object ):
97+ class FT232H (GPIO .BaseGPIO ):
98+ # Make GPIO constants that match main GPIO class for compatibility.
99+ HIGH = GPIO .HIGH
100+ LOW = GPIO .LOW
101+ IN = GPIO .IN
102+ OUT = GPIO .OUT
103+
98104 def __init__ (self , vid = FT232H_VID , pid = FT232H_PID ):
99105 """Create a FT232H object. Will search for the first available FT232H
100106 device with the specified USB vendor ID and product ID (defaults to
@@ -268,6 +274,13 @@ def mpsse_write_gpio(self):
268274 """Write the current MPSSE GPIO state to the FT232H chip."""
269275 self ._write (self .mpsse_gpio ())
270276
277+ def get_i2c_device (self , address , ** kwargs ):
278+ """Return an I2CDevice instance using this FT232H object and the provided
279+ I2C address. Meant to be passed as the i2c_provider parameter to objects
280+ which use the Adafruit_Python_GPIO library for I2C.
281+ """
282+ return I2CDevice (self , address , ** kwargs )
283+
271284 # GPIO functions below:
272285
273286 def _setup_pin (self , pin , mode ):
@@ -336,12 +349,26 @@ def input(self, pin):
336349
337350
338351class SPI (object ):
339- def __init__ (self , ft232h , clock_hz , mode = 0 , bitorder = MSBFIRST ):
352+ def __init__ (self , ft232h , cs = None , max_speed_hz = 1000000 , mode = 0 , bitorder = MSBFIRST ):
340353 self ._ft232h = ft232h
341- self .set_clock_hz (clock_hz )
354+ # Initialize chip select pin if provided to output high.
355+ if cs is not None :
356+ ft232h .setup (cs , GPIO .OUT )
357+ ft232h .set_high (cs )
358+ self ._cs = cs
359+ # Initialize clock, mode, and bit order.
360+ self .set_clock_hz (max_speed_hz )
342361 self .set_mode (mode )
343362 self .set_bit_order (bitorder )
344363
364+ def _assert_cs (self ):
365+ if self ._cs is not None :
366+ self ._ft232h .set_low (self ._cs )
367+
368+ def _deassert_cs (self ):
369+ if self ._cs is not None :
370+ self ._ft232h .set_high (self ._cs )
371+
345372 def set_clock_hz (self , hz ):
346373 """Set the speed of the SPI clock in hertz. Note that not all speeds
347374 are supported and a lower speed might be chosen by the hardware.
@@ -407,10 +434,12 @@ def write(self, data):
407434 length = len (data )- 1
408435 len_low = length & 0xFF
409436 len_high = (length >> 8 ) & 0xFF
437+ self ._assert_cs ()
410438 # Send command and length.
411439 self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
412440 # Send data.
413441 self ._ft232h ._write (str (bytearray (data )))
442+ self ._deassert_cs ()
414443
415444 def read (self , length ):
416445 """Half-duplex SPI read. The specified length of bytes will be clocked
@@ -424,8 +453,10 @@ def read(self, length):
424453 # considers 0 a length of 1 and FFFF a length of 65536
425454 len_low = (length - 1 ) & 0xFF
426455 len_high = ((length - 1 ) >> 8 ) & 0xFF
456+ self ._assert_cs ()
427457 # Send command and length.
428- self ._ft232h ._write (str (bytearray (command , len_low , len_high , 0x87 )))
458+ self ._ft232h ._write (str (bytearray ((command , len_low , len_high , 0x87 ))))
459+ self ._deassert_cs ()
429460 # Read response bytes.
430461 return bytearray (self ._ft232h ._poll_read (length ))
431462
@@ -444,9 +475,11 @@ def transfer(self, data):
444475 len_low = (length - 1 ) & 0xFF
445476 len_high = ((length - 1 ) >> 8 ) & 0xFF
446477 # Send command and length.
478+ self ._assert_cs ()
447479 self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
448480 self ._ft232h ._write (str (bytearray (data )))
449481 self ._ft232h ._write ('\x87 ' )
482+ self ._deassert_cs ()
450483 # Read response bytes.
451484 return bytearray (self ._ft232h ._poll_read (length ))
452485
0 commit comments