Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
10:3c93db1cfb12
Parent:
9:a1a39573dd43
Child:
11:de443350ec4a
--- a/main.cpp	Thu Aug 09 02:16:08 2012 +0000
+++ b/main.cpp	Fri Aug 10 18:35:43 2012 +0000
@@ -128,7 +128,9 @@
 static uint32_t       gTrgNum[kNumTrgs] = {0}; // num of this type of trg received
 // i/o
 static time_t         gLastCommWin      = 0;
-static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + SnHeaderFrame::kMaxSizeOf + SnPowerFrame::kMaxSizeOf;
+static uint32_t       gLastCountReset   = 0;
+// this should be bigger than anything that will actually be used
+static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + (2u*SnHeaderFrame::kMaxSizeOf) + SnPowerFrame::kMaxSizeOf;
 //static const uint32_t gB64Bsize=BASE64ENC_LEN(gBufSize)+1;
 //static char           gB64Buf[gB64Bsize];
 static char           gGenBuf[gBufSize]; // must be big enough for event or status or config!
@@ -251,7 +253,27 @@
     gCheckPower = false;
 }
 
+void ResetCountersClearEvt() {
+    gEvent.ClearEvent();
+    gEvtNum = gConf.GetFirstEvt();
+    gPowNum = 0;
+    memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
+    gLastCountReset = static_cast<uint32_t>(time(0));
+}
+
+void GetRates(float& thmrate, float& evtrate) {
+    thmrate = evtrate = 0;
+    const uint32_t dt = static_cast<uint32_t>(time(0)) - gLastCountReset;
+    if (dt>0) {
+        thmrate = gTrgNum[kThmTrg] / dt;
+        evtrate = (gEvtNum + gConf.GetFirstEvt()) / dt;
+    }
+}
+
 bool IsSeqComplete() {
+    printf("IsSeqComplete: eps=%u, cntpow=%d, fe=%u, pow=%u, evt=%u\r\n",
+        gConf.GetEvtsPerFile(), (int)gConf.IsCountingPowerReadings(),
+        gConf.GetFirstEvt(), gPowNum, gEvtNum);
     if (gConf.GetEvtsPerFile()>0) {
         if (gConf.IsCountingPowerReadings()) {
             return (gPowNum>=gConf.GetEvtsPerFile());
@@ -302,9 +324,14 @@
 #endif
 
 void StopRunning() {
+    printf("stop running\r\n");
+    printf("stopping force\r\n");
     stopTicker(gForceTicker);
+    printf("stop heart\r\n");
     stopTicker(gHeartbeatTicker);
+    printf("stop comm win\r\n");
     stopTicker(gCommWinTicker);
+    printf("stop power check\r\n");
     stopTicker(gPowerCheckTicker);
     while (true) {
         led3 = 1; led4=1;
@@ -446,6 +473,7 @@
         
         // make new seq?
         if (IsSeqComplete()) {
+            printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
             MakeOutputFile(gConf.IsSingleSeqRunMode());
         }
         
@@ -486,8 +514,11 @@
 }
 
 void MakeOutputFile(const bool stopRunning) {
-    printf("closing output file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
+    PIN_lockRegisters = 0; // unlock so we can talk to SD card.
+    printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
+        gEvtNum,gPowNum,(int)stopRunning);
     SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
+    printf("file closed\r\n");
     if (stopRunning) {
         StopRunning();
     }
@@ -555,10 +586,7 @@
     wait_ms(20);
     
     // reset event, timers, trigger counters
-    gEvent.ClearEvent();
-    gEvtNum = gConf.GetFirstEvt();
-    gPowNum = 0;
-    memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
+    ResetCountersClearEvt();
     
     if (AreCardsPowered()) {
         // Set PLA value(s)
@@ -762,6 +790,7 @@
     // close the file so that the data is all written out.
     // and open it back up at the beginning (for reading)
     printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
+    PIN_lockRegisters = 0; // unlock so we can talk to SD card.
     SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
     SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
     
@@ -798,8 +827,13 @@
             time(0), gLastCommWin, gConf.GetCommWinDuration());
         // update power reading in case we want to send it in status
         GetAvePowerReading();
+        // get the trigger rates
+        float thmrate=0, evtrate=0;
+        GetRates(thmrate, evtrate);
         const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow(
-            GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower, gGenBuf);
+            GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower,
+            SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
+            gGenBuf);
         if (conres>=SnCommWin::kConnected) {
             Watchdog::kick(); // don't reset!
             // connected. listen for config