Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

SnConfigFrame.cpp

Committer:
uci1
Date:
2012-06-30
Revision:
0:664899e0b988
Child:
1:e392595b4b76

File content as of revision 0:664899e0b988:

#include "SnConfigFrame.h"

#include "mbed.h"

#include "SnBitUtils.h"

extern "C" void mbed_mac_address(char *);


const int8_t   SnConfigFrame::kIOVers               = 1;
const char*    SnConfigFrame::kDefConfFile          = "/local/defaultConfig.dat";
const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr  = 14400;    // exclusive min low power comm win period (s)
const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr  = 259200;   // exclusive max low power comm win period (s)
const uint32_t SnConfigFrame::kMinCommWinDurLowPwr  = 300;      // exclusive min low power comm win duration (s)
const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr  = 3600;     // exclusive max low power comm win duration (s)
const uint8_t  SnConfigFrame::kConfLblLen;

uint64_t       SnConfigFrame::fgMacAdr              = 0;

void SnConfigFrame::SetMacAddress() {
    static const uint8_t b64 = sizeof(uint64_t);
    static char c[b64];
    mbed_mac_address(&(c[0]));
    // like a big endian union
    fgMacAdr = 0;
    const char* a = c+(b64-1);
    for (uint8_t i=0; i<b64; i++, a--) {
        fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3);
    }
}

void SnConfigFrame::SetHardDefaults() {
    sprintf(fLabel,"HardDefaults");
    fConfTime           = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT
    fRun                = 0;
    fFirstEvt           = 0;
    fStreamHiLoPlas     = 1;
    fWvLoseLSB          = 0;
    fWvLoseMSB          = 0;
    fWvBaseline         = 0;
    fDatPackType        = ~0;
    uint16_t* dc = &(fDAC[0][0]);
    for (uint16_t i=0; i<kTotDacs; i++, dc++) {
        *dc             = 3072u;
    }
    fNumPlas            = 1;
    uint16_t* pl = &(fPLA[0]);
    for (uint8_t j=0; j<kNplas; j++, pl++) {
        *pl             = 0x7FFFu;
    }
    fNumCardsMajLog     = 2;
    fEnableThermTrig    = 1;
    fForceTrigPeriod    = 2u;//67u;
    fHeartBeatPeriod    = 0;
    fAmpsOn             = 0x0Fu;
    fEvtThrtlPeriodMs   = 50u;
    fPowerMode          = 0;
    fBatVoltLowPwr      = 0;
    fCommWinPeriod      = 3300u;
    fCommWinDuration    = 300u;
    fCommSendData       = 0;
    fCommWinPrdLowPwr   = 86100u;
    fCommWinDurLowPwr   = 300u;
    fWatchDogPeriod     = kWDFailsafe;
}

void SnConfigFrame::GetPackParsFor(const EDatPackBit d,
                                   uint8_t& loseLSB, uint8_t& loseMSB,
                                   uint16_t& wvBase) const {
    const bool pack = IsDatPackedFor(d);
    loseLSB = pack ? GetWvLoseLSB()  : 0u;
    loseMSB = pack ? GetWvLoseMSB()  : 0u;
    wvBase  = pack ? GetWvBaseline() : 0u;
}

void SnConfigFrame::GetHiLoPlas(const uint16_t pla,
                                uint16_t& hiPla,
                                uint16_t& loPla,
                                const bool r2l) {
   // split the PLA bitword into 2: one for the high threshold
   // and one for the low threshold. "lows" in the string will become
   // "highs" in the low threshold PLA.
   // 
   // example 1)
   // PLA string = HLHL....
   // hi thresh  = H.H.....
   // lo thresh  = .H.H....
   //
   // example 2)
   // PLA string = HBL.....
   // hi thresh  = HL......
   // lo thresh  = .LH.....
   //
   // (with . = A here, to make the example more readable)
   //
   // A = 11, B = 00
   // H = 01 or 10, alternating
   // L = 10 or 01, alternating
   // 01 at leftmost bits is H
   // for example:
   // 0x7FFF = 01 11 11 11 11 11 11 11
   // => HAAAAAAA for LEFT TO RIGHT
   // => AAAAAAAH for RIGHT TO LEFT
   // 0x56FF = 01 01 01 10 11 11 11 11
   // => HLHHAAAA for LEFT TO RIGHT
   // => AAAAHHLH for RIGHT TO LEFT
   //
   // so HHHHHHHH is
   // 01 10 01 10 01 10 01 10 always (r2l or l2r)
   //
   // r2l = whether to read bits right to left (true) or not (false)
   // Mahshid liked right to left
   // Liang liked left to right
   // so we allow for either
   
   const int8_t start = (r2l) ?             0 : BITS_IN_SHORT-2;
   const int8_t end   = (r2l) ? BITS_IN_SHORT : -2;
   const int8_t step  = (r2l) ?                        2 : -2;
   
   uint8_t hi= (r2l) ? 0x2 : 0x1;
   uint8_t lo= (r2l) ? 0x1 : 0x2;
   
   // set all bits to 0
   hiPla = 0;
   loPla = 0;
   
   for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) {
      const uint8_t b = (pla & (0x3<<i)) >> i;
      if (b==hi) {
         hiPla |=  hi << i;
         loPla |= 0x3 << i;
      } else if (b==lo) {
         hiPla |= 0x3 << i;
         loPla |=  hi << i;
      } else if (b==0x3) {
         // any
         hiPla |= 0x3 << i;
         loPla |= 0x3 << i;
      } else {
         // no check that b is something else.. should be impossible.
         // between
         hiPla |=  lo << i;
         loPla |=  lo << i;
      }
   }
   
}

bool SnConfigFrame::ReadFromFile(const char* cfile) {
    // intended only for reading default config file
    
    bool ret = false;
    FILE* cf = fopen(cfile,"rb");
    if (cf!=0) {
        ReadFrom(cf);
        ret = (ferror(cf)==0);
        fclose(cf);
    }
    return ret;
}

bool SnConfigFrame::WriteToFile(const char* cfile) const {
    // intended only for writing default config file
    
    bool ret = false;
    FILE* cf = fopen(cfile,"wb");
    if (cf!=0) {
        WriteTo(cf);
        ret = (ferror(cf)==0);
        fclose(cf);
    }
    return ret;
}