example_test.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from __future__ import print_function
  2. import re
  3. import os
  4. import sys
  5. import time
  6. try:
  7. import IDF
  8. from IDF.IDFDUT import ESP32DUT
  9. except ImportError:
  10. test_fw_path = os.getenv('TEST_FW_PATH')
  11. if test_fw_path and test_fw_path not in sys.path:
  12. sys.path.insert(0, test_fw_path)
  13. import IDF
  14. ENTERING_SLEEP_STR = 'Entering light sleep'
  15. EXIT_SLEEP_REGEX = re.compile(r'Returned from light sleep, reason: (\w+), t=(\d+) ms, slept for (\d+) ms')
  16. WAITING_FOR_GPIO_STR = 'Waiting for GPIO0 to go high...'
  17. WAKEUP_INTERVAL_MS = 2000
  18. @IDF.idf_example_test(env_tag='Example_WIFI')
  19. def test_examples_system_light_sleep(env, extra_data):
  20. dut = env.get_dut('light_sleep_example', 'examples/system/light_sleep', dut_class=ESP32DUT)
  21. dut.start_app()
  22. # Ensure DTR and RTS are de-asserted for proper control of GPIO0
  23. dut.port_inst.setDTR(False)
  24. dut.port_inst.setRTS(False)
  25. # enter sleep first time
  26. dut.expect(ENTERING_SLEEP_STR, timeout=30)
  27. # don't check timing here, might be cache dependent
  28. dut.expect(EXIT_SLEEP_REGEX)
  29. print('Got first sleep period')
  30. # enter sleep second time
  31. dut.expect(ENTERING_SLEEP_STR)
  32. groups = dut.expect(EXIT_SLEEP_REGEX)
  33. print('Got second sleep period, wakeup from {}, slept for {}'.format(groups[0], groups[2]))
  34. # sleep time error should be less than 1ms
  35. assert(groups[0] == 'timer' and int(groups[2]) == WAKEUP_INTERVAL_MS)
  36. # this time we'll test gpio wakeup
  37. dut.expect(ENTERING_SLEEP_STR)
  38. print('Pulling GPIO0 low using DTR')
  39. dut.port_inst.setDTR(True)
  40. time.sleep(1)
  41. groups = dut.expect(EXIT_SLEEP_REGEX)
  42. print('Got third sleep period, wakeup from {}, slept for {}'.format(groups[0], groups[2]))
  43. assert(groups[0] == 'pin' and int(groups[2]) < WAKEUP_INTERVAL_MS)
  44. dut.expect(WAITING_FOR_GPIO_STR)
  45. print('Is waiting for GPIO...')
  46. dut.port_inst.setDTR(False)
  47. dut.expect(ENTERING_SLEEP_STR)
  48. print('Went to sleep again')
  49. groups = dut.expect(EXIT_SLEEP_REGEX)
  50. assert(groups[0] == 'timer' and int(groups[2]) == WAKEUP_INTERVAL_MS)
  51. print('Woke up from timer again')
  52. if __name__ == '__main__':
  53. test_examples_system_light_sleep()