From 167c2ef9c3bbd6d57b24c5e21b659d9027fcdb16 Mon Sep 17 00:00:00 2001
From: Rolf Bork <rolf@ligo.caltech.edu>
Date: Thu, 30 May 2019 15:54:43 -0500
Subject: [PATCH] Some timing related cleanup in controller IOP and TS.

---
 src/fe/controllerIop.c | 20 +-------------------
 src/fe/controllerTS.c  | 38 +++++++++++---------------------------
 2 files changed, 12 insertions(+), 46 deletions(-)

diff --git a/src/fe/controllerIop.c b/src/fe/controllerIop.c
index e4554ac35..f084b215b 100644
--- a/src/fe/controllerIop.c
+++ b/src/fe/controllerIop.c
@@ -136,7 +136,6 @@ unsigned int   gps_receiver_locked = 0; // Lock/unlock flag for GPS time card
 /// GPS time in GPS seconds
 unsigned int timeSec = 0;
 unsigned int timeSecDiag = 0;
-/* 1 - error occured on shmem; 2 - RFM; 3 - Dolphin */
 unsigned int ipcErrBits = 0;
 int cardCountErr = 0;
 
@@ -202,23 +201,12 @@ void *fe_start(void *arg)
 {
   int tempClock[4];
   int ii,jj,kk,ll;			// Dummy loop counter variables
-  // int mm;
   static int clock1Min = 0;		///  @param clockMin Minute counter (Not Used??)
   static int cpuClock[CPU_TIMER_CNT];	///  @param cpuClock[] Code timing diag variables
 
-
-  					///< Code runs longer for first few cycles on startup as it settles in,
-					///< so this helps prevent long cycles during that time.
-  // int limit = OVERFLOW_LIMIT_16BIT;      /// @param limit ADC/DAC overflow test value
-  // int mask = GSAI_DATA_MASK;            /// @param mask Bit mask for ADC/DAC read/writes
-  // int num_outs = MAX_DAC_CHN_PER_MOD;   /// @param num_outs Number of DAC channels variable
-  // volatile unsigned int *pDacData;	/// @param *pDacData Pointer to DAC PCI data space
-  // int dacEnable = 0;
-  // int pBits[9] = {1,2,4,8,16,32,64,128,256};	/// @param pBits[] Lookup table for quick power of 2 calcs
   int sync21ppsCycles = 0;		/// @param sync32ppsCycles Number of attempts to sync to 1PPS
   // int dkiTrip = 0;
   RFM_FE_COMMS *pEpicsComms;		/// @param *pEpicsComms Pointer to EPICS shared memory space
-  int myGmError2 = 0;			/// @param myGmError2 Myrinet error variable
   int status;				/// @param status Typical function return value
   float onePps;				/// @param onePps Value of 1PPS signal, if used, for diagnostics
   int onePpsHi = 0;			/// @param onePpsHi One PPS diagnostic check
@@ -244,12 +232,7 @@ void *fe_start(void *arg)
   int adcMissedCycle = 0;
   int dac_restore = 0;
 
-  // volatile GSA_18BIT_DAC_REG *dac18bitPtr;	// Pointer to 16bit DAC memory area
-  // volatile GSA_20BIT_DAC_REG *dac20bitPtr;  // Pointer to 20bit DAC memory area
-  // volatile GSC_DAC_REG *dac16bitPtr;		// Pointer to 18bit DAC memory area
   unsigned int usec = 0;
-
-
   unsigned long cpc;
   float duotoneTimeDac;
   float duotoneTime;
@@ -391,7 +374,6 @@ adcInfo_t *padcinfo = (adcInfo_t *)&adcinfo;
   vmeDone = 0;
 
   /// \> Call user application software initialization routine.
-  // printf("Calling feCode() to initialize\n");
   iopDacEnable = feCode(cycleNum,dWord,dacOut,dspPtr[0],&dspCoeff[0], (struct CDS_EPICS *)pLocalEpics,1);
 
   // Initialize timing info variables
@@ -804,7 +786,7 @@ adcInfo_t *padcinfo = (adcInfo_t *)&adcinfo;
 		
     // Call daqLib
     pLocalEpics->epicsOutput.daqByteCnt = 
-    daqWrite(1,dcuId,daq,DAQ_RATE,testpoint,dspPtr[0],myGmError2,(int *)(pLocalEpics->epicsOutput.gdsMon),xExc,pEpicsDaq);
+    daqWrite(1,dcuId,daq,DAQ_RATE,testpoint,dspPtr[0],0,(int *)(pLocalEpics->epicsOutput.gdsMon),xExc,pEpicsDaq);
     // Send the current DAQ block size to the awgtpman for TP number checking
     pEpicsComms->padSpace.feDaqBlockSize = curDaqBlockSize;
     pLocalEpics->epicsOutput.tpCnt = tpPtr->count & 0xff;
diff --git a/src/fe/controllerTS.c b/src/fe/controllerTS.c
index 26295400d..84c0088a7 100644
--- a/src/fe/controllerTS.c
+++ b/src/fe/controllerTS.c
@@ -136,25 +136,6 @@ unsigned int timeSecDiag = 0;
 /* 1 - error occured on shmem; 2 - RFM; 3 - Dolphin */
 unsigned int ipcErrBits = 0;
 int cardCountErr = 0;
-#if 0
-int adcTime;			///< Used in code cycle timing
-int adcHoldTime;		///< Stores time between code cycles
-int adcHoldTimeMax;		///< Stores time between code cycles
-int adcHoldTimeEverMax;		///< Maximum cycle time recorded
-int adcHoldTimeEverMaxWhen;
-int cpuTimeEverMax;		///< Maximum code cycle time recorded
-int cpuTimeEverMaxWhen;
-int startGpsTime;
-int adcHoldTimeMin;
-int adcHoldTimeAvg;
-int adcHoldTimeAvgPerSec;
-int usrHoldTime;		///< Max time spent in user app code
-int cycleTime;			///< Current cycle time
-int timeHold = 0;			///< Max code cycle time within 1 sec period
-int timeHoldHold = 0;			///< Max code cycle time within 1 sec period; hold for another sec
-int timeHoldWhen= 0;			///< Cycle number within last second when maximum reached; running
-int timeHoldWhenHold = 0;		///< Cycle number within last second when maximum reached
-#endif
 
 // The following are for timing histograms written to /proc files
 #if defined(SERVO64K) || defined(SERVO32K)
@@ -199,17 +180,19 @@ int initialDiagReset = 1;
 // Did not see any effect
   char fp [64*1024];
 
-inline waitPcieTimingSignal(TIMING_SIGNAL *timePtr,int cycle) {
+inline int waitPcieTimingSignal(TIMING_SIGNAL *timePtr,int cycle) {
 int loop = 0;
 
     do{
 	udelay(1);
 	loop ++;
-    }while(timePtr->cycle != cycle && loop < 16);
+    }while(timePtr->cycle != cycle && loop < 18);
+    if(loop >= 18) return(1);
+    else return(0);
 }
 inline sync2master(TIMING_SIGNAL *timePtr) {
   int loop = 0;
-  int cycle = 65535;
+  int cycle = 0;
 
     do{
 	udelay(5);
@@ -542,14 +525,13 @@ udelay(1000);
 	pLocalEpics->epicsOutput.timeErr = syncSource;
 
 	pcieTimer = (TIMING_SIGNAL *) ((volatile char *)(cdsPciModules.dolphinRead[0]) + IPC_PCIE_TIME_OFFSET);
-	printf("I am a PCIe Network TIMING SLAVE **************\n");
-	printf("Address is 0x%lx \n",(long)pcieTimer);
+    pLocalEpics->epicsOutput.fe_status = INIT_SYNC;
   	sync2master(pcieTimer);
 	timeSec = pcieTimer->gps_time;
 
 
 
-    printf("Triggered the ADC\n");
+    pLocalEpics->epicsOutput.fe_status = NORMAL_RUN;
 
   onePpsTime = cycleNum;
 #ifdef REMOTE_GPS
@@ -593,6 +575,7 @@ udelay(1000);
           	pLocalEpics->epicsOutput.timeDiag = timeSec;
 	  		if (cycle_gps_time == 0) {
 				timeinfo.startGpsTime = timeSec;
+                pLocalEpics->epicsOutput.startgpstime = timeinfo.startGpsTime;
 	  		}	
 	  		cycle_gps_time = timeSec;
 		}
@@ -606,7 +589,7 @@ udelay(1000);
 	/// code rate eg 32 samples each time thru before proceeding to match 2048 system.
 		// Read ADC data
     /// - ---- Wait new cycle count from PCIe network before proceeding
-	waitPcieTimingSignal(pcieTimer,cycleNum);
+	missedCycle += waitPcieTimingSignal(pcieTimer,cycleNum);
 	timeSec = pcieTimer->gps_time;
         for(jj=0;jj<cdsPciModules.adcCount;jj++)
 		{
@@ -639,7 +622,6 @@ udelay(1000);
 	  		pLocalEpics->epicsOutput.diagWord |= ADC_TIMEOUT_ERR;
                         stop_working_threads = 1;
 			vmeDone = 1;
-                        printf("timeout %d %d \n",jj,adcWait);
 			continue;
 		    }
 		    if(jj == 0) 
@@ -816,6 +798,7 @@ udelay(1000);
 	if(cycleNum ==HKP_TIMING_UPDATES)	
     {
       sendTimingDiags2Epics(pLocalEpics, &timeinfo, &adcinfo);
+      pLocalEpics->epicsOutput.irigbTime = missedCycle;
 	  if((adcinfo.adcHoldTime > CYCLE_TIME_ALRM_HI) || (adcinfo.adcHoldTime < CYCLE_TIME_ALRM_LO)) 
 	  {
 	  	diagWord |= FE_ADC_HOLD_ERR;
@@ -838,6 +821,7 @@ udelay(1000);
 		timeinfo.timeHoldMax = 0;
 	  	diagWord = 0;
 		ipcErrBits = 0;
+        missedCycle = 0;
 		
 		// feStatus = 0;
         for(jj=0;jj<cdsPciModules.adcCount;jj++) adcinfo.adcRdTimeMax[jj] = 0;
-- 
GitLab