pytest_light_sleep.py 2.7 KB

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