loadable_elf_example_test.py 5.3 KB

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