From 2a86f3fe590fc0ac88c734c8c84c9ad842695009 Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Thu, 20 May 2021 14:36:12 +0200
Subject: [PATCH] SPI script started by Leon Hiemstra write read switch

---
 spi_switch_Unb2c.py | 120 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)
 create mode 100644 spi_switch_Unb2c.py

diff --git a/spi_switch_Unb2c.py b/spi_switch_Unb2c.py
new file mode 100644
index 0000000..b5e2dc3
--- /dev/null
+++ b/spi_switch_Unb2c.py
@@ -0,0 +1,120 @@
+"""
+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
-- 
GitLab