Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 23 20:07:57 2012 +0000
Revision:
23:ccf39298f205
Parent:
22:f957c4f840ad
Child:
25:57b2627fe756
SBD communication enabled. Fix bug in which the SBD message number would be repeatedly increased upon failed sending attempts. Add check in SBD recv in case message is longer than buffer. Add SendString to SnCommWin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "mbed.h"
uci1 22:f957c4f840ad 2 // start a watchdog as soon as possible
uci1 22:f957c4f840ad 3 #include "Watchdog.h"
uci1 22:f957c4f840ad 4 Watchdog::SnKickStarter gKickStarter(WDFAILSAFE);
uci1 0:664899e0b988 5
uci1 18:55f1581f2ee4 6 //#define USE_RTOS_TIMER
uci1 18:55f1581f2ee4 7 //#define USE_ETH_INTERFACE
uci1 12:d472f9811262 8 //#define EVT_TIME_PROFILE
uci1 14:2736b57bbbed 9 //#define DEBUG
uci1 16:744ce85aede2 10 //#define SSNOTIFY
uci1 15:f2569d8e4176 11 #define USE_MODSERIAL
uci1 12:d472f9811262 12
uci1 0:664899e0b988 13 #include <stdint.h>
uci1 0:664899e0b988 14 #include "SDFileSystem.h"
uci1 15:f2569d8e4176 15 #ifdef USE_MODSERIAL
uci1 0:664899e0b988 16 #include "MODSERIAL.h"
uci1 18:55f1581f2ee4 17 #define MODSERIAL_RX_BUF_SIZE 512
uci1 18:55f1581f2ee4 18 #define MODSERIAL_TX_BUF_SIZE 512
uci1 15:f2569d8e4176 19 #endif
uci1 21:ce51bb0ba4a5 20 #include "FATDirHandle.h"
uci1 21:ce51bb0ba4a5 21 #include "EthernetPowerControl.h"
uci1 0:664899e0b988 22 #include "SnConstants.h"
uci1 0:664899e0b988 23 #include "SnBitUtils.h"
uci1 0:664899e0b988 24 #include "SnSDUtils.h"
uci1 0:664899e0b988 25 #include "SnConfigFrame.h"
uci1 0:664899e0b988 26 #include "SnEventFrame.h"
uci1 0:664899e0b988 27 #include "SnStatusFrame.h"
uci1 2:e67f7c158087 28 #include "SnHeaderFrame.h"
uci1 22:f957c4f840ad 29 #include "SnHeartbeatFrame.h"
uci1 0:664899e0b988 30 #include "SnCommWin.h"
uci1 18:55f1581f2ee4 31 #ifdef USE_ETH_INTERFACE
uci1 7:079617408fec 32 #include "SnCommAfarTCP.h"
uci1 18:55f1581f2ee4 33 #else
uci1 18:55f1581f2ee4 34 #include "SnCommAfarNetIf.h"
uci1 18:55f1581f2ee4 35 #endif
uci1 0:664899e0b988 36 #include "SnCommUsb.h"
uci1 15:f2569d8e4176 37 #include "SnCommSBD.h"
uci1 1:e392595b4b76 38 #include "SnBase64.h"
uci1 8:95a325df1f6b 39 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 40 #include "RtosTimer.h"
uci1 8:95a325df1f6b 41 #endif
uci1 0:664899e0b988 42
uci1 0:664899e0b988 43 //
uci1 0:664899e0b988 44 // MBED PINS (ordered by number)
uci1 0:664899e0b988 45 //
uci1 0:664899e0b988 46 // leds (for debugging)
uci1 0:664899e0b988 47 DigitalOut led1(LED1);
uci1 0:664899e0b988 48 DigitalOut led2(LED2);
uci1 0:664899e0b988 49 DigitalOut led3(LED3);
uci1 0:664899e0b988 50 DigitalOut led4(LED4);
uci1 0:664899e0b988 51 // Set up power pins - Note that it's Zero for "on"
uci1 1:e392595b4b76 52 DigitalOut PIN_turn_on_system(p17); // this turns on system
uci1 0:664899e0b988 53 DigitalOut PIN_turn_on_amps(p25);
uci1 15:f2569d8e4176 54 // SD card select
uci1 15:f2569d8e4176 55 DigitalOut PIN_SD_CS(p8);
uci1 0:664899e0b988 56 // Activate/select chip by falling edge
uci1 0:664899e0b988 57 DigitalOut PIN_ADC_CS( p9 );
uci1 0:664899e0b988 58 // clock signal to activate PLA setting
uci1 0:664899e0b988 59 DigitalOut PIN_PLA_cs(p10);
uci1 0:664899e0b988 60 // To force a trigger
uci1 0:664899e0b988 61 DigitalOut PIN_forceTrigger(p11); //modification
uci1 0:664899e0b988 62 // To suppress thermal triggers
uci1 0:664899e0b988 63 DigitalOut PIN_enableThermTrig(p12);
uci1 0:664899e0b988 64 // Restart clock on all FPGAs.
uci1 0:664899e0b988 65 DigitalOut PIN_DoNotRestartAllClocks( p13 );
uci1 0:664899e0b988 66 // This tells the DFPGAs to store the data on motherboard FPGA and
uci1 0:664899e0b988 67 // read it out.
uci1 0:664899e0b988 68 DigitalIn PIN_a_sf_clk( p14 );
uci1 0:664899e0b988 69 DigitalIn PIN_rst_a_sf(p15);
uci1 1:e392595b4b76 70 // afar power
uci1 1:e392595b4b76 71 DigitalOut PIN_afar_power(p16);
uci1 4:a91682e19d6b 72 // batter voltage/current measurement
uci1 4:a91682e19d6b 73 AnalogIn PIN_vADC1(p19);
uci1 4:a91682e19d6b 74 AnalogIn PIN_vADC2(p18);
uci1 0:664899e0b988 75 // Lock daughter card registeres (during data readout).
uci1 0:664899e0b988 76 DigitalOut PIN_lockRegisters( p20 );
uci1 1:e392595b4b76 77 // iridium (SBD) power
uci1 1:e392595b4b76 78 DigitalOut PIN_iridSbd_power(p21);
uci1 0:664899e0b988 79 // Majority logic pins
uci1 0:664899e0b988 80 DigitalOut PIN_MajLogHiBit(p22);
uci1 0:664899e0b988 81 DigitalOut PIN_MajLogLoBit(p23);
uci1 0:664899e0b988 82 // Tell FPGA to be ready to accept DAC values
uci1 0:664899e0b988 83 DigitalOut PIN_start_fpga(p26);
uci1 0:664899e0b988 84 // Two bits to the select the daughter card for readout
uci1 0:664899e0b988 85 DigitalOut PIN_selCardHiBit( p29 );
uci1 0:664899e0b988 86 DigitalOut PIN_selCardLoBit( p30 );
uci1 0:664899e0b988 87 // To launch a heartbeat pulse
uci1 0:664899e0b988 88 DigitalOut PIN_heartbeat(p24);
uci1 0:664899e0b988 89 // Setup SPI pins
uci1 0:664899e0b988 90 SPI PIN_spi( p5, p6, p7 );
uci1 0:664899e0b988 91 // The SD card
uci1 3:24c5f0f50bf1 92
uci1 3:24c5f0f50bf1 93 // this needs to be first in case some other global uses a print statement
uci1 15:f2569d8e4176 94 #ifdef USE_MODSERIAL
uci1 16:744ce85aede2 95 #define SERIAL_TYPE MODSERIAL
uci1 15:f2569d8e4176 96 #else
uci1 16:744ce85aede2 97 #define SERIAL_TYPE Serial
uci1 15:f2569d8e4176 98 #endif
uci1 16:744ce85aede2 99 static SERIAL_TYPE gCpu( USBTX, USBRX // defined here so it might be used for debugging output
uci1 16:744ce85aede2 100 #ifdef USE_MODSERIAL
uci1 16:744ce85aede2 101 ,MODSERIAL_TX_BUF_SIZE,
uci1 16:744ce85aede2 102 MODSERIAL_RX_BUF_SIZE
uci1 16:744ce85aede2 103 #endif
uci1 16:744ce85aede2 104 );
uci1 16:744ce85aede2 105 static SERIAL_TYPE gSBDport(p28, p27, "sbd");
uci1 21:ce51bb0ba4a5 106 static SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); // no leading '/'
uci1 21:ce51bb0ba4a5 107 static LocalFileSystem local(kLocalDir+1); // no leading '/'
uci1 0:664899e0b988 108
uci1 0:664899e0b988 109 //
uci1 0:664899e0b988 110 // fwd declare fcns
uci1 0:664899e0b988 111 //
uci1 0:664899e0b988 112 void ReadAllRegisters();
uci1 0:664899e0b988 113 void ReadRegister(const uint8_t chan, int16_t* dev);
uci1 22:f957c4f840ad 114 void SaveHeartbeat();
uci1 0:664899e0b988 115 void SaveEvent(const int32_t etms);
uci1 0:664899e0b988 116 void WaitTrigAndSendClock();
uci1 1:e392595b4b76 117 void SetConfigAndMakeOutputFile();
uci1 13:7a1fb885a8e4 118 SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig=false);
uci1 3:24c5f0f50bf1 119 void MakeOutputFile(const bool stopRunning=false);
uci1 4:a91682e19d6b 120 void SetPower(const bool isCommWin);
uci1 8:95a325df1f6b 121 void procForceTrigger();
uci1 8:95a325df1f6b 122 void procHeartbeat();
uci1 8:95a325df1f6b 123 void procPowerCheck();
uci1 8:95a325df1f6b 124 void procCommWin();
uci1 8:95a325df1f6b 125 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 126 void procForceTrigger(void const *) { return procForceTrigger(); }
uci1 8:95a325df1f6b 127 void procHeartbeat(void const *) { return procHeartbeat(); }
uci1 8:95a325df1f6b 128 void procPowerCheck(void const *) { return procPowerCheck(); }
uci1 8:95a325df1f6b 129 void procCommWin(void const *) { return procCommWin(); }
uci1 8:95a325df1f6b 130 #endif
uci1 0:664899e0b988 131
uci1 0:664899e0b988 132 //
uci1 0:664899e0b988 133 // globals
uci1 0:664899e0b988 134 //
uci1 0:664899e0b988 135 // readout objs
uci1 8:95a325df1f6b 136 // TODO: use RtosTimer instead of Ticker?
uci1 8:95a325df1f6b 137 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 138 static rtos::RtosTimer* gForceTicker;
uci1 8:95a325df1f6b 139 static rtos::RtosTimer* gHeartbeatTicker;
uci1 8:95a325df1f6b 140 static rtos::RtosTimer* gCommWinTicker;
uci1 8:95a325df1f6b 141 static rtos::RtosTimer* gPowerCheckTicker;
uci1 8:95a325df1f6b 142 #else
uci1 0:664899e0b988 143 static Ticker gForceTicker;
uci1 3:24c5f0f50bf1 144 static Ticker gHeartbeatTicker;
uci1 1:e392595b4b76 145 static Ticker gCommWinTicker;
uci1 8:95a325df1f6b 146 static Ticker gPowerCheckTicker;
uci1 8:95a325df1f6b 147 #endif
uci1 12:d472f9811262 148 static Timer gTrgTimer;
uci1 16:744ce85aede2 149 static Timer gAdcToMBtimer;
uci1 0:664899e0b988 150 static SnConfigFrame gConf;
uci1 0:664899e0b988 151 static SnEventFrame gEvent;
uci1 8:95a325df1f6b 152 static SnPowerFrame gPower;
uci1 0:664899e0b988 153 // parameters
uci1 21:ce51bb0ba4a5 154 static bool gCardsPowered = false;
uci1 0:664899e0b988 155 static bool gFirstEvt = true;
uci1 15:f2569d8e4176 156 static volatile bool gReadingOut = false;
uci1 15:f2569d8e4176 157 static volatile bool gCommWinOpen = false; // if it's open
uci1 1:e392595b4b76 158 static volatile bool gOpenCommWin = false; // if it should be opened
uci1 8:95a325df1f6b 159 static volatile bool gCheckPower = false; // if it should be checked
uci1 8:95a325df1f6b 160 static uint32_t gPowNum = 0;
uci1 8:95a325df1f6b 161 static uint32_t gEvtNum = 0; // num of evt written
uci1 8:95a325df1f6b 162 static uint32_t gTrgNum[kNumTrgs] = {0}; // num of this type of trg received
uci1 0:664899e0b988 163 // i/o
uci1 21:ce51bb0ba4a5 164 static time_t gLastCommWin = 0; // time
uci1 22:f957c4f840ad 165 static uint32_t gCommWinChecks = 0;
uci1 22:f957c4f840ad 166 static uint32_t gNcommWinChecks = 0;
uci1 22:f957c4f840ad 167 // heartbeat
uci1 22:f957c4f840ad 168 static time_t gLastHrtbt = 0;
uci1 23:ccf39298f205 169 static volatile bool gHrtbtFired = false;
uci1 22:f957c4f840ad 170 static uint32_t gHrtbtNum = 0;
uci1 22:f957c4f840ad 171 // rates
uci1 22:f957c4f840ad 172 static double gThmDtSum = 0; // sum of all time diffs between thermal trigs
uci1 22:f957c4f840ad 173 static double gEvtDtSum = 0; // sum of all time diffs between events
uci1 22:f957c4f840ad 174 static uint32_t gThmNumDt = 0; // number of thermal trig time diffs added up
uci1 22:f957c4f840ad 175 static uint32_t gEvtNumDt = 0; // number of event time diffs added up
uci1 10:3c93db1cfb12 176 // this should be bigger than anything that will actually be used
uci1 10:3c93db1cfb12 177 static const uint32_t gBufSize=SnStatusFrame::kMaxSizeOf + (2u*SnHeaderFrame::kMaxSizeOf) + SnPowerFrame::kMaxSizeOf;
uci1 6:6f002d202f59 178 //static const uint32_t gB64Bsize=BASE64ENC_LEN(gBufSize)+1;
uci1 6:6f002d202f59 179 //static char gB64Buf[gB64Bsize];
uci1 3:24c5f0f50bf1 180 static char gGenBuf[gBufSize]; // must be big enough for event or status or config!
uci1 11:de443350ec4a 181 static SnCommWin* gComms[kNcomms] = { 0 }; // order => priority. afar uses RTOS, and must be made inside main
uci1 0:664899e0b988 182
uci1 0:664899e0b988 183 void procForceTrigger() {
uci1 0:664899e0b988 184 if (gReadingOut==false && gCommWinOpen==false) {
uci1 15:f2569d8e4176 185 led3=!led3;
uci1 12:d472f9811262 186 #ifdef DEBUG
uci1 8:95a325df1f6b 187 printf("proc force\r\n");
uci1 12:d472f9811262 188 #endif
uci1 0:664899e0b988 189 gEvent.SetTrgBit(kFrcTrg);
uci1 21:ce51bb0ba4a5 190 gEvent.SetTrgNum(++(gTrgNum[kFrcTrg]));
uci1 23:ccf39298f205 191 //PIN_forceTrigger = 0;
uci1 0:664899e0b988 192 PIN_forceTrigger = 1; // force a trigger
uci1 21:ce51bb0ba4a5 193 PIN_forceTrigger = 0;
uci1 21:ce51bb0ba4a5 194 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 195 printf("PIN_forceTrigge=%d, PIN_turn_on_system=%d, "
uci1 21:ce51bb0ba4a5 196 "PIN_a_sf_clk=%d\r\n",
uci1 21:ce51bb0ba4a5 197 PIN_forceTrigger.read(), PIN_turn_on_system.read(),
uci1 21:ce51bb0ba4a5 198 PIN_a_sf_clk.read());
uci1 21:ce51bb0ba4a5 199 #endif
uci1 0:664899e0b988 200 }
uci1 0:664899e0b988 201 }
uci1 0:664899e0b988 202
uci1 3:24c5f0f50bf1 203 void procHeartbeat() {
uci1 3:24c5f0f50bf1 204 if (gReadingOut==false && gCommWinOpen==false) {
uci1 12:d472f9811262 205 #ifdef DEBUG
uci1 8:95a325df1f6b 206 printf("proc heartbeat\r\n");
uci1 12:d472f9811262 207 #endif
uci1 23:ccf39298f205 208 //PIN_heartbeat = 0;
uci1 3:24c5f0f50bf1 209 PIN_heartbeat = 1; // heartbeat pulse
uci1 3:24c5f0f50bf1 210 PIN_heartbeat = 0;
uci1 22:f957c4f840ad 211 gLastHrtbt = time(0);
uci1 22:f957c4f840ad 212 gHrtbtFired = true;
uci1 22:f957c4f840ad 213 ++gHrtbtNum;
uci1 3:24c5f0f50bf1 214 }
uci1 3:24c5f0f50bf1 215 }
uci1 3:24c5f0f50bf1 216
uci1 8:95a325df1f6b 217 void procPowerCheck() {
uci1 12:d472f9811262 218 #ifdef DEBUG
uci1 8:95a325df1f6b 219 printf("proc power\r\n");
uci1 12:d472f9811262 220 #endif
uci1 8:95a325df1f6b 221 gCheckPower=true;
uci1 8:95a325df1f6b 222 }
uci1 8:95a325df1f6b 223
uci1 0:664899e0b988 224 void procCommWin() {
uci1 22:f957c4f840ad 225 ++gCommWinChecks;
uci1 0:664899e0b988 226 if (gReadingOut==false && gCommWinOpen==false) {
uci1 22:f957c4f840ad 227 //if ( (time(0) - gLastCommWin) > gConf.GetCommWinPeriod() ) {
uci1 22:f957c4f840ad 228 #ifdef DEBUG
uci1 22:f957c4f840ad 229 printf("<><><><><><> gCommWinChecks=%u, gNcommWinChecks=%u\r\n",
uci1 22:f957c4f840ad 230 gCommWinChecks, gNcommWinChecks);
uci1 22:f957c4f840ad 231 #endif
uci1 22:f957c4f840ad 232 if ( gCommWinChecks >= gNcommWinChecks ) {
uci1 12:d472f9811262 233 #ifdef DEBUG
uci1 8:95a325df1f6b 234 printf("proc comm win\r\n");
uci1 12:d472f9811262 235 #endif
uci1 1:e392595b4b76 236 led3=!led3;
uci1 1:e392595b4b76 237 gOpenCommWin = true;
uci1 1:e392595b4b76 238 }
uci1 0:664899e0b988 239 }
uci1 0:664899e0b988 240 }
uci1 0:664899e0b988 241
uci1 21:ce51bb0ba4a5 242 bool AreCardsPowered(const bool checkPin) {
uci1 21:ce51bb0ba4a5 243 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 244 printf("acp: PIN_turn_on_system=%d, gCardsPowered=%d\r\n",
uci1 21:ce51bb0ba4a5 245 PIN_turn_on_system.read(), gCardsPowered);
uci1 21:ce51bb0ba4a5 246 #endif
uci1 21:ce51bb0ba4a5 247 if (checkPin) {
uci1 21:ce51bb0ba4a5 248 gCardsPowered = (PIN_turn_on_system.read()==0);
uci1 16:744ce85aede2 249 }
uci1 21:ce51bb0ba4a5 250 return gCardsPowered;
uci1 8:95a325df1f6b 251 }
uci1 0:664899e0b988 252
uci1 8:95a325df1f6b 253 void GetAvePowerReading() {
uci1 8:95a325df1f6b 254 // use one measurement as the assumed average
uci1 8:95a325df1f6b 255 // in order to reduce computational errors
uci1 8:95a325df1f6b 256 int32_t v1, v2;
uci1 8:95a325df1f6b 257 const uint16_t aaveV1 = PIN_vADC1.read_u16();
uci1 8:95a325df1f6b 258 const uint16_t aaveV2 = PIN_vADC2.read_u16();
uci1 8:95a325df1f6b 259 float n=0, ave1=0, ave2=0, rms1=0, rms2=0;
uci1 8:95a325df1f6b 260 for (uint16_t i=0; i<kNvoltsAve; i++) {
uci1 8:95a325df1f6b 261 v1 = PIN_vADC1.read_u16() - aaveV1;
uci1 8:95a325df1f6b 262 v2 = PIN_vADC2.read_u16() - aaveV2;
uci1 8:95a325df1f6b 263 n += 1;
uci1 8:95a325df1f6b 264 ave1 += v1;
uci1 8:95a325df1f6b 265 rms1 += v1*v1;
uci1 8:95a325df1f6b 266 ave2 += v2;
uci1 8:95a325df1f6b 267 rms2 += v2*v2;
uci1 8:95a325df1f6b 268 }
uci1 8:95a325df1f6b 269 rms1 -= (ave1*ave1)/n;
uci1 8:95a325df1f6b 270 rms2 -= (ave2*ave2)/n;
uci1 8:95a325df1f6b 271 rms1 /= n-1;
uci1 8:95a325df1f6b 272 rms2 /= n-1;
uci1 9:a1a39573dd43 273 rms1 = sqrt(rms1);
uci1 9:a1a39573dd43 274 rms2 = sqrt(rms2);
uci1 8:95a325df1f6b 275 ave1 /= n;
uci1 8:95a325df1f6b 276 ave2 /= n;
uci1 8:95a325df1f6b 277 ave1 += aaveV1;
uci1 8:95a325df1f6b 278 ave2 += aaveV2;
uci1 8:95a325df1f6b 279 gPower.Set(ave1, ave2, rms1, rms2, time(0));
uci1 8:95a325df1f6b 280 }
uci1 0:664899e0b988 281
uci1 8:95a325df1f6b 282 void CheckPower(const bool isCommWin) {
uci1 12:d472f9811262 283 #ifdef DEBUG
uci1 8:95a325df1f6b 284 printf("CheckPower\r\n");
uci1 12:d472f9811262 285 #endif
uci1 8:95a325df1f6b 286 // read power
uci1 8:95a325df1f6b 287 GetAvePowerReading();
uci1 8:95a325df1f6b 288 // save to disk
uci1 8:95a325df1f6b 289 FILE* cf = SnSDUtils::GetCurFile();
uci1 8:95a325df1f6b 290 if (cf!=0) {
uci1 11:de443350ec4a 291 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 12:d472f9811262 292 #ifdef DEBUG
uci1 8:95a325df1f6b 293 printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
uci1 8:95a325df1f6b 294 gPower.GetAveV1(), gPower.GetAveV2(),
uci1 8:95a325df1f6b 295 gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
uci1 8:95a325df1f6b 296 gPowNum);
uci1 12:d472f9811262 297 #endif
uci1 8:95a325df1f6b 298 SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
uci1 8:95a325df1f6b 299 }
uci1 8:95a325df1f6b 300 // do we need to change modes?
uci1 8:95a325df1f6b 301 bool changed = false;
uci1 8:95a325df1f6b 302 if (gConf.IsLowPowerMode()) {
uci1 8:95a325df1f6b 303 if (gPower.GetAveV1() > gConf.GetBatVoltLowPwr()) {
uci1 12:d472f9811262 304 #ifdef DEBUG
uci1 8:95a325df1f6b 305 printf("chaing to normal power!\r\n");
uci1 12:d472f9811262 306 #endif
uci1 8:95a325df1f6b 307 gConf.ChangeToNormPower();
uci1 8:95a325df1f6b 308 changed = true;
uci1 8:95a325df1f6b 309 }
uci1 8:95a325df1f6b 310 } else {
uci1 8:95a325df1f6b 311 if (gPower.GetAveV1() < gConf.GetBatVoltLowPwr()) {
uci1 12:d472f9811262 312 #ifdef DEBUG
uci1 8:95a325df1f6b 313 printf("chaing to low power!\r\n");
uci1 12:d472f9811262 314 #endif
uci1 8:95a325df1f6b 315 gConf.ChangeToLowPower();
uci1 8:95a325df1f6b 316 changed = true;
uci1 8:95a325df1f6b 317 }
uci1 8:95a325df1f6b 318 }
uci1 8:95a325df1f6b 319 if (changed) {
uci1 8:95a325df1f6b 320 SetPower(isCommWin);
uci1 12:d472f9811262 321 #ifdef DEBUG
uci1 8:95a325df1f6b 322 printf("Using config %s\r\n",gConf.GetLabel());
uci1 12:d472f9811262 323 #endif
uci1 8:95a325df1f6b 324 SetConfigAndMakeOutputFile(); // setup defaults in case no communication
uci1 8:95a325df1f6b 325 }
uci1 8:95a325df1f6b 326 // checking done
uci1 8:95a325df1f6b 327 gCheckPower = false;
uci1 8:95a325df1f6b 328 }
uci1 8:95a325df1f6b 329
uci1 10:3c93db1cfb12 330 void ResetCountersClearEvt() {
uci1 12:d472f9811262 331 const uint32_t evtStartCurSeq = (SnSDUtils::GetCurSeqNum()) // no -1; start with seq=0
uci1 12:d472f9811262 332 * gConf.GetEvtsPerFile();
uci1 10:3c93db1cfb12 333 gEvent.ClearEvent();
uci1 12:d472f9811262 334 gEvtNum = gConf.GetFirstEvt() + evtStartCurSeq;
uci1 12:d472f9811262 335 gPowNum = evtStartCurSeq;
uci1 10:3c93db1cfb12 336 memset(gTrgNum, 0, sizeof(uint32_t)*kNumTrgs);
uci1 22:f957c4f840ad 337 // reset rate counters
uci1 22:f957c4f840ad 338 gThmDtSum = 0;
uci1 22:f957c4f840ad 339 gThmNumDt = 0;
uci1 22:f957c4f840ad 340 gEvtDtSum = 0;
uci1 22:f957c4f840ad 341 gEvtNumDt = 0;
uci1 22:f957c4f840ad 342 // reset heartbeat counters
uci1 22:f957c4f840ad 343 gLastHrtbt = 0;
uci1 22:f957c4f840ad 344 gHrtbtFired = false;
uci1 22:f957c4f840ad 345 gHrtbtNum = 0;
uci1 13:7a1fb885a8e4 346 #ifdef DEBUG
uci1 13:7a1fb885a8e4 347 printf("Reset: gEvtNum=%u, gPowNum=%u, evtStartCS=%u\r\n",
uci1 13:7a1fb885a8e4 348 gEvtNum, gPowNum, evtStartCurSeq);
uci1 13:7a1fb885a8e4 349 #endif
uci1 10:3c93db1cfb12 350 }
uci1 10:3c93db1cfb12 351
uci1 10:3c93db1cfb12 352 void GetRates(float& thmrate, float& evtrate) {
uci1 10:3c93db1cfb12 353 thmrate = evtrate = 0;
uci1 22:f957c4f840ad 354 #ifdef DEBUG
uci1 22:f957c4f840ad 355 printf("** Getting rates: gThmNumDt=%d, gThmDtSum=%g, "
uci1 22:f957c4f840ad 356 "gEvtNumDt=%d, gEvtDtSum=%g\r\n",
uci1 22:f957c4f840ad 357 gThmNumDt, gThmDtSum, gEvtNumDt, gEvtDtSum);
uci1 22:f957c4f840ad 358 #endif
uci1 22:f957c4f840ad 359
uci1 22:f957c4f840ad 360 thmrate = (gThmDtSum>0.0) ? static_cast<float>(gThmNumDt) / (gThmDtSum/1e3)
uci1 22:f957c4f840ad 361 : 0;
uci1 22:f957c4f840ad 362 evtrate = (gEvtDtSum>0.0) ? static_cast<float>(gEvtNumDt) / (gEvtDtSum/1e3)
uci1 22:f957c4f840ad 363 : 0;
uci1 22:f957c4f840ad 364 }
uci1 22:f957c4f840ad 365
uci1 22:f957c4f840ad 366 void AddToRate(const float dt, const bool isThm) {
uci1 22:f957c4f840ad 367 if (isThm) {
uci1 22:f957c4f840ad 368 gThmDtSum += dt;
uci1 22:f957c4f840ad 369 gThmNumDt += 1u;
uci1 22:f957c4f840ad 370 } else {
uci1 22:f957c4f840ad 371 gEvtDtSum += dt;
uci1 22:f957c4f840ad 372 gEvtNumDt += 1u;
uci1 10:3c93db1cfb12 373 }
uci1 22:f957c4f840ad 374 #ifdef DEBUG
uci1 22:f957c4f840ad 375 printf("** AddToRate: dt=%g, isThm=%d\r\n",dt,(int)isThm);
uci1 22:f957c4f840ad 376 printf("** AddToRate: gThmNumDt=%d, gThmDtSum=%g, "
uci1 22:f957c4f840ad 377 "gEvtNumDt=%d, gEvtDtSum=%g\r\n",
uci1 22:f957c4f840ad 378 gThmNumDt, gThmDtSum, gEvtNumDt, gEvtDtSum);
uci1 22:f957c4f840ad 379 #endif
uci1 10:3c93db1cfb12 380 }
uci1 10:3c93db1cfb12 381
uci1 8:95a325df1f6b 382 bool IsSeqComplete() {
uci1 12:d472f9811262 383 #ifdef DEBUG
uci1 12:d472f9811262 384 printf("IsSeqComplete: eps=%u, cntpow=%d, fe=%u, pow=%u, evt=%u, seq=%hu\r\n",
uci1 10:3c93db1cfb12 385 gConf.GetEvtsPerFile(), (int)gConf.IsCountingPowerReadings(),
uci1 12:d472f9811262 386 gConf.GetFirstEvt(), gPowNum, gEvtNum, SnSDUtils::GetCurSeqNum());
uci1 12:d472f9811262 387 #endif
uci1 8:95a325df1f6b 388 if (gConf.GetEvtsPerFile()>0) {
uci1 12:d472f9811262 389 const uint32_t evtEndCurSeq = (SnSDUtils::GetCurSeqNum()+1) // account for seq=0
uci1 12:d472f9811262 390 * gConf.GetEvtsPerFile();
uci1 12:d472f9811262 391 #ifdef DEBUG
uci1 12:d472f9811262 392 printf("evtEndCurSeq=%u\r\n",evtEndCurSeq);
uci1 12:d472f9811262 393 #endif
uci1 8:95a325df1f6b 394 if (gConf.IsCountingPowerReadings()) {
uci1 12:d472f9811262 395 return (gPowNum>=evtEndCurSeq);
uci1 8:95a325df1f6b 396 } else {
uci1 12:d472f9811262 397 // first event num is a one-time per run offset, not one per sequence
uci1 12:d472f9811262 398 return (gEvtNum>=(gConf.GetFirstEvt()+evtEndCurSeq));
uci1 8:95a325df1f6b 399 }
uci1 12:d472f9811262 400 } else {
uci1 12:d472f9811262 401 return false;
uci1 8:95a325df1f6b 402 }
uci1 8:95a325df1f6b 403 }
uci1 1:e392595b4b76 404
uci1 8:95a325df1f6b 405 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 406 void stopTicker(rtos::RtosTimer* tik) {
uci1 8:95a325df1f6b 407 if (tik!=0) {
uci1 8:95a325df1f6b 408 tik->stop();
uci1 8:95a325df1f6b 409 }
uci1 8:95a325df1f6b 410 }
uci1 8:95a325df1f6b 411 #else
uci1 8:95a325df1f6b 412 void stopTicker(Ticker& tik) {
uci1 8:95a325df1f6b 413 tik.detach();
uci1 8:95a325df1f6b 414 }
uci1 8:95a325df1f6b 415 #endif
uci1 8:95a325df1f6b 416
uci1 8:95a325df1f6b 417 #ifdef USE_RTOS_TIMER
uci1 18:55f1581f2ee4 418 float resetTicker(rtos::RtosTimer* tik, const float timSec,
uci1 18:55f1581f2ee4 419 const float maxTimSec) {
uci1 8:95a325df1f6b 420 if (tik!=0) {
uci1 8:95a325df1f6b 421 tik->stop();
uci1 8:95a325df1f6b 422 if (timSec>0) {
uci1 18:55f1581f2ee4 423 float tp = timSec > maxTimSec ? maxTimSec : timSec;
uci1 8:95a325df1f6b 424 tp *= 1000u; // ms
uci1 8:95a325df1f6b 425 tik->start(tp);
uci1 8:95a325df1f6b 426 return tp;
uci1 8:95a325df1f6b 427 }
uci1 8:95a325df1f6b 428 }
uci1 8:95a325df1f6b 429 return 0;
uci1 8:95a325df1f6b 430 }
uci1 8:95a325df1f6b 431 #else
uci1 18:55f1581f2ee4 432 float resetTicker(Ticker& tik, const float timSec,
uci1 18:55f1581f2ee4 433 const float maxTimSec, void (*fptr)(void)) {
uci1 8:95a325df1f6b 434 tik.detach();
uci1 8:95a325df1f6b 435 if (timSec>0) {
uci1 18:55f1581f2ee4 436 const float tp = timSec > maxTimSec ? maxTimSec : timSec;
uci1 8:95a325df1f6b 437 tik.attach(fptr, tp);
uci1 8:95a325df1f6b 438 return tp;
uci1 8:95a325df1f6b 439 }
uci1 8:95a325df1f6b 440 return 0;
uci1 8:95a325df1f6b 441 }
uci1 8:95a325df1f6b 442 #endif
uci1 8:95a325df1f6b 443
uci1 15:f2569d8e4176 444 void StopAllTickers() {
uci1 8:95a325df1f6b 445 stopTicker(gForceTicker);
uci1 8:95a325df1f6b 446 stopTicker(gHeartbeatTicker);
uci1 8:95a325df1f6b 447 stopTicker(gCommWinTicker);
uci1 8:95a325df1f6b 448 stopTicker(gPowerCheckTicker);
uci1 15:f2569d8e4176 449 }
uci1 15:f2569d8e4176 450
uci1 15:f2569d8e4176 451 void ResetAllTickers() {
uci1 15:f2569d8e4176 452 #ifdef USE_RTOS_TIMER
uci1 18:55f1581f2ee4 453 const float ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
uci1 18:55f1581f2ee4 454 kAbsMaxTimer);
uci1 18:55f1581f2ee4 455 const float hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
uci1 18:55f1581f2ee4 456 kAbsMaxTimer);
uci1 18:55f1581f2ee4 457 const float cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
uci1 18:55f1581f2ee4 458 kCommWinLongPrdTk);
uci1 18:55f1581f2ee4 459 const float pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
uci1 18:55f1581f2ee4 460 kAbsMaxTimer);
uci1 15:f2569d8e4176 461 #else
uci1 18:55f1581f2ee4 462 const float ftp = resetTicker(gForceTicker, gConf.GetForceTrigPeriod(),
uci1 18:55f1581f2ee4 463 kAbsMaxTimer, &procForceTrigger);
uci1 18:55f1581f2ee4 464 const float hbp = resetTicker(gHeartbeatTicker, gConf.GetHeartbeatPeriod(),
uci1 18:55f1581f2ee4 465 kAbsMaxTimer, &procHeartbeat);
uci1 18:55f1581f2ee4 466 const float cwp = resetTicker(gCommWinTicker, gConf.GetCommWinPeriod(),
uci1 18:55f1581f2ee4 467 kCommWinLongPrdTk, &procCommWin);
uci1 18:55f1581f2ee4 468 const float pcp = resetTicker(gPowerCheckTicker, gConf.GetVoltCheckPeriod(),
uci1 18:55f1581f2ee4 469 kAbsMaxTimer, &procPowerCheck);
uci1 15:f2569d8e4176 470 #endif
uci1 15:f2569d8e4176 471 #ifdef DEBUG
uci1 18:55f1581f2ee4 472 printf("attach force trig %g\r\n",ftp);
uci1 18:55f1581f2ee4 473 printf("attach heart beat %g\r\n",hbp);
uci1 18:55f1581f2ee4 474 printf("attach comm win %g\r\n",cwp);
uci1 18:55f1581f2ee4 475 printf("attach power chk %g\r\n",pcp);
uci1 15:f2569d8e4176 476 #endif
uci1 15:f2569d8e4176 477 }
uci1 15:f2569d8e4176 478
uci1 15:f2569d8e4176 479 void StopRunning() {
uci1 15:f2569d8e4176 480 #if defined(DEBUG) || defined(SSNOTIFY)
uci1 15:f2569d8e4176 481 printf("stop running\r\n");
uci1 15:f2569d8e4176 482 #endif
uci1 15:f2569d8e4176 483 StopAllTickers();
uci1 17:4687bf932b8c 484 OpenCommWin();
uci1 8:95a325df1f6b 485 while (true) {
uci1 8:95a325df1f6b 486 led3 = 1; led4=1;
uci1 8:95a325df1f6b 487 wait(0.5);
uci1 8:95a325df1f6b 488 led3 = 0; led4=0;
uci1 8:95a325df1f6b 489 wait(0.5);
uci1 22:f957c4f840ad 490 // don't kick the watchdog
uci1 22:f957c4f840ad 491 // if we do, the station is unrecoverable without physical access
uci1 8:95a325df1f6b 492 }
uci1 8:95a325df1f6b 493 }
uci1 1:e392595b4b76 494
uci1 0:664899e0b988 495 int main() {
uci1 22:f957c4f840ad 496 // a failsafe
uci1 22:f957c4f840ad 497 Watchdog::kick(WDFAILSAFE);
uci1 22:f957c4f840ad 498
uci1 1:e392595b4b76 499 {
uci1 18:55f1581f2ee4 500 gCpu.baud(115200);
uci1 18:55f1581f2ee4 501 #if defined(SSNOTIFY) || defined(DEBUG)
uci1 15:f2569d8e4176 502 printf("main: start\r\n");
uci1 15:f2569d8e4176 503 #endif
uci1 2:e67f7c158087 504 led1=1; wait(0.2);
uci1 2:e67f7c158087 505 led1=0; led2=1; wait(0.2);
uci1 2:e67f7c158087 506 led2=0; led3=1; wait(0.2);
uci1 2:e67f7c158087 507 led3=0; led4=1; wait(0.2);
uci1 1:e392595b4b76 508 led4=0;
uci1 1:e392595b4b76 509 }
uci1 23:ccf39298f205 510
uci1 18:55f1581f2ee4 511 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 512 printf("initializing SD card..\r\n");
uci1 21:ce51bb0ba4a5 513 #endif
uci1 21:ce51bb0ba4a5 514 // initialize the SD card. this should prevent the issue with
uci1 21:ce51bb0ba4a5 515 // seq 0 being overwritten upon power up or the SD card first
uci1 21:ce51bb0ba4a5 516 // being insterted
uci1 21:ce51bb0ba4a5 517 sd.disk_initialize();
uci1 21:ce51bb0ba4a5 518
uci1 21:ce51bb0ba4a5 519 #ifdef DEBUG
uci1 18:55f1581f2ee4 520 printf("making comm objects\r\n");
uci1 18:55f1581f2ee4 521 #endif
uci1 16:744ce85aede2 522
uci1 8:95a325df1f6b 523 // RTOS stuff must be made inside main for some reason
uci1 21:ce51bb0ba4a5 524 /*
uci1 18:55f1581f2ee4 525 #ifdef USE_ETH_INTERFACE
uci1 18:55f1581f2ee4 526 gComms[0] = new SnCommAfarTCP(gConf);
uci1 18:55f1581f2ee4 527 #else
uci1 18:55f1581f2ee4 528 gComms[0] = new SnCommAfarNetIf(gConf);
uci1 18:55f1581f2ee4 529 #endif
uci1 21:ce51bb0ba4a5 530 */
uci1 23:ccf39298f205 531 gComms[0] = new SnCommSBD(&gSBDport, &gCpu);
uci1 23:ccf39298f205 532 //gComms[0] = new SnCommUsb(&gCpu);
uci1 18:55f1581f2ee4 533
uci1 18:55f1581f2ee4 534 #ifdef DEBUG
uci1 18:55f1581f2ee4 535 printf("make comm objects\r\n");
uci1 18:55f1581f2ee4 536 #endif
uci1 13:7a1fb885a8e4 537
uci1 8:95a325df1f6b 538 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 539 gForceTicker = new rtos::RtosTimer(&procForceTrigger);
uci1 8:95a325df1f6b 540 gHeartbeatTicker = new rtos::RtosTimer(&procHeartbeat);
uci1 8:95a325df1f6b 541 gCommWinTicker = new rtos::RtosTimer(&procCommWin);
uci1 8:95a325df1f6b 542 gPowerCheckTicker = new rtos::RtosTimer(&procPowerCheck);
uci1 8:95a325df1f6b 543 #endif
uci1 8:95a325df1f6b 544
uci1 0:664899e0b988 545 led2=1;
uci1 0:664899e0b988 546 //wait_ms(100);
uci1 1:e392595b4b76 547
uci1 12:d472f9811262 548 #ifdef DEBUG
uci1 3:24c5f0f50bf1 549 printf("\n\n\n\n\n\nstarting\r\n");
uci1 12:d472f9811262 550 #endif
uci1 1:e392595b4b76 551
uci1 1:e392595b4b76 552 // set the clock to the BS time, if it's not set
uci1 1:e392595b4b76 553 if ( (static_cast<int32_t>(time(0)))<0 ) {
uci1 1:e392595b4b76 554 set_time(kBStime);
uci1 1:e392595b4b76 555 }
uci1 12:d472f9811262 556 #ifdef DEBUG
uci1 1:e392595b4b76 557 printf("time = %d\r\n",(int32_t)time(0));
uci1 12:d472f9811262 558 #endif
uci1 1:e392595b4b76 559 gLastCommWin = time(0); // prevent comm win proc
uci1 0:664899e0b988 560
uci1 8:95a325df1f6b 561 #ifdef USE_RTOS_TIMER
uci1 8:95a325df1f6b 562 gForceTicker->stop();
uci1 8:95a325df1f6b 563 #else
uci1 0:664899e0b988 564 gForceTicker.detach();
uci1 8:95a325df1f6b 565 #endif
uci1 0:664899e0b988 566 gFirstEvt = true;
uci1 0:664899e0b988 567
uci1 4:a91682e19d6b 568 // (probably) power down comms and power up cards,amps
uci1 4:a91682e19d6b 569 SetPower(false);
uci1 4:a91682e19d6b 570
uci1 0:664899e0b988 571 //
uci1 0:664899e0b988 572 // get config
uci1 0:664899e0b988 573 //
uci1 12:d472f9811262 574 #ifdef DEBUG
uci1 8:95a325df1f6b 575 printf("call OpenCommWin\r\n");
uci1 12:d472f9811262 576 #endif
uci1 13:7a1fb885a8e4 577 OpenCommWin(true); // alwasy configure, even if no new config
uci1 3:24c5f0f50bf1 578
uci1 0:664899e0b988 579 // get ready to trigger
uci1 0:664899e0b988 580 PIN_spi.format( 16, 1 ); // change to data readout format
uci1 15:f2569d8e4176 581 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 0:664899e0b988 582
uci1 0:664899e0b988 583 led2=0;
uci1 0:664899e0b988 584
uci1 0:664899e0b988 585 // the main event loop. wait for triggers in SendClock
uci1 21:ce51bb0ba4a5 586 AreCardsPowered(true);
uci1 12:d472f9811262 587 gTrgTimer.start();
uci1 12:d472f9811262 588 register int32_t etms=0; // time between written events
uci1 0:664899e0b988 589 while( true )
uci1 0:664899e0b988 590 {
uci1 0:664899e0b988 591 // in here, we wait for triggers from the MB-FPGA
uci1 0:664899e0b988 592 Watchdog::kick(); // don't reset!
uci1 1:e392595b4b76 593
uci1 1:e392595b4b76 594 led1 = !led1;
uci1 1:e392595b4b76 595
uci1 12:d472f9811262 596 #ifdef DEBUG
uci1 1:e392595b4b76 597 printf("calling wait trig\r\n");
uci1 1:e392595b4b76 598 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 5:9cea89700c66 599 printf("readingout=%d\r\n",(int)gReadingOut);
uci1 12:d472f9811262 600 #endif
uci1 0:664899e0b988 601 PIN_lockRegisters = 0; // allow data to come from DFPGA
uci1 0:664899e0b988 602 WaitTrigAndSendClock();
uci1 0:664899e0b988 603 PIN_lockRegisters = 1; // block registers during readout
uci1 1:e392595b4b76 604
uci1 12:d472f9811262 605 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 606 Timer prof;
uci1 12:d472f9811262 607 prof.start();
uci1 12:d472f9811262 608 int befReadWv=0, aftReadWv=0, befSaveEvt=0, aftSaveEvt=0,
uci1 12:d472f9811262 609 befChkPow=0, aftChkPow=0, befNewSeq=0, aftNewSeq=0, endOfLoop=0;
uci1 12:d472f9811262 610 #endif
uci1 12:d472f9811262 611
uci1 1:e392595b4b76 612 if (gReadingOut) {
uci1 12:d472f9811262 613
uci1 12:d472f9811262 614 const int32_t ttms = gTrgTimer.read_ms(); // time since last trigger
uci1 12:d472f9811262 615 gTrgTimer.reset(); gTrgTimer.start(); // restart trigger timer
uci1 12:d472f9811262 616 etms += ttms;
uci1 8:95a325df1f6b 617
uci1 8:95a325df1f6b 618 Watchdog::kick(); // don't reset!
uci1 15:f2569d8e4176 619
uci1 1:e392595b4b76 620 //
uci1 1:e392595b4b76 621 // got trigger. read registers to mbed and build the event
uci1 1:e392595b4b76 622 //
uci1 1:e392595b4b76 623
uci1 1:e392595b4b76 624 led4=1;
uci1 1:e392595b4b76 625
uci1 22:f957c4f840ad 626 if ( gEvent.IsForcedTrg() || gFirstEvt ||
uci1 22:f957c4f840ad 627 (etms>gConf.GetEvtThrtlPeriodMs()) ) {
uci1 22:f957c4f840ad 628
uci1 22:f957c4f840ad 629 // read data & calc CRC
uci1 12:d472f9811262 630 #ifdef EVT_TIME_PROFILE
uci1 22:f957c4f840ad 631 prof.stop(); befReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 632 #endif
uci1 12:d472f9811262 633
uci1 22:f957c4f840ad 634 // get the data to the MBED
uci1 22:f957c4f840ad 635 gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit);
uci1 15:f2569d8e4176 636
uci1 12:d472f9811262 637 #ifdef EVT_TIME_PROFILE
uci1 22:f957c4f840ad 638 prof.stop(); aftReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 639 #endif
uci1 12:d472f9811262 640
uci1 22:f957c4f840ad 641 gEvent.SetCurMbedTime();
uci1 22:f957c4f840ad 642 // TODO: no way to check for external trigger?
uci1 22:f957c4f840ad 643 if (gEvent.IsForcedTrg()==false) {
uci1 22:f957c4f840ad 644 gEvent.SetTrgBit(kThmTrg);
uci1 22:f957c4f840ad 645 gEvent.SetTrgNum(++(gTrgNum[kThmTrg]));
uci1 22:f957c4f840ad 646 AddToRate(ttms, true);
uci1 22:f957c4f840ad 647 } // else already set by procForceTrigger
uci1 22:f957c4f840ad 648 // (no need to calc if we throw this event away)
uci1 22:f957c4f840ad 649
uci1 22:f957c4f840ad 650 Watchdog::kick(); // don't reset!
uci1 22:f957c4f840ad 651
uci1 12:d472f9811262 652 #ifdef DEBUG
uci1 22:f957c4f840ad 653 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 12:d472f9811262 654 #endif
uci1 1:e392595b4b76 655
uci1 1:e392595b4b76 656 led2=1;
uci1 22:f957c4f840ad 657 /*
uci1 21:ce51bb0ba4a5 658 gRecentCountTime = static_cast<uint32_t>(time(0));
uci1 21:ce51bb0ba4a5 659 gRecentEvtNum = gEvtNum;
uci1 21:ce51bb0ba4a5 660 // do start time second so that if we get only one event,
uci1 21:ce51bb0ba4a5 661 // the delta(t) will be less than 0 and the rates not calculated
uci1 21:ce51bb0ba4a5 662 if (gDoResetLastCount) {
uci1 21:ce51bb0ba4a5 663 gLastCountReset = static_cast<uint32_t>(time(0)); // to calc rates
uci1 21:ce51bb0ba4a5 664 gLastEventReset = gEvtNum;
uci1 21:ce51bb0ba4a5 665 gDoResetLastCount = false;
uci1 21:ce51bb0ba4a5 666 }
uci1 22:f957c4f840ad 667 */
uci1 21:ce51bb0ba4a5 668
uci1 1:e392595b4b76 669 PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card.
uci1 1:e392595b4b76 670
uci1 12:d472f9811262 671 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 672 prof.stop(); befSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 673 #endif
uci1 12:d472f9811262 674
uci1 1:e392595b4b76 675 SaveEvent(etms);
uci1 22:f957c4f840ad 676 AddToRate(etms, false);
uci1 12:d472f9811262 677 etms=0;
uci1 8:95a325df1f6b 678
uci1 12:d472f9811262 679 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 680 prof.stop(); aftSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 681 #endif
uci1 1:e392595b4b76 682 }
uci1 1:e392595b4b76 683 }
uci1 12:d472f9811262 684 #ifdef DEBUG
uci1 1:e392595b4b76 685 printf("past reading out\r\n");
uci1 12:d472f9811262 686 #endif
uci1 1:e392595b4b76 687
uci1 23:ccf39298f205 688 if (gHrtbtFired) {
uci1 23:ccf39298f205 689 SaveHeartbeat();
uci1 23:ccf39298f205 690 gHrtbtFired=false;
uci1 23:ccf39298f205 691 }
uci1 23:ccf39298f205 692
uci1 1:e392595b4b76 693 led4=0; led2=0;
uci1 12:d472f9811262 694
uci1 12:d472f9811262 695 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 696 prof.stop(); befChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 697 #endif
uci1 8:95a325df1f6b 698 // check the power?
uci1 8:95a325df1f6b 699 if (gCheckPower) {
uci1 12:d472f9811262 700 #ifdef DEBUG
uci1 8:95a325df1f6b 701 printf("call check power\r\n");
uci1 12:d472f9811262 702 #endif
uci1 8:95a325df1f6b 703 CheckPower(false);
uci1 8:95a325df1f6b 704 }
uci1 12:d472f9811262 705 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 706 prof.stop(); aftChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 707 #endif
uci1 8:95a325df1f6b 708
uci1 21:ce51bb0ba4a5 709 // open comm win?
uci1 21:ce51bb0ba4a5 710 if (gOpenCommWin) {
uci1 21:ce51bb0ba4a5 711 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 712 printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false");
uci1 21:ce51bb0ba4a5 713 #endif
uci1 21:ce51bb0ba4a5 714 OpenCommWin();
uci1 21:ce51bb0ba4a5 715 gOpenCommWin=false;
uci1 22:f957c4f840ad 716 gFirstEvt = true;
uci1 22:f957c4f840ad 717 gTrgTimer.reset();
uci1 22:f957c4f840ad 718 etms=0;
uci1 21:ce51bb0ba4a5 719 } else {
uci1 21:ce51bb0ba4a5 720 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 721 printf("gOpenCommWin=false\r\n");
uci1 21:ce51bb0ba4a5 722 #endif
uci1 21:ce51bb0ba4a5 723 }
uci1 21:ce51bb0ba4a5 724
uci1 12:d472f9811262 725 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 726 prof.stop(); befNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 727 #endif
uci1 8:95a325df1f6b 728 // make new seq?
uci1 8:95a325df1f6b 729 if (IsSeqComplete()) {
uci1 12:d472f9811262 730 #ifdef DEBUG
uci1 10:3c93db1cfb12 731 printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
uci1 12:d472f9811262 732 #endif
uci1 8:95a325df1f6b 733 MakeOutputFile(gConf.IsSingleSeqRunMode());
uci1 22:f957c4f840ad 734 gFirstEvt = true;
uci1 22:f957c4f840ad 735 gTrgTimer.reset();
uci1 22:f957c4f840ad 736 etms=0;
uci1 8:95a325df1f6b 737 }
uci1 12:d472f9811262 738 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 739 prof.stop(); aftNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 740 #endif
uci1 12:d472f9811262 741
uci1 12:d472f9811262 742 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 743 prof.stop(); endOfLoop=prof.read_us(); prof.start();
uci1 12:d472f9811262 744 printf("befReadWv=%d, aftReadWv=%d, befSaveEvt=%d, aftSaveEvt=%d, "
uci1 12:d472f9811262 745 "befChkPow=%d, aftChkPow=%d, befNewSeq=%d, aftNewSeq=%d, endOfLoop=%d\r\n",
uci1 12:d472f9811262 746 befReadWv, aftReadWv, befSaveEvt, aftSaveEvt,
uci1 12:d472f9811262 747 befChkPow, aftChkPow, befNewSeq, aftNewSeq, endOfLoop);
uci1 12:d472f9811262 748 #endif
uci1 15:f2569d8e4176 749
uci1 15:f2569d8e4176 750 // get ready to trigger
uci1 15:f2569d8e4176 751 PIN_spi.format( 16, 1 ); // change to data readout format
uci1 15:f2569d8e4176 752 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 22:f957c4f840ad 753
uci1 21:ce51bb0ba4a5 754 // reset event
uci1 22:f957c4f840ad 755 // clear after comm win, so full event can be sent with status
uci1 22:f957c4f840ad 756 // but don't clear counters or trigger bits, as
uci1 22:f957c4f840ad 757 gEvent.ClearEvent(true);
uci1 21:ce51bb0ba4a5 758
uci1 0:664899e0b988 759 }
uci1 0:664899e0b988 760
uci1 0:664899e0b988 761 }
uci1 0:664899e0b988 762
uci1 0:664899e0b988 763 //
uci1 22:f957c4f840ad 764 // save a heartbeat tag
uci1 22:f957c4f840ad 765 //
uci1 22:f957c4f840ad 766 void SaveHeartbeat() {
uci1 22:f957c4f840ad 767 if (gHrtbtNum>0) {
uci1 22:f957c4f840ad 768 #ifdef DEBUG
uci1 22:f957c4f840ad 769 printf("save heartbeat #%u, time %u\r\n",
uci1 22:f957c4f840ad 770 gHrtbtNum-1, gLastHrtbt);
uci1 22:f957c4f840ad 771 #endif
uci1 22:f957c4f840ad 772 // save to SD
uci1 22:f957c4f840ad 773 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 22:f957c4f840ad 774 SnSDUtils::WriteHeartbeatTo(SnSDUtils::GetCurFile(),
uci1 22:f957c4f840ad 775 gLastHrtbt, gHrtbtNum-1); // -1 so it counts from 0
uci1 22:f957c4f840ad 776 }
uci1 22:f957c4f840ad 777 }
uci1 22:f957c4f840ad 778
uci1 22:f957c4f840ad 779 //
uci1 0:664899e0b988 780 // save the event
uci1 0:664899e0b988 781 //
uci1 0:664899e0b988 782 void SaveEvent(const int32_t etms) {
uci1 0:664899e0b988 783 // write the event
uci1 12:d472f9811262 784
uci1 12:d472f9811262 785 #ifdef DEBUG
uci1 3:24c5f0f50bf1 786 printf("save event\r\n");
uci1 12:d472f9811262 787 #endif
uci1 3:24c5f0f50bf1 788
uci1 0:664899e0b988 789 // set the event number & dt
uci1 3:24c5f0f50bf1 790 gEvent.SetEvtNum(gEvtNum);
uci1 0:664899e0b988 791 gEvent.SetDTms(etms);
uci1 0:664899e0b988 792
uci1 0:664899e0b988 793 // save to SD
uci1 11:de443350ec4a 794 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 1:e392595b4b76 795 SnSDUtils::WriteEventTo(SnSDUtils::GetCurFile(), gGenBuf, gEvent, gConf);
uci1 0:664899e0b988 796
uci1 3:24c5f0f50bf1 797 // increment event number
uci1 3:24c5f0f50bf1 798 ++gEvtNum;
uci1 3:24c5f0f50bf1 799
uci1 12:d472f9811262 800 #ifdef DEBUG
uci1 8:95a325df1f6b 801 printf("gEvtNum=%u\r\n",gEvtNum);
uci1 12:d472f9811262 802 #endif
uci1 3:24c5f0f50bf1 803 }
uci1 3:24c5f0f50bf1 804
uci1 3:24c5f0f50bf1 805 void MakeOutputFile(const bool stopRunning) {
uci1 10:3c93db1cfb12 806 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 12:d472f9811262 807 #ifdef DEBUG
uci1 10:3c93db1cfb12 808 printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
uci1 10:3c93db1cfb12 809 gEvtNum,gPowNum,(int)stopRunning);
uci1 12:d472f9811262 810 #endif
uci1 13:7a1fb885a8e4 811
uci1 3:24c5f0f50bf1 812 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 13:7a1fb885a8e4 813
uci1 12:d472f9811262 814 #ifdef DEBUG
uci1 10:3c93db1cfb12 815 printf("file closed\r\n");
uci1 12:d472f9811262 816 #endif
uci1 3:24c5f0f50bf1 817 if (stopRunning) {
uci1 8:95a325df1f6b 818 StopRunning();
uci1 0:664899e0b988 819 }
uci1 8:95a325df1f6b 820 FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(),
uci1 8:95a325df1f6b 821 gConf.GetRun());
uci1 13:7a1fb885a8e4 822 // reset event, timers, trigger counters
uci1 13:7a1fb885a8e4 823 ResetCountersClearEvt();
uci1 8:95a325df1f6b 824 if (cf!=0) {
uci1 8:95a325df1f6b 825 wait_ms(200);
uci1 8:95a325df1f6b 826 GetAvePowerReading();
uci1 12:d472f9811262 827 #ifdef DEBUG
uci1 8:95a325df1f6b 828 printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
uci1 8:95a325df1f6b 829 gPower.GetAveV1(), gPower.GetAveV2(),
uci1 8:95a325df1f6b 830 gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
uci1 8:95a325df1f6b 831 gPowNum);
uci1 12:d472f9811262 832 #endif
uci1 8:95a325df1f6b 833 SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
uci1 8:95a325df1f6b 834 }
uci1 12:d472f9811262 835 #ifdef DEBUG
uci1 3:24c5f0f50bf1 836 printf("made output file with run %u\r\n",gConf.GetRun());
uci1 3:24c5f0f50bf1 837 printf("filename=%s\r\n",SnSDUtils::GetCurFileName());
uci1 12:d472f9811262 838 #endif
uci1 3:24c5f0f50bf1 839 SnSDUtils::WriteConfig(SnSDUtils::GetCurFile(), gConf);
uci1 19:74155d652c37 840 #ifdef DEBUG
uci1 19:74155d652c37 841 printf("write config to file\r\n");
uci1 19:74155d652c37 842 #endif
uci1 0:664899e0b988 843 }
uci1 0:664899e0b988 844
uci1 0:664899e0b988 845 //
uci1 4:a91682e19d6b 846 // power stuff
uci1 4:a91682e19d6b 847 //
uci1 4:a91682e19d6b 848 void SetPower(const bool isCommWin) {
uci1 21:ce51bb0ba4a5 849 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 850 printf("bef: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
uci1 21:ce51bb0ba4a5 851 LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
uci1 21:ce51bb0ba4a5 852 printf("pcenet bef power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
uci1 21:ce51bb0ba4a5 853 #endif
uci1 8:95a325df1f6b 854 // TODO: turn on amps individually, when that's possible
uci1 4:a91682e19d6b 855 if (isCommWin) {
uci1 5:9cea89700c66 856 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardComWin);
uci1 4:a91682e19d6b 857 wait_ms(10);
uci1 5:9cea89700c66 858 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsComWin);
uci1 4:a91682e19d6b 859 wait_ms(10);
uci1 5:9cea89700c66 860 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridComWin);
uci1 4:a91682e19d6b 861 wait_ms(10);
uci1 21:ce51bb0ba4a5 862 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 863 printf("afar pin=%d, com powsetting=%d\r\n",PIN_afar_power.read(),
uci1 21:ce51bb0ba4a5 864 gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin));
uci1 21:ce51bb0ba4a5 865 #endif
uci1 21:ce51bb0ba4a5 866 if (gConf.IsPoweredFor(SnConfigFrame::kAfarComWin)) {
uci1 21:ce51bb0ba4a5 867 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 868 printf("PHY cowin powering up\r\n");
uci1 21:ce51bb0ba4a5 869 #endif
uci1 21:ce51bb0ba4a5 870 PHY_PowerUp(); wait(1);
uci1 21:ce51bb0ba4a5 871 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 872 printf("PHY cowin powered up\r\n");
uci1 21:ce51bb0ba4a5 873 #endif
uci1 21:ce51bb0ba4a5 874 } else {
uci1 21:ce51bb0ba4a5 875 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 876 printf("PHY cowin powering down\r\n");
uci1 21:ce51bb0ba4a5 877 #endif
uci1 21:ce51bb0ba4a5 878 PHY_PowerDown(); wait(1);
uci1 21:ce51bb0ba4a5 879 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 880 printf("PHY cowin powered down\r\n");
uci1 21:ce51bb0ba4a5 881 #endif
uci1 21:ce51bb0ba4a5 882 }
uci1 21:ce51bb0ba4a5 883 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 884 printf("PHY done\r\n");
uci1 21:ce51bb0ba4a5 885 #endif
uci1 21:ce51bb0ba4a5 886 wait_ms(100);
uci1 5:9cea89700c66 887 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin);
uci1 4:a91682e19d6b 888 wait_ms(10);
uci1 4:a91682e19d6b 889 } else {
uci1 5:9cea89700c66 890 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardDatTak);
uci1 4:a91682e19d6b 891 wait_ms(10);
uci1 5:9cea89700c66 892 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsDatTak);
uci1 4:a91682e19d6b 893 wait_ms(10);
uci1 5:9cea89700c66 894 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridDatTak);
uci1 4:a91682e19d6b 895 wait_ms(10);
uci1 21:ce51bb0ba4a5 896 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 897 printf("afar pin=%d, dat powsetting=%d, ispow=%d\r\n",PIN_afar_power.read(),
uci1 21:ce51bb0ba4a5 898 gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak),
uci1 21:ce51bb0ba4a5 899 (int)(gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak)));
uci1 21:ce51bb0ba4a5 900 #endif
uci1 21:ce51bb0ba4a5 901 if (gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak)) {
uci1 21:ce51bb0ba4a5 902 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 903 printf("PHY dattak powering up\r\n");
uci1 21:ce51bb0ba4a5 904 #endif
uci1 21:ce51bb0ba4a5 905 PHY_PowerUp(); wait(1);
uci1 21:ce51bb0ba4a5 906 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 907 printf("PHY dattak powered up\r\n");
uci1 21:ce51bb0ba4a5 908 #endif
uci1 21:ce51bb0ba4a5 909 } else {
uci1 21:ce51bb0ba4a5 910 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 911 printf("PHY dattak powering down\r\n");
uci1 21:ce51bb0ba4a5 912 #endif
uci1 21:ce51bb0ba4a5 913 PHY_PowerDown(); wait(1);
uci1 21:ce51bb0ba4a5 914 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 915 printf("PHY dattak powered down\r\n");
uci1 21:ce51bb0ba4a5 916 #endif
uci1 21:ce51bb0ba4a5 917 }
uci1 21:ce51bb0ba4a5 918 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 919 printf("PHY done\r\n");
uci1 21:ce51bb0ba4a5 920 #endif
uci1 21:ce51bb0ba4a5 921 wait_ms(100);
uci1 5:9cea89700c66 922 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak);
uci1 4:a91682e19d6b 923 wait_ms(10);
uci1 4:a91682e19d6b 924 }
uci1 16:744ce85aede2 925 wait(1.5); // let things power up
uci1 12:d472f9811262 926 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 927 printf("aft: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
uci1 21:ce51bb0ba4a5 928 LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
uci1 21:ce51bb0ba4a5 929 #endif
uci1 21:ce51bb0ba4a5 930 #ifdef DEBUG
uci1 16:744ce85aede2 931 printf("power word (%hhu): ",gConf.GetPowerMode()); SnBitUtils::printBits(gConf.GetPowerMode(),true);
uci1 6:6f002d202f59 932 printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 933 isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 934 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 21:ce51bb0ba4a5 935 printf("pcenet aft power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
uci1 12:d472f9811262 936 #endif
uci1 4:a91682e19d6b 937 }
uci1 4:a91682e19d6b 938
uci1 4:a91682e19d6b 939 //
uci1 0:664899e0b988 940 // set configuration
uci1 0:664899e0b988 941 //
uci1 1:e392595b4b76 942 void SetConfigAndMakeOutputFile() {
uci1 12:d472f9811262 943 #ifdef DEBUG
uci1 1:e392595b4b76 944 printf("SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 945 #endif
uci1 1:e392595b4b76 946
uci1 0:664899e0b988 947 // restart watchdog
uci1 0:664899e0b988 948 Watchdog::kick(gConf.GetWatchdogPeriod());
uci1 0:664899e0b988 949
uci1 1:e392595b4b76 950 // block (thermal) triggers during configuration
uci1 1:e392595b4b76 951 PIN_enableThermTrig = 0;
uci1 1:e392595b4b76 952 PIN_ADC_CS = 1;
uci1 1:e392595b4b76 953 PIN_DoNotRestartAllClocks = 1;
uci1 1:e392595b4b76 954 PIN_forceTrigger = 0;
uci1 3:24c5f0f50bf1 955 PIN_heartbeat = 0;
uci1 1:e392595b4b76 956 wait_ms(20);
uci1 1:e392595b4b76 957
uci1 22:f957c4f840ad 958 gCommWinChecks = 0;
uci1 22:f957c4f840ad 959 gNcommWinChecks = gConf.GetCommWinPeriod() / kCommWinLongPrdTk;
uci1 22:f957c4f840ad 960
uci1 21:ce51bb0ba4a5 961 if (AreCardsPowered(true)) {
uci1 8:95a325df1f6b 962 // Set PLA value(s)
uci1 8:95a325df1f6b 963 PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting
uci1 8:95a325df1f6b 964 PIN_spi.frequency(1000000);
uci1 8:95a325df1f6b 965 PIN_MajLogHiBit=1;
uci1 8:95a325df1f6b 966 PIN_MajLogLoBit=1;
uci1 8:95a325df1f6b 967 PIN_enableThermTrig=0;
uci1 0:664899e0b988 968
uci1 8:95a325df1f6b 969 uint16_t hi, lo;
uci1 8:95a325df1f6b 970 PIN_PLA_cs=1;
uci1 8:95a325df1f6b 971 wait(4);
uci1 8:95a325df1f6b 972 for (uint8_t pi=0; pi<kNplas; pi++) {
uci1 8:95a325df1f6b 973 if (pi < gConf.GetNumPlas()) {
uci1 8:95a325df1f6b 974 SnConfigFrame::GetHiLoPlas(gConf.GetPla(pi), hi, lo);
uci1 8:95a325df1f6b 975 PIN_spi.write(hi);
uci1 8:95a325df1f6b 976 PIN_spi.write(lo);
uci1 12:d472f9811262 977 #ifdef DEBUG
uci1 8:95a325df1f6b 978 printf("pla hi %hu, lo %hu\r\n",hi,lo);
uci1 12:d472f9811262 979 #endif
uci1 8:95a325df1f6b 980 } else {
uci1 8:95a325df1f6b 981 PIN_spi.write(kNoTrigPla); // hi
uci1 8:95a325df1f6b 982 PIN_spi.write(kNoTrigPla); // lo
uci1 12:d472f9811262 983 #ifdef DEBUG
uci1 8:95a325df1f6b 984 printf("pla hi %hu, lo %hu\r\n",kNoTrigPla,kNoTrigPla);
uci1 12:d472f9811262 985 #endif
uci1 8:95a325df1f6b 986 }
uci1 8:95a325df1f6b 987 Watchdog::kick();
uci1 0:664899e0b988 988 }
uci1 8:95a325df1f6b 989 wait(3);
uci1 8:95a325df1f6b 990 PIN_PLA_cs=0;
uci1 8:95a325df1f6b 991 wait(3);
uci1 0:664899e0b988 992
uci1 8:95a325df1f6b 993 // DAC values
uci1 8:95a325df1f6b 994 //
uci1 8:95a325df1f6b 995 // first 12 bits = DAC value
uci1 8:95a325df1f6b 996 // next 2 bits = DAC ID
uci1 8:95a325df1f6b 997 // last 2 bits = dFPGA ID
uci1 8:95a325df1f6b 998 //
uci1 8:95a325df1f6b 999 // But FPGA uses "gray encoding" which means only 1 bit
uci1 8:95a325df1f6b 1000 // can change at a time (of the last 4 bits). So even tho
uci1 8:95a325df1f6b 1001 // the card/dac# is encoded, the order is also important
uci1 8:95a325df1f6b 1002 // 0000 (dac0,card0), 0001 (dac0,card1), 0011 (dac0,card3), 0010 (dac0,card2),
uci1 8:95a325df1f6b 1003 // 0110 (dac1,card2), 0111 (dac1,card3), 0101 (dac1,card1), etc.
uci1 12:d472f9811262 1004 #ifdef DEBUG
uci1 8:95a325df1f6b 1005 printf("setting dacs\r\n");
uci1 12:d472f9811262 1006 #endif
uci1 8:95a325df1f6b 1007 uint16_t dv=0;
uci1 8:95a325df1f6b 1008 for (uint8_t i=0, gri=0; i<kTotDacs; i++) {
uci1 8:95a325df1f6b 1009 // get the gray-codes for this iteration
uci1 8:95a325df1f6b 1010 gri = SnBitUtils::binToGray(i);
uci1 8:95a325df1f6b 1011
uci1 8:95a325df1f6b 1012 // build bit word
uci1 8:95a325df1f6b 1013 dv = static_cast<int>(gConf.GetDac(gri & 0x0003u, gri >> 2u));
uci1 8:95a325df1f6b 1014 dv <<= 4u;
uci1 8:95a325df1f6b 1015 dv |= gri;
uci1 8:95a325df1f6b 1016
uci1 12:d472f9811262 1017 #ifdef DEBUG
uci1 8:95a325df1f6b 1018 printf("dac %04x\r\n",dv);
uci1 12:d472f9811262 1019 #endif
uci1 8:95a325df1f6b 1020
uci1 8:95a325df1f6b 1021 // send to FPGA
uci1 8:95a325df1f6b 1022 PIN_start_fpga=1;
uci1 8:95a325df1f6b 1023 PIN_spi.write(dv);
uci1 8:95a325df1f6b 1024 PIN_start_fpga=0;
uci1 8:95a325df1f6b 1025
uci1 8:95a325df1f6b 1026 Watchdog::kick();
uci1 8:95a325df1f6b 1027
uci1 8:95a325df1f6b 1028 }
uci1 12:d472f9811262 1029 #ifdef DEBUG
uci1 8:95a325df1f6b 1030 printf("dacs set\r\n");
uci1 12:d472f9811262 1031 #endif
uci1 8:95a325df1f6b 1032 wait_ms(20);
uci1 8:95a325df1f6b 1033 } else {
uci1 12:d472f9811262 1034 #ifdef DEBUG
uci1 8:95a325df1f6b 1035 printf("cards off. skipping PLA and DAC setting\r\n");
uci1 12:d472f9811262 1036 #endif
uci1 0:664899e0b988 1037 }
uci1 0:664899e0b988 1038
uci1 0:664899e0b988 1039 // Majority Logic Trigger selection (# of cards)
uci1 0:664899e0b988 1040 SnBitUtils::SetChanNumBits(gConf.GetNumCardsMajLog() - 1u,
uci1 0:664899e0b988 1041 PIN_MajLogHiBit, PIN_MajLogLoBit);
uci1 0:664899e0b988 1042
uci1 0:664899e0b988 1043 // Enable thermal trigger?
uci1 0:664899e0b988 1044 PIN_enableThermTrig = gConf.IsThermTrigEnabled();
uci1 0:664899e0b988 1045
uci1 0:664899e0b988 1046 PIN_spi.format( 16, 1 ); // back to trigger mode
uci1 1:e392595b4b76 1047 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 1:e392595b4b76 1048
uci1 8:95a325df1f6b 1049 // make new output file
uci1 8:95a325df1f6b 1050 // put after PLA/DAC, in case they affect the power readings
uci1 8:95a325df1f6b 1051 wait_ms(200);
uci1 8:95a325df1f6b 1052 MakeOutputFile();
uci1 8:95a325df1f6b 1053
uci1 8:95a325df1f6b 1054 // TODO: change comm parameters
uci1 8:95a325df1f6b 1055 /*
uci1 8:95a325df1f6b 1056 printf("set comm params\r\n");
uci1 8:95a325df1f6b 1057 for (uint8_t cc=0; cc<kNcomms; cc++) {
uci1 8:95a325df1f6b 1058 if (gComms[cc]!=0) {
uci1 8:95a325df1f6b 1059 gComms[cc]->Set(gConf);
uci1 8:95a325df1f6b 1060 }
uci1 3:24c5f0f50bf1 1061 }
uci1 8:95a325df1f6b 1062 */
uci1 21:ce51bb0ba4a5 1063
uci1 21:ce51bb0ba4a5 1064 // reset tickers
uci1 21:ce51bb0ba4a5 1065 ResetAllTickers();
uci1 0:664899e0b988 1066
uci1 0:664899e0b988 1067 Watchdog::kick(); // don't reset!
uci1 8:95a325df1f6b 1068
uci1 12:d472f9811262 1069 #ifdef DEBUG
uci1 8:95a325df1f6b 1070 printf("set config done\r\n");
uci1 12:d472f9811262 1071 #endif
uci1 0:664899e0b988 1072 }
uci1 0:664899e0b988 1073
uci1 0:664899e0b988 1074 //
uci1 0:664899e0b988 1075 // readout functions
uci1 0:664899e0b988 1076 //
uci1 0:664899e0b988 1077 void WaitTrigAndSendClock() {
uci1 1:e392595b4b76 1078
uci1 12:d472f9811262 1079 #ifdef DEBUG
uci1 1:e392595b4b76 1080 printf("WaitTrigAndSendClock\r\n");
uci1 6:6f002d202f59 1081 printf("wait trig: (pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 1082 gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 1083 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 21:ce51bb0ba4a5 1084 printf("cards powered=%d\r\n",(int)AreCardsPowered(true));
uci1 12:d472f9811262 1085 #endif
uci1 0:664899e0b988 1086
uci1 15:f2569d8e4176 1087 PIN_spi.format( 16, 1 ); // back to trigger mode
uci1 15:f2569d8e4176 1088 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 15:f2569d8e4176 1089
uci1 21:ce51bb0ba4a5 1090 if (AreCardsPowered(false)) {
uci1 8:95a325df1f6b 1091
uci1 21:ce51bb0ba4a5 1092 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1093 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 21:ce51bb0ba4a5 1094 #endif
uci1 8:95a325df1f6b 1095 if (gFirstEvt==false) {
uci1 8:95a325df1f6b 1096 PIN_DoNotRestartAllClocks = 0;
uci1 8:95a325df1f6b 1097 wait_us(1);
uci1 8:95a325df1f6b 1098 PIN_DoNotRestartAllClocks = 1;
uci1 8:95a325df1f6b 1099 //led3 = !led3; // toggle send clock led
uci1 8:95a325df1f6b 1100 } else {
uci1 8:95a325df1f6b 1101 gFirstEvt = false;
uci1 8:95a325df1f6b 1102 }
uci1 8:95a325df1f6b 1103
uci1 8:95a325df1f6b 1104 //
uci1 8:95a325df1f6b 1105 // wait for a trigger here.
uci1 8:95a325df1f6b 1106 //
uci1 12:d472f9811262 1107 #ifdef DEBUG
uci1 8:95a325df1f6b 1108 printf("starting wait for trig\r\n");
uci1 12:d472f9811262 1109 #endif
uci1 16:744ce85aede2 1110
uci1 8:95a325df1f6b 1111 gReadingOut = false; // this will allow forced triggers (see procForceTrigger())
uci1 8:95a325df1f6b 1112 while ( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 1113 if (gOpenCommWin || gCheckPower) {
uci1 12:d472f9811262 1114 #ifdef DEBUG
uci1 8:95a325df1f6b 1115 printf("break com=%d, pow=%d\r\n",gOpenCommWin,gCheckPower);
uci1 12:d472f9811262 1116 #endif
uci1 8:95a325df1f6b 1117 return; // break out to open comms or check power
uci1 8:95a325df1f6b 1118 }
uci1 0:664899e0b988 1119 }
uci1 21:ce51bb0ba4a5 1120 //PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers
uci1 8:95a325df1f6b 1121 gReadingOut = true; // disallow new forced triggers
uci1 15:f2569d8e4176 1122
uci1 21:ce51bb0ba4a5 1123 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1124 printf("after wait for trig. PIN_a_sf_clk=%d\r\n",
uci1 21:ce51bb0ba4a5 1125 PIN_a_sf_clk.read());
uci1 21:ce51bb0ba4a5 1126 #endif
uci1 21:ce51bb0ba4a5 1127
uci1 15:f2569d8e4176 1128 // we can't be interrupted before data arrives at the MB FPGA
uci1 15:f2569d8e4176 1129 //StopAllTickers();
uci1 15:f2569d8e4176 1130 /*
uci1 15:f2569d8e4176 1131 procForceTrigger();
uci1 15:f2569d8e4176 1132 procHeartbeat();
uci1 15:f2569d8e4176 1133 procPowerCheck();
uci1 15:f2569d8e4176 1134 procCommWin();
uci1 15:f2569d8e4176 1135 */
uci1 16:744ce85aede2 1136
uci1 23:ccf39298f205 1137 //wait_us(5);
uci1 16:744ce85aede2 1138
uci1 8:95a325df1f6b 1139 //
uci1 8:95a325df1f6b 1140 // collect data from daughter cards
uci1 8:95a325df1f6b 1141 //
uci1 8:95a325df1f6b 1142 // TODO: what if some card (set of channels) doesn't respond?
uci1 8:95a325df1f6b 1143 // currently, will wait forever?
uci1 8:95a325df1f6b 1144 // also, if ch1 is dead, will wait forever (due to FPGA code)
uci1 16:744ce85aede2 1145 gAdcToMBtimer.start();
uci1 16:744ce85aede2 1146 for( uint8_t i = 0; i < 128; ++i ) {
uci1 16:744ce85aede2 1147 while (PIN_a_sf_clk==1) {}
uci1 16:744ce85aede2 1148 while (PIN_a_sf_clk==0) {}
uci1 16:744ce85aede2 1149 /*
uci1 16:744ce85aede2 1150 if ((i == 10)&&(gEvtNum % 20)) {
uci1 16:744ce85aede2 1151 wait_us(8);
uci1 16:744ce85aede2 1152 }
uci1 16:744ce85aede2 1153 */
uci1 16:744ce85aede2 1154 PIN_ADC_CS = 0;
uci1 16:744ce85aede2 1155 PIN_spi.write( 0x00 );
uci1 16:744ce85aede2 1156 PIN_ADC_CS = 1;
uci1 16:744ce85aede2 1157 /*
uci1 8:95a325df1f6b 1158 if( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 1159 if( i == 0 )
uci1 8:95a325df1f6b 1160 wait_us( 1 );
uci1 8:95a325df1f6b 1161
uci1 8:95a325df1f6b 1162 PIN_ADC_CS = 0;
uci1 8:95a325df1f6b 1163 PIN_spi.write( 0x00 );
uci1 8:95a325df1f6b 1164 PIN_ADC_CS = 1;
uci1 8:95a325df1f6b 1165 } else {
uci1 8:95a325df1f6b 1166 i--;
uci1 8:95a325df1f6b 1167 }
uci1 16:744ce85aede2 1168 */
uci1 0:664899e0b988 1169 }
uci1 16:744ce85aede2 1170 gAdcToMBtimer.stop();
uci1 16:744ce85aede2 1171 #ifdef DEBUG
uci1 16:744ce85aede2 1172 printf("total time = %d us\r\n", gAdcToMBtimer.read_us());
uci1 16:744ce85aede2 1173 #endif
uci1 16:744ce85aede2 1174 if ( kAdcToMBtimeCut < gAdcToMBtimer.read_us() ) {
uci1 16:744ce85aede2 1175 gEvent.SetTrgBit(kAdcToMBflag);
uci1 16:744ce85aede2 1176 }
uci1 16:744ce85aede2 1177 gAdcToMBtimer.reset();
uci1 15:f2569d8e4176 1178
uci1 15:f2569d8e4176 1179 // restart the timers
uci1 15:f2569d8e4176 1180 //ResetAllTickers();
uci1 8:95a325df1f6b 1181 } else {
uci1 8:95a325df1f6b 1182 // cards have no power. don't try reading out
uci1 8:95a325df1f6b 1183 gReadingOut=false;
uci1 0:664899e0b988 1184 }
uci1 0:664899e0b988 1185 }
uci1 0:664899e0b988 1186
uci1 13:7a1fb885a8e4 1187 SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig) {
uci1 0:664899e0b988 1188 // loop through each comm mode:
uci1 0:664899e0b988 1189 // a) try to connect
uci1 0:664899e0b988 1190 // b) if connected, listen for config
uci1 0:664899e0b988 1191 // c) if config requests data, send it
uci1 16:744ce85aede2 1192 /*
uci1 16:744ce85aede2 1193 for (int i=0; i<5; i++) {
uci1 16:744ce85aede2 1194 led4=1;
uci1 16:744ce85aede2 1195 led3=1;
uci1 16:744ce85aede2 1196 wait(0.5);
uci1 16:744ce85aede2 1197 led4=0;
uci1 16:744ce85aede2 1198 led3=0;
uci1 16:744ce85aede2 1199 wait(0.5);
uci1 16:744ce85aede2 1200 }
uci1 16:744ce85aede2 1201 */
uci1 3:24c5f0f50bf1 1202 gLastCommWin = time(0);
uci1 13:7a1fb885a8e4 1203
uci1 0:664899e0b988 1204 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 0:664899e0b988 1205
uci1 21:ce51bb0ba4a5 1206 // get the trigger rates
uci1 21:ce51bb0ba4a5 1207 float thmrate=0, evtrate=0;
uci1 21:ce51bb0ba4a5 1208 GetRates(thmrate, evtrate);
uci1 22:f957c4f840ad 1209 #ifdef DEBUG
uci1 22:f957c4f840ad 1210 printf("thmrate=%g, evtrate=%g\r\n",thmrate,evtrate);
uci1 22:f957c4f840ad 1211 #endif
uci1 21:ce51bb0ba4a5 1212
uci1 21:ce51bb0ba4a5 1213 StopAllTickers();
uci1 13:7a1fb885a8e4 1214
uci1 13:7a1fb885a8e4 1215 if (gConf.GetCommWinDuration()==0) {
uci1 13:7a1fb885a8e4 1216 // TODO: set min so this is not possible
uci1 13:7a1fb885a8e4 1217 res = SnCommWin::kOkNoMsg;
uci1 13:7a1fb885a8e4 1218 } else {
uci1 13:7a1fb885a8e4 1219
uci1 13:7a1fb885a8e4 1220 gCommWinOpen = true;
uci1 1:e392595b4b76 1221 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1222
uci1 13:7a1fb885a8e4 1223 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1224 printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
uci1 16:744ce85aede2 1225 printf("duration=%u\r\n",gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1226 #endif
uci1 13:7a1fb885a8e4 1227
uci1 13:7a1fb885a8e4 1228 // close the file so that the data is all written out.
uci1 13:7a1fb885a8e4 1229 // and open it back up at the beginning (for reading)
uci1 12:d472f9811262 1230 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1231 printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
uci1 12:d472f9811262 1232 #endif
uci1 13:7a1fb885a8e4 1233 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 13:7a1fb885a8e4 1234 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 13:7a1fb885a8e4 1235 SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
uci1 13:7a1fb885a8e4 1236
uci1 21:ce51bb0ba4a5 1237 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1238 printf("setting power\r\n");
uci1 21:ce51bb0ba4a5 1239 #endif
uci1 13:7a1fb885a8e4 1240 // (probably) power down cards,amps and power up comms
uci1 13:7a1fb885a8e4 1241 SetPower(true);
uci1 13:7a1fb885a8e4 1242
uci1 21:ce51bb0ba4a5 1243 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1244 printf("start loop over comms\r\n");
uci1 21:ce51bb0ba4a5 1245 #endif
uci1 13:7a1fb885a8e4 1246 bool sendStat[kNcomms];
uci1 13:7a1fb885a8e4 1247 for (uint8_t i=0; i<kNcomms; i++) {
uci1 13:7a1fb885a8e4 1248 sendStat[i]=true;
uci1 13:7a1fb885a8e4 1249 }
uci1 13:7a1fb885a8e4 1250 bool* ss = sendStat;
uci1 13:7a1fb885a8e4 1251 SnCommWin** cw = gComms;
uci1 13:7a1fb885a8e4 1252 for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
uci1 1:e392595b4b76 1253 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1254 if (i==kNcomms) {
uci1 13:7a1fb885a8e4 1255 i=0;
uci1 13:7a1fb885a8e4 1256 cw = gComms;
uci1 13:7a1fb885a8e4 1257 ss = sendStat;
uci1 13:7a1fb885a8e4 1258 }
uci1 13:7a1fb885a8e4 1259 if ((*cw)==0) {
uci1 13:7a1fb885a8e4 1260 continue;
uci1 13:7a1fb885a8e4 1261 }
uci1 16:744ce85aede2 1262
uci1 16:744ce85aede2 1263 const uint32_t conto =
uci1 16:744ce85aede2 1264 (gConf.GetCommWinDuration() < (*cw)->GetConnectTimeout()) ?
uci1 16:744ce85aede2 1265 gConf.GetCommWinDuration() : (*cw)->GetConnectTimeout();
uci1 16:744ce85aede2 1266 const uint32_t listo =
uci1 16:744ce85aede2 1267 (gConf.GetCommWinDuration() < (*cw)->GetListenTimeout()) ?
uci1 16:744ce85aede2 1268 gConf.GetCommWinDuration() : (*cw)->GetListenTimeout();
uci1 16:744ce85aede2 1269
uci1 16:744ce85aede2 1270 // update power reading in case we want to send it in status
uci1 16:744ce85aede2 1271 GetAvePowerReading();
uci1 21:ce51bb0ba4a5 1272
uci1 13:7a1fb885a8e4 1273 // open window and (mabye) send status update
uci1 12:d472f9811262 1274 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1275 printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
uci1 21:ce51bb0ba4a5 1276 printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",gConf.GetTimeoutTime(gLastCommWin,conto),
uci1 13:7a1fb885a8e4 1277 time(0), gLastCommWin, gConf.GetCommWinDuration());
uci1 12:d472f9811262 1278 #endif
uci1 13:7a1fb885a8e4 1279 const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow(
uci1 21:ce51bb0ba4a5 1280 gConf.GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower,
uci1 13:7a1fb885a8e4 1281 SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
uci1 13:7a1fb885a8e4 1282 gGenBuf);
uci1 13:7a1fb885a8e4 1283 if (conres>=SnCommWin::kConnected) {
uci1 1:e392595b4b76 1284 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1285 // connected. listen for config
uci1 13:7a1fb885a8e4 1286 *ss = false; // don't send status next time
uci1 12:d472f9811262 1287 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1288 printf("get conf gtt=%u\r\n",gConf.GetTimeoutTime(gLastCommWin, listo));
uci1 12:d472f9811262 1289 #endif
uci1 13:7a1fb885a8e4 1290 const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
uci1 21:ce51bb0ba4a5 1291 gConf, gConf.GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);
uci1 16:744ce85aede2 1292
uci1 13:7a1fb885a8e4 1293 if (cfgres>=SnCommWin::kOkWithMsg) {
uci1 13:7a1fb885a8e4 1294 Watchdog::kick(); // don't reset!
uci1 16:744ce85aede2 1295
uci1 12:d472f9811262 1296 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1297 printf("received config!\r\n");
uci1 13:7a1fb885a8e4 1298 printf("send data = %d\r\n", gConf.GetCommSendData());
uci1 12:d472f9811262 1299 #endif
uci1 13:7a1fb885a8e4 1300 // send data if need be (files, some events, etc)
uci1 21:ce51bb0ba4a5 1301 const uint32_t winto = gConf.GetTimeoutTime(gLastCommWin,
uci1 21:ce51bb0ba4a5 1302 gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1303 const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
uci1 13:7a1fb885a8e4 1304 if (gConf.GetCommSendData()!=0) {
uci1 12:d472f9811262 1305 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1306 printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n",
uci1 21:ce51bb0ba4a5 1307 gConf.GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()),
uci1 13:7a1fb885a8e4 1308 gLastCommWin, gConf.GetCommWinDuration(),
uci1 13:7a1fb885a8e4 1309 gConf.IsObeyingTimeout() ? "true" : "false");
uci1 12:d472f9811262 1310 #endif
uci1 16:744ce85aede2 1311
uci1 13:7a1fb885a8e4 1312 res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize,
uci1 21:ce51bb0ba4a5 1313 gtt, gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1314 } else {
uci1 13:7a1fb885a8e4 1315 // don't send anything
uci1 13:7a1fb885a8e4 1316 res = cfgres;
uci1 13:7a1fb885a8e4 1317 }
uci1 13:7a1fb885a8e4 1318 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1319 printf("Got config!\r\n");
uci1 13:7a1fb885a8e4 1320 #endif
uci1 13:7a1fb885a8e4 1321 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1322 break;
uci1 13:7a1fb885a8e4 1323 }
uci1 21:ce51bb0ba4a5 1324 } else {
uci1 21:ce51bb0ba4a5 1325 (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin, listo));
uci1 21:ce51bb0ba4a5 1326 } // if connected
uci1 13:7a1fb885a8e4 1327
uci1 13:7a1fb885a8e4 1328 Watchdog::kick(); // don't reset!
uci1 21:ce51bb0ba4a5 1329 } // end loop over comms
uci1 15:f2569d8e4176 1330 // close the connection(s)
uci1 15:f2569d8e4176 1331 cw = gComms;
uci1 15:f2569d8e4176 1332 for (uint8_t i=0; i<kNcomms; i++, cw++) {
uci1 15:f2569d8e4176 1333 if ((*cw)==0) {
uci1 15:f2569d8e4176 1334 continue;
uci1 15:f2569d8e4176 1335 }
uci1 16:744ce85aede2 1336 if ((*cw)->GetCommType()==SnConfigFrame::kIrid) {
uci1 16:744ce85aede2 1337 #ifdef DEBUG
uci1 16:744ce85aede2 1338 printf("try to set iridium time\r\n");
uci1 16:744ce85aede2 1339 #endif
uci1 16:744ce85aede2 1340 // set the clock before closing connection
uci1 16:744ce85aede2 1341 const bool con =
uci1 21:ce51bb0ba4a5 1342 (*cw)->Connect(gConf.GetTimeoutTime(gLastCommWin,
uci1 16:744ce85aede2 1343 gConf.GetCommWinDuration()));
uci1 16:744ce85aede2 1344 if (con) {
uci1 16:744ce85aede2 1345 const uint32_t nt = (*cw)->TrySetSysTimeUnix(
uci1 21:ce51bb0ba4a5 1346 gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
uci1 16:744ce85aede2 1347 }
uci1 16:744ce85aede2 1348 }
uci1 21:ce51bb0ba4a5 1349 (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
uci1 15:f2569d8e4176 1350 Watchdog::kick(); // don't reset!
uci1 15:f2569d8e4176 1351 }
uci1 0:664899e0b988 1352 }
uci1 12:d472f9811262 1353
uci1 4:a91682e19d6b 1354 // (probably) power down comms and power up cards,amps
uci1 4:a91682e19d6b 1355 SetPower(false);
uci1 4:a91682e19d6b 1356
uci1 1:e392595b4b76 1357 // reset config with system powered (for DAC/PLA setting)
uci1 12:d472f9811262 1358 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1359 printf("calling SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 1360 #endif
uci1 22:f957c4f840ad 1361
uci1 21:ce51bb0ba4a5 1362 SetConfigAndMakeOutputFile();
uci1 21:ce51bb0ba4a5 1363
uci1 12:d472f9811262 1364 #ifdef DEBUG
uci1 1:e392595b4b76 1365 printf("closing comm win at %d\r\n",(int32_t)time(0));
uci1 12:d472f9811262 1366 #endif
uci1 1:e392595b4b76 1367
uci1 0:664899e0b988 1368 gCommWinOpen = false;
uci1 0:664899e0b988 1369 return res;
uci1 0:664899e0b988 1370 }