Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python_test_scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOFAR2.0
python_test_scripts
Commits
2a86f3fe
Commit
2a86f3fe
authored
4 years ago
by
Gijs Schoonderbeek
Browse files
Options
Downloads
Patches
Plain Diff
SPI script started by Leon Hiemstra write read switch
parent
a4a26a14
No related branches found
No related tags found
1 merge request
!2
Modified the scripts to run on Raspberry Pi.
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
spi_switch_Unb2c.py
+120
-0
120 additions, 0 deletions
spi_switch_Unb2c.py
with
120 additions
and
0 deletions
spi_switch_Unb2c.py
0 → 100644
+
120
−
0
View file @
2a86f3fe
"""
Copyright 2021 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten,
ASTRON Netherlands Institute for Radio Astronomy
Licensed under the Apache License, Version 2.0 (the
"
License
"
);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an
"
AS IS
"
BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Created: 2021-05-19 by Leon Hiemstra, edited by Gijs
file used to read and write to switch registers
"""
import
time
import
spidev
DEBUG
=
False
# We only have SPI bus 0 available to us on the Pi
bus
=
0
#Device is the chip select pin. Set to 0 or 1, depending on the connections
device
=
0
# Enable SPI
spi
=
spidev
.
SpiDev
()
cmd_normal_read
=
0x60
cmd_normal_write
=
0x61
def
read_register
(
addr
):
cmd
=
cmd_normal_read
if
0
:
spi
.
writebytes
([
cmd
,
addr
])
ret
=
spi
.
readbytes
(
2
)
else
:
ret
=
spi
.
xfer2
([
cmd
,
addr
,
0
,
0
,
0
,
0
])
if
DEBUG
:
stri
=
'
read_register 0x{:0>2x} = 0x{:0>2x}, 0x{:0>2x}
'
.
format
(
addr
,
ret
[
2
],
ret
[
3
])
print
(
stri
)
return
ret
[
2
:]
def
write_register
(
addr
,
data
):
cmd
=
cmd_normal_write
if
0
:
spi
.
writebytes
([
cmd
,
addr
,
data
])
ret
=
spi
.
readbytes
(
2
)
else
:
ret
=
spi
.
xfer2
([
cmd
,
addr
,
data
])
if
DEBUG
:
stri
=
'
write_register 0x{:0>2x} = 0x{:0>2x}
'
.
format
(
addr
,
data
)
print
(
stri
)
def
read_switch
(
page
,
addr
):
stri
=
'
<< read switch from page: 0x{0:0>2x}, address: 0x{1:0>2x}
'
.
format
(
page
,
addr
)
ret
=
spi
.
xfer2
([
cmd_normal_write
,
0xff
,
page
])
ret
=
spi
.
xfer2
([
cmd_normal_read
,
addr
,
0
,
0
,
0
,
0
])
ret
=
read_register
(
0xfe
)
if
(
ret
[
2
]
&
0xf0
)
==
0xa0
:
ret
=
read_register
(
0xf0
)
ret
.
reverse
()
stri
+=
"
data 0x
"
for
byte
in
ret
:
stri
+=
"
{:0>2x}
"
.
format
(
byte
)
print
(
stri
)
else
:
print
(
"
read error
"
)
def
write_switch_bytes
(
page
,
addr
,
data
):
stri
=
'
> write switch from page: 0x{0:0>2x}, address: 0x{1:0>2x} data 0x
'
.
format
(
page
,
addr
)
pl_bytes
=
data
pl_bytes
.
reverse
()
for
data_byte
in
pl_bytes
:
add_stri
=
"
{0:0>2x}
"
.
format
(
data_byte
)
stri
+=
add_stri
print
(
stri
)
read_register
(
0xfe
)
ret
=
spi
.
xfer2
([
cmd_normal_write
,
0xff
,
page
])
wr_bytes
=
[
cmd_normal_write
,
addr
]
wr_bytes
.
extend
(
data
)
ret
=
spi
.
xfer2
(
wr_bytes
)
print
(
"
SPI status reg 0x{:0>2x}
"
.
format
(
read_register
(
0xfe
)[
2
]))
if
DEBUG
:
read_register
(
0xfe
)
read_register
(
0xfe
)
read_register
(
addr
)
# Open a connection to a specific bus and device (chip select pin)
spi
.
open
(
bus
,
device
)
# Set SPI speed and mode
spi
.
max_speed_hz
=
1000000
#spi.max_speed_hz = 50000
spi
.
mode
=
1
print
(
"
write and read led register
"
)
write_switch_bytes
(
0x00
,
0x24
,
[
0x20
,
0x02
])
# lsb first
read_switch
(
0x00
,
0x24
)
print
(
"
links status register
"
)
read_switch
(
0x01
,
0x00
)
print
(
"
strap resistors
"
)
read_switch
(
0x01
,
0x70
)
for
cnt
in
range
(
4
):
print
(
"
Port status phy nr {}
"
.
format
(
cnt
))
read_switch
(
0x01
,
0x20
+
cnt
)
print
(
"
Tx pause status
"
)
read_switch
(
0x01
,
0x14
)
print
(
"
Rx pause status
"
)
read_switch
(
0x01
,
0x18
)
write_switch_bytes
(
0x30
,
0x00
,
[
0x40
])
# lsb first
read_switch
(
0x30
,
0x00
)
spi
.
close
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment