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.

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