From b06497e890c4cfaa396bed69bd7235357728bc38 Mon Sep 17 00:00:00 2001
From: Rolf Bork <rbork@caltech.edu>
Date: Fri, 2 Oct 2020 17:21:49 -0500
Subject: [PATCH] - Moved DAC overflow indicator (dacOF) into the dacInfo type.
 - Updated all code that uses the above structure. - Fixed user space code to
 use dac_info routine instead of their own.

---
 src/fe/controllerAppUser.c          |  1 -
 src/fe/controllerIopUser.c          | 57 ++++-------------------------
 src/include/cds_types.h             |  1 +
 src/include/drv/app_dac_functions.c |  2 +-
 src/include/drv/dac_info.c          |  4 +-
 src/include/drv/iop_dac_functions.c |  2 +-
 6 files changed, 13 insertions(+), 54 deletions(-)

diff --git a/src/fe/controllerAppUser.c b/src/fe/controllerAppUser.c
index 07b1f7e06..1d59b6283 100644
--- a/src/fe/controllerAppUser.c
+++ b/src/fe/controllerAppUser.c
@@ -54,7 +54,6 @@
 #include "drv/mapuser.h"
 #include "timing.c" // timing module / IRIG-B  functions
 
-// int dacOF[ MAX_DAC_MODULES ];
 #include "drv/inputFilterModule.h"
 #include "drv/inputFilterModule1.h"
 #include <drv/app_dac_functions.c>
diff --git a/src/fe/controllerIopUser.c b/src/fe/controllerIopUser.c
index 48f5fe883..fe5c6c18d 100644
--- a/src/fe/controllerIopUser.c
+++ b/src/fe/controllerIopUser.c
@@ -55,6 +55,7 @@
 
 #include "drv/inputFilterModule.h"
 #include "drv/inputFilterModule1.h"
+#include <drv/dac_info.c>
 
 #ifdef DOLPHIN_TEST
 #include "dolphin.c"
@@ -168,14 +169,13 @@ fe_start_iop_user( )
         cpuClock[ CPU_TIMER_CNT ]; ///  @param cpuClock[] Code timing diag
                                    ///  variables
     static int chanHop = 0; /// @param chanHop Adc channel hopping status
-    int        dacOF[ MAX_DAC_MODULES ];
 
     int adcData[ MAX_ADC_MODULES ]
                [ MAX_ADC_CHN_PER_MOD ]; /// @param adcData[][]  ADC raw data
     int adcChanErr[ MAX_ADC_MODULES ];
     // int adcWait = 0;
     adcInfo_t adcInfo;
-    dacInfo_t dacInfo;
+    dacInfo_t dacinfo;
     int adcOF[ MAX_ADC_MODULES ]; /// @param adcOF[]  ADC overrange counters
 
     static int dacWriteEnable =
@@ -291,7 +291,7 @@ fe_start_iop_user( )
         {
             dacOut[ ii ][ jj ] = 0.0;
             dacOutUsed[ ii ][ jj ] = 0;
-            dacInfo.dacOutBufSize[ ii ] = 0;
+            dacinfo.dacOutBufSize[ ii ] = 0;
             // Zero out DAC channel map in the shared memory
             // to be used to check on control models' channel allocation
             ioMemData->dacOutUsed[ ii ][ jj ] = 0;
@@ -665,10 +665,10 @@ fe_start_iop_user( )
                 /// errors
                 if ( dac_out > limit || dac_out < -limit )
                 {
-                    dacInfo.overflowDac[ jj ][ ii ]++;
+                    dacinfo.overflowDac[ jj ][ ii ]++;
                     pLocalEpics->epicsOutput.overflowDacAcc[ jj ][ ii ]++;
                     overflowAcc++;
-                    dacOF[ jj ] = 1;
+                    dacinfo.dacOF[ jj ] = 1;
                     odcStateWord |= ODC_DAC_OVF;
                     ;
                     if ( dac_out > limit )
@@ -681,7 +681,7 @@ fe_start_iop_user( )
                     dac_out = 0;
                 /// - ---- Load last values to EPICS channels for monitoring on
                 /// GDS_TP screen.
-                dacInfo.dacOutEpics[ jj ][ ii ] = dac_out;
+                dacinfo.dacOutEpics[ jj ][ ii ] = dac_out;
 
                 /// - ---- Load DAC testpoints
                 floatDacOut[ 16 * jj + ii ] = dac_out;
@@ -696,15 +696,6 @@ fe_start_iop_user( )
             /// again by Master
             ioMemData->iodata[ mm ][ ioMemCntrDac ].cycle = -1;
             /// - -- DMA Write data to DAC module
-#if 0
-        if(dacWriteEnable > 4) {
-            if(cdsPciModules.dacType[jj] == GSC_16AO16) {
-                gsc16ao16DmaStart(jj);
-            } else {
-                gsc18ao8DmaStart(jj);
-            }
-        }
-#endif
         }
         /// \> Increment DAC memory block pointers for next cycle
         ioClockDac = ( ioClockDac + 1 ) % IOP_IO_RATE;
@@ -883,7 +874,7 @@ fe_start_iop_user( )
                 for ( ii = 0; ii < MAX_DAC_CHN_PER_MOD; ii++ )
                 {
                     pLocalEpics->epicsOutput.dacValue[ jj ][ ii ] =
-                        dacInfo.dacOutEpics[ jj ][ ii ];
+                        dacinfo.dacOutEpics[ jj ][ ii ];
                 }
             }
         }
@@ -944,39 +935,7 @@ fe_start_iop_user( )
                 printf( "Code is exiting ..............\n" );
                 continue;
             }
-            for ( jj = 0; jj < cdsPciModules.dacCount; jj++ )
-            {
-                if ( dacOF[ jj ] )
-                {
-                    pLocalEpics->epicsOutput.statDac[ jj ] &=
-                        ~( DAC_OVERFLOW_BIT );
-                    feStatus |= FE_ERROR_OVERFLOW;
-                    ;
-                }
-                else
-                    pLocalEpics->epicsOutput.statDac[ jj ] |= DAC_OVERFLOW_BIT;
-                dacOF[ jj ] = 0;
-                if ( dacChanErr[ jj ] )
-                {
-                    pLocalEpics->epicsOutput.statDac[ jj ] &=
-                        ~( DAC_TIMING_BIT );
-                }
-                else
-                    pLocalEpics->epicsOutput.statDac[ jj ] |= DAC_TIMING_BIT;
-                dacChanErr[ jj ] = 0;
-                for ( ii = 0; ii < MAX_DAC_CHN_PER_MOD; ii++ )
-                {
-
-                    if ( pLocalEpics->epicsOutput.overflowDacAcc[ jj ][ ii ] >
-                         OVERFLOW_CNTR_LIMIT )
-                    {
-                        pLocalEpics->epicsOutput.overflowDacAcc[ jj ][ ii ] = 0;
-                    }
-                    pLocalEpics->epicsOutput.overflowDac[ jj ][ ii ] =
-                        dacInfo.overflowDac[ jj ][ ii ];
-                    dacInfo.overflowDac[ jj ][ ii ] = 0;
-                }
-            }
+            feStatus |= dac_status_update( &dacinfo );
         }
         // *********************************************************************
         // Capture end of cycle time.
diff --git a/src/include/cds_types.h b/src/include/cds_types.h
index ccc117851..5286c6081 100644
--- a/src/include/cds_types.h
+++ b/src/include/cds_types.h
@@ -25,6 +25,7 @@ typedef struct dacInfo_t
                    [ MAX_DAC_CHN_PER_MOD ]; // DAC overflow diagnostics
     int dacOutBufSize[ MAX_DAC_MODULES ];
     int dacOutEpics[ MAX_DAC_MODULES ][ MAX_DAC_CHN_PER_MOD ];
+    int dacOF[ MAX_DAC_MODULES ];
 } dacInfo_t;
 
 typedef struct duotone_diag_t
diff --git a/src/include/drv/app_dac_functions.c b/src/include/drv/app_dac_functions.c
index 11a7dbd83..4dc415074 100644
--- a/src/include/drv/app_dac_functions.c
+++ b/src/include/drv/app_dac_functions.c
@@ -163,7 +163,7 @@ app_dac_write( int ioMemCtrDac, int ioClkDac, dacInfo_t* dacinfo )
                         dacinfo->overflowDac[ jj ][ ii ]++;
                         pLocalEpics->epicsOutput.overflowDacAcc[ jj ][ ii ]++;
                         overflowAcc++;
-                        dacOF[ jj ] = 1;
+                        dacinfo->dacOF[ jj ] = 1;
                         odcStateWord |= ODC_DAC_OVF;
                         ;
                         if ( dac_out > limit )
diff --git a/src/include/drv/dac_info.c b/src/include/drv/dac_info.c
index 5a8c5ca26..d8ab39aea 100644
--- a/src/include/drv/dac_info.c
+++ b/src/include/drv/dac_info.c
@@ -8,7 +8,7 @@ dac_status_update( dacInfo_t* dacinfo )
 
     for ( jj = 0; jj < cdsPciModules.dacCount; jj++ )
     {
-        if ( dacOF[ jj ] )
+        if ( dacinfo->dacOF[ jj ] )
         {
             pLocalEpics->epicsOutput.statDac[ jj ] &= ~( DAC_OVERFLOW_BIT );
             status |= FE_ERROR_OVERFLOW;
@@ -16,7 +16,7 @@ dac_status_update( dacInfo_t* dacinfo )
         }
         else
             pLocalEpics->epicsOutput.statDac[ jj ] |= DAC_OVERFLOW_BIT;
-        dacOF[ jj ] = 0;
+        dacinfo->dacOF[ jj ] = 0;
         if ( dacChanErr[ jj ] )
         {
             pLocalEpics->epicsOutput.statDac[ jj ] &= ~( DAC_TIMING_BIT );
diff --git a/src/include/drv/iop_dac_functions.c b/src/include/drv/iop_dac_functions.c
index 399118fe1..2b046675c 100644
--- a/src/include/drv/iop_dac_functions.c
+++ b/src/include/drv/iop_dac_functions.c
@@ -217,7 +217,7 @@ iop_dac_write( int in_delay )
                     dacinfo.overflowDac[ card ][ chan ]++;
                     pLocalEpics->epicsOutput.overflowDacAcc[ card ][ chan ]++;
                     overflowAcc++;
-                    dacOF[ card ] = 1;
+                    dacinfo.dacOF[ card ] = 1;
                     odcStateWord |= ODC_DAC_OVF;
                     ;
                     if ( dac_out > limit )
-- 
GitLab