Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
56:0bba0ef15697
Parent:
40:1324da35afd4
Child:
84:80b15993944e
--- a/SnEventFrame.h	Fri Jan 03 13:03:01 2014 +0000
+++ b/SnEventFrame.h	Thu Oct 30 06:42:17 2014 +0000
@@ -11,9 +11,13 @@
  public:
     // i/o version
     static const uint8_t    kIOVers;   // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
-    static const uint32_t   kMaxSizeOf = 
+    static const uint32_t   kMaxSizeOfV1 =  
             ((sizeof(uint32_t)*4u)+sizeof(int32_t)+sizeof(uint16_t)
-            +(kTotSamps*sizeof(uint16_t))+1u);
+            +(kTotSampsAtwd4ch*sizeof(uint16_t))+1u);
+    static const uint32_t   kMaxSizeOfV2 = kMaxSizeOfV1
+            - (kTotSampsAtwd4ch*sizeof(uint16_t)) + (kTotSampsSst4ch*sizeof(uint16_t))
+            + (kNstopBytesSst4ch*sizeof(uint8_t));
+    static const uint32_t   kMaxSizeOf = kMaxSizeOfV2; // biggest one
 
  private:
     // !!
@@ -28,10 +32,12 @@
     uint32_t            fTrgNum;           // this event is trigger number ...
     uint16_t            fTrgBits;          // trigger bit word
     mutable uint32_t    fCRC;              // CRC on the uncompressed waveform data
+#if CHIPBOARD!=ATWD4CH
+    uint8_t             fStop[kNstopBytes];// the stop bit word (one bit per sample)
+#endif
     // TODO: check if other parameters should be added:
     // - stop position?
     // - card(s) producing trigger?
-    // - power & voltage during acquisition?
     
     void            CalcCRC();
     
@@ -49,24 +55,34 @@
     virtual ~SnEventFrame() {}
 
     virtual void CopyTo(SnEventFrame& evt) const {
-        evt.fMbedTime = fMbedTime;
-        evt.fEvtNum   = fEvtNum;
-        evt.fDTms     = fDTms;
-        evt.fTrgNum   = fTrgNum;
-        evt.fTrgBits  = fTrgBits;
-        evt.fCRC      = fCRC;
-        memcpy(evt.fData, fData, kTotSamps*sizeof(uint16_t));
+        if (&evt!=this) {
+            evt.fMbedTime = fMbedTime;
+            evt.fEvtNum   = fEvtNum;
+            evt.fDTms     = fDTms;
+            evt.fTrgNum   = fTrgNum;
+            evt.fTrgBits  = fTrgBits;
+            evt.fCRC      = fCRC;
+            memcpy(evt.fData, fData, kTotSamps*sizeof(uint16_t));
+#if CHIPBOARD!=ATWD4CH
+            memcpy(evt.fStop, fStop, kNstopBytes*sizeof(uint8_t));
+#endif
+        }
     }
     
     const uint16_t* GetData() const { return fData; }
           uint16_t* GetData()       { return fData; }
     const uint16_t* GetData(const uint8_t ch) const { return fData + (ch*kNsamps); }
           uint16_t* GetData(const uint8_t ch)       { return fData + (ch*kNsamps); }
-          uint16_t  GetData(const uint8_t ch, const uint8_t sm) const
+          uint16_t  GetData(const uint8_t ch, const uint16_t sm) const
         { return fData[(ch*kNsamps)+sm]; }
-          uint16_t& GetData(const uint8_t ch, const uint8_t sm)
+          uint16_t& GetData(const uint8_t ch, const uint16_t sm)
         { return fData[(ch*kNsamps)+sm]; }
     
+#if CHIPBOARD!=ATWD4CH
+    const uint8_t*  GetStop() const { return fStop; }
+          uint8_t*  GetStop()       { return fStop; }
+#endif
+        
     uint32_t GetMbedTime() const { return fMbedTime; }
     
     uint32_t GetEvtNum() const { return fEvtNum; }
@@ -77,6 +93,9 @@
 #endif
         fMbedTime = 0;
         memset(fData, 0, kTotSamps*sizeof(int16_t));
+#if CHIPBOARD!=ATWD4CH
+        memset(fStop, 0, kNstopBytes*sizeof(uint8_t));
+#endif
         fCRC = 0;
         if (fully) {
             fEvtNum = fTrgNum = 0;
@@ -92,21 +111,106 @@
     
     bool IsForcedTrg() const { return (fTrgBits & kTrgBW[kFrcTrg])!=0; }
     
-    void ReadWaveforms(SPI& spi,
-                       DigitalOut& cardHiBit, DigitalOut& cardLoBit) {
+#if CHIPBOARD!=ATWD4CH
+    void ReadWaveformsSST(SPI& spi, DigitalOut& readingData) {
+        readingData   = 1;  // start reading
+
+        // first get waveform data
+        uint16_t* d = fData;
+        for (uint8_t ch=0; ch<kNchans; ++ch) {
+#ifdef EVDEBUG
+            printf("new channel %hhu\r\n",ch);
+#endif
+
+            spi.format( 5, 1 ); // throw away leading bits (1 meaningless + 4 MSB 0's bits before 12 bit ADC value)
+            spi.write(0);       // get the throw away bits
+            spi.format( 12, 1); // read remaining
+
+            for (uint16_t i=0; i<kNsamps; ++i, ++d) {
+                *d = spi.write(0x00)/* >> 1*/; // get rid of stop bit (TODO: is this right?)
+                if (i==0) {
+                    spi.format( 16, 1); // back to normal
+                }
+#ifdef EVDEBUG
+                if (i==0 || i==1) {
+                    printf("d: spi gave (%hu) ",*d);
+                    SnBitUtils::printBits(*d, false);
+                }
+#endif
+            }
+        }
+        // now get stop data
+        uint8_t* s = fStop;
+        uint16_t sd(0);
+        for (uint16_t i=0; i<kNstopBytes; i+=2, s+=2) {
+            if (i==0) {
+                // need to throw away the very first bit
+                // but we can't ask spi to send only 1 bit
+                spi.format( 5, 1 );
+                sd = spi.write(0x00);
+                // toss the first bit totally
+                // move the other 4 bits to the front
+                sd <<= 12;
+                spi.format( 12, 1); // read the rest of the real 16 bits
+                sd |= spi.write(0x00);
+                spi.format( 16, 1); // normal after this
+            } else {
+                sd = spi.write(0x00);
+            }
+#ifdef EVDEBUG
+            /*
+            printf("s: spi gave (%hu) ",sd);
+            SnBitUtils::printBits(sd, false);
+            */
+#endif
+            *s     = (sd & 0xFF00) >> BITS_IN_CHAR;
+            *(s+1) =  sd & 0x00FF;
+        }
+        readingData   = 0;  // done reading
+        /*
+        // get rid of stop bit (TODO: is this right?)
+        *s >>= 1;
+        *s <<= 1;
+        */
+#ifdef EVDEBUG
+        /*
+        d = fData;
+        for (uint8_t ch=0; ch<kNchans; ch++) {
+            for (uint16_t i=0; i<kNsamps; i++, d++) {
+                printf("(%hhu,%03hhu,%hu) ",ch,i,*d);
+            }
+            printf("\r\n");
+        }
+        s = fStop;
+        for (uint16_t i=0; i<kNstopBytes; ++i, ++s) {
+            for (int16_t j=(sizeof(uint8_t)*BITS_IN_CHAR)-1; j>-1; --j) {
+                if ( (*s&(1<<j))!=0 ) {
+                    printf("stop bit at %hu\r\n",
+                        j+(BITS_IN_CHAR*sizeof(uint8_t)*i));
+                }
+            }
+        }
+        */
+#endif
+        CalcCRC();
+    }
+#endif
+    
+    void ReadWaveformsATWD(SPI& spi,
+                           DigitalOut& cardHiBit, DigitalOut& cardLoBit) {
         uint16_t* dev=fData;
         for( uint8_t ch = 0; ch < kNchans; ch++ ) {
             // Pick which register to read.
             SnBitUtils::SetChanNumBits(ch, cardHiBit, cardLoBit);
-            for( uint8_t i = 0; i < kNsamps; i++, dev++ ) {        
+            for( uint16_t i = 0; i < kNsamps; i++, dev++ ) {        
                 *dev = spi.write(0x00) >> 1;
             }
         }
 #ifdef EVDEBUG
         dev = fData;
         for (uint8_t ch=0; ch<kNchans; ch++) {
-            for (uint8_t i=0; i<kNsamps; i++, dev++) {
-                printf("(%hhd,%03hhd,%hd) ",ch,i,*dev);
+            for (uint16_t i=0; i<kNsamps; i++, dev++) {
+                printf("(%hhu,%03hhu,%hu) ",ch,i,*dev);
             }
             printf("\r\n");
         }
@@ -114,14 +218,42 @@
         CalcCRC();
     }
     
+        
+    static
+    uint16_t GetTotSamplesForIOVers(const uint8_t rv) {
+        if (rv<2) {
+            return kTotSampsAtwd4ch;
+        } else {
+            return kTotSampsSst4ch;
+        }
+    }
+    
+    static
+    uint16_t GetStopBytesForIOVers(const uint8_t rv) {
+        if (rv<2) {
+            return 0;
+        } else {
+            return kNstopBytesSst4ch;
+        }
+    }
+    
     static
     uint32_t SizeOf(const uint8_t rv, const uint8_t loseLSB, const uint8_t loseMSB) {
         // size of member vars + size of packed waveform + 1 for i/o version
-        if ((loseLSB==0) && (loseMSB==0)) {
-            return kMaxSizeOf;
+        uint32_t sz(0);
+        const uint16_t ntotsamps = GetTotSamplesForIOVers(rv);
+        if (rv==1) {
+            sz = kMaxSizeOfV1;
+        } else if (rv==2) {
+            sz = kMaxSizeOfV2;
         } else {
-            return (kMaxSizeOf-(kTotSamps*sizeof(uint16_t))
-                               +SizeOfPackedWavef(loseLSB, loseMSB));
+            sz = kMaxSizeOf;
+        }
+        if ((loseLSB==0) && (loseMSB==0)) {
+            return sz;
+        } else {
+            return (sz-(ntotsamps*sizeof(uint16_t))
+                      +SizeOfPackedWavef(loseLSB, loseMSB));
         }
     }
     
@@ -152,12 +284,14 @@
                                uint16_t* const data,
                                const uint8_t loseLSB,
                                const uint8_t loseMSB,
-                               const uint16_t wvBaseline);
+                               const uint16_t wvBaseline,
+                               const uint16_t nsamps);
     
     static
     uint8_t* 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);
 };