blob: fa0af5f82b2c38e09aff8fe96d18319e36999eb4 [file] [log] [blame]
Adarsh Babu Kalepalli6e4f9c52021-05-31 16:23:51 +05301# 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>
Alex Kiernan1073c232020-03-11 08:46:29 +00005
6import pytest
Adarsh Babu Kalepalli6e4f9c52021-05-31 16:23:51 +05307import 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"""
Alex Kiernan1073c232020-03-11 08:46:29 +000014
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)
Adarsh Babu Kalepalli6e4f9c52021-05-31 16:23:51 +053048
Diego Rondini5ffde632022-04-11 12:02:09 +020049@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 response = u_boot_console.run_command('gpio read var 0; echo val:$var,rc:$?')
55 expected_response = 'val:0,rc:0'
56 assert(expected_response in response)
57 response = u_boot_console.run_command('gpio toggle 0; gpio read var 0; echo val:$var,rc:$?')
58 expected_response = 'val:1,rc:0'
59 assert(expected_response in response)
60 response = u_boot_console.run_command('setenv var; gpio read var nonexistent-gpio; echo val:$var,rc:$?')
61 expected_response = 'val:,rc:1'
62 assert(expected_response in response)
Adarsh Babu Kalepalli6e4f9c52021-05-31 16:23:51 +053063
64"""
65Generic Tests for 'gpio' command on sandbox and real hardware.
66The below sequence of tests rely on env__gpio_dev_config for configuration values of gpio pins.
67
68 Configuration data for gpio command.
69 The set,clear,toggle ,input and status options of 'gpio' command are verified.
70 For sake of verification,A LED/buzzer could be connected to GPIO pins configured as O/P.
71 Logic level '1'/'0' can be applied onto GPIO pins configured as I/P
72
73
74env__gpio_dev_config = {
75 #the number of 'gpio_str_x' strings should equal to
76 #'gpio_str_count' value
77 'gpio_str_count':4 ,
78 'gpio_str_1': '0',
79 'gpio_str_2': '31',
80 'gpio_str_3': '63',
81 'gpio_str_4': '127',
82 'gpio_op_pin': '64',
83 'gpio_ip_pin_set':'65',
84 'gpio_ip_pin_clear':'66',
85 'gpio_clear_value': 'value is 0',
86 'gpio_set_value': 'value is 1',
87}
88"""
89
90
91@pytest.mark.buildconfigspec('cmd_gpio')
92def test_gpio_status_all_generic(u_boot_console):
93 """Test the 'gpio status' command.
94
95 Displays all gpio pins available on the Board.
96 To verify if the status of pins is displayed or not,
97 the user can configure (gpio_str_count) and verify existence of certain
98 pins.The details of these can be configured in 'gpio_str_n'.
99 of boardenv_* (example above).User can configure any
100 number of such pins and mention that count in 'gpio_str_count'.
101 """
102
103 f = u_boot_console.config.env.get('env__gpio_dev_config',False)
104 if not f:
105 pytest.skip("gpio not configured")
106
107 gpio_str_count = f['gpio_str_count']
108
109 #Display all the GPIO ports
110 cmd = 'gpio status -a'
111 response = u_boot_console.run_command(cmd)
112
113 for str_value in range(1,gpio_str_count + 1):
114 assert f["gpio_str_%d" %(str_value)] in response
115
116
117@pytest.mark.buildconfigspec('cmd_gpio')
118def test_gpio_set_generic(u_boot_console):
119 """Test the 'gpio set' command.
120
121 A specific gpio pin configured by user as output
122 (mentioned in gpio_op_pin) is verified for
123 'set' option
124
125 """
126
127 f = u_boot_console.config.env.get('env__gpio_dev_config',False)
128 if not f:
129 pytest.skip("gpio not configured")
130
131 gpio_pin_adr = f['gpio_op_pin'];
132 gpio_set_value = f['gpio_set_value'];
133
134
135 cmd = 'gpio set ' + gpio_pin_adr
136 response = u_boot_console.run_command(cmd)
137 good_response = gpio_set_value
138 assert good_response in response
139
140
141
142@pytest.mark.buildconfigspec('cmd_gpio')
143def test_gpio_clear_generic(u_boot_console):
144 """Test the 'gpio clear' command.
145
146 A specific gpio pin configured by user as output
147 (mentioned in gpio_op_pin) is verified for
148 'clear' option
149 """
150
151 f = u_boot_console.config.env.get('env__gpio_dev_config',False)
152 if not f:
153 pytest.skip("gpio not configured")
154
155 gpio_pin_adr = f['gpio_op_pin'];
156 gpio_clear_value = f['gpio_clear_value'];
157
158
159 cmd = 'gpio clear ' + gpio_pin_adr
160 response = u_boot_console.run_command(cmd)
161 good_response = gpio_clear_value
162 assert good_response in response
163
164
165@pytest.mark.buildconfigspec('cmd_gpio')
166def test_gpio_toggle_generic(u_boot_console):
167 """Test the 'gpio toggle' command.
168
169 A specific gpio pin configured by user as output
170 (mentioned in gpio_op_pin) is verified for
171 'toggle' option
172 """
173
174
175 f = u_boot_console.config.env.get('env__gpio_dev_config',False)
176 if not f:
177 pytest.skip("gpio not configured")
178
179 gpio_pin_adr = f['gpio_op_pin'];
180 gpio_set_value = f['gpio_set_value'];
181 gpio_clear_value = f['gpio_clear_value'];
182
183 cmd = 'gpio set ' + gpio_pin_adr
184 response = u_boot_console.run_command(cmd)
185 good_response = gpio_set_value
186 assert good_response in response
187
188 cmd = 'gpio toggle ' + gpio_pin_adr
189 response = u_boot_console.run_command(cmd)
190 good_response = gpio_clear_value
191 assert good_response in response
192
193
194@pytest.mark.buildconfigspec('cmd_gpio')
195def test_gpio_input_generic(u_boot_console):
196 """Test the 'gpio input' command.
197
198 Specific gpio pins configured by user as input
199 (mentioned in gpio_ip_pin_set and gpio_ip_pin_clear)
200 is verified for logic '1' and logic '0' states
201 """
202
203 f = u_boot_console.config.env.get('env__gpio_dev_config',False)
204 if not f:
205 pytest.skip("gpio not configured")
206
207 gpio_pin_adr = f['gpio_ip_pin_clear'];
208 gpio_clear_value = f['gpio_clear_value'];
209
210
211 cmd = 'gpio input ' + gpio_pin_adr
212 response = u_boot_console.run_command(cmd)
213 good_response = gpio_clear_value
214 assert good_response in response
215
216
217 gpio_pin_adr = f['gpio_ip_pin_set'];
218 gpio_set_value = f['gpio_set_value'];
219
220
221 cmd = 'gpio input ' + gpio_pin_adr
222 response = u_boot_console.run_command(cmd)
223 good_response = gpio_set_value
224 assert good_response in response