example_test.py 3.7 KB

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