From c62ebf53165f146d83f402b736832c7431737918 Mon Sep 17 00:00:00 2001
From: kruger <kruger@astron.nl>
Date: Fri, 25 Mar 2022 14:21:20 +0100
Subject: [PATCH] RCU_DTH_PWR added

---
 config/RECVTR.yaml       | 38 ++++++++++++++++++++++++++++++++++----
 i2cserv/i2c.py           |  2 +-
 opcuaserv/smbus_float.py | 27 +++++++++++++++++++++++++++
 opcuaserv/yamlreader.py  | 13 +++++++++++--
 scripts/DTH_test.py      | 21 ++++++++++++++++++---
 5 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/config/RECVTR.yaml b/config/RECVTR.yaml
index 5774e35..c0f3ca0 100644
--- a/config/RECVTR.yaml
+++ b/config/RECVTR.yaml
@@ -10,9 +10,10 @@ drivers:
    type: i2c_array #An array of similar devices connected to an I2C switch
    parent: I2C1
    parameters: [20,22,0,2,4,6,10,12,14,16,18,24,26,28,30,8,7,5,3,1,23,21,19,13,11,9,15,17,31,29,27,25] #RCU lookup table 
+#   parameters: [20,22,0,2,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32] #RCU lookup table 
 #   parameters: [22,0,2,4,6,10,22,0,2,4,6,10,22,0,2,4,6,10,22,0,2,4,6,10,22,0,2,4,6,10,22,0]#repeat 1..7 for RCUH BF test
 #    12,14,16,18,24,26,28,30,8,7,5,3,1,23,21,19,13,11,9,15,17,31,29,27,25] #RCU lookup table 
-   I2Ccut: 5 #error count to disable I2C (14 for LTS?)
+   I2Ccut: 35 #error count to disable I2C (14 for LTS?)
    status: RECVTR_I2C_error
  - name: I2C_HBAT
    type: hba1 #Special driver to manage HBAT1s.
@@ -223,6 +224,9 @@ device_registers:
       address: [0x1221,0x1121]
     - name: CONF
       address: [0x1210,0x1110]
+    - name: PA_CONFIG
+      address: [0x1260,0x1160]
+      store: True
 
 variables:
    - name: RECVTR_monitor_rate
@@ -677,6 +681,20 @@ variables:
      dim2: [3,32]
      mask: ANT_mask
 
+   - name: RCU_DTH_PWR
+     description: RCU Dither source power
+     driver: I2C_RCU
+     devreg:  [DTH1.PA_CONFIG,DTH2.PA_CONFIG,DTH3.PA_CONFIG]
+     width: 48
+     rw:  rw
+     dtype: double
+     scale: si4012_6bytes_to_pwr
+     dim: 96
+     dim2: [3,32]
+     mask: ANT_mask
+#     debug: true
+
+
    - name: RCU_DTH_tune
      driver: I2C_RCU
      devreg:  [DTH1.Tune,DTH2.Tune,DTH3.Tune]
@@ -843,7 +861,7 @@ methods:
       - RCU_ADC_sync: Update
       - RCU_DTH_shutdown: Update
 #      - RCU_DTH_freq: Update
-      - RCU_DTH_on: Update
+#      - RCU_DTH_on: Update
       - RCU_hband_select: Update
       - RCU_DAB_select: Update
       - RCU_firmware_version: Update
@@ -921,11 +939,11 @@ methods:
 #    rw: hidden
     instructions:
      - RCU_DTH_config : [0,0,0]
-     - RCU_DTH_config: Update #debug
+#     - RCU_DTH_config: Update #debug
      - DTH1.Tune :    [0,0] #no tuning
      - DTH2.Tune :    [0,0] #no tuning
      - DTH3.Tune :    [0,0] #no tuning
-     - RCU_DTH_tune: Update #debug
+#     - RCU_DTH_tune: Update #debug
      - DTH1.Start :    [0,1,0,0,1]
      - DTH2.Start :    [0,1,0,0,1]
      - DTH3.Start :    [0,1,0,0,1]
@@ -949,6 +967,18 @@ methods:
 #     - DTH3.State :    [0,0]
      - RCU_DTH_on: Update
 
+  - name: RCU_DTH_restart #restart to update frequency
+    driver: I2C_RCU
+    mask: RCU_mask
+    debug: True
+    instructions:
+     - DTH1.Stop :    [0,0]
+     - DTH2.Stop :    [0,0]
+     - DTH3.Stop :    [0,0]
+     - DTH1.Start :    [0,1,0,0,1]
+     - DTH2.Start :    [0,1,0,0,1]
+     - DTH3.Start :    [0,1,0,0,1]
+
   - name: RCU_DTH_shutdown
     description: shutdown dither source
     driver: I2C_RCU
diff --git a/i2cserv/i2c.py b/i2cserv/i2c.py
index a902c7f..8575d4e 100644
--- a/i2cserv/i2c.py
+++ b/i2cserv/i2c.py
@@ -43,7 +43,7 @@ class i2c(hwdev):
                      logging.debug(str(("I2C set",addr,reg,bytes(bytearray(data)),read)))
                      TXlen=bus.ioctl_write(reg,bytes(bytearray(data)))
                      if TXlen<0:
-                          bus.close;
+                          bus.close()
                           return False;
                      if (TXlen!=len(data)): logging.warn("I2C write error %i/%i"%(TXlen,len(data)))
               bus.close()
diff --git a/opcuaserv/smbus_float.py b/opcuaserv/smbus_float.py
index 9190699..7e02ece 100644
--- a/opcuaserv/smbus_float.py
+++ b/opcuaserv/smbus_float.py
@@ -19,3 +19,30 @@ def smbus_2bytes_to_float_exp13(data):
     return smbus_2bytes_to_float_exp12(data,expo=-13)
 
 
+def si4012_6bytes_to_pwr(data):
+    M=20/79.3
+    Reg0=-25.5
+    Step=-28.9
+    pwr=data>>32
+    if pwr>=256: pwr-=128;
+    pwr=pwr*M+Reg0+Step*(pwr>126)
+    return pwr;
+
+def si4012_6bytes_to_pwr_inv(data):
+    pwr=data;
+    Reg0=-25.5
+    if pwr<Reg0: pwr=Reg0
+    if pwr>Reg0+22: pwr=Reg0+22
+    M=20/79.3
+    Step=-28.9
+    pwr=(pwr-Reg0)/M
+    pwr-=Step/M*(pwr>75)
+    pwr=int(pwr)
+    print(pwr)
+    if pwr>=128: pwr+=128;
+    pwr<<=32
+#    pwr+=0<<16 #Cap
+    pwr+=125<<8 #fAlphaSteps #default
+    pwr+=127    #fBetaSteps #default
+    return pwr
+
diff --git a/opcuaserv/yamlreader.py b/opcuaserv/yamlreader.py
index 094e48b..bcf8792 100644
--- a/opcuaserv/yamlreader.py
+++ b/opcuaserv/yamlreader.py
@@ -134,7 +134,11 @@ class yamlreader(yamlconfig):
         if not(isinstance(data,list)): data=[data];
         if (dtype=="double"): 
                 scale=v.get('scale',1.)
-                data=[int(d/scale) for d in data]
+                if scale in ["si4012_6bytes_to_pwr"]: 
+                     data=[int(eval(scale+"_inv")(d)) for d in data]
+                else:
+                    scale=float(scale)
+                    data=[int(d/scale) for d in data]
         if (dtype=="boolean"): 
                 convert=v.get("convert_unit")
                 if convert: data=[eval("convert_unit."+convert)(d) for d in data]
@@ -149,6 +153,9 @@ class yamlreader(yamlconfig):
                 for a in data: data2.extend(struct.pack('>L',a)[1:])
             elif width==4:
                 data2 = struct.pack(endian+'%sL' % len(data), *data)
+            elif width==6:
+                data2=bytearray()
+                for a in data: data2.extend(struct.pack('>Q',a)[2:])
             elif width==8:
                 data2 = struct.pack(endian+'%sQ' % len(data), *data)
             else:
@@ -211,6 +218,8 @@ class yamlreader(yamlconfig):
                 data2 = [struct.unpack(endian+'L' ,bytearray([0])+data[x*3:x*3+3])[0] for x in range(len(data)//3)]
             elif width==4:
                 data2 = struct.unpack(endian+'%sL' % (len(data)//4), data)
+            elif width==6:
+                data2 = [struct.unpack(endian+'Q' ,bytearray([0,0])+data[x*6:x*6+6])[0] for x in range(len(data)//6)]
             elif width==8:
                 data2 = struct.unpack(endian+'%sQ' % (len(data)//8), data)
             else:
@@ -226,7 +235,7 @@ class yamlreader(yamlconfig):
                 return;
         if dtype=="double": 
                 scale=v.get('scale',1.)
-                if scale in ["smbus_2bytes_to_float","smbus_2bytes_to_float_exp12","smbus_2bytes_to_float_exp13"]: 
+                if scale in ["smbus_2bytes_to_float","smbus_2bytes_to_float_exp12","smbus_2bytes_to_float_exp13","si4012_6bytes_to_pwr"]: 
                      data2=[float(eval(scale)(d)) for d in data2]
                 else:
                     scale=float(scale)
diff --git a/scripts/DTH_test.py b/scripts/DTH_test.py
index 0b6fdb4..4a688ad 100644
--- a/scripts/DTH_test.py
+++ b/scripts/DTH_test.py
@@ -1,7 +1,7 @@
 from test_common import *
 import numpy as np
 
-RCU=[0,1,2,3];
+RCU=[0];
 connect()
 setAntmask(RCU)
 setRCUmask(RCU)
@@ -12,7 +12,7 @@ FCH=0.425;
 F0=97.0
 name="RCU_DTH_freq"
 
-if True:
+if False:
  att=get_value(name+"_R")
  print("freq old:",att[3*RCU[0]:3*RCU[-1]+3])
 
@@ -27,7 +27,22 @@ if True:
  att=get_value(name+"_R")
  print("freq new :",att[3*RCU[0]:3*RCU[-1]+3])
 
+name="RCU_DTH_PWR"
+Pwr=-40.0
+if False:
+ att=get_value(name+"_R")
+ print("pwr old:",att[3*RCU[0]:3*RCU[-1]+3])
+
+ for r in RCU:
+   att[3*r:3*r+3]=[Pwr,Pwr,Pwr]
+ print("pwr set:",att[3*RCU[0]:3*RCU[-1]+3])
+ set_value(name+"_RW",att)
+
+ time.sleep(5)
+ att=get_value(name+"_R")
+ print("pwr new :",att[3*RCU[0]:3*RCU[-1]+3])
+
 #callmethod("RCU_DTH_on")
-#callmethod("RCU_DTH_off")
+callmethod("RCU_DTH_off")
 
 disconnect()
\ No newline at end of file
-- 
GitLab