140a360c2SBrad Bishop#!/usr/bin/env python 240a360c2SBrad Bishop 340a360c2SBrad Bishopimport sys 440a360c2SBrad Bishopimport os 540a360c2SBrad Bishopimport getopt 60b380f7bSBrad Bishopimport obmc_system_config as System 740a360c2SBrad Bishop 840a360c2SBrad Bishop 940a360c2SBrad Bishopdef printUsage(): 1040a360c2SBrad Bishop print '\nUsage:' 110b380f7bSBrad Bishop print 'gpioutil -n GPIO_NAME [-v value]' 120b380f7bSBrad Bishop print 'gpioutil -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]' 130b380f7bSBrad Bishop print 'gpioutil -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]' 140b380f7bSBrad Bishop print 'gpioutil -l PIN_NAME (lookup pin name only)' 1540a360c2SBrad Bishop exit(1) 1640a360c2SBrad Bishop 1740a360c2SBrad Bishop 1840a360c2SBrad Bishop 190b380f7bSBrad Bishopif (len(sys.argv) < 2): 2040a360c2SBrad Bishop printUsage() 2140a360c2SBrad Bishop 22*ff47af29SAdriana Kobylak# Pop the command name and point to the args 23*ff47af29SAdriana Kobylaksys.argv.pop(0) 24*ff47af29SAdriana Kobylak 2540a360c2SBrad BishopGPIO_SYSFS = '/sys/class/gpio/' 2640a360c2SBrad Bishop 2740a360c2SBrad Bishopclass Gpio: 2840a360c2SBrad Bishop def __init__(self,gpio_num): 2940a360c2SBrad Bishop self.gpio_num = str(gpio_num) 3040a360c2SBrad Bishop self.direction = '' 3140a360c2SBrad Bishop self.interrupt = '' 3240a360c2SBrad Bishop self.exported = False 3340a360c2SBrad Bishop 3440a360c2SBrad Bishop def getPath(self,name): 3540a360c2SBrad Bishop return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name 3640a360c2SBrad Bishop 3740a360c2SBrad Bishop def export(self): 3840a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'export') == False): 3940a360c2SBrad Bishop raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?") 4040a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False): 4140a360c2SBrad Bishop self.write(GPIO_SYSFS+'export',self.gpio_num) 4240a360c2SBrad Bishop 4340a360c2SBrad Bishop self.exported = True 4440a360c2SBrad Bishop 4540a360c2SBrad Bishop def setDirection(self,dir): 4640a360c2SBrad Bishop if (self.exported == False): 4740a360c2SBrad Bishop raise Exception("ERROR - Not exported: "+self.getPath()) 4840a360c2SBrad Bishop 4940a360c2SBrad Bishop self.direction = '' 5040a360c2SBrad Bishop self.interrupt = '' 5140a360c2SBrad Bishop if (dir == 'in' or dir == 'out'): 5240a360c2SBrad Bishop self.direction = dir 5340a360c2SBrad Bishop elif (dir == 'rising' or 5440a360c2SBrad Bishop dir == 'falling' or 5540a360c2SBrad Bishop dir == 'both'): 5640a360c2SBrad Bishop self.direction = 'in' 5740a360c2SBrad Bishop self.interrupt = dir 5840a360c2SBrad Bishop self.write(self.getPath('edge'),self.interrupt) 5940a360c2SBrad Bishop else: 6040a360c2SBrad Bishop raise Exception("ERROR - Invalid Direction: "+dir) 6140a360c2SBrad Bishop 6240a360c2SBrad Bishop 6340a360c2SBrad Bishop self.write(self.getPath('direction'),self.direction) 6440a360c2SBrad Bishop 6540a360c2SBrad Bishop def setValue(self,value): 6640a360c2SBrad Bishop if (value == '0'): 6740a360c2SBrad Bishop self.write(self.getPath('value'),'0') 6840a360c2SBrad Bishop elif (value == '1'): 6940a360c2SBrad Bishop self.write(self.getPath('value'),'1') 7040a360c2SBrad Bishop else: 7140a360c2SBrad Bishop raise Exception("ERROR - Invalid value: "+value) 7240a360c2SBrad Bishop 7340a360c2SBrad Bishop def getValue(self): 7440a360c2SBrad Bishop return self.read(self.getPath('value')) 7540a360c2SBrad Bishop 7640a360c2SBrad Bishop def write(self,path,data): 7740a360c2SBrad Bishop f = open(path,'w') 7840a360c2SBrad Bishop f.write(data) 7940a360c2SBrad Bishop f.close() 8040a360c2SBrad Bishop 8140a360c2SBrad Bishop 8240a360c2SBrad Bishop def read(self,path): 8340a360c2SBrad Bishop f = open(path,'r') 8440a360c2SBrad Bishop data = f.readline() 8540a360c2SBrad Bishop f.close() 8640a360c2SBrad Bishop return data 8740a360c2SBrad Bishop 8840a360c2SBrad Bishop 8940a360c2SBrad Bishop 9040a360c2SBrad Bishopif __name__ == '__main__': 9140a360c2SBrad Bishop 9240a360c2SBrad Bishop try: 9340a360c2SBrad Bishop opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") 9440a360c2SBrad Bishop except getopt.GetoptError: 9540a360c2SBrad Bishop printUsage() 9640a360c2SBrad Bishop 9740a360c2SBrad Bishop 9840a360c2SBrad Bishop 9940a360c2SBrad Bishop lookup = False 10040a360c2SBrad Bishop gpio_name = "" 10140a360c2SBrad Bishop value = "" 10240a360c2SBrad Bishop direction = "" 10340a360c2SBrad Bishop for opt, arg in opts: 10440a360c2SBrad Bishop if opt == '-h': 10540a360c2SBrad Bishop printUsage() 10640a360c2SBrad Bishop 10740a360c2SBrad Bishop elif opt in ("-n"): 10840a360c2SBrad Bishop gpio_name = arg 10940a360c2SBrad Bishop lookup = True 11040a360c2SBrad Bishop elif opt in ("-i"): 11140a360c2SBrad Bishop gpio_name = arg 11240a360c2SBrad Bishop elif opt in ("-d"): 11340a360c2SBrad Bishop direction = arg 11440a360c2SBrad Bishop elif opt in ("-v"): 11540a360c2SBrad Bishop value = arg 11640a360c2SBrad Bishop elif opt in ("-p"): 11740a360c2SBrad Bishop gpio_name = System.convertGpio(arg) 11840a360c2SBrad Bishop elif opt in ("-l"): 11940a360c2SBrad Bishop gpio_name = System.convertGpio(arg) 12040a360c2SBrad Bishop print gpio_name 12140a360c2SBrad Bishop exit(0) 12240a360c2SBrad Bishop 12340a360c2SBrad Bishop gpio_info = {} 12440a360c2SBrad Bishop if (lookup == True): 12540a360c2SBrad Bishop if (System.GPIO_CONFIG.has_key(gpio_name) == False): 12640a360c2SBrad Bishop print "ERROR - GPIO Name doesn't exist" 12740a360c2SBrad Bishop print "Valid names: " 12840a360c2SBrad Bishop for n in System.GPIO_CONFIG: 12940a360c2SBrad Bishop print "\t"+n 13040a360c2SBrad Bishop exit(0) 13140a360c2SBrad Bishop gpio_info = System.GPIO_CONFIG[gpio_name] 13240a360c2SBrad Bishop direction = gpio_info['direction'] 13340a360c2SBrad Bishop if (gpio_info.has_key('gpio_num')): 13440a360c2SBrad Bishop gpio_name = str(gpio_info['gpio_num']) 13540a360c2SBrad Bishop else: 13640a360c2SBrad Bishop gpio_name = str(System.convertGpio(gpio_info['gpio_pin'])) 13740a360c2SBrad Bishop print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction 13840a360c2SBrad Bishop 13940a360c2SBrad Bishop 14040a360c2SBrad Bishop ## Rules 14140a360c2SBrad Bishop if (gpio_name == ""): 14240a360c2SBrad Bishop print "ERROR - Gpio not specified" 14340a360c2SBrad Bishop printUsage() 14440a360c2SBrad Bishop 14540a360c2SBrad Bishop if (direction == "in" and value != ""): 14640a360c2SBrad Bishop print "ERROR - Value cannot be specified when direction = in" 14740a360c2SBrad Bishop printUsage() 14840a360c2SBrad Bishop 14940a360c2SBrad Bishop gpio = Gpio(gpio_name) 15040a360c2SBrad Bishop try: 15140a360c2SBrad Bishop gpio.export() 15240a360c2SBrad Bishop if (direction != ""): 15340a360c2SBrad Bishop gpio.setDirection(direction) 15440a360c2SBrad Bishop 15540a360c2SBrad Bishop if (value == ""): 15640a360c2SBrad Bishop print gpio.getValue() 15740a360c2SBrad Bishop else: 15840a360c2SBrad Bishop gpio.setValue(value) 15940a360c2SBrad Bishop 16040a360c2SBrad Bishop except Exception as e: 16140a360c2SBrad Bishop print e 16240a360c2SBrad Bishop 163