|
| 1 | +#!/usr/bin/python |
| 2 | +#Script to read/write the io based registers |
| 3 | + |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import getopt |
| 7 | +import struct |
| 8 | + |
| 9 | +io_resource='/dev/port' |
| 10 | + |
| 11 | +def usage(): |
| 12 | + ''' This is the Usage Method ''' |
| 13 | + |
| 14 | + print 'Utility for IO read/write' |
| 15 | + print '\t\t io_rd_wr.py --get --offset <offset>' |
| 16 | + print '\t\t io_rd_wr.py --set --val <val> --offset <offset>' |
| 17 | + sys.exit(1) |
| 18 | + |
| 19 | +def io_reg_read(io_resource,offset): |
| 20 | + fd=os.open(io_resource, os.O_RDONLY) |
| 21 | + if(fd<0): |
| 22 | + print 'file open failed %s"%io_resource' |
| 23 | + return |
| 24 | + if(os.lseek(fd, offset, os.SEEK_SET) != offset): |
| 25 | + print 'lseek failed on %s'%io_resource |
| 26 | + return |
| 27 | + buf=os.read(fd,1) |
| 28 | + reg_val1=ord(buf) |
| 29 | + print 'reg value %x'%reg_val1 |
| 30 | + os.close(fd) |
| 31 | + |
| 32 | +def io_reg_write(io_resource,offset,val): |
| 33 | + fd=os.open(io_resource,os.O_RDWR) |
| 34 | + if(fd<0): |
| 35 | + print 'file open failed %s"%io_resource' |
| 36 | + return |
| 37 | + if(os.lseek(fd, offset, os.SEEK_SET) != offset): |
| 38 | + print 'lseek failed on %s'%io_resource |
| 39 | + return |
| 40 | + ret=os.write(fd,struct.pack('B',val)) |
| 41 | + if(ret != 1): |
| 42 | + print 'write failed %d'%ret |
| 43 | + return |
| 44 | + os.close(fd) |
| 45 | + |
| 46 | +def main(argv): |
| 47 | + |
| 48 | + ''' The main function will read the user input from the |
| 49 | + command line argument and process the request ''' |
| 50 | + |
| 51 | + opts = '' |
| 52 | + val = '' |
| 53 | + choice = '' |
| 54 | + resouce = '' |
| 55 | + offset = '' |
| 56 | + |
| 57 | + try: |
| 58 | + opts, args = getopt.getopt(argv, "hgs:" , \ |
| 59 | + ["val=","offset=","help", "get", "set"]) |
| 60 | + |
| 61 | + except getopt.GetoptError: |
| 62 | + usage() |
| 63 | + |
| 64 | + for opt,arg in opts: |
| 65 | + |
| 66 | + if opt in ('-h','--help'): |
| 67 | + choice = 'help' |
| 68 | + |
| 69 | + elif opt in ('-g', '--get'): |
| 70 | + choice = 'get' |
| 71 | + |
| 72 | + elif opt in ('-s', '--set'): |
| 73 | + choice = 'set' |
| 74 | + |
| 75 | + elif opt == '--offset': |
| 76 | + offset = int(arg,16) |
| 77 | + |
| 78 | + elif opt == '--val': |
| 79 | + val = int(arg,16) |
| 80 | + |
| 81 | + if choice == 'get' and offset != '': |
| 82 | + io_reg_read(io_resource,offset) |
| 83 | + |
| 84 | + elif choice == 'set' and offset != '' and val != '': |
| 85 | + io_reg_write(io_resource,offset,val) |
| 86 | + |
| 87 | + else: |
| 88 | + usage() |
| 89 | + |
| 90 | +#Calling the main method |
| 91 | +if __name__ == "__main__": |
| 92 | + main(sys.argv[1:]) |
| 93 | + |
0 commit comments