Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import getopt |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 6 | import obmc_system_config as System |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 7 | import obmc.system |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def printUsage(): |
| 11 | print '\nUsage:' |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 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)' |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 16 | exit(1) |
| 17 | |
| 18 | |
| 19 | |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 20 | if (len(sys.argv) < 2): |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 21 | printUsage() |
| 22 | |
Adriana Kobylak | ff47af2 | 2016-06-16 09:27:37 -0500 | [diff] [blame] | 23 | # Pop the command name and point to the args |
| 24 | sys.argv.pop(0) |
| 25 | |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 26 | GPIO_SYSFS = '/sys/class/gpio/' |
| 27 | |
| 28 | class 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 | |
| 64 | self.write(self.getPath('direction'),self.direction) |
| 65 | |
| 66 | def setValue(self,value): |
| 67 | if (value == '0'): |
| 68 | self.write(self.getPath('value'),'0') |
| 69 | elif (value == '1'): |
| 70 | self.write(self.getPath('value'),'1') |
| 71 | else: |
| 72 | raise Exception("ERROR - Invalid value: "+value) |
| 73 | |
| 74 | def getValue(self): |
| 75 | return self.read(self.getPath('value')) |
| 76 | |
| 77 | def write(self,path,data): |
| 78 | f = open(path,'w') |
| 79 | f.write(data) |
| 80 | f.close() |
| 81 | |
| 82 | |
| 83 | def read(self,path): |
| 84 | f = open(path,'r') |
| 85 | data = f.readline() |
| 86 | f.close() |
| 87 | return data |
| 88 | |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | |
| 93 | try: |
| 94 | opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") |
| 95 | except getopt.GetoptError: |
| 96 | printUsage() |
| 97 | |
| 98 | |
| 99 | |
| 100 | lookup = False |
| 101 | gpio_name = "" |
| 102 | value = "" |
| 103 | direction = "" |
| 104 | for opt, arg in opts: |
| 105 | if opt == '-h': |
| 106 | printUsage() |
| 107 | |
| 108 | elif opt in ("-n"): |
| 109 | gpio_name = arg |
| 110 | lookup = True |
| 111 | elif opt in ("-i"): |
| 112 | gpio_name = arg |
| 113 | elif opt in ("-d"): |
| 114 | direction = arg |
| 115 | elif opt in ("-v"): |
| 116 | value = arg |
| 117 | elif opt in ("-p"): |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 118 | gpio_name = obmc.system.convertGpio(arg) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 119 | elif opt in ("-l"): |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 120 | gpio_name = obmc.system.convertGpio(arg) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 121 | print gpio_name |
| 122 | exit(0) |
| 123 | |
| 124 | gpio_info = {} |
| 125 | if (lookup == True): |
| 126 | if (System.GPIO_CONFIG.has_key(gpio_name) == False): |
| 127 | print "ERROR - GPIO Name doesn't exist" |
| 128 | print "Valid names: " |
| 129 | for n in System.GPIO_CONFIG: |
| 130 | print "\t"+n |
| 131 | exit(0) |
| 132 | gpio_info = System.GPIO_CONFIG[gpio_name] |
| 133 | direction = gpio_info['direction'] |
Norman James | 0db7297 | 2015-10-28 13:07:04 -0500 | [diff] [blame] | 134 | if (gpio_info.has_key('gpio_num')): |
| 135 | gpio_name = str(gpio_info['gpio_num']) |
| 136 | else: |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 137 | gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin'])) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 138 | print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction |
| 139 | |
| 140 | |
| 141 | ## Rules |
| 142 | if (gpio_name == ""): |
| 143 | print "ERROR - Gpio not specified" |
| 144 | printUsage() |
| 145 | |
| 146 | if (direction == "in" and value != ""): |
| 147 | print "ERROR - Value cannot be specified when direction = in" |
| 148 | printUsage() |
| 149 | |
| 150 | gpio = Gpio(gpio_name) |
| 151 | try: |
| 152 | gpio.export() |
| 153 | if (direction != ""): |
| 154 | gpio.setDirection(direction) |
| 155 | |
| 156 | if (value == ""): |
| 157 | print gpio.getValue() |
| 158 | else: |
| 159 | gpio.setValue(value) |
| 160 | |
| 161 | except Exception as e: |
| 162 | print e |
| 163 | |