pytest_esp_timer.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import logging
  4. import pytest
  5. from pytest_embedded import Dut
  6. STARTING_TIMERS_REGEX = r'Started timers, time since boot: (\d+) us'
  7. # name, period, next_alarm, times_started, times_fired, times_skipped, cb_exec_time
  8. TIMER_DUMP_LINE_REGEX = r'([\w-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)'
  9. PERIODIC_TIMER_REGEX = r'Periodic timer called, time since boot: (\d+) us'
  10. LIGHT_SLEEP_ENTER_REGEX = r'Entering light sleep for 0\.5s, time since boot: (\d+) us'
  11. LIGHT_SLEEP_EXIT_REGEX = r'Woke up from light sleep, time since boot: (\d+) us'
  12. ONE_SHOT_REGEX = r'One\-shot timer called, time since boot: (\d+) us'
  13. RESTART_REGEX = r'Restarted periodic timer with 1s period, time since boot: (\d+) us'
  14. STOP_REGEX = r'Stopped and deleted timers'
  15. INITIAL_TIMER_PERIOD = 500000
  16. FINAL_TIMER_PERIOD = 1000000
  17. LIGHT_SLEEP_TIME = 500000
  18. ONE_SHOT_TIMER_PERIOD = 5000000
  19. @pytest.mark.supported_targets
  20. @pytest.mark.generic
  21. def test_esp_timer(dut: Dut) -> None:
  22. match = dut.expect(STARTING_TIMERS_REGEX)
  23. start_time = int(match.group(1))
  24. logging.info('Start time: {} us'.format(start_time))
  25. match = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  26. assert(match.group(1).decode('utf8') == 'periodic' and int(match.group(2)) == INITIAL_TIMER_PERIOD)
  27. match = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  28. assert(match.group(1).decode('utf8') == 'one-shot' and int(match.group(2)) == 0)
  29. for i in range(0, 5):
  30. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  31. cur_time = int(match.group(1))
  32. diff = start_time + (i + 1) * INITIAL_TIMER_PERIOD - cur_time
  33. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  34. assert(abs(diff) < 100)
  35. match = dut.expect(ONE_SHOT_REGEX, timeout=3)
  36. one_shot_timer_time = int(match.group(1))
  37. diff = start_time + ONE_SHOT_TIMER_PERIOD - one_shot_timer_time
  38. logging.info('One-shot timer, time: {} us, diff: {}'.format(one_shot_timer_time, diff))
  39. assert(abs(diff) < 220)
  40. match = dut.expect(RESTART_REGEX, timeout=3)
  41. start_time = int(match.group(1))
  42. logging.info('Timer restarted, time: {} us'.format(start_time))
  43. for i in range(0, 5):
  44. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  45. cur_time = int(match.group(1))
  46. diff = start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time
  47. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  48. assert(abs(diff) < 100)
  49. match = dut.expect(LIGHT_SLEEP_ENTER_REGEX, timeout=2)
  50. sleep_enter_time = int(match.group(1))
  51. match = dut.expect(LIGHT_SLEEP_EXIT_REGEX, timeout=2)
  52. sleep_exit_time = int(match.group(1))
  53. sleep_time = sleep_exit_time - sleep_enter_time
  54. logging.info('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. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  59. cur_time = int(match.group(1))
  60. diff = abs(start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time)
  61. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  62. assert(diff < 100)
  63. dut.expect(STOP_REGEX, timeout=2)