Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
56:0bba0ef15697
Parent:
42:ac162d15e578
Child:
114:554fa3a956b4
--- a/SnEventFrame.cpp	Fri Jan 03 13:03:01 2014 +0000
+++ b/SnEventFrame.cpp	Thu Oct 30 06:42:17 2014 +0000
@@ -4,7 +4,11 @@
 
 //#define DEBUG
 
+#if CHIPBOARD==ATWD4CH
 const uint8_t     SnEventFrame::kIOVers = 1;   // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
+#else // SST
+const uint8_t     SnEventFrame::kIOVers = 2;   // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
+#endif
 
 const char* SnEventFrame::ReadFrom(const char* const buf,
                                    const uint8_t loseLSB, const uint8_t loseMSB,
@@ -22,17 +26,27 @@
         const uint8_t* u;
     } b;
     b.s = buf;
-    
+   
     uint8_t Rv=0;
     b.s           = SnBitUtils::ReadFrom(b.s, Rv); // i/o version
     if (Rv>0) {
+
+        const uint16_t nsamps = GetTotSamplesForIOVers(Rv);
+
         b.s       = SnBitUtils::ReadFrom(b.s, fMbedTime);
         b.s       = SnBitUtils::ReadFrom(b.s, fEvtNum);
         b.s       = SnBitUtils::ReadFrom(b.s, fDTms);
         b.s       = SnBitUtils::ReadFrom(b.s, fTrgNum);
         b.s       = SnBitUtils::ReadFrom(b.s, fTrgBits);
-        b.u       = UnpackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline);
+        b.u       = UnpackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline, nsamps);
         b.s       = SnBitUtils::ReadFrom(b.s, fCRC);
+
+#if CHIPBOARD!=ATWD4CH
+        const uint16_t nstopBytes = GetStopBytesForIOVers(Rv);
+        if (Rv>1) {
+            b.s   = SnBitUtils::ReadFrom(b.s, fStop, nstopBytes);
+        }
+#endif
     }
     
     return b.s;
@@ -57,14 +71,27 @@
     
     b.s           = SnBitUtils::WriteTo(b.s, kIOVers); // i/o version
 
+    const uint16_t nsamps = GetTotSamplesForIOVers(kIOVers);
+    
     b.s           = SnBitUtils::WriteTo(b.s, fMbedTime);
     b.s           = SnBitUtils::WriteTo(b.s, fEvtNum);
     b.s           = SnBitUtils::WriteTo(b.s, fDTms);
     b.s           = SnBitUtils::WriteTo(b.s, fTrgNum);
     b.s           = SnBitUtils::WriteTo(b.s, fTrgBits);
-    b.u           = PackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline);
+    b.u           = PackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline, nsamps);
     b.s           = SnBitUtils::WriteTo(b.s, fCRC);
 
+#if CHIPBOARD!=ATWD4CH
+    const uint16_t nstopBytes = GetStopBytesForIOVers(kIOVers);
+    if (kIOVers>1) {
+        // we test kIOVers rather than the CHIPBOARD #define in order
+        // to ensure that the i/o version is the ONLY variable that determines
+        // what the event looks like in a file or communication.
+        // this is the only way to ensure readability going forward.
+        b.s       = SnBitUtils::WriteTo(b.s, fStop, nstopBytes);
+    }
+#endif
+
 #ifdef DEBUG
     printf("SnEventFrame::WriteTo:\r\n");
     for (uint32_t i=0; i<b.s-buf; i++) {
@@ -79,6 +106,9 @@
 void SnEventFrame::CalcCRC() {
     // CRC made using union on a little endian (mbed) processor
     fCRC = SnCRCUtils::GetCRC32for(fData, kTotSamps);
+#if CHIPBOARD!=ATWD4CH
+    fCRC = SnCRCUtils::GetUpdatedCRC32for(fCRC, fStop, kNstopBytes);
+#endif
 #ifdef DEBUG
     printf("SnEventFrame::CalcCRC  crc=%u\r\n",fCRC);
 #endif
@@ -135,7 +165,8 @@
 
 uint8_t* SnEventFrame::PackWavef(uint8_t* const buf, const uint16_t* const data,
                                  const uint8_t loseLSB, const uint8_t loseMSB,
-                                 const uint16_t wvBaseline) {
+                                 const uint16_t wvBaseline,
+                                 const uint16_t nsamps) {
     // Compress the data. This is potentially LOSSY; it depends
     // on the dynamic range and on the options.
     // See SnConfigFrame::fWvLoseLSB and SnConfigFrame::fWvLoseMSB.
@@ -172,7 +203,7 @@
     const uint16_t* dev = data;
     uint16_t dum;
     int8_t sbit=0;
-    for (uint16_t i=0; i<kTotSamps; ++i, ++dev) {
+    for (uint16_t i=0; i<nsamps; ++i, ++dev) {
         // dump the bits we don't want
         dum = (*dev) - wvBaseline;
         if (dum<clipHi) {
@@ -219,21 +250,22 @@
                                          uint16_t* const data,
                                          const uint8_t loseLSB,
                                          const uint8_t loseMSB,
-                                         const uint16_t wvBaseline) {
+                                         const uint16_t wvBaseline,
+                                         const uint16_t nsamps) {
     if (loseLSB==0 && loseMSB==0 && wvBaseline==0) {
-        memcpy(data, buf, kTotSamps*sizeof(uint16_t));
+        memcpy(data, buf, nsamps*sizeof(uint16_t));
     } else {
 
         const uint8_t packSmpBits = BITS_IN_SHORT-loseLSB-loseMSB;
     
         // make sure data is all 0's to start
-        memset(data, 0, kTotSamps*sizeof(uint16_t));
+        memset(data, 0, nsamps*sizeof(uint16_t));
         
         const uint8_t* b = buf;
         uint16_t* dev = data;
         uint16_t dum;
         int8_t sbit=0;
-        for (uint16_t i=0; i<kTotSamps; ++i, ++dev) {
+        for (uint16_t i=0; i<nsamps; ++i, ++dev) {
             dum  = (*b) << 8u;
             dum |= *(b+1);
             if (sbit<=0) {