blob: 5ed13e2a01c9e19857ac77c7d1fd0862775e35f0 [file] [log] [blame]
Norman James6bbe0b52015-10-27 09:18:05 -05001#!/usr/bin/env python
2
3import sys
4import os
5import getopt
Brad Bishop0b380f72016-06-10 00:29:50 -04006import obmc_system_config as System
Brad Bishopa7ac8052016-09-21 09:17:05 -04007import obmc.system
Norman James6bbe0b52015-10-27 09:18:05 -05008
9
10def printUsage():
11 print '\nUsage:'
Brad Bishop0b380f72016-06-10 00:29:50 -040012 print 'gpioutil -n GPIO_NAME [-v value]'
13 print 'gpioutil -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]'
14 print 'gpioutil -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]'
15 print 'gpioutil -l PIN_NAME (lookup pin name only)'
Norman James6bbe0b52015-10-27 09:18:05 -050016 exit(1)
17
18
19
Brad Bishop0b380f72016-06-10 00:29:50 -040020if (len(sys.argv) < 2):
Norman James6bbe0b52015-10-27 09:18:05 -050021 printUsage()
22
Adriana Kobylakff47af22016-06-16 09:27:37 -050023# Pop the command name and point to the args
24sys.argv.pop(0)
25
Norman James6bbe0b52015-10-27 09:18:05 -050026GPIO_SYSFS = '/sys/class/gpio/'
27
28class Gpio:
29 def __init__(self,gpio_num):
30 self.gpio_num = str(gpio_num)
31 self.direction = ''
32 self.interrupt = ''
33 self.exported = False
34
35 def getPath(self,name):
36 return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name
37
38 def export(self):
39 if (os.path.exists(GPIO_SYSFS+'export') == False):
40 raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?")
41 if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False):
42 self.write(GPIO_SYSFS+'export',self.gpio_num)
43
44 self.exported = True
45
46 def setDirection(self,dir):
47 if (self.exported == False):
48 raise Exception("ERROR - Not exported: "+self.getPath())
49
50 self.direction = ''
51 self.interrupt = ''
52 if (dir == 'in' or dir == 'out'):
53 self.direction = dir
54 elif (dir == 'rising' or
55 dir == 'falling' or
56 dir == 'both'):
57 self.direction = 'in'
58 self.interrupt = dir
59 self.write(self.getPath('edge'),self.interrupt)
60 else:
61 raise Exception("ERROR - Invalid Direction: "+dir)
Xo Wang5d4a54e2016-12-02 10:26:15 -080062
63 current_direction = self.read(self.getPath('direction'))
64 if current_direction != self.direction:
65 self.write(self.getPath('direction'),self.direction)
66
Norman James6bbe0b52015-10-27 09:18:05 -050067 def setValue(self,value):
68 if (value == '0'):
69 self.write(self.getPath('value'),'0')
70 elif (value == '1'):
71 self.write(self.getPath('value'),'1')
72 else:
73 raise Exception("ERROR - Invalid value: "+value)
74
75 def getValue(self):
76 return self.read(self.getPath('value'))
77
78 def write(self,path,data):
79 f = open(path,'w')
80 f.write(data)
81 f.close()
82
83
84 def read(self,path):
85 f = open(path,'r')
86 data = f.readline()
87 f.close()
88 return data
89
90
91
92if __name__ == '__main__':
93
94 try:
95 opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:")
96 except getopt.GetoptError:
97 printUsage()
98
99
100
101 lookup = False
102 gpio_name = ""
103 value = ""
104 direction = ""
105 for opt, arg in opts:
106 if opt == '-h':
107 printUsage()
108
109 elif opt in ("-n"):
110 gpio_name = arg
111 lookup = True
112 elif opt in ("-i"):
113 gpio_name = arg
114 elif opt in ("-d"):
115 direction = arg
116 elif opt in ("-v"):
117 value = arg
118 elif opt in ("-p"):
Brad Bishopa7ac8052016-09-21 09:17:05 -0400119 gpio_name = obmc.system.convertGpio(arg)
Norman James6bbe0b52015-10-27 09:18:05 -0500120 elif opt in ("-l"):
Brad Bishopa7ac8052016-09-21 09:17:05 -0400121 gpio_name = obmc.system.convertGpio(arg)
Norman James6bbe0b52015-10-27 09:18:05 -0500122 print gpio_name
123 exit(0)
124
125 gpio_info = {}
126 if (lookup == True):
127 if (System.GPIO_CONFIG.has_key(gpio_name) == False):
128 print "ERROR - GPIO Name doesn't exist"
129 print "Valid names: "
130 for n in System.GPIO_CONFIG:
131 print "\t"+n
132 exit(0)
133 gpio_info = System.GPIO_CONFIG[gpio_name]
134 direction = gpio_info['direction']
Norman James0db72972015-10-28 13:07:04 -0500135 if (gpio_info.has_key('gpio_num')):
136 gpio_name = str(gpio_info['gpio_num'])
137 else:
Brad Bishopa7ac8052016-09-21 09:17:05 -0400138 gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin']))
Norman James6bbe0b52015-10-27 09:18:05 -0500139 print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
140
141
142 ## Rules
143 if (gpio_name == ""):
144 print "ERROR - Gpio not specified"
145 printUsage()
146
147 if (direction == "in" and value != ""):
148 print "ERROR - Value cannot be specified when direction = in"
149 printUsage()
150
151 gpio = Gpio(gpio_name)
152 try:
153 gpio.export()
154 if (direction != ""):
155 gpio.setDirection(direction)
156
157 if (value == ""):
158 print gpio.getValue()
159 else:
160 gpio.setValue(value)
161
162 except Exception as e:
163 print e
164