Skip to content

RFC: Attempt To Recognize Raspberry Pi Running Non-Raspbian OS #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions adafruit_platformdetect/board.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Detect boards."""
import os
import re
import adafruit_platformdetect.chip as ap_chip

# Allow for aligned constant definitions:
Expand Down Expand Up @@ -367,6 +368,30 @@ def _pi_id(self):
for model, codes in _PI_REV_CODES.items():
if pi_rev_code in codes:
return model

# We may be on a non-Raspbian OS, so try to lazily determine
# the version based on `get_device_model`
else:
pi_model = self.detector.get_device_model()
if pi_model:
pi_model = pi_model.upper().replace(' ', '_')
if "PLUS" in pi_model:
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)(PLUS)',
pi_model)
elif "CM" in pi_model: # untested for Compute Module
re_model = re.search(r'(RASPBERRY_PI_CM)(\d)',
pi_model)
else: # untested for non-plus models
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)',
pi_model)

if re_model:
pi_model = "".join(re_model.groups())
available_models = _PI_REV_CODES.keys()
for model in available_models:
if model == pi_model:
return model

return None

def _pi_rev_code(self):
Expand Down
63 changes: 40 additions & 23 deletions adafruit_platformdetect/chip.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
MIPS24KC = "MIPS24KC"
MIPS24KEC = "MIPS24KEC"

BCM_RANGE = {'BCM2708', 'BCM2709', 'BCM2835', 'BCM2837', 'bcm2708', 'bcm2709',
'bcm2835', 'bcm2837'}

class Chip:
"""Attempt detection of current chip / CPU."""
def __init__(self, detector):
Expand Down Expand Up @@ -61,13 +64,13 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s
return BINHO

platform = sys.platform
if platform == "linux" or platform == "linux2":
if platform in ('linux', 'linux2'):
return self._linux_id()
if platform == "esp8266":
if platform == 'esp8266':
return ESP8266
if platform == "samd21":
if platform == 'samd21':
return SAMD21
if platform == "pyboard":
if platform == 'pyboard':
return STM32
# nothing found!
return None
Expand All @@ -76,18 +79,18 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s
def _linux_id(self): # pylint: disable=too-many-branches
"""Attempt to detect the CPU on a computer running the Linux kernel."""

if self.detector.check_dt_compatible_value("qcom,apq8016"):
if self.detector.check_dt_compatible_value('qcom,apq8016'):
return APQ8016

if self.detector.check_dt_compatible_value("fu500"):
if self.detector.check_dt_compatible_value('fu500'):
return HFU540

linux_id = None
hardware = self.detector.get_cpuinfo_field("Hardware")
hardware = self.detector.get_cpuinfo_field('Hardware')

if hardware is None:
vendor_id = self.detector.get_cpuinfo_field("vendor_id")
if vendor_id in ("GenuineIntel", "AuthenticAMD"):
vendor_id = self.detector.get_cpuinfo_field('vendor_id')
if vendor_id in ('GenuineIntel', 'AuthenticAMD'):
linux_id = GENERIC_X86

compatible = self.detector.get_device_compatible()
Expand All @@ -112,20 +115,34 @@ def _linux_id(self): # pylint: disable=too-many-branches
elif "MIPS 24KEc" in cpu_model:
linux_id = MIPS24KEC

elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
linux_id = BCM2XXX
elif "AM33XX" in hardware:
linux_id = AM33XX
elif "sun8i" in hardware:
linux_id = SUN8I
elif "ODROIDC" in hardware:
linux_id = S805
elif "ODROID-C2" in hardware:
linux_id = S905
elif "ODROID-N2" in hardware:
linux_id = S922X
elif "SAMA5" in hardware:
linux_id = SAMA5
# we still haven't identified the hardware, so
# convert it to a list and let the remaining
# conditions attempt.
if not linux_id:
hardware = [
entry.replace('\x00', '') for entry in compatible.split(',')
]

if not linux_id:
if 'AM33XX' in hardware:
linux_id = AM33XX
elif 'sun8i' in hardware:
linux_id = SUN8I
elif 'ODROIDC' in hardware:
linux_id = S805
elif 'ODROID-C2' in hardware:
linux_id = S905
elif 'ODROID-N2' in hardware:
linux_id = S922X
elif 'SAMA5' in hardware:
linux_id = SAMA5
else:
if isinstance(hardware, str):
if hardware in BCM_RANGE:
linux_id = BCM2XXX
elif isinstance(hardware, list):
if set(hardware) & BCM_RANGE:
linux_id = BCM2XXX

return linux_id

Expand Down