Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Aug 18 05:00:32 2012 +0000
Revision:
12:d472f9811262
Parent:
11:de443350ec4a
Child:
13:7a1fb885a8e4
Fixed bug in throttling timers. Now evt stores dt of triggers but throttle works on dt of events. AfarTCP communication possibly done. Debug output put behind precompiler macros.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "mbed.h"
uci1 0:664899e0b988 2
uci1 8:95a325df1f6b 3 #define USE_RTOS_TIMER
uci1 8:95a325df1f6b 4
uci1 12:d472f9811262 5 //#define EVT_TIME_PROFILE
uci1 12:d472f9811262 6 //#define DEBUG
uci1 12:d472f9811262 7
uci1 0:664899e0b988 8 #include <stdint.h>
uci1 0:664899e0b988 9 #include "SDFileSystem.h"
uci1 0:664899e0b988 10 #include "MODSERIAL.h"
uci1 0:664899e0b988 11 #include "Watchdog.h"
uci1 0:664899e0b988 12 #include "SnConstants.h"
uci1 0:664899e0b988 13 #include "SnBitUtils.h"
uci1 0:664899e0b988 14 #include "SnSDUtils.h"
uci1 0:664899e0b988 15 #include "SnConfigFrame.h"
uci1 0:664899e0b988 16 #include "SnEventFrame.h"
uci1 0:664899e0b988 17 #include "SnStatusFrame.h"
uci1 2:e67f7c158087 18 #include "SnHeaderFrame.h"
uci1 0:664899e0b988 19 #include "SnCommWin.h"
uci1 7:079617408fec 20 #include "SnCommAfarTCP.h"
uci1 0:664899e0b988 21 #include "SnCommUsb.h"
uci1 1:e392595b4b76 22 #include "SnBase64.h"
uci1 8:95a325df1f6b 23 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 24 #include "RtosTimer.h"
uci1 8:95a325df1f6b 25 #endif
uci1 0:664899e0b988 26
uci1 0:664899e0b988 27 //
uci1 0:664899e0b988 28 // MBED PINS (ordered by number)
uci1 0:664899e0b988 29 //
uci1 0:664899e0b988 30 // leds (for debugging)
uci1 0:664899e0b988 31 DigitalOut led1(LED1);
uci1 0:664899e0b988 32 DigitalOut led2(LED2);
uci1 0:664899e0b988 33 DigitalOut led3(LED3);
uci1 0:664899e0b988 34 DigitalOut led4(LED4);
uci1 0:664899e0b988 35 // Set up power pins - Note that it's Zero for "on"
uci1 1:e392595b4b76 36 DigitalOut PIN_turn_on_system(p17); // this turns on system
uci1 0:664899e0b988 37 DigitalOut PIN_turn_on_amps(p25);
uci1 0:664899e0b988 38 // Activate/select chip by falling edge
uci1 0:664899e0b988 39 DigitalOut PIN_ADC_CS( p9 );
uci1 0:664899e0b988 40 // clock signal to activate PLA setting
uci1 0:664899e0b988 41 DigitalOut PIN_PLA_cs(p10);
uci1 0:664899e0b988 42 // To force a trigger
uci1 0:664899e0b988 43 DigitalOut PIN_forceTrigger(p11); //modification
uci1 0:664899e0b988 44 // To suppress thermal triggers
uci1 0:664899e0b988 45 DigitalOut PIN_enableThermTrig(p12);
uci1 0:664899e0b988 46 // Restart clock on all FPGAs.
uci1 0:664899e0b988 47 DigitalOut PIN_DoNotRestartAllClocks( p13 );
uci1 0:664899e0b988 48 // This tells the DFPGAs to store the data on motherboard FPGA and
uci1 0:664899e0b988 49 // read it out.
uci1 0:664899e0b988 50 DigitalIn PIN_a_sf_clk( p14 );
uci1 0:664899e0b988 51 DigitalIn PIN_rst_a_sf(p15);
uci1 1:e392595b4b76 52 // afar power
uci1 1:e392595b4b76 53 DigitalOut PIN_afar_power(p16);
uci1 4:a91682e19d6b 54 // batter voltage/current measurement
uci1 4:a91682e19d6b 55 AnalogIn PIN_vADC1(p19);
uci1 4:a91682e19d6b 56 AnalogIn PIN_vADC2(p18);
uci1 0:664899e0b988 57 // Lock daughter card registeres (during data readout).
uci1 0:664899e0b988 58 DigitalOut PIN_lockRegisters( p20 );
uci1 1:e392595b4b76 59 // iridium (SBD) power
uci1 1:e392595b4b76 60 DigitalOut PIN_iridSbd_power(p21);
uci1 0:664899e0b988 61 // Majority logic pins
uci1 0:664899e0b988 62 DigitalOut PIN_MajLogHiBit(p22);
uci1 0:664899e0b988 63 DigitalOut PIN_MajLogLoBit(p23);
uci1 0:664899e0b988 64 // Tell FPGA to be ready to accept DAC values
uci1 0:664899e0b988 65 DigitalOut PIN_start_fpga(p26);
uci1 0:664899e0b988 66 // Two bits to the select the daughter card for readout
uci1 0:664899e0b988 67 DigitalOut PIN_selCardHiBit( p29 );
uci1 0:664899e0b988 68 DigitalOut PIN_selCardLoBit( p30 );
uci1 0:664899e0b988 69 // To launch a heartbeat pulse
uci1 0:664899e0b988 70 DigitalOut PIN_heartbeat(p24);
uci1 0:664899e0b988 71 // Setup SPI pins
uci1 0:664899e0b988 72 SPI PIN_spi( p5, p6, p7 );
uci1 0:664899e0b988 73 // The SD card
uci1 3:24c5f0f50bf1 74
uci1 3:24c5f0f50bf1 75 // this needs to be first in case some other global uses a print statement
uci1 3:24c5f0f50bf1 76 static MODSERIAL gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output
uci1 3:24c5f0f50bf1 77
uci1 0:664899e0b988 78 SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1);
uci1 0:664899e0b988 79 LocalFileSystem local("local");
uci1 0:664899e0b988 80
uci1 0:664899e0b988 81 //
uci1 0:664899e0b988 82 // fwd declare fcns
uci1 0:664899e0b988 83 //
uci1 0:664899e0b988 84 void ReadAllRegisters();
uci1 0:664899e0b988 85 void ReadRegister(const uint8_t chan, int16_t* dev);
uci1 0:664899e0b988 86 void SaveEvent(const int32_t etms);
uci1 0:664899e0b988 87 void WaitTrigAndSendClock();
uci1 1:e392595b4b76 88 void SetConfigAndMakeOutputFile();
uci1 0:664899e0b988 89 SnCommWin::ECommWinResult OpenCommWin();
uci1 3:24c5f0f50bf1 90 void MakeOutputFile(const bool stopRunning=false);
uci1 4:a91682e19d6b 91 void SetPower(const bool isCommWin);
uci1 8:95a325df1f6b 92 void procForceTrigger();
uci1 8:95a325df1f6b 93 void procHeartbeat();
uci1 8:95a325df1f6b 94 void procPowerCheck();
uci1 8:95a325df1f6b 95 void procCommWin();
uci1 8:95a325df1f6b 96 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 97 void procForceTrigger(void const *) { return procForceTrigger(); }
uci1 8:95a325df1f6b 98 void procHeartbeat(void const *) { return procHeartbeat(); }
uci1 8:95a325df1f6b 99 void procPowerCheck(void const *) { return procPowerCheck(); }
uci1 8:95a325df1f6b 100 void procCommWin(void const *) { return procCommWin(); }
uci1 8:95a325df1f6b 101 #endif
uci1 0:664899e0b988 102
uci1 0:664899e0b988 103 //
uci1 0:664899e0b988 104 // globals
uci1 0:664899e0b988 105 //
uci1 0:664899e0b988 106 // readout objs
uci1 8:95a325df1f6b 107 // TODO: use RtosTimer instead of Ticker?
uci1 8:95a325df1f6b 108 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 109 static rtos::RtosTimer* gForceTicker;
uci1 8:95a325df1f6b 110 static rtos::RtosTimer* gHeartbeatTicker;
uci1 8:95a325df1f6b 111 static rtos::RtosTimer* gCommWinTicker;
uci1 8:95a325df1f6b 112 static rtos::RtosTimer* gPowerCheckTicker;
uci1 8:95a325df1f6b 113 #else
uci1 0:664899e0b988 114 static Ticker gForceTicker;
uci1 3:24c5f0f50bf1 115 static Ticker gHeartbeatTicker;
uci1 1:e392595b4b76 116 static Ticker gCommWinTicker;
uci1 8:95a325df1f6b 117 static Ticker gPowerCheckTicker;
uci1 8:95a325df1f6b 118 #endif
uci1 12:d472f9811262 119 static Timer gTrgTimer;
uci1 0:664899e0b988 120 static SnConfigFrame gConf;
uci1 0:664899e0b988 121 static SnEventFrame gEvent;
uci1 8:95a325df1f6b 122 static SnPowerFrame gPower;
uci1 0:664899e0b988 123 // parameters
uci1 0:664899e0b988 124 static bool gFirstEvt = true;
uci1 0:664899e0b988 125 static bool gReadingOut = false;
uci1 0:664899e0b988 126 static bool gCommWinOpen = false; // if it's open
uci1 1:e392595b4b76 127 static volatile bool gOpenCommWin = false; // if it should be opened
uci1 8:95a325df1f6b 128 static volatile bool gCheckPower = false; // if it should be checked
uci1 8:95a325df1f6b 129 static uint32_t gPowNum = 0;
uci1 8:95a325df1f6b 130 static uint32_t gEvtNum = 0; // num of evt written
uci1 8:95a325df1f6b 131 static uint32_t gTrgNum[kNumTrgs] = {0}; // num of this type of trg received
uci1 0:664899e0b988 132 // i/o
uci1 1:e392595b4b76 133 static time_t gLastCommWin = 0;
uci1 10:3c93db1cfb12 134 static uint32_t gLastCountReset = 0;
uci1 10:3c93db1cfb12 135 // this should be bigger than anything that will actually be used
uci1 10:3c93db1cfb12 136 static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + (2u*SnHeaderFrame::kMaxSizeOf) + SnPowerFrame::kMaxSizeOf;
uci1 6:6f002d202f59 137 //static const uint32_t gB64Bsize=BASE64ENC_LEN(gBufSize)+1;
uci1 6:6f002d202f59 138 //static char gB64Buf[gB64Bsize];
uci1 3:24c5f0f50bf1 139 static char gGenBuf[gBufSize]; // must be big enough for event or status or config!
uci1 11:de443350ec4a 140 static SnCommWin* gComms[kNcomms] = { 0 }; // order => priority. afar uses RTOS, and must be made inside main
uci1 0:664899e0b988 141
uci1 0:664899e0b988 142 void procForceTrigger() {
uci1 3:24c5f0f50bf1 143 led3=!led3;
uci1 0:664899e0b988 144 if (gReadingOut==false && gCommWinOpen==false) {
uci1 12:d472f9811262 145 #ifdef DEBUG
uci1 8:95a325df1f6b 146 printf("proc force\r\n");
uci1 12:d472f9811262 147 #endif
uci1 0:664899e0b988 148 gEvent.SetTrgBit(kFrcTrg);
uci1 0:664899e0b988 149 gEvent.SetTrgNum((gTrgNum[kFrcTrg])++);
uci1 0:664899e0b988 150 PIN_forceTrigger = 1; // force a trigger
uci1 0:664899e0b988 151 }
uci1 0:664899e0b988 152 }
uci1 0:664899e0b988 153
uci1 3:24c5f0f50bf1 154 void procHeartbeat() {
uci1 3:24c5f0f50bf1 155 if (gReadingOut==false && gCommWinOpen==false) {
uci1 12:d472f9811262 156 #ifdef DEBUG
uci1 8:95a325df1f6b 157 printf("proc heartbeat\r\n");
uci1 12:d472f9811262 158 #endif
uci1 3:24c5f0f50bf1 159 PIN_heartbeat = 1; // heartbeat pulse
uci1 3:24c5f0f50bf1 160 PIN_heartbeat = 0;
uci1 3:24c5f0f50bf1 161 }
uci1 3:24c5f0f50bf1 162 }
uci1 3:24c5f0f50bf1 163
uci1 8:95a325df1f6b 164 void procPowerCheck() {
uci1 12:d472f9811262 165 #ifdef DEBUG
uci1 8:95a325df1f6b 166 printf("proc power\r\n");
uci1 12:d472f9811262 167 #endif
uci1 8:95a325df1f6b 168 gCheckPower=true;
uci1 8:95a325df1f6b 169 }
uci1 8:95a325df1f6b 170
uci1 0:664899e0b988 171 void procCommWin() {
uci1 0:664899e0b988 172 if (gReadingOut==false && gCommWinOpen==false) {
uci1 1:e392595b4b76 173 if ( (time(0) - gLastCommWin) > gConf.GetCommWinPeriod() ) {
uci1 12:d472f9811262 174 #ifdef DEBUG
uci1 8:95a325df1f6b 175 printf("proc comm win\r\n");
uci1 12:d472f9811262 176 #endif
uci1 1:e392595b4b76 177 led3=!led3;
uci1 1:e392595b4b76 178 gOpenCommWin = true;
uci1 1:e392595b4b76 179 }
uci1 0:664899e0b988 180 }
uci1 0:664899e0b988 181 }
uci1 0:664899e0b988 182
uci1 1:e392595b4b76 183 uint32_t GetTimeoutTime(const uint32_t startTime,
uci1 1:e392595b4b76 184 const uint32_t delta) {
uci1 1:e392595b4b76 185 const uint32_t lst = time(0)-startTime;
uci1 1:e392595b4b76 186 const uint32_t lio =
uci1 1:e392595b4b76 187 ((lst+delta) < gConf.GetCommWinDuration()) ?
uci1 1:e392595b4b76 188 lst+delta : gConf.GetCommWinDuration();
uci1 1:e392595b4b76 189 return lio+startTime;
uci1 1:e392595b4b76 190 }
uci1 1:e392595b4b76 191
uci1 8:95a325df1f6b 192 bool AreCardsPowered() {
uci1 12:d472f9811262 193 #ifdef DEBUG
uci1 8:95a325df1f6b 194 printf("acp: PIN_turn_on_system=%d\r\n",PIN_turn_on_system.read());
uci1 12:d472f9811262 195 #endif
uci1 8:95a325df1f6b 196 return (PIN_turn_on_system.read()==0);
uci1 8:95a325df1f6b 197 }
uci1 0:664899e0b988 198
uci1 8:95a325df1f6b 199 void GetAvePowerReading() {
uci1 8:95a325df1f6b 200 // use one measurement as the assumed average
uci1 8:95a325df1f6b 201 // in order to reduce computational errors
uci1 8:95a325df1f6b 202 int32_t v1, v2;
uci1 8:95a325df1f6b 203 const uint16_t aaveV1 = PIN_vADC1.read_u16();
uci1 8:95a325df1f6b 204 const uint16_t aaveV2 = PIN_vADC2.read_u16();
uci1 8:95a325df1f6b 205 float n=0, ave1=0, ave2=0, rms1=0, rms2=0;
uci1 8:95a325df1f6b 206 for (uint16_t i=0; i<kNvoltsAve; i++) {
uci1 8:95a325df1f6b 207 v1 = PIN_vADC1.read_u16() - aaveV1;
uci1 8:95a325df1f6b 208 v2 = PIN_vADC2.read_u16() - aaveV2;
uci1 8:95a325df1f6b 209 n += 1;
uci1 8:95a325df1f6b 210 ave1 += v1;
uci1 8:95a325df1f6b 211 rms1 += v1*v1;
uci1 8:95a325df1f6b 212 ave2 += v2;
uci1 8:95a325df1f6b 213 rms2 += v2*v2;
uci1 8:95a325df1f6b 214 }
uci1 8:95a325df1f6b 215 rms1 -= (ave1*ave1)/n;
uci1 8:95a325df1f6b 216 rms2 -= (ave2*ave2)/n;
uci1 8:95a325df1f6b 217 rms1 /= n-1;
uci1 8:95a325df1f6b 218 rms2 /= n-1;
uci1 9:a1a39573dd43 219 rms1 = sqrt(rms1);
uci1 9:a1a39573dd43 220 rms2 = sqrt(rms2);
uci1 8:95a325df1f6b 221 ave1 /= n;
uci1 8:95a325df1f6b 222 ave2 /= n;
uci1 8:95a325df1f6b 223 ave1 += aaveV1;
uci1 8:95a325df1f6b 224 ave2 += aaveV2;
uci1 8:95a325df1f6b 225 gPower.Set(ave1, ave2, rms1, rms2, time(0));
uci1 8:95a325df1f6b 226 }
uci1 0:664899e0b988 227
uci1 8:95a325df1f6b 228 void CheckPower(const bool isCommWin) {
uci1 12:d472f9811262 229 #ifdef DEBUG
uci1 8:95a325df1f6b 230 printf("CheckPower\r\n");
uci1 12:d472f9811262 231 #endif
uci1 8:95a325df1f6b 232 // read power
uci1 8:95a325df1f6b 233 GetAvePowerReading();
uci1 8:95a325df1f6b 234 // save to disk
uci1 8:95a325df1f6b 235 FILE* cf = SnSDUtils::GetCurFile();
uci1 8:95a325df1f6b 236 if (cf!=0) {
uci1 11:de443350ec4a 237 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 12:d472f9811262 238 #ifdef DEBUG
uci1 8:95a325df1f6b 239 printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
uci1 8:95a325df1f6b 240 gPower.GetAveV1(), gPower.GetAveV2(),
uci1 8:95a325df1f6b 241 gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
uci1 8:95a325df1f6b 242 gPowNum);
uci1 12:d472f9811262 243 #endif
uci1 8:95a325df1f6b 244 SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
uci1 8:95a325df1f6b 245 }
uci1 8:95a325df1f6b 246 // do we need to change modes?
uci1 8:95a325df1f6b 247 bool changed = false;
uci1 8:95a325df1f6b 248 if (gConf.IsLowPowerMode()) {
uci1 8:95a325df1f6b 249 if (gPower.GetAveV1() > gConf.GetBatVoltLowPwr()) {
uci1 12:d472f9811262 250 #ifdef DEBUG
uci1 8:95a325df1f6b 251 printf("chaing to normal power!\r\n");
uci1 12:d472f9811262 252 #endif
uci1 8:95a325df1f6b 253 gConf.ChangeToNormPower();
uci1 8:95a325df1f6b 254 changed = true;
uci1 8:95a325df1f6b 255 }
uci1 8:95a325df1f6b 256 } else {
uci1 8:95a325df1f6b 257 if (gPower.GetAveV1() < gConf.GetBatVoltLowPwr()) {
uci1 12:d472f9811262 258 #ifdef DEBUG
uci1 8:95a325df1f6b 259 printf("chaing to low power!\r\n");
uci1 12:d472f9811262 260 #endif
uci1 8:95a325df1f6b 261 gConf.ChangeToLowPower();
uci1 8:95a325df1f6b 262 changed = true;
uci1 8:95a325df1f6b 263 }
uci1 8:95a325df1f6b 264 }
uci1 8:95a325df1f6b 265 if (changed) {
uci1 8:95a325df1f6b 266 SetPower(isCommWin);
uci1 12:d472f9811262 267 #ifdef DEBUG
uci1 8:95a325df1f6b 268 printf("Using config %s\r\n",gConf.GetLabel());
uci1 12:d472f9811262 269 #endif
uci1 8:95a325df1f6b 270 SetConfigAndMakeOutputFile(); // setup defaults in case no communication
uci1 8:95a325df1f6b 271 }
uci1 8:95a325df1f6b 272 // checking done
uci1 8:95a325df1f6b 273 gCheckPower = false;
uci1 8:95a325df1f6b 274 }
uci1 8:95a325df1f6b 275
uci1 10:3c93db1cfb12 276 void ResetCountersClearEvt() {
uci1 12:d472f9811262 277 const uint32_t evtStartCurSeq = (SnSDUtils::GetCurSeqNum()) // no -1; start with seq=0
uci1 12:d472f9811262 278 * gConf.GetEvtsPerFile();
uci1 10:3c93db1cfb12 279 gEvent.ClearEvent();
uci1 12:d472f9811262 280 gEvtNum = gConf.GetFirstEvt() + evtStartCurSeq;
uci1 12:d472f9811262 281 gPowNum = evtStartCurSeq;
uci1 10:3c93db1cfb12 282 memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
uci1 10:3c93db1cfb12 283 gLastCountReset = static_cast<uint32_t>(time(0));
uci1 10:3c93db1cfb12 284 }
uci1 10:3c93db1cfb12 285
uci1 10:3c93db1cfb12 286 void GetRates(float& thmrate, float& evtrate) {
uci1 10:3c93db1cfb12 287 thmrate = evtrate = 0;
uci1 10:3c93db1cfb12 288 const uint32_t dt = static_cast<uint32_t>(time(0)) - gLastCountReset;
uci1 10:3c93db1cfb12 289 if (dt>0) {
uci1 10:3c93db1cfb12 290 thmrate = gTrgNum[kThmTrg] / dt;
uci1 10:3c93db1cfb12 291 evtrate = (gEvtNum + gConf.GetFirstEvt()) / dt;
uci1 10:3c93db1cfb12 292 }
uci1 10:3c93db1cfb12 293 }
uci1 10:3c93db1cfb12 294
uci1 8:95a325df1f6b 295 bool IsSeqComplete() {
uci1 12:d472f9811262 296 #ifdef DEBUG
uci1 12:d472f9811262 297 printf("IsSeqComplete: eps=%u, cntpow=%d, fe=%u, pow=%u, evt=%u, seq=%hu\r\n",
uci1 10:3c93db1cfb12 298 gConf.GetEvtsPerFile(), (int)gConf.IsCountingPowerReadings(),
uci1 12:d472f9811262 299 gConf.GetFirstEvt(), gPowNum, gEvtNum, SnSDUtils::GetCurSeqNum());
uci1 12:d472f9811262 300 #endif
uci1 8:95a325df1f6b 301 if (gConf.GetEvtsPerFile()>0) {
uci1 12:d472f9811262 302 const uint32_t evtEndCurSeq = (SnSDUtils::GetCurSeqNum()+1) // account for seq=0
uci1 12:d472f9811262 303 * gConf.GetEvtsPerFile();
uci1 12:d472f9811262 304 #ifdef DEBUG
uci1 12:d472f9811262 305 printf("evtEndCurSeq=%u\r\n",evtEndCurSeq);
uci1 12:d472f9811262 306 #endif
uci1 8:95a325df1f6b 307 if (gConf.IsCountingPowerReadings()) {
uci1 12:d472f9811262 308 return (gPowNum>=evtEndCurSeq);
uci1 8:95a325df1f6b 309 } else {
uci1 12:d472f9811262 310 // first event num is a one-time per run offset, not one per sequence
uci1 12:d472f9811262 311 return (gEvtNum>=(gConf.GetFirstEvt()+evtEndCurSeq));
uci1 8:95a325df1f6b 312 }
uci1 12:d472f9811262 313 } else {
uci1 12:d472f9811262 314 return false;
uci1 8:95a325df1f6b 315 }
uci1 8:95a325df1f6b 316 }
uci1 1:e392595b4b76 317
uci1 8:95a325df1f6b 318 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 319 void stopTicker(rtos::RtosTimer* tik) {
uci1 8:95a325df1f6b 320 if (tik!=0) {
uci1 8:95a325df1f6b 321 tik->stop();
uci1 8:95a325df1f6b 322 }
uci1 8:95a325df1f6b 323 }
uci1 8:95a325df1f6b 324 #else
uci1 8:95a325df1f6b 325 void stopTicker(Ticker& tik) {
uci1 8:95a325df1f6b 326 tik.detach();
uci1 8:95a325df1f6b 327 }
uci1 8:95a325df1f6b 328 #endif
uci1 8:95a325df1f6b 329
uci1 8:95a325df1f6b 330 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 331 uint32_t resetTicker(rtos::RtosTimer* tik, const uint32_t timSec,
uci1 8:95a325df1f6b 332 const uint32_t maxTimSec) {
uci1 8:95a325df1f6b 333 if (tik!=0) {
uci1 8:95a325df1f6b 334 tik->stop();
uci1 8:95a325df1f6b 335 if (timSec>0) {
uci1 8:95a325df1f6b 336 uint32_t tp = timSec > maxTimSec ? maxTimSec : timSec;
uci1 8:95a325df1f6b 337 tp *= 1000u; // ms
uci1 8:95a325df1f6b 338 tik->start(tp);
uci1 8:95a325df1f6b 339 return tp;
uci1 8:95a325df1f6b 340 }
uci1 8:95a325df1f6b 341 }
uci1 8:95a325df1f6b 342 return 0;
uci1 8:95a325df1f6b 343 }
uci1 8:95a325df1f6b 344 #else
uci1 8:95a325df1f6b 345 uint32_t resetTicker(Ticker& tik, const uint32_t timSec,
uci1 8:95a325df1f6b 346 const uint32_t maxTimSec, void (*fptr)(void)) {
uci1 8:95a325df1f6b 347 tik.detach();
uci1 8:95a325df1f6b 348 if (timSec>0) {
uci1 8:95a325df1f6b 349 const uint32_t tp = timSec > maxTimSec ? maxTimSec : timSec;
uci1 8:95a325df1f6b 350 tik.attach(fptr, tp);
uci1 8:95a325df1f6b 351 return tp;
uci1 8:95a325df1f6b 352 }
uci1 8:95a325df1f6b 353 return 0;
uci1 8:95a325df1f6b 354 }
uci1 8:95a325df1f6b 355 #endif
uci1 8:95a325df1f6b 356
uci1 8:95a325df1f6b 357 void StopRunning() {
uci1 12:d472f9811262 358 #ifdef DEBUG
uci1 10:3c93db1cfb12 359 printf("stop running\r\n");
uci1 12:d472f9811262 360 #endif
uci1 8:95a325df1f6b 361 stopTicker(gForceTicker);
uci1 8:95a325df1f6b 362 stopTicker(gHeartbeatTicker);
uci1 8:95a325df1f6b 363 stopTicker(gCommWinTicker);
uci1 8:95a325df1f6b 364 stopTicker(gPowerCheckTicker);
uci1 8:95a325df1f6b 365 while (true) {
uci1 8:95a325df1f6b 366 led3 = 1; led4=1;
uci1 8:95a325df1f6b 367 wait(0.5);
uci1 8:95a325df1f6b 368 led3 = 0; led4=0;
uci1 8:95a325df1f6b 369 wait(0.5);
uci1 8:95a325df1f6b 370 //Watchdog::kick(); - if we kick the watchdog, the station is unrecoverable without physical access
uci1 8:95a325df1f6b 371 }
uci1 8:95a325df1f6b 372 }
uci1 1:e392595b4b76 373
uci1 0:664899e0b988 374 int main() {
uci1 1:e392595b4b76 375 {
uci1 2:e67f7c158087 376 led1=1; wait(0.2);
uci1 2:e67f7c158087 377 led1=0; led2=1; wait(0.2);
uci1 2:e67f7c158087 378 led2=0; led3=1; wait(0.2);
uci1 2:e67f7c158087 379 led3=0; led4=1; wait(0.2);
uci1 1:e392595b4b76 380 led4=0;
uci1 1:e392595b4b76 381 }
uci1 1:e392595b4b76 382
uci1 8:95a325df1f6b 383 // RTOS stuff must be made inside main for some reason
uci1 8:95a325df1f6b 384 gComms[0] = new SnCommAfarTCP(gConf);
uci1 8:95a325df1f6b 385 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 386 gForceTicker = new rtos::RtosTimer(&procForceTrigger);
uci1 8:95a325df1f6b 387 gHeartbeatTicker = new rtos::RtosTimer(&procHeartbeat);
uci1 8:95a325df1f6b 388 gCommWinTicker = new rtos::RtosTimer(&procCommWin);
uci1 8:95a325df1f6b 389 gPowerCheckTicker = new rtos::RtosTimer(&procPowerCheck);
uci1 8:95a325df1f6b 390 #endif
uci1 8:95a325df1f6b 391
uci1 0:664899e0b988 392 led2=1;
uci1 0:664899e0b988 393 //wait_ms(100);
uci1 1:e392595b4b76 394
uci1 12:d472f9811262 395 #ifdef DEBUG
uci1 3:24c5f0f50bf1 396 printf("\n\n\n\n\n\nstarting\r\n");
uci1 12:d472f9811262 397 #endif
uci1 1:e392595b4b76 398
uci1 0:664899e0b988 399 // a failsafe
uci1 0:664899e0b988 400 Watchdog::kick(kWDFailsafe);
uci1 0:664899e0b988 401
uci1 1:e392595b4b76 402 // set the clock to the BS time, if it's not set
uci1 1:e392595b4b76 403 if ( (static_cast<int32_t>(time(0)))<0 ) {
uci1 1:e392595b4b76 404 set_time(kBStime);
uci1 1:e392595b4b76 405 }
uci1 12:d472f9811262 406 #ifdef DEBUG
uci1 1:e392595b4b76 407 printf("time = %d\r\n",(int32_t)time(0));
uci1 12:d472f9811262 408 #endif
uci1 1:e392595b4b76 409 gLastCommWin = time(0); // prevent comm win proc
uci1 0:664899e0b988 410
uci1 8:95a325df1f6b 411 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 412 gForceTicker->stop();
uci1 8:95a325df1f6b 413 #else
uci1 0:664899e0b988 414 gForceTicker.detach();
uci1 8:95a325df1f6b 415 #endif
uci1 0:664899e0b988 416 gFirstEvt = true;
uci1 0:664899e0b988 417
uci1 4:a91682e19d6b 418 // (probably) power down comms and power up cards,amps
uci1 4:a91682e19d6b 419 SetPower(false);
uci1 4:a91682e19d6b 420
uci1 12:d472f9811262 421 #ifdef DEBUG
uci1 3:24c5f0f50bf1 422 printf("Using config %s\r\n",gConf.GetLabel());
uci1 12:d472f9811262 423 #endif
uci1 1:e392595b4b76 424 SetConfigAndMakeOutputFile(); // setup defaults in case no communication
uci1 4:a91682e19d6b 425
uci1 0:664899e0b988 426 //
uci1 0:664899e0b988 427 // get config
uci1 0:664899e0b988 428 //
uci1 12:d472f9811262 429 #ifdef DEBUG
uci1 8:95a325df1f6b 430 printf("call OpenCommWin\r\n");
uci1 12:d472f9811262 431 #endif
uci1 2:e67f7c158087 432 OpenCommWin();
uci1 3:24c5f0f50bf1 433
uci1 0:664899e0b988 434 // get ready to trigger
uci1 0:664899e0b988 435 PIN_spi.format( 16, 1 ); // change to data readout format
uci1 0:664899e0b988 436
uci1 0:664899e0b988 437 led2=0;
uci1 0:664899e0b988 438
uci1 0:664899e0b988 439 // the main event loop. wait for triggers in SendClock
uci1 12:d472f9811262 440 gTrgTimer.start();
uci1 12:d472f9811262 441 register int32_t etms=0; // time between written events
uci1 0:664899e0b988 442 while( true )
uci1 0:664899e0b988 443 {
uci1 0:664899e0b988 444 // in here, we wait for triggers from the MB-FPGA
uci1 0:664899e0b988 445 Watchdog::kick(); // don't reset!
uci1 1:e392595b4b76 446
uci1 1:e392595b4b76 447 led1 = !led1;
uci1 1:e392595b4b76 448
uci1 12:d472f9811262 449 #ifdef DEBUG
uci1 1:e392595b4b76 450 printf("calling wait trig\r\n");
uci1 1:e392595b4b76 451 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 5:9cea89700c66 452 printf("readingout=%d\r\n",(int)gReadingOut);
uci1 12:d472f9811262 453 #endif
uci1 1:e392595b4b76 454
uci1 0:664899e0b988 455 PIN_lockRegisters = 0; // allow data to come from DFPGA
uci1 0:664899e0b988 456 WaitTrigAndSendClock();
uci1 0:664899e0b988 457 PIN_lockRegisters = 1; // block registers during readout
uci1 1:e392595b4b76 458
uci1 12:d472f9811262 459 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 460 Timer prof;
uci1 12:d472f9811262 461 prof.start();
uci1 12:d472f9811262 462 int befReadWv=0, aftReadWv=0, befSaveEvt=0, aftSaveEvt=0,
uci1 12:d472f9811262 463 befChkPow=0, aftChkPow=0, befNewSeq=0, aftNewSeq=0, endOfLoop=0;
uci1 12:d472f9811262 464 #endif
uci1 12:d472f9811262 465
uci1 1:e392595b4b76 466 if (gReadingOut) {
uci1 12:d472f9811262 467
uci1 12:d472f9811262 468
uci1 12:d472f9811262 469 const int32_t ttms = gTrgTimer.read_ms(); // time since last trigger
uci1 12:d472f9811262 470 gTrgTimer.reset(); gTrgTimer.start(); // restart trigger timer
uci1 12:d472f9811262 471 etms += ttms;
uci1 8:95a325df1f6b 472
uci1 8:95a325df1f6b 473 Watchdog::kick(); // don't reset!
uci1 8:95a325df1f6b 474
uci1 1:e392595b4b76 475 //
uci1 1:e392595b4b76 476 // got trigger. read registers to mbed and build the event
uci1 1:e392595b4b76 477 //
uci1 1:e392595b4b76 478
uci1 1:e392595b4b76 479 led4=1;
uci1 12:d472f9811262 480 #ifdef DEBUG
uci1 1:e392595b4b76 481 printf("readout\r\n");
uci1 12:d472f9811262 482 #endif
uci1 1:e392595b4b76 483
uci1 1:e392595b4b76 484 // read data & calc CRC
uci1 12:d472f9811262 485 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 486 prof.stop(); befReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 487 #endif
uci1 12:d472f9811262 488
uci1 1:e392595b4b76 489 gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit);
uci1 12:d472f9811262 490
uci1 12:d472f9811262 491 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 492 prof.stop(); aftReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 493 #endif
uci1 12:d472f9811262 494
uci1 1:e392595b4b76 495 gEvent.SetCurMbedTime();
uci1 1:e392595b4b76 496 // TODO: no way to check for external trigger?
uci1 1:e392595b4b76 497 if (gEvent.IsForcedTrg()==false) {
uci1 1:e392595b4b76 498 gEvent.SetTrgBit(kThmTrg);
uci1 1:e392595b4b76 499 gEvent.SetTrgNum((gTrgNum[kThmTrg])++);
uci1 1:e392595b4b76 500 } // else already set by procForceTrigger
uci1 1:e392595b4b76 501 // (no need to calc if we throw this event away)
uci1 1:e392595b4b76 502
uci1 1:e392595b4b76 503 Watchdog::kick(); // don't reset!
uci1 1:e392595b4b76 504
uci1 12:d472f9811262 505 #ifdef DEBUG
uci1 1:e392595b4b76 506 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 12:d472f9811262 507 #endif
uci1 1:e392595b4b76 508
uci1 1:e392595b4b76 509 if ( gEvent.IsForcedTrg() || gFirstEvt ||
uci1 1:e392595b4b76 510 (etms>gConf.GetEvtThrtlPeriodMs()) ) {
uci1 1:e392595b4b76 511
uci1 1:e392595b4b76 512 led2=1;
uci1 1:e392595b4b76 513
uci1 1:e392595b4b76 514 PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card.
uci1 1:e392595b4b76 515
uci1 12:d472f9811262 516 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 517 prof.stop(); befSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 518 #endif
uci1 12:d472f9811262 519
uci1 1:e392595b4b76 520 SaveEvent(etms);
uci1 12:d472f9811262 521 etms=0;
uci1 8:95a325df1f6b 522
uci1 12:d472f9811262 523 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 524 prof.stop(); aftSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 525 #endif
uci1 1:e392595b4b76 526 }
uci1 1:e392595b4b76 527 }
uci1 12:d472f9811262 528 #ifdef DEBUG
uci1 1:e392595b4b76 529 printf("past reading out\r\n");
uci1 12:d472f9811262 530 #endif
uci1 1:e392595b4b76 531
uci1 1:e392595b4b76 532 led4=0; led2=0;
uci1 12:d472f9811262 533
uci1 12:d472f9811262 534 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 535 prof.stop(); befChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 536 #endif
uci1 8:95a325df1f6b 537 // check the power?
uci1 8:95a325df1f6b 538 if (gCheckPower) {
uci1 12:d472f9811262 539 #ifdef DEBUG
uci1 8:95a325df1f6b 540 printf("call check power\r\n");
uci1 12:d472f9811262 541 #endif
uci1 8:95a325df1f6b 542 CheckPower(false);
uci1 8:95a325df1f6b 543 }
uci1 12:d472f9811262 544 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 545 prof.stop(); aftChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 546 #endif
uci1 8:95a325df1f6b 547
uci1 12:d472f9811262 548 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 549 prof.stop(); befNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 550 #endif
uci1 8:95a325df1f6b 551 // make new seq?
uci1 8:95a325df1f6b 552 if (IsSeqComplete()) {
uci1 12:d472f9811262 553 #ifdef DEBUG
uci1 10:3c93db1cfb12 554 printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
uci1 12:d472f9811262 555 #endif
uci1 8:95a325df1f6b 556 MakeOutputFile(gConf.IsSingleSeqRunMode());
uci1 8:95a325df1f6b 557 }
uci1 12:d472f9811262 558 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 559 prof.stop(); aftNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 560 #endif
uci1 12:d472f9811262 561
uci1 8:95a325df1f6b 562 // open comm win?
uci1 0:664899e0b988 563 if (gOpenCommWin) {
uci1 12:d472f9811262 564 #ifdef DEBUG
uci1 1:e392595b4b76 565 printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false");
uci1 12:d472f9811262 566 #endif
uci1 0:664899e0b988 567 OpenCommWin();
uci1 0:664899e0b988 568 gOpenCommWin=false;
uci1 1:e392595b4b76 569 } else {
uci1 12:d472f9811262 570 #ifdef DEBUG
uci1 1:e392595b4b76 571 printf("gOpenCommWin=false\r\n");
uci1 12:d472f9811262 572 #endif
uci1 0:664899e0b988 573 }
uci1 12:d472f9811262 574
uci1 12:d472f9811262 575 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 576 prof.stop(); endOfLoop=prof.read_us(); prof.start();
uci1 12:d472f9811262 577 printf("befReadWv=%d, aftReadWv=%d, befSaveEvt=%d, aftSaveEvt=%d, "
uci1 12:d472f9811262 578 "befChkPow=%d, aftChkPow=%d, befNewSeq=%d, aftNewSeq=%d, endOfLoop=%d\r\n",
uci1 12:d472f9811262 579 befReadWv, aftReadWv, befSaveEvt, aftSaveEvt,
uci1 12:d472f9811262 580 befChkPow, aftChkPow, befNewSeq, aftNewSeq, endOfLoop);
uci1 12:d472f9811262 581 #endif
uci1 0:664899e0b988 582 }
uci1 0:664899e0b988 583
uci1 0:664899e0b988 584 }
uci1 0:664899e0b988 585
uci1 0:664899e0b988 586 //
uci1 0:664899e0b988 587 // save the event
uci1 0:664899e0b988 588 //
uci1 0:664899e0b988 589 void SaveEvent(const int32_t etms) {
uci1 0:664899e0b988 590 // write the event
uci1 12:d472f9811262 591
uci1 12:d472f9811262 592 #ifdef DEBUG
uci1 3:24c5f0f50bf1 593 printf("save event\r\n");
uci1 12:d472f9811262 594 #endif
uci1 3:24c5f0f50bf1 595
uci1 0:664899e0b988 596 // set the event number & dt
uci1 3:24c5f0f50bf1 597 gEvent.SetEvtNum(gEvtNum);
uci1 0:664899e0b988 598 gEvent.SetDTms(etms);
uci1 0:664899e0b988 599
uci1 0:664899e0b988 600 // save to SD
uci1 11:de443350ec4a 601 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 1:e392595b4b76 602 SnSDUtils::WriteEventTo(SnSDUtils::GetCurFile(), gGenBuf, gEvent, gConf);
uci1 0:664899e0b988 603
uci1 0:664899e0b988 604 // reset
uci1 0:664899e0b988 605 gEvent.ClearEvent();
uci1 0:664899e0b988 606
uci1 3:24c5f0f50bf1 607 // increment event number
uci1 3:24c5f0f50bf1 608 ++gEvtNum;
uci1 3:24c5f0f50bf1 609
uci1 12:d472f9811262 610 #ifdef DEBUG
uci1 8:95a325df1f6b 611 printf("gEvtNum=%u\r\n",gEvtNum);
uci1 12:d472f9811262 612 #endif
uci1 3:24c5f0f50bf1 613 }
uci1 3:24c5f0f50bf1 614
uci1 3:24c5f0f50bf1 615 void MakeOutputFile(const bool stopRunning) {
uci1 10:3c93db1cfb12 616 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 12:d472f9811262 617 #ifdef DEBUG
uci1 10:3c93db1cfb12 618 printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
uci1 10:3c93db1cfb12 619 gEvtNum,gPowNum,(int)stopRunning);
uci1 12:d472f9811262 620 #endif
uci1 3:24c5f0f50bf1 621 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 12:d472f9811262 622 #ifdef DEBUG
uci1 10:3c93db1cfb12 623 printf("file closed\r\n");
uci1 12:d472f9811262 624 #endif
uci1 3:24c5f0f50bf1 625 if (stopRunning) {
uci1 8:95a325df1f6b 626 StopRunning();
uci1 0:664899e0b988 627 }
uci1 8:95a325df1f6b 628 FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(),
uci1 8:95a325df1f6b 629 gConf.GetRun());
uci1 8:95a325df1f6b 630 if (cf!=0) {
uci1 8:95a325df1f6b 631 wait_ms(200);
uci1 8:95a325df1f6b 632 GetAvePowerReading();
uci1 12:d472f9811262 633 #ifdef DEBUG
uci1 8:95a325df1f6b 634 printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
uci1 8:95a325df1f6b 635 gPower.GetAveV1(), gPower.GetAveV2(),
uci1 8:95a325df1f6b 636 gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
uci1 8:95a325df1f6b 637 gPowNum);
uci1 12:d472f9811262 638 #endif
uci1 8:95a325df1f6b 639 SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
uci1 8:95a325df1f6b 640 }
uci1 12:d472f9811262 641 #ifdef DEBUG
uci1 3:24c5f0f50bf1 642 printf("made output file with run %u\r\n",gConf.GetRun());
uci1 3:24c5f0f50bf1 643 printf("filename=%s\r\n",SnSDUtils::GetCurFileName());
uci1 12:d472f9811262 644 #endif
uci1 3:24c5f0f50bf1 645 SnSDUtils::WriteConfig(SnSDUtils::GetCurFile(), gConf);
uci1 0:664899e0b988 646 }
uci1 0:664899e0b988 647
uci1 0:664899e0b988 648 //
uci1 4:a91682e19d6b 649 // power stuff
uci1 4:a91682e19d6b 650 //
uci1 4:a91682e19d6b 651 void SetPower(const bool isCommWin) {
uci1 8:95a325df1f6b 652 // TODO: turn on amps individually, when that's possible
uci1 4:a91682e19d6b 653 if (isCommWin) {
uci1 5:9cea89700c66 654 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardComWin);
uci1 4:a91682e19d6b 655 wait_ms(10);
uci1 5:9cea89700c66 656 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsComWin);
uci1 4:a91682e19d6b 657 wait_ms(10);
uci1 5:9cea89700c66 658 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridComWin);
uci1 4:a91682e19d6b 659 wait_ms(10);
uci1 5:9cea89700c66 660 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin);
uci1 4:a91682e19d6b 661 wait_ms(10);
uci1 4:a91682e19d6b 662 } else {
uci1 5:9cea89700c66 663 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardDatTak);
uci1 4:a91682e19d6b 664 wait_ms(10);
uci1 5:9cea89700c66 665 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsDatTak);
uci1 4:a91682e19d6b 666 wait_ms(10);
uci1 5:9cea89700c66 667 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridDatTak);
uci1 4:a91682e19d6b 668 wait_ms(10);
uci1 5:9cea89700c66 669 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak);
uci1 4:a91682e19d6b 670 wait_ms(10);
uci1 4:a91682e19d6b 671 }
uci1 8:95a325df1f6b 672 wait(1);
uci1 12:d472f9811262 673 #ifdef DEBUG
uci1 6:6f002d202f59 674 printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 675 isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 676 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 12:d472f9811262 677 #endif
uci1 4:a91682e19d6b 678 }
uci1 4:a91682e19d6b 679
uci1 4:a91682e19d6b 680 //
uci1 0:664899e0b988 681 // set configuration
uci1 0:664899e0b988 682 //
uci1 1:e392595b4b76 683 void SetConfigAndMakeOutputFile() {
uci1 12:d472f9811262 684 #ifdef DEBUG
uci1 1:e392595b4b76 685 printf("SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 686 #endif
uci1 1:e392595b4b76 687
uci1 0:664899e0b988 688 // restart watchdog
uci1 0:664899e0b988 689 Watchdog::kick(gConf.GetWatchdogPeriod());
uci1 0:664899e0b988 690
uci1 1:e392595b4b76 691 // block (thermal) triggers during configuration
uci1 1:e392595b4b76 692 PIN_enableThermTrig = 0;
uci1 1:e392595b4b76 693 PIN_ADC_CS = 1;
uci1 1:e392595b4b76 694 PIN_DoNotRestartAllClocks = 1;
uci1 1:e392595b4b76 695 PIN_forceTrigger = 0;
uci1 3:24c5f0f50bf1 696 PIN_heartbeat = 0;
uci1 1:e392595b4b76 697 wait_ms(20);
uci1 1:e392595b4b76 698
uci1 0:664899e0b988 699 // reset event, timers, trigger counters
uci1 10:3c93db1cfb12 700 ResetCountersClearEvt();
uci1 0:664899e0b988 701
uci1 8:95a325df1f6b 702 if (AreCardsPowered()) {
uci1 8:95a325df1f6b 703 // Set PLA value(s)
uci1 8:95a325df1f6b 704 PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting
uci1 8:95a325df1f6b 705 PIN_spi.frequency(1000000);
uci1 8:95a325df1f6b 706 PIN_MajLogHiBit=1;
uci1 8:95a325df1f6b 707 PIN_MajLogLoBit=1;
uci1 8:95a325df1f6b 708 PIN_enableThermTrig=0;
uci1 0:664899e0b988 709
uci1 8:95a325df1f6b 710 uint16_t hi, lo;
uci1 8:95a325df1f6b 711 PIN_PLA_cs=1;
uci1 8:95a325df1f6b 712 wait(4);
uci1 8:95a325df1f6b 713 for (uint8_t pi=0; pi<kNplas; pi++) {
uci1 8:95a325df1f6b 714 if (pi < gConf.GetNumPlas()) {
uci1 8:95a325df1f6b 715 SnConfigFrame::GetHiLoPlas(gConf.GetPla(pi), hi, lo);
uci1 8:95a325df1f6b 716 PIN_spi.write(hi);
uci1 8:95a325df1f6b 717 PIN_spi.write(lo);
uci1 12:d472f9811262 718 #ifdef DEBUG
uci1 8:95a325df1f6b 719 printf("pla hi %hu, lo %hu\r\n",hi,lo);
uci1 12:d472f9811262 720 #endif
uci1 8:95a325df1f6b 721 } else {
uci1 8:95a325df1f6b 722 PIN_spi.write(kNoTrigPla); // hi
uci1 8:95a325df1f6b 723 PIN_spi.write(kNoTrigPla); // lo
uci1 12:d472f9811262 724 #ifdef DEBUG
uci1 8:95a325df1f6b 725 printf("pla hi %hu, lo %hu\r\n",kNoTrigPla,kNoTrigPla);
uci1 12:d472f9811262 726 #endif
uci1 8:95a325df1f6b 727 }
uci1 8:95a325df1f6b 728 Watchdog::kick();
uci1 0:664899e0b988 729 }
uci1 8:95a325df1f6b 730 wait(3);
uci1 8:95a325df1f6b 731 PIN_PLA_cs=0;
uci1 8:95a325df1f6b 732 wait(3);
uci1 0:664899e0b988 733
uci1 8:95a325df1f6b 734 // DAC values
uci1 8:95a325df1f6b 735 //
uci1 8:95a325df1f6b 736 // first 12 bits = DAC value
uci1 8:95a325df1f6b 737 // next 2 bits = DAC ID
uci1 8:95a325df1f6b 738 // last 2 bits = dFPGA ID
uci1 8:95a325df1f6b 739 //
uci1 8:95a325df1f6b 740 // But FPGA uses "gray encoding" which means only 1 bit
uci1 8:95a325df1f6b 741 // can change at a time (of the last 4 bits). So even tho
uci1 8:95a325df1f6b 742 // the card/dac# is encoded, the order is also important
uci1 8:95a325df1f6b 743 // 0000 (dac0,card0), 0001 (dac0,card1), 0011 (dac0,card3), 0010 (dac0,card2),
uci1 8:95a325df1f6b 744 // 0110 (dac1,card2), 0111 (dac1,card3), 0101 (dac1,card1), etc.
uci1 12:d472f9811262 745 #ifdef DEBUG
uci1 8:95a325df1f6b 746 printf("setting dacs\r\n");
uci1 12:d472f9811262 747 #endif
uci1 8:95a325df1f6b 748 uint16_t dv=0;
uci1 8:95a325df1f6b 749 for (uint8_t i=0, gri=0; i<kTotDacs; i++) {
uci1 8:95a325df1f6b 750 // get the gray-codes for this iteration
uci1 8:95a325df1f6b 751 gri = SnBitUtils::binToGray(i);
uci1 8:95a325df1f6b 752
uci1 8:95a325df1f6b 753 // build bit word
uci1 8:95a325df1f6b 754 dv = static_cast<int>(gConf.GetDac(gri & 0x0003u, gri >> 2u));
uci1 8:95a325df1f6b 755 dv <<= 4u;
uci1 8:95a325df1f6b 756 dv |= gri;
uci1 8:95a325df1f6b 757
uci1 12:d472f9811262 758 #ifdef DEBUG
uci1 8:95a325df1f6b 759 printf("dac %04x\r\n",dv);
uci1 12:d472f9811262 760 #endif
uci1 8:95a325df1f6b 761
uci1 8:95a325df1f6b 762 // send to FPGA
uci1 8:95a325df1f6b 763 PIN_start_fpga=1;
uci1 8:95a325df1f6b 764 PIN_spi.write(dv);
uci1 8:95a325df1f6b 765 PIN_start_fpga=0;
uci1 8:95a325df1f6b 766
uci1 8:95a325df1f6b 767 Watchdog::kick();
uci1 8:95a325df1f6b 768
uci1 8:95a325df1f6b 769 }
uci1 12:d472f9811262 770 #ifdef DEBUG
uci1 8:95a325df1f6b 771 printf("dacs set\r\n");
uci1 12:d472f9811262 772 #endif
uci1 8:95a325df1f6b 773 wait_ms(20);
uci1 8:95a325df1f6b 774 } else {
uci1 12:d472f9811262 775 #ifdef DEBUG
uci1 8:95a325df1f6b 776 printf("cards off. skipping PLA and DAC setting\r\n");
uci1 12:d472f9811262 777 #endif
uci1 0:664899e0b988 778 }
uci1 0:664899e0b988 779
uci1 0:664899e0b988 780 // Majority Logic Trigger selection (# of cards)
uci1 0:664899e0b988 781 SnBitUtils::SetChanNumBits(gConf.GetNumCardsMajLog() - 1u,
uci1 0:664899e0b988 782 PIN_MajLogHiBit, PIN_MajLogLoBit);
uci1 0:664899e0b988 783
uci1 0:664899e0b988 784 // Enable thermal trigger?
uci1 0:664899e0b988 785 PIN_enableThermTrig = gConf.IsThermTrigEnabled();
uci1 0:664899e0b988 786
uci1 0:664899e0b988 787 PIN_spi.format( 16, 1 ); // back to trigger mode
uci1 1:e392595b4b76 788 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 1:e392595b4b76 789
uci1 8:95a325df1f6b 790 // make new output file
uci1 8:95a325df1f6b 791 // put after PLA/DAC, in case they affect the power readings
uci1 8:95a325df1f6b 792 wait_ms(200);
uci1 8:95a325df1f6b 793 MakeOutputFile();
uci1 8:95a325df1f6b 794
uci1 1:e392595b4b76 795 // force a trigger every...
uci1 8:95a325df1f6b 796 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 797 const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
uci1 8:95a325df1f6b 798 kAbsMaxTimer);
uci1 8:95a325df1f6b 799 const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
uci1 8:95a325df1f6b 800 kAbsMaxTimer);
uci1 8:95a325df1f6b 801 const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
uci1 8:95a325df1f6b 802 kCommWinLongPrdTk);
uci1 8:95a325df1f6b 803 const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
uci1 8:95a325df1f6b 804 kAbsMaxTimer);
uci1 8:95a325df1f6b 805 #else
uci1 8:95a325df1f6b 806 const uint32_t ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
uci1 8:95a325df1f6b 807 kAbsMaxTimer, &procForceTrigger);
uci1 8:95a325df1f6b 808 const uint32_t hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
uci1 8:95a325df1f6b 809 kAbsMaxTimer, &procHeartbeat);
uci1 8:95a325df1f6b 810 const uint32_t cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
uci1 8:95a325df1f6b 811 kCommWinLongPrdTk, &procCommWin);
uci1 8:95a325df1f6b 812 const uint32_t pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
uci1 8:95a325df1f6b 813 kAbsMaxTimer, &procPowerCheck);
uci1 8:95a325df1f6b 814 #endif
uci1 12:d472f9811262 815 #ifdef DEBUG
uci1 8:95a325df1f6b 816 printf("attach force trig %u\r\n",ftp);
uci1 8:95a325df1f6b 817 printf("attach heart beat %u\r\n",hbp);
uci1 8:95a325df1f6b 818 printf("attach comm win %u\r\n",cwp);
uci1 8:95a325df1f6b 819 printf("attach power chk %u\r\n",pcp);
uci1 12:d472f9811262 820 #endif
uci1 8:95a325df1f6b 821
uci1 8:95a325df1f6b 822 // TODO: change comm parameters
uci1 8:95a325df1f6b 823 /*
uci1 8:95a325df1f6b 824 printf("set comm params\r\n");
uci1 8:95a325df1f6b 825 for (uint8_t cc=0; cc<kNcomms; cc++) {
uci1 8:95a325df1f6b 826 if (gComms[cc]!=0) {
uci1 8:95a325df1f6b 827 gComms[cc]->Set(gConf);
uci1 8:95a325df1f6b 828 }
uci1 3:24c5f0f50bf1 829 }
uci1 8:95a325df1f6b 830 */
uci1 0:664899e0b988 831
uci1 0:664899e0b988 832 Watchdog::kick(); // don't reset!
uci1 8:95a325df1f6b 833
uci1 12:d472f9811262 834 #ifdef DEBUG
uci1 8:95a325df1f6b 835 printf("set config done\r\n");
uci1 12:d472f9811262 836 #endif
uci1 0:664899e0b988 837 }
uci1 0:664899e0b988 838
uci1 0:664899e0b988 839 //
uci1 0:664899e0b988 840 // readout functions
uci1 0:664899e0b988 841 //
uci1 0:664899e0b988 842 void WaitTrigAndSendClock() {
uci1 1:e392595b4b76 843
uci1 12:d472f9811262 844 #ifdef DEBUG
uci1 1:e392595b4b76 845 printf("WaitTrigAndSendClock\r\n");
uci1 6:6f002d202f59 846 printf("wait trig: (pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 847 gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 848 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 8:95a325df1f6b 849 printf("cards powered=%d\r\n",(int)AreCardsPowered());
uci1 12:d472f9811262 850 #endif
uci1 0:664899e0b988 851
uci1 8:95a325df1f6b 852 if (AreCardsPowered()) {
uci1 8:95a325df1f6b 853
uci1 8:95a325df1f6b 854 if (gFirstEvt==false) {
uci1 8:95a325df1f6b 855 PIN_DoNotRestartAllClocks = 0;
uci1 8:95a325df1f6b 856 wait_us(1);
uci1 8:95a325df1f6b 857 PIN_DoNotRestartAllClocks = 1;
uci1 8:95a325df1f6b 858 //led3 = !led3; // toggle send clock led
uci1 8:95a325df1f6b 859 } else {
uci1 8:95a325df1f6b 860 gFirstEvt = false;
uci1 8:95a325df1f6b 861 }
uci1 8:95a325df1f6b 862
uci1 8:95a325df1f6b 863 //
uci1 8:95a325df1f6b 864 // wait for a trigger here.
uci1 8:95a325df1f6b 865 //
uci1 12:d472f9811262 866 #ifdef DEBUG
uci1 8:95a325df1f6b 867 printf("starting wait for trig\r\n");
uci1 12:d472f9811262 868 #endif
uci1 8:95a325df1f6b 869 gReadingOut = false; // this will allow forced triggers (see procForceTrigger())
uci1 8:95a325df1f6b 870 while ( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 871 if (gOpenCommWin || gCheckPower) {
uci1 12:d472f9811262 872 #ifdef DEBUG
uci1 8:95a325df1f6b 873 printf("break com=%d, pow=%d\r\n",gOpenCommWin,gCheckPower);
uci1 12:d472f9811262 874 #endif
uci1 8:95a325df1f6b 875 return; // break out to open comms or check power
uci1 8:95a325df1f6b 876 }
uci1 0:664899e0b988 877 }
uci1 12:d472f9811262 878 #ifdef DEBUG
uci1 8:95a325df1f6b 879 printf("starting readout. force=%d, clk=%d\r\n",
uci1 8:95a325df1f6b 880 PIN_forceTrigger.read(), PIN_a_sf_clk.read());
uci1 12:d472f9811262 881 #endif
uci1 8:95a325df1f6b 882 PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers
uci1 8:95a325df1f6b 883 gReadingOut = true; // disallow new forced triggers
uci1 8:95a325df1f6b 884 //
uci1 8:95a325df1f6b 885 // collect data from daughter cards
uci1 8:95a325df1f6b 886 //
uci1 8:95a325df1f6b 887 // TODO: what if some card (set of channels) doesn't respond?
uci1 8:95a325df1f6b 888 // currently, will wait forever?
uci1 8:95a325df1f6b 889 // also, if ch1 is dead, will wait forever (due to FPGA code)
uci1 8:95a325df1f6b 890 for( uint8_t i = 0; i < kNsamps; i++ ) {
uci1 8:95a325df1f6b 891 if( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 892 if( i == 0 )
uci1 8:95a325df1f6b 893 wait_us( 1 );
uci1 8:95a325df1f6b 894
uci1 8:95a325df1f6b 895 PIN_ADC_CS = 0;
uci1 8:95a325df1f6b 896 PIN_spi.write( 0x00 );
uci1 8:95a325df1f6b 897 PIN_ADC_CS = 1;
uci1 8:95a325df1f6b 898 } else {
uci1 8:95a325df1f6b 899 i--;
uci1 8:95a325df1f6b 900 }
uci1 0:664899e0b988 901 }
uci1 8:95a325df1f6b 902 } else {
uci1 8:95a325df1f6b 903 // cards have no power. don't try reading out
uci1 8:95a325df1f6b 904 gReadingOut=false;
uci1 0:664899e0b988 905 }
uci1 0:664899e0b988 906 }
uci1 0:664899e0b988 907
uci1 0:664899e0b988 908 SnCommWin::ECommWinResult OpenCommWin() {
uci1 0:664899e0b988 909 // loop through each comm mode:
uci1 0:664899e0b988 910 // a) try to connect
uci1 0:664899e0b988 911 // b) if connected, listen for config
uci1 0:664899e0b988 912 // c) if config requests data, send it
uci1 3:24c5f0f50bf1 913
uci1 3:24c5f0f50bf1 914 gLastCommWin = time(0);
uci1 3:24c5f0f50bf1 915 if (gConf.GetCommWinDuration()==0) {
uci1 3:24c5f0f50bf1 916 // TODO: set min so this is not possible
uci1 3:24c5f0f50bf1 917 return SnCommWin::kOkNoMsg;
uci1 3:24c5f0f50bf1 918 }
uci1 0:664899e0b988 919
uci1 0:664899e0b988 920 gCommWinOpen = true;
uci1 0:664899e0b988 921 Watchdog::kick(); // don't reset!
uci1 0:664899e0b988 922
uci1 12:d472f9811262 923 #ifdef DEBUG
uci1 3:24c5f0f50bf1 924 printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
uci1 12:d472f9811262 925 #endif
uci1 3:24c5f0f50bf1 926
uci1 3:24c5f0f50bf1 927 // close the file so that the data is all written out.
uci1 3:24c5f0f50bf1 928 // and open it back up at the beginning (for reading)
uci1 12:d472f9811262 929 #ifdef DEBUG
uci1 8:95a325df1f6b 930 printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
uci1 12:d472f9811262 931 #endif
uci1 10:3c93db1cfb12 932 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 3:24c5f0f50bf1 933 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 3:24c5f0f50bf1 934 SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
uci1 0:664899e0b988 935
uci1 4:a91682e19d6b 936 // (probably) power down cards,amps and power up comms
uci1 4:a91682e19d6b 937 SetPower(true);
uci1 0:664899e0b988 938
uci1 0:664899e0b988 939 const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ?
uci1 0:664899e0b988 940 gConf.GetCommWinDuration() : kConnectTimeout;
uci1 0:664899e0b988 941 const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ?
uci1 0:664899e0b988 942 gConf.GetCommWinDuration() : kListenTimeout;
uci1 0:664899e0b988 943
uci1 0:664899e0b988 944 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 0:664899e0b988 945
uci1 1:e392595b4b76 946 bool gotNewConfig=false;
uci1 1:e392595b4b76 947 bool sendStat[kNcomms];
uci1 1:e392595b4b76 948 for (uint8_t i=0; i<kNcomms; i++) {
uci1 1:e392595b4b76 949 sendStat[i]=true;
uci1 1:e392595b4b76 950 }
uci1 1:e392595b4b76 951 bool* ss = sendStat;
uci1 0:664899e0b988 952 SnCommWin** cw = gComms;
uci1 1:e392595b4b76 953 for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
uci1 1:e392595b4b76 954 Watchdog::kick(); // don't reset!
uci1 1:e392595b4b76 955 if (i==kNcomms) {
uci1 1:e392595b4b76 956 i=0;
uci1 1:e392595b4b76 957 cw = gComms;
uci1 1:e392595b4b76 958 ss = sendStat;
uci1 1:e392595b4b76 959 }
uci1 7:079617408fec 960 if ((*cw)==0) {
uci1 7:079617408fec 961 continue;
uci1 7:079617408fec 962 }
uci1 0:664899e0b988 963 // open window and (mabye) send status update
uci1 12:d472f9811262 964 #ifdef DEBUG
uci1 1:e392595b4b76 965 printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
uci1 1:e392595b4b76 966 printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",GetTimeoutTime(gLastCommWin,conto),
uci1 1:e392595b4b76 967 time(0), gLastCommWin, gConf.GetCommWinDuration());
uci1 12:d472f9811262 968 #endif
uci1 8:95a325df1f6b 969 // update power reading in case we want to send it in status
uci1 8:95a325df1f6b 970 GetAvePowerReading();
uci1 10:3c93db1cfb12 971 // get the trigger rates
uci1 10:3c93db1cfb12 972 float thmrate=0, evtrate=0;
uci1 10:3c93db1cfb12 973 GetRates(thmrate, evtrate);
uci1 0:664899e0b988 974 const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow(
uci1 10:3c93db1cfb12 975 GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower,
uci1 10:3c93db1cfb12 976 SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
uci1 10:3c93db1cfb12 977 gGenBuf);
uci1 0:664899e0b988 978 if (conres>=SnCommWin::kConnected) {
uci1 1:e392595b4b76 979 Watchdog::kick(); // don't reset!
uci1 0:664899e0b988 980 // connected. listen for config
uci1 1:e392595b4b76 981 *ss = false; // don't send status next time
uci1 12:d472f9811262 982 #ifdef DEBUG
uci1 12:d472f9811262 983 printf("get conf gtt=%u\r\n",GetTimeoutTime(gLastCommWin, listo));
uci1 12:d472f9811262 984 #endif
uci1 0:664899e0b988 985 const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
uci1 3:24c5f0f50bf1 986 gConf, GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);
uci1 0:664899e0b988 987 if (cfgres>=SnCommWin::kOkWithMsg) {
uci1 1:e392595b4b76 988 Watchdog::kick(); // don't reset!
uci1 12:d472f9811262 989 #ifdef DEBUG
uci1 8:95a325df1f6b 990 printf("received config!\r\n");
uci1 12:d472f9811262 991 printf("send data = %d\r\n", gConf.GetCommSendData());
uci1 12:d472f9811262 992 #endif
uci1 0:664899e0b988 993 // send data if need be (files, some events, etc)
uci1 12:d472f9811262 994 const uint32_t winto = GetTimeoutTime(gLastCommWin,
uci1 12:d472f9811262 995 gConf.GetCommWinDuration());
uci1 12:d472f9811262 996 const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
uci1 0:664899e0b988 997 if (gConf.GetCommSendData()!=0) {
uci1 12:d472f9811262 998 #ifdef DEBUG
uci1 12:d472f9811262 999 printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n",
uci1 12:d472f9811262 1000 GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()),
uci1 12:d472f9811262 1001 gLastCommWin, gConf.GetCommWinDuration(),
uci1 12:d472f9811262 1002 gConf.IsObeyingTimeout() ? "true" : "false");
uci1 12:d472f9811262 1003 #endif
uci1 8:95a325df1f6b 1004 res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize,
uci1 12:d472f9811262 1005 gtt, winto);
uci1 0:664899e0b988 1006 } else {
uci1 0:664899e0b988 1007 // don't send anything
uci1 0:664899e0b988 1008 res = cfgres;
uci1 0:664899e0b988 1009 }
uci1 12:d472f9811262 1010 #ifdef DEBUG
uci1 1:e392595b4b76 1011 printf("Got config!\r\n");
uci1 12:d472f9811262 1012 #endif
uci1 12:d472f9811262 1013 gotNewConfig = (cfgres!=SnCommWin::kOkWthMsgNoConf);
uci1 1:e392595b4b76 1014 Watchdog::kick(); // don't reset!
uci1 12:d472f9811262 1015 (*cw)->CloseConn();
uci1 0:664899e0b988 1016 break;
uci1 0:664899e0b988 1017 }
uci1 0:664899e0b988 1018 }
uci1 0:664899e0b988 1019
uci1 0:664899e0b988 1020 Watchdog::kick(); // don't reset!
uci1 0:664899e0b988 1021
uci1 0:664899e0b988 1022 }
uci1 12:d472f9811262 1023
uci1 4:a91682e19d6b 1024 // (probably) power down comms and power up cards,amps
uci1 4:a91682e19d6b 1025 SetPower(false);
uci1 4:a91682e19d6b 1026
uci1 1:e392595b4b76 1027 gFirstEvt = true;
uci1 4:a91682e19d6b 1028
uci1 1:e392595b4b76 1029 // reset config with system powered (for DAC/PLA setting)
uci1 1:e392595b4b76 1030 if (gotNewConfig) {
uci1 12:d472f9811262 1031 #ifdef DEBUG
uci1 1:e392595b4b76 1032 printf("calling SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 1033 #endif
uci1 1:e392595b4b76 1034 SetConfigAndMakeOutputFile();
uci1 12:d472f9811262 1035 } else {
uci1 12:d472f9811262 1036 MakeOutputFile();
uci1 1:e392595b4b76 1037 }
uci1 12:d472f9811262 1038 #ifdef DEBUG
uci1 1:e392595b4b76 1039 printf("closing comm win at %d\r\n",(int32_t)time(0));
uci1 12:d472f9811262 1040 #endif
uci1 1:e392595b4b76 1041
uci1 0:664899e0b988 1042 gCommWinOpen = false;
uci1 0:664899e0b988 1043 return res;
uci1 0:664899e0b988 1044 }