xref: /openbmc/skeleton/pytools/gpioutil (revision 1902990d)
1#!/usr/bin/env python
2
3import sys
4import os
5import getopt
6import obmc_system_config as System
7import obmc.system
8
9
10def printUsage():
11	print '\nUsage:'
12	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)'
16	exit(1)
17
18
19
20if (len(sys.argv) < 2):
21	printUsage()
22
23# Pop the command name and point to the args
24sys.argv.pop(0)
25
26GPIO_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)
62
63		current_direction = self.read(self.getPath('direction'))
64		if current_direction != self.direction:
65			self.write(self.getPath('direction'),self.direction)
66
67	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().strip()
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"):
119			gpio_name = obmc.system.convertGpio(arg)
120		elif opt in ("-l"):
121			gpio_name = obmc.system.convertGpio(arg)
122			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']
135		if (gpio_info.has_key('gpio_num')):
136			gpio_name = str(gpio_info['gpio_num'])
137		else:
138			gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin']))
139		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
165