Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Oct 10 05:54:12 2012 +0000
Revision:
21:ce51bb0ba4a5
Parent:
18:55f1581f2ee4
Child:
22:f957c4f840ad
Uses USB comm. Fix rates calc. Power up/down ETH with Afar. Fix sending evt with status. Add num files and bytes of data to status. Can save a local file (i.e. reprogram MBED) via comms. Set config after each comm win (need if cards pow cycle).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "SnConfigFrame.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "mbed.h"
uci1 0:664899e0b988 4
uci1 0:664899e0b988 5 #include "SnBitUtils.h"
uci1 16:744ce85aede2 6 #include "SnHeaderFrame.h"
uci1 0:664899e0b988 7
uci1 0:664899e0b988 8 extern "C" void mbed_mac_address(char *);
uci1 0:664899e0b988 9
uci1 0:664899e0b988 10
uci1 18:55f1581f2ee4 11 const uint8_t SnConfigFrame::kIOVers = 4;
uci1 3:24c5f0f50bf1 12 const char* const SnConfigFrame::kDefConfFile = "/local/DEFCONF.DAT";
uci1 1:e392595b4b76 13 const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr = 14400; // exclusive min low power comm win period (s)
uci1 1:e392595b4b76 14 const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr = 259200; // exclusive max low power comm win period (s)
uci1 1:e392595b4b76 15 const uint32_t SnConfigFrame::kMinCommWinDurLowPwr = 300; // exclusive min low power comm win duration (s)
uci1 1:e392595b4b76 16 const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr = 3600; // exclusive max low power comm win duration (s)
uci1 1:e392595b4b76 17 const uint8_t SnConfigFrame::kConfLblLen;
uci1 0:664899e0b988 18
uci1 0:664899e0b988 19 uint64_t SnConfigFrame::fgMacAdr = 0;
uci1 0:664899e0b988 20
uci1 0:664899e0b988 21 void SnConfigFrame::SetMacAddress() {
uci1 0:664899e0b988 22 static const uint8_t b64 = sizeof(uint64_t);
uci1 0:664899e0b988 23 static char c[b64];
uci1 0:664899e0b988 24 mbed_mac_address(&(c[0]));
uci1 0:664899e0b988 25 // like a big endian union
uci1 0:664899e0b988 26 fgMacAdr = 0;
uci1 0:664899e0b988 27 const char* a = c+(b64-1);
uci1 0:664899e0b988 28 for (uint8_t i=0; i<b64; i++, a--) {
uci1 0:664899e0b988 29 fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3);
uci1 0:664899e0b988 30 }
uci1 0:664899e0b988 31 }
uci1 0:664899e0b988 32
uci1 0:664899e0b988 33 void SnConfigFrame::SetHardDefaults() {
uci1 0:664899e0b988 34 sprintf(fLabel,"HardDefaults");
uci1 0:664899e0b988 35 fConfTime = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT
uci1 0:664899e0b988 36 fRun = 0;
uci1 0:664899e0b988 37 fFirstEvt = 0;
uci1 8:95a325df1f6b 38 fEvtsPerSeq = 1000;
uci1 8:95a325df1f6b 39 fRunMode = 0;
uci1 1:e392595b4b76 40 fStreamHiLoPlas = 0;
uci1 0:664899e0b988 41 fWvLoseLSB = 0;
uci1 1:e392595b4b76 42 fWvLoseMSB = 4;
uci1 0:664899e0b988 43 fWvBaseline = 0;
uci1 0:664899e0b988 44 fDatPackType = ~0;
uci1 0:664899e0b988 45 uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 46 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 47 *dc = 3072u;
uci1 0:664899e0b988 48 }
uci1 0:664899e0b988 49 fNumPlas = 1;
uci1 0:664899e0b988 50 uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 51 for (uint8_t j=0; j<kNplas; j++, pl++) {
uci1 0:664899e0b988 52 *pl = 0x7FFFu;
uci1 0:664899e0b988 53 }
uci1 3:24c5f0f50bf1 54 fNumCardsMajLog = 1;
uci1 0:664899e0b988 55 fEnableThermTrig = 1;
uci1 1:e392595b4b76 56 fForceTrigPeriod = 67u;
uci1 0:664899e0b988 57 fHeartBeatPeriod = 0;
uci1 0:664899e0b988 58 fAmpsOn = 0x0Fu;
uci1 0:664899e0b988 59 fEvtThrtlPeriodMs = 50u;
uci1 4:a91682e19d6b 60 fPowerMode = kAmpsDatTak|kCardDatTak|kIridComWin|kAfarComWin;
uci1 0:664899e0b988 61 fBatVoltLowPwr = 0;
uci1 8:95a325df1f6b 62 fVoltCheckPeriod = 1200u;
uci1 0:664899e0b988 63 fCommWinPeriod = 3300u;
uci1 1:e392595b4b76 64 fCommWinDuration = 600u;
uci1 0:664899e0b988 65 fCommSendData = 0;
uci1 0:664899e0b988 66 fCommWinPrdLowPwr = 86100u;
uci1 0:664899e0b988 67 fCommWinDurLowPwr = 300u;
uci1 8:95a325df1f6b 68 sprintf(fRemoteServer,"%s","128.195.204.151");
uci1 8:95a325df1f6b 69 fRemotePort = 6655;
uci1 8:95a325df1f6b 70 sprintf(fMbedIP,"%s","128.195.204.148");
uci1 8:95a325df1f6b 71 sprintf(fMbedMask,"%s","255.255.255.0");
uci1 8:95a325df1f6b 72 sprintf(fMbedGate,"%s","128.195.204.1");
uci1 0:664899e0b988 73 fWatchDogPeriod = kWDFailsafe;
uci1 8:95a325df1f6b 74
uci1 8:95a325df1f6b 75 fIsLowPower = false;
uci1 8:95a325df1f6b 76 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 77 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 78
uci1 8:95a325df1f6b 79
uci1 8:95a325df1f6b 80 }
uci1 8:95a325df1f6b 81
uci1 21:ce51bb0ba4a5 82 uint32_t SnConfigFrame::GetTimeoutTime(const uint32_t startTime,
uci1 21:ce51bb0ba4a5 83 const uint32_t delta) const {
uci1 21:ce51bb0ba4a5 84 const uint32_t ct = time(0);
uci1 21:ce51bb0ba4a5 85 uint32_t lst = time(0)-startTime;
uci1 21:ce51bb0ba4a5 86 if ( (ct<startTime) || (ct==0) ||
uci1 21:ce51bb0ba4a5 87 (lst>kSecsPerDay) ) {
uci1 21:ce51bb0ba4a5 88 // possible clock problems
uci1 21:ce51bb0ba4a5 89 lst = kDefTimeout;
uci1 21:ce51bb0ba4a5 90 }
uci1 21:ce51bb0ba4a5 91 const uint32_t lio =
uci1 21:ce51bb0ba4a5 92 ((lst+delta) < GetCommWinDuration()) ?
uci1 21:ce51bb0ba4a5 93 lst+delta : GetCommWinDuration();
uci1 21:ce51bb0ba4a5 94 return lio+startTime;
uci1 21:ce51bb0ba4a5 95 }
uci1 21:ce51bb0ba4a5 96
uci1 8:95a325df1f6b 97 void SnConfigFrame::ChangeToLowPower() {
uci1 8:95a325df1f6b 98
uci1 8:95a325df1f6b 99 // save old label
uci1 8:95a325df1f6b 100 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 101
uci1 8:95a325df1f6b 102 // append label
uci1 8:95a325df1f6b 103 // this will allow the new config to be put in the DB
uci1 8:95a325df1f6b 104 int slen = strlen(fLabel);
uci1 8:95a325df1f6b 105 static const char* tag = "_LOWPOW";
uci1 8:95a325df1f6b 106 const int ml = strlen(tag)+1;
uci1 8:95a325df1f6b 107 if (slen > (kConfLblLen-ml) ) {
uci1 8:95a325df1f6b 108 memset(fLabel+kConfLblLen-ml, '\0', ml);
uci1 8:95a325df1f6b 109 }
uci1 8:95a325df1f6b 110 strncat(fLabel, tag, ml-1);
uci1 8:95a325df1f6b 111
uci1 8:95a325df1f6b 112 // save power settings
uci1 8:95a325df1f6b 113 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 114
uci1 8:95a325df1f6b 115 // change power settings
uci1 8:95a325df1f6b 116 DisablePowerFor(kAmpsDatTak);
uci1 8:95a325df1f6b 117 DisablePowerFor(kCardDatTak);
uci1 8:95a325df1f6b 118 DisablePowerFor(kIridDatTak);
uci1 8:95a325df1f6b 119 DisablePowerFor(kAfarDatTak);
uci1 8:95a325df1f6b 120 DisablePowerFor(kAmpsComWin);
uci1 8:95a325df1f6b 121 DisablePowerFor(kCardComWin);
uci1 8:95a325df1f6b 122 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 8:95a325df1f6b 123 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 8:95a325df1f6b 124 // TODO: turn on only iridum maybe?
uci1 8:95a325df1f6b 125 EnablePowerFor(kIridComWin);
uci1 8:95a325df1f6b 126 EnablePowerFor(kAfarComWin);
uci1 8:95a325df1f6b 127 }
uci1 8:95a325df1f6b 128 // set mode to low power
uci1 8:95a325df1f6b 129 fIsLowPower = true;
uci1 8:95a325df1f6b 130 }
uci1 8:95a325df1f6b 131
uci1 8:95a325df1f6b 132 void SnConfigFrame::ChangeToNormPower() {
uci1 8:95a325df1f6b 133 // put label back
uci1 8:95a325df1f6b 134 memcpy(fLabel, fNormLabel, kConfLblLen);
uci1 8:95a325df1f6b 135 // put power settings back
uci1 8:95a325df1f6b 136 fPowerMode = fNormPowerMode;
uci1 8:95a325df1f6b 137 // set mode to normal
uci1 8:95a325df1f6b 138 fIsLowPower = false;
uci1 0:664899e0b988 139 }
uci1 0:664899e0b988 140
uci1 0:664899e0b988 141 void SnConfigFrame::GetPackParsFor(const EDatPackBit d,
uci1 0:664899e0b988 142 uint8_t& loseLSB, uint8_t& loseMSB,
uci1 0:664899e0b988 143 uint16_t& wvBase) const {
uci1 0:664899e0b988 144 const bool pack = IsDatPackedFor(d);
uci1 0:664899e0b988 145 loseLSB = pack ? GetWvLoseLSB() : 0u;
uci1 0:664899e0b988 146 loseMSB = pack ? GetWvLoseMSB() : 0u;
uci1 0:664899e0b988 147 wvBase = pack ? GetWvBaseline() : 0u;
uci1 0:664899e0b988 148 }
uci1 0:664899e0b988 149
uci1 0:664899e0b988 150 void SnConfigFrame::GetHiLoPlas(const uint16_t pla,
uci1 0:664899e0b988 151 uint16_t& hiPla,
uci1 0:664899e0b988 152 uint16_t& loPla,
uci1 0:664899e0b988 153 const bool r2l) {
uci1 0:664899e0b988 154 // split the PLA bitword into 2: one for the high threshold
uci1 0:664899e0b988 155 // and one for the low threshold. "lows" in the string will become
uci1 0:664899e0b988 156 // "highs" in the low threshold PLA.
uci1 0:664899e0b988 157 //
uci1 0:664899e0b988 158 // example 1)
uci1 0:664899e0b988 159 // PLA string = HLHL....
uci1 0:664899e0b988 160 // hi thresh = H.H.....
uci1 0:664899e0b988 161 // lo thresh = .H.H....
uci1 0:664899e0b988 162 //
uci1 0:664899e0b988 163 // example 2)
uci1 0:664899e0b988 164 // PLA string = HBL.....
uci1 0:664899e0b988 165 // hi thresh = HL......
uci1 0:664899e0b988 166 // lo thresh = .LH.....
uci1 0:664899e0b988 167 //
uci1 0:664899e0b988 168 // (with . = A here, to make the example more readable)
uci1 0:664899e0b988 169 //
uci1 0:664899e0b988 170 // A = 11, B = 00
uci1 0:664899e0b988 171 // H = 01 or 10, alternating
uci1 0:664899e0b988 172 // L = 10 or 01, alternating
uci1 0:664899e0b988 173 // 01 at leftmost bits is H
uci1 0:664899e0b988 174 // for example:
uci1 0:664899e0b988 175 // 0x7FFF = 01 11 11 11 11 11 11 11
uci1 0:664899e0b988 176 // => HAAAAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 177 // => AAAAAAAH for RIGHT TO LEFT
uci1 0:664899e0b988 178 // 0x56FF = 01 01 01 10 11 11 11 11
uci1 0:664899e0b988 179 // => HLHHAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 180 // => AAAAHHLH for RIGHT TO LEFT
uci1 0:664899e0b988 181 //
uci1 0:664899e0b988 182 // so HHHHHHHH is
uci1 0:664899e0b988 183 // 01 10 01 10 01 10 01 10 always (r2l or l2r)
uci1 0:664899e0b988 184 //
uci1 0:664899e0b988 185 // r2l = whether to read bits right to left (true) or not (false)
uci1 0:664899e0b988 186 // Mahshid liked right to left
uci1 0:664899e0b988 187 // Liang liked left to right
uci1 0:664899e0b988 188 // so we allow for either
uci1 0:664899e0b988 189
uci1 0:664899e0b988 190 const int8_t start = (r2l) ? 0 : BITS_IN_SHORT-2;
uci1 0:664899e0b988 191 const int8_t end = (r2l) ? BITS_IN_SHORT : -2;
uci1 0:664899e0b988 192 const int8_t step = (r2l) ? 2 : -2;
uci1 0:664899e0b988 193
uci1 0:664899e0b988 194 uint8_t hi= (r2l) ? 0x2 : 0x1;
uci1 0:664899e0b988 195 uint8_t lo= (r2l) ? 0x1 : 0x2;
uci1 0:664899e0b988 196
uci1 0:664899e0b988 197 // set all bits to 0
uci1 0:664899e0b988 198 hiPla = 0;
uci1 0:664899e0b988 199 loPla = 0;
uci1 0:664899e0b988 200
uci1 0:664899e0b988 201 for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) {
uci1 0:664899e0b988 202 const uint8_t b = (pla & (0x3<<i)) >> i;
uci1 0:664899e0b988 203 if (b==hi) {
uci1 0:664899e0b988 204 hiPla |= hi << i;
uci1 0:664899e0b988 205 loPla |= 0x3 << i;
uci1 0:664899e0b988 206 } else if (b==lo) {
uci1 0:664899e0b988 207 hiPla |= 0x3 << i;
uci1 0:664899e0b988 208 loPla |= hi << i;
uci1 0:664899e0b988 209 } else if (b==0x3) {
uci1 0:664899e0b988 210 // any
uci1 0:664899e0b988 211 hiPla |= 0x3 << i;
uci1 0:664899e0b988 212 loPla |= 0x3 << i;
uci1 0:664899e0b988 213 } else {
uci1 0:664899e0b988 214 // no check that b is something else.. should be impossible.
uci1 0:664899e0b988 215 // between
uci1 0:664899e0b988 216 hiPla |= lo << i;
uci1 0:664899e0b988 217 loPla |= lo << i;
uci1 0:664899e0b988 218 }
uci1 0:664899e0b988 219 }
uci1 0:664899e0b988 220
uci1 0:664899e0b988 221 }
uci1 0:664899e0b988 222
uci1 0:664899e0b988 223 bool SnConfigFrame::ReadFromFile(const char* cfile) {
uci1 0:664899e0b988 224 // intended only for reading default config file
uci1 0:664899e0b988 225
uci1 3:24c5f0f50bf1 226 /*
uci1 3:24c5f0f50bf1 227 DIR* d;
uci1 3:24c5f0f50bf1 228 struct dirent* dent;
uci1 3:24c5f0f50bf1 229 printf("files in /local:\r\n");
uci1 3:24c5f0f50bf1 230 if ( (d = opendir( "/local" ))!=NULL ) {
uci1 3:24c5f0f50bf1 231 while ( (dent = readdir(d))!=NULL ) {
uci1 3:24c5f0f50bf1 232 printf("%s\r\n",dent->d_name);
uci1 3:24c5f0f50bf1 233 }
uci1 3:24c5f0f50bf1 234 closedir(d);
uci1 3:24c5f0f50bf1 235 }
uci1 3:24c5f0f50bf1 236 */
uci1 0:664899e0b988 237 bool ret = false;
uci1 0:664899e0b988 238 FILE* cf = fopen(cfile,"rb");
uci1 0:664899e0b988 239 if (cf!=0) {
uci1 16:744ce85aede2 240 // check the header and file size to be
uci1 16:744ce85aede2 241 // protect a bit against corruption
uci1 16:744ce85aede2 242 uint8_t hc; uint32_t hl;
uci1 16:744ce85aede2 243 SnHeaderFrame::ReadFrom(cf, hc, hl);
uci1 16:744ce85aede2 244 if (hc==SnHeaderFrame::kConfigCode) {
uci1 16:744ce85aede2 245 const int fpos = ftell(cf);
uci1 16:744ce85aede2 246 // how many bytes?
uci1 16:744ce85aede2 247 fseek(cf, 0, SEEK_END); // go to end
uci1 16:744ce85aede2 248 const int fend = ftell(cf);
uci1 16:744ce85aede2 249 fseek(cf, fpos, SEEK_SET); // go back
uci1 16:744ce85aede2 250
uci1 16:744ce85aede2 251 if (hl == fend-fpos) {
uci1 16:744ce85aede2 252 ReadFrom(cf);
uci1 16:744ce85aede2 253 ret = (ferror(cf)==0);
uci1 16:744ce85aede2 254 }
uci1 16:744ce85aede2 255 }
uci1 0:664899e0b988 256 fclose(cf);
uci1 0:664899e0b988 257 }
uci1 0:664899e0b988 258 return ret;
uci1 0:664899e0b988 259 }
uci1 0:664899e0b988 260
uci1 0:664899e0b988 261 bool SnConfigFrame::WriteToFile(const char* cfile) const {
uci1 0:664899e0b988 262 // intended only for writing default config file
uci1 0:664899e0b988 263
uci1 0:664899e0b988 264 bool ret = false;
uci1 0:664899e0b988 265 FILE* cf = fopen(cfile,"wb");
uci1 0:664899e0b988 266 if (cf!=0) {
uci1 0:664899e0b988 267 WriteTo(cf);
uci1 0:664899e0b988 268 ret = (ferror(cf)==0);
uci1 0:664899e0b988 269 fclose(cf);
uci1 0:664899e0b988 270 }
uci1 0:664899e0b988 271 return ret;
uci1 0:664899e0b988 272 }