Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

main.cpp

Committer:
uci1
Date:
2013-11-28
Revision:
50:163ec8d88aa9
Parent:
49:a2fb3ce86a5c
Child:
51:b2bc37d660c0

File content as of revision 50:163ec8d88aa9:

#include "mbed.h"
// start a watchdog as soon as possible
#include "Watchdog.h"
Watchdog::SnKickStarter gKickStarter(WDFAILSAFE);

#define DISABLE_CONFIG_SAFETYNETS

//#define ENABLE_AFAR_COMM
//#define ENABLE_SBD_COMM
#define ENABLE_USB_COMM
//#define ENABLE_AFAR_TWITTER

//#define USE_RTOS // change in CommConstants and EthernetPower also
//#define USE_ETH_INTERFACE
//#define EVT_TIME_PROFILE
//#define DEBUG
//#define SSNOTIFY
#define USE_MODSERIAL

#include <stdint.h>
#include "SnConstants.h"

#include "SDFileSystem.h"
#ifdef USE_MODSERIAL
#define MODSERIAL_RX_BUF_SIZE 512
#define MODSERIAL_TX_BUF_SIZE 512 
#include "MODSERIAL.h"
#endif
#include "FATDirHandle.h"
#include "EthernetPowerControl.h"
#include "SnBitUtils.h"
#include "SnSDUtils.h"
#include "SnConfigFrame.h"
#include "SnEventFrame.h"
#include "SnStatusFrame.h"
#include "SnHeaderFrame.h"
#include "SnHeartbeatFrame.h"
#include "SnClockSetFrame.h"
#include "SnSignalStrengthFrame.h"
#include "SnCommWin.h"
#ifdef ENABLE_AFAR_COMM
#ifdef USE_ETH_INTERFACE
#include "SnCommAfarTCP.h"
#else
#include "SnCommWinAfar.h"
#ifdef ENABLE_AFAR_TWITTER
#include "SnCommWinTwitter.h"
#endif // ENABLE_AFAR_TWITTER
#endif // USE_ETH_INTERFACE
#endif // ENABLE_AFAR_COMM
#ifdef ENABLE_USB_COMM
#include "SnCommWinUsb.h"
#endif
#ifdef ENABLE_SBD_COMM
#include "SnCommWinSBD.h"
#endif
//#include "SnBase64.h"
#ifdef USE_RTOS_TIMER
#include "RtosTimer.h"
#endif

extern "C" void mbed_reset();

//
// MBED PINS (ordered by number)
//
// leds (for debugging)
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
// Set up power pins - Note that it's Zero for "on"
DigitalOut PIN_turn_on_system(p17); // this turns on system
DigitalOut PIN_turn_on_amps(p25);
// SD card select
DigitalOut PIN_SD_CS(p8);
// Activate/select chip by falling edge
DigitalOut PIN_ADC_CS( p9 );
// clock signal to activate PLA setting
DigitalOut PIN_PLA_cs(p10);
// To force a trigger
DigitalOut PIN_forceTrigger(p11);  //modification
// To suppress thermal triggers
DigitalOut PIN_enableThermTrig(p12);
// Restart clock on all FPGAs.
DigitalOut PIN_DoNotRestartAllClocks( p13 );
// This tells the DFPGAs to store the data on motherboard FPGA and
// read it out.
DigitalIn PIN_a_sf_clk( p14 );
DigitalIn PIN_rst_a_sf(p15);
// afar power
DigitalOut PIN_afar_power(p16);
// batter voltage/current measurement
AnalogIn PIN_vADC1(p19); 
AnalogIn PIN_vADC2(p18); 
// Lock daughter card registeres (during data readout).
DigitalOut PIN_lockRegisters( p20 );
// iridium (SBD) power
DigitalOut PIN_iridSbd_power(p21);
// Majority logic pins
DigitalOut PIN_MajLogHiBit(p22);
DigitalOut PIN_MajLogLoBit(p23);
// Tell FPGA to be ready to accept DAC values
DigitalOut PIN_start_fpga(p26); 
// Two bits to the select the daughter card for readout
DigitalOut PIN_selCardHiBit( p29 );
DigitalOut PIN_selCardLoBit( p30 );
// To launch a heartbeat pulse
DigitalOut PIN_heartbeat(p24);
// Setup SPI pins
SPI PIN_spi( p5, p6, p7 );
// The SD card


// we have to do this shit because Serial and MODSERIAL don't have virtual functions
// so calling, e.g. readable from a Serial* will call Serial::readable instead of MODSERIAL::readable
// and the comms will wait forever
#ifdef USE_MODSERIAL
#define MAIN_SERIALTYPE AjK::MODSERIAL
#else
#define MAIN_SERIALTYPE Serial
#endif

// this needs to be first in case some other global uses a print statement
static MAIN_SERIALTYPE  gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output

static MAIN_SERIALTYPE  gSBDport(p28, p27, 
#ifdef USE_MODSERIAL
                                 MODSERIAL_TX_BUF_SIZE, MODSERIAL_RX_BUF_SIZE,
#endif
                                 "sbd");

static SDFileSystem     sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); // no leading '/'
static LocalFileSystem  local((SnCommWin::kLocalDir)+1); // no leading '/'

//
// fwd declare fcns
//
void                      ReadAllRegisters();
void                      ReadRegister(const uint8_t chan, int16_t* dev);
void                      SaveHeartbeat();
void                      SaveEvent(const int32_t etms);
void                      WaitTrigAndSendClock();
void                      SetConfigAndMakeOutputFile();
SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig=false,
                                      const bool isStartupWin=false);
void                      MakeOutputFile(const bool stopRunning=false);
void                      SetPower(const bool isCommWin);
void                      procForceTrigger();
void                      procHeartbeat();
void                      procPowerCheck();
void                      procCommWin();
#ifdef USE_RTOS
void                      procForceTrigger(void const *) { return procForceTrigger(); }
void                      procHeartbeat(void const *) { return procHeartbeat(); }
void                      procPowerCheck(void const *) { return procPowerCheck(); }
void                      procCommWin(void const *) { return procCommWin(); }
#endif

//
// globals
//
// readout objs
// TODO: use RtosTimer instead of Ticker?
#ifdef USE_RTOS
static rtos::RtosTimer*     gForceTicker;
static rtos::RtosTimer*     gHeartbeatTicker;
static rtos::RtosTimer*     gCommWinTicker;
static rtos::RtosTimer*     gPowerCheckTicker;
#else
static Ticker         gForceTicker;
static Ticker         gHeartbeatTicker;
static Ticker         gCommWinTicker;
static Ticker         gPowerCheckTicker;
#endif
static Timer          gAllTrgTimer;
static Timer          gThmTrgTimer;
static Timer          gAdcToMBtimer;
static SnClockSetFrame       gClkSet;
static SnSignalStrengthFrame gSigStr;
#ifdef DISABLE_CONFIG_SAFETYNETS
static SnConfigFrame  gConf(false);
#else
static SnConfigFrame  gConf;
#endif
static SnEventFrame   gEvent;
static SnEventFrame   gLastEvent;
static SnPowerFrame   gPower;
// parameters
static bool           gCardsPowered     = false;
static bool           gFirstEvt         = true;
static volatile bool  gReadingOut       = false;
static volatile bool  gCommWinOpen      = false; // if it's open
static volatile bool  gOpenCommWin      = false; // if it should be opened
static volatile bool  gCheckPower       = false; // if it should be checked
static uint32_t       gPowNum           = 0;
static uint32_t       gEvtNum           = 0;   // num of evt written
static uint32_t       gTrgNum[kNumTrgs] = {0}; // num of this type of trg received
// i/o
static time_t         gLastCommWin      = 0; // time
static uint32_t       gCommWinChecks    = 0;
static uint32_t       gNcommWinChecks   = 0;
static uint16_t       gConsecCommFails  = 0;
// heartbeat
static time_t         gLastHrtbt        = 0;
static volatile bool  gHrtbtFired       = false;
static uint32_t       gHrtbtNum         = 0;
// rates
static double         gThmDtSum         = 0; // sum of all time diffs between thermal trigs
static double         gEvtDtSum         = 0; // sum of all time diffs between events
static uint32_t       gThmNumDt         = 0; // number of thermal trig time diffs added up
static uint32_t       gEvtNumDt         = 0; // number of event time diffs added up
// this should be bigger than anything that will actually be used
static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + (2u*SnHeaderFrame::kMaxSizeOf) + SnPowerFrame::kMaxSizeOf
                                + SnEventFrame::kMaxSizeOf; // (this is redundant and could be removed if mem is sparse)
//static const uint32_t gB64Bsize=BASE64ENC_LEN(gBufSize)+1;
//static char           gB64Buf[gB64Bsize];
static char           gGenBuf[gBufSize]; // must be big enough for event or status or config!
static SnCommWin*     gComms[kNcomms]   = { 0 }; // order => priority. afar uses RTOS, and must be made inside main
#if defined(ENABLE_AFAR_TWITTER) && defined(ENABLE_AFAR_COMM)
static SnCommAfarNetIfTwitter* gTwit     = 0;
#endif

void procForceTrigger() {
    if (gReadingOut==false && gCommWinOpen==false) {
        led3=!led3;
#ifdef DEBUG
        printf("proc force\r\n");
        printf("PIN_forceTrigge=%d, PIN_turn_on_system=%d, "
            "PIN_a_sf_clk=%d\r\n",
            PIN_forceTrigger.read(), PIN_turn_on_system.read(),
            PIN_a_sf_clk.read());
#endif
        gEvent.SetTrgBit(kFrcTrg);
        gEvent.SetTrgNum(++(gTrgNum[kFrcTrg]));
        //PIN_forceTrigger = 0;
        PIN_forceTrigger = 1;     // force a trigger
        PIN_forceTrigger = 0;
    }
}

void procHeartbeat() {
    if (gReadingOut==false && gCommWinOpen==false) {
#ifdef DEBUG
        printf("proc heartbeat\r\n");
#endif
        //PIN_heartbeat = 0;
        PIN_heartbeat = 1; // heartbeat pulse
        PIN_heartbeat = 0;
        gLastHrtbt    = time(0);
        gHrtbtFired   = true;
        ++gHrtbtNum;
    }
}

void procPowerCheck() {
#ifdef DEBUG
    printf("proc power\r\n");
#endif
    gCheckPower=true;
}

void procCommWin() {
    ++gCommWinChecks;
    //if ( (time(0) - gLastCommWin) > gConf.GetCommWinPeriod() ) {
#ifdef DEBUG
    printf("<><><><><><> gCommWinChecks=%u, gNcommWinChecks=%u\r\n",
        gCommWinChecks, gNcommWinChecks);
#endif
    if ( gCommWinChecks >= gNcommWinChecks ) {
#ifdef DEBUG
        printf("proc comm win.\r\n"); 
#endif
        led3=!led3;
        gOpenCommWin = true;
    }
}

bool AreCardsPowered(const bool checkPin) {
#ifdef DEBUG
    printf("acp: PIN_turn_on_system=%d, gCardsPowered=%d\r\n",
        PIN_turn_on_system.read(), gCardsPowered);
#endif
    if (checkPin) {
        gCardsPowered = (PIN_turn_on_system.read()==0);
    }
    return gCardsPowered;
}

void GetAvePowerReading() {
    // use one measurement as the assumed average
    // in order to reduce computational errors
    int32_t v1, v2;
    const uint16_t aaveV1 = PIN_vADC1.read_u16();
    const uint16_t aaveV2 = PIN_vADC2.read_u16();
    float n=0, ave1=0, ave2=0, rms1=0, rms2=0;
    for (uint16_t i=0; i<kNvoltsAve; i++) {
        v1    = PIN_vADC1.read_u16() - aaveV1;
        v2    = PIN_vADC2.read_u16() - aaveV2;
        n    += 1;
        ave1 += v1;
        rms1 += v1*v1;
        ave2 += v2;
        rms2 += v2*v2;
    }
    rms1 -= (ave1*ave1)/n;
    rms2 -= (ave2*ave2)/n;
    rms1 /= n-1;
    rms2 /= n-1;
    rms1  = sqrt(rms1);
    rms2  = sqrt(rms2);
    ave1 /= n;
    ave2 /= n;
    ave1 += aaveV1;
    ave2 += aaveV2;
    gPower.Set(ave1, ave2, rms1, rms2, time(0));
}

void CheckPower(const bool isCommWin) {
#ifdef DEBUG
    printf("CheckPower\r\n");
#endif
    // read power
    GetAvePowerReading();
    // save to disk
    FILE* cf = SnSDUtils::GetCurFile();
    if (cf!=0) {
        PIN_lockRegisters = 0; // unlock so we can talk to SD card.
#ifdef DEBUG
        printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
            gPower.GetAveV1(), gPower.GetAveV2(), 
            gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
            gPowNum);
#endif
        SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
    }
    // do we need to change modes?
    bool changed = false;
    if (gConf.IsLowPowerMode()) {
        if (gPower.GetAveV1() > gConf.GetBatVoltFromLowPwr()) {
#ifdef DEBUG
            printf("chaing to normal power!\r\n");
#endif
            gConf.ChangeToNormPower();
            changed = true;
        }
    } else {
        if (gPower.GetAveV1() < gConf.GetBatVoltToLowPwr()) {
#ifdef DEBUG
            printf("chaing to low power!\r\n");
#endif
            gConf.ChangeToLowPower();
            changed = true;
        }
    }
    if (changed) {
        SetPower(isCommWin); // TODO: This will fail if isCommWin==false? (currently not possible, but..)
#ifdef DEBUG
        printf("Using config %s\r\n",gConf.GetLabel());
#endif
        SetConfigAndMakeOutputFile(); // setup defaults in case no communication
    }
    // checking done
    gCheckPower = false;
}

void ResetCountersClearEvt() {
    const uint32_t evtStartCurSeq = (SnSDUtils::GetCurSeqNum()) // no -1; start with seq=0
                                     * gConf.GetEvtsPerFile();
    gEvent.ClearEvent();
    gEvtNum = evtStartCurSeq;
    gPowNum = evtStartCurSeq;
    memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
    // reset rate counters
    gThmDtSum = 0;
    gThmNumDt = 0;
    gEvtDtSum = 0;
    gEvtNumDt = 0;
    // reset heartbeat counters
    gLastHrtbt  = 0;
    gHrtbtFired = false;
    gHrtbtNum   = 0;
#ifdef DEBUG
    printf("Reset: gEvtNum=%u, gPowNum=%u, evtStartCS=%u\r\n",
        gEvtNum, gPowNum, evtStartCurSeq);
#endif
}

void GetRates(float& thmrate, float& evtrate) {
    thmrate = evtrate = 0;
#ifdef DEBUG
    printf("** Getting rates: gThmNumDt=%d, gThmDtSum=%g, "
           "gEvtNumDt=%d, gEvtDtSum=%g\r\n",
           gThmNumDt, gThmDtSum, gEvtNumDt, gEvtDtSum);
#endif
    
    thmrate = (gThmDtSum>0.0 && gThmNumDt>1) ? 
        static_cast<float>(gThmNumDt) / (gThmDtSum/1e3) : 0;
    evtrate = (gEvtDtSum>0.0 && gEvtNumDt>1) ? 
        static_cast<float>(gEvtNumDt) / (gEvtDtSum/1e3) :0;
}

void AddToRate(const float dt, const bool isThm) {
    if (isThm) {
        gThmDtSum += dt;
        gThmNumDt += 1u;
    } else {
        gEvtDtSum += dt;
        gEvtNumDt += 1u;
    }
#ifdef DEBUG
    printf("** AddToRate: dt=%g, isThm=%d\r\n",dt,(int)isThm);
    printf("** AddToRate: gThmNumDt=%d, gThmDtSum=%g, "
           "gEvtNumDt=%d, gEvtDtSum=%g\r\n",
           gThmNumDt, gThmDtSum, gEvtNumDt, gEvtDtSum);
#endif
}

bool IsSeqComplete() {
#ifdef DEBUG
    printf("IsSeqComplete: eps=%u, cntpow=%d, pow=%u, evt=%u, seq=%hu\r\n",
        gConf.GetEvtsPerFile(), (int)gConf.IsCountingPowerReadings(),
        gPowNum, gEvtNum, SnSDUtils::GetCurSeqNum());
#endif
    if (gConf.GetEvtsPerFile()>0) {
        const uint32_t evtEndCurSeq = (SnSDUtils::GetCurSeqNum()+1) // account for seq=0
                                        * gConf.GetEvtsPerFile();
#ifdef DEBUG
        printf("evtEndCurSeq=%u\r\n",evtEndCurSeq);
#endif
        if (gConf.IsCountingPowerReadings()) {
            return (gPowNum>=evtEndCurSeq);
        } else {
            // first event num is a one-time per run offset, not one per sequence
            return (gEvtNum>=evtEndCurSeq);
        }
    } else {
        return false;
    }
}

#ifdef USE_RTOS
void stopTicker(rtos::RtosTimer* tik) {
    if (tik!=0) {
        tik->stop();
    }
}
#else
void stopTicker(Ticker& tik) {
    tik.detach();
}
#endif

#ifdef USE_RTOS
float resetTicker(rtos::RtosTimer* tik, const float timSec,
                  const float maxTimSec) {
    if (tik!=0) {
        tik->stop();
        if (timSec>0) {
            float tp = timSec > maxTimSec ? maxTimSec : timSec;
            tp *= 1000u; // ms
            tik->start(tp);
            return tp;
        }
     }
     return 0;
}
#else
float resetTicker(Ticker& tik, const float timSec,
                  const float maxTimSec, void (*fptr)(void)) {
    tik.detach();
    if (timSec>0) {
        const float tp = timSec > maxTimSec ? maxTimSec : timSec;
        tik.attach(fptr, tp);
        return tp;
    }
    return 0;
}
#endif

void StopAllTickers() {
    stopTicker(gForceTicker);
    stopTicker(gHeartbeatTicker);
    stopTicker(gCommWinTicker);
    stopTicker(gPowerCheckTicker);
}

void ResetAllTickers() {
#ifdef USE_RTOS
    const float ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
                                  kAbsMaxTimer);
    Thread::wait(131); // make it less likely for multiple triggers to fire too close together
    const float hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
                                  kAbsMaxTimer);
    Thread::wait(173); // make it less likely for multiple triggers to fire too close together
    const float cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
                                  kCommWinLongPrdTk);
    Thread::wait(169); // make it less likely for multiple triggers to fire too close together
    const float pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
                                  kAbsMaxTimer);
#else
    const float ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
                                  kAbsMaxTimer, &procForceTrigger);
    wait_ms(131); // make it less likely for multiple triggers to fire too close together
    const float hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
                                  kAbsMaxTimer, &procHeartbeat);
    wait_ms(173); // make it less likely for multiple triggers to fire too close together
    const float cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
                                  kCommWinLongPrdTk, &procCommWin);
    wait_ms(169); // make it less likely for multiple triggers to fire too close together
    const float pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
                                  kAbsMaxTimer, &procPowerCheck);
#endif
#ifdef DEBUG
    printf("attach force trig %g\r\n",ftp);
    printf("attach heart beat %g\r\n",hbp);
    printf("attach comm win   %g\r\n",cwp);
    printf("attach power chk  %g\r\n",pcp);
#endif
}

void StopRunning() {
#if defined(DEBUG) || defined(SSNOTIFY)
    printf("stop running\r\n");
#endif
    StopAllTickers();
    OpenCommWin();
    while (true) {
        led3 = 1; led4=1;
#ifdef USE_RTOS
        Thread::wait(500);
#else
        wait(0.5);
#endif
        led3 = 0; led4=0;
#ifdef USE_RTOS
        Thread::wait(500);
#else
        wait(0.5);
#endif
        // don't kick the watchdog
        // if we do, the station is unrecoverable without physical access
    }
}


void InitSDCard() {
#ifdef DEBUG
    printf("initializing SD card..\r\n");
#endif
    // initialize the SD card. this should prevent the issue with
    // seq 0 being overwritten upon power up or the SD card first
    // being insterted
    sd.disk_initialize();
}

int main() {
    // a failsafe
    Watchdog::kick(WDFAILSAFE);

    {
        gCpu.baud(CPUBAUD_SN);
#if defined(SSNOTIFY) || defined(DEBUG)
        printf("\n\n\n\n\nmain: start\r\n");
#endif
#ifdef USE_RTOS
        led1=1; Thread::wait(200);
        led1=0; led2=1; Thread::wait(200);
        led2=0; led3=1; Thread::wait(200);
        led3=0; led4=1; Thread::wait(200);
#else
        led1=1; wait(0.2);
        led1=0; led2=1; wait(0.2);
        led2=0; led3=1; wait(0.2);
        led3=0; led4=1; wait(0.2);
#endif
        led4=0;
    }

    SnSDUtils::fgDoInit = &InitSDCard;
        
#ifdef DEBUG
    printf("making comm objects\r\n");
#endif

    uint8_t comi(0);
#ifdef ENABLE_AFAR_COMM
    // RTOS stuff must be made inside main for some reason    
#ifdef USE_ETH_INTERFACE
#ifdef DEBUG
    printf("making SnCommAfarTCP\r\n");
#endif
    gComms[comi++] = new SnCommAfarTCP(gConf);
#else
#ifdef DEBUG
    printf("making SnCommWinAfar\r\n");
#endif
    //gComms[comi++] = new SnCommAfarNetIf(gConf);
    gComms[comi++] = new SnCommWinAfar(gConf);
#ifdef ENABLE_AFAR_TWITTER
#ifdef DEBUG
    printf("making SnCommAfarNetIfTwitter\r\n");
#endif
    gTwit = new SnCommAfarNetIfTwitter(gConf);
#endif
#endif
#endif
#ifdef ENABLE_SBD_COMM
#ifdef DEBUG
    printf("making SnCommWinSBD\r\n");
#endif
    gComms[comi++] = new SnCommWinSBD(&gSBDport);
#endif
#ifdef ENABLE_USB_COMM
#ifdef DEBUG
    printf("makin SnCommWinUsb\r\n");
#endif
    gComms[comi++] = new SnCommWinUsb(&gCpu);
#endif

#ifdef DEBUG
    printf("made comm objects\r\n");
#ifdef USE_MODSERIAL
    printf("using MODSERIAL\r\n");
#endif
#endif
    
    if (comi!=kNcomms) {
        error("comi=[%hhu] != kNcomms=[%hhu]\r\n",
            comi, kNcomms);
        // will die here with blue lights of death
    }
    
#ifdef USE_RTOS
    gForceTicker        = new rtos::RtosTimer(&procForceTrigger);
    gHeartbeatTicker    = new rtos::RtosTimer(&procHeartbeat);
    gCommWinTicker      = new rtos::RtosTimer(&procCommWin);
    gPowerCheckTicker   = new rtos::RtosTimer(&procPowerCheck);
#endif    
    
    led2=1;
            
    // set the clock to the BS time, if it's not set
    if ( (static_cast<int32_t>(time(0)))<0 ) {
        set_time(kBStime);
    }
#ifdef DEBUG
    printf("time = %d\r\n",(int32_t)time(0));
#endif
    gLastCommWin = time(0); // prevent comm win proc
    
#ifdef USE_RTOS
    gForceTicker->stop();
#else
    gForceTicker.detach();
#endif
    gFirstEvt = true;
    
    // (probably) power down comms and power up cards,amps
    SetPower(false);

    //
    // get config
    //
#ifdef DEBUG
    printf("call OpenCommWin\r\n");
#endif
    OpenCommWin(true, true); // alwasy configure, even if no new config
    
    // get ready to trigger
    PIN_spi.format( 16, 1 ); // change to data readout format
    PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz

    led2=0;
    
    // read and cache the dCard power pin setting,
    // so we can use the cached value later
    AreCardsPowered(true); // TODO: should this be an if?
    register int32_t etms=0; // time between written events

    // the main event loop. wait for triggers in SendClock    
    while ( true ) {
        // in here, we wait for triggers from the MB-FPGA
        Watchdog::kick(); // don't reset!
        
        led1 = !led1;
        
#ifdef DEBUG
        printf("calling wait trig\r\n");
        printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
        printf("readingout=%d\r\n",(int)gReadingOut);
#endif
        if (gFirstEvt) {
#ifdef DEBUG
            printf("WriteTrigWaitWinTime (start)\r\n");
#endif
            SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                            gClkSet,
                                            true);
            gThmTrgTimer.reset(); gThmTrgTimer.start();
            gAllTrgTimer.reset(); gAllTrgTimer.start();
        }
        PIN_lockRegisters = 0; // allow data to come from DFPGA
        WaitTrigAndSendClock();  // wiat for trigger and move data to MB. this returns immediately if cards are powered off
        PIN_lockRegisters = 1; // block registers during readout
        
#ifdef EVT_TIME_PROFILE
        Timer prof;
        prof.start();
        int befReadWv=0, aftReadWv=0, befSaveEvt=0, aftSaveEvt=0,
            befChkPow=0, aftChkPow=0, befNewSeq=0, aftNewSeq=0, endOfLoop=0;
#endif

        if (gReadingOut) {
            
            const int32_t ttms = gThmTrgTimer.read_ms(); // for rate calculation
            const int32_t atms = gAllTrgTimer.read_ms(); // for throttle
            if (gEvent.IsForcedTrg()==false) {
                // don't reset if not a thermal trigger
                gThmTrgTimer.reset(); gThmTrgTimer.start();
            }
            gAllTrgTimer.reset(); gAllTrgTimer.start();     // restart trigger timer
            etms += atms; // time between events
            
            Watchdog::kick(); // don't reset!
                        
            //
            // got trigger. read registers to mbed and build the event
            //
            
            led4=1;

            // TODO: no way to check for external trigger?
            if (gEvent.IsForcedTrg()==false) {
                gEvent.SetTrgBit(kThmTrg);
                gEvent.SetTrgNum(++(gTrgNum[kThmTrg]));
                AddToRate(ttms, true);
            } // else already set by procForceTrigger
            
            if ( gEvent.IsForcedTrg() || gFirstEvt ||
                (etms>gConf.GetEvtThrtlPeriodMs()) ) {

                // read data & calc CRC
#ifdef EVT_TIME_PROFILE
                prof.stop(); befReadWv=prof.read_us(); prof.start();
#endif

                // get the data to the MBED
                gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit);
            
#ifdef EVT_TIME_PROFILE
                prof.stop(); aftReadWv=prof.read_us(); prof.start();
#endif

                gEvent.SetCurMbedTime();
                
                Watchdog::kick(); // don't reset!
        
#ifdef DEBUG
                printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
#endif
                
                led2=1;
                /*
                gRecentCountTime = static_cast<uint32_t>(time(0));
                gRecentEvtNum    = gEvtNum;
                // do start time second so that if we get only one event,
                // the delta(t) will be less than 0 and the rates not calculated
                if (gDoResetLastCount) {
                    gLastCountReset = static_cast<uint32_t>(time(0)); // to calc rates
                    gLastEventReset = gEvtNum;
                    gDoResetLastCount = false;
                }
                */
                
                PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card.
                
#ifdef EVT_TIME_PROFILE
                prof.stop(); befSaveEvt=prof.read_us(); prof.start();
#endif

                SaveEvent(etms);
                AddToRate(etms, false);
                etms=0;
                
#ifdef EVT_TIME_PROFILE
                prof.stop(); aftSaveEvt=prof.read_us(); prof.start();
#endif
            }
        }
#ifdef DEBUG
        printf("past reading out\r\n");
#endif
        
        if (gHrtbtFired) {
            SaveHeartbeat();
            gHrtbtFired=false;
        }

        led4=0; led2=0;
                
#ifdef EVT_TIME_PROFILE
        prof.stop(); befChkPow=prof.read_us(); prof.start();
#endif
        // check the power?
        if (gCheckPower) {
#ifdef DEBUG
            printf("call check power\r\n");
#endif
            CheckPower(false);
        }
#ifdef EVT_TIME_PROFILE
        prof.stop(); aftChkPow=prof.read_us(); prof.start();
#endif
        
        // open comm win?
        if (gOpenCommWin) {
#ifdef DEBUG
            printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false");
            printf("WriteTrigWaitWinTime (stop)\r\n");
#endif
            SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                            gClkSet,
                                            false);
            OpenCommWin();
            gOpenCommWin=false;
            gFirstEvt = true;
            gAllTrgTimer.reset();
            gThmTrgTimer.reset();
            etms=0;
        } else {
#ifdef DEBUG
            printf("gOpenCommWin=false, gCommWinChecks=%u, gNcommWinChecks=%u\r\n",
                gCommWinChecks, gNcommWinChecks);
#endif
        }
        
#ifdef EVT_TIME_PROFILE
        prof.stop(); befNewSeq=prof.read_us(); prof.start();
#endif
        // make new seq?
        if (IsSeqComplete()) {
#ifdef DEBUG
            printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
            printf("WriteTrigWaitWinTime (stop)\r\n");
#endif
            SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                            gClkSet,
                                            false);
            MakeOutputFile(gConf.IsSingleSeqRunMode());
            gFirstEvt = true;
            gThmTrgTimer.reset();
            gAllTrgTimer.reset();
            etms=0;
        }
#ifdef EVT_TIME_PROFILE
        prof.stop(); aftNewSeq=prof.read_us(); prof.start();
#endif

#ifdef EVT_TIME_PROFILE
        prof.stop(); endOfLoop=prof.read_us(); prof.start();
        printf("befReadWv=%d, aftReadWv=%d, befSaveEvt=%d, aftSaveEvt=%d, "
            "befChkPow=%d, aftChkPow=%d, befNewSeq=%d, aftNewSeq=%d, endOfLoop=%d\r\n",
            befReadWv, aftReadWv, befSaveEvt, aftSaveEvt,
            befChkPow, aftChkPow, befNewSeq, aftNewSeq, endOfLoop);
#endif
        
        /*
        // get ready to trigger
        PIN_spi.format( 16, 1 ); // change to data readout format
        PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
        */
        
        // reset event
        // clear after comm win, so full event can be sent with status
        // but don't clear counters or trigger bits, as
        gEvent.ClearEvent(true);
    
    } // end while (true)

}

//
// save a heartbeat tag
//
void SaveHeartbeat() {
    if (gHrtbtNum>0) {
#ifdef DEBUG
        printf("save heartbeat #%u, time %u\r\n",
            gHrtbtNum-1, gLastHrtbt);
#endif
        // save to SD
        PIN_lockRegisters = 0; // unlock so we can talk to SD card.
        SnSDUtils::WriteHeartbeatTo(SnSDUtils::GetCurFile(),
            gLastHrtbt, gHrtbtNum-1); // -1 so it counts from 0
    }
}

//
// save the event
//
void SaveEvent(const int32_t etms) {
    // write the event
    
#ifdef DEBUG
    printf("save event\r\n");
#endif

    // set the event number & dt
    gEvent.SetEvtNum(gEvtNum);
    gEvent.SetDTms(etms);
    
    // save to SD
    PIN_lockRegisters = 0; // unlock so we can talk to SD card.
    SnSDUtils::WriteEventTo(SnSDUtils::GetCurFile(), gGenBuf, gEvent, gConf);
    
    // make a copy in case we need to send it with the status
    // (copy it because we are going to clear this event while
    // waiting for the next trigger)
    gEvent.CopyTo(gLastEvent);
    
    // increment event number
    ++gEvtNum;
    
#ifdef DEBUG
    printf("gEvtNum=%u\r\n",gEvtNum);
#endif
}

void MakeOutputFile(const bool stopRunning) {
    PIN_lockRegisters = 0; // unlock so we can talk to SD card.
#ifdef DEBUG
    printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
        gEvtNum,gPowNum,(int)stopRunning);
#endif
    
    SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
    
#ifdef DEBUG
    printf("file closed\r\n");
#endif
    if (stopRunning) {
        StopRunning();
    }
    FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(),
                                            gConf.GetRun(),
                                            gConf.GetFirstSeq());
    // reset event, timers, trigger counters
    ResetCountersClearEvt();
    if (cf!=0) {
#ifdef USE_RTOS
        Thread::wait(200);
#else
        wait_ms(200);
#endif
        GetAvePowerReading();
#ifdef DEBUG
        printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
            gPower.GetAveV1(), gPower.GetAveV2(), 
            gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
            gPowNum);
#endif
        SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
    }
#ifdef DEBUG
    printf("made output file with run %u\r\n",gConf.GetRun());
    printf("filename=%s\r\n",SnSDUtils::GetCurFileName());
#endif
    SnSDUtils::WriteConfig(SnSDUtils::GetCurFile(), gConf);
#ifdef DEBUG
    printf("write config to file\r\n");
#endif
}

bool PowerDownCommPeriph(const SnConfigFrame::EDatPackBit type) {
    
    SnCommWin** cw = gComms;
    for (uint8_t i=0; i<kNcomms; i++, cw++) {
        if ((*cw)==0) {
            continue;
        } else if ((*cw)->GetCommType()==type) {
            return (*cw)->PowerDown(gConf.GetTimeoutTime(time(0),
                                    gConf.GetCommWinConnectTO()));
        }
    }
    return false;
}

//
// power stuff
//
void SetPower(const bool isCommWin) {
#ifdef DEBUG
    printf("set power. isCommWin=%s\r\n",(isCommWin)?"true":"false");
    printf("bef: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
        LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
    printf("pcenet bef power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
#endif
    
    SnConfigFrame::EPowerModeBit cardpb(SnConfigFrame::kCardDatTak),
                                 ampspb(SnConfigFrame::kAmpsDatTak),
                                 iridpb(SnConfigFrame::kIridDatTak),
                                 afarpb(SnConfigFrame::kAfarDatTak);
    if (isCommWin) {
        cardpb = SnConfigFrame::kCardComWin;
        ampspb = SnConfigFrame::kAmpsComWin;
        iridpb = SnConfigFrame::kIridComWin;
        afarpb = SnConfigFrame::kAfarComWin;
    }
    // TODO: turn on amps individually, when that's possible
    
    // change cards power
#ifdef DEBUG
    printf("setting cards pin power\r\n");
#endif
    PIN_turn_on_system = gConf.GetPowPinSetting(cardpb);
#ifdef USE_RTOS
    Thread::wait(10);
#else
    wait_ms(10);
#endif
    // change amps power
#ifdef DEBUG
    printf("setting amps pin power\r\n");
#endif
    PIN_turn_on_amps = gConf.GetPowPinSetting(ampspb);
#ifdef USE_RTOS
    Thread::wait(10);
#else
    wait_ms(10);
#endif
    // change iridium power
    // power down periph if going from on to off
    const bool iridToOn = (kIridPwrFromAfar) ?
        gConf.IsPoweredFor(afarpb) :
        gConf.IsPoweredFor(iridpb);
    const bool iridFromOn = PIN_iridSbd_power.read()==
        (kIridPwrFromAfar ? gConf.GetPowPinSetting(afarpb, true)
                          : gConf.GetPowPinSetting(iridpb, true));
    if ( iridFromOn && (iridToOn==false) ) {
#ifdef DEBUG
        printf("calling PowerDown for Iridium\r\n");
#endif
        PowerDownCommPeriph(SnConfigFrame::kIrid);
    }
#ifdef DEBUG
    printf("setting iridium pin power\r\n");
#endif
    PIN_iridSbd_power = (kIridPwrFromAfar)
        ? gConf.GetPowPinSetting(iridpb, false) // leave the iridium relay off. use afar relay.
        : gConf.GetPowPinSetting(iridpb);
#ifdef USE_RTOS
    Thread::wait(10);
#else
    wait_ms(10);
#endif
    // change ethernet PHY port power
#ifdef DEBUG
    printf("afar pin=%d, com powsetting=%d\r\n",PIN_afar_power.read(),
        gConf.GetPowPinSetting(afarpb));
#endif
    if (gConf.IsPoweredFor(afarpb) ||
        (kIridPwrFromAfar && gConf.IsPoweredFor(iridpb)) ) {
#ifdef DEBUG
        printf("PHY cowin powering up\r\n");
#endif
        PHY_PowerUp();
#ifdef USE_RTOS
        Thread::wait(1000);
#else
        wait(1);
#endif
#ifdef DEBUG
        printf("PHY cowin powered up\r\n");
#endif
    } else {
        // change afar power
        // power down periph if going from on to off
        // change afar power
        int afon = gConf.GetPowPinSetting(afarpb, true);
        if (kIridPwrFromAfar) {
            // NOTE: the following only works because for the
            // irid & afar pins, 1 = on!
            afon |= gConf.GetPowPinSetting(iridpb, true);
        }
        const bool afarFromOn = (PIN_afar_power.read()==afon);
        if (afarFromOn) {
            PowerDownCommPeriph(SnConfigFrame::kAfar);
        }
#ifdef DEBUG
        printf("PHY cowin powering down\r\n");
#endif
        PHY_PowerDown();
#ifdef USE_RTOS
        Thread::wait(1000);
#else
        wait(1);
#endif
#ifdef DEBUG
        printf("PHY cowin powered down\r\n");
#endif
    }
#ifdef DEBUG
    printf("PHY done\r\n");
#endif
#ifdef USE_RTOS
    Thread::wait(100);
#else
    wait_ms(100);
#endif
    // change afar power
    int afpp = gConf.GetPowPinSetting(afarpb);
    if (kIridPwrFromAfar) {
        // NOTE: the following only works because for the
        // irid & afar pins, 1 = on!
        afpp |= gConf.GetPowPinSetting(iridpb);
    }
    PIN_afar_power = afpp;
#ifdef USE_RTOS
    Thread::wait(1500);
#else
    wait(1.5);
#endif
#ifdef DEBUG
    printf("aft: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
        LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
#endif
#ifdef DEBUG
    printf("power word (%hhu): ",gConf.GetPowerMode()); SnBitUtils::printBits(gConf.GetPowerMode(),true);
    printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
        isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
        PIN_iridSbd_power.read(), PIN_afar_power.read());
    printf("pcenet aft power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
#endif
}

//
// set configuration
//
void SetConfigAndMakeOutputFile() {
#ifdef DEBUG
    printf("SetConfigAndMakeOutputFile\r\n");
#endif
    
    // restart watchdog
    Watchdog::kick(gConf.GetWatchdogPeriod());
    
    // block (thermal) triggers during configuration
    PIN_enableThermTrig       = 0;
    PIN_ADC_CS                = 1;
    PIN_DoNotRestartAllClocks = 1;
    PIN_forceTrigger          = 0;
    PIN_heartbeat             = 0;
#ifdef USE_RTOS
    Thread::wait(20);
#else
    wait_ms(20);
#endif
    
    gCommWinChecks  = 0;
    gNcommWinChecks = gConf.GetCommWinPeriod() / kCommWinLongPrdTk;
    
    if (AreCardsPowered(true)) {
        // Set PLA value(s)
        PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting
        PIN_spi.frequency(1000000);
        PIN_MajLogHiBit=1;
        PIN_MajLogLoBit=1;
        PIN_enableThermTrig=0;
    
        uint16_t hi, lo;
        PIN_PLA_cs=1;
#ifdef USE_RTOS
        Thread::wait(4000);
#else
        wait(4);
#endif
        for (uint8_t pi=0; pi<kNplas; pi++) {
            if (pi < gConf.GetNumPlas()) {
                SnConfigFrame::GetHiLoPlas(gConf.GetPla(pi), hi, lo);
                PIN_spi.write(hi);
                PIN_spi.write(lo);
#ifdef DEBUG
                printf("pla hi %hu, lo %hu\r\n",hi,lo);
#endif
            } else {
                PIN_spi.write(kNoTrigPla); // hi
                PIN_spi.write(kNoTrigPla); // lo
#ifdef DEBUG
                printf("pla hi %hu, lo %hu\r\n",kNoTrigPla,kNoTrigPla);
#endif
            }
            Watchdog::kick();
        }
#ifdef USE_RTOS
        Thread::wait(3000);
#else
        wait(3);
#endif
        PIN_PLA_cs=0;
#ifdef USE_RTOS
        Thread::wait(3000);
#else
        wait(3);
#endif
    
        // DAC values
        //
        // first 12 bits = DAC value
        // next 2 bits = DAC ID
        // last 2 bits = dFPGA ID
        //
        // But FPGA uses "gray encoding" which means only 1 bit
        // can change at a time (of the last 4 bits). So even tho
        // the card/dac# is encoded, the order is also important
        // 0000 (dac0,card0), 0001 (dac0,card1), 0011 (dac0,card3), 0010 (dac0,card2),
        // 0110 (dac1,card2), 0111 (dac1,card3), 0101 (dac1,card1), etc.
#ifdef DEBUG
        printf("setting dacs\r\n");
#endif
        uint16_t dv=0;
        for (uint8_t i=0, gri=0; i<kTotDacs; i++) {
            // get the gray-codes for this iteration
            gri = SnBitUtils::binToGray(i);
            
            // build bit word
            dv   = static_cast<int>(gConf.GetDac(gri & 0x0003u, gri >> 2u));
            dv <<= 4u;
            dv  |= gri;
            
#ifdef DEBUG
            printf("dac %04x\r\n",dv);
#endif
            
            // send to FPGA
            PIN_start_fpga=1;
            PIN_spi.write(dv);
            PIN_start_fpga=0;
    
            Watchdog::kick();
            
        }
#ifdef DEBUG
        printf("dacs set\r\n");
#endif
#ifdef USE_RTOS
        Thread::wait(20);
#else
        wait_ms(20);
#endif
    } else {
#ifdef DEBUG
        printf("cards off. skipping PLA and DAC setting\r\n");
#endif
    }
    
    // Majority Logic Trigger selection (# of cards)
    SnBitUtils::SetChanNumBits(gConf.GetNumCardsMajLog() - 1u,
                   PIN_MajLogHiBit, PIN_MajLogLoBit);
    
    // Enable thermal trigger?
    PIN_enableThermTrig = gConf.IsThermTrigEnabled();

    PIN_spi.format( 16, 1 ); // back to trigger mode
    PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz

    // make new output file
    // put after PLA/DAC, in case they affect the power readings
#ifdef USE_RTOS
    Thread::wait(200);
#else
    wait_ms(200);
#endif
    MakeOutputFile();
    
    // reset tickers
    ResetAllTickers();
    
    Watchdog::kick(); // don't reset!
    
#ifdef DEBUG
    printf("set config done\r\n");
#endif
}

//
// readout functions
//
void WaitTrigAndSendClock() {
    
#ifdef DEBUG
    printf("WaitTrigAndSendClock\r\n");
    printf("wait trig: (pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
        gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
        PIN_iridSbd_power.read(), PIN_afar_power.read());
    printf("cards powered=%d\r\n",(int)AreCardsPowered(true));
#endif

#ifdef DEBUG
    printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
#endif
    if (AreCardsPowered(false)) {

        PIN_spi.format( 16, 1 ); // back to trigger mode
        PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
        
        if (gFirstEvt==false) {
            PIN_DoNotRestartAllClocks    = 0;
            wait_us(1);
            PIN_DoNotRestartAllClocks    = 1;
            //led3 = !led3; // toggle send clock led
        } else {
            gFirstEvt = false;
        }
            
        //
        // wait for a trigger here.
        //
#ifdef DEBUG
        printf("starting wait for trig\r\n");
#endif

        gReadingOut = false;  // this will allow forced triggers (see procForceTrigger())
        while ( PIN_a_sf_clk == 1 ) {
            if (gOpenCommWin || gCheckPower) {
#ifdef DEBUG
                printf("break com=%d, pow=%d\r\n",gOpenCommWin,gCheckPower);
#endif
                return; // break out to open comms or check power
            }
        }
        //PIN_forceTrigger=0;   // necessary for forced triggers, harmless for other triggers
        gReadingOut = true;   // disallow new forced triggers
/*        
#ifdef DEBUG
        printf("after wait for trig. PIN_a_sf_clk=%d\r\n",
            PIN_a_sf_clk.read());
#endif
*/
        // we can't be interrupted before data arrives at the MB FPGA
        //StopAllTickers();
/*
                      procForceTrigger();
                      procHeartbeat();
                      procPowerCheck();
                      procCommWin();
*/
        
        //wait_us(5);
        
        //
        // collect data from daughter cards
        //
        // TODO: what if some card (set of channels) doesn't respond?
        // currently, will wait forever?
        // also, if ch1 is dead, will wait forever (due to FPGA code)
        gAdcToMBtimer.start();
        for( uint8_t i = 0; i < 128; ++i ) {
            while (PIN_a_sf_clk==1) {}
            while (PIN_a_sf_clk==0) {}
            /*
            if ((i == 10)&&(gEvtNum % 20)) {
                wait_us(8);
            }
            */
            PIN_ADC_CS = 0;
            PIN_spi.write( 0x00 );
            PIN_ADC_CS = 1;
            /*
            if( PIN_a_sf_clk == 1 ) {
                if( i == 0 )
                    wait_us( 1 );
    
                PIN_ADC_CS = 0;
                PIN_spi.write( 0x00 );
                PIN_ADC_CS = 1;
            } else {
                i--;
            }
            */
        }
        gAdcToMBtimer.stop();
#ifdef DEBUG
        printf("total time = %d us\r\n", gAdcToMBtimer.read_us());
#endif
        if ( kAdcToMBtimeCut < gAdcToMBtimer.read_us() ) {
            gEvent.SetTrgBit(kAdcToMBflag);
        }
        gAdcToMBtimer.reset();
        
        // restart the timers
        //ResetAllTickers();
    } else {
        // cards have no power. don't try reading out
        gReadingOut=false;
        // set gFirstEvt to false even if cards are powered off.
        // otherwise, if cards ARE powered off, it will always be
        // true and the "start trigger" clock will be written continuously
        gFirstEvt = false;
    }
}

bool IsPinPowered(const SnCommWin* cw) {
    bool havePower = false;
    switch (cw->GetCommType()) {
        case SnConfigFrame::kIrid:
            havePower = gConf.IsPoweredFor(SnConfigFrame::kIridComWin)
                && ( (kIridPwrFromAfar)
                      ? PIN_afar_power.read()
                      : PIN_iridSbd_power.read() ==
                    gConf.GetPowPinSetting(SnConfigFrame::kIridComWin));
            break;
        case SnConfigFrame::kAfar:
            havePower = gConf.IsPoweredFor(SnConfigFrame::kAfarComWin)
                && (PIN_afar_power.read() ==
                    gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin));
            break;
        case SnConfigFrame::kUSB:
            havePower = true; // USB always on (for now)
            break;
        case SnConfigFrame::kSDcard: // shouldn't happen. skip it
        default: // unknown.. skip it
            break;
    };
    return havePower;
}


SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig,
                                      const bool isStartupWin) {
    // loop through each comm mode:
    //  a) try to connect
    //  b) if connected, listen for config
    //  c) if config requests data, send it
    /*
    for (int i=0; i<5; i++) {
        led4=1;
        led3=1;
        wait(0.5);
        led4=0;
        led3=0;
        wait(0.5);
    }
    */
    gLastCommWin = time(0);

    SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
    
    // get the trigger rates
    float thmrate=0, evtrate=0;
    GetRates(thmrate, evtrate);
#ifdef DEBUG
    printf("thmrate=%g, evtrate=%g\r\n",thmrate,evtrate);
#endif

    StopAllTickers();
    
    if (gConf.GetCommWinDuration()==0) {
        // TODO: set min so this is not possible
        res = SnCommWin::kOkNoMsg;
    } else {
    
        gCommWinOpen = true;
        Watchdog::kick(); // don't reset!
    
#ifdef DEBUG
        printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
        printf("duration=%u\r\n",gConf.GetCommWinDuration());
#endif
    
        // close the file so that the data is all written out.
        // and open it back up at the beginning (for reading)
#ifdef DEBUG
        printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
        printf("curfile=%p, filename=%s\r\n",SnSDUtils::GetCurFile(),
            SnSDUtils::GetCurFileName());
#endif
        
        if (isStartupWin==false) {
            PIN_lockRegisters = 0; // unlock so we can talk to SD card.
#ifdef DEBUG
            printf("closing output file\r\n");
#endif
            SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
#ifdef DEBUG
            printf("open existing file (%d)\r\n",strlen(SnSDUtils::GetCurFileName()));
#endif
            SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true, false);
        }

#ifdef DEBUG
        printf("setting power\r\n");
#endif
        // (probably) power down cards,amps and power up comms
        SetPower(true);
        
        // time to recount files for the status update
        // for the startup win, don't access SD card in case we
        // rebooted due to a problem with the SD card
        SnStatusFrame::fgRecalcFiles = !isStartupWin;
        
#if defined(ENABLE_AFAR_TWITTER) && defined(ENABLE_AFAR_COMM)
        bool doTwitter = false;
#endif

#ifdef DEBUG
        printf("start loop over comms\r\n");
#endif
        bool sendStat[kNcomms];
        for (uint8_t i=0; i<kNcomms; i++) {
            sendStat[i]=true;
        }
        bool* ss = sendStat;
        SnCommWin** cw = gComms;
        for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
            Watchdog::kick(); // don't reset!
            if (i==kNcomms) {
                i=0;
                cw = gComms;
                ss = sendStat;
            }
            // skip if no comm object
            if ((*cw)==0) {
                continue;
            }
            // skip if no power for this comm
            // THIS IS VITAL! For example, if the ethernet
            // port is powered down, making an Ethernet obejct
            // (done in netif) will stall forever waiting for the clock.
            // Do it here to keep all PIN usage in main.cpp
            const bool havePower=IsPinPowered(*cw);
            if (havePower==false) {
                continue;
            }
            
            // always apply safety nets to connection and listen so
            // that we don't accidently shut down comms (i.e. with
            // connectTO or listnTO being 0)
            gConf.ApplyConnectListenSafetyNets();
            const uint32_t conto = 
                (gConf.GetCommWinDuration() < gConf.GetCommWinConnectTO()) ?
                 gConf.GetCommWinDuration() : gConf.GetCommWinConnectTO();
            const uint32_t listo = 
                (gConf.GetCommWinDuration() < gConf.GetCommWinListenTO()) ?
                 gConf.GetCommWinDuration() : gConf.GetCommWinListenTO();
            
            // update power reading in case we want to send it in status
            GetAvePowerReading();

            // open window and (mabye) send status update
#ifdef DEBUG
            printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
            printf("conto=%u, listo=%u, dur=%u, connTO=%u, lisTO=%u\r\n",
                conto,listo,gConf.GetCommWinDuration(),
                gConf.GetCommWinConnectTO(), gConf.GetCommWinListenTO());
            printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",gConf.GetTimeoutTime(gLastCommWin,conto),
                time(0), gLastCommWin, gConf.GetCommWinDuration());
#endif
            const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow(
//                gConf.GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower,
                gConf.GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gLastEvent, gPower,
                SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
                gGenBuf);
            if (conres>=SnCommWin::kConnected) {
                Watchdog::kick(); // don't reset!
                // connected. listen for config
                *ss = false; // don't send status next time
                
                // clear watchdog reset bit now that we've told someone
                Watchdog::clearResetFlag();
                
#if defined(ENABLE_AFAR_TWITTER) && defined(ENABLE_AFAR_COMM)
                if ((*cw)->GetCommType()==SnConfigFrame::kAfar) {
                    // if we connected by Afar
                    doTwitter = true;
                }
#endif

#ifdef DEBUG
                printf("get conf gtt=%u\r\n",gConf.GetTimeoutTime(gLastCommWin, listo));
#endif
                const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
                    gConf, gConf.GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);

                if (cfgres>=SnCommWin::kOkWithMsg) {
                    Watchdog::kick(); // don't reset!
                    
#ifdef DEBUG
                    printf("received config!\r\n");
                    printf("send data = %d\r\n", gConf.GetCommSendData());
#endif
                    // send data if need be (files, some events, etc)
                    const uint32_t winto = gConf.GetTimeoutTime(gLastCommWin, 
                                                         gConf.GetCommWinDuration());
                    //const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
                    
                    // check if there are any requests before sending data
                    if (gConf.IsWaitingHndShkBeforeSendData()) {
                        // send handshake request
                        (*cw)->SendHndshkReq(gGenBuf, winto);
                        // wait for response
                        uint8_t hndshk(0); uint32_t hndshkLen(0);
                        res = (*cw)->WaitHandshake(gConf, winto, gGenBuf, gBufSize, hndshk,
                                                   &hndshkLen);
                        // handle response
                        if (SnCommWin::kOkWithMsg==res) {
                            res = (*cw)->HandleHandshake(SnSDUtils::GetCurFile(), 
                                      SnSDUtils::GetCurFileName(),
                                      gConf, gLastEvent, gPower, gGenBuf, gBufSize,
                                      winto, hndshk, hndshkLen);
                        }
                    }
                    if (gConf.GetCommSendData()!=0) {
#ifdef DEBUG
                        printf("sending data, winto=%u. lcw=%u, dur=%u, obey=%s\r\n",
                            winto,
                            gLastCommWin, gConf.GetCommWinDuration(),
                            gConf.IsObeyingTimeout() ? "true" : "false");
#endif

                        res = (*cw)->SendData(gConf, gLastEvent, gPower, gGenBuf, gBufSize,
                                              winto);
                    } else {
                        // don't send anything
                        res = cfgres;
                    }
#ifdef DEBUG
                    printf("Got config!\r\n");
#endif
                    Watchdog::kick(); // don't reset!
                    break;
                }
            } else {
                // OpenWindow did not connect
                (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin, listo));
            } // if connected
            
            Watchdog::kick(); // don't reset!
        } // end loop over comms
        
        // check Iridium time, send Iridium signal strength, and close the connection(s)
        cw = gComms;
        for (uint8_t i=0; i<kNcomms; i++, cw++) {
            if ((*cw)==0) {
                continue;
            }
            const bool havePower=IsPinPowered(*cw);
            if (havePower==false) {
                continue;
            }

            // check Iridium time
            if ((*cw)->GetCommType()==SnConfigFrame::kIrid) {
#ifdef DEBUG
                printf("try to set iridium time\r\n");
#endif
                // set the clock before closing connection
                const uint32_t totime = 
                    gConf.GetTimeoutTime(gLastCommWin,
                                         gConf.GetCommWinDuration());
#ifdef DEBUG
                printf("totime=%u, ctime=%u\r\n",totime,time(0));
#endif
                const bool con = (*cw)->Connect(totime);
                if (con) {
                    uint32_t prvTime(0), setTime(0);
                    const bool gottime = (*cw)->TrySetSysTimeUnix(
                        totime, prvTime, setTime);
                    if (gottime) {
                        gClkSet.SetClocks(prvTime, setTime);
#ifdef DEBUG
                        printf("sig str: totime=%u, ctime=%u\r\n",totime,time(0));
#endif
                        // got time; now send signal strength
                        (*cw)->SendSignalStrength( gGenBuf, gSigStr, totime );
                    }
                }
            }
            // close the connection -- this must be why Twitter didn't work!
            //(*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
            // after normal Afar connection closed, try to tweet
#if defined(ENABLE_AFAR_TWITTER) && defined(ENABLE_AFAR_COMM)
            if ((*cw)->GetCommType()==SnConfigFrame::kAfar) {
                // tweet
#ifdef DEBUG
                printf("for twitter: gTwit=%p, doTwitter=%d\r\n",gTwit,(int)doTwitter);
#endif
                // send a twitter update
                if ( (gTwit!=0) && doTwitter ) {
                    const uint32_t conto = 
                        (gConf.GetCommWinDuration() < gTwit->GetConnectTimeout()) ?
                         gConf.GetCommWinDuration() : gTwit->GetConnectTimeout();
                    const uint32_t listo = 
                        (gConf.GetCommWinDuration() < gTwit->GetListenTimeout()) ?
                         gConf.GetCommWinDuration() : gTwit->GetListenTimeout();
#ifdef DEBUG
                    printf("open twit window. conto=%u, listo=%u\r\n",
                        conto, listo);
#endif
                    const SnCommWin::ECommWinResult conres = gTwit->OpenWindow(
                        gConf.GetTimeoutTime(gLastCommWin, conto), false, gConf,
                        gLastEvent, gPower,
                        SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
                        gGenBuf);
                    if (conres>=SnCommWin::kConnected) {
                        Watchdog::kick(); // don't reset!
                        gTwit->Tweet(gConf, thmrate, evtrate, gGenBuf,
                                     gConf.GetTimeoutTime(time(0), listo));
                    }
                }
            } // end tweet block
#endif
            Watchdog::kick(); // don't reset!
        } // end loop: check time, tweet, etc
        
        // close connections
        const uint32_t extraDiscTime = gLastCommWin + gConf.GetCommWinConnectTO();
        cw = gComms;
        for (uint8_t i=0; i<kNcomms; i++, cw++) {
            if ((*cw)==0) {
                continue;
            } else {
                (*cw)->CloseConn(gConf.GetTimeoutTime(extraDiscTime, gConf.GetCommWinDuration()));
            }
        }

    } // if duration >0
    
    /* not working. must use DEFCONF.DAT to change IP's.
    // change comm parameters (IP addresses)
#ifdef DEBUG
    printf("set comm params\r\n");
#endif
    for (uint8_t cc=0; cc<kNcomms; cc++) {
        if (gComms[cc]!=0) {
            gComms[cc]->Set(gConf);
        }
    }
    */

    
    // check if we missed too many consecutive connections
    if (res<=SnCommWin::kAllFails) {
        ++gConsecCommFails;
#ifdef DEBUG
        printf("gConsecCommFails=%hu, kMaxConsecCommFails=%hu\r\n",
            gConsecCommFails,kMaxConsecCommFails);
#endif                    
        if (gConsecCommFails>kMaxConsecCommFails) {
#ifdef DEBUG
            printf("rebooting\r\n");
#endif                    
            // goodbye cruel world, it's over. walk on by...
            mbed_reset();
        }
    } else {
        gConsecCommFails=0;
    }
    
    // (probably) power down comms and power up cards,amps
    SetPower(false);

    // reset config with system powered (for DAC/PLA setting)
#ifdef DEBUG
    printf("calling SetConfigAndMakeOutputFile\r\n");
#endif
    
    SetConfigAndMakeOutputFile();

#ifdef DEBUG
    printf("closing comm win at %d\r\n",(int32_t)time(0));
#endif
    
    gCommWinOpen = false;
    return res;
}