Skip to content

Commit 34ddceb

Browse files
committed
Set maximum SPI speed for MCP23S17
The linux-raspberrypi kernel update to 4.9.43 included a change to the default reported max speed of the linux spidev, from 100KHz to 125MHz. kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz. See raspberrypi/linux#2165. It appears the reported max speed is used as the default if you don't specify it for the device. The MCP23S17 chip used in the PiFace digital board can only handle 10MHz but we were using the default so it was not working after the recent kernel update. This change explicitly sets the maximum speed after opening the device.
1 parent 4dc7216 commit 34ddceb

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pifaceio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
class _SPIdev(object):
2424
'Class to package 3 byte write + read transfers to spi device'
25-
def __init__(self, devname):
25+
def __init__(self, devname, speed):
2626
'Constructor'
2727
self.fp = open(devname, 'r+b', buffering=0)
2828
self.fn = self.fp.fileno()
2929
self.count = 0
30+
# Set the max speed
31+
fcntl.ioctl(self.fn, 0x40046B04, struct.pack('L', speed))
3032

3133
def write(self, tx):
3234
'Write given transfer "tx" to device'
@@ -62,7 +64,8 @@ class PiFace(object):
6264
_spi = {}
6365

6466
def __init__(self, board=0, pull_ups=0xff, read_polarity=0x00,
65-
write_polarity=0xff, init_board=True, bus=0, chip=0):
67+
write_polarity=0xff, init_board=True, bus=0, chip=0,
68+
speed=int(10e6)):
6669
'''
6770
PiFace board constructor.
6871
board: Piface board number = 0 to 7, default = 0.
@@ -71,6 +74,7 @@ def __init__(self, board=0, pull_ups=0xff, read_polarity=0x00,
7174
init_board: Set False to inhibit board initialisaion. Default is True.
7275
bus: SPI bus = 0 or 1, default = 0.
7376
chip: SPI chip select = 0 or 1, default = 0
77+
speed: in Hz. Default is MCP23S17 max speed of 10MHz.
7478
7579
Note that bus 0, chip 0 corresponds to /dev/spidev0.0, etc.
7680
@@ -86,7 +90,7 @@ def __init__(self, board=0, pull_ups=0xff, read_polarity=0x00,
8690
self.busaddr = (bus, chip)
8791
if self.busaddr not in PiFace._spi:
8892
PiFace._spi[self.busaddr] = \
89-
_SPIdev('/dev/spidev%d.%d' % self.busaddr)
93+
_SPIdev('/dev/spidev%d.%d' % self.busaddr, speed)
9094

9195
self.spi = PiFace._spi[self.busaddr]
9296
self.spi.count += 1

0 commit comments

Comments
 (0)