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.

493 lines
15 KiB

5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
  1. """Detect boards."""
  2. import os
  3. import adafruit_platformdetect.chip as ap_chip
  4. # Allow for aligned constant definitions:
  5. # pylint: disable=bad-whitespace
  6. BEAGLEBONE = 'BEAGLEBONE'
  7. BEAGLEBONE_BLACK = 'BEAGLEBONE_BLACK'
  8. BEAGLEBONE_BLUE = 'BEAGLEBONE_BLUE'
  9. BEAGLEBONE_BLACK_WIRELESS = 'BEAGLEBONE_BLACK_WIRELESS'
  10. BEAGLEBONE_POCKETBEAGLE = 'BEAGLEBONE_POCKETBEAGLE'
  11. BEAGLEBONE_GREEN = 'BEAGLEBONE_GREEN'
  12. BEAGLEBONE_GREEN_WIRELESS = 'BEAGLEBONE_GREEN_WIRELESS'
  13. BEAGLEBONE_BLACK_INDUSTRIAL = 'BEAGLEBONE_BLACK_INDUSTRIAL'
  14. BEAGLEBONE_ENHANCED = 'BEAGLEBONE_ENHANCED'
  15. BEAGLEBONE_USOMIQ = 'BEAGLEBONE_USOMIQ'
  16. BEAGLEBONE_AIR = 'BEAGLEBONE_AIR'
  17. BEAGLEBONE_POCKETBONE = 'BEAGLEBONE_POCKETBONE'
  18. BEAGLELOGIC_STANDALONE = 'BEAGLELOGIC_STANDALONE'
  19. OSD3358_DEV_BOARD = 'OSD3358_DEV_BOARD'
  20. OSD3358_SM_RED = 'OSD3358_SM_RED'
  21. FEATHER_HUZZAH = "FEATHER_HUZZAH"
  22. FEATHER_M0_EXPRESS = "FEATHER_M0_EXPRESS"
  23. GENERIC_LINUX_PC = "GENERIC_LINUX_PC"
  24. PYBOARD = "PYBOARD"
  25. NODEMCU = "NODEMCU"
  26. ORANGE_PI_PC = "ORANGE_PI_PC"
  27. ORANGE_PI_R1 = "ORANGE_PI_R1"
  28. GIANT_BOARD = "GIANT_BOARD"
  29. # NVIDIA Jetson boards
  30. JETSON_TX1 = 'JETSON_TX1'
  31. JETSON_TX2 = 'JETSON_TX2'
  32. JETSON_XAVIER = 'JETSON_XAVIER'
  33. JETSON_NANO = 'JETSON_NANO'
  34. # Google Coral dev board
  35. CORAL_EDGE_TPU_DEV = "CORAL_EDGE_TPU_DEV"
  36. # Various Raspberry Pi models
  37. RASPBERRY_PI_B_REV1 = "RASPBERRY_PI_B_REV1"
  38. RASPBERRY_PI_B_REV2 = "RASPBERRY_PI_B_REV2"
  39. RASPBERRY_PI_B_PLUS = "RASPBERRY_PI_B_PLUS"
  40. RASPBERRY_PI_A = "RASPBERRY_PI_A"
  41. RASPBERRY_PI_A_PLUS = "RASPBERRY_PI_A_PLUS"
  42. RASPBERRY_PI_CM1 = "RASPBERRY_PI_CM1"
  43. RASPBERRY_PI_ZERO = "RASPBERRY_PI_ZERO"
  44. RASPBERRY_PI_ZERO_W = "RASPBERRY_PI_ZERO_W"
  45. RASPBERRY_PI_2B = "RASPBERRY_PI_2B"
  46. RASPBERRY_PI_3B = "RASPBERRY_PI_3B"
  47. RASPBERRY_PI_3B_PLUS = "RASPBERRY_PI_3B_PLUS"
  48. RASPBERRY_PI_CM3 = "RASPBERRY_PI_CM3"
  49. RASPBERRY_PI_3A_PLUS = "RASPBERRY_PI_3A_PLUS"
  50. RASPBERRY_PI_CM3_PLUS = "RASPBERRY_PI_CM3_PLUS"
  51. RASPBERRY_PI_4B = "RASPBERRY_PI_4B"
  52. ODROID_C1 = "ODROID_C1"
  53. ODROID_C1_PLUS = "ODROID_C1_PLUS"
  54. ODROID_C2 = "ODROID_C2"
  55. ODROID_N2 = "ODROID_N2"
  56. FTDI_FT232H = "FT232H"
  57. DRAGONBOARD_410C = "DRAGONBOARD_410C"
  58. SIFIVE_UNLEASHED = "SIFIVE_UNLEASHED"
  59. # pylint: enable=bad-whitespace
  60. #OrangePI
  61. _ORANGE_PI_IDS = (
  62. ORANGE_PI_PC,
  63. ORANGE_PI_R1
  64. )
  65. _CORAL_IDS = (
  66. CORAL_EDGE_TPU_DEV,
  67. )
  68. _JETSON_IDS = (
  69. JETSON_TX1,
  70. JETSON_TX2,
  71. JETSON_XAVIER,
  72. JETSON_NANO
  73. )
  74. _RASPBERRY_PI_40_PIN_IDS = (
  75. RASPBERRY_PI_B_PLUS,
  76. RASPBERRY_PI_A_PLUS,
  77. RASPBERRY_PI_ZERO,
  78. RASPBERRY_PI_ZERO_W,
  79. RASPBERRY_PI_2B,
  80. RASPBERRY_PI_3B,
  81. RASPBERRY_PI_3B_PLUS,
  82. RASPBERRY_PI_3A_PLUS,
  83. RASPBERRY_PI_4B
  84. )
  85. _RASPBERRY_PI_CM_IDS = (
  86. RASPBERRY_PI_CM1,
  87. RASPBERRY_PI_CM3,
  88. RASPBERRY_PI_CM3_PLUS
  89. )
  90. _ODROID_40_PIN_IDS = (
  91. ODROID_C1,
  92. ODROID_C1_PLUS,
  93. ODROID_C2,
  94. ODROID_N2
  95. )
  96. _BEAGLEBONE_IDS = (
  97. BEAGLEBONE,
  98. BEAGLEBONE_BLACK,
  99. BEAGLEBONE_BLUE,
  100. BEAGLEBONE_BLACK_WIRELESS,
  101. BEAGLEBONE_POCKETBEAGLE,
  102. BEAGLEBONE_GREEN,
  103. BEAGLEBONE_GREEN_WIRELESS,
  104. BEAGLEBONE_BLACK_INDUSTRIAL,
  105. BEAGLEBONE_ENHANCED,
  106. BEAGLEBONE_USOMIQ,
  107. BEAGLEBONE_AIR,
  108. BEAGLEBONE_POCKETBONE,
  109. BEAGLELOGIC_STANDALONE,
  110. OSD3358_DEV_BOARD,
  111. OSD3358_SM_RED,
  112. )
  113. _LINARO_96BOARDS_IDS = (
  114. DRAGONBOARD_410C,
  115. )
  116. _SIFIVE_IDS = (
  117. SIFIVE_UNLEASHED,
  118. )
  119. # BeagleBone eeprom board ids from:
  120. # https://github.com/beagleboard/image-builder
  121. # Thanks to zmatt on freenode #beagle for pointers.
  122. _BEAGLEBONE_BOARD_IDS = {
  123. # Original bone/white:
  124. BEAGLEBONE: (
  125. ('A4', 'A335BONE00A4'),
  126. ('A5', 'A335BONE00A5'),
  127. ('A6', 'A335BONE00A6'),
  128. ('A6A', 'A335BONE0A6A'),
  129. ('A6B', 'A335BONE0A6B'),
  130. ('B', 'A335BONE000B'),
  131. ),
  132. BEAGLEBONE_BLACK: (
  133. ('A5', 'A335BNLT00A5'),
  134. ('A5A', 'A335BNLT0A5A'),
  135. ('A5B', 'A335BNLT0A5B'),
  136. ('A5C', 'A335BNLT0A5C'),
  137. ('A6', 'A335BNLT00A6'),
  138. ('C', 'A335BNLT000C'),
  139. ('C', 'A335BNLT00C0'),
  140. ),
  141. BEAGLEBONE_BLUE: (
  142. ('A2', 'A335BNLTBLA2'),
  143. ),
  144. BEAGLEBONE_BLACK_WIRELESS: (
  145. ('A5', 'A335BNLTBWA5'),
  146. ),
  147. BEAGLEBONE_POCKETBEAGLE: (
  148. ('A2', 'A335PBGL00A2'),
  149. ),
  150. BEAGLEBONE_GREEN: (
  151. ('1A', 'A335BNLT....'),
  152. ('UNKNOWN', 'A335BNLTBBG1'),
  153. ),
  154. BEAGLEBONE_GREEN_WIRELESS: (
  155. ('W1A', 'A335BNLTGW1A'),
  156. ),
  157. BEAGLEBONE_BLACK_INDUSTRIAL: (
  158. ('A0', 'A335BNLTAIA0'), # Arrow
  159. ('A0', 'A335BNLTEIA0'), # Element14
  160. ),
  161. BEAGLEBONE_ENHANCED: (
  162. ('A', 'A335BNLTSE0A'),
  163. ),
  164. BEAGLEBONE_USOMIQ: (
  165. ('6', 'A335BNLTME06'),
  166. ),
  167. BEAGLEBONE_AIR: (
  168. ('A0', 'A335BNLTNAD0'),
  169. ),
  170. BEAGLEBONE_POCKETBONE: (
  171. ('0', 'A335BNLTBP00'),
  172. ),
  173. OSD3358_DEV_BOARD: (
  174. ('0.1', 'A335BNLTGH01'),
  175. ),
  176. OSD3358_SM_RED: (
  177. ('0', 'A335BNLTOS00'),
  178. ),
  179. BEAGLELOGIC_STANDALONE: (
  180. ('A', 'A335BLGC000A'),
  181. )
  182. }
  183. # Pi revision codes from:
  184. # https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
  185. # Each tuple here contains both the base codes, and the versions that indicate
  186. # the Pi is overvolted / overclocked - for 4-digit codes, this will be prefixed
  187. # with 1000, and for 6-digit codes it'll be prefixed with 1. These are placed
  188. # on separate lines.
  189. _PI_REV_CODES = {
  190. RASPBERRY_PI_B_REV1: (
  191. # Regular codes:
  192. '0002', '0003',
  193. # Overvolted/clocked versions:
  194. '1000002', '1000003',
  195. ),
  196. RASPBERRY_PI_B_REV2: (
  197. '0005', '0006', '000d', '000e', '000f',
  198. '1000005', '1000006', '100000d', '100000e', '100000f',
  199. ),
  200. RASPBERRY_PI_B_PLUS: (
  201. '0010', '0013', '900032',
  202. '1000010', '1000013', '1900032',
  203. ),
  204. RASPBERRY_PI_A: (
  205. '0007', '0008', '0009',
  206. '1000007', '1000008', '1000009',
  207. ),
  208. RASPBERRY_PI_A_PLUS: (
  209. '0012', '0015', '900021',
  210. '1000012', '1000015', '1900021',
  211. ),
  212. RASPBERRY_PI_CM1: (
  213. '0011', '0014',
  214. '10000011', '10000014',
  215. ),
  216. RASPBERRY_PI_ZERO: (
  217. '900092', '920092', '900093', '920093',
  218. '1900092', '1920092', '1900093', '1920093', # warranty bit 24
  219. '2900092', '2920092', '2900093', '2920093', # warranty bit 25
  220. ),
  221. RASPBERRY_PI_ZERO_W: (
  222. '9000c1',
  223. '19000c1', '29000c1', # warranty bits
  224. ),
  225. RASPBERRY_PI_2B: (
  226. 'a01040', 'a01041', 'a21041', 'a22042',
  227. '1a01040', '1a01041', '1a21041', '1a22042', # warranty bit 24
  228. '2a01040', '2a01041', '2a21041', '2a22042', # warranty bit 25
  229. ),
  230. RASPBERRY_PI_3B: (
  231. 'a02082', 'a22082', 'a32082', 'a52082',
  232. '1a02082', '1a22082', '1a32082', '1a52082', # warranty bit 24
  233. '2a02082', '2a22082', '2a32082', '2a52082', # warranty bit 25
  234. ),
  235. RASPBERRY_PI_3B_PLUS: (
  236. 'a020d3',
  237. '1a020d3', '2a020d3', # warranty bits
  238. ),
  239. RASPBERRY_PI_CM3: (
  240. 'a020a0', 'a220a0',
  241. '1a020a0', '2a020a0', # warranty bits
  242. '1a220a0', '2a220a0',
  243. ),
  244. RASPBERRY_PI_3A_PLUS: (
  245. '9020e0',
  246. '19020e0', '29020e0', # warranty bits
  247. ),
  248. RASPBERRY_PI_CM3_PLUS: (
  249. 'a02100',
  250. '1a02100', '2a02100', # warranty bits
  251. ),
  252. RASPBERRY_PI_4B: (
  253. 'a03111', 'b03111', 'c03111',
  254. '1a03111', '2a03111', '1b03111', '2b03111', # warranty bits
  255. '1c03111', '2c03111',
  256. ),
  257. }
  258. class Board:
  259. """Attempt to detect specific boards."""
  260. def __init__(self, detector):
  261. self.detector = detector
  262. # pylint: disable=invalid-name, too-many-branches
  263. @property
  264. def id(self):
  265. """Return a unique id for the detected board, if any."""
  266. # There are some times we want to trick the platform detection
  267. # say if a raspberry pi doesn't have the right ID, or for testing
  268. try:
  269. return os.environ['BLINKA_FORCEBOARD']
  270. except KeyError: # no forced board, continue with testing!
  271. pass
  272. chip_id = self.detector.chip.id
  273. board_id = None
  274. if chip_id == ap_chip.BCM2XXX:
  275. board_id = self._pi_id()
  276. elif chip_id == ap_chip.AM33XX:
  277. board_id = self._beaglebone_id()
  278. elif chip_id == ap_chip.GENERIC_X86:
  279. board_id = GENERIC_LINUX_PC
  280. elif chip_id == ap_chip.SUN8I:
  281. board_id = self._armbian_id()
  282. elif chip_id == ap_chip.SAMA5:
  283. board_id = self._sama5_id()
  284. elif chip_id == ap_chip.IMX8MX:
  285. board_id = self._imx8mx_id()
  286. elif chip_id == ap_chip.ESP8266:
  287. board_id = FEATHER_HUZZAH
  288. elif chip_id == ap_chip.SAMD21:
  289. board_id = FEATHER_M0_EXPRESS
  290. elif chip_id == ap_chip.STM32:
  291. board_id = PYBOARD
  292. elif chip_id == ap_chip.S805:
  293. board_id = ODROID_C1
  294. elif chip_id == ap_chip.S905:
  295. board_id = ODROID_C2
  296. elif chip_id == ap_chip.S922X:
  297. board_id = ODROID_N2
  298. elif chip_id == ap_chip.FT232H:
  299. board_id = FTDI_FT232H
  300. elif chip_id == ap_chip.APQ8016:
  301. board_id = DRAGONBOARD_410C
  302. elif chip_id in (ap_chip.T210, ap_chip.T186, ap_chip.T194):
  303. board_id = self._tegra_id()
  304. elif chip_id == ap_chip.HFU540:
  305. board_id = self._sifive_id()
  306. return board_id
  307. # pylint: enable=invalid-name
  308. def _pi_id(self):
  309. """Try to detect id of a Raspberry Pi."""
  310. # Check for Pi boards:
  311. pi_rev_code = self._pi_rev_code()
  312. if pi_rev_code:
  313. for model, codes in _PI_REV_CODES.items():
  314. if pi_rev_code in codes:
  315. return model
  316. return None
  317. def _pi_rev_code(self):
  318. """Attempt to find a Raspberry Pi revision code for this board."""
  319. # 2708 is Pi 1
  320. # 2709 is Pi 2
  321. # 2835 is Pi 3 (or greater) on 4.9.x kernel
  322. # Anything else is not a Pi.
  323. if self.detector.chip.id != ap_chip.BCM2XXX:
  324. # Something else, not a Pi.
  325. return None
  326. return self.detector.get_cpuinfo_field('Revision')
  327. # pylint: disable=no-self-use
  328. def _beaglebone_id(self):
  329. """Try to detect id of a Beaglebone."""
  330. try:
  331. with open("/sys/bus/nvmem/devices/0-00500/nvmem", "rb") as eeprom:
  332. eeprom_bytes = eeprom.read(16)
  333. except FileNotFoundError:
  334. return None
  335. if eeprom_bytes[:4] != b'\xaaU3\xee':
  336. return None
  337. id_string = eeprom_bytes[4:].decode("ascii")
  338. for model, bb_ids in _BEAGLEBONE_BOARD_IDS.items():
  339. for bb_id in bb_ids:
  340. if id_string == bb_id[1]:
  341. return model
  342. return None
  343. # pylint: enable=no-self-use
  344. def _armbian_id(self):
  345. """Check whether the current board is an OrangePi PC or OrangePI R1."""
  346. board_value = self.detector.get_armbian_release_field('BOARD')
  347. if board_value == "orangepipc":
  348. return ORANGE_PI_PC
  349. if board_value == "orangepi-r1":
  350. return ORANGE_PI_R1
  351. return None
  352. def _sama5_id(self):
  353. """Check what type sama5 board."""
  354. board_value = self.detector.get_device_model()
  355. if "Giant Board" in board_value:
  356. return GIANT_BOARD
  357. return None
  358. def _imx8mx_id(self):
  359. """Check what type iMX8M board."""
  360. board_value = self.detector.get_device_model()
  361. if "Phanbell" in board_value:
  362. return CORAL_EDGE_TPU_DEV
  363. return None
  364. def _tegra_id(self):
  365. """Try to detect the id of aarch64 board."""
  366. board_value = self.detector.get_device_model()
  367. board = None
  368. if 'tx1' in board_value:
  369. board = JETSON_TX1
  370. elif 'quill' in board_value:
  371. board = JETSON_TX2
  372. elif 'xavier' in board_value:
  373. board = JETSON_XAVIER
  374. elif 'nano' in board_value or "Nano" in board_value:
  375. board = JETSON_NANO
  376. return board
  377. def _sifive_id(self):
  378. """Try to detect the id for Sifive RISCV64 board."""
  379. board_value = self.detector.get_device_model()
  380. if 'hifive-unleashed-a00' in board_value:
  381. return SIFIVE_UNLEASHED
  382. return None
  383. @property
  384. def any_96boards(self):
  385. """Check whether the current board is any 96boards board."""
  386. return self.id in _LINARO_96BOARDS_IDS
  387. @property
  388. def any_raspberry_pi(self):
  389. """Check whether the current board is any Raspberry Pi."""
  390. return self._pi_rev_code() is not None
  391. @property
  392. def any_raspberry_pi_40_pin(self):
  393. """Check whether the current board is any 40-pin Raspberry Pi."""
  394. return self.id in _RASPBERRY_PI_40_PIN_IDS
  395. @property
  396. def any_raspberry_pi_cm(self):
  397. """Check whether the current board is any Compute Module Raspberry Pi."""
  398. return self.id in _RASPBERRY_PI_CM_IDS
  399. @property
  400. def any_beaglebone(self):
  401. """Check whether the current board is any Beaglebone-family system."""
  402. return self.id in _BEAGLEBONE_IDS
  403. @property
  404. def any_orange_pi(self):
  405. """Check whether the current board is any defined Orange Pi."""
  406. return self.id in _ORANGE_PI_IDS
  407. @property
  408. def any_coral_board(self):
  409. """Check whether the current board is any defined Coral."""
  410. return self.CORAL_EDGE_TPU_DEV
  411. @property
  412. def any_giant_board(self):
  413. """Check whether the current board is any defined Giant Board."""
  414. return self.GIANT_BOARD
  415. @property
  416. def any_odroid_40_pin(self):
  417. """Check whether the current board is any defined 40-pin Odroid."""
  418. return self.id in _ODROID_40_PIN_IDS
  419. @property
  420. def any_jetson_board(self):
  421. """Check whether the current board is any defined Jetson Board."""
  422. return self.id in _JETSON_IDS
  423. @property
  424. def any_sifive_board(self):
  425. """Check whether the current board is any defined Jetson Board."""
  426. return self.id in _SIFIVE_IDS
  427. @property
  428. def any_embedded_linux(self):
  429. """Check whether the current board is any embedded Linux device."""
  430. return self.any_raspberry_pi or self.any_beaglebone or \
  431. self.any_orange_pi or self.any_giant_board or self.any_jetson_board or \
  432. self.any_coral_board or self.any_odroid_40_pin or self.any_96boards or \
  433. self.any_sifive_board
  434. def __getattr__(self, attr):
  435. """
  436. Detect whether the given attribute is the currently-detected board. See list
  437. of constants at the top of this module for available options.
  438. """
  439. if self.id == attr:
  440. return True
  441. return False