example_test.py 3.7 KB

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