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) |
Xo Wang | 5d4a54e | 2016-12-02 10:26:15 -0800 | [diff] [blame] | 62 | |
| 63 | current_direction = self.read(self.getPath('direction')) |
| 64 | if current_direction != self.direction: |
| 65 | self.write(self.getPath('direction'),self.direction) |
| 66 | |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 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): |
Xo Wang | 3045be3 | 2016-12-02 10:47:43 -0800 | [diff] [blame] | 76 | return self.read(self.getPath('value')) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 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') |
Xo Wang | 3045be3 | 2016-12-02 10:47:43 -0800 | [diff] [blame] | 86 | data = f.readline().strip() |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 87 | f.close() |
| 88 | return data |
| 89 | |
| 90 | |
| 91 | |
| 92 | if __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"): |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 119 | gpio_name = obmc.system.convertGpio(arg) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 120 | elif opt in ("-l"): |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 121 | gpio_name = obmc.system.convertGpio(arg) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 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'] |
Norman James | 0db7297 | 2015-10-28 13:07:04 -0500 | [diff] [blame] | 135 | if (gpio_info.has_key('gpio_num')): |
| 136 | gpio_name = str(gpio_info['gpio_num']) |
| 137 | else: |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 138 | gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin'])) |
Norman James | 6bbe0b5 | 2015-10-27 09:18:05 -0500 | [diff] [blame] | 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 | |