Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f04bede

Browse files
committed
Refactor I2C and SPI to support FT232H as I2C and SPI master. No changes to existing I2C and SPI interface.
1 parent be1d242 commit f04bede

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

Adafruit_GPIO/FT232H.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

338351
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):
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

Adafruit_GPIO/I2C.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ def get_default_bus():
5656
else:
5757
raise RuntimeError('Could not determine default I2C bus for platform.')
5858

59+
def get_i2c_device(address, busnum=get_default_bus(), **kwargs):
60+
"""Return an I2C device for the specified address and on the specified bus.
61+
If busnum isn't specified, the default I2C bus for the platform will attempt
62+
to be detected.
63+
"""
64+
return Device(address, busnum, **kwargs)
65+
5966

6067
class Device(object):
6168
"""Class for communicating with an I2C device using the smbus library.

Adafruit_GPIO/MCP230xx.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ class MCP230xxBase(GPIO.BaseGPIO):
3030
class for interacting with device.
3131
"""
3232

33-
def __init__(self, address, busnum=I2C.get_default_bus()):
33+
def __init__(self, address, i2c=None):
3434
"""Initialize MCP230xx at specified I2C address and bus number. If bus
3535
is not specified it will default to the appropriate platform detected bus.
3636
"""
37-
self._i2c = I2C.Device(address, busnum)
37+
# Create I2C device.
38+
if i2c is None:
39+
import Adafruit_GPIO.I2C as I2C
40+
i2c = I2C
41+
self._device = i2c.get_i2c_device(address, **kwargs)
3842
# Assume starting in ICON.BANK = 0 mode (sequential access).
3943
# Compute how many bytes are needed to store count of GPIO.
4044
self.gpio_bytes = int(math.ceil(self.NUM_GPIO/8.0))
@@ -98,7 +102,7 @@ def input(self, pin):
98102
"""
99103
self._validate_pin(pin)
100104
# Get GPIO state.
101-
gpio = self._i2c.readList(self.GPIO, self.gpio_bytes)
105+
gpio = self._device.readList(self.GPIO, self.gpio_bytes)
102106
# Return True if pin's bit is set.
103107
return (gpio[int(pin/8)] & 1 << (int(pin%8))) > 0
104108

@@ -119,23 +123,23 @@ def write_gpio(self, gpio=None):
119123
"""
120124
if gpio is not None:
121125
self.gpio = gpio
122-
self._i2c.writeList(self.GPIO, self.gpio)
126+
self._device.writeList(self.GPIO, self.gpio)
123127

124128
def write_iodir(self, iodir=None):
125129
"""Write the specified byte value to the IODIR registor. If no value
126130
specified the current buffered value will be written.
127131
"""
128132
if iodir is not None:
129133
self.iodir = iodir
130-
self._i2c.writeList(self.IODIR, self.iodir)
134+
self._device.writeList(self.IODIR, self.iodir)
131135

132136
def write_gppu(self, gppu=None):
133137
"""Write the specified byte value to the GPPU registor. If no value
134138
specified the current buffered value will be written.
135139
"""
136140
if gppu is not None:
137141
self.gppu = gppu
138-
self._i2c.writeList(self.GPPU, self.gppu)
142+
self._device.writeList(self.GPPU, self.gppu)
139143

140144

141145
class MCP23017(MCP230xxBase):

Adafruit_GPIO/SPI.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import operator
2323
import time
2424

25-
import spidev
26-
2725
import Adafruit_GPIO as GPIO
2826

2927

@@ -39,6 +37,7 @@ def __init__(self, port, device, max_speed_hz=500000):
3937
identify the device, for example the device /dev/spidev1.0 would be port
4038
1 and device 0.
4139
"""
40+
import spidev
4241
self._device = spidev.SpiDev()
4342
self._device.open(port, device)
4443
self._device.max_speed_hz=max_speed_hz

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
requires.append('spidev')
1212

1313
setup(name = 'Adafruit_GPIO',
14-
version = '0.6.1',
14+
version = '0.6.5',
1515
author = 'Tony DiCola',
1616
author_email = '[email protected]',
1717
description = 'Library to provide a cross-platform GPIO interface on the Raspberry Pi and Beaglebone Black using the RPi.GPIO and Adafruit_BBIO libraries.',

0 commit comments

Comments
 (0)