new gpio utility
diff --git a/bin/Palmetto.py b/bin/Palmetto.py
index f38fd73..371036f 100755
--- a/bin/Palmetto.py
+++ b/bin/Palmetto.py
@@ -1,8 +1,5 @@
#! /usr/bin/python
-import dbus
-import Openbmc
-
HOME_PATH = './'
CACHE_PATH = HOME_PATH+'cache/'
FLASH_DOWNLOAD_PATH = "/tmp"
@@ -365,4 +362,14 @@
GPIO_CONFIG['MEZZ0_PRESENT'] = { 'gpio_num': 112, 'direction': 'in' }
GPIO_CONFIG['MEZZ1_PRESENT'] = { 'gpio_num': 113, 'direction': 'in' }
+def convertGpio(name):
+ name = name.upper()
+ c = name[0:1]
+ num = name[1:]
+ a = ord(c)-65
+ base = 480 - (int(a/4) * 32)
+ offset = a%4*8 + int(num)
+ return base+offset
+
+
diff --git a/bin/gpio2num b/bin/gpio2num
deleted file mode 100755
index a176ec5..0000000
--- a/bin/gpio2num
+++ /dev/null
@@ -1,18 +0,0 @@
-#! /usr/bin/python
-
-import sys
-
-if (len(sys.argv) <2):
- print "Usage: gpio2num [gpio1] [gpio2] ... [gpion]"
- print "\tExample: gpio2num A2 B4"
- exit(0)
-
-sys.argv.pop(0)
-for name in sys.argv:
- c = name[0:1]
- num = name[1:]
- a = ord(c)-65
- base = 480 - (int(a/4) * 32)
- offset = a%4*8 + int(num)
- gpio_num = base+offset
- print name+" = " + str(gpio_num)
diff --git a/bin/gpioutil b/bin/gpioutil
new file mode 100755
index 0000000..c39c4cf
--- /dev/null
+++ b/bin/gpioutil
@@ -0,0 +1,159 @@
+#!/usr/bin/env python
+
+import sys
+import os
+import getopt
+
+
+def printUsage():
+ print '\nUsage:'
+ print 'gpioutil SYSTEM_NAME -n GPIO_NAME [-v value]'
+ print 'gpioutil SYSTEM_NAME -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]'
+ print 'gpioutil SYSTEM_NAME -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]'
+ print 'gpioutil SYSTEM_NAME -l PIN_NAME (lookup pin name only)'
+ exit(1)
+
+
+
+if (len(sys.argv) < 3):
+ printUsage()
+
+sys.argv.pop(0)
+libr = sys.argv.pop(0)
+System = __import__(libr)
+
+GPIO_SYSFS = '/sys/class/gpio/'
+
+class Gpio:
+ def __init__(self,gpio_num):
+ self.gpio_num = str(gpio_num)
+ self.direction = ''
+ self.interrupt = ''
+ self.exported = False
+
+ def getPath(self,name):
+ return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name
+
+ def export(self):
+ if (os.path.exists(GPIO_SYSFS+'export') == False):
+ raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?")
+ if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False):
+ self.write(GPIO_SYSFS+'export',self.gpio_num)
+
+ self.exported = True
+
+ def setDirection(self,dir):
+ if (self.exported == False):
+ raise Exception("ERROR - Not exported: "+self.getPath())
+
+ self.direction = ''
+ self.interrupt = ''
+ if (dir == 'in' or dir == 'out'):
+ self.direction = dir
+ elif (dir == 'rising' or
+ dir == 'falling' or
+ dir == 'both'):
+ self.direction = 'in'
+ self.interrupt = dir
+ self.write(self.getPath('edge'),self.interrupt)
+ else:
+ raise Exception("ERROR - Invalid Direction: "+dir)
+
+
+ self.write(self.getPath('direction'),self.direction)
+
+ def setValue(self,value):
+ if (value == '0'):
+ self.write(self.getPath('value'),'0')
+ elif (value == '1'):
+ self.write(self.getPath('value'),'1')
+ else:
+ raise Exception("ERROR - Invalid value: "+value)
+
+ def getValue(self):
+ return self.read(self.getPath('value'))
+
+ def write(self,path,data):
+ f = open(path,'w')
+ f.write(data)
+ f.close()
+
+
+ def read(self,path):
+ f = open(path,'r')
+ data = f.readline()
+ f.close()
+ return data
+
+
+
+if __name__ == '__main__':
+
+ try:
+ opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:")
+ except getopt.GetoptError:
+ printUsage()
+
+
+
+ lookup = False
+ gpio_name = ""
+ value = ""
+ direction = ""
+ for opt, arg in opts:
+ if opt == '-h':
+ printUsage()
+
+ elif opt in ("-n"):
+ gpio_name = arg
+ lookup = True
+ elif opt in ("-i"):
+ gpio_name = arg
+ elif opt in ("-d"):
+ direction = arg
+ elif opt in ("-v"):
+ value = arg
+ elif opt in ("-p"):
+ gpio_name = System.convertGpio(arg)
+ elif opt in ("-l"):
+ gpio_name = System.convertGpio(arg)
+ print gpio_name
+ exit(0)
+
+ gpio_info = {}
+ if (lookup == True):
+ if (System.GPIO_CONFIG.has_key(gpio_name) == False):
+ print "ERROR - GPIO Name doesn't exist"
+ print "Valid names: "
+ for n in System.GPIO_CONFIG:
+ print "\t"+n
+ exit(0)
+ gpio_info = System.GPIO_CONFIG[gpio_name]
+ direction = gpio_info['direction']
+ gpio_name = str(gpio_info['gpio_num'])
+ print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
+
+
+ ## Rules
+ if (gpio_name == ""):
+ print "ERROR - Gpio not specified"
+ printUsage()
+
+ if (direction == "in" and value != ""):
+ print "ERROR - Value cannot be specified when direction = in"
+ printUsage()
+
+ gpio = Gpio(gpio_name)
+ try:
+ gpio.export()
+ if (direction != ""):
+ gpio.setDirection(direction)
+
+ if (value == ""):
+ print gpio.getValue()
+ else:
+ gpio.setValue(value)
+
+ except Exception as e:
+ print e
+