Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
41:d6f5e2f09e07
Parent:
40:1324da35afd4
Child:
42:ac162d15e578
--- a/main.cpp	Sat Oct 05 04:45:22 2013 +0000
+++ b/main.cpp	Tue Oct 08 23:44:00 2013 +0000
@@ -5,26 +5,26 @@
 
 #define DISABLE_CONFIG_SAFETYNETS
 
-#define ENABLE_AFAR_COMM
-//#define ENABLE_SBD_COMM
+//#define ENABLE_AFAR_COMM
+#define ENABLE_SBD_COMM
 //#define ENABLE_USB_COMM
 //#define ENABLE_AFAR_TWITTER
 
 //#define USE_RTOS // change in CommConstants and EthernetPower also
 //#define USE_ETH_INTERFACE
 //#define EVT_TIME_PROFILE
-//#define DEBUG
+#define DEBUG
 //#define SSNOTIFY
-//#define USE_MODSERIAL
+#define USE_MODSERIAL
 
 #include <stdint.h>
 #include "SnConstants.h"
 
 #include "SDFileSystem.h"
 #ifdef USE_MODSERIAL
+#define MODSERIAL_RX_BUF_SIZE 512
+#define MODSERIAL_TX_BUF_SIZE 512 
 #include "MODSERIAL.h"
-#define MODSERIAL_RX_BUF_SIZE 512
-#define MODSERIAL_TX_BUF_SIZE 512
 #endif
 #include "FATDirHandle.h"
 #include "EthernetPowerControl.h"
@@ -36,22 +36,25 @@
 #include "SnHeaderFrame.h"
 #include "SnHeartbeatFrame.h"
 #include "SnClockSetFrame.h"
+#include "SnSignalStrengthFrame.h"
 #include "SnCommWin.h"
+#ifdef ENABLE_AFAR_COMM
 #ifdef USE_ETH_INTERFACE
 #include "SnCommAfarTCP.h"
 #else
-//#include "SnCommAfarNetIf.h"
 #include "SnCommWinAfar.h"
 #ifdef ENABLE_AFAR_TWITTER
-//#include "SnCommAfarNetIfTwitter.h"
 #include "SnCommWinTwitter.h"
-#endif
+#endif // ENABLE_AFAR_TWITTER
+#endif // USE_ETH_INTERFACE
+#endif // ENABLE_AFAR_COMM
+#ifdef ENABLE_USB_COMM
+#include "SnCommWinUsb.h"
 #endif
-//#include "SnCommUsb.h"
-#include "SnCommWinUsb.h"
-//#include "SnCommSBD.h"
+#ifdef ENABLE_SBD_COMM
 #include "SnCommWinSBD.h"
-#include "SnBase64.h"
+#endif
+//#include "SnBase64.h"
 #ifdef USE_RTOS_TIMER
 #include "RtosTimer.h"
 #endif
@@ -108,19 +111,25 @@
 SPI PIN_spi( p5, p6, p7 );
 // The SD card
 
-// this needs to be first in case some other global uses a print statement
+
+// we have to do this shit because Serial and MODSERIAL don't have virtual functions
+// so calling, e.g. readable from a Serial* will call Serial::readable instead of MODSERIAL::readable
+// and the comms will wait forever
 #ifdef USE_MODSERIAL
-#define SERIAL_TYPE MODSERIAL
+#define MAIN_SERIALTYPE AjK::MODSERIAL
 #else
-#define SERIAL_TYPE Serial
+#define MAIN_SERIALTYPE Serial
 #endif
-static SERIAL_TYPE      gCpu( USBTX, USBRX  // defined here so it might be used for debugging output
+
+// this needs to be first in case some other global uses a print statement
+static MAIN_SERIALTYPE  gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
+
+static MAIN_SERIALTYPE  gSBDport(p28, p27, 
 #ifdef USE_MODSERIAL
-                             ,MODSERIAL_TX_BUF_SIZE,
-                              MODSERIAL_RX_BUF_SIZE
+                                 MODSERIAL_TX_BUF_SIZE, MODSERIAL_RX_BUF_SIZE,
 #endif
-                                                    );
-static SERIAL_TYPE      gSBDport(p28, p27, "sbd");
+                                 "sbd");
+
 static SDFileSystem     sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); // no leading '/'
 static LocalFileSystem  local((SnCommWin::kLocalDir)+1); // no leading '/'
 
@@ -167,7 +176,8 @@
 static Timer          gAllTrgTimer;
 static Timer          gThmTrgTimer;
 static Timer          gAdcToMBtimer;
-static SnClockSetFrame gClkSet;
+static SnClockSetFrame       gClkSet;
+static SnSignalStrengthFrame gSigStr;
 #ifdef DISABLE_CONFIG_SAFETYNETS
 static SnConfigFrame  gConf(false);
 #else
@@ -546,7 +556,6 @@
     sd.disk_initialize();
 }
 
-
 int main() {
     // a failsafe
     Watchdog::kick(WDFAILSAFE);
@@ -554,7 +563,7 @@
     {
         gCpu.baud(CPUBAUD_SN);
 #if defined(SSNOTIFY) || defined(DEBUG)
-        printf("main: start\r\n");
+        printf("\n\n\n\n\nmain: start\r\n");
 #endif
 #ifdef USE_RTOS
         led1=1; Thread::wait(200);
@@ -580,24 +589,42 @@
 #ifdef ENABLE_AFAR_COMM
     // RTOS stuff must be made inside main for some reason    
 #ifdef USE_ETH_INTERFACE
+#ifdef DEBUG
+    printf("making SnCommAfarTCP\r\n");
+#endif
     gComms[comi++] = new SnCommAfarTCP(gConf);
 #else
+#ifdef DEBUG
+    printf("making SnCommWinAfar\r\n");
+#endif
     //gComms[comi++] = new SnCommAfarNetIf(gConf);
     gComms[comi++] = new SnCommWinAfar(gConf);
 #ifdef ENABLE_AFAR_TWITTER
+#ifdef DEBUG
+    printf("making SnCommAfarNetIfTwitter\r\n");
+#endif
     gTwit = new SnCommAfarNetIfTwitter(gConf);
 #endif
 #endif
 #endif
 #ifdef ENABLE_SBD_COMM
+#ifdef DEBUG
+    printf("making SnCommWinSBD\r\n");
+#endif
     gComms[comi++] = new SnCommWinSBD(&gSBDport);
 #endif
 #ifdef ENABLE_USB_COMM
+#ifdef DEBUG
+    printf("makin SnCommWinUsb\r\n");
+#endif
     gComms[comi++] = new SnCommWinUsb(&gCpu);
 #endif
 
 #ifdef DEBUG
     printf("made comm objects\r\n");
+#ifdef USE_MODSERIAL
+    printf("using MODSERIAL\r\n");
+#endif
 #endif
     
     if (comi!=kNcomms) {
@@ -614,11 +641,7 @@
 #endif    
     
     led2=1;
-    
-#ifdef DEBUG
-    printf("\n\n\n\n\n\nstarting\r\n");
-#endif
-        
+            
     // set the clock to the BS time, if it's not set
     if ( (static_cast<int32_t>(time(0)))<0 ) {
         set_time(kBStime);
@@ -652,11 +675,13 @@
 
     led2=0;
     
-    // the main event loop. wait for triggers in SendClock
+    // read and cache the dCard power pin setting,
+    // so we can use the cached value later
     AreCardsPowered(true); // TODO: should this be an if?
     register int32_t etms=0; // time between written events
-    while( true )
-    {
+
+    // the main event loop. wait for triggers in SendClock    
+    while ( true ) {
         // in here, we wait for triggers from the MB-FPGA
         Watchdog::kick(); // don't reset!
         
@@ -668,6 +693,9 @@
         printf("readingout=%d\r\n",(int)gReadingOut);
 #endif
         if (gFirstEvt) {
+#ifdef DEBUG
+            printf("WriteTrigWaitWinTime (start)\r\n");
+#endif
             SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                             gClkSet,
                                             true);
@@ -675,7 +703,7 @@
             gAllTrgTimer.reset(); gAllTrgTimer.start();
         }
         PIN_lockRegisters = 0; // allow data to come from DFPGA
-        WaitTrigAndSendClock();
+        WaitTrigAndSendClock();  // wiat for trigger and move data to MB. this returns immediately if cards are powered off
         PIN_lockRegisters = 1; // block registers during readout
         
 #ifdef EVT_TIME_PROFILE
@@ -791,6 +819,7 @@
         if (gOpenCommWin) {
 #ifdef DEBUG
             printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false");
+            printf("WriteTrigWaitWinTime (stop)\r\n");
 #endif
             SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                             gClkSet,
@@ -815,6 +844,7 @@
         if (IsSeqComplete()) {
 #ifdef DEBUG
             printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
+            printf("WriteTrigWaitWinTime (stop)\r\n");
 #endif
             SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                             gClkSet,
@@ -836,17 +866,19 @@
             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
+        */
         
         // reset event
         // clear after comm win, so full event can be sent with status
         // but don't clear counters or trigger bits, as
         gEvent.ClearEvent(true);
     
-    }
+    } // end while (true)
 
 }
 
@@ -1265,23 +1297,26 @@
         PIN_iridSbd_power.read(), PIN_afar_power.read());
     printf("cards powered=%d\r\n",(int)AreCardsPowered(true));
 #endif
-    
-    PIN_spi.format( 16, 1 ); // back to trigger mode
-    PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
 
-    if (AreCardsPowered(false)) {
-        
+    // set gFirstEvt to false even if cards are powered off.
+    // otherwise, if cards ARE powered off, it will always be
+    // true and the "start trigger" clock will be written continuously
 #ifdef DEBUG
-        printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
+    printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
 #endif
-        if (gFirstEvt==false) {
-            PIN_DoNotRestartAllClocks    = 0;
-            wait_us(1);
-            PIN_DoNotRestartAllClocks    = 1;
-            //led3 = !led3; // toggle send clock led
-        } else {
-            gFirstEvt = false;
-        }
+    if (gFirstEvt==false) {
+        PIN_DoNotRestartAllClocks    = 0;
+        wait_us(1);
+        PIN_DoNotRestartAllClocks    = 1;
+        //led3 = !led3; // toggle send clock led
+    } else {
+        gFirstEvt = false;
+    }
+        
+    if (AreCardsPowered(false)) {
+
+        PIN_spi.format( 16, 1 ); // back to trigger mode
+        PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
         
         //
         // wait for a trigger here.
@@ -1604,7 +1639,7 @@
             Watchdog::kick(); // don't reset!
         } // end loop over comms
         
-        // check Iridium time & close the connection(s)
+        // check Iridium time, send Iridium signal strength, and close the connection(s)
         cw = gComms;
         for (uint8_t i=0; i<kNcomms; i++, cw++) {
             if ((*cw)==0) {
@@ -1621,16 +1656,25 @@
                 printf("try to set iridium time\r\n");
 #endif
                 // set the clock before closing connection
-                const bool con = 
-                    (*cw)->Connect(gConf.GetTimeoutTime(gLastCommWin,
-                        gConf.GetCommWinDuration()));
+                const uint32_t totime = 
+                    gConf.GetTimeoutTime(gLastCommWin,
+                                         gConf.GetCommWinDuration());
+#ifdef DEBUG
+                printf("totime=%u, ctime=%u\r\n",totime,time(0));
+#endif
+                const bool con = (*cw)->Connect(totime);
                 if (con) {
                     uint32_t prvTime(0), setTime(0);
-                    const bool settime = (*cw)->TrySetSysTimeUnix(
-                        gConf.GetTimeoutTime(gLastCommWin,
-                                             gConf.GetCommWinDuration()),
-                        prvTime, setTime);
-                    gClkSet.SetClocks(prvTime, setTime);
+                    const bool gottime = (*cw)->TrySetSysTimeUnix(
+                        totime, prvTime, setTime);
+                    if (gottime) {
+                        gClkSet.SetClocks(prvTime, setTime);
+#ifdef DEBUG
+                        printf("sig str: totime=%u, ctime=%u\r\n",totime,time(0));
+#endif
+                        // got time; now send signal strength
+                        (*cw)->SendSignalStrength( gGenBuf, gSigStr, totime );
+                    }
                 }
             }
             // close the connection -- this must be why Twitter didn't work!