pytest_esp_timer.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # SPDX-FileCopyrightText: 2022-2023 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.temp_skip_ci(targets=['esp32c6', 'esp32h2'], reason='c6/h2 support TBD')
  21. @pytest.mark.generic
  22. @pytest.mark.parametrize(
  23. 'config',
  24. [
  25. 'rtc',
  26. ],
  27. indirect=True
  28. )
  29. def test_esp_timer(dut: Dut) -> None:
  30. match = dut.expect(STARTING_TIMERS_REGEX)
  31. start_time = int(match.group(1))
  32. logging.info('Start time: {} us'.format(start_time))
  33. match = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  34. assert match.group(1).decode('utf8') == 'periodic' and int(match.group(2)) == INITIAL_TIMER_PERIOD
  35. match = dut.expect(TIMER_DUMP_LINE_REGEX, timeout=2)
  36. assert match.group(1).decode('utf8') == 'one-shot' and int(match.group(2)) == 0
  37. for i in range(0, 5):
  38. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  39. cur_time = int(match.group(1))
  40. diff = start_time + (i + 1) * INITIAL_TIMER_PERIOD - cur_time
  41. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  42. assert abs(diff) < 100
  43. match = dut.expect(ONE_SHOT_REGEX, timeout=3)
  44. one_shot_timer_time = int(match.group(1))
  45. diff = start_time + ONE_SHOT_TIMER_PERIOD - one_shot_timer_time
  46. logging.info('One-shot timer, time: {} us, diff: {}'.format(one_shot_timer_time, diff))
  47. assert abs(diff) < 350
  48. match = dut.expect(RESTART_REGEX, timeout=3)
  49. start_time = int(match.group(1))
  50. logging.info('Timer restarted, time: {} us'.format(start_time))
  51. for i in range(0, 5):
  52. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  53. cur_time = int(match.group(1))
  54. diff = start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time
  55. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  56. assert abs(diff) < 100
  57. match = dut.expect(LIGHT_SLEEP_ENTER_REGEX, timeout=2)
  58. sleep_enter_time = int(match.group(1))
  59. match = dut.expect(LIGHT_SLEEP_EXIT_REGEX, timeout=2)
  60. sleep_exit_time = int(match.group(1))
  61. sleep_time = sleep_exit_time - sleep_enter_time
  62. logging.info('Enter sleep: {}, exit sleep: {}, slept: {}'.format(
  63. sleep_enter_time, sleep_exit_time, sleep_time))
  64. assert abs(sleep_time - LIGHT_SLEEP_TIME) < 1000
  65. for i in range(5, 7):
  66. match = dut.expect(PERIODIC_TIMER_REGEX, timeout=2)
  67. cur_time = int(match.group(1))
  68. diff = abs(start_time + (i + 1) * FINAL_TIMER_PERIOD - cur_time)
  69. logging.info('Callback #{}, time: {} us, diff: {} us'.format(i, cur_time, diff))
  70. assert diff < 100
  71. dut.expect(STOP_REGEX, timeout=2)