1# SPDX-License-Identifier:  GPL-2.0+
2#
3# Copyright (c) 2021 Adarsh Babu Kalepalli <opensource.kab@gmail.com>
4# Copyright (c) 2020 Alex Kiernan <alex.kiernan@gmail.com>
5
6import pytest
7import time
8import u_boot_utils
9
10"""
11	test_gpio_input is intended to test the fix 4dbc107f4683.
12	4dbc107f4683:"cmd: gpio: Correct do_gpio() return value"
13"""
14
15@pytest.mark.boardspec('sandbox')
16@pytest.mark.buildconfigspec('cmd_gpio')
17def test_gpio_input(u_boot_console):
18    """Test that gpio input correctly returns the value of a gpio pin."""
19
20    response = u_boot_console.run_command('gpio input 0; echo rc:$?')
21    expected_response = 'rc:0'
22    assert(expected_response in response)
23    response = u_boot_console.run_command('gpio toggle 0; gpio input 0; echo rc:$?')
24    expected_response = 'rc:1'
25    assert(expected_response in response)
26
27@pytest.mark.boardspec('sandbox')
28@pytest.mark.buildconfigspec('cmd_gpio')
29def test_gpio_exit_statuses(u_boot_console):
30    """Test that non-input gpio commands correctly return the command
31    success/failure status."""
32
33    expected_response = 'rc:0'
34    response = u_boot_console.run_command('gpio clear 0; echo rc:$?')
35    assert(expected_response in response)
36    response = u_boot_console.run_command('gpio set 0; echo rc:$?')
37    assert(expected_response in response)
38    response = u_boot_console.run_command('gpio toggle 0; echo rc:$?')
39    assert(expected_response in response)
40    response = u_boot_console.run_command('gpio status -a; echo rc:$?')
41    assert(expected_response in response)
42
43    expected_response = 'rc:1'
44    response = u_boot_console.run_command('gpio nonexistent-command; echo rc:$?')
45    assert(expected_response in response)
46    response = u_boot_console.run_command('gpio input 200; echo rc:$?')
47    assert(expected_response in response)
48
49@pytest.mark.boardspec('sandbox')
50@pytest.mark.buildconfigspec('cmd_gpio')
51def test_gpio_read(u_boot_console):
52    """Test that gpio read correctly sets the variable to the value of a gpio pin."""
53
54    u_boot_console.run_command('gpio clear 0')
55    response = u_boot_console.run_command('gpio read var 0; echo val:$var,rc:$?')
56    expected_response = 'val:0,rc:0'
57    assert(expected_response in response)
58    response = u_boot_console.run_command('gpio toggle 0; gpio read var 0; echo val:$var,rc:$?')
59    expected_response = 'val:1,rc:0'
60    assert(expected_response in response)
61    response = u_boot_console.run_command('setenv var; gpio read var nonexistent-gpio; echo val:$var,rc:$?')
62    expected_response = 'val:,rc:1'
63    assert(expected_response in response)
64
65"""
66Generic Tests for 'gpio' command on sandbox and real hardware.
67The below sequence of tests rely on env__gpio_dev_config for configuration values of gpio pins.
68
69 Configuration data for gpio command.
70 The  set,clear,toggle ,input and status options of 'gpio' command are verified.
71 For sake of verification,A  LED/buzzer could be connected to GPIO pins configured as O/P.
72 Logic level '1'/'0' can be applied onto GPIO pins configured as I/P
73
74
75env__gpio_dev_config = {
76        #the number of 'gpio_str_x' strings should equal to
77        #'gpio_str_count' value
78        'gpio_str_count':4 ,
79        'gpio_str_1': '0',
80        'gpio_str_2': '31',
81        'gpio_str_3': '63',
82        'gpio_str_4': '127',
83        'gpio_op_pin': '64',
84        'gpio_ip_pin_set':'65',
85        'gpio_ip_pin_clear':'66',
86        'gpio_clear_value': 'value is 0',
87        'gpio_set_value': 'value is 1',
88}
89"""
90
91
92@pytest.mark.buildconfigspec('cmd_gpio')
93def test_gpio_status_all_generic(u_boot_console):
94    """Test the 'gpio status' command.
95
96	Displays all gpio pins available on the Board.
97	To verify if the status of pins is displayed or not,
98        the user can configure (gpio_str_count) and verify existence of certain
99	pins.The details of these can be configured in 'gpio_str_n'.
100        of boardenv_* (example above).User can configure any
101        number of such pins and mention that count in 'gpio_str_count'.
102    """
103
104    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
105    if not f:
106        pytest.skip("gpio not configured")
107
108    gpio_str_count = f['gpio_str_count']
109
110    #Display all the GPIO ports
111    cmd = 'gpio status -a'
112    response = u_boot_console.run_command(cmd)
113
114    for str_value in range(1,gpio_str_count + 1):
115        assert f["gpio_str_%d" %(str_value)] in response
116
117
118@pytest.mark.buildconfigspec('cmd_gpio')
119def test_gpio_set_generic(u_boot_console):
120    """Test the 'gpio set' command.
121
122	A specific gpio pin configured by user as output
123        (mentioned in gpio_op_pin) is verified for
124	'set' option
125
126    """
127
128    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
129    if not f:
130        pytest.skip("gpio not configured")
131
132    gpio_pin_adr = f['gpio_op_pin'];
133    gpio_set_value = f['gpio_set_value'];
134
135
136    cmd = 'gpio set ' + gpio_pin_adr
137    response = u_boot_console.run_command(cmd)
138    good_response = gpio_set_value
139    assert good_response in response
140
141
142
143@pytest.mark.buildconfigspec('cmd_gpio')
144def test_gpio_clear_generic(u_boot_console):
145    """Test the 'gpio clear' command.
146
147	A specific gpio pin configured by user as output
148        (mentioned in gpio_op_pin) is verified for
149	'clear' option
150    """
151
152    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
153    if not f:
154        pytest.skip("gpio not configured")
155
156    gpio_pin_adr = f['gpio_op_pin'];
157    gpio_clear_value = f['gpio_clear_value'];
158
159
160    cmd = 'gpio clear ' + gpio_pin_adr
161    response = u_boot_console.run_command(cmd)
162    good_response = gpio_clear_value
163    assert good_response in response
164
165
166@pytest.mark.buildconfigspec('cmd_gpio')
167def test_gpio_toggle_generic(u_boot_console):
168    """Test the 'gpio toggle' command.
169
170	A specific gpio pin configured by user as output
171        (mentioned in gpio_op_pin) is verified for
172	'toggle' option
173    """
174
175
176    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
177    if not f:
178        pytest.skip("gpio not configured")
179
180    gpio_pin_adr = f['gpio_op_pin'];
181    gpio_set_value = f['gpio_set_value'];
182    gpio_clear_value = f['gpio_clear_value'];
183
184    cmd = 'gpio set ' + gpio_pin_adr
185    response = u_boot_console.run_command(cmd)
186    good_response = gpio_set_value
187    assert good_response in response
188
189    cmd = 'gpio toggle ' + gpio_pin_adr
190    response = u_boot_console.run_command(cmd)
191    good_response = gpio_clear_value
192    assert good_response in response
193
194
195@pytest.mark.buildconfigspec('cmd_gpio')
196def test_gpio_input_generic(u_boot_console):
197    """Test the 'gpio input' command.
198
199	Specific gpio pins configured by user as input
200        (mentioned in gpio_ip_pin_set and gpio_ip_pin_clear)
201	is verified for logic '1' and logic '0' states
202    """
203
204    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
205    if not f:
206        pytest.skip("gpio not configured")
207
208    gpio_pin_adr = f['gpio_ip_pin_clear'];
209    gpio_clear_value = f['gpio_clear_value'];
210
211
212    cmd = 'gpio input ' + gpio_pin_adr
213    response = u_boot_console.run_command(cmd)
214    good_response = gpio_clear_value
215    assert good_response in response
216
217
218    gpio_pin_adr = f['gpio_ip_pin_set'];
219    gpio_set_value = f['gpio_set_value'];
220
221
222    cmd = 'gpio input ' + gpio_pin_adr
223    response = u_boot_console.run_command(cmd)
224    good_response = gpio_set_value
225    assert good_response in response
226