example_test.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from __future__ import print_function
  2. import re
  3. import ttfw_idf
  4. STARTING_TIMERS_REGEX = re.compile(r'Started timers, time since boot: (\d+) us')
  5. # name, period, next_alarm, times_started, times_fired, cb_exec_time
  6. TIMER_DUMP_LINE_REGEX = re.compile(r'([\w-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)')
  7. PERIODIC_TIMER_REGEX = re.compile(r'Periodic timer called, time since boot: (\d+) us')
  8. LIGHT_SLEEP_ENTER_REGEX = re.compile(r'Entering light sleep for 0\.5s, time since boot: (\d+) us')
  9. LIGHT_SLEEP_EXIT_REGEX = re.compile(r'Woke up from light sleep, time since boot: (\d+) us')
  10. ONE_SHOT_REGEX = re.compile(r'One\-shot timer called, time since boot: (\d+) us')
  11. RESTART_REGEX = re.compile(r'Restarted periodic timer with 1s period, time since boot: (\d+) us')
  12. STOP_REGEX = re.compile(r'Stopped and deleted timers')
  13. INITIAL_TIMER_PERIOD = 500000
  14. FINAL_TIMER_PERIOD = 1000000
  15. LIGHT_SLEEP_TIME = 500000
  16. ONE_SHOT_TIMER_PERIOD = 5000000
  17. @ttfw_idf.idf_example_test(env_tag='Example_WIFI')
  18. def test_examples_system_esp_timer(env, extra_data):
  19. dut = env.get_dut('esp_timer_example', 'examples/system/esp_timer', dut_class=ttfw_idf.ESP32DUT)
  20. # start test
  21. dut.start_app()
  22. groups = dut.expect(STARTING_TIMERS_REGEX, timeout=30)
  23. start_time = int(groups[0])
  24. print('Start time: {} us'.format(start_time))
  25. groups = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  26. assert(groups[0] == 'periodic' and int(groups[1]) == INITIAL_TIMER_PERIOD)
  27. groups = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  28. assert(groups[0] == 'one-shot' and int(groups[1]) == 0)
  29. for i in range(0, 5):
  30. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  31. cur_time = int(groups[0])
  32. diff = start_time + (i + 1) * INITIAL_TIMER_PERIOD - cur_time
  33. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  34. assert(abs(diff) < 100)
  35. groups = dut.expect(ONE_SHOT_REGEX, timeout=3)
  36. one_shot_timer_time = int(groups[0])
  37. diff = start_time + ONE_SHOT_TIMER_PERIOD - one_shot_timer_time
  38. print('One-shot timer, time: {} us, diff: {}'.format(one_shot_timer_time, diff))
  39. assert(abs(diff) < 200)
  40. groups = dut.expect(RESTART_REGEX, timeout=3)
  41. start_time = int(groups[0])
  42. print('Timer restarted, time: {} us'.format(start_time))
  43. for i in range(0, 5):
  44. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  45. cur_time = int(groups[0])
  46. diff = start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time
  47. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  48. assert(abs(diff) < 100)
  49. groups = dut.expect(LIGHT_SLEEP_ENTER_REGEX, timeout=2)
  50. sleep_enter_time = int(groups[0])
  51. groups = dut.expect(LIGHT_SLEEP_EXIT_REGEX, timeout=2)
  52. sleep_exit_time = int(groups[0])
  53. sleep_time = sleep_exit_time - sleep_enter_time
  54. print('Enter sleep: {}, exit sleep: {}, slept: {}'.format(
  55. sleep_enter_time, sleep_exit_time, sleep_time))
  56. assert(abs(sleep_time - LIGHT_SLEEP_TIME) < 1000)
  57. for i in range(5, 7):
  58. groups = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  59. cur_time = int(groups[0])
  60. diff = abs(start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time)
  61. print('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  62. assert(diff < 100)
  63. dut.expect(STOP_REGEX, timeout=2)
  64. if __name__ == '__main__':
  65. test_examples_system_esp_timer()