example_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import print_function
  2. import re
  3. import os
  4. import sys
  5. try:
  6. import IDF
  7. from IDF.IDFDUT import ESP32DUT
  8. except ImportError:
  9. # this is a test case write with tiny-test-fw.
  10. # to run test cases outside tiny-test-fw,
  11. # we need to set environment variable `TEST_FW_PATH`,
  12. # then get and insert `TEST_FW_PATH` to sys path before import FW module
  13. test_fw_path = os.getenv('TEST_FW_PATH')
  14. if test_fw_path and test_fw_path not in sys.path:
  15. sys.path.insert(0, test_fw_path)
  16. import IDF
  17. STARTING_TIMERS_REGEX = re.compile(r'Started timers, time since boot: (\d+) us')
  18. # name, period, next_alarm, times_started, times_fired, cb_exec_time
  19. TIMER_DUMP_LINE_REGEX = re.compile(r'([\w-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)')
  20. PERIODIC_TIMER_REGEX = re.compile(r'Periodic timer called, time since boot: (\d+) us')
  21. LIGHT_SLEEP_ENTER_REGEX = re.compile(r'Entering light sleep for 0\.5s, time since boot: (\d+) us')
  22. LIGHT_SLEEP_EXIT_REGEX = re.compile(r'Woke up from light sleep, time since boot: (\d+) us')
  23. ONE_SHOT_REGEX = re.compile(r'One\-shot timer called, time since boot: (\d+) us')
  24. RESTART_REGEX = re.compile(r'Restarted periodic timer with 1s period, time since boot: (\d+) us')
  25. STOP_REGEX = re.compile(r'Stopped and deleted timers')
  26. INITIAL_TIMER_PERIOD = 500000
  27. FINAL_TIMER_PERIOD = 1000000
  28. LIGHT_SLEEP_TIME = 500000
  29. ONE_SHOT_TIMER_PERIOD = 5000000
  30. @IDF.idf_example_test(env_tag='Example_WIFI')
  31. def test_examples_system_esp_timer(env, extra_data):
  32. dut = env.get_dut('esp_timer_example', 'examples/system/esp_timer', dut_class=ESP32DUT)
  33. # start test
  34. dut.start_app()
  35. groups = dut.expect(STARTING_TIMERS_REGEX, timeout=30)
  36. start_time = int(groups[0])
  37. print('Start time: {} us'.format(start_time))
  38. groups = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  39. assert(groups[0] == 'periodic' and int(groups[1]) == INITIAL_TIMER_PERIOD)
  40. groups = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  41. assert(groups[0] == 'one-shot' and int(groups[1]) == 0)
  42. for i in range(0, 5):
  43. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  44. cur_time = int(groups[0])
  45. diff = start_time + (i + 1) * INITIAL_TIMER_PERIOD - cur_time
  46. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  47. assert(abs(diff) < 100)
  48. groups = dut.expect(ONE_SHOT_REGEX, timeout=3)
  49. one_shot_timer_time = int(groups[0])
  50. diff = start_time + ONE_SHOT_TIMER_PERIOD - one_shot_timer_time
  51. print('One-shot timer, time: {} us, diff: {}'.format(one_shot_timer_time, diff))
  52. assert(abs(diff) < 200)
  53. groups = dut.expect(RESTART_REGEX, timeout=3)
  54. start_time = int(groups[0])
  55. print('Timer restarted, time: {} us'.format(start_time))
  56. for i in range(0, 5):
  57. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  58. cur_time = int(groups[0])
  59. diff = start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time
  60. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  61. assert(abs(diff) < 100)
  62. groups = dut.expect(LIGHT_SLEEP_ENTER_REGEX, timeout=2)
  63. sleep_enter_time = int(groups[0])
  64. groups = dut.expect(LIGHT_SLEEP_EXIT_REGEX, timeout=2)
  65. sleep_exit_time = int(groups[0])
  66. sleep_time = sleep_exit_time - sleep_enter_time
  67. print('Enter sleep: {}, exit sleep: {}, slept: {}'.format(
  68. sleep_enter_time, sleep_exit_time, sleep_time))
  69. assert(abs(sleep_time - LIGHT_SLEEP_TIME) < 1000)
  70. for i in range(5, 7):
  71. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  72. cur_time = int(groups[0])
  73. diff = abs(start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time)
  74. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  75. assert(diff < 100)
  76. dut.expect(STOP_REGEX, timeout=2)
  77. if __name__ == '__main__':
  78. test_examples_system_esp_timer()