Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: main.cpp
- Revision:
- 21:ce51bb0ba4a5
- Parent:
- 19:74155d652c37
- Child:
- 22:f957c4f840ad
--- a/main.cpp Wed Oct 03 00:22:27 2012 +0000 +++ b/main.cpp Wed Oct 10 05:54:12 2012 +0000 @@ -4,6 +4,7 @@ //#define USE_ETH_INTERFACE //#define EVT_TIME_PROFILE + //#define DEBUG //#define SSNOTIFY #define USE_MODSERIAL @@ -15,7 +16,9 @@ #define MODSERIAL_RX_BUF_SIZE 512 #define MODSERIAL_TX_BUF_SIZE 512 #endif +#include "FATDirHandle.h" #include "Watchdog.h" +#include "EthernetPowerControl.h" #include "SnConstants.h" #include "SnBitUtils.h" #include "SnSDUtils.h" @@ -99,8 +102,8 @@ #endif ); static SERIAL_TYPE gSBDport(p28, p27, "sbd"); -static SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); -static LocalFileSystem local("local"); +static SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); // no leading '/' +static LocalFileSystem local(kLocalDir+1); // no leading '/' // // fwd declare fcns @@ -146,6 +149,7 @@ static SnEventFrame gEvent; 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 @@ -155,8 +159,12 @@ 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; +static time_t gLastCommWin = 0; // time +static uint32_t gRecentCountTime = 0; // time of most recent event (for rate) +static uint32_t gLastCountReset = 0; // start time of event count (for rate) +static uint32_t gLastEventReset = 0; // start event number (for rate) +static uint32_t gRecentEvtNum = 0; // number of most recent event (for rate) +static bool gDoResetLastCount = false; // 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; @@ -171,8 +179,16 @@ printf("proc force\r\n"); #endif gEvent.SetTrgBit(kFrcTrg); - gEvent.SetTrgNum((gTrgNum[kFrcTrg])++); + gEvent.SetTrgNum(++(gTrgNum[kFrcTrg])); + PIN_forceTrigger = 0; PIN_forceTrigger = 1; // force a trigger + PIN_forceTrigger = 0; +#ifdef DEBUG + 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 } } @@ -181,6 +197,7 @@ #ifdef DEBUG printf("proc heartbeat\r\n"); #endif + PIN_heartbeat = 0; PIN_heartbeat = 1; // heartbeat pulse PIN_heartbeat = 0; } @@ -205,26 +222,15 @@ } } -uint32_t GetTimeoutTime(const uint32_t startTime, - const uint32_t delta) { - const uint32_t ct = time(0); - uint32_t lst = time(0)-startTime; - if ( (ct<startTime) || (ct==0) || - (lst>kSecsPerDay) ) { - // possible clock problems - lst = kDefTimeout; +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); } - const uint32_t lio = - ((lst+delta) < gConf.GetCommWinDuration()) ? - lst+delta : gConf.GetCommWinDuration(); - return lio+startTime; -} - -bool AreCardsPowered() { -#ifdef DEBUG - printf("acp: PIN_turn_on_system=%d\r\n",PIN_turn_on_system.read()); -#endif - return (PIN_turn_on_system.read()==0); + return gCardsPowered; } void GetAvePowerReading() { @@ -311,7 +317,7 @@ gEvtNum = gConf.GetFirstEvt() + evtStartCurSeq; gPowNum = evtStartCurSeq; memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs); - gLastCountReset = static_cast<uint32_t>(time(0)); // to calc rates + gDoResetLastCount = true; #ifdef DEBUG printf("Reset: gEvtNum=%u, gPowNum=%u, evtStartCS=%u\r\n", gEvtNum, gPowNum, evtStartCurSeq); @@ -320,10 +326,11 @@ void GetRates(float& thmrate, float& evtrate) { thmrate = evtrate = 0; - const uint32_t dt = static_cast<uint32_t>(time(0)) - gLastCountReset; + const uint32_t dt = gRecentCountTime - gLastCountReset; if (dt>0) { - thmrate = gTrgNum[kThmTrg] / dt; - evtrate = (gEvtNum + gConf.GetFirstEvt()) / dt; + const float fdt = static_cast<float>(dt); + thmrate = static_cast<float>(gTrgNum[kThmTrg]) / fdt; + evtrate = static_cast<float>(gRecentEvtNum - gLastEventReset) / fdt; } } @@ -459,17 +466,25 @@ } #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(); + +#ifdef DEBUG printf("making comm objects\r\n"); #endif // RTOS stuff must be made inside main for some reason -/* + /* #ifdef USE_ETH_INTERFACE gComms[0] = new SnCommAfarTCP(gConf); #else gComms[0] = new SnCommAfarNetIf(gConf); #endif -*/ + */ //gComms[0] = new SnCommSBD(&gSBDport, &gCpu); gComms[0] = new SnCommUsb(&gCpu); @@ -528,6 +543,7 @@ led2=0; // the main event loop. wait for triggers in SendClock + AreCardsPowered(true); gTrgTimer.start(); register int32_t etms=0; // time between written events while( true ) @@ -542,7 +558,6 @@ printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false"); printf("readingout=%d\r\n",(int)gReadingOut); #endif - PIN_lockRegisters = 0; // allow data to come from DFPGA WaitTrigAndSendClock(); PIN_lockRegisters = 1; // block registers during readout @@ -573,6 +588,7 @@ 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 @@ -583,7 +599,7 @@ // TODO: no way to check for external trigger? if (gEvent.IsForcedTrg()==false) { gEvent.SetTrgBit(kThmTrg); - gEvent.SetTrgNum((gTrgNum[kThmTrg])++); + gEvent.SetTrgNum(++(gTrgNum[kThmTrg])); } // else already set by procForceTrigger // (no need to calc if we throw this event away) @@ -598,6 +614,16 @@ 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 @@ -632,6 +658,19 @@ 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"); +#endif + OpenCommWin(); + gOpenCommWin=false; + } else { +#ifdef DEBUG + printf("gOpenCommWin=false\r\n"); +#endif + } + #ifdef EVT_TIME_PROFILE prof.stop(); befNewSeq=prof.read_us(); prof.start(); #endif @@ -646,19 +685,6 @@ prof.stop(); aftNewSeq=prof.read_us(); prof.start(); #endif - // open comm win? - if (gOpenCommWin) { -#ifdef DEBUG - printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false"); -#endif - OpenCommWin(); - gOpenCommWin=false; - } else { -#ifdef DEBUG - printf("gOpenCommWin=false\r\n"); -#endif - } - #ifdef EVT_TIME_PROFILE prof.stop(); endOfLoop=prof.read_us(); prof.start(); printf("befReadWv=%d, aftReadWv=%d, befSaveEvt=%d, aftSaveEvt=%d, " @@ -670,6 +696,10 @@ // 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 + gEvent.ClearEvent(); + } } @@ -692,9 +722,6 @@ 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; @@ -747,6 +774,11 @@ // power stuff // void SetPower(const bool isCommWin) { +#ifdef DEBUG + 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 // TODO: turn on amps individually, when that's possible if (isCommWin) { PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardComWin); @@ -755,6 +787,31 @@ wait_ms(10); PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridComWin); wait_ms(10); +#ifdef DEBUG + printf("afar pin=%d, com powsetting=%d\r\n",PIN_afar_power.read(), + gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin)); +#endif + if (gConf.IsPoweredFor(SnConfigFrame::kAfarComWin)) { +#ifdef DEBUG + printf("PHY cowin powering up\r\n"); +#endif + PHY_PowerUp(); wait(1); +#ifdef DEBUG + printf("PHY cowin powered up\r\n"); +#endif + } else { +#ifdef DEBUG + printf("PHY cowin powering down\r\n"); +#endif + PHY_PowerDown(); wait(1); +#ifdef DEBUG + printf("PHY cowin powered down\r\n"); +#endif + } +#ifdef DEBUG + printf("PHY done\r\n"); +#endif + wait_ms(100); PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin); wait_ms(10); } else { @@ -764,15 +821,46 @@ wait_ms(10); PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridDatTak); wait_ms(10); +#ifdef DEBUG + printf("afar pin=%d, dat powsetting=%d, ispow=%d\r\n",PIN_afar_power.read(), + gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak), + (int)(gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak))); +#endif + if (gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak)) { +#ifdef DEBUG + printf("PHY dattak powering up\r\n"); +#endif + PHY_PowerUp(); wait(1); +#ifdef DEBUG + printf("PHY dattak powered up\r\n"); +#endif + } else { +#ifdef DEBUG + printf("PHY dattak powering down\r\n"); +#endif + PHY_PowerDown(); wait(1); +#ifdef DEBUG + printf("PHY dattak powered down\r\n"); +#endif + } +#ifdef DEBUG + printf("PHY done\r\n"); +#endif + wait_ms(100); PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak); wait_ms(10); } wait(1.5); // let things power up #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 } @@ -795,7 +883,7 @@ PIN_heartbeat = 0; wait_ms(20); - if (AreCardsPowered()) { + if (AreCardsPowered(true)) { // Set PLA value(s) PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting PIN_spi.frequency(1000000); @@ -888,9 +976,6 @@ wait_ms(200); MakeOutputFile(); - // reset tickers - ResetAllTickers(); - // TODO: change comm parameters /* printf("set comm params\r\n"); @@ -900,6 +985,9 @@ } } */ + + // reset tickers + ResetAllTickers(); Watchdog::kick(); // don't reset! @@ -918,14 +1006,17 @@ 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()); + printf("cards powered=%d\r\n",(int)AreCardsPowered(true)); #endif PIN_spi.format( 16, 1 ); // back to trigger mode PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz - if (AreCardsPowered()) { + if (AreCardsPowered(false)) { +#ifdef DEBUG + printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false"); +#endif if (gFirstEvt==false) { PIN_DoNotRestartAllClocks = 0; wait_us(1); @@ -951,9 +1042,14 @@ return; // break out to open comms or check power } } - PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers + //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(); /* @@ -1032,7 +1128,11 @@ SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; - bool gotNewConfig=false; + // get the trigger rates + float thmrate=0, evtrate=0; + GetRates(thmrate, evtrate); + + StopAllTickers(); if (gConf.GetCommWinDuration()==0) { // TODO: set min so this is not possible @@ -1056,10 +1156,15 @@ SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile()); SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true); +#ifdef DEBUG + printf("setting power\r\n"); +#endif // (probably) power down cards,amps and power up comms SetPower(true); - +#ifdef DEBUG + printf("start loop over comms\r\n"); +#endif bool sendStat[kNcomms]; for (uint8_t i=0; i<kNcomms; i++) { sendStat[i]=true; @@ -1086,17 +1191,15 @@ // 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); + // open window and (mabye) send status update #ifdef DEBUG printf("calling OpenWindow. ss=%d\r\n",(int)(*ss)); - printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",GetTimeoutTime(gLastCommWin,conto), + 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( - GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower, + gConf.GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower, SnSDUtils::GetCurSeqNum(), thmrate, evtrate, gGenBuf); if (conres>=SnCommWin::kConnected) { @@ -1104,10 +1207,10 @@ // connected. listen for config *ss = false; // don't send status next time #ifdef DEBUG - printf("get conf gtt=%u\r\n",GetTimeoutTime(gLastCommWin, listo)); + printf("get conf gtt=%u\r\n",gConf.GetTimeoutTime(gLastCommWin, listo)); #endif const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig( - gConf, GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize); + gConf, gConf.GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize); if (cfgres>=SnCommWin::kOkWithMsg) { Watchdog::kick(); // don't reset! @@ -1117,19 +1220,19 @@ printf("send data = %d\r\n", gConf.GetCommSendData()); #endif // send data if need be (files, some events, etc) - const uint32_t winto = GetTimeoutTime(gLastCommWin, - gConf.GetCommWinDuration()); + const uint32_t winto = gConf.GetTimeoutTime(gLastCommWin, + gConf.GetCommWinDuration()); const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0; if (gConf.GetCommSendData()!=0) { #ifdef DEBUG printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n", - GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()), + gConf.GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()), gLastCommWin, gConf.GetCommWinDuration(), gConf.IsObeyingTimeout() ? "true" : "false"); #endif res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize, - gtt, winto); + gtt, gConf.GetCommWinDuration()); } else { // don't send anything res = cfgres; @@ -1137,14 +1240,15 @@ #ifdef DEBUG printf("Got config!\r\n"); #endif - gotNewConfig = (cfgres!=SnCommWin::kOkWthMsgNoConf); Watchdog::kick(); // don't reset! break; } - } + } else { + (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin, listo)); + } // if connected Watchdog::kick(); // don't reset! - } + } // end loop over comms // close the connection(s) cw = gComms; for (uint8_t i=0; i<kNcomms; i++, cw++) { @@ -1157,14 +1261,14 @@ #endif // set the clock before closing connection const bool con = - (*cw)->Connect(GetTimeoutTime(gLastCommWin, + (*cw)->Connect(gConf.GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration())); if (con) { const uint32_t nt = (*cw)->TrySetSysTimeUnix( - GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); + gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); } } - (*cw)->CloseConn(GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); + (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); Watchdog::kick(); // don't reset! } } @@ -1175,14 +1279,11 @@ gFirstEvt = true; // reset config with system powered (for DAC/PLA setting) - if (gotNewConfig || forceReconfig) { #ifdef DEBUG - printf("calling SetConfigAndMakeOutputFile\r\n"); + printf("calling SetConfigAndMakeOutputFile\r\n"); #endif - SetConfigAndMakeOutputFile(); - } else { - MakeOutputFile(); - } + SetConfigAndMakeOutputFile(); + #ifdef DEBUG printf("closing comm win at %d\r\n",(int32_t)time(0)); #endif