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.

139 lines
4.7 KiB

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