Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
scouting-demonstrator
SCONE
Commits
570f6dbe
Commit
570f6dbe
authored
Dec 09, 2020
by
Gaia Grosso
Browse files
Update main.py
parent
b44dcd03
Changes
1
Hide whitespace changes
Inline
Side-by-side
main.py
View file @
570f6dbe
...
...
@@ -47,17 +47,28 @@ actions = ["get", "set"]
def
ReadProperty
(
device
,
location
,
offset
,
width
,
verbose
=
False
):
read_32bits
=
ctypes
.
c_uint
(
reg
.
read_bits
(
device
,
location
))
read_32bits
=
ctypes
.
c_uint
(
0
);
isRead
=
reg
.
read_bits
(
device
,
location
,
ctypes
.
byref
(
read_32bits
))
if
isRead
==
-
1
:
print
(
"An error occured while executing C function read_bitsò"
)
return
-
1
string_32bits
=
"{:032b}"
.
format
(
read_32bits
.
value
)
if
verbose
:
print
(
"Read: %s"
%
(
string_32bits
))
strin_out
=
string_32bits
[
-
1
*
(
offset
+
width
):
-
1
*
offset
]
# remove masked bits
output
=
int
(
string_out
,
2
)
# convert selected string to integer
if
verbose
:
print
(
"Unmasked result: %i"
%
(
output
))
return
output
def
WriteProperty
(
device
,
location
,
offset
,
width
,
write_value
,
verbose
=
False
):
read_value
=
ctypes
.
c_uint
(
reg
.
read_bits
(
device
,
location
))
read_value
=
ctypes
.
c_uint
(
0
);
isRead
=
reg
.
read_bits
(
device
,
location
,
ctypes
.
byref
(
read_value
))
if
isRead
==
-
1
:
print
(
"An error occured while executing C function read_bitsò"
)
return
-
1
string_read
=
"{:032b}"
.
format
(
read_value
.
value
)
string_write
=
"{:032b}"
.
format
(
write_value
.
value
<<
offset
)
if
verbose
:
...
...
@@ -66,15 +77,18 @@ def WriteProperty(device, location, offset, width, write_value, verbose=False):
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
)
isWritten
=
reg
.
write_bits
(
device
,
location
,
output
)
if
isWritten
==
-
1
:
print
(
"An error occured while executing C function write_bits."
)
return
-
1
# Readback as checkout
read_value_new
=
ctypes
.
c_uint
(
reg
.
read_bits
(
device
,
location
)).
value
if
read_value_new
==
output
:
return
True
return
0
else
:
print
(
"Readback failed."
)
return
False
print
(
"Readback failed. Register not writable."
)
print
(
"Check the reading permission for location %s on board %s"
%
(
location
,
device
))
return
-
1
if
__name__
==
'__main__'
:
...
...
@@ -94,35 +108,39 @@ if __name__ == '__main__':
action
=
args
.
action
value
=
args
.
value
if
offest
>
30
:
print
(
"Offset %i is out of range 0-30. Check the configuration file"
%
(
offset
))
# exceptions:
if
offes
e
t
>
30
:
print
(
"Offset
value
%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"
)
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"
)
print
(
"Mask ends out of range. Check the configuration file
.
"
)
exit
()
if
action
==
"set"
:
if
value
==
None
:
print
(
"action 'set' requires the optional argument --value (-v)."
)
exit
()
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
:
writevalue
=
ctypes
.
c_uint
(
int
(
args
.
value
))
#unsigned int (32 bits)
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
))
writevalue
=
ctypes
.
c_uint
(
int
(
args
.
value
))
#unsigned int (32 bits)
isWritten
=
WriteProperty
(
device
,
location
,
offset
,
width
,
writevalue
)
if
isWritten
==
-
1
:
print
(
'The value could not be updated.'
)
exit
()
else
:
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
:
print
(
'The value could not be updated. Check the reading permission for property %s in board %s'
%
(
prop
,
board
))
exit
()
print
(
'Set property %s from location %s on %s: %i'
%
(
prop
,
location
,
board
,
writevalue
.
value
))
elif
action
==
"get"
:
readvalue
=
ReadProperty
(
device
,
location
,
offset
,
width
)
print
(
'Get property %s from location %s on %s: %i'
%
(
property
,
location
,
board
,
readvalue
))
isRead
=
ReadProperty
(
device
,
location
,
offset
,
width
)
if
isRead
==
-
1
:
print
(
'The register could not be read.'
)
exit
()
else
:
print
(
'Get property %s from location %s on %s: %i'
%
(
prop
,
location
,
board
,
isRead
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment