From fa8342a7546f74b65b5551946c869011f83b1a81 Mon Sep 17 00:00:00 2001
From: marcofilho <marco.filho@ess.eu>
Date: Fri, 3 Jan 2025 15:56:35 +0100
Subject: [PATCH] Add DAC index setting for all vmms of all hybrids

removed proof of concept. Made it work for all VMMs of all hybrids.
---
 vmmTblApp/Db/vmm.template     | 20 ++++++++++++++++++++
 vmmTblApp/Db/vmm_tbl.template | 28 ----------------------------
 vmmTblApp/src/vmm_tbl.cpp     | 28 +++++++++++++++++++++++++---
 vmmTblApp/src/vmm_tbl.h       |  1 +
 4 files changed, 46 insertions(+), 31 deletions(-)

diff --git a/vmmTblApp/Db/vmm.template b/vmmTblApp/Db/vmm.template
index 4ed97dc..8c64b2d 100644
--- a/vmmTblApp/Db/vmm.template
+++ b/vmmTblApp/Db/vmm.template
@@ -33,3 +33,23 @@ record(acalcout, "$(P)$(R)$(HYB)$(VMM)$(C)#All$(CH)-S") {
     field(INAA, "$(P)$(R)$(HYB)$(VMM)$(C)$(CH)-S")
     field(OUT,  "$(P)$(R)$(HYB)$(VMM)$(C)$(CH)-S PP")
 }
+
+# 0-63 are for reading straight from these channels DACs.
+# 64, 65, 66 and 67 are respectively for reading
+# Pulser, Threshold, Bandgap and Temperature respectively.
+
+record(ao, "$(P)$(R)$(HYB)$(VMM)$(C)AnalogMon-S"){
+    field(DESC, "Select analog monitor (sm)")
+    field(DTYP, "asynInt32")
+    field(DRVL, "0")
+    field(DRVH, "67")
+    field(OUT,  "@asyn($(PORT),$(ADDR),$(TIMEOUT))HYB_$(HYB)_$(VMM)_ANALOGMON")
+}
+
+record(ai, "$(P)$(R)$(HYB)$(VMM)$(C)AnalogMon-R"){
+    field(DESC, "Read analog monitor index (sm)")
+    field(DTYP, "asynInt32")
+    field(PINI, "YES")
+    field(SCAN, "I/O Intr")
+    field(INP,  "@asyn($(PORT),$(ADDR),$(TIMEOUT))HYB_$(HYB)_$(VMM)_ANALOGMON")
+}
\ No newline at end of file
diff --git a/vmmTblApp/Db/vmm_tbl.template b/vmmTblApp/Db/vmm_tbl.template
index 350c05d..036a525 100644
--- a/vmmTblApp/Db/vmm_tbl.template
+++ b/vmmTblApp/Db/vmm_tbl.template
@@ -1,31 +1,3 @@
-record(mbbo, "$(P)$(R)AnalogMon-VMM0-S") {
-    field(DESC, "Select analog monitor (sm)")
-    field(ZRST, "Pulser DAC [mV]")
-    field(ZRVL, "0")
-    field(ONST, "Threshold DAC [mV]")
-    field(ONVL, "1")
-    field(TWST, "Bandgap ref [mV]")
-    field(TWVL, "2")
-    field(THST, "Temperature [degC]")
-    field(THVL, "3")
-    field(DTYP, "asynInt32")
-    field(OUT,  "@asyn($(PORT),$(ADDR),$(TIMEOUT))SEL_ANALOG_MONITOR_VMM0")
-}
-
-record(mbbo, "$(P)$(R)AnalogMon-VMM1-S") {
-    field(DESC, "Select analog monitor (sm)")
-    field(ZRST, "Pulser DAC [mV]")
-    field(ZRVL, "0")
-    field(ONST, "Threshold DAC [mV]")
-    field(ONVL, "1")
-    field(TWST, "Bandgap ref [mV]")
-    field(TWVL, "2")
-    field(THST, "Temperature [degC]")
-    field(THVL, "3")
-    field(DTYP, "asynInt32")
-    field(OUT,  "@asyn($(PORT),$(ADDR),$(TIMEOUT))SEL_ANALOG_MONITOR_VMM1")
-}
-
 record(bo, "$(P)$(R)ReadADC-VMMs-S") {
     field(DESC, "Input signal")
     field(VAL,  "0")
diff --git a/vmmTblApp/src/vmm_tbl.cpp b/vmmTblApp/src/vmm_tbl.cpp
index 6d8ae89..4b43239 100644
--- a/vmmTblApp/src/vmm_tbl.cpp
+++ b/vmmTblApp/src/vmm_tbl.cpp
@@ -29,7 +29,7 @@ asynStatus VMMTbl::readInt32(asynUser *pasynUser, epicsInt32 *value) {
   int tmp_val;
   bool tmp_val_bool;
   uint8_t tmp_val_uint8;
-  int param_index;
+  int param_index, hyb_index, vmm_index;
 
   if (function < FIRST_VMM_PARAM) {
     return asynPortDriver::readInt32(pasynUser, value);
@@ -72,8 +72,18 @@ asynStatus VMMTbl::readInt32(asynUser *pasynUser, epicsInt32 *value) {
     goto endOfReadInt32;
   }
 
+  param_index = VecUtils::getIndex(vmmDACIDX_, function, hyb_index, vmm_index);
+  if (param_index == 0) {
+    this->pVmmAPI->getDACIdx(hyb_index, vmm_index, tmp_val);
+    *value = tmp_val;
+    goto endOfReadInt32;
+  }
+
   endOfReadInt32:
   if (param_index < 0) {
+    asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
+      "%s:%s: Failed to find parameter %d.\n",
+      driverName, __FUNCTION__, function);
     *value = 0;
     return asynError;
   }
@@ -178,7 +188,7 @@ asynStatus VMMTbl::createEpicsParams() {
       createParamAndStoreInVector("HYB_" + std::to_string(hyb) + key, typ, vec);
     }
 
-    std::vector<int> SC, SL, ST, STH, SM, SD, SMX;
+    std::vector<int> SC, SL, ST, STH, SM, SD, SMX, DACIDX;
     
     for (int vmm=0; vmm < VMMS_PER_HYBRID; vmm++) {
 
@@ -211,6 +221,10 @@ asynStatus VMMTbl::createEpicsParams() {
       param_name << "HYB_" << hyb <<"_" << vmm << "_SMX";
       createParamAndStoreInVector(param_name.str(), asynParamInt8Array, &SMX);
 
+      param_name.str("");
+      param_name << "HYB_" << hyb <<"_" << vmm << "_ANALOGMON";
+      createParamAndStoreInVector(param_name.str(), asynParamInt32, &DACIDX);
+
     }
 
     this->vmmSC_.push_back(SC);
@@ -220,6 +234,7 @@ asynStatus VMMTbl::createEpicsParams() {
     this->vmmSM_.push_back(SM);
     this->vmmSD_.push_back(SD);
     this->vmmSMX_.push_back(SMX);
+    this->vmmDACIDX_.push_back(DACIDX);
 
   }
 
@@ -231,7 +246,7 @@ asynStatus VMMTbl::writeInt32(asynUser *pasynUser, epicsInt32 value) {
   asynStatus status = asynSuccess;
   vmmStatus vmm_stat;
   int function = pasynUser->reason;
-  int param_index = -1;
+  int param_index = -1, hyb_index, vmm_index;
 
   // if (function == vmmAcquire_) {
   
@@ -268,6 +283,13 @@ asynStatus VMMTbl::writeInt32(asynUser *pasynUser, epicsInt32 value) {
     goto endOfWriteInt32;
   }
 
+  param_index = VecUtils::getIndex(vmmDACIDX_, function, hyb_index, vmm_index);
+  if (param_index == 0) {
+    vmm_stat = this->pVmmAPI->setDACIdx(hyb_index, vmm_index, value);
+    if (vmm_stat != vmmSuccess) status = asynError;
+    goto endOfWriteInt32;
+  }
+
   endOfWriteInt32:
   if (param_index < 0) {
     status = asynError;
diff --git a/vmmTblApp/src/vmm_tbl.h b/vmmTblApp/src/vmm_tbl.h
index 585109e..dbefed4 100644
--- a/vmmTblApp/src/vmm_tbl.h
+++ b/vmmTblApp/src/vmm_tbl.h
@@ -49,6 +49,7 @@ protected:
   std::vector<std::vector<int>> vmmSM_;
   std::vector<std::vector<int>> vmmSD_;
   std::vector<std::vector<int>> vmmSMX_;
+  std::vector<std::vector<int>> vmmDACIDX_;
 
 private:
   static constexpr const char *driverName = "VMMTbl";
-- 
GitLab