Skip to content

Commit 60b73ff

Browse files
authored
Merge pull request #4 from jerryneedell/jerryn_fixspi
Add SPI mode switch if init fails
2 parents 1905220 + 10d315c commit 60b73ff

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

adafruit_stmpe610.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ class Adafruit_STMPE610:
135135
A driver for the STMPE610 Resistive Touch sensor.
136136
"""
137137
def __init__(self):
138-
"""Check the STMPE610 was found"""
139-
# Check device version.
140-
version = self.get_version
141-
if _STMPE_VERSION != version:
142-
raise RuntimeError('Failed to find STMPE610! Chip Version 0x%x' % version)
138+
"""Reset the controller"""
143139
self._write_register_byte(_STMPE_SYS_CTRL1, _STMPE_SYS_CTRL1_RESET)
144140
time.sleep(.001)
145141

@@ -257,6 +253,10 @@ def __init__(self, i2c, address=_STMPE_ADDR):
257253
"""
258254
import adafruit_bus_device.i2c_device as i2c_device
259255
self._i2c = i2c_device.I2CDevice(i2c, address)
256+
# Check device version.
257+
version = self.get_version
258+
if _STMPE_VERSION != version:
259+
raise RuntimeError('Failed to find STMPE610! Chip Version 0x%x' % version)
260260
super().__init__()
261261

262262
def _read_register(self, register, length):
@@ -278,12 +278,20 @@ class Adafruit_STMPE610_SPI(Adafruit_STMPE610):
278278
"""
279279
SPI driver for the STMPE610 Resistive Touch sensor.
280280
"""
281-
def __init__(self, spi, cs, baudrate=100000):
281+
def __init__(self, spi, cs, baudrate=1000000):
282282
"""
283-
Check the STMPE610 was found,Default clock rate is 100000 but can be changed with 'baudrate'
283+
Check the STMPE610 was found,Default clock rate 1000000 - can be changed with 'baudrate'
284284
"""
285285
import adafruit_bus_device.spi_device as spi_device
286286
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
287+
# Check device version.
288+
version = self.get_version
289+
if _STMPE_VERSION != version:
290+
# if it fails try SPI MODE 1 -- that is what Arduino does
291+
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=1)
292+
version = self.get_version
293+
if _STMPE_VERSION != version:
294+
raise RuntimeError('Failed to find STMPE610! Chip Version 0x%x' % version)
287295
super().__init__()
288296

289297
def _read_register(self, register, length):

0 commit comments

Comments
 (0)