You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. """Attempt detection of current chip / CPU."""
  2. import os
  3. import sys
  4. from adafruit_platformdetect.constants import chips
  5. class Chip:
  6. """Attempt detection of current chip / CPU."""
  7. def __init__(self, detector):
  8. self.detector = detector
  9. @property
  10. def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
  11. """Return a unique id for the detected chip, if any."""
  12. # There are some times we want to trick the platform detection
  13. # say if a raspberry pi doesn't have the right ID, or for testing
  14. try:
  15. return os.environ['BLINKA_FORCECHIP']
  16. except KeyError: # no forced chip, continue with testing!
  17. pass
  18. # Special cases controlled by environment var
  19. if os.environ.get('BLINKA_FT232H'):
  20. from pyftdi.usbtools import UsbTools # pylint: disable=import-error
  21. # look for it based on PID/VID
  22. count = len(UsbTools.find_all([(0x0403, 0x6014)]))
  23. if count == 0:
  24. raise RuntimeError('BLINKA_FT232H environment variable ' + \
  25. 'set, but no FT232H device found')
  26. return chips.FT232H
  27. if os.environ.get('BLINKA_MCP2221'):
  28. import hid # pylint: disable=import-error
  29. # look for it based on PID/VID
  30. for dev in hid.enumerate():
  31. if dev['vendor_id'] == 0x04D8 and dev['product_id'] == 0x00DD:
  32. return chips.MCP2221
  33. raise RuntimeError('BLINKA_MCP2221 environment variable ' + \
  34. 'set, but no MCP2221 device found')
  35. if os.environ.get('BLINKA_NOVA'):
  36. return chips.BINHO
  37. platform = sys.platform
  38. if platform in ('linux', 'linux2'):
  39. return self._linux_id()
  40. if platform == 'esp8266':
  41. return chips.ESP8266
  42. if platform == 'samd21':
  43. return chips.SAMD21
  44. if platform == 'pyboard':
  45. return chips.STM32
  46. # nothing found!
  47. return None
  48. # pylint: enable=invalid-name
  49. def _linux_id(self): # pylint: disable=too-many-branches,too-many-statements
  50. """Attempt to detect the CPU on a computer running the Linux kernel."""
  51. if self.detector.check_dt_compatible_value('qcom,apq8016'):
  52. return chips.APQ8016
  53. if self.detector.check_dt_compatible_value('fu500'):
  54. return chips.HFU540
  55. linux_id = None
  56. hardware = self.detector.get_cpuinfo_field('Hardware')
  57. if hardware is None:
  58. vendor_id = self.detector.get_cpuinfo_field('vendor_id')
  59. if vendor_id in ('GenuineIntel', 'AuthenticAMD'):
  60. linux_id = chips.GENERIC_X86
  61. compatible = self.detector.get_device_compatible()
  62. if compatible and 'tegra' in compatible:
  63. if 'cv' in compatible or 'nano' in compatible:
  64. linux_id = chips.T210
  65. elif 'quill' in compatible:
  66. linux_id = chips.T186
  67. elif 'xavier' in compatible:
  68. linux_id = chips.T194
  69. if compatible and 'imx8m' in compatible:
  70. linux_id = chips.IMX8MX
  71. if compatible and 'odroid-c2' in compatible:
  72. linux_id = chips.S905
  73. if compatible and 'amlogic, g12b' in compatible:
  74. linux_id = chips.S922X
  75. cpu_model = self.detector.get_cpuinfo_field("cpu model")
  76. if cpu_model is not None:
  77. if "MIPS 24Kc" in cpu_model:
  78. linux_id = chips.MIPS24KC
  79. elif "MIPS 24KEc" in cpu_model:
  80. linux_id = chips.MIPS24KEC
  81. # we still haven't identified the hardware, so
  82. # convert it to a list and let the remaining
  83. # conditions attempt.
  84. if not linux_id:
  85. hardware = [
  86. entry.replace('\x00', '') for entry in compatible.split(',')
  87. ]
  88. if not linux_id:
  89. if 'AM33XX' in hardware:
  90. linux_id = chips.AM33XX
  91. elif 'sun8i' in hardware:
  92. linux_id = chips.SUN8I
  93. elif 'ODROIDC' in hardware:
  94. linux_id = chips.S805
  95. elif 'ODROID-C2' in hardware:
  96. linux_id = chips.S905
  97. elif 'ODROID-N2' in hardware:
  98. linux_id = chips.S922X
  99. elif 'SAMA5' in hardware:
  100. linux_id = chips.SAMA5
  101. elif "Pinebook" in hardware:
  102. linux_id = chips.A64
  103. elif "sun50iw1p1" in hardware:
  104. linux_id = chips.A64
  105. else:
  106. if isinstance(hardware, str):
  107. if hardware in chips.BCM_RANGE:
  108. linux_id = chips.BCM2XXX
  109. elif isinstance(hardware, list):
  110. if set(hardware) & chips.BCM_RANGE:
  111. linux_id = chips.BCM2XXX
  112. return linux_id
  113. def __getattr__(self, attr):
  114. """
  115. Detect whether the given attribute is the currently-detected chip. See
  116. list of constants at the top of this module for available options.
  117. """
  118. if self.id == attr:
  119. return True
  120. return False