IDFDUT.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ DUT for IDF applications """
  15. import os
  16. import os.path
  17. import sys
  18. import re
  19. import functools
  20. import tempfile
  21. import subprocess
  22. import time
  23. import pexpect
  24. # python2 and python3 queue package name is different
  25. try:
  26. import Queue as _queue
  27. except ImportError:
  28. import queue as _queue
  29. from serial.tools import list_ports
  30. from tiny_test_fw import DUT, Utility
  31. try:
  32. import esptool
  33. except ImportError: # cheat and use IDF's copy of esptool if available
  34. idf_path = os.getenv("IDF_PATH")
  35. if not idf_path or not os.path.exists(idf_path):
  36. raise
  37. sys.path.insert(0, os.path.join(idf_path, "components", "esptool_py", "esptool"))
  38. import esptool
  39. class IDFToolError(OSError):
  40. pass
  41. class IDFDUTException(RuntimeError):
  42. pass
  43. class IDFRecvThread(DUT.RecvThread):
  44. PERFORMANCE_PATTERN = re.compile(r"\[Performance]\[(\w+)]: ([^\r\n]+)\r?\n")
  45. EXCEPTION_PATTERNS = [
  46. re.compile(r"(Guru Meditation Error: Core\s+\d panic'ed \([\w].*?\))"),
  47. re.compile(r"(abort\(\) was called at PC 0x[a-fA-F\d]{8} on core \d)"),
  48. re.compile(r"(rst 0x\d+ \(TG\dWDT_SYS_RESET|TGWDT_CPU_RESET\))")
  49. ]
  50. BACKTRACE_PATTERN = re.compile(r"Backtrace:((\s(0x[0-9a-f]{8}):0x[0-9a-f]{8})+)")
  51. BACKTRACE_ADDRESS_PATTERN = re.compile(r"(0x[0-9a-f]{8}):0x[0-9a-f]{8}")
  52. def __init__(self, read, dut):
  53. super(IDFRecvThread, self).__init__(read, dut)
  54. self.exceptions = _queue.Queue()
  55. self.performance_items = _queue.Queue()
  56. def collect_performance(self, comp_data):
  57. matches = self.PERFORMANCE_PATTERN.findall(comp_data)
  58. for match in matches:
  59. Utility.console_log("[Performance][{}]: {}".format(match[0], match[1]),
  60. color="orange")
  61. self.performance_items.put((match[0], match[1]))
  62. def detect_exception(self, comp_data):
  63. for pattern in self.EXCEPTION_PATTERNS:
  64. start = 0
  65. while True:
  66. match = pattern.search(comp_data, pos=start)
  67. if match:
  68. start = match.end()
  69. self.exceptions.put(match.group(0))
  70. Utility.console_log("[Exception]: {}".format(match.group(0)), color="red")
  71. else:
  72. break
  73. def detect_backtrace(self, comp_data):
  74. start = 0
  75. while True:
  76. match = self.BACKTRACE_PATTERN.search(comp_data, pos=start)
  77. if match:
  78. start = match.end()
  79. Utility.console_log("[Backtrace]:{}".format(match.group(1)), color="red")
  80. # translate backtrace
  81. addresses = self.BACKTRACE_ADDRESS_PATTERN.findall(match.group(1))
  82. translated_backtrace = ""
  83. for addr in addresses:
  84. ret = self.dut.lookup_pc_address(addr)
  85. if ret:
  86. translated_backtrace += ret + "\n"
  87. if translated_backtrace:
  88. Utility.console_log("Translated backtrace\n:" + translated_backtrace, color="yellow")
  89. else:
  90. Utility.console_log("Failed to translate backtrace", color="yellow")
  91. else:
  92. break
  93. CHECK_FUNCTIONS = [collect_performance, detect_exception, detect_backtrace]
  94. def _uses_esptool(func):
  95. """ Suspend listener thread, connect with esptool,
  96. call target function with esptool instance,
  97. then resume listening for output
  98. """
  99. @functools.wraps(func)
  100. def handler(self, *args, **kwargs):
  101. self.stop_receive()
  102. settings = self.port_inst.get_settings()
  103. try:
  104. if not self._rom_inst:
  105. self._rom_inst = esptool.ESPLoader.detect_chip(self.port_inst)
  106. self._rom_inst.connect('hard_reset')
  107. esp = self._rom_inst.run_stub()
  108. ret = func(self, esp, *args, **kwargs)
  109. # do hard reset after use esptool
  110. esp.hard_reset()
  111. finally:
  112. # always need to restore port settings
  113. self.port_inst.apply_settings(settings)
  114. self.start_receive()
  115. return ret
  116. return handler
  117. class IDFDUT(DUT.SerialDUT):
  118. """ IDF DUT, extends serial with esptool methods
  119. (Becomes aware of IDFApp instance which holds app-specific data)
  120. """
  121. # /dev/ttyAMA0 port is listed in Raspberry Pi
  122. # /dev/tty.Bluetooth-Incoming-Port port is listed in Mac
  123. INVALID_PORT_PATTERN = re.compile(r"AMA|Bluetooth")
  124. # if need to erase NVS partition in start app
  125. ERASE_NVS = True
  126. RECV_THREAD_CLS = IDFRecvThread
  127. def __init__(self, name, port, log_file, app, allow_dut_exception=False, **kwargs):
  128. super(IDFDUT, self).__init__(name, port, log_file, app, **kwargs)
  129. self.allow_dut_exception = allow_dut_exception
  130. self.exceptions = _queue.Queue()
  131. self.performance_items = _queue.Queue()
  132. self._rom_inst = None
  133. @classmethod
  134. def _get_rom(cls):
  135. raise NotImplementedError("This is an abstraction class, method not defined.")
  136. @classmethod
  137. def get_mac(cls, app, port):
  138. """
  139. get MAC address via esptool
  140. :param app: application instance (to get tool)
  141. :param port: serial port as string
  142. :return: MAC address or None
  143. """
  144. esp = None
  145. try:
  146. esp = cls._get_rom()(port)
  147. esp.connect()
  148. return esp.read_mac()
  149. except RuntimeError:
  150. return None
  151. finally:
  152. if esp:
  153. # do hard reset after use esptool
  154. esp.hard_reset()
  155. esp._port.close()
  156. @classmethod
  157. def confirm_dut(cls, port, **kwargs):
  158. inst = None
  159. try:
  160. expected_rom_class = cls._get_rom()
  161. except NotImplementedError:
  162. expected_rom_class = None
  163. try:
  164. # TODO: check whether 8266 works with this logic
  165. # Otherwise overwrite it in ESP8266DUT
  166. inst = esptool.ESPLoader.detect_chip(port)
  167. if expected_rom_class and type(inst) != expected_rom_class:
  168. raise RuntimeError("Target not expected")
  169. return inst.read_mac() is not None, get_target_by_rom_class(type(inst))
  170. except(esptool.FatalError, RuntimeError):
  171. return False, None
  172. finally:
  173. if inst is not None:
  174. inst._port.close()
  175. @_uses_esptool
  176. def _try_flash(self, esp, erase_nvs, baud_rate):
  177. """
  178. Called by start_app() to try flashing at a particular baud rate.
  179. Structured this way so @_uses_esptool will reconnect each time
  180. """
  181. flash_files = []
  182. try:
  183. # note: opening here prevents us from having to seek back to 0 each time
  184. flash_files = [(offs, open(path, "rb")) for (offs, path) in self.app.flash_files]
  185. if erase_nvs:
  186. address = self.app.partition_table["nvs"]["offset"]
  187. size = self.app.partition_table["nvs"]["size"]
  188. nvs_file = tempfile.TemporaryFile()
  189. nvs_file.write(b'\xff' * size)
  190. nvs_file.seek(0)
  191. flash_files.append((int(address, 0), nvs_file))
  192. # fake flasher args object, this is a hack until
  193. # esptool Python API is improved
  194. class FlashArgs(object):
  195. def __init__(self, attributes):
  196. for key, value in attributes.items():
  197. self.__setattr__(key, value)
  198. flash_args = FlashArgs({
  199. 'flash_size': self.app.flash_settings["flash_size"],
  200. 'flash_mode': self.app.flash_settings["flash_mode"],
  201. 'flash_freq': self.app.flash_settings["flash_freq"],
  202. 'addr_filename': flash_files,
  203. 'no_stub': False,
  204. 'compress': True,
  205. 'verify': False,
  206. 'encrypt': self.app.flash_settings.get("encrypt", False),
  207. 'erase_all': False,
  208. })
  209. esp.change_baud(baud_rate)
  210. esptool.detect_flash_size(esp, flash_args)
  211. esptool.write_flash(esp, flash_args)
  212. finally:
  213. for (_, f) in flash_files:
  214. f.close()
  215. def start_app(self, erase_nvs=ERASE_NVS):
  216. """
  217. download and start app.
  218. :param: erase_nvs: whether erase NVS partition during flash
  219. :return: None
  220. """
  221. for baud_rate in [921600, 115200]:
  222. try:
  223. self._try_flash(erase_nvs, baud_rate)
  224. break
  225. except RuntimeError:
  226. continue
  227. else:
  228. raise IDFToolError()
  229. @_uses_esptool
  230. def reset(self, esp):
  231. """
  232. hard reset DUT
  233. :return: None
  234. """
  235. # decorator `_use_esptool` will do reset
  236. # so we don't need to do anything in this method
  237. pass
  238. @_uses_esptool
  239. def erase_partition(self, esp, partition):
  240. """
  241. :param partition: partition name to erase
  242. :return: None
  243. """
  244. raise NotImplementedError() # TODO: implement this
  245. # address = self.app.partition_table[partition]["offset"]
  246. size = self.app.partition_table[partition]["size"]
  247. # TODO can use esp.erase_region() instead of this, I think
  248. with open(".erase_partition.tmp", "wb") as f:
  249. f.write(chr(0xFF) * size)
  250. @_uses_esptool
  251. def dump_flush(self, esp, output_file, **kwargs):
  252. """
  253. dump flush
  254. :param output_file: output file name, if relative path, will use sdk path as base path.
  255. :keyword partition: partition name, dump the partition.
  256. ``partition`` is preferred than using ``address`` and ``size``.
  257. :keyword address: dump from address (need to be used with size)
  258. :keyword size: dump size (need to be used with address)
  259. :return: None
  260. """
  261. if os.path.isabs(output_file) is False:
  262. output_file = os.path.relpath(output_file, self.app.get_log_folder())
  263. if "partition" in kwargs:
  264. partition = self.app.partition_table[kwargs["partition"]]
  265. _address = partition["offset"]
  266. _size = partition["size"]
  267. elif "address" in kwargs and "size" in kwargs:
  268. _address = kwargs["address"]
  269. _size = kwargs["size"]
  270. else:
  271. raise IDFToolError("You must specify 'partition' or ('address' and 'size') to dump flash")
  272. content = esp.read_flash(_address, _size)
  273. with open(output_file, "wb") as f:
  274. f.write(content)
  275. @classmethod
  276. def list_available_ports(cls):
  277. ports = [x.device for x in list_ports.comports()]
  278. espport = os.getenv('ESPPORT')
  279. if not espport:
  280. # It's a little hard filter out invalid port with `serial.tools.list_ports.grep()`:
  281. # The check condition in `grep` is: `if r.search(port) or r.search(desc) or r.search(hwid)`.
  282. # This means we need to make all 3 conditions fail, to filter out the port.
  283. # So some part of the filters will not be straight forward to users.
  284. # And negative regular expression (`^((?!aa|bb|cc).)*$`) is not easy to understand.
  285. # Filter out invalid port by our own will be much simpler.
  286. return [x for x in ports if not cls.INVALID_PORT_PATTERN.search(x)]
  287. # On MacOs with python3.6: type of espport is already utf8
  288. if isinstance(espport, type(u'')):
  289. port_hint = espport
  290. else:
  291. port_hint = espport.decode('utf8')
  292. # If $ESPPORT is a valid port, make it appear first in the list
  293. if port_hint in ports:
  294. ports.remove(port_hint)
  295. return [port_hint] + ports
  296. # On macOS, user may set ESPPORT to /dev/tty.xxx while
  297. # pySerial lists only the corresponding /dev/cu.xxx port
  298. if sys.platform == 'darwin' and 'tty.' in port_hint:
  299. port_hint = port_hint.replace('tty.', 'cu.')
  300. if port_hint in ports:
  301. ports.remove(port_hint)
  302. return [port_hint] + ports
  303. return ports
  304. def lookup_pc_address(self, pc_addr):
  305. cmd = ["%saddr2line" % self.TOOLCHAIN_PREFIX,
  306. "-pfiaC", "-e", self.app.elf_file, pc_addr]
  307. ret = ""
  308. try:
  309. translation = subprocess.check_output(cmd)
  310. ret = translation.decode()
  311. except OSError:
  312. pass
  313. return ret
  314. @staticmethod
  315. def _queue_read_all(source_queue):
  316. output = []
  317. while True:
  318. try:
  319. output.append(source_queue.get(timeout=0))
  320. except _queue.Empty:
  321. break
  322. return output
  323. def _queue_copy(self, source_queue, dest_queue):
  324. data = self._queue_read_all(source_queue)
  325. for d in data:
  326. dest_queue.put(d)
  327. def _get_from_queue(self, queue_name):
  328. self_queue = getattr(self, queue_name)
  329. if self.receive_thread:
  330. recv_thread_queue = getattr(self.receive_thread, queue_name)
  331. self._queue_copy(recv_thread_queue, self_queue)
  332. return self._queue_read_all(self_queue)
  333. def stop_receive(self):
  334. if self.receive_thread:
  335. for name in ["performance_items", "exceptions"]:
  336. source_queue = getattr(self.receive_thread, name)
  337. dest_queue = getattr(self, name)
  338. self._queue_copy(source_queue, dest_queue)
  339. super(IDFDUT, self).stop_receive()
  340. def get_exceptions(self):
  341. """ Get exceptions detected by DUT receive thread. """
  342. return self._get_from_queue("exceptions")
  343. def get_performance_items(self):
  344. """
  345. DUT receive thread will automatic collect performance results with pattern ``[Performance][name]: value\n``.
  346. This method is used to get all performance results.
  347. :return: a list of performance items.
  348. """
  349. return self._get_from_queue("performance_items")
  350. def close(self):
  351. super(IDFDUT, self).close()
  352. if not self.allow_dut_exception and self.get_exceptions():
  353. Utility.console_log("DUT exception detected on {}".format(self), color="red")
  354. raise IDFDUTException()
  355. class ESP32DUT(IDFDUT):
  356. TARGET = "esp32"
  357. TOOLCHAIN_PREFIX = "xtensa-esp32-elf-"
  358. @classmethod
  359. def _get_rom(cls):
  360. return esptool.ESP32ROM
  361. class ESP32S2DUT(IDFDUT):
  362. TARGET = "esp32s2beta"
  363. TOOLCHAIN_PREFIX = "xtensa-esp32s2-elf-"
  364. @classmethod
  365. def _get_rom(cls):
  366. return esptool.ESP32S2ROM
  367. class ESP8266DUT(IDFDUT):
  368. TARGET = "esp8266"
  369. TOOLCHAIN_PREFIX = "xtensa-lx106-elf-"
  370. @classmethod
  371. def _get_rom(cls):
  372. return esptool.ESP8266ROM
  373. def get_target_by_rom_class(cls):
  374. for c in [ESP32DUT, ESP32S2DUT, ESP8266DUT, IDFQEMUDUT]:
  375. if c._get_rom() == cls:
  376. return c.TARGET
  377. return None
  378. class IDFQEMUDUT(IDFDUT):
  379. TARGET = None
  380. TOOLCHAIN_PREFIX = None
  381. ERASE_NVS = True
  382. DEFAULT_EXPECT_TIMEOUT = 30 # longer timeout, since app startup takes more time in QEMU (due to slow SHA emulation)
  383. QEMU_SERIAL_PORT = 3334
  384. def __init__(self, name, port, log_file, app, allow_dut_exception=False, **kwargs):
  385. self.flash_image = tempfile.NamedTemporaryFile('rb+', suffix=".bin", prefix="qemu_flash_img")
  386. self.app = app
  387. self.flash_size = 4 * 1024 * 1024
  388. self._write_flash_img()
  389. args = [
  390. "qemu-system-xtensa",
  391. "-nographic",
  392. "-machine", self.TARGET,
  393. "-drive", "file={},if=mtd,format=raw".format(self.flash_image.name),
  394. "-nic", "user,model=open_eth",
  395. "-serial", "tcp::{},server,nowait".format(self.QEMU_SERIAL_PORT),
  396. "-S",
  397. "-global driver=timer.esp32.timg,property=wdt_disable,value=true"]
  398. # TODO(IDF-1242): generate a temporary efuse binary, pass it to QEMU
  399. if "QEMU_BIOS_PATH" in os.environ:
  400. args += ["-L", os.environ["QEMU_BIOS_PATH"]]
  401. self.qemu = pexpect.spawn(" ".join(args), timeout=self.DEFAULT_EXPECT_TIMEOUT)
  402. self.qemu.expect_exact(b"(qemu)")
  403. super(IDFQEMUDUT, self).__init__(name, port, log_file, app, allow_dut_exception=allow_dut_exception, **kwargs)
  404. def _write_flash_img(self):
  405. self.flash_image.seek(0)
  406. self.flash_image.write(b'\x00' * self.flash_size)
  407. for offs, path in self.app.flash_files:
  408. with open(path, "rb") as flash_file:
  409. contents = flash_file.read()
  410. self.flash_image.seek(offs)
  411. self.flash_image.write(contents)
  412. self.flash_image.flush()
  413. @classmethod
  414. def _get_rom(cls):
  415. return esptool.ESP32ROM
  416. @classmethod
  417. def get_mac(cls, app, port):
  418. # TODO(IDF-1242): get this from QEMU/efuse binary
  419. return "11:22:33:44:55:66"
  420. @classmethod
  421. def confirm_dut(cls, port, **kwargs):
  422. return True, cls.TARGET
  423. def start_app(self, erase_nvs=ERASE_NVS):
  424. # TODO: implement erase_nvs
  425. # since the flash image is generated every time in the constructor, maybe this isn't needed...
  426. self.qemu.sendline(b"cont\n")
  427. self.qemu.expect_exact(b"(qemu)")
  428. def reset(self):
  429. self.qemu.sendline(b"system_reset\n")
  430. self.qemu.expect_exact(b"(qemu)")
  431. def erase_partition(self, partition):
  432. raise NotImplementedError("method not erase_partition not implemented")
  433. def dump_flush(self, output_file, **kwargs):
  434. raise NotImplementedError("method not dump_flush not implemented")
  435. @classmethod
  436. def list_available_ports(cls):
  437. return ["socket://localhost:{}".format(cls.QEMU_SERIAL_PORT)]
  438. def close(self):
  439. super(IDFQEMUDUT, self).close()
  440. self.qemu.sendline(b"q\n")
  441. self.qemu.expect_exact(b"(qemu)")
  442. for _ in range(self.DEFAULT_EXPECT_TIMEOUT):
  443. if not self.qemu.isalive():
  444. break
  445. time.sleep(1)
  446. else:
  447. self.qemu.terminate(force=True)
  448. class ESP32QEMUDUT(IDFQEMUDUT):
  449. TARGET = "esp32"
  450. TOOLCHAIN_PREFIX = "xtensa-esp32-elf-"