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 # GPIO pin list to test gpio functionality for each pins, pin should be 89 # pin names (str) 90 'gpio_pin_list': ['gpio@1000031', 'gpio@1000032', 'gpio@20000033'], 91 # GPIO input output list for shorted gpio pins to test gpio 92 # functionality for each of pairs, where the first element is 93 # configured as input and second as output 94 'gpio_ip_op_list': [['gpio0', 'gpio1'], ['gpio2', 'gpio3']], 95} 96""" 97 98 99@pytest.mark.buildconfigspec('cmd_gpio') 100def test_gpio_status_all_generic(u_boot_console): 101 """Test the 'gpio status' command. 102 103 Displays all gpio pins available on the Board. 104 To verify if the status of pins is displayed or not, 105 the user can configure (gpio_str_count) and verify existence of certain 106 pins.The details of these can be configured in 'gpio_str_n'. 107 of boardenv_* (example above).User can configure any 108 number of such pins and mention that count in 'gpio_str_count'. 109 """ 110 111 f = u_boot_console.config.env.get('env__gpio_dev_config',False) 112 if not f: 113 pytest.skip("gpio not configured") 114 115 gpio_str_count = f['gpio_str_count'] 116 117 #Display all the GPIO ports 118 cmd = 'gpio status -a' 119 response = u_boot_console.run_command(cmd) 120 121 for str_value in range(1,gpio_str_count + 1): 122 assert f["gpio_str_%d" %(str_value)] in response 123 124 125@pytest.mark.buildconfigspec('cmd_gpio') 126def test_gpio_set_generic(u_boot_console): 127 """Test the 'gpio set' command. 128 129 A specific gpio pin configured by user as output 130 (mentioned in gpio_op_pin) is verified for 131 'set' option 132 133 """ 134 135 f = u_boot_console.config.env.get('env__gpio_dev_config',False) 136 if not f: 137 pytest.skip("gpio not configured") 138 139 gpio_pin_adr = f['gpio_op_pin']; 140 gpio_set_value = f['gpio_set_value']; 141 142 143 cmd = 'gpio set ' + gpio_pin_adr 144 response = u_boot_console.run_command(cmd) 145 good_response = gpio_set_value 146 assert good_response in response 147 148 149 150@pytest.mark.buildconfigspec('cmd_gpio') 151def test_gpio_clear_generic(u_boot_console): 152 """Test the 'gpio clear' command. 153 154 A specific gpio pin configured by user as output 155 (mentioned in gpio_op_pin) is verified for 156 'clear' option 157 """ 158 159 f = u_boot_console.config.env.get('env__gpio_dev_config',False) 160 if not f: 161 pytest.skip("gpio not configured") 162 163 gpio_pin_adr = f['gpio_op_pin']; 164 gpio_clear_value = f['gpio_clear_value']; 165 166 167 cmd = 'gpio clear ' + gpio_pin_adr 168 response = u_boot_console.run_command(cmd) 169 good_response = gpio_clear_value 170 assert good_response in response 171 172 173@pytest.mark.buildconfigspec('cmd_gpio') 174def test_gpio_toggle_generic(u_boot_console): 175 """Test the 'gpio toggle' command. 176 177 A specific gpio pin configured by user as output 178 (mentioned in gpio_op_pin) is verified for 179 'toggle' option 180 """ 181 182 183 f = u_boot_console.config.env.get('env__gpio_dev_config',False) 184 if not f: 185 pytest.skip("gpio not configured") 186 187 gpio_pin_adr = f['gpio_op_pin']; 188 gpio_set_value = f['gpio_set_value']; 189 gpio_clear_value = f['gpio_clear_value']; 190 191 cmd = 'gpio set ' + gpio_pin_adr 192 response = u_boot_console.run_command(cmd) 193 good_response = gpio_set_value 194 assert good_response in response 195 196 cmd = 'gpio toggle ' + gpio_pin_adr 197 response = u_boot_console.run_command(cmd) 198 good_response = gpio_clear_value 199 assert good_response in response 200 201 202@pytest.mark.buildconfigspec('cmd_gpio') 203def test_gpio_input_generic(u_boot_console): 204 """Test the 'gpio input' command. 205 206 Specific gpio pins configured by user as input 207 (mentioned in gpio_ip_pin_set and gpio_ip_pin_clear) 208 is verified for logic '1' and logic '0' states 209 """ 210 211 f = u_boot_console.config.env.get('env__gpio_dev_config',False) 212 if not f: 213 pytest.skip("gpio not configured") 214 215 gpio_pin_adr = f['gpio_ip_pin_clear']; 216 gpio_clear_value = f['gpio_clear_value']; 217 218 219 cmd = 'gpio input ' + gpio_pin_adr 220 response = u_boot_console.run_command(cmd) 221 good_response = gpio_clear_value 222 assert good_response in response 223 224 225 gpio_pin_adr = f['gpio_ip_pin_set']; 226 gpio_set_value = f['gpio_set_value']; 227 228 229 cmd = 'gpio input ' + gpio_pin_adr 230 response = u_boot_console.run_command(cmd) 231 good_response = gpio_set_value 232 assert good_response in response 233 234@pytest.mark.buildconfigspec('cmd_gpio') 235def test_gpio_pins_generic(u_boot_console): 236 """Test various gpio related functionality, such as the input, set, clear, 237 and toggle for the set of gpio pin list. 238 239 Specific set of gpio pins (by mentioning gpio pin name) configured as 240 input (mentioned as 'gpio_pin_list') to be tested for multiple gpio 241 commands. 242 """ 243 244 f = u_boot_console.config.env.get('env__gpio_dev_config', False) 245 if not f: 246 pytest.skip('gpio not configured') 247 248 gpio_pins = f.get('gpio_pin_list', None) 249 if not gpio_pins: 250 pytest.skip('gpio pin list are not configured') 251 252 for gpin in gpio_pins: 253 # gpio input 254 u_boot_console.run_command(f'gpio input {gpin}') 255 expected_response = f'{gpin}: input:' 256 response = u_boot_console.run_command(f'gpio status -a {gpin}') 257 assert expected_response in response 258 259 # gpio set 260 u_boot_console.run_command(f'gpio set {gpin}') 261 expected_response = f'{gpin}: output: 1' 262 response = u_boot_console.run_command(f'gpio status -a {gpin}') 263 assert expected_response in response 264 265 # gpio clear 266 u_boot_console.run_command(f'gpio clear {gpin}') 267 expected_response = f'{gpin}: output: 0' 268 response = u_boot_console.run_command(f'gpio status -a {gpin}') 269 assert expected_response in response 270 271 # gpio toggle 272 u_boot_console.run_command(f'gpio toggle {gpin}') 273 expected_response = f'{gpin}: output: 1' 274 response = u_boot_console.run_command(f'gpio status -a {gpin}') 275 assert expected_response in response 276 277@pytest.mark.buildconfigspec('cmd_gpio') 278def test_gpio_pins_input_output_generic(u_boot_console): 279 """Test gpio related functionality such as input and output for the list of 280 shorted gpio pins provided as a pair of input and output pins. This test 281 will fail, if the gpio pins are not shorted properly. 282 283 Specific set of shorted gpio pins (by mentioning gpio pin name) 284 configured as input and output (mentioned as 'gpio_ip_op_list') as a 285 pair to be tested for gpio input output case. 286 """ 287 288 f = u_boot_console.config.env.get('env__gpio_dev_config', False) 289 if not f: 290 pytest.skip('gpio not configured') 291 292 gpio_pins = f.get('gpio_ip_op_list', None) 293 if not gpio_pins: 294 pytest.skip('gpio pin list for input and output are not configured') 295 296 for gpins in gpio_pins: 297 u_boot_console.run_command(f'gpio input {gpins[0]}') 298 expected_response = f'{gpins[0]}: input:' 299 response = u_boot_console.run_command(f'gpio status -a {gpins[0]}') 300 assert expected_response in response 301 302 u_boot_console.run_command(f'gpio set {gpins[1]}') 303 expected_response = f'{gpins[1]}: output:' 304 response = u_boot_console.run_command(f'gpio status -a {gpins[1]}') 305 assert expected_response in response 306 307 u_boot_console.run_command(f'gpio clear {gpins[1]}') 308 expected_response = f'{gpins[0]}: input: 0' 309 response = u_boot_console.run_command(f'gpio status -a {gpins[0]}') 310 assert expected_response in response 311 312 u_boot_console.run_command(f'gpio set {gpins[1]}') 313 expected_response = f'{gpins[0]}: input: 1' 314 response = u_boot_console.run_command(f'gpio status -a {gpins[0]}') 315 assert expected_response in response 316