Skip to content

Commit 9830db0

Browse files
authored
Merge pull request #8 from ladyada/master
Fix one missing Pi chip ID, add FT232H detect and overriding chip/board with envvar
2 parents 0f122a7 + 09a1fcc commit 9830db0

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

adafruit_platformdetect/board.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Detect boards."""
2+
import os
23
import adafruit_platformdetect.chip as ap_chip
34

45
# Allow for aligned constant definitions:
@@ -43,6 +44,8 @@
4344
ODROID_C1 = "ODROID_C1"
4445
ODROID_C1_PLUS = "ODROID_C1_PLUS"
4546
ODROID_C2 = "ODROID_C2"
47+
48+
FTDI_FT232H = "FT232H"
4649
# pylint: enable=bad-whitespace
4750

4851
_RASPBERRY_PI_40_PIN_IDS = (
@@ -173,6 +176,12 @@ def __init__(self, detector):
173176
@property
174177
def id(self):
175178
"""Return a unique id for the detected board, if any."""
179+
# There are some times we want to trick the platform detection
180+
# say if a raspberry pi doesn't have the right ID, or for testing
181+
try:
182+
return os.environ['BLINKA_FORCEBOARD']
183+
except KeyError: # no forced board, continue with testing!
184+
pass
176185

177186
chip_id = self.detector.chip.id
178187
board_id = None
@@ -195,7 +204,8 @@ def id(self):
195204
board_id = ODROID_C1
196205
elif chip_id == ap_chip.S905:
197206
board_id = ODROID_C2
198-
207+
elif chip_id == ap_chip.FT232H:
208+
board_id = FTDI_FT232H
199209
return board_id
200210
# pylint: enable=invalid-name
201211

adafruit_platformdetect/chip.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Attempt detection of current chip / CPU."""
22
import sys
3+
import os
34

45
AM33XX = "AM33XX"
56
BCM2XXX = "BCM2XXX"
@@ -10,16 +11,48 @@
1011
S805 = "S805"
1112
S905 = "S905"
1213
GENERIC_X86 = "GENERIC_X86"
14+
FT232H = "FT232H"
1315

1416
class Chip:
1517
"""Attempt detection of current chip / CPU."""
1618
def __init__(self, detector):
1719
self.detector = detector
1820

1921
@property
20-
# pylint: disable=invalid-name
21-
def id(self):
22+
def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
2223
"""Return a unique id for the detected chip, if any."""
24+
# There are some times we want to trick the platform detection
25+
# say if a raspberry pi doesn't have the right ID, or for testing
26+
try:
27+
return os.environ['BLINKA_FORCECHIP']
28+
except KeyError: # no forced chip, continue with testing!
29+
pass
30+
31+
# Special case, if we have an environment var set, we could use FT232H
32+
try:
33+
if os.environ['BLINKA_FT232H']:
34+
# we can't have ftdi1 as a dependency cause its wierd
35+
# to install, sigh.
36+
import ftdi1 as ftdi # pylint: disable=import-error
37+
try:
38+
ctx = None
39+
ctx = ftdi.new() # Create a libftdi context.
40+
# Enumerate FTDI devices.
41+
count, _ = ftdi.usb_find_all(ctx, 0, 0)
42+
if count < 0:
43+
raise RuntimeError('ftdi_usb_find_all returned error %d : %s' %
44+
count, ftdi.get_error_string(self._ctx))
45+
if count == 0:
46+
raise RuntimeError('BLINKA_FT232H environment variable' + \
47+
'set, but no FT232H device found')
48+
finally:
49+
# Make sure to clean up list and context when done.
50+
if ctx is not None:
51+
ftdi.free(ctx)
52+
return FT232H
53+
except KeyError: # no FT232H environment var
54+
pass
55+
2356
platform = sys.platform
2457
if platform == "linux":
2558
return self._linux_id()
@@ -29,6 +62,7 @@ def id(self):
2962
return SAMD21
3063
if platform == "pyboard":
3164
return STM32
65+
# nothing found!
3266
return None
3367
# pylint: enable=invalid-name
3468

@@ -42,7 +76,7 @@ def _linux_id(self):
4276
vendor_id = self.detector.get_cpuinfo_field("vendor_id")
4377
if vendor_id in ("GenuineIntel", "AuthenticAMD"):
4478
linux_id = GENERIC_X86
45-
elif hardware in ("BCM2708", "BCM2708", "BCM2835"):
79+
elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
4680
linux_id = BCM2XXX
4781
elif "AM33XX" in hardware:
4882
linux_id = AM33XX

0 commit comments

Comments
 (0)