2020-10-17 14:31:55 -05:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# Script to read/write the io based registers
|
2018-08-13 05:31:11 -05:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import getopt
|
|
|
|
import struct
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
io_resource = '/dev/port'
|
2018-08-13 05:31:11 -05:00
|
|
|
|
|
|
|
def usage():
|
|
|
|
''' This is the Usage Method '''
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
print('Utility for IO read/write')
|
|
|
|
print('\t\t io_rd_wr.py --get --offset <offset>')
|
|
|
|
print('\t\t io_rd_wr.py --set --val <val> --offset <offset>')
|
2018-08-13 05:31:11 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def io_reg_read(io_resource, offset):
|
|
|
|
fd = os.open(io_resource, os.O_RDONLY)
|
|
|
|
if(fd < 0):
|
|
|
|
print('file open failed %s' % io_resource)
|
2018-08-13 05:31:11 -05:00
|
|
|
return
|
|
|
|
if(os.lseek(fd, offset, os.SEEK_SET) != offset):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('lseek failed on %s' % io_resource)
|
2018-08-13 05:31:11 -05:00
|
|
|
return
|
2020-10-17 14:31:55 -05:00
|
|
|
buf = os.read(fd, 1)
|
|
|
|
reg_val1 = ord(buf)
|
|
|
|
print('reg value %x' % reg_val1)
|
2018-08-13 05:31:11 -05:00
|
|
|
os.close(fd)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def io_reg_write(io_resource, offset, val):
|
|
|
|
fd = os.open(io_resource, os.O_RDWR)
|
|
|
|
if(fd < 0):
|
|
|
|
print('file open failed %s' % io_resource)
|
2018-08-13 05:31:11 -05:00
|
|
|
return
|
|
|
|
if(os.lseek(fd, offset, os.SEEK_SET) != offset):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('lseek failed on %s' % io_resource)
|
2018-08-13 05:31:11 -05:00
|
|
|
return
|
2020-10-17 14:31:55 -05:00
|
|
|
ret = os.write(fd, struct.pack('B', val))
|
2018-08-13 05:31:11 -05:00
|
|
|
if(ret != 1):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('write failed %d' % ret)
|
2018-08-13 05:31:11 -05:00
|
|
|
return
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
|
|
|
''' The main function will read the user input from the
|
|
|
|
command line argument and process the request '''
|
|
|
|
|
|
|
|
opts = ''
|
|
|
|
val = ''
|
|
|
|
choice = ''
|
|
|
|
offset = ''
|
|
|
|
|
|
|
|
try:
|
2020-10-17 14:31:55 -05:00
|
|
|
opts, args = getopt.getopt(argv, "hgs:",
|
|
|
|
["val=", "offset=", "help", "get", "set"])
|
2018-08-13 05:31:11 -05:00
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
for opt, arg in opts:
|
2018-08-13 05:31:11 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
if opt in ('-h', '--help'):
|
2018-08-13 05:31:11 -05:00
|
|
|
choice = 'help'
|
|
|
|
|
|
|
|
elif opt in ('-g', '--get'):
|
|
|
|
choice = 'get'
|
|
|
|
|
|
|
|
elif opt in ('-s', '--set'):
|
|
|
|
choice = 'set'
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--offset':
|
|
|
|
offset = int(arg, 16)
|
2018-08-13 05:31:11 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--val':
|
|
|
|
val = int(arg, 16)
|
2018-08-13 05:31:11 -05:00
|
|
|
|
|
|
|
if choice == 'get' and offset != '':
|
2020-10-17 14:31:55 -05:00
|
|
|
io_reg_read(io_resource, offset)
|
2018-08-13 05:31:11 -05:00
|
|
|
|
|
|
|
elif choice == 'set' and offset != '' and val != '':
|
2020-10-17 14:31:55 -05:00
|
|
|
io_reg_write(io_resource, offset, val)
|
2018-08-13 05:31:11 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
# Calling the main method
|
2018-08-13 05:31:11 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|