loadable_elf_example_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import os
  2. import pexpect
  3. import serial
  4. import sys
  5. import threading
  6. import time
  7. try:
  8. import IDF
  9. except ImportError:
  10. test_fw_path = os.getenv('TEST_FW_PATH')
  11. if test_fw_path and test_fw_path not in sys.path:
  12. sys.path.insert(0, test_fw_path)
  13. import IDF
  14. import Utility
  15. class CustomProcess(object):
  16. def __init__(self, cmd, logfile):
  17. self.f = open(logfile, 'wb')
  18. self.p = pexpect.spawn(cmd, timeout=60, logfile=self.f)
  19. def __enter__(self):
  20. return self
  21. def close(self):
  22. self.p.terminate(force=True)
  23. def __exit__(self, type, value, traceback):
  24. self.close()
  25. self.f.close()
  26. class OCDProcess(CustomProcess):
  27. def __init__(self, proj_path):
  28. cmd = 'openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg'
  29. log_file = os.path.join(proj_path, 'openocd.log')
  30. super(OCDProcess, self).__init__(cmd, log_file)
  31. i = self.p.expect_exact(['Info : Listening on port 3333 for gdb connections', 'Error:'])
  32. if i == 0:
  33. Utility.console_log('openocd is listening for gdb connections')
  34. else:
  35. raise RuntimeError('openocd initialization has failed')
  36. def close(self):
  37. try:
  38. self.p.sendcontrol('c')
  39. self.p.expect_exact('shutdown command invoked')
  40. except Exception:
  41. Utility.console_log('openocd needs to be killed', 'O')
  42. super(OCDProcess, self).close()
  43. class GDBProcess(CustomProcess):
  44. def __init__(self, proj_path, elf_path):
  45. cmd = 'xtensa-esp32-elf-gdb -x {} --directory={} {}'.format(os.path.join(proj_path, '.gdbinit.ci'),
  46. os.path.join(proj_path, 'main'),
  47. elf_path)
  48. log_file = os.path.join(proj_path, 'gdb.log')
  49. super(GDBProcess, self).__init__(cmd, log_file)
  50. self.p.sendline('') # it is for "---Type <return> to continue, or q <return> to quit---"
  51. i = self.p.expect_exact(['Thread 1 hit Temporary breakpoint 2, app_main ()',
  52. 'Load failed'])
  53. if i == 0:
  54. Utility.console_log('gdb is at breakpoint')
  55. elif i == 1:
  56. raise RuntimeError('Load has failed. Please examine the logs.')
  57. else:
  58. Utility.console_log('i = {}'.format(i))
  59. Utility.console_log(str(self.p))
  60. # This really should not happen. TIMEOUT and EOF failures are exceptions.
  61. raise RuntimeError('An unknown error has occurred. Please examine the logs.')
  62. self.p.expect_exact('(gdb)')
  63. def close(self):
  64. try:
  65. self.p.sendline('q')
  66. self.p.expect_exact('Quit anyway? (y or n)')
  67. self.p.sendline('y')
  68. self.p.expect_exact('Ending remote debugging.')
  69. except Exception:
  70. Utility.console_log('gdb needs to be killed', 'O')
  71. super(GDBProcess, self).close()
  72. def break_till_end(self):
  73. self.p.sendline('b esp_restart')
  74. self.p.sendline('c')
  75. self.p.expect_exact('Thread 1 hit Breakpoint 3, esp_restart ()')
  76. class SerialThread(object):
  77. def run(self, log_path, exit_event):
  78. with serial.Serial('/dev/ttyUSB1', 115200) as ser, open(log_path, 'wb') as f:
  79. while True:
  80. f.write(ser.read(ser.in_waiting))
  81. if exit_event.is_set():
  82. break
  83. time.sleep(1)
  84. def __init__(self, log_path):
  85. self.exit_event = threading.Event()
  86. self.t = threading.Thread(target=self.run, args=(log_path, self.exit_event,))
  87. self.t.start()
  88. def __enter__(self):
  89. return self
  90. def __exit__(self, type, value, traceback):
  91. self.exit_event.set()
  92. self.t.join(60)
  93. if self.t.is_alive():
  94. Utility.console_log('The pyserial thread is still alive', 'O')
  95. @IDF.idf_example_test(env_tag="test_jtag_arm")
  96. def test_examples_loadable_elf(env, extra_data):
  97. idf_path = os.environ['IDF_PATH']
  98. rel_project_path = os.path.join('examples', 'get-started', 'hello_world')
  99. proj_path = os.path.join(idf_path, rel_project_path)
  100. example = IDF.Example(rel_project_path)
  101. sdkconfig = example.get_sdkconfig()
  102. elf_path = os.path.join(example.get_binary_path(rel_project_path), 'hello-world.elf')
  103. esp_log_path = os.path.join(proj_path, 'esp.log')
  104. assert(sdkconfig['CONFIG_IDF_TARGET_ESP32'] == 'y'), "Only ESP32 target is supported"
  105. assert(sdkconfig['CONFIG_APP_BUILD_TYPE_ELF_RAM'] == 'y'), "ELF should be built with CONFIG_APP_BUILD_TYPE_ELF_RAM"
  106. with SerialThread(esp_log_path):
  107. with OCDProcess(proj_path), GDBProcess(proj_path, elf_path) as gdb:
  108. gdb.break_till_end()
  109. if pexpect.run('grep "Restarting now." {}'.format(esp_log_path), withexitstatus=True)[1]:
  110. raise RuntimeError('Expected output from ESP was not received')
  111. if __name__ == '__main__':
  112. test_examples_loadable_elf()