pytest_light_sleep.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import logging
  4. import time
  5. import pytest
  6. from pytest_embedded import Dut
  7. @pytest.mark.supported_targets
  8. @pytest.mark.generic
  9. def test_light_sleep(dut: Dut) -> None:
  10. ENTERING_SLEEP_STR = 'Entering light sleep'
  11. EXIT_SLEEP_REGEX = r'Returned from light sleep, reason: (\w+), t=(\d+) ms, slept for (\d+) ms'
  12. EXIT_SLEEP_PIN_REGEX = r'Returned from light sleep, reason: (pin), t=(\d+) ms, slept for (\d+) ms'
  13. EXIT_SLEEP_UART_REGEX = r'Returned from light sleep, reason: (uart), t=(\d+) ms, slept for (\d+) ms'
  14. WAITING_FOR_GPIO_STR = r'Waiting for GPIO\d to go high...'
  15. WAKEUP_INTERVAL_MS = 2000
  16. # Ensure DTR and RTS are de-asserted for proper control of GPIO0
  17. dut.serial.proc.setDTR(False)
  18. dut.serial.proc.setRTS(False)
  19. # enter sleep first time
  20. dut.expect_exact(ENTERING_SLEEP_STR, timeout=30)
  21. # don't check timing here, might be cache dependent
  22. dut.expect(EXIT_SLEEP_REGEX)
  23. logging.info('Got first sleep period')
  24. # enter sleep second time
  25. dut.expect_exact(ENTERING_SLEEP_STR)
  26. match = dut.expect(EXIT_SLEEP_REGEX)
  27. logging.info('Got second sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  28. # sleep time error should be less than 1ms
  29. assert match.group(1).decode('utf8') == 'timer' and int(match.group(3)) >= WAKEUP_INTERVAL_MS - 1 and int(match.group(3)) <= WAKEUP_INTERVAL_MS + 1
  30. # this time we'll test gpio wakeup
  31. dut.expect_exact(ENTERING_SLEEP_STR)
  32. logging.info('Pulling GPIO0 low using DTR')
  33. dut.serial.proc.setDTR(True)
  34. time.sleep(1)
  35. match = dut.expect(EXIT_SLEEP_PIN_REGEX)
  36. logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  37. assert int(match.group(3)) < WAKEUP_INTERVAL_MS
  38. dut.expect(WAITING_FOR_GPIO_STR)
  39. logging.info('Is waiting for GPIO...')
  40. dut.serial.proc.setDTR(False)
  41. dut.expect_exact(ENTERING_SLEEP_STR)
  42. logging.info('Went to sleep again')
  43. # Write 'U' to uart, 'U' in ascii is 0x55 which contains 8 edges in total
  44. dut.write('U')
  45. time.sleep(1)
  46. match = dut.expect(EXIT_SLEEP_UART_REGEX)
  47. logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  48. assert int(match.group(3)) < WAKEUP_INTERVAL_MS
  49. logging.info('Went to sleep again')
  50. match = dut.expect(EXIT_SLEEP_REGEX)
  51. assert match.group(1).decode('utf8') == 'timer' and int(match.group(3)) >= WAKEUP_INTERVAL_MS - 1 and int(match.group(3)) <= WAKEUP_INTERVAL_MS + 1
  52. logging.info('Woke up from timer again')