pytest_light_sleep.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # SPDX-FileCopyrightText: 2022 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. WAITING_FOR_GPIO_STR = r'Waiting for GPIO\d to go high...'
  13. WAKEUP_INTERVAL_MS = 2000
  14. # Ensure DTR and RTS are de-asserted for proper control of GPIO0
  15. dut.serial.proc.setDTR(False)
  16. dut.serial.proc.setRTS(False)
  17. # enter sleep first time
  18. dut.expect_exact(ENTERING_SLEEP_STR, timeout=30)
  19. # don't check timing here, might be cache dependent
  20. dut.expect(EXIT_SLEEP_REGEX)
  21. logging.info('Got first sleep period')
  22. # enter sleep second time
  23. dut.expect_exact(ENTERING_SLEEP_STR)
  24. match = dut.expect(EXIT_SLEEP_REGEX)
  25. logging.info('Got second sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  26. # sleep time error should be less than 1ms
  27. 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)
  28. # this time we'll test gpio wakeup
  29. dut.expect_exact(ENTERING_SLEEP_STR)
  30. logging.info('Pulling GPIO0 low using DTR')
  31. dut.serial.proc.setDTR(True)
  32. time.sleep(1)
  33. match = dut.expect(EXIT_SLEEP_REGEX)
  34. logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  35. assert(match.group(1).decode('utf8') == 'pin' and int(match.group(3)) < WAKEUP_INTERVAL_MS)
  36. dut.expect(WAITING_FOR_GPIO_STR)
  37. logging.info('Is waiting for GPIO...')
  38. dut.serial.proc.setDTR(False)
  39. dut.expect_exact(ENTERING_SLEEP_STR)
  40. logging.info('Went to sleep again')
  41. # Write 'U' to uart, 'U' in ascii is 0x55 which contains 8 edges in total
  42. dut.write('U')
  43. time.sleep(1)
  44. match = dut.expect(EXIT_SLEEP_REGEX)
  45. logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
  46. assert(match.group(1).decode('utf8') == 'uart' and int(match.group(3)) < WAKEUP_INTERVAL_MS)
  47. logging.info('Went to sleep again')
  48. match = dut.expect(EXIT_SLEEP_REGEX)
  49. 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)
  50. logging.info('Woke up from timer again')