pytest_esp_timer.py 3.4 KB

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