loadable_elf_example_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 board/esp32-wrover-kit-3.3v.cfg'
  29. log_file = os.path.join(proj_path, 'openocd.log')
  30. super(OCDProcess, self).__init__(cmd, log_file)
  31. patterns = ['Info : Listening on port 3333 for gdb connections',
  32. 'Error: type \'esp32\' is missing virt2phys']
  33. try:
  34. while True:
  35. i = self.p.expect_exact(patterns, timeout=30)
  36. # TIMEOUT or EOF exceptions will be thrown upon other errors
  37. if i == 0:
  38. Utility.console_log('openocd is listening for gdb connections')
  39. break # success
  40. elif i == 1:
  41. Utility.console_log('Ignoring error: "{}"'.format(patterns[i]))
  42. # this error message is ignored because it is not a fatal error
  43. except Exception:
  44. Utility.console_log('openocd initialization has failed', 'R')
  45. raise
  46. def close(self):
  47. try:
  48. self.p.sendcontrol('c')
  49. self.p.expect_exact('shutdown command invoked')
  50. except Exception:
  51. Utility.console_log('openocd needs to be killed', 'O')
  52. super(OCDProcess, self).close()
  53. class GDBProcess(CustomProcess):
  54. def __init__(self, proj_path, elf_path):
  55. cmd = 'xtensa-esp32-elf-gdb -x {} --directory={} {}'.format(os.path.join(proj_path, '.gdbinit.ci'),
  56. os.path.join(proj_path, 'main'),
  57. elf_path)
  58. log_file = os.path.join(proj_path, 'gdb.log')
  59. super(GDBProcess, self).__init__(cmd, log_file)
  60. self.p.sendline('') # it is for "---Type <return> to continue, or q <return> to quit---"
  61. i = self.p.expect_exact(['Thread 1 hit Temporary breakpoint 2, app_main ()',
  62. 'Load failed'])
  63. if i == 0:
  64. Utility.console_log('gdb is at breakpoint')
  65. elif i == 1:
  66. raise RuntimeError('Load has failed. Please examine the logs.')
  67. else:
  68. Utility.console_log('i = {}'.format(i))
  69. Utility.console_log(str(self.p))
  70. # This really should not happen. TIMEOUT and EOF failures are exceptions.
  71. raise RuntimeError('An unknown error has occurred. Please examine the logs.')
  72. self.p.expect_exact('(gdb)')
  73. def close(self):
  74. try:
  75. self.p.sendline('q')
  76. self.p.expect_exact('Quit anyway? (y or n)')
  77. self.p.sendline('y')
  78. self.p.expect_exact('Ending remote debugging.')
  79. except Exception:
  80. Utility.console_log('gdb needs to be killed', 'O')
  81. super(GDBProcess, self).close()
  82. def break_till_end(self):
  83. self.p.sendline('b esp_restart')
  84. self.p.sendline('c')
  85. self.p.expect_exact('Thread 1 hit Breakpoint 3, esp_restart ()')
  86. class SerialThread(object):
  87. def run(self, log_path, exit_event):
  88. with serial.Serial('/dev/ttyUSB1', 115200) as ser, open(log_path, 'wb') as f:
  89. while True:
  90. f.write(ser.read(ser.in_waiting))
  91. if exit_event.is_set():
  92. break
  93. time.sleep(1)
  94. def __init__(self, log_path):
  95. self.exit_event = threading.Event()
  96. self.t = threading.Thread(target=self.run, args=(log_path, self.exit_event,))
  97. self.t.start()
  98. def __enter__(self):
  99. return self
  100. def __exit__(self, type, value, traceback):
  101. self.exit_event.set()
  102. self.t.join(60)
  103. if self.t.is_alive():
  104. Utility.console_log('The pyserial thread is still alive', 'O')
  105. @IDF.idf_example_test(env_tag="test_jtag_arm")
  106. def test_examples_loadable_elf(env, extra_data):
  107. idf_path = os.environ['IDF_PATH']
  108. rel_project_path = os.path.join('examples', 'get-started', 'hello_world')
  109. proj_path = os.path.join(idf_path, rel_project_path)
  110. example = IDF.Example(rel_project_path, target="esp32")
  111. sdkconfig = example.get_sdkconfig()
  112. elf_path = os.path.join(example.get_binary_path(rel_project_path), 'hello-world.elf')
  113. esp_log_path = os.path.join(proj_path, 'esp.log')
  114. assert(sdkconfig['CONFIG_IDF_TARGET_ESP32'] == 'y'), "Only ESP32 target is supported"
  115. assert(sdkconfig['CONFIG_APP_BUILD_TYPE_ELF_RAM'] == 'y'), "ELF should be built with CONFIG_APP_BUILD_TYPE_ELF_RAM"
  116. with SerialThread(esp_log_path):
  117. with OCDProcess(proj_path), GDBProcess(proj_path, elf_path) as gdb:
  118. gdb.break_till_end()
  119. if pexpect.run('grep "Restarting now." {}'.format(esp_log_path), withexitstatus=True)[1]:
  120. raise RuntimeError('Expected output from ESP was not received')
  121. if __name__ == '__main__':
  122. test_examples_loadable_elf()