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.

133 lines
4.5 KiB

4 years ago
5 years ago
5 years ago
5 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. class Chip:
  23. """Attempt detection of current chip / CPU."""
  24. def __init__(self, detector):
  25. self.detector = detector
  26. @property
  27. def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
  28. """Return a unique id for the detected chip, if any."""
  29. # There are some times we want to trick the platform detection
  30. # say if a raspberry pi doesn't have the right ID, or for testing
  31. try:
  32. return os.environ['BLINKA_FORCECHIP']
  33. except KeyError: # no forced chip, continue with testing!
  34. pass
  35. # Special case, if we have an environment var set, we could use FT232H
  36. try:
  37. if os.environ['BLINKA_FT232H']:
  38. # we can't have ftdi1 as a dependency cause its wierd
  39. # to install, sigh.
  40. import ftdi1 as ftdi # pylint: disable=import-error
  41. try:
  42. ctx = None
  43. ctx = ftdi.new() # Create a libftdi context.
  44. # Enumerate FTDI devices.
  45. count, _ = ftdi.usb_find_all(ctx, 0, 0)
  46. if count < 0:
  47. raise RuntimeError('ftdi_usb_find_all returned error %d : %s' %
  48. count, ftdi.get_error_string(self._ctx))
  49. if count == 0:
  50. raise RuntimeError('BLINKA_FT232H environment variable' + \
  51. 'set, but no FT232H device found')
  52. finally:
  53. # Make sure to clean up list and context when done.
  54. if ctx is not None:
  55. ftdi.free(ctx)
  56. return FT232H
  57. except KeyError: # no FT232H environment var
  58. pass
  59. platform = sys.platform
  60. if platform == "linux" or platform == "linux2":
  61. return self._linux_id()
  62. if platform == "esp8266":
  63. return ESP8266
  64. if platform == "samd21":
  65. return SAMD21
  66. if platform == "pyboard":
  67. return STM32
  68. # nothing found!
  69. return None
  70. # pylint: enable=invalid-name
  71. def _linux_id(self): # pylint: disable=too-many-branches
  72. """Attempt to detect the CPU on a computer running the Linux kernel."""
  73. if self.detector.check_dt_compatible_value("qcom,apq8016"):
  74. return APQ8016
  75. if self.detector.check_dt_compatible_value("fu500"):
  76. return HFU540
  77. linux_id = None
  78. hardware = self.detector.get_cpuinfo_field("Hardware")
  79. if hardware is None:
  80. vendor_id = self.detector.get_cpuinfo_field("vendor_id")
  81. if vendor_id in ("GenuineIntel", "AuthenticAMD"):
  82. linux_id = GENERIC_X86
  83. compatible = self.detector.get_device_compatible()
  84. if compatible and 'tegra' in compatible:
  85. if 'cv' in compatible or 'nano' in compatible:
  86. linux_id = T210
  87. elif 'quill' in compatible:
  88. linux_id = T186
  89. elif 'xavier' in compatible:
  90. linux_id = T194
  91. if compatible and 'imx8m' in compatible:
  92. linux_id = IMX8MX
  93. if compatible and 'odroid-c2' in compatible:
  94. linux_id = S905
  95. if compatible and 'amlogic, g12b' in compatible:
  96. linux_id = S922X
  97. elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
  98. linux_id = BCM2XXX
  99. elif "AM33XX" in hardware:
  100. linux_id = AM33XX
  101. elif "sun8i" in hardware:
  102. linux_id = SUN8I
  103. elif "ODROIDC" in hardware:
  104. linux_id = S805
  105. elif "ODROID-C2" in hardware:
  106. linux_id = S905
  107. elif "ODROID-N2" in hardware:
  108. linux_id = S922X
  109. elif "SAMA5" in hardware:
  110. linux_id = SAMA5
  111. return linux_id
  112. def __getattr__(self, attr):
  113. """
  114. Detect whether the given attribute is the currently-detected chip. See
  115. list of constants at the top of this module for available options.
  116. """
  117. if self.id == attr:
  118. return True
  119. return False