@@ -94,7 +94,13 @@ def use_FT232H():
94
94
atexit .register (enable_FTDI_driver )
95
95
96
96
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
+
98
104
def __init__ (self , vid = FT232H_VID , pid = FT232H_PID ):
99
105
"""Create a FT232H object. Will search for the first available FT232H
100
106
device with the specified USB vendor ID and product ID (defaults to
@@ -268,6 +274,13 @@ def mpsse_write_gpio(self):
268
274
"""Write the current MPSSE GPIO state to the FT232H chip."""
269
275
self ._write (self .mpsse_gpio ())
270
276
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
+
271
284
# GPIO functions below:
272
285
273
286
def _setup_pin (self , pin , mode ):
@@ -336,12 +349,26 @@ def input(self, pin):
336
349
337
350
338
351
class 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 ):
340
353
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 )
342
361
self .set_mode (mode )
343
362
self .set_bit_order (bitorder )
344
363
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
+
345
372
def set_clock_hz (self , hz ):
346
373
"""Set the speed of the SPI clock in hertz. Note that not all speeds
347
374
are supported and a lower speed might be chosen by the hardware.
@@ -407,10 +434,12 @@ def write(self, data):
407
434
length = len (data )- 1
408
435
len_low = length & 0xFF
409
436
len_high = (length >> 8 ) & 0xFF
437
+ self ._assert_cs ()
410
438
# Send command and length.
411
439
self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
412
440
# Send data.
413
441
self ._ft232h ._write (str (bytearray (data )))
442
+ self ._deassert_cs ()
414
443
415
444
def read (self , length ):
416
445
"""Half-duplex SPI read. The specified length of bytes will be clocked
@@ -424,8 +453,10 @@ def read(self, length):
424
453
# considers 0 a length of 1 and FFFF a length of 65536
425
454
len_low = (length - 1 ) & 0xFF
426
455
len_high = ((length - 1 ) >> 8 ) & 0xFF
456
+ self ._assert_cs ()
427
457
# 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 ()
429
460
# Read response bytes.
430
461
return bytearray (self ._ft232h ._poll_read (length ))
431
462
@@ -444,9 +475,11 @@ def transfer(self, data):
444
475
len_low = (length - 1 ) & 0xFF
445
476
len_high = ((length - 1 ) >> 8 ) & 0xFF
446
477
# Send command and length.
478
+ self ._assert_cs ()
447
479
self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
448
480
self ._ft232h ._write (str (bytearray (data )))
449
481
self ._ft232h ._write ('\x87 ' )
482
+ self ._deassert_cs ()
450
483
# Read response bytes.
451
484
return bytearray (self ._ft232h ._poll_read (length ))
452
485
0 commit comments