Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
15:f2569d8e4176
Parent:
14:2736b57bbbed
Child:
16:744ce85aede2
--- a/main.cpp	Tue Aug 21 02:19:35 2012 +0000
+++ b/main.cpp	Fri Aug 31 02:09:09 2012 +0000
@@ -4,10 +4,14 @@
 
 //#define EVT_TIME_PROFILE
 //#define DEBUG
+#define SSNOTIFY
+#define USE_MODSERIAL
 
 #include <stdint.h>
 #include "SDFileSystem.h"
+#ifdef USE_MODSERIAL
 #include "MODSERIAL.h"
+#endif
 #include "Watchdog.h"
 #include "SnConstants.h"
 #include "SnBitUtils.h"
@@ -19,6 +23,7 @@
 #include "SnCommWin.h"
 #include "SnCommAfarTCP.h"
 #include "SnCommUsb.h"
+#include "SnCommSBD.h"
 #include "SnBase64.h"
 #ifdef USE_RTOS_TIMER
 #include "RtosTimer.h"
@@ -35,6 +40,8 @@
 // Set up power pins - Note that it's Zero for "on"
 DigitalOut PIN_turn_on_system(p17); // this turns on system
 DigitalOut PIN_turn_on_amps(p25);
+// SD card select
+DigitalOut PIN_SD_CS(p8);
 // Activate/select chip by falling edge
 DigitalOut PIN_ADC_CS( p9 );
 // clock signal to activate PLA setting
@@ -73,8 +80,13 @@
 // The SD card
 
 // this needs to be first in case some other global uses a print statement
+#ifdef USE_MODSERIAL
 static MODSERIAL        gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
+static MODSERIAL        gSBDport(p28, p27, "sbd");
+#else
+static Serial           gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
 static Serial           gSBDport(p28, p27, "sbd");
+#endif
 static SDFileSystem     sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1);
 static LocalFileSystem  local("local");
 
@@ -122,8 +134,8 @@
 static SnPowerFrame   gPower;
 // parameters
 static bool           gFirstEvt         = true;
-static bool           gReadingOut       = false;
-static bool           gCommWinOpen      = false; // if it's open
+static volatile bool  gReadingOut       = false;
+static volatile bool  gCommWinOpen      = false; // if it's open
 static volatile bool  gOpenCommWin      = false; // if it should be opened
 static volatile bool  gCheckPower       = false; // if it should be checked
 static uint32_t       gPowNum           = 0;
@@ -140,8 +152,8 @@
 static SnCommWin*     gComms[kNcomms]   = { 0 }; // order => priority. afar uses RTOS, and must be made inside main
 
 void procForceTrigger() {
-    led3=!led3;
     if (gReadingOut==false && gCommWinOpen==false) {
+        led3=!led3;
 #ifdef DEBUG
         printf("proc force\r\n");
 #endif
@@ -358,14 +370,46 @@
 }
 #endif
 
-void StopRunning() {
-#ifdef DEBUG
-    printf("stop running\r\n");
-#endif
+void StopAllTickers() {
     stopTicker(gForceTicker);
     stopTicker(gHeartbeatTicker);
     stopTicker(gCommWinTicker);
     stopTicker(gPowerCheckTicker);
+}
+
+void ResetAllTickers() {
+#ifdef USE_RTOS_TIMER
+    const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
+                                     kAbsMaxTimer);
+    const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
+                                     kAbsMaxTimer);
+    const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
+                                     kCommWinLongPrdTk);
+    const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
+                                     kAbsMaxTimer);
+#else
+    const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
+                                     kAbsMaxTimer, &procForceTrigger);
+    const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
+                                     kAbsMaxTimer, &procHeartbeat);
+    const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
+                                     kCommWinLongPrdTk, &procCommWin);
+    const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
+                                     kAbsMaxTimer, &procPowerCheck);
+#endif
+#ifdef DEBUG
+    printf("attach force trig %u\r\n",ftp);
+    printf("attach heart beat %u\r\n",hbp);
+    printf("attach comm win   %u\r\n",cwp);
+    printf("attach power chk  %u\r\n",pcp);
+#endif
+}
+
+void StopRunning() {
+#if defined(DEBUG) || defined(SSNOTIFY)
+    printf("stop running\r\n");
+#endif
+    StopAllTickers();
     while (true) {
         led3 = 1; led4=1;
         wait(0.5);
@@ -377,6 +421,9 @@
 
 int main() {
     {
+#ifdef SSNOTIFY
+        printf("main: start\r\n");
+#endif
         led1=1; wait(0.2);
         led1=0; led2=1; wait(0.2);
         led2=0; led3=1; wait(0.2);
@@ -385,8 +432,8 @@
     }
     
     // RTOS stuff must be made inside main for some reason
-    gComms[0] = new SnCommAfarTCP(gConf);
-    //gComms[0] = new SnCommSBD(gSBDport);
+    //gComms[0] = new SnCommAfarTCP(gConf);
+    gComms[0] = new SnCommSBD(&gSBDport);
     
 #ifdef USE_RTOS_TIMER
     gForceTicker        = new rtos::RtosTimer(&procForceTrigger);
@@ -434,6 +481,7 @@
     
     // get ready to trigger
     PIN_spi.format( 16, 1 ); // change to data readout format
+    PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
 
     led2=0;
     
@@ -466,21 +514,17 @@
 
         if (gReadingOut) {
             
-            
             const int32_t ttms = gTrgTimer.read_ms(); // time since last trigger
             gTrgTimer.reset(); gTrgTimer.start();     // restart trigger timer
             etms += ttms;
             
             Watchdog::kick(); // don't reset!
-            
+                        
             //
             // got trigger. read registers to mbed and build the event
             //
             
             led4=1;
-#ifdef DEBUG
-            printf("readout\r\n");
-#endif
             
             // read data & calc CRC
 #ifdef EVT_TIME_PROFILE
@@ -488,7 +532,7 @@
 #endif
 
             gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit);
-
+            
 #ifdef EVT_TIME_PROFILE
             prof.stop(); aftReadWv=prof.read_us(); prof.start();
 #endif
@@ -580,6 +624,10 @@
             befReadWv, aftReadWv, befSaveEvt, aftSaveEvt,
             befChkPow, aftChkPow, befNewSeq, aftNewSeq, endOfLoop);
 #endif
+
+        // get ready to trigger
+        PIN_spi.format( 16, 1 ); // change to data readout format
+        PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
     }
 
 }
@@ -674,7 +722,7 @@
         PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak);
         wait_ms(10);
     }
-    wait(1);
+    wait(1); // let things power up
 #ifdef DEBUG
     printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
         isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
@@ -794,32 +842,8 @@
     wait_ms(200);
     MakeOutputFile();
     
-    // force a trigger every...
-#ifdef USE_RTOS_TIMER
-    const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
-                                     kAbsMaxTimer);
-    const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
-                                     kAbsMaxTimer);
-    const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
-                                     kCommWinLongPrdTk);
-    const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
-                                     kAbsMaxTimer);
-#else
-    const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
-                                     kAbsMaxTimer, &procForceTrigger);
-    const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
-                                     kAbsMaxTimer, &procHeartbeat);
-    const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
-                                     kCommWinLongPrdTk, &procCommWin);
-    const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
-                                     kAbsMaxTimer, &procPowerCheck);
-#endif
-#ifdef DEBUG
-    printf("attach force trig %u\r\n",ftp);
-    printf("attach heart beat %u\r\n",hbp);
-    printf("attach comm win   %u\r\n",cwp);
-    printf("attach power chk  %u\r\n",pcp);
-#endif
+    // reset tickers
+    ResetAllTickers();
     
     // TODO: change comm parameters
     /*
@@ -851,6 +875,9 @@
     printf("cards powered=%d\r\n",(int)AreCardsPowered());
 #endif
     
+    PIN_spi.format( 16, 1 ); // back to trigger mode
+    PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
+
     if (AreCardsPowered()) {
         
         if (gFirstEvt==false) {
@@ -877,12 +904,18 @@
                 return; // break out to open comms or check power
             }
         }
-#ifdef DEBUG
-        printf("starting readout. force=%d, clk=%d\r\n",
-            PIN_forceTrigger.read(), PIN_a_sf_clk.read());
-#endif
         PIN_forceTrigger=0;   // necessary for forced triggers, harmless for other triggers
         gReadingOut = true;   // disallow new forced triggers
+        
+        // we can't be interrupted before data arrives at the MB FPGA
+        //StopAllTickers();
+/*
+                      procForceTrigger();
+                      procHeartbeat();
+                      procPowerCheck();
+                      procCommWin();
+*/
+
         //
         // collect data from daughter cards
         //
@@ -901,6 +934,10 @@
                 i--;
             }
         }
+        
+        // restart the timers
+        //ResetAllTickers();
+        
     } else {
         // cards have no power. don't try reading out
         gReadingOut=false;
@@ -943,8 +980,12 @@
         // (probably) power down cards,amps and power up comms
         SetPower(true);
         
-        const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ?
-            gConf.GetCommWinDuration() : kConnectTimeout;
+        
+        // TODO: implement asking the comms for their timeout
+        //const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ?
+        //    gConf.GetCommWinDuration() : kConnectTimeout;
+        const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTOSBD) ?
+            gConf.GetCommWinDuration() : kConnectTOSBD;
         const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ?
             gConf.GetCommWinDuration() : kListenTimeout;
         
@@ -1016,13 +1057,21 @@
 #endif
                     gotNewConfig = (cfgres!=SnCommWin::kOkWthMsgNoConf);
                     Watchdog::kick(); // don't reset!
-                    (*cw)->CloseConn();
                     break;
                 }
             }
             
             Watchdog::kick(); // don't reset!
         }
+        // close the connection(s)
+        cw = gComms;
+        for (uint8_t i=0; i<kNcomms; i++, cw++) {
+            if ((*cw)==0) {
+                continue;
+            }
+            (*cw)->CloseConn(GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
+            Watchdog::kick(); // don't reset!
+        }
     }
         
     // (probably) power down comms and power up cards,amps