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.

128 lines
4.3 KiB

5 years ago
5 years ago
5 years ago
5 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. SAMA5 = "SAMA5"
  14. T210 = "T210"
  15. T186 = "T186"
  16. T194 = "T194"
  17. APQ8016 = "APQ8016"
  18. GENERIC_X86 = "GENERIC_X86"
  19. FT232H = "FT232H"
  20. HFU540 = "HFU540"
  21. class Chip:
  22. """Attempt detection of current chip / CPU."""
  23. def __init__(self, detector):
  24. self.detector = detector
  25. @property
  26. def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements
  27. """Return a unique id for the detected chip, if any."""
  28. # There are some times we want to trick the platform detection
  29. # say if a raspberry pi doesn't have the right ID, or for testing
  30. try:
  31. return os.environ['BLINKA_FORCECHIP']
  32. except KeyError: # no forced chip, continue with testing!
  33. pass
  34. # Special case, if we have an environment var set, we could use FT232H
  35. try:
  36. if os.environ['BLINKA_FT232H']:
  37. # we can't have ftdi1 as a dependency cause its wierd
  38. # to install, sigh.
  39. import ftdi1 as ftdi # pylint: disable=import-error
  40. try:
  41. ctx = None
  42. ctx = ftdi.new() # Create a libftdi context.
  43. # Enumerate FTDI devices.
  44. count, _ = ftdi.usb_find_all(ctx, 0, 0)
  45. if count < 0:
  46. raise RuntimeError('ftdi_usb_find_all returned error %d : %s' %
  47. count, ftdi.get_error_string(self._ctx))
  48. if count == 0:
  49. raise RuntimeError('BLINKA_FT232H environment variable' + \
  50. 'set, but no FT232H device found')
  51. finally:
  52. # Make sure to clean up list and context when done.
  53. if ctx is not None:
  54. ftdi.free(ctx)
  55. return FT232H
  56. except KeyError: # no FT232H environment var
  57. pass
  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. elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
  95. linux_id = BCM2XXX
  96. elif "AM33XX" in hardware:
  97. linux_id = AM33XX
  98. elif "sun8i" in hardware:
  99. linux_id = SUN8I
  100. elif "ODROIDC" in hardware:
  101. linux_id = S805
  102. elif "ODROID-C2" in hardware:
  103. linux_id = S905
  104. elif "SAMA5" in hardware:
  105. linux_id = SAMA5
  106. return linux_id
  107. def __getattr__(self, attr):
  108. """
  109. Detect whether the given attribute is the currently-detected chip. See
  110. list of constants at the top of this module for available options.
  111. """
  112. if self.id == attr:
  113. return True
  114. return False