Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: main.cpp
- Revision:
- 16:744ce85aede2
- Parent:
- 15:f2569d8e4176
- Child:
- 17:4687bf932b8c
diff -r f2569d8e4176 -r 744ce85aede2 main.cpp --- a/main.cpp Fri Aug 31 02:09:09 2012 +0000 +++ b/main.cpp Wed Sep 12 04:47:22 2012 +0000 @@ -4,13 +4,15 @@ //#define EVT_TIME_PROFILE //#define DEBUG -#define SSNOTIFY +//#define SSNOTIFY #define USE_MODSERIAL #include <stdint.h> #include "SDFileSystem.h" #ifdef USE_MODSERIAL #include "MODSERIAL.h" +#define MODSERIAL_RX_BUF_SIZE 2048 +#define MODSERIAL_TX_BUF_SIZE 2048 #endif #include "Watchdog.h" #include "SnConstants.h" @@ -81,12 +83,17 @@ // this needs to be first in case some other global uses a print statement #ifdef USE_MODSERIAL -static MODSERIAL gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output -static MODSERIAL gSBDport(p28, p27, "sbd"); +#define SERIAL_TYPE MODSERIAL #else -static Serial gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output -static Serial gSBDport(p28, p27, "sbd"); +#define SERIAL_TYPE Serial #endif +static SERIAL_TYPE gCpu( USBTX, USBRX // defined here so it might be used for debugging output +#ifdef USE_MODSERIAL + ,MODSERIAL_TX_BUF_SIZE, + MODSERIAL_RX_BUF_SIZE +#endif + ); +static SERIAL_TYPE gSBDport(p28, p27, "sbd"); static SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1); static LocalFileSystem local("local"); @@ -129,6 +136,7 @@ static Ticker gPowerCheckTicker; #endif static Timer gTrgTimer; +static Timer gAdcToMBtimer; static SnConfigFrame gConf; static SnEventFrame gEvent; static SnPowerFrame gPower; @@ -194,7 +202,13 @@ uint32_t GetTimeoutTime(const uint32_t startTime, const uint32_t delta) { - const uint32_t lst = time(0)-startTime; + const uint32_t ct = time(0); + uint32_t lst = time(0)-startTime; + if ( (ct<startTime) || (ct==0) || + (lst>kSecsPerDay) ) { + // possible clock problems + lst = kDefTimeout; + } const uint32_t lio = ((lst+delta) < gConf.GetCommWinDuration()) ? lst+delta : gConf.GetCommWinDuration(); @@ -351,6 +365,12 @@ if (timSec>0) { uint32_t tp = timSec > maxTimSec ? maxTimSec : timSec; tp *= 1000u; // ms + /* + if (tik==gForceTicker) { + tik->start((1./10.)*1e3); + return ((1./10.)*1e3); + } else + */ tik->start(tp); return tp; } @@ -421,6 +441,7 @@ int main() { { + //gCpu.baud(19200); #ifdef SSNOTIFY printf("main: start\r\n"); #endif @@ -430,10 +451,11 @@ led3=0; led4=1; wait(0.2); led4=0; } - + // RTOS stuff must be made inside main for some reason //gComms[0] = new SnCommAfarTCP(gConf); - gComms[0] = new SnCommSBD(&gSBDport); + //gComms[0] = new SnCommSBD(&gSBDport, &gCpu); + gComms[0] = new SnCommUsb(&gCpu); #ifdef USE_RTOS_TIMER gForceTicker = new rtos::RtosTimer(&procForceTrigger); @@ -722,8 +744,9 @@ PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak); wait_ms(10); } - wait(1); // let things power up + wait(1.5); // let things power up #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()); @@ -895,6 +918,7 @@ #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) { @@ -915,14 +939,28 @@ procPowerCheck(); procCommWin(); */ - + + //wait_us(6); + // // 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++ ) { + 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 ); @@ -933,11 +971,19 @@ } 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; @@ -949,7 +995,16 @@ // 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; @@ -966,6 +1021,7 @@ #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. @@ -981,14 +1037,6 @@ SetPower(true); - // TODO: implement asking the comms for their timeout - //const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ? - // gConf.GetCommWinDuration() : kConnectTimeout; - const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTOSBD) ? - gConf.GetCommWinDuration() : kConnectTOSBD; - const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ? - gConf.GetCommWinDuration() : kListenTimeout; - bool sendStat[kNcomms]; for (uint8_t i=0; i<kNcomms; i++) { sendStat[i]=true; @@ -1005,17 +1053,25 @@ if ((*cw)==0) { continue; } + + const uint32_t conto = + (gConf.GetCommWinDuration() < (*cw)->GetConnectTimeout()) ? + gConf.GetCommWinDuration() : (*cw)->GetConnectTimeout(); + const uint32_t listo = + (gConf.GetCommWinDuration() < (*cw)->GetListenTimeout()) ? + gConf.GetCommWinDuration() : (*cw)->GetListenTimeout(); + + // 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), time(0), gLastCommWin, gConf.GetCommWinDuration()); #endif - // 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, @@ -1029,8 +1085,10 @@ #endif const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig( 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()); @@ -1046,6 +1104,7 @@ gLastCommWin, gConf.GetCommWinDuration(), gConf.IsObeyingTimeout() ? "true" : "false"); #endif + res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize, gtt, winto); } else { @@ -1069,6 +1128,19 @@ if ((*cw)==0) { continue; } + if ((*cw)->GetCommType()==SnConfigFrame::kIrid) { +#ifdef DEBUG + printf("try to set iridium time\r\n"); +#endif + // set the clock before closing connection + const bool con = + (*cw)->Connect(GetTimeoutTime(gLastCommWin, + gConf.GetCommWinDuration())); + if (con) { + const uint32_t nt = (*cw)->TrySetSysTimeUnix( + GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); + } + } (*cw)->CloseConn(GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration())); Watchdog::kick(); // don't reset! }