blob: d06a9a4d0083ccf6464227b107d9b50c4655ce0e [file] [log] [blame]
Norman James6bbe0b52015-10-27 09:18:05 -05001#!/usr/bin/env python
2
3import sys
4import os
5import getopt
Adriana Kobylak7e480382018-09-24 11:01:42 -05006from glob import glob
7from os.path import join
Brad Bishop0b380f72016-06-10 00:29:50 -04008import obmc_system_config as System
Norman James6bbe0b52015-10-27 09:18:05 -05009
10
11def printUsage():
12 print '\nUsage:'
Brad Bishop0b380f72016-06-10 00:29:50 -040013 print 'gpioutil -n GPIO_NAME [-v value]'
14 print 'gpioutil -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]'
15 print 'gpioutil -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]'
16 print 'gpioutil -l PIN_NAME (lookup pin name only)'
Norman James6bbe0b52015-10-27 09:18:05 -050017 exit(1)
18
19
20
Brad Bishop0b380f72016-06-10 00:29:50 -040021if (len(sys.argv) < 2):
Norman James6bbe0b52015-10-27 09:18:05 -050022 printUsage()
23
Adriana Kobylakff47af22016-06-16 09:27:37 -050024# Pop the command name and point to the args
25sys.argv.pop(0)
26
Norman James6bbe0b52015-10-27 09:18:05 -050027GPIO_SYSFS = '/sys/class/gpio/'
28
Adriana Kobylak7e480382018-09-24 11:01:42 -050029
30def find_gpio_base(path="/sys/class/gpio/"):
31 pattern = "gpiochip*"
32 for gc in glob(join(path, pattern)):
33 with open(join(gc, "label")) as f:
34 label = f.readline().strip()
35 if label == "1e780000.gpio":
36 with open(join(gc, "base")) as f:
37 return int(f.readline().strip())
38 # trigger a file not found exception
39 open(join(path, "gpiochip"))
40
41
42GPIO_BASE = find_gpio_base()
43
44
45def convertGpio(name):
46 offset = int(''.join(list(filter(str.isdigit, name))))
47 port = list(filter(str.isalpha, name.upper()))
48 a = ord(port[-1]) - ord('A')
49 if len(port) > 1:
50 a += 26
51 base = a * 8 + GPIO_BASE
52 return base + offset
53
54
Norman James6bbe0b52015-10-27 09:18:05 -050055class Gpio:
56 def __init__(self,gpio_num):
57 self.gpio_num = str(gpio_num)
58 self.direction = ''
59 self.interrupt = ''
60 self.exported = False
61
62 def getPath(self,name):
63 return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name
64
65 def export(self):
66 if (os.path.exists(GPIO_SYSFS+'export') == False):
67 raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?")
68 if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False):
69 self.write(GPIO_SYSFS+'export',self.gpio_num)
70
71 self.exported = True
72
73 def setDirection(self,dir):
74 if (self.exported == False):
75 raise Exception("ERROR - Not exported: "+self.getPath())
76
77 self.direction = ''
78 self.interrupt = ''
79 if (dir == 'in' or dir == 'out'):
80 self.direction = dir
81 elif (dir == 'rising' or
82 dir == 'falling' or
83 dir == 'both'):
84 self.direction = 'in'
85 self.interrupt = dir
86 self.write(self.getPath('edge'),self.interrupt)
87 else:
88 raise Exception("ERROR - Invalid Direction: "+dir)
Xo Wang5d4a54e2016-12-02 10:26:15 -080089
90 current_direction = self.read(self.getPath('direction'))
91 if current_direction != self.direction:
92 self.write(self.getPath('direction'),self.direction)
93
Norman James6bbe0b52015-10-27 09:18:05 -050094 def setValue(self,value):
95 if (value == '0'):
96 self.write(self.getPath('value'),'0')
97 elif (value == '1'):
98 self.write(self.getPath('value'),'1')
99 else:
100 raise Exception("ERROR - Invalid value: "+value)
101
102 def getValue(self):
Xo Wang3045be32016-12-02 10:47:43 -0800103 return self.read(self.getPath('value'))
Norman James6bbe0b52015-10-27 09:18:05 -0500104
105 def write(self,path,data):
106 f = open(path,'w')
107 f.write(data)
108 f.close()
109
110
111 def read(self,path):
112 f = open(path,'r')
Xo Wang3045be32016-12-02 10:47:43 -0800113 data = f.readline().strip()
Norman James6bbe0b52015-10-27 09:18:05 -0500114 f.close()
115 return data
116
117
118
119if __name__ == '__main__':
120
121 try:
122 opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:")
123 except getopt.GetoptError:
124 printUsage()
125
126
127
128 lookup = False
129 gpio_name = ""
130 value = ""
131 direction = ""
132 for opt, arg in opts:
133 if opt == '-h':
134 printUsage()
135
136 elif opt in ("-n"):
137 gpio_name = arg
138 lookup = True
139 elif opt in ("-i"):
140 gpio_name = arg
141 elif opt in ("-d"):
142 direction = arg
143 elif opt in ("-v"):
144 value = arg
145 elif opt in ("-p"):
Adriana Kobylak7e480382018-09-24 11:01:42 -0500146 gpio_name = convertGpio(arg)
Norman James6bbe0b52015-10-27 09:18:05 -0500147 elif opt in ("-l"):
Adriana Kobylak7e480382018-09-24 11:01:42 -0500148 gpio_name = convertGpio(arg)
Norman James6bbe0b52015-10-27 09:18:05 -0500149 print gpio_name
150 exit(0)
151
152 gpio_info = {}
153 if (lookup == True):
154 if (System.GPIO_CONFIG.has_key(gpio_name) == False):
155 print "ERROR - GPIO Name doesn't exist"
156 print "Valid names: "
157 for n in System.GPIO_CONFIG:
158 print "\t"+n
159 exit(0)
160 gpio_info = System.GPIO_CONFIG[gpio_name]
161 direction = gpio_info['direction']
Norman James0db72972015-10-28 13:07:04 -0500162 if (gpio_info.has_key('gpio_num')):
163 gpio_name = str(gpio_info['gpio_num'])
164 else:
Adriana Kobylak7e480382018-09-24 11:01:42 -0500165 gpio_name = str(convertGpio(gpio_info['gpio_pin']))
Norman James6bbe0b52015-10-27 09:18:05 -0500166 print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
167
168
169 ## Rules
170 if (gpio_name == ""):
171 print "ERROR - Gpio not specified"
172 printUsage()
173
174 if (direction == "in" and value != ""):
175 print "ERROR - Value cannot be specified when direction = in"
176 printUsage()
177
178 gpio = Gpio(gpio_name)
179 try:
180 gpio.export()
181 if (direction != ""):
182 gpio.setDirection(direction)
183
184 if (value == ""):
185 print gpio.getValue()
186 else:
187 gpio.setValue(value)
188
189 except Exception as e:
190 print e
191