140a360c2SBrad Bishop#!/usr/bin/env python 240a360c2SBrad Bishop 340a360c2SBrad Bishopimport sys 440a360c2SBrad Bishopimport os 540a360c2SBrad Bishopimport getopt 6*7e48038aSAdriana Kobylakfrom glob import glob 7*7e48038aSAdriana Kobylakfrom os.path import join 80b380f7bSBrad Bishopimport obmc_system_config as System 940a360c2SBrad Bishop 1040a360c2SBrad Bishop 1140a360c2SBrad Bishopdef printUsage(): 1240a360c2SBrad Bishop print '\nUsage:' 130b380f7bSBrad Bishop print 'gpioutil -n GPIO_NAME [-v value]' 140b380f7bSBrad Bishop print 'gpioutil -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]' 150b380f7bSBrad Bishop print 'gpioutil -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]' 160b380f7bSBrad Bishop print 'gpioutil -l PIN_NAME (lookup pin name only)' 1740a360c2SBrad Bishop exit(1) 1840a360c2SBrad Bishop 1940a360c2SBrad Bishop 2040a360c2SBrad Bishop 210b380f7bSBrad Bishopif (len(sys.argv) < 2): 2240a360c2SBrad Bishop printUsage() 2340a360c2SBrad Bishop 24ff47af29SAdriana Kobylak# Pop the command name and point to the args 25ff47af29SAdriana Kobylaksys.argv.pop(0) 26ff47af29SAdriana Kobylak 2740a360c2SBrad BishopGPIO_SYSFS = '/sys/class/gpio/' 2840a360c2SBrad Bishop 29*7e48038aSAdriana Kobylak 30*7e48038aSAdriana Kobylakdef find_gpio_base(path="/sys/class/gpio/"): 31*7e48038aSAdriana Kobylak pattern = "gpiochip*" 32*7e48038aSAdriana Kobylak for gc in glob(join(path, pattern)): 33*7e48038aSAdriana Kobylak with open(join(gc, "label")) as f: 34*7e48038aSAdriana Kobylak label = f.readline().strip() 35*7e48038aSAdriana Kobylak if label == "1e780000.gpio": 36*7e48038aSAdriana Kobylak with open(join(gc, "base")) as f: 37*7e48038aSAdriana Kobylak return int(f.readline().strip()) 38*7e48038aSAdriana Kobylak # trigger a file not found exception 39*7e48038aSAdriana Kobylak open(join(path, "gpiochip")) 40*7e48038aSAdriana Kobylak 41*7e48038aSAdriana Kobylak 42*7e48038aSAdriana KobylakGPIO_BASE = find_gpio_base() 43*7e48038aSAdriana Kobylak 44*7e48038aSAdriana Kobylak 45*7e48038aSAdriana Kobylakdef convertGpio(name): 46*7e48038aSAdriana Kobylak offset = int(''.join(list(filter(str.isdigit, name)))) 47*7e48038aSAdriana Kobylak port = list(filter(str.isalpha, name.upper())) 48*7e48038aSAdriana Kobylak a = ord(port[-1]) - ord('A') 49*7e48038aSAdriana Kobylak if len(port) > 1: 50*7e48038aSAdriana Kobylak a += 26 51*7e48038aSAdriana Kobylak base = a * 8 + GPIO_BASE 52*7e48038aSAdriana Kobylak return base + offset 53*7e48038aSAdriana Kobylak 54*7e48038aSAdriana Kobylak 5540a360c2SBrad Bishopclass Gpio: 5640a360c2SBrad Bishop def __init__(self,gpio_num): 5740a360c2SBrad Bishop self.gpio_num = str(gpio_num) 5840a360c2SBrad Bishop self.direction = '' 5940a360c2SBrad Bishop self.interrupt = '' 6040a360c2SBrad Bishop self.exported = False 6140a360c2SBrad Bishop 6240a360c2SBrad Bishop def getPath(self,name): 6340a360c2SBrad Bishop return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name 6440a360c2SBrad Bishop 6540a360c2SBrad Bishop def export(self): 6640a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'export') == False): 6740a360c2SBrad Bishop raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?") 6840a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False): 6940a360c2SBrad Bishop self.write(GPIO_SYSFS+'export',self.gpio_num) 7040a360c2SBrad Bishop 7140a360c2SBrad Bishop self.exported = True 7240a360c2SBrad Bishop 7340a360c2SBrad Bishop def setDirection(self,dir): 7440a360c2SBrad Bishop if (self.exported == False): 7540a360c2SBrad Bishop raise Exception("ERROR - Not exported: "+self.getPath()) 7640a360c2SBrad Bishop 7740a360c2SBrad Bishop self.direction = '' 7840a360c2SBrad Bishop self.interrupt = '' 7940a360c2SBrad Bishop if (dir == 'in' or dir == 'out'): 8040a360c2SBrad Bishop self.direction = dir 8140a360c2SBrad Bishop elif (dir == 'rising' or 8240a360c2SBrad Bishop dir == 'falling' or 8340a360c2SBrad Bishop dir == 'both'): 8440a360c2SBrad Bishop self.direction = 'in' 8540a360c2SBrad Bishop self.interrupt = dir 8640a360c2SBrad Bishop self.write(self.getPath('edge'),self.interrupt) 8740a360c2SBrad Bishop else: 8840a360c2SBrad Bishop raise Exception("ERROR - Invalid Direction: "+dir) 8940a360c2SBrad Bishop 905d4a54e8SXo Wang current_direction = self.read(self.getPath('direction')) 915d4a54e8SXo Wang if current_direction != self.direction: 9240a360c2SBrad Bishop self.write(self.getPath('direction'),self.direction) 9340a360c2SBrad Bishop 9440a360c2SBrad Bishop def setValue(self,value): 9540a360c2SBrad Bishop if (value == '0'): 9640a360c2SBrad Bishop self.write(self.getPath('value'),'0') 9740a360c2SBrad Bishop elif (value == '1'): 9840a360c2SBrad Bishop self.write(self.getPath('value'),'1') 9940a360c2SBrad Bishop else: 10040a360c2SBrad Bishop raise Exception("ERROR - Invalid value: "+value) 10140a360c2SBrad Bishop 10240a360c2SBrad Bishop def getValue(self): 10340a360c2SBrad Bishop return self.read(self.getPath('value')) 10440a360c2SBrad Bishop 10540a360c2SBrad Bishop def write(self,path,data): 10640a360c2SBrad Bishop f = open(path,'w') 10740a360c2SBrad Bishop f.write(data) 10840a360c2SBrad Bishop f.close() 10940a360c2SBrad Bishop 11040a360c2SBrad Bishop 11140a360c2SBrad Bishop def read(self,path): 11240a360c2SBrad Bishop f = open(path,'r') 1133045be31SXo Wang data = f.readline().strip() 11440a360c2SBrad Bishop f.close() 11540a360c2SBrad Bishop return data 11640a360c2SBrad Bishop 11740a360c2SBrad Bishop 11840a360c2SBrad Bishop 11940a360c2SBrad Bishopif __name__ == '__main__': 12040a360c2SBrad Bishop 12140a360c2SBrad Bishop try: 12240a360c2SBrad Bishop opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") 12340a360c2SBrad Bishop except getopt.GetoptError: 12440a360c2SBrad Bishop printUsage() 12540a360c2SBrad Bishop 12640a360c2SBrad Bishop 12740a360c2SBrad Bishop 12840a360c2SBrad Bishop lookup = False 12940a360c2SBrad Bishop gpio_name = "" 13040a360c2SBrad Bishop value = "" 13140a360c2SBrad Bishop direction = "" 13240a360c2SBrad Bishop for opt, arg in opts: 13340a360c2SBrad Bishop if opt == '-h': 13440a360c2SBrad Bishop printUsage() 13540a360c2SBrad Bishop 13640a360c2SBrad Bishop elif opt in ("-n"): 13740a360c2SBrad Bishop gpio_name = arg 13840a360c2SBrad Bishop lookup = True 13940a360c2SBrad Bishop elif opt in ("-i"): 14040a360c2SBrad Bishop gpio_name = arg 14140a360c2SBrad Bishop elif opt in ("-d"): 14240a360c2SBrad Bishop direction = arg 14340a360c2SBrad Bishop elif opt in ("-v"): 14440a360c2SBrad Bishop value = arg 14540a360c2SBrad Bishop elif opt in ("-p"): 146*7e48038aSAdriana Kobylak gpio_name = convertGpio(arg) 14740a360c2SBrad Bishop elif opt in ("-l"): 148*7e48038aSAdriana Kobylak gpio_name = convertGpio(arg) 14940a360c2SBrad Bishop print gpio_name 15040a360c2SBrad Bishop exit(0) 15140a360c2SBrad Bishop 15240a360c2SBrad Bishop gpio_info = {} 15340a360c2SBrad Bishop if (lookup == True): 15440a360c2SBrad Bishop if (System.GPIO_CONFIG.has_key(gpio_name) == False): 15540a360c2SBrad Bishop print "ERROR - GPIO Name doesn't exist" 15640a360c2SBrad Bishop print "Valid names: " 15740a360c2SBrad Bishop for n in System.GPIO_CONFIG: 15840a360c2SBrad Bishop print "\t"+n 15940a360c2SBrad Bishop exit(0) 16040a360c2SBrad Bishop gpio_info = System.GPIO_CONFIG[gpio_name] 16140a360c2SBrad Bishop direction = gpio_info['direction'] 16240a360c2SBrad Bishop if (gpio_info.has_key('gpio_num')): 16340a360c2SBrad Bishop gpio_name = str(gpio_info['gpio_num']) 16440a360c2SBrad Bishop else: 165*7e48038aSAdriana Kobylak gpio_name = str(convertGpio(gpio_info['gpio_pin'])) 16640a360c2SBrad Bishop print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction 16740a360c2SBrad Bishop 16840a360c2SBrad Bishop 16940a360c2SBrad Bishop ## Rules 17040a360c2SBrad Bishop if (gpio_name == ""): 17140a360c2SBrad Bishop print "ERROR - Gpio not specified" 17240a360c2SBrad Bishop printUsage() 17340a360c2SBrad Bishop 17440a360c2SBrad Bishop if (direction == "in" and value != ""): 17540a360c2SBrad Bishop print "ERROR - Value cannot be specified when direction = in" 17640a360c2SBrad Bishop printUsage() 17740a360c2SBrad Bishop 17840a360c2SBrad Bishop gpio = Gpio(gpio_name) 17940a360c2SBrad Bishop try: 18040a360c2SBrad Bishop gpio.export() 18140a360c2SBrad Bishop if (direction != ""): 18240a360c2SBrad Bishop gpio.setDirection(direction) 18340a360c2SBrad Bishop 18440a360c2SBrad Bishop if (value == ""): 18540a360c2SBrad Bishop print gpio.getValue() 18640a360c2SBrad Bishop else: 18740a360c2SBrad Bishop gpio.setValue(value) 18840a360c2SBrad Bishop 18940a360c2SBrad Bishop except Exception as e: 19040a360c2SBrad Bishop print e 19140a360c2SBrad Bishop 192