From 0132a0827229bdc438ea00948fb3104139094643 Mon Sep 17 00:00:00 2001
From: kruger <kruger@astron.nl>
Date: Sat, 3 Jul 2021 16:49:49 +0200
Subject: [PATCH] RCU multiple switches added for 32 channels

---
 config/RECVTR.yaml    |  2 +-
 config/UNB2TR.yaml    |  4 +++
 i2cserv/i2c_array.py  |  5 ++--
 i2cserv/i2c_switch.py | 58 ++++++++++++++++++++++++++++++++-----------
 scripts/ADCreset.py   | 18 ++++++++------
 scripts/setpin.py     | 16 ++++++++++++
 6 files changed, 77 insertions(+), 26 deletions(-)
 create mode 100644 scripts/setpin.py

diff --git a/config/RECVTR.yaml b/config/RECVTR.yaml
index 7dce229..f22105d 100644
--- a/config/RECVTR.yaml
+++ b/config/RECVTR.yaml
@@ -4,7 +4,7 @@ description: "1234"
 drivers:
  - name: I2C1 #TCA9548
    type: i2c_switch
-   devreg: [0x70]
+   devreg: [0x70,0x71,0x72,0x73]
    parameters: [1] #I2C port number
  - name: I2C_RCU 
    type: i2c_array #An array of similar devices connected to an I2C switch
diff --git a/config/UNB2TR.yaml b/config/UNB2TR.yaml
index 101d468..1f7c3ed 100644
--- a/config/UNB2TR.yaml
+++ b/config/UNB2TR.yaml
@@ -47,6 +47,10 @@ drivers:
    type: gpio
    parameters: [19,26] 
 
+ - name: GPIO2
+   type: gpio
+   parameters: [11,13] 
+
   
 #This is the I2C devices in the RCU
 device_registers:
diff --git a/i2cserv/i2c_array.py b/i2cserv/i2c_array.py
index 379ad08..77a11fb 100644
--- a/i2cserv/i2c_array.py
+++ b/i2cserv/i2c_array.py
@@ -33,10 +33,11 @@ class i2c_array(i2c_dev):
 
     def SetSwitchMask(self,mask):
         m=0;
+        self.conf['parentcls'].ClearNewChannel();
         for RCUi in range(self.N):
            if (mask[RCUi]) and (self.I2Cmask[RCUi]<=self.I2Ccut): 
-               m|=1<<self.RCU_Switch1[RCUi];
-        self.conf['parentcls'].SetChannel(m);
+               self.conf['parentcls'].AddNewChannel(self.RCU_Switch1[RCUi]);
+        self.conf['parentcls'].UpdateNewChannel();
 
     def SetGetVarValueMask(self,var1,data,mask,getalso=True):
         Step,Step2=GetSteps(var1);
diff --git a/i2cserv/i2c_switch.py b/i2cserv/i2c_switch.py
index c77bb90..ad8df31 100644
--- a/i2cserv/i2c_switch.py
+++ b/i2cserv/i2c_switch.py
@@ -6,26 +6,54 @@ from .i2c import i2c
 class i2c_switch(i2c):
     def __init__(self,config):
         i2c.__init__(self,config)
-        self.SWaddr=config['devreg'][0]['addr']
-        self.CurrentChannel=0
-        self.NoSwitch=self.SWaddr==0;
-        logging.info("i2c switch at address "+str(self.SWaddr))
+#        self.SWaddr=config['devreg'][0]['addr']
+        self.SWcnt=len(config['devreg']);
+        self.SWaddrs=[config['devreg'][x]['addr'] for x in range(self.SWcnt)]; 
+        self.CurrentChannel=[-1 for x in range(self.SWcnt)];
+        self.NoSwitch=self.SWcnt==0;
+        S=''; 
+        for x in range(self.SWcnt): S+=str(self.SWaddrs[x])+",";
+        logging.info("i2c switch at address "+S)
         if self.NoSwitch:
            logging.warn("i2c switch disabled!")
 
     def SetSW1(self,channelbit):
-        channel=(0 if (channelbit>5) else 1<<(channelbit)) #LTS
-        if (channel)==self.CurrentChannel: return True;
-        logging.debug("SetChannelbit=%i" % channelbit)
-        self.CurrentChannel=channel
-        return (True if self.NoSwitch else self.i2csetget(self.SWaddr,[channel]))
+#        channel=(0 if (channelbit>5) else 1<<(channelbit)) #LTS
+        if self.NoSwitch: return True;
+        SWn=channelbit>>3;
+        channel=1<<(channelbit & 0x07)
+        for x in range(self.SWcnt):
+           if x==SWn:
+              if (channel)!=self.CurrentChannel[x]:
+                 self.CurrentChannel[x]=channel
+                 logging.debug("SetChannel addr %i = val %i" % (SWn,channel));
+                 if not(self.i2csetget(self.SWaddrs[x],[channel])): return False;
+           else:
+              if self.CurrentChannel[x]!=0:
+                 logging.debug("SetChannel addr %i = val %i" % (x,0));
+                 self.CurrentChannel[x]=0
+                 if not(self.i2csetget(self.SWaddrs[x],[0])): return False;
+        return True
 
-    def SetChannel(self,channel):
-        channel&=0x3F;#LTS
-        if (channel)==self.CurrentChannel: return True;
-        logging.debug("SetChannel=%i" % channel)
-        self.CurrentChannel=channel
-        return (True if self.NoSwitch else self.i2csetget(self.SWaddr,[channel]))
+    def ClearNewChannel(self):
+        self.newChannel=[0 for x in self.SWaddrs];
+
+    def AddNewChannel(self,channelbit):
+        SWn=channelbit>>3;
+        channel=1<<(channelbit & 0x07)
+        self.newChannel[SWn]|=channel;
+
+    def UpdateNewChannel(self):
+#        channel&=0x3F;#LTS
+        if self.NoSwitch: return True;
+        for x in range(self.SWcnt):
+              if self.newChannel[x]!=self.CurrentChannel[x]:
+                self.CurrentChannel[x]=self.newChannel[x];
+                logging.debug("SetChannel2 addr %i = val %i" % (x,self.newChannel[x]));
+                if not(self.i2csetget(self.SWaddrs[x],[self.newChannel[x]])): return False;
+#        logging.debug("SetChannel=%i" % channel)
+#        self.CurrentChannel=channel
+        return True
 
 #    def I2Ccallback(self,RCU,addr,data,reg=None,read=0):
 #        self.callback1(addr,data,reg,read)    
diff --git a/scripts/ADCreset.py b/scripts/ADCreset.py
index 97c4491..f607378 100644
--- a/scripts/ADCreset.py
+++ b/scripts/ADCreset.py
@@ -1,23 +1,25 @@
 from test_common import *
-RCUs=[0,1,2,3];
+
+RCUs=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
 
 connect()
+
 setRCUmask(RCUs)
 
-def wait(var1="RECVTR_translator_busy_R"):
-  for x in range(20):
-    busy=get_value(var1)
+#def wait(var1="RECVTR_translator_busy_R"):
+#  for x in range(20):#
+#    busy=get_value(var1)
 #    print(busy)
-    if not(busy): break
-    time.sleep(0.1)
-  print("Time=",x*0.1,"s")
+#    if not(busy): break
+#    time.sleep(0.1)
+ # print("Time=",x*0.1,"s")
 
 #callmethod("RCU_off")
 #wait()
 #exit()
 #time.sleep(2)
 callmethod("RCU_on")
-wait()
+#wait()
 #callmethod("RCU_on")
 #time.sleep(1)
 #callmethod("ADC_on")
diff --git a/scripts/setpin.py b/scripts/setpin.py
new file mode 100644
index 0000000..6eae8cf
--- /dev/null
+++ b/scripts/setpin.py
@@ -0,0 +1,16 @@
+#from gpiozero import LED,Button
+import RPi.GPIO as GPIO
+#SCL=3
+#SDA=2
+#PIN=13
+PIN=11
+
+GPIO.setmode(GPIO.BCM)
+
+GPIO.setup(PIN,GPIO.OUT)
+GPIO.output(PIN,1)
+#print("SDA ",GPIO.input(SDA))
+#print("SCL ",GPIO.input(SCL))
+
+GPIO.cleanup()
+
-- 
GitLab