Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Jun 30 02:03:51 2012 +0000
Revision:
0:664899e0b988
Child:
1:e392595b4b76
first version. SD card writing and data readout works. communications not tested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #ifndef SN_SnConfigFrame
uci1 0:664899e0b988 2 #define SN_SnConfigFrame
uci1 0:664899e0b988 3
uci1 0:664899e0b988 4 #include <stdint.h>
uci1 0:664899e0b988 5 #include "SnConstants.h"
uci1 0:664899e0b988 6 #include "SnBitUtils.h"
uci1 0:664899e0b988 7
uci1 0:664899e0b988 8 class SnConfigFrame {
uci1 0:664899e0b988 9 public:
uci1 0:664899e0b988 10 static const uint32_t kMinCommWinPrdLowPwr; // exclusive min low power comm win period (s)
uci1 0:664899e0b988 11 static const uint32_t kMaxCommWinPrdLowPwr; // exclusive max low power comm win period (s)
uci1 0:664899e0b988 12 static const uint32_t kMinCommWinDurLowPwr; // exclusive min low power comm win duration (s)
uci1 0:664899e0b988 13 static const uint32_t kMaxCommWinDurLowPwr; // exclusive max low power comm win duration (s)
uci1 0:664899e0b988 14 static const uint8_t kConfLblLen=64; // length of configuration label char array (63+'\0')
uci1 0:664899e0b988 15
uci1 0:664899e0b988 16 static const char* kDefConfFile; // default configuration file
uci1 0:664899e0b988 17
uci1 0:664899e0b988 18 static const uint32_t kMaxSizeOf =
uci1 0:664899e0b988 19 1u + // the i/o version
uci1 0:664899e0b988 20 + (8u*sizeof(uint32_t)) + (8u*sizeof(uint16_t))
uci1 0:664899e0b988 21 + (9u*sizeof(uint8_t)) + (2u*kNplas*sizeof(uint16_t))
uci1 0:664899e0b988 22 + (kConfLblLen*sizeof(uint8_t));
uci1 0:664899e0b988 23
uci1 0:664899e0b988 24
uci1 0:664899e0b988 25 enum EDatPackBit {
uci1 0:664899e0b988 26 kSDcard = BIT(0),
uci1 0:664899e0b988 27 kIrid = BIT(1),
uci1 0:664899e0b988 28 kAfar = BIT(2),
uci1 0:664899e0b988 29 kUSB = BIT(3)
uci1 0:664899e0b988 30 };
uci1 0:664899e0b988 31
uci1 0:664899e0b988 32 enum ESendDataBit {
uci1 0:664899e0b988 33 // can't use BIT(0)! (-0 = 0 => send nothing)
uci1 0:664899e0b988 34 kAllFiles = BIT(1), // if bit=0 => send most recent file
uci1 0:664899e0b988 35 kTimeout = BIT(2), // if bit=0 => ignore timeout
uci1 0:664899e0b988 36 kDelete = BIT(3) // if bit=0 => do not delete sent files
uci1 0:664899e0b988 37 };
uci1 0:664899e0b988 38
uci1 0:664899e0b988 39 // i/o version
uci1 0:664899e0b988 40 static const int8_t kIOVers; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
uci1 0:664899e0b988 41
uci1 0:664899e0b988 42 private:
uci1 0:664899e0b988 43 // !!
uci1 0:664899e0b988 44 // !! If any member variables change, update: SizeOf function and kIOVers value! (also if kNchans, etc. change!)
uci1 0:664899e0b988 45 // !!
uci1 0:664899e0b988 46
uci1 0:664899e0b988 47 // mbed mac address
uci1 0:664899e0b988 48 static uint64_t fgMacAdr; // mbed mac address
uci1 0:664899e0b988 49 // conf header
uci1 0:664899e0b988 50 char fLabel[kConfLblLen]; // configuration label
uci1 0:664899e0b988 51 uint32_t fConfTime; // cpu config time
uci1 0:664899e0b988 52 uint32_t fRun; // run number
uci1 0:664899e0b988 53 uint32_t fFirstEvt; // starting event number
uci1 0:664899e0b988 54 uint8_t fStreamHiLoPlas; // (1byte bool) if true, add the separated hi/lo thresh PLA patterns to the i/o
uci1 0:664899e0b988 55 // data packing
uci1 0:664899e0b988 56 uint8_t fWvLoseLSB; // number of least significant bits to lose when packing waveform data
uci1 0:664899e0b988 57 uint8_t fWvLoseMSB; // number of most significant bits to lose when packing waveform data
uci1 0:664899e0b988 58 uint16_t fWvBaseline; // global baseline to use when packing data (useful to reduce clipping on the high end)
uci1 0:664899e0b988 59 uint8_t fDatPackType; // type of data packing. OR'd bitword: if bit 1, will pack for writing. see EDatPackBit. default: always pack (all 1's)
uci1 0:664899e0b988 60 // trigger setup
uci1 0:664899e0b988 61 uint16_t fDAC[kNchans][kNfpgaDacs]; //[card id][dac id] values should be 0-4095 here (not checked tho)
uci1 0:664899e0b988 62 uint8_t fNumPlas; // number of patterns to use. must be <= kNplas.
uci1 0:664899e0b988 63 uint16_t fPLA[kNplas]; //[pattern id] (same for each card)
uci1 0:664899e0b988 64 uint8_t fNumCardsMajLog; // number of cards participating in the MajLogic trigger (1 to 4)
uci1 0:664899e0b988 65 uint8_t fEnableThermTrig; // (1byte bool) whether or not to allow thermal triggers
uci1 0:664899e0b988 66 uint16_t fForceTrigPeriod; // number of seconds between force triggers (0=none)
uci1 0:664899e0b988 67 uint16_t fHeartBeatPeriod; // number of seconds between heartbeats (0=none)
uci1 0:664899e0b988 68 uint8_t fAmpsOn; // which amps are on (bit word. uint8_t => 8 amps max)
uci1 0:664899e0b988 69 uint16_t fEvtThrtlPeriodMs; // throttle period to write events (ms)
uci1 0:664899e0b988 70 // power
uci1 0:664899e0b988 71 uint8_t fPowerMode; // power mode bit word
uci1 0:664899e0b988 72 int16_t fBatVoltLowPwr; // battery level at which to switch to low power (not used?)
uci1 0:664899e0b988 73 // communication
uci1 0:664899e0b988 74 uint32_t fCommWinPeriod; // seconds between communication window startup (0=always on)
uci1 0:664899e0b988 75 uint32_t fCommWinDuration; // seconds that communication window stays open (0=always open)
uci1 0:664899e0b988 76 int16_t fCommSendData; // data to send during comm win (=0: none, >0=send up to x events from last file until comm win closes, <0=see ESendDataBit)
uci1 0:664899e0b988 77 uint32_t fCommWinPrdLowPwr; // low power communication window period (seconds) (range enforced)
uci1 0:664899e0b988 78 uint32_t fCommWinDurLowPwr; // low power communication window duration (seconds) (range enforced)
uci1 0:664899e0b988 79 // watchdog
uci1 0:664899e0b988 80 uint32_t fWatchDogPeriod; // number of seconds of inactivity for watchdog to issue a reset
uci1 0:664899e0b988 81
uci1 0:664899e0b988 82 void SetHardDefaults();
uci1 0:664899e0b988 83
uci1 0:664899e0b988 84 static
uci1 0:664899e0b988 85 uint32_t SizeOf(const bool streamHiLoPlas, const uint8_t lblLen) {
uci1 0:664899e0b988 86 // private because it cannot be used to read from a buffer
uci1 0:664899e0b988 87 // (the label length and fStreamHiLoPlas are not known a priori)
uci1 0:664899e0b988 88 // returns the num of bytes needed to stream this object
uci1 0:664899e0b988 89 // = size of member vars + 1 for i/o version + extra PLA strings (maybe)
uci1 0:664899e0b988 90 uint32_t sz = kMaxSizeOf - kConfLblLen + lblLen;
uci1 0:664899e0b988 91 if (streamHiLoPlas==false) {
uci1 0:664899e0b988 92 static const uint32_t shlp = 2u*kNplas*sizeof(uint16_t);
uci1 0:664899e0b988 93 sz -= shlp;
uci1 0:664899e0b988 94 }
uci1 0:664899e0b988 95 return sz;
uci1 0:664899e0b988 96 }
uci1 0:664899e0b988 97
uci1 0:664899e0b988 98 public:
uci1 0:664899e0b988 99 SnConfigFrame() { Reset(); }
uci1 0:664899e0b988 100 virtual ~SnConfigFrame() {}
uci1 0:664899e0b988 101
uci1 0:664899e0b988 102 const char* GetLabel() const { return fLabel; }
uci1 0:664899e0b988 103 uint32_t GetLabelStrLen() const { return strlen(fLabel); }
uci1 0:664899e0b988 104 uint32_t GetRun() const { return fRun; }
uci1 0:664899e0b988 105 uint32_t GetFirstEvt() const { return fFirstEvt; }
uci1 0:664899e0b988 106 uint16_t GetEvtThrtlPeriodMs() const { return fEvtThrtlPeriodMs; }
uci1 0:664899e0b988 107 uint16_t GetForceTrigPeriod() const { return fForceTrigPeriod; }
uci1 0:664899e0b988 108 uint32_t GetWatchdogPeriod() const { return fWatchDogPeriod; }
uci1 0:664899e0b988 109 uint16_t GetDac(const uint8_t ch, const uint8_t dn) const { return fDAC[ch][dn]; }
uci1 0:664899e0b988 110 uint8_t GetNumPlas() const { return fNumPlas; }
uci1 0:664899e0b988 111 uint16_t GetPla(const uint8_t pn) const { return fPLA[pn]; }
uci1 0:664899e0b988 112 uint8_t GetNumCardsMajLog() const { return fNumCardsMajLog; }
uci1 0:664899e0b988 113 bool IsThermTrigEnabled() const { return fEnableThermTrig!=0; }
uci1 0:664899e0b988 114 bool IsEachAmpOn() const {
uci1 0:664899e0b988 115 bool allon=true;
uci1 0:664899e0b988 116 for (uint8_t i=0; (i<kNchans) && allon; i++) {
uci1 0:664899e0b988 117 allon = (fAmpsOn & BIT(i))!=0;
uci1 0:664899e0b988 118 }
uci1 0:664899e0b988 119 return allon;
uci1 0:664899e0b988 120 }
uci1 0:664899e0b988 121 // TODO: allow check for individual amps, when they can be turned on individually
uci1 0:664899e0b988 122
uci1 0:664899e0b988 123 uint32_t GetCommWinPeriod() const { return fCommWinPeriod; }
uci1 0:664899e0b988 124 uint32_t GetCommWinDuration() const { return fCommWinDuration; }
uci1 0:664899e0b988 125 int16_t GetCommSendData() const { return fCommSendData; }
uci1 0:664899e0b988 126
uci1 0:664899e0b988 127 bool IsSendingAllFiles() const
uci1 0:664899e0b988 128 { return (fCommSendData<0) && ((fCommSendData & kAllFiles)!=0); }
uci1 0:664899e0b988 129 bool IsObeyingTimeout() const
uci1 0:664899e0b988 130 { return (fCommSendData<0) && ((fCommSendData & kTimeout)!=0); }
uci1 0:664899e0b988 131 bool IsDeletingFiles() const
uci1 0:664899e0b988 132 { return (fCommSendData<0) && ((fCommSendData & kDelete)!=0); }
uci1 0:664899e0b988 133
uci1 0:664899e0b988 134
uci1 0:664899e0b988 135 const char* GetOutFileName(const char* dir) const;
uci1 0:664899e0b988 136
uci1 0:664899e0b988 137 // waveform packing info
uci1 0:664899e0b988 138 uint16_t GetWvBaseline() const { return fWvBaseline; }
uci1 0:664899e0b988 139 uint8_t GetWvLoseLSB() const { return fWvLoseLSB; }
uci1 0:664899e0b988 140 uint8_t GetWvLoseMSB() const { return fWvLoseMSB; }
uci1 0:664899e0b988 141 bool IsDatPackedFor(const EDatPackBit d) const { return (fDatPackType & d)!=0; }
uci1 0:664899e0b988 142 void GetPackParsFor(const EDatPackBit d,
uci1 0:664899e0b988 143 uint8_t& loseLSB, uint8_t& loseMSB,
uci1 0:664899e0b988 144 uint16_t& wvBase) const;
uci1 0:664899e0b988 145
uci1 0:664899e0b988 146 // i/o
uci1 0:664899e0b988 147 template<class T>
uci1 0:664899e0b988 148 void ReadFrom(T& b) {
uci1 0:664899e0b988 149 // no check on the length of buf is done here
uci1 0:664899e0b988 150 // that should be been done already
uci1 0:664899e0b988 151 //
uci1 0:664899e0b988 152 // must match WriteTo
uci1 0:664899e0b988 153
uci1 0:664899e0b988 154 int8_t Rv=0;
uci1 0:664899e0b988 155 b = SnBitUtils::ReadFrom(b, Rv); // i/o version
uci1 0:664899e0b988 156 if (Rv>0) {
uci1 0:664899e0b988 157 uint32_t llen=kConfLblLen;
uci1 0:664899e0b988 158 b = SnBitUtils::ReadFrom(b, llen);
uci1 0:664899e0b988 159 b = SnBitUtils::ReadFrom(b, fLabel, llen);
uci1 0:664899e0b988 160 b = SnBitUtils::ReadFrom(b, fConfTime);
uci1 0:664899e0b988 161 b = SnBitUtils::ReadFrom(b, fRun);
uci1 0:664899e0b988 162 b = SnBitUtils::ReadFrom(b, fFirstEvt);
uci1 0:664899e0b988 163 b = SnBitUtils::ReadFrom(b, fStreamHiLoPlas);
uci1 0:664899e0b988 164 b = SnBitUtils::ReadFrom(b, fWvLoseLSB);
uci1 0:664899e0b988 165 b = SnBitUtils::ReadFrom(b, fWvLoseMSB);
uci1 0:664899e0b988 166 b = SnBitUtils::ReadFrom(b, fWvBaseline);
uci1 0:664899e0b988 167 b = SnBitUtils::ReadFrom(b, fDatPackType);
uci1 0:664899e0b988 168 uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 169 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 170 b = SnBitUtils::ReadFrom(b, *dc);
uci1 0:664899e0b988 171 }
uci1 0:664899e0b988 172 b = SnBitUtils::ReadFrom(b, fNumPlas);
uci1 0:664899e0b988 173 uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 174 for (uint8_t j=0; j<fNumPlas; j++, pl++) {
uci1 0:664899e0b988 175 b = SnBitUtils::ReadFrom(b, *pl);
uci1 0:664899e0b988 176 }
uci1 0:664899e0b988 177 b = SnBitUtils::ReadFrom(b, fNumCardsMajLog);
uci1 0:664899e0b988 178 b = SnBitUtils::ReadFrom(b, fEnableThermTrig);
uci1 0:664899e0b988 179 b = SnBitUtils::ReadFrom(b, fForceTrigPeriod);
uci1 0:664899e0b988 180 b = SnBitUtils::ReadFrom(b, fHeartBeatPeriod);
uci1 0:664899e0b988 181 b = SnBitUtils::ReadFrom(b, fAmpsOn);
uci1 0:664899e0b988 182 b = SnBitUtils::ReadFrom(b, fEvtThrtlPeriodMs);
uci1 0:664899e0b988 183 b = SnBitUtils::ReadFrom(b, fPowerMode);
uci1 0:664899e0b988 184 b = SnBitUtils::ReadFrom(b, fBatVoltLowPwr);
uci1 0:664899e0b988 185 b = SnBitUtils::ReadFrom(b, fCommWinPeriod);
uci1 0:664899e0b988 186 b = SnBitUtils::ReadFrom(b, fCommWinDuration);
uci1 0:664899e0b988 187 b = SnBitUtils::ReadFrom(b, fCommSendData);
uci1 0:664899e0b988 188 b = SnBitUtils::ReadFrom(b, fCommWinPrdLowPwr);
uci1 0:664899e0b988 189 b = SnBitUtils::ReadFrom(b, fCommWinDurLowPwr);
uci1 0:664899e0b988 190 b = SnBitUtils::ReadFrom(b, fWatchDogPeriod);
uci1 0:664899e0b988 191 if (fStreamHiLoPlas!=0) {
uci1 0:664899e0b988 192 pl = fPLA;
uci1 0:664899e0b988 193 uint16_t hi, lo;
uci1 0:664899e0b988 194 for (uint8_t j=0; j<fNumPlas; j++, pl++) {
uci1 0:664899e0b988 195 GetHiLoPlas(*pl, hi, lo);
uci1 0:664899e0b988 196 b = SnBitUtils::ReadFrom(b, hi);
uci1 0:664899e0b988 197 b = SnBitUtils::ReadFrom(b, lo);
uci1 0:664899e0b988 198 }
uci1 0:664899e0b988 199 }
uci1 0:664899e0b988 200 }
uci1 0:664899e0b988 201 }
uci1 0:664899e0b988 202
uci1 0:664899e0b988 203 template <class T>
uci1 0:664899e0b988 204 void WriteTo(T& b) const {
uci1 0:664899e0b988 205 // no check on the length of the buf is done here
uci1 0:664899e0b988 206 // that should be done already
uci1 0:664899e0b988 207 //
uci1 0:664899e0b988 208 // must match ReadFromBuf
uci1 0:664899e0b988 209 //
uci1 0:664899e0b988 210 // intentionally not writing mac address here, so we don't have to read it in
uci1 0:664899e0b988 211
uci1 0:664899e0b988 212 const uint32_t llen = strlen(fLabel);
uci1 0:664899e0b988 213
uci1 0:664899e0b988 214 b = SnBitUtils::WriteTo(b, kIOVers); // i/o version
uci1 0:664899e0b988 215 b = SnBitUtils::WriteTo(b, llen);
uci1 0:664899e0b988 216 b = SnBitUtils::WriteTo(b, fLabel, llen);
uci1 0:664899e0b988 217 b = SnBitUtils::WriteTo(b, fConfTime);
uci1 0:664899e0b988 218 b = SnBitUtils::WriteTo(b, fRun);
uci1 0:664899e0b988 219 b = SnBitUtils::WriteTo(b, fFirstEvt);
uci1 0:664899e0b988 220 b = SnBitUtils::WriteTo(b, fStreamHiLoPlas);
uci1 0:664899e0b988 221 b = SnBitUtils::WriteTo(b, fWvLoseLSB);
uci1 0:664899e0b988 222 b = SnBitUtils::WriteTo(b, fWvLoseMSB);
uci1 0:664899e0b988 223 b = SnBitUtils::WriteTo(b, fWvBaseline);
uci1 0:664899e0b988 224 b = SnBitUtils::WriteTo(b, fDatPackType);
uci1 0:664899e0b988 225 const uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 226 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 227 b = SnBitUtils::WriteTo(b, *dc);
uci1 0:664899e0b988 228 }
uci1 0:664899e0b988 229 b = SnBitUtils::WriteTo(b, fNumPlas);
uci1 0:664899e0b988 230 const uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 231 for (uint8_t j=0; j<fNumPlas; j++, pl++) {
uci1 0:664899e0b988 232 b = SnBitUtils::WriteTo(b, *pl);
uci1 0:664899e0b988 233 }
uci1 0:664899e0b988 234 b = SnBitUtils::WriteTo(b, fNumCardsMajLog);
uci1 0:664899e0b988 235 b = SnBitUtils::WriteTo(b, fEnableThermTrig);
uci1 0:664899e0b988 236 b = SnBitUtils::WriteTo(b, fForceTrigPeriod);
uci1 0:664899e0b988 237 b = SnBitUtils::WriteTo(b, fHeartBeatPeriod);
uci1 0:664899e0b988 238 b = SnBitUtils::WriteTo(b, fAmpsOn);
uci1 0:664899e0b988 239 b = SnBitUtils::WriteTo(b, fEvtThrtlPeriodMs);
uci1 0:664899e0b988 240 b = SnBitUtils::WriteTo(b, fPowerMode);
uci1 0:664899e0b988 241 b = SnBitUtils::WriteTo(b, fBatVoltLowPwr);
uci1 0:664899e0b988 242 b = SnBitUtils::WriteTo(b, fCommWinPeriod);
uci1 0:664899e0b988 243 b = SnBitUtils::WriteTo(b, fCommWinDuration);
uci1 0:664899e0b988 244 b = SnBitUtils::WriteTo(b, fCommSendData);
uci1 0:664899e0b988 245 b = SnBitUtils::WriteTo(b, fCommWinPrdLowPwr);
uci1 0:664899e0b988 246 b = SnBitUtils::WriteTo(b, fCommWinDurLowPwr);
uci1 0:664899e0b988 247 b = SnBitUtils::WriteTo(b, fWatchDogPeriod);
uci1 0:664899e0b988 248 if (fStreamHiLoPlas!=0) {
uci1 0:664899e0b988 249 pl = fPLA;
uci1 0:664899e0b988 250 uint16_t hi, lo;
uci1 0:664899e0b988 251 for (uint8_t j=0; j<fNumPlas; j++, pl++) {
uci1 0:664899e0b988 252 GetHiLoPlas(*pl, hi, lo);
uci1 0:664899e0b988 253 b = SnBitUtils::WriteTo(b, hi);
uci1 0:664899e0b988 254 b = SnBitUtils::WriteTo(b, lo);
uci1 0:664899e0b988 255 }
uci1 0:664899e0b988 256 }
uci1 0:664899e0b988 257 }
uci1 0:664899e0b988 258
uci1 0:664899e0b988 259 bool ReadFromFile(const char* cfile);
uci1 0:664899e0b988 260 bool WriteToFile(const char* cfile) const;
uci1 0:664899e0b988 261
uci1 0:664899e0b988 262 void Reset() {
uci1 0:664899e0b988 263 memset(fLabel, 0, sizeof(char)*kConfLblLen);
uci1 0:664899e0b988 264 if (ReadFromFile(kDefConfFile)==false) {
uci1 0:664899e0b988 265 // couldn't get default. use hardcoded version.
uci1 0:664899e0b988 266 SetHardDefaults();
uci1 0:664899e0b988 267 }
uci1 0:664899e0b988 268 }
uci1 0:664899e0b988 269
uci1 0:664899e0b988 270 uint32_t SizeOf() const {
uci1 0:664899e0b988 271 // returns the num of bytes needed to stream this object
uci1 0:664899e0b988 272 // = size of member vars + 1 for i/o version + extra PLA strings (maybe)
uci1 0:664899e0b988 273 // + length of label string
uci1 0:664899e0b988 274 return SizeOf(fStreamHiLoPlas!=0, strlen(fLabel));
uci1 0:664899e0b988 275 }
uci1 0:664899e0b988 276
uci1 0:664899e0b988 277 static void SetMacAddress();
uci1 0:664899e0b988 278 static uint64_t GetMacAddress() {
uci1 0:664899e0b988 279 if (fgMacAdr==0) {
uci1 0:664899e0b988 280 SetMacAddress();
uci1 0:664899e0b988 281 }
uci1 0:664899e0b988 282 return fgMacAdr;
uci1 0:664899e0b988 283 }
uci1 0:664899e0b988 284
uci1 0:664899e0b988 285 static uint32_t GetLabelMaxLen() { return kConfLblLen; }
uci1 0:664899e0b988 286
uci1 0:664899e0b988 287 static void GetHiLoPlas(const uint16_t pla,
uci1 0:664899e0b988 288 uint16_t& hiPla,
uci1 0:664899e0b988 289 uint16_t& loPla,
uci1 0:664899e0b988 290 const bool r2l=false);
uci1 0:664899e0b988 291 };
uci1 0:664899e0b988 292
uci1 0:664899e0b988 293 #endif // SN_SnConfigFrame