Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
main.cpp
- Committer:
- uci1
- Date:
- 2012-08-13
- Revision:
- 11:de443350ec4a
- Parent:
- 10:3c93db1cfb12
- Child:
- 12:d472f9811262
File content as of revision 11:de443350ec4a:
#include "mbed.h" #define USE_RTOS_TIMER #include <stdint.h> #include "SDFileSystem.h" #include "MODSERIAL.h" #include "Watchdog.h" #include "SnConstants.h" #include "SnBitUtils.h" #include "SnSDUtils.h" #include "SnConfigFrame.h" #include "SnEventFrame.h" #include "SnStatusFrame.h" #include "SnHeaderFrame.h" #include "SnCommWin.h" #include "SnCommAfarTCP.h" #include "SnCommUsb.h" #include "SnBase64.h" #ifdef USE_RTOS_TIMER #include "RtosTimer.h" #endif // // 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); // 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 // this needs to be first in case some other global uses a print statement static MODSERIAL gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1); LocalFileSystem local("local"); // // fwd declare fcns // void ReadAllRegisters(); void ReadRegister(const uint8_t chan, int16_t* dev); void SaveEvent(const int32_t etms); void WaitTrigAndSendClock(); void SetConfigAndMakeOutputFile(); SnCommWin::ECommWinResult OpenCommWin(); void MakeOutputFile(const bool stopRunning=false); void SetPower(const bool isCommWin); void procForceTrigger(); void procHeartbeat(); void procPowerCheck(); void procCommWin(); #ifdef USE_RTOS_TIMER 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_TIMER 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 gEvtTimer; static SnConfigFrame gConf; static SnEventFrame gEvent; static SnPowerFrame gPower; // parameters static bool gFirstEvt = true; static bool gReadingOut = false; static 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; static uint32_t gLastCountReset = 0; // this should be bigger than anything that will actually be used static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + (2u*SnHeaderFrame::kMaxSizeOf) + SnPowerFrame::kMaxSizeOf; //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 void procForceTrigger() { led3=!led3; if (gReadingOut==false && gCommWinOpen==false) { printf("proc force\r\n"); gEvent.SetTrgBit(kFrcTrg); gEvent.SetTrgNum((gTrgNum[kFrcTrg])++); PIN_forceTrigger = 1; // force a trigger } } void procHeartbeat() { if (gReadingOut==false && gCommWinOpen==false) { printf("proc heartbeat\r\n"); PIN_heartbeat = 1; // heartbeat pulse PIN_heartbeat = 0; } } void procPowerCheck() { printf("proc power\r\n"); gCheckPower=true; } void procCommWin() { if (gReadingOut==false && gCommWinOpen==false) { if ( (time(0) - gLastCommWin) > gConf.GetCommWinPeriod() ) { printf("proc comm win\r\n"); led3=!led3; gOpenCommWin = true; } } } uint32_t GetTimeoutTime(const uint32_t startTime, const uint32_t delta) { const uint32_t lst = time(0)-startTime; const uint32_t lio = ((lst+delta) < gConf.GetCommWinDuration()) ? lst+delta : gConf.GetCommWinDuration(); return lio+startTime; } bool AreCardsPowered() { printf("acp: PIN_turn_on_system=%d\r\n",PIN_turn_on_system.read()); return (PIN_turn_on_system.read()==0); } 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) { printf("CheckPower\r\n"); // read power GetAvePowerReading(); // save to disk FILE* cf = SnSDUtils::GetCurFile(); if (cf!=0) { PIN_lockRegisters = 0; // unlock so we can talk to SD card. 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); SnSDUtils::WritePowerTo(cf, gPower, gPowNum); } // do we need to change modes? bool changed = false; if (gConf.IsLowPowerMode()) { if (gPower.GetAveV1() > gConf.GetBatVoltLowPwr()) { printf("chaing to normal power!\r\n"); gConf.ChangeToNormPower(); changed = true; } } else { if (gPower.GetAveV1() < gConf.GetBatVoltLowPwr()) { printf("chaing to low power!\r\n"); gConf.ChangeToLowPower(); changed = true; } } if (changed) { SetPower(isCommWin); printf("Using config %s\r\n",gConf.GetLabel()); SetConfigAndMakeOutputFile(); // setup defaults in case no communication } // checking done gCheckPower = false; } void ResetCountersClearEvt() { gEvent.ClearEvent(); gEvtNum = gConf.GetFirstEvt(); gPowNum = 0; memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs); gLastCountReset = static_cast<uint32_t>(time(0)); } void GetRates(float& thmrate, float& evtrate) { thmrate = evtrate = 0; const uint32_t dt = static_cast<uint32_t>(time(0)) - gLastCountReset; if (dt>0) { thmrate = gTrgNum[kThmTrg] / dt; evtrate = (gEvtNum + gConf.GetFirstEvt()) / dt; } } bool IsSeqComplete() { printf("IsSeqComplete: eps=%u, cntpow=%d, fe=%u, pow=%u, evt=%u\r\n", gConf.GetEvtsPerFile(), (int)gConf.IsCountingPowerReadings(), gConf.GetFirstEvt(), gPowNum, gEvtNum); if (gConf.GetEvtsPerFile()>0) { if (gConf.IsCountingPowerReadings()) { return (gPowNum>=gConf.GetEvtsPerFile()); } else { return (gEvtNum>=(gConf.GetFirstEvt()+gConf.GetEvtsPerFile())); } } return false; } #ifdef USE_RTOS_TIMER void stopTicker(rtos::RtosTimer* tik) { if (tik!=0) { tik->stop(); } } #else void stopTicker(Ticker& tik) { tik.detach(); } #endif #ifdef USE_RTOS_TIMER uint32_t resetTicker(rtos::RtosTimer* tik, const uint32_t timSec, const uint32_t maxTimSec) { if (tik!=0) { tik->stop(); if (timSec>0) { uint32_t tp = timSec > maxTimSec ? maxTimSec : timSec; tp *= 1000u; // ms tik->start(tp); return tp; } } return 0; } #else uint32_t resetTicker(Ticker& tik, const uint32_t timSec, const uint32_t maxTimSec, void (*fptr)(void)) { tik.detach(); if (timSec>0) { const uint32_t tp = timSec > maxTimSec ? maxTimSec : timSec; tik.attach(fptr, tp); return tp; } return 0; } #endif void StopRunning() { printf("stop running\r\n"); printf("stopping force\r\n"); stopTicker(gForceTicker); printf("stop heart\r\n"); stopTicker(gHeartbeatTicker); printf("stop comm win\r\n"); stopTicker(gCommWinTicker); printf("stop power check\r\n"); stopTicker(gPowerCheckTicker); while (true) { led3 = 1; led4=1; wait(0.5); led3 = 0; led4=0; wait(0.5); //Watchdog::kick(); - if we kick the watchdog, the station is unrecoverable without physical access } } int main() { { 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); led4=0; } // RTOS stuff must be made inside main for some reason gComms[0] = new SnCommAfarTCP(gConf); #ifdef USE_RTOS_TIMER gForceTicker = new rtos::RtosTimer(&procForceTrigger); gHeartbeatTicker = new rtos::RtosTimer(&procHeartbeat); gCommWinTicker = new rtos::RtosTimer(&procCommWin); gPowerCheckTicker = new rtos::RtosTimer(&procPowerCheck); #endif led2=1; //wait_ms(100); printf("\n\n\n\n\n\nstarting\r\n"); // a failsafe Watchdog::kick(kWDFailsafe); // set the clock to the BS time, if it's not set if ( (static_cast<int32_t>(time(0)))<0 ) { set_time(kBStime); } printf("time = %d\r\n",(int32_t)time(0)); gLastCommWin = time(0); // prevent comm win proc #ifdef USE_RTOS_TIMER gForceTicker->stop(); #else gForceTicker.detach(); #endif gFirstEvt = true; // (probably) power down comms and power up cards,amps SetPower(false); printf("Using config %s\r\n",gConf.GetLabel()); SetConfigAndMakeOutputFile(); // setup defaults in case no communication // // get config // printf("call OpenCommWin\r\n"); OpenCommWin(); // get ready to trigger PIN_spi.format( 16, 1 ); // change to data readout format led2=0; // the main event loop. wait for triggers in SendClock gEvtTimer.start(); while( true ) { // in here, we wait for triggers from the MB-FPGA Watchdog::kick(); // don't reset! led1 = !led1; printf("calling wait trig\r\n"); printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false"); printf("readingout=%d\r\n",(int)gReadingOut); PIN_lockRegisters = 0; // allow data to come from DFPGA WaitTrigAndSendClock(); PIN_lockRegisters = 1; // block registers during readout if (gReadingOut) { const int32_t etms = gEvtTimer.read_ms(); // time since last trigger gEvtTimer.reset(); gEvtTimer.start(); // start counter from this trigger Watchdog::kick(); // don't reset! // // got trigger. read registers to mbed and build the event // led4=1; printf("readout\r\n"); // read data & calc CRC gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit); gEvent.SetCurMbedTime(); // TODO: no way to check for external trigger? if (gEvent.IsForcedTrg()==false) { gEvent.SetTrgBit(kThmTrg); gEvent.SetTrgNum((gTrgNum[kThmTrg])++); } // else already set by procForceTrigger // (no need to calc if we throw this event away) Watchdog::kick(); // don't reset! printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false"); if ( gEvent.IsForcedTrg() || gFirstEvt || (etms>gConf.GetEvtThrtlPeriodMs()) ) { led2=1; PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card. SaveEvent(etms); /* } else { printf("forced=%s, gFirstEvt=%s, e>t %d>%hu %s\r\n", gEvent.IsForcedTrg()?"true":"false", gFirstEvt?"true":"false", etms, gConf.GetEvtThrtlPeriodMs(), etms>gConf.GetEvtThrtlPeriodMs() ? "true":"false"); */ } } printf("past reading out\r\n"); led4=0; led2=0; // check the power? if (gCheckPower) { printf("call check power\r\n"); CheckPower(false); } // make new seq? if (IsSeqComplete()) { printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode()); MakeOutputFile(gConf.IsSingleSeqRunMode()); } // open comm win? if (gOpenCommWin) { printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false"); OpenCommWin(); gOpenCommWin=false; } else { printf("gOpenCommWin=false\r\n"); } } } // // save the event // void SaveEvent(const int32_t etms) { // write the event printf("save event\r\n"); // 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); // reset gEvent.ClearEvent(); // increment event number ++gEvtNum; printf("gEvtNum=%u\r\n",gEvtNum); } void MakeOutputFile(const bool stopRunning) { PIN_lockRegisters = 0; // unlock so we can talk to SD card. printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n", gEvtNum,gPowNum,(int)stopRunning); SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile()); printf("file closed\r\n"); if (stopRunning) { StopRunning(); } FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(), gConf.GetRun()); if (cf!=0) { wait_ms(200); GetAvePowerReading(); 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); SnSDUtils::WritePowerTo(cf, gPower, gPowNum); } printf("made output file with run %u\r\n",gConf.GetRun()); printf("filename=%s\r\n",SnSDUtils::GetCurFileName()); SnSDUtils::WriteConfig(SnSDUtils::GetCurFile(), gConf); } // // power stuff // void SetPower(const bool isCommWin) { // TODO: turn on amps individually, when that's possible if (isCommWin) { PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardComWin); wait_ms(10); PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsComWin); wait_ms(10); PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridComWin); wait_ms(10); PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin); wait_ms(10); } else { PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardDatTak); wait_ms(10); PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsDatTak); wait_ms(10); PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridDatTak); wait_ms(10); PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak); wait_ms(10); } wait(1); 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()); } // // set configuration // void SetConfigAndMakeOutputFile() { printf("SetConfigAndMakeOutputFile\r\n"); // 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; wait_ms(20); // reset event, timers, trigger counters ResetCountersClearEvt(); if (AreCardsPowered()) { // 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; wait(4); 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); printf("pla hi %hu, lo %hu\r\n",hi,lo); } else { PIN_spi.write(kNoTrigPla); // hi PIN_spi.write(kNoTrigPla); // lo printf("pla hi %hu, lo %hu\r\n",kNoTrigPla,kNoTrigPla); } Watchdog::kick(); } wait(3); PIN_PLA_cs=0; wait(3); // 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. printf("setting dacs\r\n"); 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; printf("dac %04x\r\n",dv); // send to FPGA PIN_start_fpga=1; PIN_spi.write(dv); PIN_start_fpga=0; Watchdog::kick(); } printf("dacs set\r\n"); wait_ms(20); } else { printf("cards off. skipping PLA and DAC setting\r\n"); } // 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 wait_ms(200); MakeOutputFile(); // force a trigger every... #ifdef USE_RTOS_TIMER const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(), kAbsMaxTimer); const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(), kAbsMaxTimer); const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(), kCommWinLongPrdTk); const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(), kAbsMaxTimer); #else const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(), kAbsMaxTimer, &procForceTrigger); const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(), kAbsMaxTimer, &procHeartbeat); const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(), kCommWinLongPrdTk, &procCommWin); const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(), kAbsMaxTimer, &procPowerCheck); #endif printf("attach force trig %u\r\n",ftp); printf("attach heart beat %u\r\n",hbp); printf("attach comm win %u\r\n",cwp); printf("attach power chk %u\r\n",pcp); // TODO: change comm parameters /* printf("set comm params\r\n"); for (uint8_t cc=0; cc<kNcomms; cc++) { if (gComms[cc]!=0) { gComms[cc]->Set(gConf); } } */ Watchdog::kick(); // don't reset! printf("set config done\r\n"); } // // readout functions // void WaitTrigAndSendClock() { 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()); if (AreCardsPowered()) { 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. // printf("starting wait for trig\r\n"); gReadingOut = false; // this will allow forced triggers (see procForceTrigger()) while ( PIN_a_sf_clk == 1 ) { if (gOpenCommWin || gCheckPower) { printf("break com=%d, pow=%d\r\n",gOpenCommWin,gCheckPower); return; // break out to open comms or check power } } printf("starting readout. force=%d, clk=%d\r\n", PIN_forceTrigger.read(), PIN_a_sf_clk.read()); PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers gReadingOut = true; // disallow new forced triggers // // 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) for( uint8_t i = 0; i < kNsamps; i++ ) { 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--; } } } else { // cards have no power. don't try reading out gReadingOut=false; } } SnCommWin::ECommWinResult OpenCommWin() { // loop through each comm mode: // a) try to connect // b) if connected, listen for config // c) if config requests data, send it gLastCommWin = time(0); if (gConf.GetCommWinDuration()==0) { // TODO: set min so this is not possible return SnCommWin::kOkNoMsg; } gCommWinOpen = true; Watchdog::kick(); // don't reset! printf("opening comm window at %d\r\n", (int32_t)gLastCommWin); // close the file so that the data is all written out. // and open it back up at the beginning (for reading) printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum); PIN_lockRegisters = 0; // unlock so we can talk to SD card. SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile()); SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true); // (probably) power down cards,amps and power up comms SetPower(true); const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ? gConf.GetCommWinDuration() : kConnectTimeout; const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ? gConf.GetCommWinDuration() : kListenTimeout; SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; bool gotNewConfig=false; 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; } if ((*cw)==0) { continue; } // open window and (mabye) send status update printf("calling OpenWindow. ss=%d\r\n",(int)(*ss)); printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",GetTimeoutTime(gLastCommWin,conto), time(0), gLastCommWin, gConf.GetCommWinDuration()); // update power reading in case we want to send it in status GetAvePowerReading(); // get the trigger rates float thmrate=0, evtrate=0; GetRates(thmrate, evtrate); const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow( GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, 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 const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig( gConf, GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize); if (cfgres>=SnCommWin::kOkWithMsg) { Watchdog::kick(); // don't reset! printf("received config!\r\n"); // send data if need be (files, some events, etc) printf("send data = %d\r\n", gConf.GetCommSendData()); if (gConf.GetCommSendData()!=0) { printf("sending data\r\n"); res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize, GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration())); } else { // don't send anything res = cfgres; } printf("Got config!\r\n"); gotNewConfig=true; Watchdog::kick(); // don't reset! break; } } Watchdog::kick(); // don't reset! } // (probably) power down comms and power up cards,amps SetPower(false); gFirstEvt = true; // reset config with system powered (for DAC/PLA setting) if (gotNewConfig) { printf("calling SetConfigAndMakeOutputFile\r\n"); SetConfigAndMakeOutputFile(); // TODO: remove } printf("closing comm win at %d\r\n",(int32_t)time(0)); gCommWinOpen = false; return res; }