2023-01-17 05:50:39 -05:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-03-07 22:15:37 -05:00
|
|
|
# SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pytest_embedded import Dut
|
|
|
|
|
|
|
|
|
2022-05-21 15:28:15 -04:00
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.esp32s2
|
|
|
|
@pytest.mark.esp32s3
|
|
|
|
@pytest.mark.esp32c3
|
2023-02-01 03:37:04 -05:00
|
|
|
@pytest.mark.esp32c2
|
2023-01-17 05:50:39 -05:00
|
|
|
@pytest.mark.esp32c6
|
2022-03-07 22:15:37 -05:00
|
|
|
@pytest.mark.generic
|
|
|
|
def test_light_sleep(dut: Dut) -> None:
|
|
|
|
|
|
|
|
ENTERING_SLEEP_STR = 'Entering light sleep'
|
|
|
|
EXIT_SLEEP_REGEX = r'Returned from light sleep, reason: (\w+), t=(\d+) ms, slept for (\d+) ms'
|
2022-07-18 23:03:16 -04:00
|
|
|
EXIT_SLEEP_PIN_REGEX = r'Returned from light sleep, reason: (pin), t=(\d+) ms, slept for (\d+) ms'
|
|
|
|
EXIT_SLEEP_UART_REGEX = r'Returned from light sleep, reason: (uart), t=(\d+) ms, slept for (\d+) ms'
|
2022-03-07 22:15:37 -05:00
|
|
|
WAITING_FOR_GPIO_STR = r'Waiting for GPIO\d to go high...'
|
|
|
|
|
|
|
|
WAKEUP_INTERVAL_MS = 2000
|
|
|
|
|
|
|
|
# Ensure DTR and RTS are de-asserted for proper control of GPIO0
|
|
|
|
dut.serial.proc.setDTR(False)
|
|
|
|
dut.serial.proc.setRTS(False)
|
|
|
|
|
|
|
|
# enter sleep first time
|
|
|
|
dut.expect_exact(ENTERING_SLEEP_STR, timeout=30)
|
|
|
|
# don't check timing here, might be cache dependent
|
|
|
|
dut.expect(EXIT_SLEEP_REGEX)
|
|
|
|
logging.info('Got first sleep period')
|
|
|
|
|
|
|
|
# enter sleep second time
|
|
|
|
dut.expect_exact(ENTERING_SLEEP_STR)
|
|
|
|
match = dut.expect(EXIT_SLEEP_REGEX)
|
|
|
|
logging.info('Got second sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
|
|
|
|
# sleep time error should be less than 1ms
|
2022-08-10 03:01:57 -04:00
|
|
|
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
|
2022-03-07 22:15:37 -05:00
|
|
|
|
|
|
|
# this time we'll test gpio wakeup
|
|
|
|
dut.expect_exact(ENTERING_SLEEP_STR)
|
|
|
|
logging.info('Pulling GPIO0 low using DTR')
|
|
|
|
dut.serial.proc.setDTR(True)
|
|
|
|
time.sleep(1)
|
2022-07-18 23:03:16 -04:00
|
|
|
match = dut.expect(EXIT_SLEEP_PIN_REGEX)
|
2022-03-07 22:15:37 -05:00
|
|
|
logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
|
2022-08-10 03:01:57 -04:00
|
|
|
assert int(match.group(3)) < WAKEUP_INTERVAL_MS
|
2022-03-07 22:15:37 -05:00
|
|
|
|
|
|
|
dut.expect(WAITING_FOR_GPIO_STR)
|
|
|
|
logging.info('Is waiting for GPIO...')
|
|
|
|
|
|
|
|
dut.serial.proc.setDTR(False)
|
|
|
|
dut.expect_exact(ENTERING_SLEEP_STR)
|
|
|
|
logging.info('Went to sleep again')
|
|
|
|
|
|
|
|
# Write 'U' to uart, 'U' in ascii is 0x55 which contains 8 edges in total
|
|
|
|
dut.write('U')
|
|
|
|
time.sleep(1)
|
2022-07-18 23:03:16 -04:00
|
|
|
match = dut.expect(EXIT_SLEEP_UART_REGEX)
|
2022-03-07 22:15:37 -05:00
|
|
|
logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(match.group(1), match.group(3)))
|
2022-08-10 03:01:57 -04:00
|
|
|
assert int(match.group(3)) < WAKEUP_INTERVAL_MS
|
2022-03-07 22:15:37 -05:00
|
|
|
logging.info('Went to sleep again')
|
|
|
|
|
|
|
|
match = dut.expect(EXIT_SLEEP_REGEX)
|
2022-08-10 03:01:57 -04:00
|
|
|
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
|
2022-03-07 22:15:37 -05:00
|
|
|
logging.info('Woke up from timer again')
|