Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnConfigFrame.cpp@0:664899e0b988, 2012-06-30 (annotated)
- 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?
User | Revision | Line number | New 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 | 0:664899e0b988 | 6 | |
uci1 | 0:664899e0b988 | 7 | extern "C" void mbed_mac_address(char *); |
uci1 | 0:664899e0b988 | 8 | |
uci1 | 0:664899e0b988 | 9 | |
uci1 | 0:664899e0b988 | 10 | const int8_t SnConfigFrame::kIOVers = 1; |
uci1 | 0:664899e0b988 | 11 | const char* SnConfigFrame::kDefConfFile = "/local/defaultConfig.dat"; |
uci1 | 0:664899e0b988 | 12 | const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr = 14400; // exclusive min low power comm win period (s) |
uci1 | 0:664899e0b988 | 13 | const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr = 259200; // exclusive max low power comm win period (s) |
uci1 | 0:664899e0b988 | 14 | const uint32_t SnConfigFrame::kMinCommWinDurLowPwr = 300; // exclusive min low power comm win duration (s) |
uci1 | 0:664899e0b988 | 15 | const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr = 3600; // exclusive max low power comm win duration (s) |
uci1 | 0:664899e0b988 | 16 | const uint8_t SnConfigFrame::kConfLblLen; |
uci1 | 0:664899e0b988 | 17 | |
uci1 | 0:664899e0b988 | 18 | uint64_t SnConfigFrame::fgMacAdr = 0; |
uci1 | 0:664899e0b988 | 19 | |
uci1 | 0:664899e0b988 | 20 | void SnConfigFrame::SetMacAddress() { |
uci1 | 0:664899e0b988 | 21 | static const uint8_t b64 = sizeof(uint64_t); |
uci1 | 0:664899e0b988 | 22 | static char c[b64]; |
uci1 | 0:664899e0b988 | 23 | mbed_mac_address(&(c[0])); |
uci1 | 0:664899e0b988 | 24 | // like a big endian union |
uci1 | 0:664899e0b988 | 25 | fgMacAdr = 0; |
uci1 | 0:664899e0b988 | 26 | const char* a = c+(b64-1); |
uci1 | 0:664899e0b988 | 27 | for (uint8_t i=0; i<b64; i++, a--) { |
uci1 | 0:664899e0b988 | 28 | fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3); |
uci1 | 0:664899e0b988 | 29 | } |
uci1 | 0:664899e0b988 | 30 | } |
uci1 | 0:664899e0b988 | 31 | |
uci1 | 0:664899e0b988 | 32 | void SnConfigFrame::SetHardDefaults() { |
uci1 | 0:664899e0b988 | 33 | sprintf(fLabel,"HardDefaults"); |
uci1 | 0:664899e0b988 | 34 | fConfTime = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT |
uci1 | 0:664899e0b988 | 35 | fRun = 0; |
uci1 | 0:664899e0b988 | 36 | fFirstEvt = 0; |
uci1 | 0:664899e0b988 | 37 | fStreamHiLoPlas = 1; |
uci1 | 0:664899e0b988 | 38 | fWvLoseLSB = 0; |
uci1 | 0:664899e0b988 | 39 | fWvLoseMSB = 0; |
uci1 | 0:664899e0b988 | 40 | fWvBaseline = 0; |
uci1 | 0:664899e0b988 | 41 | fDatPackType = ~0; |
uci1 | 0:664899e0b988 | 42 | uint16_t* dc = &(fDAC[0][0]); |
uci1 | 0:664899e0b988 | 43 | for (uint16_t i=0; i<kTotDacs; i++, dc++) { |
uci1 | 0:664899e0b988 | 44 | *dc = 3072u; |
uci1 | 0:664899e0b988 | 45 | } |
uci1 | 0:664899e0b988 | 46 | fNumPlas = 1; |
uci1 | 0:664899e0b988 | 47 | uint16_t* pl = &(fPLA[0]); |
uci1 | 0:664899e0b988 | 48 | for (uint8_t j=0; j<kNplas; j++, pl++) { |
uci1 | 0:664899e0b988 | 49 | *pl = 0x7FFFu; |
uci1 | 0:664899e0b988 | 50 | } |
uci1 | 0:664899e0b988 | 51 | fNumCardsMajLog = 2; |
uci1 | 0:664899e0b988 | 52 | fEnableThermTrig = 1; |
uci1 | 0:664899e0b988 | 53 | fForceTrigPeriod = 2u;//67u; |
uci1 | 0:664899e0b988 | 54 | fHeartBeatPeriod = 0; |
uci1 | 0:664899e0b988 | 55 | fAmpsOn = 0x0Fu; |
uci1 | 0:664899e0b988 | 56 | fEvtThrtlPeriodMs = 50u; |
uci1 | 0:664899e0b988 | 57 | fPowerMode = 0; |
uci1 | 0:664899e0b988 | 58 | fBatVoltLowPwr = 0; |
uci1 | 0:664899e0b988 | 59 | fCommWinPeriod = 3300u; |
uci1 | 0:664899e0b988 | 60 | fCommWinDuration = 300u; |
uci1 | 0:664899e0b988 | 61 | fCommSendData = 0; |
uci1 | 0:664899e0b988 | 62 | fCommWinPrdLowPwr = 86100u; |
uci1 | 0:664899e0b988 | 63 | fCommWinDurLowPwr = 300u; |
uci1 | 0:664899e0b988 | 64 | fWatchDogPeriod = kWDFailsafe; |
uci1 | 0:664899e0b988 | 65 | } |
uci1 | 0:664899e0b988 | 66 | |
uci1 | 0:664899e0b988 | 67 | void SnConfigFrame::GetPackParsFor(const EDatPackBit d, |
uci1 | 0:664899e0b988 | 68 | uint8_t& loseLSB, uint8_t& loseMSB, |
uci1 | 0:664899e0b988 | 69 | uint16_t& wvBase) const { |
uci1 | 0:664899e0b988 | 70 | const bool pack = IsDatPackedFor(d); |
uci1 | 0:664899e0b988 | 71 | loseLSB = pack ? GetWvLoseLSB() : 0u; |
uci1 | 0:664899e0b988 | 72 | loseMSB = pack ? GetWvLoseMSB() : 0u; |
uci1 | 0:664899e0b988 | 73 | wvBase = pack ? GetWvBaseline() : 0u; |
uci1 | 0:664899e0b988 | 74 | } |
uci1 | 0:664899e0b988 | 75 | |
uci1 | 0:664899e0b988 | 76 | void SnConfigFrame::GetHiLoPlas(const uint16_t pla, |
uci1 | 0:664899e0b988 | 77 | uint16_t& hiPla, |
uci1 | 0:664899e0b988 | 78 | uint16_t& loPla, |
uci1 | 0:664899e0b988 | 79 | const bool r2l) { |
uci1 | 0:664899e0b988 | 80 | // split the PLA bitword into 2: one for the high threshold |
uci1 | 0:664899e0b988 | 81 | // and one for the low threshold. "lows" in the string will become |
uci1 | 0:664899e0b988 | 82 | // "highs" in the low threshold PLA. |
uci1 | 0:664899e0b988 | 83 | // |
uci1 | 0:664899e0b988 | 84 | // example 1) |
uci1 | 0:664899e0b988 | 85 | // PLA string = HLHL.... |
uci1 | 0:664899e0b988 | 86 | // hi thresh = H.H..... |
uci1 | 0:664899e0b988 | 87 | // lo thresh = .H.H.... |
uci1 | 0:664899e0b988 | 88 | // |
uci1 | 0:664899e0b988 | 89 | // example 2) |
uci1 | 0:664899e0b988 | 90 | // PLA string = HBL..... |
uci1 | 0:664899e0b988 | 91 | // hi thresh = HL...... |
uci1 | 0:664899e0b988 | 92 | // lo thresh = .LH..... |
uci1 | 0:664899e0b988 | 93 | // |
uci1 | 0:664899e0b988 | 94 | // (with . = A here, to make the example more readable) |
uci1 | 0:664899e0b988 | 95 | // |
uci1 | 0:664899e0b988 | 96 | // A = 11, B = 00 |
uci1 | 0:664899e0b988 | 97 | // H = 01 or 10, alternating |
uci1 | 0:664899e0b988 | 98 | // L = 10 or 01, alternating |
uci1 | 0:664899e0b988 | 99 | // 01 at leftmost bits is H |
uci1 | 0:664899e0b988 | 100 | // for example: |
uci1 | 0:664899e0b988 | 101 | // 0x7FFF = 01 11 11 11 11 11 11 11 |
uci1 | 0:664899e0b988 | 102 | // => HAAAAAAA for LEFT TO RIGHT |
uci1 | 0:664899e0b988 | 103 | // => AAAAAAAH for RIGHT TO LEFT |
uci1 | 0:664899e0b988 | 104 | // 0x56FF = 01 01 01 10 11 11 11 11 |
uci1 | 0:664899e0b988 | 105 | // => HLHHAAAA for LEFT TO RIGHT |
uci1 | 0:664899e0b988 | 106 | // => AAAAHHLH for RIGHT TO LEFT |
uci1 | 0:664899e0b988 | 107 | // |
uci1 | 0:664899e0b988 | 108 | // so HHHHHHHH is |
uci1 | 0:664899e0b988 | 109 | // 01 10 01 10 01 10 01 10 always (r2l or l2r) |
uci1 | 0:664899e0b988 | 110 | // |
uci1 | 0:664899e0b988 | 111 | // r2l = whether to read bits right to left (true) or not (false) |
uci1 | 0:664899e0b988 | 112 | // Mahshid liked right to left |
uci1 | 0:664899e0b988 | 113 | // Liang liked left to right |
uci1 | 0:664899e0b988 | 114 | // so we allow for either |
uci1 | 0:664899e0b988 | 115 | |
uci1 | 0:664899e0b988 | 116 | const int8_t start = (r2l) ? 0 : BITS_IN_SHORT-2; |
uci1 | 0:664899e0b988 | 117 | const int8_t end = (r2l) ? BITS_IN_SHORT : -2; |
uci1 | 0:664899e0b988 | 118 | const int8_t step = (r2l) ? 2 : -2; |
uci1 | 0:664899e0b988 | 119 | |
uci1 | 0:664899e0b988 | 120 | uint8_t hi= (r2l) ? 0x2 : 0x1; |
uci1 | 0:664899e0b988 | 121 | uint8_t lo= (r2l) ? 0x1 : 0x2; |
uci1 | 0:664899e0b988 | 122 | |
uci1 | 0:664899e0b988 | 123 | // set all bits to 0 |
uci1 | 0:664899e0b988 | 124 | hiPla = 0; |
uci1 | 0:664899e0b988 | 125 | loPla = 0; |
uci1 | 0:664899e0b988 | 126 | |
uci1 | 0:664899e0b988 | 127 | for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) { |
uci1 | 0:664899e0b988 | 128 | const uint8_t b = (pla & (0x3<<i)) >> i; |
uci1 | 0:664899e0b988 | 129 | if (b==hi) { |
uci1 | 0:664899e0b988 | 130 | hiPla |= hi << i; |
uci1 | 0:664899e0b988 | 131 | loPla |= 0x3 << i; |
uci1 | 0:664899e0b988 | 132 | } else if (b==lo) { |
uci1 | 0:664899e0b988 | 133 | hiPla |= 0x3 << i; |
uci1 | 0:664899e0b988 | 134 | loPla |= hi << i; |
uci1 | 0:664899e0b988 | 135 | } else if (b==0x3) { |
uci1 | 0:664899e0b988 | 136 | // any |
uci1 | 0:664899e0b988 | 137 | hiPla |= 0x3 << i; |
uci1 | 0:664899e0b988 | 138 | loPla |= 0x3 << i; |
uci1 | 0:664899e0b988 | 139 | } else { |
uci1 | 0:664899e0b988 | 140 | // no check that b is something else.. should be impossible. |
uci1 | 0:664899e0b988 | 141 | // between |
uci1 | 0:664899e0b988 | 142 | hiPla |= lo << i; |
uci1 | 0:664899e0b988 | 143 | loPla |= lo << i; |
uci1 | 0:664899e0b988 | 144 | } |
uci1 | 0:664899e0b988 | 145 | } |
uci1 | 0:664899e0b988 | 146 | |
uci1 | 0:664899e0b988 | 147 | } |
uci1 | 0:664899e0b988 | 148 | |
uci1 | 0:664899e0b988 | 149 | bool SnConfigFrame::ReadFromFile(const char* cfile) { |
uci1 | 0:664899e0b988 | 150 | // intended only for reading default config file |
uci1 | 0:664899e0b988 | 151 | |
uci1 | 0:664899e0b988 | 152 | bool ret = false; |
uci1 | 0:664899e0b988 | 153 | FILE* cf = fopen(cfile,"rb"); |
uci1 | 0:664899e0b988 | 154 | if (cf!=0) { |
uci1 | 0:664899e0b988 | 155 | ReadFrom(cf); |
uci1 | 0:664899e0b988 | 156 | ret = (ferror(cf)==0); |
uci1 | 0:664899e0b988 | 157 | fclose(cf); |
uci1 | 0:664899e0b988 | 158 | } |
uci1 | 0:664899e0b988 | 159 | return ret; |
uci1 | 0:664899e0b988 | 160 | } |
uci1 | 0:664899e0b988 | 161 | |
uci1 | 0:664899e0b988 | 162 | bool SnConfigFrame::WriteToFile(const char* cfile) const { |
uci1 | 0:664899e0b988 | 163 | // intended only for writing default config file |
uci1 | 0:664899e0b988 | 164 | |
uci1 | 0:664899e0b988 | 165 | bool ret = false; |
uci1 | 0:664899e0b988 | 166 | FILE* cf = fopen(cfile,"wb"); |
uci1 | 0:664899e0b988 | 167 | if (cf!=0) { |
uci1 | 0:664899e0b988 | 168 | WriteTo(cf); |
uci1 | 0:664899e0b988 | 169 | ret = (ferror(cf)==0); |
uci1 | 0:664899e0b988 | 170 | fclose(cf); |
uci1 | 0:664899e0b988 | 171 | } |
uci1 | 0:664899e0b988 | 172 | return ret; |
uci1 | 0:664899e0b988 | 173 | } |