aboutsummaryrefslogtreecommitdiff
path: root/test/py/tests/test_gpio.py
blob: 0af186f23602c51b1db5bdb3c33d8be6df00ff73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# SPDX-License-Identifier:  GPL-2.0+
#
# Copyright (c) 2021 Adarsh Babu Kalepalli <opensource.kab@gmail.com>
# Copyright (c) 2020 Alex Kiernan <alex.kiernan@gmail.com>

import pytest
import time
import u_boot_utils

"""
	test_gpio_input is intended to test the fix 4dbc107f4683.
	4dbc107f4683:"cmd: gpio: Correct do_gpio() return value"
"""

@pytest.mark.boardspec('sandbox')
@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_input(u_boot_console):
    """Test that gpio input correctly returns the value of a gpio pin."""

    response = u_boot_console.run_command('gpio input 0; echo rc:$?')
    expected_response = 'rc:0'
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio toggle 0; gpio input 0; echo rc:$?')
    expected_response = 'rc:1'
    assert(expected_response in response)

@pytest.mark.boardspec('sandbox')
@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_exit_statuses(u_boot_console):
    """Test that non-input gpio commands correctly return the command
    success/failure status."""

    expected_response = 'rc:0'
    response = u_boot_console.run_command('gpio clear 0; echo rc:$?')
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio set 0; echo rc:$?')
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio toggle 0; echo rc:$?')
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio status -a; echo rc:$?')
    assert(expected_response in response)

    expected_response = 'rc:1'
    response = u_boot_console.run_command('gpio nonexistent-command; echo rc:$?')
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio input 200; echo rc:$?')
    assert(expected_response in response)

@pytest.mark.boardspec('sandbox')
@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_read(u_boot_console):
    """Test that gpio read correctly sets the variable to the value of a gpio pin."""

    u_boot_console.run_command('gpio clear 0')
    response = u_boot_console.run_command('gpio read var 0; echo val:$var,rc:$?')
    expected_response = 'val:0,rc:0'
    assert(expected_response in response)
    response = u_boot_console.run_command('gpio toggle 0; gpio read var 0; echo val:$var,rc:$?')
    expected_response = 'val:1,rc:0'
    assert(expected_response in response)
    response = u_boot_console.run_command('setenv var; gpio read var nonexistent-gpio; echo val:$var,rc:$?')
    expected_response = 'val:,rc:1'
    assert(expected_response in response)

"""
Generic Tests for 'gpio' command on sandbox and real hardware.
The below sequence of tests rely on env__gpio_dev_config for configuration values of gpio pins.

 Configuration data for gpio command.
 The  set,clear,toggle ,input and status options of 'gpio' command are verified.
 For sake of verification,A  LED/buzzer could be connected to GPIO pins configured as O/P.
 Logic level '1'/'0' can be applied onto GPIO pins configured as I/P


env__gpio_dev_config = {
        #the number of 'gpio_str_x' strings should equal to
        #'gpio_str_count' value
        'gpio_str_count':4 ,
        'gpio_str_1': '0',
        'gpio_str_2': '31',
        'gpio_str_3': '63',
        'gpio_str_4': '127',
        'gpio_op_pin': '64',
        'gpio_ip_pin_set':'65',
        'gpio_ip_pin_clear':'66',
        'gpio_clear_value': 'value is 0',
        'gpio_set_value': 'value is 1',
}
"""


@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_status_all_generic(u_boot_console):
    """Test the 'gpio status' command.

	Displays all gpio pins available on the Board.
	To verify if the status of pins is displayed or not,
        the user can configure (gpio_str_count) and verify existence of certain
	pins.The details of these can be configured in 'gpio_str_n'.
        of boardenv_* (example above).User can configure any
        number of such pins and mention that count in 'gpio_str_count'.
    """

    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
    if not f:
        pytest.skip("gpio not configured")

    gpio_str_count = f['gpio_str_count']

    #Display all the GPIO ports
    cmd = 'gpio status -a'
    response = u_boot_console.run_command(cmd)

    for str_value in range(1,gpio_str_count + 1):
        assert f["gpio_str_%d" %(str_value)] in response


@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_set_generic(u_boot_console):
    """Test the 'gpio set' command.

	A specific gpio pin configured by user as output
        (mentioned in gpio_op_pin) is verified for
	'set' option

    """

    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
    if not f:
        pytest.skip("gpio not configured")

    gpio_pin_adr = f['gpio_op_pin'];
    gpio_set_value = f['gpio_set_value'];


    cmd = 'gpio set ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_set_value
    assert good_response in response



@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_clear_generic(u_boot_console):
    """Test the 'gpio clear' command.

	A specific gpio pin configured by user as output
        (mentioned in gpio_op_pin) is verified for
	'clear' option
    """

    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
    if not f:
        pytest.skip("gpio not configured")

    gpio_pin_adr = f['gpio_op_pin'];
    gpio_clear_value = f['gpio_clear_value'];


    cmd = 'gpio clear ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_clear_value
    assert good_response in response


@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_toggle_generic(u_boot_console):
    """Test the 'gpio toggle' command.

	A specific gpio pin configured by user as output
        (mentioned in gpio_op_pin) is verified for
	'toggle' option
    """


    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
    if not f:
        pytest.skip("gpio not configured")

    gpio_pin_adr = f['gpio_op_pin'];
    gpio_set_value = f['gpio_set_value'];
    gpio_clear_value = f['gpio_clear_value'];

    cmd = 'gpio set ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_set_value
    assert good_response in response

    cmd = 'gpio toggle ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_clear_value
    assert good_response in response


@pytest.mark.buildconfigspec('cmd_gpio')
def test_gpio_input_generic(u_boot_console):
    """Test the 'gpio input' command.

	Specific gpio pins configured by user as input
        (mentioned in gpio_ip_pin_set and gpio_ip_pin_clear)
	is verified for logic '1' and logic '0' states
    """

    f = u_boot_console.config.env.get('env__gpio_dev_config',False)
    if not f:
        pytest.skip("gpio not configured")

    gpio_pin_adr = f['gpio_ip_pin_clear'];
    gpio_clear_value = f['gpio_clear_value'];


    cmd = 'gpio input ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_clear_value
    assert good_response in response


    gpio_pin_adr = f['gpio_ip_pin_set'];
    gpio_set_value = f['gpio_set_value'];


    cmd = 'gpio input ' + gpio_pin_adr
    response = u_boot_console.run_command(cmd)
    good_response = gpio_set_value
    assert good_response in response