Commit af555662 authored by Gaia Grosso's avatar Gaia Grosso
Browse files

Update main.py

parent 49152b43
......@@ -12,12 +12,14 @@ config_dict = {
"property" : {
"property0" : {
"location": "0x00000000",
"bitmask" : "00000000000000000000000000001111"
"offset" : 0,
"width" : 10,
},
"property1" : {
"location": "0x00000000",
"bitmask" : "00000000111000000000000000000000"
"offset" : 10,
"width" : 20
}
}
},
......@@ -25,13 +27,15 @@ config_dict = {
"driver" : "/dev/wz-xdma1_user",
"property" : {
"property0" : {
"location": "0x00000000",
"bitmask" : "00000000000000000000000000001111"
"location": "0x00000001",
"offset" : 0,
"width" : 32
},
"property1" : {
"location": "0x00000001",
"bitmask" : "00000000000000000000000000001111"
"location": "0x00000002",
"offset" : 30,
"width" : 2
}
}
}
......@@ -41,76 +45,37 @@ config_dict = {
actions = ["get", "set"]
def CheckMask(mask_32bits):
list_32mask = [int(bit) for bit in mask_32bits]
variations = [x[i]-x[i+1] for i in range(len(list_32mask)-1)]
n_variations = sum([abs(x) for x in variations])
if n_variations > 2:
return False
elif n_variations == 2:
for x in variantions:
if x<0: return True
if x>0: return False
else: return True
def ReadProperty(device, location, mask_32bits, verbose=False):
def ReadProperty(device, location, offset, width, verbose=False):
read_32bits = ctypes.c_uint(reg.read_bits(device, location))
string_32bits = "{:032b}".format(read_32bits.value)
if verbose:
print("Read: %s"%(string_32bits))
list_32bits = [int(bit) for bit in string_32bits]
list_32mask = [int(bit) for bit in mask_32bits]
selected_bits = []
for i in range(32):
if list_32mask[i]:
selected_bits = [list_32bits[i]] + selected_bits
output = 0
for bit in selected_bits:
output = (output<<1)|bit
strin_out = string_32bits[-1*(offset+width): -1*offset] # remove masked bits
output = int(string_out, 2) # convert selected string to integer
return output
def WriteProperty(device, location, string_mask, write_value):
def WriteProperty(device, location, offset, width, write_value, verbose=False):
read_value = ctypes.c_uint(reg.read_bits(device, location))
string_read = "{:032b}".format(read_value.value)
string_write = "{:032b}".format(write_value.value)
print("Read: %s"%(string_read))
print("Mask: %s"%(string_mask))
print("Write: %s"%(string_write))
list_32read = [int(bit) for bit in string_read]
list_32mask = [int(bit) for bit in string_mask]
list_32write = [int(bit) for bit in string_write]
list_32out = []
j=0
for i in range(32):
if not list_32mask[31-i]:
list_32out = [list_32read[31-i]]+list_32out
else:
list_32out = [list_32write[31-j]]+list_32out
j+=1
output = 0
for bit in list_32out:
output = (output<<1)|bit
output = ctypes.c_uint(output)
string_write = "{:032b}".format(write_value.value<<offset)
if verbose:
print("Read: %s"%(string_read))
print("Write: %s"%(string_write))
string_write = string_read[:-1*(offset+width)] + string_write[-1*(offset+width):-1*offset]] + string_read[-1*offset:]
output = int(string_write, 2)
reg.write_bits(device, location, output)
# Readback as checkout
read_value_new = ctypes.c_uint(reg.read_bits(device, location))
if read_value_new == output: return True
else: return False
def NBitsAllowed(mask): #string of bits
output=0
for i in range(32):
if int(mask[i])==1:
output +=1
print("Number of allowed bits: %i"%(output))
return output
read_value_new = ctypes.c_uint(reg.read_bits(device, location)).value
if read_value_new == output:
return True
else:
print("Readback failed.")
return False
def NBitsNeeded(value):
return math.ceil(math.log(value+1, 2))
if __name__ == '__main__':
parser = argparse.ArgumentParser() #Python tool that makes it easy to create an user-friendly command-line interface
......@@ -124,13 +89,19 @@ if __name__ == '__main__':
prop = args.property
device = config_dict[board]["driver"]
location = config_dict[board]["property"][prop]["location"]
bitmask = config_dict[board]["property"][prop]["bitmask"]
offset = config_dict[board]["property"][prop]["offset"]
width = config_dict[board]["property"][prop]["width"]
action = args.action
value = args.value
isMaskContiguous = CheckMask(bitmask)
if not isMaskContiguous:
print("Bit mask %s for property %s at location (%s) is not valid. Check the configuration file"%(bitmask, prop, location, board))
if offest > 30:
print("Offset %i is out of range 0-30. Check the configuration file"%(offset))
exit()
if width == 0:
print("Width must be grater than 0. Check the configuration file")
if offset+width > 31:
print("Mask ends out of range. Check the configuration file")
exit()
if action == "set":
......@@ -139,13 +110,13 @@ if __name__ == '__main__':
exit()
else:
writevalue = ctypes.c_uint(int(args.value)) #unsigned int (32 bits)
nbits_allowed = NBitsAllowed(bitmask)
nbits_write = NBitsNeeded(value)
nbits_allowed = width # number of allowed bits
nbits_write = value.bit_length() # number of bits needed
if nbits_write > nbits_allowed:
print("Overflow error! value %s doesn't fit in the allowed memory location."%(args.value))
exit()
else:
isWritten = WriteProperty(device, location, bitmask, writevalue)
isWritten = WriteProperty(device, location, offset, width, writevalue)
if is Written:
print('Set property %s from location %s on %s: %i'%(property, location, board, writevalue.value))
else:
......@@ -153,5 +124,5 @@ if __name__ == '__main__':
exit()
elif action == "get":
readvalue = ReadProperty(device, location, bitmask)
readvalue = ReadProperty(device, location, offset, width)
print('Get property %s from location %s on %s: %i'%(property, location, board, readvalue))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment