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.

59 lines
1.7 KiB

  1. """Attempt detection of current chip / CPU."""
  2. import sys
  3. AM33XX = "AM33XX"
  4. BCM2XXX = "BCM2XXX"
  5. ESP8266 = "ESP8266"
  6. SAMD21 = "SAMD21"
  7. STM32 = "STM32"
  8. SUN8I = "SUN8I"
  9. GENERIC_X86 = "GENERIC_X86"
  10. class Chip:
  11. """Attempt detection of current chip / CPU."""
  12. def __init__(self, detector):
  13. self.detector = detector
  14. @property
  15. # pylint: disable=invalid-name
  16. def id(self):
  17. """Return a unique id for the detected chip, if any."""
  18. platform = sys.platform
  19. if platform == "linux":
  20. return self._linux_id()
  21. if platform == "esp8266":
  22. return ESP8266
  23. if platform == "samd21":
  24. return SAMD21
  25. if platform == "pyboard":
  26. return STM32
  27. return None
  28. # pylint: enable=invalid-name
  29. def _linux_id(self):
  30. """Attempt to detect the CPU on a computer running the Linux kernel."""
  31. linux_id = None
  32. hardware = self.detector.get_cpuinfo_field("Hardware")
  33. if hardware is None:
  34. vendor_id = self.detector.get_cpuinfo_field("vendor_id")
  35. if vendor_id in ("GenuineIntel", "AuthenticAMD"):
  36. linux_id = GENERIC_X86
  37. elif hardware in ("BCM2708", "BCM2708", "BCM2835"):
  38. linux_id = BCM2XXX
  39. elif "AM33XX" in hardware:
  40. linux_id = AM33XX
  41. elif "sun8i" in hardware:
  42. linux_id = SUN8I
  43. return linux_id
  44. def __getattr__(self, attr):
  45. """
  46. Detect whether the given attribute is the currently-detected chip. See
  47. list of constants at the top of this module for available options.
  48. """
  49. if self.id == attr:
  50. return True
  51. return False