Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 16 04:47:44 2012 +0000
Revision:
22:f957c4f840ad
Parent:
21:ce51bb0ba4a5
Child:
40:1324da35afd4
USB comm only. Make firing of comm window independent of real time clock. Add heartbeat firing time to the data stream.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #ifndef SN_SnEventFrame
uci1 0:664899e0b988 2 #define SN_SnEventFrame
uci1 0:664899e0b988 3
uci1 0:664899e0b988 4 #include <stdint.h>
uci1 0:664899e0b988 5 #include "SnConstants.h"
uci1 0:664899e0b988 6
uci1 15:f2569d8e4176 7 //#define EVDEBUG
uci1 15:f2569d8e4176 8
uci1 0:664899e0b988 9 class SnEventFrame {
uci1 0:664899e0b988 10
uci1 0:664899e0b988 11 public:
uci1 0:664899e0b988 12 // i/o version
uci1 8:95a325df1f6b 13 static const uint8_t kIOVers; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
uci1 0:664899e0b988 14 static const uint32_t kMaxSizeOf =
uci1 0:664899e0b988 15 ((sizeof(uint32_t)*4u)+sizeof(int32_t)+sizeof(uint16_t)
uci1 0:664899e0b988 16 +(kTotSamps*sizeof(uint16_t))+1u);
uci1 0:664899e0b988 17
uci1 0:664899e0b988 18 private:
uci1 0:664899e0b988 19 // !!
uci1 0:664899e0b988 20 // !! If any member variables change, update: SizeOf function and kIOVers value! (also if kNchans, etc. change!)
uci1 0:664899e0b988 21 // !!
uci1 0:664899e0b988 22
uci1 0:664899e0b988 23 uint16_t fData[kTotSamps]; // the (uncompressed) waveform data
uci1 0:664899e0b988 24 uint32_t fMbedTime; // mbed time in seconds since epoch
uci1 0:664899e0b988 25 // TODO: time since last event?
uci1 0:664899e0b988 26 uint32_t fEvtNum; // written event number ...
uci1 0:664899e0b988 27 int32_t fDTms; // time since last written event (ms)
uci1 0:664899e0b988 28 uint32_t fTrgNum; // this event is trigger number ...
uci1 0:664899e0b988 29 uint16_t fTrgBits; // trigger bit word
uci1 0:664899e0b988 30 mutable uint32_t fCRC; // CRC on the uncompressed waveform data
uci1 0:664899e0b988 31 // TODO: check if other parameters should be added:
uci1 0:664899e0b988 32 // - stop position?
uci1 0:664899e0b988 33 // - card(s) producing trigger?
uci1 0:664899e0b988 34 // - power & voltage during acquisition?
uci1 0:664899e0b988 35
uci1 0:664899e0b988 36 void CalcCRC();
uci1 0:664899e0b988 37
uci1 0:664899e0b988 38 static
uci1 0:664899e0b988 39 bool ReadFromFileToBuf(FILE* f,
uci1 0:664899e0b988 40 char* const evtBuf,
uci1 0:664899e0b988 41 const uint8_t loseLSB, const uint8_t loseMSB);
uci1 0:664899e0b988 42
uci1 0:664899e0b988 43 static
uci1 0:664899e0b988 44 bool WriteToFileFromBuf(FILE* f, char* const evtBuf,
uci1 0:664899e0b988 45 const uint8_t loseLSB, const uint8_t loseMSB);
uci1 0:664899e0b988 46
uci1 0:664899e0b988 47 public:
uci1 0:664899e0b988 48 SnEventFrame() { ClearEvent(); }
uci1 0:664899e0b988 49 virtual ~SnEventFrame() {}
uci1 0:664899e0b988 50
uci1 0:664899e0b988 51 const uint16_t* GetData() const { return fData; }
uci1 0:664899e0b988 52 uint16_t* GetData() { return fData; }
uci1 0:664899e0b988 53 const uint16_t* GetData(const uint8_t ch) const { return fData + (ch*kNsamps); }
uci1 0:664899e0b988 54 uint16_t* GetData(const uint8_t ch) { return fData + (ch*kNsamps); }
uci1 0:664899e0b988 55 uint16_t GetData(const uint8_t ch, const uint8_t sm) const
uci1 0:664899e0b988 56 { return fData[(ch*kNsamps)+sm]; }
uci1 0:664899e0b988 57 uint16_t& GetData(const uint8_t ch, const uint8_t sm)
uci1 0:664899e0b988 58 { return fData[(ch*kNsamps)+sm]; }
uci1 0:664899e0b988 59
uci1 22:f957c4f840ad 60 uint32_t GetMbedTime() const { return fMbedTime; }
uci1 22:f957c4f840ad 61
uci1 8:95a325df1f6b 62 uint32_t GetEvtNum() const { return fEvtNum; }
uci1 8:95a325df1f6b 63
uci1 22:f957c4f840ad 64 void ClearEvent(const bool fully=true) {
uci1 21:ce51bb0ba4a5 65 #ifdef EVDEBUG
uci1 21:ce51bb0ba4a5 66 printf("CLEARING EVENT!\r\n");
uci1 21:ce51bb0ba4a5 67 #endif
uci1 22:f957c4f840ad 68 fMbedTime = 0;
uci1 0:664899e0b988 69 memset(fData, 0, kTotSamps*sizeof(int16_t));
uci1 0:664899e0b988 70 fCRC = 0;
uci1 22:f957c4f840ad 71 if (fully) {
uci1 22:f957c4f840ad 72 fEvtNum = fTrgNum = 0;
uci1 22:f957c4f840ad 73 fTrgBits = 0;
uci1 22:f957c4f840ad 74 }
uci1 0:664899e0b988 75 }
uci1 0:664899e0b988 76
uci1 0:664899e0b988 77 void SetTrgBit(const ESnTrgTypes t) { fTrgBits |= kTrgBW[t]; }
uci1 0:664899e0b988 78 void SetTrgNum(const uint32_t t) { fTrgNum = t; }
uci1 0:664899e0b988 79 void SetEvtNum(const uint32_t n) { fEvtNum = n; }
uci1 0:664899e0b988 80 void SetDTms(const int32_t dtms) { fDTms = dtms; }
uci1 0:664899e0b988 81 void SetCurMbedTime() { fMbedTime = time(0); }
uci1 0:664899e0b988 82
uci1 0:664899e0b988 83 bool IsForcedTrg() const { return (fTrgBits & kTrgBW[kFrcTrg])!=0; }
uci1 0:664899e0b988 84
uci1 0:664899e0b988 85 void ReadWaveforms(SPI& spi,
uci1 0:664899e0b988 86 DigitalOut& cardHiBit, DigitalOut& cardLoBit) {
uci1 0:664899e0b988 87 uint16_t* dev=fData;
uci1 0:664899e0b988 88 for( uint8_t ch = 0; ch < kNchans; ch++ ) {
uci1 0:664899e0b988 89 // Pick which register to read.
uci1 0:664899e0b988 90 SnBitUtils::SetChanNumBits(ch, cardHiBit, cardLoBit);
uci1 0:664899e0b988 91 for( uint8_t i = 0; i < kNsamps; i++, dev++ ) {
uci1 0:664899e0b988 92 *dev = spi.write(0x00) >> 1;
uci1 0:664899e0b988 93 }
uci1 0:664899e0b988 94 }
uci1 15:f2569d8e4176 95 #ifdef EVDEBUG
uci1 15:f2569d8e4176 96 dev = fData;
uci1 15:f2569d8e4176 97 for (uint8_t ch=0; ch<kNchans; ch++) {
uci1 15:f2569d8e4176 98 for (uint8_t i=0; i<kNsamps; i++, dev++) {
uci1 15:f2569d8e4176 99 printf("(%hhd,%03hhd,%hd) ",ch,i,*dev);
uci1 15:f2569d8e4176 100 }
uci1 15:f2569d8e4176 101 printf("\r\n");
uci1 15:f2569d8e4176 102 }
uci1 15:f2569d8e4176 103 #endif
uci1 0:664899e0b988 104 CalcCRC();
uci1 0:664899e0b988 105 }
uci1 0:664899e0b988 106
uci1 0:664899e0b988 107 static
uci1 8:95a325df1f6b 108 uint32_t SizeOf(const uint8_t rv, const uint8_t loseLSB, const uint8_t loseMSB) {
uci1 0:664899e0b988 109 // size of member vars + size of packed waveform + 1 for i/o version
uci1 0:664899e0b988 110 if ((loseLSB==0) && (loseMSB==0)) {
uci1 0:664899e0b988 111 return kMaxSizeOf;
uci1 0:664899e0b988 112 } else {
uci1 0:664899e0b988 113 return (kMaxSizeOf-(kTotSamps*sizeof(uint16_t))
uci1 0:664899e0b988 114 +SizeOfPackedWavef(loseLSB, loseMSB));
uci1 0:664899e0b988 115 }
uci1 0:664899e0b988 116 }
uci1 0:664899e0b988 117
uci1 0:664899e0b988 118 static
uci1 0:664899e0b988 119 uint32_t SizeOfPackedWavef(const uint8_t loseLSB,
uci1 0:664899e0b988 120 const uint8_t loseMSB) {
uci1 0:664899e0b988 121 const uint8_t p = BITS_IN_SHORT-loseLSB-loseMSB;
uci1 0:664899e0b988 122 return ((p*kTotSamps)/8u) + (((p*kTotSamps)%8u)!=0 ? 1u : 0u);
uci1 0:664899e0b988 123 }
uci1 0:664899e0b988 124
uci1 3:24c5f0f50bf1 125 const char* ReadFrom(const char* const buf,
uci1 3:24c5f0f50bf1 126 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 3:24c5f0f50bf1 127 const uint16_t wvBaseline);
uci1 0:664899e0b988 128
uci1 3:24c5f0f50bf1 129 char* WriteTo(char* const buf,
uci1 3:24c5f0f50bf1 130 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 3:24c5f0f50bf1 131 const uint16_t wvBaseline) const;
uci1 0:664899e0b988 132
uci1 0:664899e0b988 133 bool ReadFrom(FILE* f, char* const evtBuf,
uci1 0:664899e0b988 134 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 135 const uint16_t wvBaseline);
uci1 0:664899e0b988 136 bool WriteTo(FILE* f, char* const evtBuf,
uci1 0:664899e0b988 137 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 138 const uint16_t wvBaseline) const;
uci1 0:664899e0b988 139
uci1 0:664899e0b988 140 static
uci1 0:664899e0b988 141 const uint8_t* UnpackWavef(const uint8_t* const buf,
uci1 0:664899e0b988 142 uint16_t* const data,
uci1 0:664899e0b988 143 const uint8_t loseLSB,
uci1 0:664899e0b988 144 const uint8_t loseMSB,
uci1 0:664899e0b988 145 const uint16_t wvBaseline);
uci1 0:664899e0b988 146
uci1 0:664899e0b988 147 static
uci1 0:664899e0b988 148 uint8_t* PackWavef(uint8_t* const buf, const uint16_t* const data,
uci1 0:664899e0b988 149 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 150 const uint16_t wvBaseline);
uci1 0:664899e0b988 151 };
uci1 0:664899e0b988 152
uci1 0:664899e0b988 153
uci1 0:664899e0b988 154 #endif // SN_SnEventFrame