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
|
#!/usr/bin/python -tt
# Copyright 2015-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from soc_gpio_table import soc_gpio_table
import openbmc_gpio
import openbmc_gpio_table
import argparse
import logging
import sys
def _get_gpio_table():
gpio = openbmc_gpio_table.SocGPIOTable(soc_gpio_table)
return gpio
def dump_func(args):
gpio = _get_gpio_table()
gpio.dump_functions()
return 0
def config_func(args):
gpio = _get_gpio_table()
try:
gpio.config_function(args.function)
except openbmc_gpio_table.NotSmartEnoughException as e:
print('The code is not smart enough to set function "%s": %s\n'
'Please set the function manually.'
% (args.function, str(e)))
print('The current HW setting for this function is:')
gpio.dump_function(args.function)
return -1
except Exception as e:
print('Failed to set function "%s": %s\n'
'Please set the function manually.'
% (args.function, str(e)))
print('The current HW setting for this function is:')
gpio.dump_function(args.function)
logging.exception('Exception:')
return -2
print('Function "%s" is set' % args.function)
return 0
def export_func(args):
openbmc_gpio.gpio_export(args.gpio, args.shadow)
def set_func(args):
openbmc_gpio.gpio_set(args.gpio, args.value,
change_direction=False if args.keep else True)
def get_func(args):
val = openbmc_gpio.gpio_get(
args.gpio, change_direction=False if args.keep else True)
print('%d' % val)
def info_func(args):
res = openbmc_gpio.gpio_info(args.gpio)
print('GPIO info for %s:' % args.gpio)
print('Path: %s\nShadow: %s\nDirection: %s\nValue: %s'
% (res['path'], res['shadow'], res['direction'], res['value']))
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--debug', action='store_true',
help='Enable debug messages')
subparser = ap.add_subparsers()
dump_parser = subparser.add_parser(
'dump', help='Dump the current HW GPIO settings')
dump_parser.set_defaults(func=dump_func)
config_parser = subparser.add_parser(
'config', help='Configure one HW pin to a function')
config_parser.add_argument(
'function', help='The function name to set')
config_parser.set_defaults(func=config_func)
export_parser = subparser.add_parser(
'export', help='Export a GPIO directory')
export_parser.add_argument(
'gpio', help='The GPIO name, i.e. "A4", or "GPIOD2"')
export_parser.add_argument(
'shadow', default=None,
help='The shadow name given to this GPIO')
export_parser.set_defaults(func=export_func)
set_parser = subparser.add_parser(
'set', help='Set a value for a GPIO')
set_parser.add_argument(
'gpio', help='The GPIO name or number')
set_parser.add_argument(
'value', type=int, choices=[0, 1],
help='The value to set')
set_parser.add_argument(
'-k', '--keep', action='store_true',
help='Keep the GPIO direction')
set_parser.set_defaults(func=set_func)
get_parser = subparser.add_parser(
'get', help='Get a GPIO\'s value')
get_parser.add_argument(
'gpio', help='The GPIO name or number')
get_parser.add_argument(
'-k', '--keep', action='store_true',
help='Keep the GPIO direction')
get_parser.set_defaults(func=get_func)
info_parser = subparser.add_parser(
'info', help='Get a GPIO\'s info')
info_parser.add_argument(
'gpio', help='The GPIO name or number')
info_parser.set_defaults(func=info_func)
args = ap.parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO,
format='%(asctime)s: %(message)s')
return args.func(args)
rc = main()
sys.exit(rc)
|