Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
13:7a1fb885a8e4
Parent:
12:d472f9811262
Child:
14:2736b57bbbed
--- a/main.cpp	Sat Aug 18 05:00:32 2012 +0000
+++ b/main.cpp	Tue Aug 21 02:18:27 2012 +0000
@@ -3,7 +3,7 @@
 #define USE_RTOS_TIMER
 
 //#define EVT_TIME_PROFILE
-//#define DEBUG
+#define DEBUG
 
 #include <stdint.h>
 #include "SDFileSystem.h"
@@ -73,10 +73,10 @@
 // The SD card
 
 // this needs to be first in case some other global uses a print statement
-static MODSERIAL      gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
-
-SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1);
-LocalFileSystem local("local");
+static MODSERIAL        gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
+static Serial           gSBDport(p28, p27, "sbd");
+static SDFileSystem     sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1);
+static LocalFileSystem  local("local");
 
 //
 // fwd declare fcns
@@ -86,7 +86,7 @@
 void                      SaveEvent(const int32_t etms);
 void                      WaitTrigAndSendClock();
 void                      SetConfigAndMakeOutputFile();
-SnCommWin::ECommWinResult OpenCommWin();
+SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig=false);
 void                      MakeOutputFile(const bool stopRunning=false);
 void                      SetPower(const bool isCommWin);
 void                      procForceTrigger();
@@ -280,7 +280,11 @@
     gEvtNum = gConf.GetFirstEvt() + evtStartCurSeq;
     gPowNum = evtStartCurSeq;
     memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
-    gLastCountReset = static_cast<uint32_t>(time(0));
+    gLastCountReset = static_cast<uint32_t>(time(0)); // to calc rates
+#ifdef DEBUG
+    printf("Reset: gEvtNum=%u, gPowNum=%u, evtStartCS=%u\r\n",
+        gEvtNum, gPowNum, evtStartCurSeq);
+#endif
 }
 
 void GetRates(float& thmrate, float& evtrate) {
@@ -382,6 +386,8 @@
     
     // RTOS stuff must be made inside main for some reason
     gComms[0] = new SnCommAfarTCP(gConf);
+    //gComms[0] = new SnCommSBD(gSBDport);
+    
 #ifdef USE_RTOS_TIMER
     gForceTicker        = new rtos::RtosTimer(&procForceTrigger);
     gHeartbeatTicker    = new rtos::RtosTimer(&procHeartbeat);
@@ -418,18 +424,13 @@
     // (probably) power down comms and power up cards,amps
     SetPower(false);
 
-#ifdef DEBUG
-    printf("Using config %s\r\n",gConf.GetLabel());
-#endif
-    SetConfigAndMakeOutputFile(); // setup defaults in case no communication
-
     //
     // get config
     //
 #ifdef DEBUG
     printf("call OpenCommWin\r\n");
 #endif
-    OpenCommWin();
+    OpenCommWin(true); // alwasy configure, even if no new config
     
     // get ready to trigger
     PIN_spi.format( 16, 1 ); // change to data readout format
@@ -618,7 +619,9 @@
     printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
         gEvtNum,gPowNum,(int)stopRunning);
 #endif
+    
     SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
+    
 #ifdef DEBUG
     printf("file closed\r\n");
 #endif
@@ -627,6 +630,8 @@
     }
     FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(),
                                             gConf.GetRun());
+    // reset event, timers, trigger counters
+    ResetCountersClearEvt();
     if (cf!=0) {
         wait_ms(200);
         GetAvePowerReading();
@@ -696,9 +701,6 @@
     PIN_heartbeat             = 0;
     wait_ms(20);
     
-    // reset event, timers, trigger counters
-    ResetCountersClearEvt();
-    
     if (AreCardsPowered()) {
         // Set PLA value(s)
         PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting
@@ -905,120 +907,122 @@
     }
 }
 
-SnCommWin::ECommWinResult OpenCommWin() {
+SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig) {
     // loop through each comm mode:
     //  a) try to connect
     //  b) if connected, listen for config
     //  c) if config requests data, send it
 
     gLastCommWin = time(0);
-    if (gConf.GetCommWinDuration()==0) {
-        // TODO: set min so this is not possible
-        return SnCommWin::kOkNoMsg;
-    }
-    
-    gCommWinOpen = true;
-    Watchdog::kick(); // don't reset!
-    
-#ifdef DEBUG
-    printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
-#endif
-    
-    // close the file so that the data is all written out.
-    // and open it back up at the beginning (for reading)
-#ifdef DEBUG
-    printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
-#endif
-    PIN_lockRegisters = 0; // unlock so we can talk to SD card.
-    SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
-    SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
-    
-    // (probably) power down cards,amps and power up comms
-    SetPower(true);
-    
-    const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ?
-        gConf.GetCommWinDuration() : kConnectTimeout;
-    const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ?
-        gConf.GetCommWinDuration() : kListenTimeout;
-    
+
     SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
     
     bool gotNewConfig=false;
-    bool sendStat[kNcomms];
-    for (uint8_t i=0; i<kNcomms; i++) {
-        sendStat[i]=true;
-    }
-    bool* ss = sendStat;
-    SnCommWin** cw = gComms;
-    for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
+
+    if (gConf.GetCommWinDuration()==0) {
+        // TODO: set min so this is not possible
+        res = SnCommWin::kOkNoMsg;
+    } else {
+    
+        gCommWinOpen = true;
         Watchdog::kick(); // don't reset!
-        if (i==kNcomms) {
-            i=0;
-            cw = gComms;
-            ss = sendStat;
-        }
-        if ((*cw)==0) {
-            continue;
-        }
-        // open window and (mabye) send status update
+    
+#ifdef DEBUG
+        printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
+#endif
+    
+        // close the file so that the data is all written out.
+        // and open it back up at the beginning (for reading)
 #ifdef DEBUG
-        printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
-        printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",GetTimeoutTime(gLastCommWin,conto),
-            time(0), gLastCommWin, gConf.GetCommWinDuration());
+        printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
 #endif
-        // 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,
-            SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
-            gGenBuf);
-        if (conres>=SnCommWin::kConnected) {
+        PIN_lockRegisters = 0; // unlock so we can talk to SD card.
+        SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
+        SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
+        
+        // (probably) power down cards,amps and power up comms
+        SetPower(true);
+        
+        const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ?
+            gConf.GetCommWinDuration() : kConnectTimeout;
+        const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ?
+            gConf.GetCommWinDuration() : kListenTimeout;
+        
+        bool sendStat[kNcomms];
+        for (uint8_t i=0; i<kNcomms; i++) {
+            sendStat[i]=true;
+        }
+        bool* ss = sendStat;
+        SnCommWin** cw = gComms;
+        for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
             Watchdog::kick(); // don't reset!
-            // connected. listen for config
-            *ss = false; // don't send status next time
+            if (i==kNcomms) {
+                i=0;
+                cw = gComms;
+                ss = sendStat;
+            }
+            if ((*cw)==0) {
+                continue;
+            }
+            // open window and (mabye) send status update
 #ifdef DEBUG
-            printf("get conf gtt=%u\r\n",GetTimeoutTime(gLastCommWin, listo));
+            printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
+            printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",GetTimeoutTime(gLastCommWin,conto),
+                time(0), gLastCommWin, gConf.GetCommWinDuration());
 #endif
-            const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
-                gConf, GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);
-            if (cfgres>=SnCommWin::kOkWithMsg) {
+            // 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,
+                SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
+                gGenBuf);
+            if (conres>=SnCommWin::kConnected) {
                 Watchdog::kick(); // don't reset!
+                // connected. listen for config
+                *ss = false; // don't send status next time
 #ifdef DEBUG
-                printf("received config!\r\n");
-                printf("send data = %d\r\n", gConf.GetCommSendData());
+                printf("get conf gtt=%u\r\n",GetTimeoutTime(gLastCommWin, listo));
 #endif
-                // send data if need be (files, some events, etc)
-                const uint32_t winto = GetTimeoutTime(gLastCommWin, 
-                                                      gConf.GetCommWinDuration());
-                const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
-                if (gConf.GetCommSendData()!=0) {
+                const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
+                    gConf, GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);
+                if (cfgres>=SnCommWin::kOkWithMsg) {
+                    Watchdog::kick(); // don't reset!
 #ifdef DEBUG
-                    printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n",
-                        GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()),
-                        gLastCommWin, gConf.GetCommWinDuration(),
-                        gConf.IsObeyingTimeout() ? "true" : "false");
+                    printf("received config!\r\n");
+                    printf("send data = %d\r\n", gConf.GetCommSendData());
 #endif
-                    res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize,
-                                          gtt, winto);
-                } else {
-                    // don't send anything
-                    res = cfgres;
-                }
+                    // send data if need be (files, some events, etc)
+                    const uint32_t winto = GetTimeoutTime(gLastCommWin, 
+                                                          gConf.GetCommWinDuration());
+                    const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
+                    if (gConf.GetCommSendData()!=0) {
 #ifdef DEBUG
-                printf("Got config!\r\n");
+                        printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n",
+                            GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()),
+                            gLastCommWin, gConf.GetCommWinDuration(),
+                            gConf.IsObeyingTimeout() ? "true" : "false");
 #endif
-                gotNewConfig = (cfgres!=SnCommWin::kOkWthMsgNoConf);
-                Watchdog::kick(); // don't reset!
-                (*cw)->CloseConn();
-                break;
+                        res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize,
+                                              gtt, winto);
+                    } else {
+                        // don't send anything
+                        res = cfgres;
+                    }
+#ifdef DEBUG
+                    printf("Got config!\r\n");
+#endif
+                    gotNewConfig = (cfgres!=SnCommWin::kOkWthMsgNoConf);
+                    Watchdog::kick(); // don't reset!
+                    (*cw)->CloseConn();
+                    break;
+                }
             }
+            
+            Watchdog::kick(); // don't reset!
         }
-        
-        Watchdog::kick(); // don't reset!
-
     }
         
     // (probably) power down comms and power up cards,amps
@@ -1027,7 +1031,7 @@
     gFirstEvt = true;
 
     // reset config with system powered (for DAC/PLA setting)
-    if (gotNewConfig) {
+    if (gotNewConfig || forceReconfig) {
 #ifdef DEBUG
         printf("calling SetConfigAndMakeOutputFile\r\n");
 #endif