Skip to content

Commit 87b7a2e

Browse files
committed
# Conflicts: # adafruit_platformdetect/chip.py
2 parents ad3a44f + eab68f0 commit 87b7a2e

File tree

2 files changed

+89
-43
lines changed

2 files changed

+89
-43
lines changed

adafruit_platformdetect/board.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Detect boards."""
22
import os
3+
import re
4+
35
import adafruit_platformdetect.chip as ap_chip
46

57
# Allow for aligned constant definitions:
@@ -252,44 +254,44 @@
252254
),
253255
RASPBERRY_PI_ZERO: (
254256
'900092', '920092', '900093', '920093',
255-
'1900092', '1920092', '1900093', '1920093', # warranty bit 24
256-
'2900092', '2920092', '2900093', '2920093', # warranty bit 25
257+
'1900092', '1920092', '1900093', '1920093', # warranty bit 24
258+
'2900092', '2920092', '2900093', '2920093', # warranty bit 25
257259
),
258260
RASPBERRY_PI_ZERO_W: (
259261
'9000c1',
260-
'19000c1', '29000c1', # warranty bits
262+
'19000c1', '29000c1', # warranty bits
261263
),
262264
RASPBERRY_PI_2B: (
263265
'a01040', 'a01041', 'a21041', 'a22042',
264-
'1a01040', '1a01041', '1a21041', '1a22042', # warranty bit 24
265-
'2a01040', '2a01041', '2a21041', '2a22042', # warranty bit 25
266+
'1a01040', '1a01041', '1a21041', '1a22042', # warranty bit 24
267+
'2a01040', '2a01041', '2a21041', '2a22042', # warranty bit 25
266268
),
267269
RASPBERRY_PI_3B: (
268270
'a02082', 'a22082', 'a32082', 'a52082',
269-
'1a02082', '1a22082', '1a32082', '1a52082', # warranty bit 24
270-
'2a02082', '2a22082', '2a32082', '2a52082', # warranty bit 25
271+
'1a02082', '1a22082', '1a32082', '1a52082', # warranty bit 24
272+
'2a02082', '2a22082', '2a32082', '2a52082', # warranty bit 25
271273
),
272274
RASPBERRY_PI_3B_PLUS: (
273275
'a020d3',
274-
'1a020d3', '2a020d3', # warranty bits
276+
'1a020d3', '2a020d3', # warranty bits
275277
),
276278
RASPBERRY_PI_CM3: (
277279
'a020a0', 'a220a0',
278-
'1a020a0', '2a020a0', # warranty bits
280+
'1a020a0', '2a020a0', # warranty bits
279281
'1a220a0', '2a220a0',
280282
),
281283
RASPBERRY_PI_3A_PLUS: (
282284
'9020e0',
283-
'19020e0', '29020e0', # warranty bits
285+
'19020e0', '29020e0', # warranty bits
284286
),
285287
RASPBERRY_PI_CM3_PLUS: (
286288
'a02100',
287-
'1a02100', '2a02100', # warranty bits
289+
'1a02100', '2a02100', # warranty bits
288290
),
289291
RASPBERRY_PI_4B: (
290292
'a03111', 'b03111', 'c03111',
291293
'a03112', 'b03112', 'c03112',
292-
'1a03111', '2a03111', '1b03111', '2b03111', # warranty bits
294+
'1a03111', '2a03111', '1b03111', '2b03111', # warranty bits
293295
'1c03111', '2c03111', '1a03112', '2a03112',
294296
'1b03112', '2b03112', '1c03112', '2c03112',
295297
),
@@ -380,6 +382,30 @@ def _pi_id(self):
380382
for model, codes in _PI_REV_CODES.items():
381383
if pi_rev_code in codes:
382384
return model
385+
386+
# We may be on a non-Raspbian OS, so try to lazily determine
387+
# the version based on `get_device_model`
388+
else:
389+
pi_model = self.detector.get_device_model()
390+
if pi_model:
391+
pi_model = pi_model.upper().replace(' ', '_')
392+
if "PLUS" in pi_model:
393+
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)(PLUS)',
394+
pi_model)
395+
elif "CM" in pi_model: # untested for Compute Module
396+
re_model = re.search(r'(RASPBERRY_PI_CM)(\d)',
397+
pi_model)
398+
else: # untested for non-plus models
399+
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)',
400+
pi_model)
401+
402+
if re_model:
403+
pi_model = "".join(re_model.groups())
404+
available_models = _PI_REV_CODES.keys()
405+
for model in available_models:
406+
if model == pi_model:
407+
return model
408+
383409
return None
384410

385411
def _pi_rev_code(self):

adafruit_platformdetect/chip.py

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Attempt detection of current chip / CPU."""
2-
import sys
32
import os
3+
import sys
44

55
AM33XX = "AM33XX"
66
IMX8MX = "IMX8MX"
@@ -26,32 +26,37 @@
2626
MIPS24KEC = "MIPS24KEC"
2727
A64 = "A64"
2828

29+
BCM_RANGE = {'BCM2708', 'BCM2709', 'BCM2835', 'BCM2837', 'bcm2708', 'bcm2709',
30+
'bcm2835', 'bcm2837'}
31+
32+
2933
class Chip:
3034
"""Attempt detection of current chip / CPU."""
35+
3136
def __init__(self, detector):
3237
self.detector = detector
3338

3439
@property
35-
def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
40+
def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
3641
"""Return a unique id for the detected chip, if any."""
3742
# There are some times we want to trick the platform detection
3843
# say if a raspberry pi doesn't have the right ID, or for testing
3944
try:
4045
return os.environ['BLINKA_FORCECHIP']
41-
except KeyError: # no forced chip, continue with testing!
46+
except KeyError: # no forced chip, continue with testing!
4247
pass
4348

4449
# Special cases controlled by environment var
4550
if os.environ.get('BLINKA_FT232H'):
46-
from pyftdi.usbtools import UsbTools # pylint: disable=import-error
51+
from pyftdi.usbtools import UsbTools # pylint: disable=import-error
4752
# look for it based on PID/VID
4853
count = len(UsbTools.find_all([(0x0403, 0x6014)]))
4954
if count == 0:
5055
raise RuntimeError('BLINKA_FT232H environment variable ' + \
5156
'set, but no FT232H device found')
5257
return FT232H
5358
if os.environ.get('BLINKA_MCP2221'):
54-
import hid # pylint: disable=import-error
59+
import hid # pylint: disable=import-error
5560
# look for it based on PID/VID
5661
for dev in hid.enumerate():
5762
if dev['vendor_id'] == 0x04D8 and dev['product_id'] == 0x00DD:
@@ -62,33 +67,34 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s
6267
return BINHO
6368

6469
platform = sys.platform
65-
if platform == "linux" or platform == "linux2":
70+
if platform in ('linux', 'linux2'):
6671
return self._linux_id()
67-
if platform == "esp8266":
72+
if platform == 'esp8266':
6873
return ESP8266
69-
if platform == "samd21":
74+
if platform == 'samd21':
7075
return SAMD21
71-
if platform == "pyboard":
76+
if platform == 'pyboard':
7277
return STM32
7378
# nothing found!
7479
return None
80+
7581
# pylint: enable=invalid-name
7682

77-
def _linux_id(self): # pylint: disable=too-many-branches
83+
def _linux_id(self): # pylint: disable=too-many-branches
7884
"""Attempt to detect the CPU on a computer running the Linux kernel."""
7985

80-
if self.detector.check_dt_compatible_value("qcom,apq8016"):
86+
if self.detector.check_dt_compatible_value('qcom,apq8016'):
8187
return APQ8016
8288

83-
if self.detector.check_dt_compatible_value("fu500"):
89+
if self.detector.check_dt_compatible_value('fu500'):
8490
return HFU540
8591

8692
linux_id = None
87-
hardware = self.detector.get_cpuinfo_field("Hardware")
93+
hardware = self.detector.get_cpuinfo_field('Hardware')
8894

8995
if hardware is None:
90-
vendor_id = self.detector.get_cpuinfo_field("vendor_id")
91-
if vendor_id in ("GenuineIntel", "AuthenticAMD"):
96+
vendor_id = self.detector.get_cpuinfo_field('vendor_id')
97+
if vendor_id in ('GenuineIntel', 'AuthenticAMD'):
9298
linux_id = GENERIC_X86
9399

94100
compatible = self.detector.get_device_compatible()
@@ -114,22 +120,36 @@ def _linux_id(self): # pylint: disable=too-many-branches
114120
elif "MIPS 24KEc" in cpu_model:
115121
linux_id = MIPS24KEC
116122

117-
elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
118-
linux_id = BCM2XXX
119-
elif "AM33XX" in hardware:
120-
linux_id = AM33XX
121-
elif "sun8i" in hardware:
122-
linux_id = SUN8I
123-
elif "ODROIDC" in hardware:
124-
linux_id = S805
125-
elif "ODROID-C2" in hardware:
126-
linux_id = S905
127-
elif "ODROID-N2" in hardware:
128-
linux_id = S922X
129-
elif "SAMA5" in hardware:
130-
linux_id = SAMA5
131-
elif "Pinebook" in hardware:
132-
linux_id = A64
123+
# we still haven't identified the hardware, so
124+
# convert it to a list and let the remaining
125+
# conditions attempt.
126+
if not linux_id:
127+
hardware = [
128+
entry.replace('\x00', '') for entry in compatible.split(',')
129+
]
130+
131+
if not linux_id:
132+
if 'AM33XX' in hardware:
133+
linux_id = AM33XX
134+
elif 'sun8i' in hardware:
135+
linux_id = SUN8I
136+
elif 'ODROIDC' in hardware:
137+
linux_id = S805
138+
elif 'ODROID-C2' in hardware:
139+
linux_id = S905
140+
elif 'ODROID-N2' in hardware:
141+
linux_id = S922X
142+
elif 'SAMA5' in hardware:
143+
linux_id = SAMA5
144+
elif "Pinebook" in hardware:
145+
linux_id = A64
146+
else:
147+
if isinstance(hardware, str):
148+
if hardware in BCM_RANGE:
149+
linux_id = BCM2XXX
150+
elif isinstance(hardware, list):
151+
if set(hardware) & BCM_RANGE:
152+
linux_id = BCM2XXX
133153

134154
return linux_id
135155

0 commit comments

Comments
 (0)