1from __future__ import unicode_literals
2
3import re
4import time
5
6import ttfw_idf
7from tiny_test_fw import Utility
8
9
10@ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32'])
11def test_examples_ulp(env, extra_data):
12
13    dut = env.get_dut('ulp', 'examples/system/ulp_fsm/ulp')
14    dut.start_app()
15
16    dut.expect_all('Not ULP wakeup, initializing ULP',
17                   'Entering deep sleep',
18                   timeout=30)
19
20    def generate_gpio0_events():
21        for _ in range(5):
22            dut.port_inst.setDTR(True)  # Pulling GPIO0 low using DTR
23            time.sleep(0.25)
24            dut.port_inst.setDTR(False)
25            time.sleep(0.25)
26
27    nvs_value = None
28    for _ in range(5):
29        generate_gpio0_events()
30        dut.expect('ULP wakeup, saving pulse count', timeout=5)
31        Utility.console_log('Woke up...')
32        init_count = int(dut.expect(re.compile(r'Read pulse count from NVS:\s+(\d+)'), timeout=5)[0], 10)
33        assert nvs_value in (init_count, None), ('Read count is {} and previously written value is {}'
34                                                 ''.format(init_count, nvs_value))
35        inc = int(dut.expect(re.compile(r'Pulse count from ULP:\s+(\d+)'), timeout=5)[0], 10)
36        assert inc in (5, 6), 'pulse count is {}'.format(inc)
37        new_count = int(dut.expect(re.compile(r'Wrote updated pulse count to NVS:\s+(\d+)'), timeout=5)[0], 10)
38        assert init_count + inc == new_count, '{} + {} != {}'.format(init_count, inc, new_count)
39        nvs_value = new_count
40        Utility.console_log('Pulse count written to NVS: {}. Entering deep sleep...'.format(nvs_value))
41        dut.expect('Entering deep sleep', timeout=5)
42
43
44if __name__ == '__main__':
45    test_examples_ulp()
46