Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 16 04:47:44 2012 +0000
Revision:
22:f957c4f840ad
Parent:
21:ce51bb0ba4a5
Child:
23:ccf39298f205
USB comm only. Make firing of comm window independent of real time clock. Add heartbeat firing time to the data stream.

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 22:f957c4f840ad 169 static 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 21:ce51bb0ba4a5 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 21:ce51bb0ba4a5 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 18:55f1581f2ee4 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 16:744ce85aede2 531 //gComms[0] = new SnCommSBD(&gSBDport, &gCpu);
uci1 16:744ce85aede2 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 22:f957c4f840ad 605
uci1 22:f957c4f840ad 606 if (gHrtbtFired) {
uci1 22:f957c4f840ad 607 SaveHeartbeat();
uci1 22:f957c4f840ad 608 gHrtbtFired=false;
uci1 22:f957c4f840ad 609 }
uci1 22:f957c4f840ad 610
uci1 12:d472f9811262 611 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 612 Timer prof;
uci1 12:d472f9811262 613 prof.start();
uci1 12:d472f9811262 614 int befReadWv=0, aftReadWv=0, befSaveEvt=0, aftSaveEvt=0,
uci1 12:d472f9811262 615 befChkPow=0, aftChkPow=0, befNewSeq=0, aftNewSeq=0, endOfLoop=0;
uci1 12:d472f9811262 616 #endif
uci1 12:d472f9811262 617
uci1 1:e392595b4b76 618 if (gReadingOut) {
uci1 12:d472f9811262 619
uci1 12:d472f9811262 620 const int32_t ttms = gTrgTimer.read_ms(); // time since last trigger
uci1 12:d472f9811262 621 gTrgTimer.reset(); gTrgTimer.start(); // restart trigger timer
uci1 12:d472f9811262 622 etms += ttms;
uci1 8:95a325df1f6b 623
uci1 8:95a325df1f6b 624 Watchdog::kick(); // don't reset!
uci1 15:f2569d8e4176 625
uci1 1:e392595b4b76 626 //
uci1 1:e392595b4b76 627 // got trigger. read registers to mbed and build the event
uci1 1:e392595b4b76 628 //
uci1 1:e392595b4b76 629
uci1 1:e392595b4b76 630 led4=1;
uci1 1:e392595b4b76 631
uci1 22:f957c4f840ad 632 if ( gEvent.IsForcedTrg() || gFirstEvt ||
uci1 22:f957c4f840ad 633 (etms>gConf.GetEvtThrtlPeriodMs()) ) {
uci1 22:f957c4f840ad 634
uci1 22:f957c4f840ad 635 // read data & calc CRC
uci1 12:d472f9811262 636 #ifdef EVT_TIME_PROFILE
uci1 22:f957c4f840ad 637 prof.stop(); befReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 638 #endif
uci1 12:d472f9811262 639
uci1 22:f957c4f840ad 640 // get the data to the MBED
uci1 22:f957c4f840ad 641 gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit);
uci1 15:f2569d8e4176 642
uci1 12:d472f9811262 643 #ifdef EVT_TIME_PROFILE
uci1 22:f957c4f840ad 644 prof.stop(); aftReadWv=prof.read_us(); prof.start();
uci1 12:d472f9811262 645 #endif
uci1 12:d472f9811262 646
uci1 22:f957c4f840ad 647 gEvent.SetCurMbedTime();
uci1 22:f957c4f840ad 648 // TODO: no way to check for external trigger?
uci1 22:f957c4f840ad 649 if (gEvent.IsForcedTrg()==false) {
uci1 22:f957c4f840ad 650 gEvent.SetTrgBit(kThmTrg);
uci1 22:f957c4f840ad 651 gEvent.SetTrgNum(++(gTrgNum[kThmTrg]));
uci1 22:f957c4f840ad 652 AddToRate(ttms, true);
uci1 22:f957c4f840ad 653 } // else already set by procForceTrigger
uci1 22:f957c4f840ad 654 // (no need to calc if we throw this event away)
uci1 22:f957c4f840ad 655
uci1 22:f957c4f840ad 656 Watchdog::kick(); // don't reset!
uci1 22:f957c4f840ad 657
uci1 12:d472f9811262 658 #ifdef DEBUG
uci1 22:f957c4f840ad 659 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 12:d472f9811262 660 #endif
uci1 1:e392595b4b76 661
uci1 1:e392595b4b76 662 led2=1;
uci1 22:f957c4f840ad 663 /*
uci1 21:ce51bb0ba4a5 664 gRecentCountTime = static_cast<uint32_t>(time(0));
uci1 21:ce51bb0ba4a5 665 gRecentEvtNum = gEvtNum;
uci1 21:ce51bb0ba4a5 666 // do start time second so that if we get only one event,
uci1 21:ce51bb0ba4a5 667 // the delta(t) will be less than 0 and the rates not calculated
uci1 21:ce51bb0ba4a5 668 if (gDoResetLastCount) {
uci1 21:ce51bb0ba4a5 669 gLastCountReset = static_cast<uint32_t>(time(0)); // to calc rates
uci1 21:ce51bb0ba4a5 670 gLastEventReset = gEvtNum;
uci1 21:ce51bb0ba4a5 671 gDoResetLastCount = false;
uci1 21:ce51bb0ba4a5 672 }
uci1 22:f957c4f840ad 673 */
uci1 21:ce51bb0ba4a5 674
uci1 1:e392595b4b76 675 PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card.
uci1 1:e392595b4b76 676
uci1 12:d472f9811262 677 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 678 prof.stop(); befSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 679 #endif
uci1 12:d472f9811262 680
uci1 1:e392595b4b76 681 SaveEvent(etms);
uci1 22:f957c4f840ad 682 AddToRate(etms, false);
uci1 12:d472f9811262 683 etms=0;
uci1 8:95a325df1f6b 684
uci1 12:d472f9811262 685 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 686 prof.stop(); aftSaveEvt=prof.read_us(); prof.start();
uci1 12:d472f9811262 687 #endif
uci1 1:e392595b4b76 688 }
uci1 1:e392595b4b76 689 }
uci1 12:d472f9811262 690 #ifdef DEBUG
uci1 1:e392595b4b76 691 printf("past reading out\r\n");
uci1 12:d472f9811262 692 #endif
uci1 1:e392595b4b76 693
uci1 1:e392595b4b76 694 led4=0; led2=0;
uci1 12:d472f9811262 695
uci1 12:d472f9811262 696 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 697 prof.stop(); befChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 698 #endif
uci1 8:95a325df1f6b 699 // check the power?
uci1 8:95a325df1f6b 700 if (gCheckPower) {
uci1 12:d472f9811262 701 #ifdef DEBUG
uci1 8:95a325df1f6b 702 printf("call check power\r\n");
uci1 12:d472f9811262 703 #endif
uci1 8:95a325df1f6b 704 CheckPower(false);
uci1 8:95a325df1f6b 705 }
uci1 12:d472f9811262 706 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 707 prof.stop(); aftChkPow=prof.read_us(); prof.start();
uci1 12:d472f9811262 708 #endif
uci1 8:95a325df1f6b 709
uci1 21:ce51bb0ba4a5 710 // open comm win?
uci1 21:ce51bb0ba4a5 711 if (gOpenCommWin) {
uci1 21:ce51bb0ba4a5 712 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 713 printf("gOpenComWin=%s, opening\r\n",gOpenCommWin?"true":"false");
uci1 21:ce51bb0ba4a5 714 #endif
uci1 21:ce51bb0ba4a5 715 OpenCommWin();
uci1 21:ce51bb0ba4a5 716 gOpenCommWin=false;
uci1 22:f957c4f840ad 717 gFirstEvt = true;
uci1 22:f957c4f840ad 718 gTrgTimer.reset();
uci1 22:f957c4f840ad 719 etms=0;
uci1 21:ce51bb0ba4a5 720 } else {
uci1 21:ce51bb0ba4a5 721 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 722 printf("gOpenCommWin=false\r\n");
uci1 21:ce51bb0ba4a5 723 #endif
uci1 21:ce51bb0ba4a5 724 }
uci1 21:ce51bb0ba4a5 725
uci1 12:d472f9811262 726 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 727 prof.stop(); befNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 728 #endif
uci1 8:95a325df1f6b 729 // make new seq?
uci1 8:95a325df1f6b 730 if (IsSeqComplete()) {
uci1 12:d472f9811262 731 #ifdef DEBUG
uci1 10:3c93db1cfb12 732 printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
uci1 12:d472f9811262 733 #endif
uci1 8:95a325df1f6b 734 MakeOutputFile(gConf.IsSingleSeqRunMode());
uci1 22:f957c4f840ad 735 gFirstEvt = true;
uci1 22:f957c4f840ad 736 gTrgTimer.reset();
uci1 22:f957c4f840ad 737 etms=0;
uci1 8:95a325df1f6b 738 }
uci1 12:d472f9811262 739 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 740 prof.stop(); aftNewSeq=prof.read_us(); prof.start();
uci1 12:d472f9811262 741 #endif
uci1 12:d472f9811262 742
uci1 12:d472f9811262 743 #ifdef EVT_TIME_PROFILE
uci1 12:d472f9811262 744 prof.stop(); endOfLoop=prof.read_us(); prof.start();
uci1 12:d472f9811262 745 printf("befReadWv=%d, aftReadWv=%d, befSaveEvt=%d, aftSaveEvt=%d, "
uci1 12:d472f9811262 746 "befChkPow=%d, aftChkPow=%d, befNewSeq=%d, aftNewSeq=%d, endOfLoop=%d\r\n",
uci1 12:d472f9811262 747 befReadWv, aftReadWv, befSaveEvt, aftSaveEvt,
uci1 12:d472f9811262 748 befChkPow, aftChkPow, befNewSeq, aftNewSeq, endOfLoop);
uci1 12:d472f9811262 749 #endif
uci1 15:f2569d8e4176 750
uci1 15:f2569d8e4176 751 // get ready to trigger
uci1 15:f2569d8e4176 752 PIN_spi.format( 16, 1 ); // change to data readout format
uci1 15:f2569d8e4176 753 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 22:f957c4f840ad 754
uci1 21:ce51bb0ba4a5 755 // reset event
uci1 22:f957c4f840ad 756 // clear after comm win, so full event can be sent with status
uci1 22:f957c4f840ad 757 // but don't clear counters or trigger bits, as
uci1 22:f957c4f840ad 758 gEvent.ClearEvent(true);
uci1 21:ce51bb0ba4a5 759
uci1 0:664899e0b988 760 }
uci1 0:664899e0b988 761
uci1 0:664899e0b988 762 }
uci1 0:664899e0b988 763
uci1 0:664899e0b988 764 //
uci1 22:f957c4f840ad 765 // save a heartbeat tag
uci1 22:f957c4f840ad 766 //
uci1 22:f957c4f840ad 767 void SaveHeartbeat() {
uci1 22:f957c4f840ad 768 if (gHrtbtNum>0) {
uci1 22:f957c4f840ad 769 #ifdef DEBUG
uci1 22:f957c4f840ad 770 printf("save heartbeat #%u, time %u\r\n",
uci1 22:f957c4f840ad 771 gHrtbtNum-1, gLastHrtbt);
uci1 22:f957c4f840ad 772 #endif
uci1 22:f957c4f840ad 773 // save to SD
uci1 22:f957c4f840ad 774 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 22:f957c4f840ad 775 SnSDUtils::WriteHeartbeatTo(SnSDUtils::GetCurFile(),
uci1 22:f957c4f840ad 776 gLastHrtbt, gHrtbtNum-1); // -1 so it counts from 0
uci1 22:f957c4f840ad 777 }
uci1 22:f957c4f840ad 778 }
uci1 22:f957c4f840ad 779
uci1 22:f957c4f840ad 780 //
uci1 0:664899e0b988 781 // save the event
uci1 0:664899e0b988 782 //
uci1 0:664899e0b988 783 void SaveEvent(const int32_t etms) {
uci1 0:664899e0b988 784 // write the event
uci1 12:d472f9811262 785
uci1 12:d472f9811262 786 #ifdef DEBUG
uci1 3:24c5f0f50bf1 787 printf("save event\r\n");
uci1 12:d472f9811262 788 #endif
uci1 3:24c5f0f50bf1 789
uci1 0:664899e0b988 790 // set the event number & dt
uci1 3:24c5f0f50bf1 791 gEvent.SetEvtNum(gEvtNum);
uci1 0:664899e0b988 792 gEvent.SetDTms(etms);
uci1 0:664899e0b988 793
uci1 0:664899e0b988 794 // save to SD
uci1 11:de443350ec4a 795 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 1:e392595b4b76 796 SnSDUtils::WriteEventTo(SnSDUtils::GetCurFile(), gGenBuf, gEvent, gConf);
uci1 0:664899e0b988 797
uci1 3:24c5f0f50bf1 798 // increment event number
uci1 3:24c5f0f50bf1 799 ++gEvtNum;
uci1 3:24c5f0f50bf1 800
uci1 12:d472f9811262 801 #ifdef DEBUG
uci1 8:95a325df1f6b 802 printf("gEvtNum=%u\r\n",gEvtNum);
uci1 12:d472f9811262 803 #endif
uci1 3:24c5f0f50bf1 804 }
uci1 3:24c5f0f50bf1 805
uci1 3:24c5f0f50bf1 806 void MakeOutputFile(const bool stopRunning) {
uci1 10:3c93db1cfb12 807 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 12:d472f9811262 808 #ifdef DEBUG
uci1 10:3c93db1cfb12 809 printf("closing output file. gEvtNum=%u, gPowNum=%u, stop=%d\r\n",
uci1 10:3c93db1cfb12 810 gEvtNum,gPowNum,(int)stopRunning);
uci1 12:d472f9811262 811 #endif
uci1 13:7a1fb885a8e4 812
uci1 3:24c5f0f50bf1 813 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 13:7a1fb885a8e4 814
uci1 12:d472f9811262 815 #ifdef DEBUG
uci1 10:3c93db1cfb12 816 printf("file closed\r\n");
uci1 12:d472f9811262 817 #endif
uci1 3:24c5f0f50bf1 818 if (stopRunning) {
uci1 8:95a325df1f6b 819 StopRunning();
uci1 0:664899e0b988 820 }
uci1 8:95a325df1f6b 821 FILE* cf = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(),
uci1 8:95a325df1f6b 822 gConf.GetRun());
uci1 13:7a1fb885a8e4 823 // reset event, timers, trigger counters
uci1 13:7a1fb885a8e4 824 ResetCountersClearEvt();
uci1 8:95a325df1f6b 825 if (cf!=0) {
uci1 8:95a325df1f6b 826 wait_ms(200);
uci1 8:95a325df1f6b 827 GetAvePowerReading();
uci1 12:d472f9811262 828 #ifdef DEBUG
uci1 8:95a325df1f6b 829 printf("writing power. v1=%g, v2=%g, r1=%g, r2=%g, t=%u, pownum=%u\r\n",
uci1 8:95a325df1f6b 830 gPower.GetAveV1(), gPower.GetAveV2(),
uci1 8:95a325df1f6b 831 gPower.GetRmsV1(), gPower.GetRmsV2(), gPower.GetTime(),
uci1 8:95a325df1f6b 832 gPowNum);
uci1 12:d472f9811262 833 #endif
uci1 8:95a325df1f6b 834 SnSDUtils::WritePowerTo(cf, gPower, gPowNum);
uci1 8:95a325df1f6b 835 }
uci1 12:d472f9811262 836 #ifdef DEBUG
uci1 3:24c5f0f50bf1 837 printf("made output file with run %u\r\n",gConf.GetRun());
uci1 3:24c5f0f50bf1 838 printf("filename=%s\r\n",SnSDUtils::GetCurFileName());
uci1 12:d472f9811262 839 #endif
uci1 3:24c5f0f50bf1 840 SnSDUtils::WriteConfig(SnSDUtils::GetCurFile(), gConf);
uci1 19:74155d652c37 841 #ifdef DEBUG
uci1 19:74155d652c37 842 printf("write config to file\r\n");
uci1 19:74155d652c37 843 #endif
uci1 0:664899e0b988 844 }
uci1 0:664899e0b988 845
uci1 0:664899e0b988 846 //
uci1 4:a91682e19d6b 847 // power stuff
uci1 4:a91682e19d6b 848 //
uci1 4:a91682e19d6b 849 void SetPower(const bool isCommWin) {
uci1 21:ce51bb0ba4a5 850 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 851 printf("bef: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
uci1 21:ce51bb0ba4a5 852 LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
uci1 21:ce51bb0ba4a5 853 printf("pcenet bef power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
uci1 21:ce51bb0ba4a5 854 #endif
uci1 8:95a325df1f6b 855 // TODO: turn on amps individually, when that's possible
uci1 4:a91682e19d6b 856 if (isCommWin) {
uci1 5:9cea89700c66 857 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardComWin);
uci1 4:a91682e19d6b 858 wait_ms(10);
uci1 5:9cea89700c66 859 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsComWin);
uci1 4:a91682e19d6b 860 wait_ms(10);
uci1 5:9cea89700c66 861 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridComWin);
uci1 4:a91682e19d6b 862 wait_ms(10);
uci1 21:ce51bb0ba4a5 863 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 864 printf("afar pin=%d, com powsetting=%d\r\n",PIN_afar_power.read(),
uci1 21:ce51bb0ba4a5 865 gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin));
uci1 21:ce51bb0ba4a5 866 #endif
uci1 21:ce51bb0ba4a5 867 if (gConf.IsPoweredFor(SnConfigFrame::kAfarComWin)) {
uci1 21:ce51bb0ba4a5 868 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 869 printf("PHY cowin powering up\r\n");
uci1 21:ce51bb0ba4a5 870 #endif
uci1 21:ce51bb0ba4a5 871 PHY_PowerUp(); wait(1);
uci1 21:ce51bb0ba4a5 872 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 873 printf("PHY cowin powered up\r\n");
uci1 21:ce51bb0ba4a5 874 #endif
uci1 21:ce51bb0ba4a5 875 } else {
uci1 21:ce51bb0ba4a5 876 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 877 printf("PHY cowin powering down\r\n");
uci1 21:ce51bb0ba4a5 878 #endif
uci1 21:ce51bb0ba4a5 879 PHY_PowerDown(); wait(1);
uci1 21:ce51bb0ba4a5 880 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 881 printf("PHY cowin powered down\r\n");
uci1 21:ce51bb0ba4a5 882 #endif
uci1 21:ce51bb0ba4a5 883 }
uci1 21:ce51bb0ba4a5 884 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 885 printf("PHY done\r\n");
uci1 21:ce51bb0ba4a5 886 #endif
uci1 21:ce51bb0ba4a5 887 wait_ms(100);
uci1 5:9cea89700c66 888 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarComWin);
uci1 4:a91682e19d6b 889 wait_ms(10);
uci1 4:a91682e19d6b 890 } else {
uci1 5:9cea89700c66 891 PIN_turn_on_system = gConf.GetPowPinSetting(SnConfigFrame::kCardDatTak);
uci1 4:a91682e19d6b 892 wait_ms(10);
uci1 5:9cea89700c66 893 PIN_turn_on_amps = gConf.GetPowPinSetting(SnConfigFrame::kAmpsDatTak);
uci1 4:a91682e19d6b 894 wait_ms(10);
uci1 5:9cea89700c66 895 PIN_iridSbd_power = gConf.GetPowPinSetting(SnConfigFrame::kIridDatTak);
uci1 4:a91682e19d6b 896 wait_ms(10);
uci1 21:ce51bb0ba4a5 897 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 898 printf("afar pin=%d, dat powsetting=%d, ispow=%d\r\n",PIN_afar_power.read(),
uci1 21:ce51bb0ba4a5 899 gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak),
uci1 21:ce51bb0ba4a5 900 (int)(gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak)));
uci1 21:ce51bb0ba4a5 901 #endif
uci1 21:ce51bb0ba4a5 902 if (gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak)) {
uci1 21:ce51bb0ba4a5 903 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 904 printf("PHY dattak powering up\r\n");
uci1 21:ce51bb0ba4a5 905 #endif
uci1 21:ce51bb0ba4a5 906 PHY_PowerUp(); wait(1);
uci1 21:ce51bb0ba4a5 907 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 908 printf("PHY dattak powered up\r\n");
uci1 21:ce51bb0ba4a5 909 #endif
uci1 21:ce51bb0ba4a5 910 } else {
uci1 21:ce51bb0ba4a5 911 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 912 printf("PHY dattak powering down\r\n");
uci1 21:ce51bb0ba4a5 913 #endif
uci1 21:ce51bb0ba4a5 914 PHY_PowerDown(); wait(1);
uci1 21:ce51bb0ba4a5 915 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 916 printf("PHY dattak powered down\r\n");
uci1 21:ce51bb0ba4a5 917 #endif
uci1 21:ce51bb0ba4a5 918 }
uci1 21:ce51bb0ba4a5 919 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 920 printf("PHY done\r\n");
uci1 21:ce51bb0ba4a5 921 #endif
uci1 21:ce51bb0ba4a5 922 wait_ms(100);
uci1 5:9cea89700c66 923 PIN_afar_power = gConf.GetPowPinSetting(SnConfigFrame::kAfarDatTak);
uci1 4:a91682e19d6b 924 wait_ms(10);
uci1 4:a91682e19d6b 925 }
uci1 16:744ce85aede2 926 wait(1.5); // let things power up
uci1 12:d472f9811262 927 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 928 printf("aft: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
uci1 21:ce51bb0ba4a5 929 LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
uci1 21:ce51bb0ba4a5 930 #endif
uci1 21:ce51bb0ba4a5 931 #ifdef DEBUG
uci1 16:744ce85aede2 932 printf("power word (%hhu): ",gConf.GetPowerMode()); SnBitUtils::printBits(gConf.GetPowerMode(),true);
uci1 6:6f002d202f59 933 printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 934 isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 935 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 21:ce51bb0ba4a5 936 printf("pcenet aft power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
uci1 12:d472f9811262 937 #endif
uci1 4:a91682e19d6b 938 }
uci1 4:a91682e19d6b 939
uci1 4:a91682e19d6b 940 //
uci1 0:664899e0b988 941 // set configuration
uci1 0:664899e0b988 942 //
uci1 1:e392595b4b76 943 void SetConfigAndMakeOutputFile() {
uci1 12:d472f9811262 944 #ifdef DEBUG
uci1 1:e392595b4b76 945 printf("SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 946 #endif
uci1 1:e392595b4b76 947
uci1 0:664899e0b988 948 // restart watchdog
uci1 0:664899e0b988 949 Watchdog::kick(gConf.GetWatchdogPeriod());
uci1 0:664899e0b988 950
uci1 1:e392595b4b76 951 // block (thermal) triggers during configuration
uci1 1:e392595b4b76 952 PIN_enableThermTrig = 0;
uci1 1:e392595b4b76 953 PIN_ADC_CS = 1;
uci1 1:e392595b4b76 954 PIN_DoNotRestartAllClocks = 1;
uci1 1:e392595b4b76 955 PIN_forceTrigger = 0;
uci1 3:24c5f0f50bf1 956 PIN_heartbeat = 0;
uci1 1:e392595b4b76 957 wait_ms(20);
uci1 1:e392595b4b76 958
uci1 22:f957c4f840ad 959 gCommWinChecks = 0;
uci1 22:f957c4f840ad 960 gNcommWinChecks = gConf.GetCommWinPeriod() / kCommWinLongPrdTk;
uci1 22:f957c4f840ad 961
uci1 21:ce51bb0ba4a5 962 if (AreCardsPowered(true)) {
uci1 8:95a325df1f6b 963 // Set PLA value(s)
uci1 8:95a325df1f6b 964 PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting
uci1 8:95a325df1f6b 965 PIN_spi.frequency(1000000);
uci1 8:95a325df1f6b 966 PIN_MajLogHiBit=1;
uci1 8:95a325df1f6b 967 PIN_MajLogLoBit=1;
uci1 8:95a325df1f6b 968 PIN_enableThermTrig=0;
uci1 0:664899e0b988 969
uci1 8:95a325df1f6b 970 uint16_t hi, lo;
uci1 8:95a325df1f6b 971 PIN_PLA_cs=1;
uci1 8:95a325df1f6b 972 wait(4);
uci1 8:95a325df1f6b 973 for (uint8_t pi=0; pi<kNplas; pi++) {
uci1 8:95a325df1f6b 974 if (pi < gConf.GetNumPlas()) {
uci1 8:95a325df1f6b 975 SnConfigFrame::GetHiLoPlas(gConf.GetPla(pi), hi, lo);
uci1 8:95a325df1f6b 976 PIN_spi.write(hi);
uci1 8:95a325df1f6b 977 PIN_spi.write(lo);
uci1 12:d472f9811262 978 #ifdef DEBUG
uci1 8:95a325df1f6b 979 printf("pla hi %hu, lo %hu\r\n",hi,lo);
uci1 12:d472f9811262 980 #endif
uci1 8:95a325df1f6b 981 } else {
uci1 8:95a325df1f6b 982 PIN_spi.write(kNoTrigPla); // hi
uci1 8:95a325df1f6b 983 PIN_spi.write(kNoTrigPla); // lo
uci1 12:d472f9811262 984 #ifdef DEBUG
uci1 8:95a325df1f6b 985 printf("pla hi %hu, lo %hu\r\n",kNoTrigPla,kNoTrigPla);
uci1 12:d472f9811262 986 #endif
uci1 8:95a325df1f6b 987 }
uci1 8:95a325df1f6b 988 Watchdog::kick();
uci1 0:664899e0b988 989 }
uci1 8:95a325df1f6b 990 wait(3);
uci1 8:95a325df1f6b 991 PIN_PLA_cs=0;
uci1 8:95a325df1f6b 992 wait(3);
uci1 0:664899e0b988 993
uci1 8:95a325df1f6b 994 // DAC values
uci1 8:95a325df1f6b 995 //
uci1 8:95a325df1f6b 996 // first 12 bits = DAC value
uci1 8:95a325df1f6b 997 // next 2 bits = DAC ID
uci1 8:95a325df1f6b 998 // last 2 bits = dFPGA ID
uci1 8:95a325df1f6b 999 //
uci1 8:95a325df1f6b 1000 // But FPGA uses "gray encoding" which means only 1 bit
uci1 8:95a325df1f6b 1001 // can change at a time (of the last 4 bits). So even tho
uci1 8:95a325df1f6b 1002 // the card/dac# is encoded, the order is also important
uci1 8:95a325df1f6b 1003 // 0000 (dac0,card0), 0001 (dac0,card1), 0011 (dac0,card3), 0010 (dac0,card2),
uci1 8:95a325df1f6b 1004 // 0110 (dac1,card2), 0111 (dac1,card3), 0101 (dac1,card1), etc.
uci1 12:d472f9811262 1005 #ifdef DEBUG
uci1 8:95a325df1f6b 1006 printf("setting dacs\r\n");
uci1 12:d472f9811262 1007 #endif
uci1 8:95a325df1f6b 1008 uint16_t dv=0;
uci1 8:95a325df1f6b 1009 for (uint8_t i=0, gri=0; i<kTotDacs; i++) {
uci1 8:95a325df1f6b 1010 // get the gray-codes for this iteration
uci1 8:95a325df1f6b 1011 gri = SnBitUtils::binToGray(i);
uci1 8:95a325df1f6b 1012
uci1 8:95a325df1f6b 1013 // build bit word
uci1 8:95a325df1f6b 1014 dv = static_cast<int>(gConf.GetDac(gri & 0x0003u, gri >> 2u));
uci1 8:95a325df1f6b 1015 dv <<= 4u;
uci1 8:95a325df1f6b 1016 dv |= gri;
uci1 8:95a325df1f6b 1017
uci1 12:d472f9811262 1018 #ifdef DEBUG
uci1 8:95a325df1f6b 1019 printf("dac %04x\r\n",dv);
uci1 12:d472f9811262 1020 #endif
uci1 8:95a325df1f6b 1021
uci1 8:95a325df1f6b 1022 // send to FPGA
uci1 8:95a325df1f6b 1023 PIN_start_fpga=1;
uci1 8:95a325df1f6b 1024 PIN_spi.write(dv);
uci1 8:95a325df1f6b 1025 PIN_start_fpga=0;
uci1 8:95a325df1f6b 1026
uci1 8:95a325df1f6b 1027 Watchdog::kick();
uci1 8:95a325df1f6b 1028
uci1 8:95a325df1f6b 1029 }
uci1 12:d472f9811262 1030 #ifdef DEBUG
uci1 8:95a325df1f6b 1031 printf("dacs set\r\n");
uci1 12:d472f9811262 1032 #endif
uci1 8:95a325df1f6b 1033 wait_ms(20);
uci1 8:95a325df1f6b 1034 } else {
uci1 12:d472f9811262 1035 #ifdef DEBUG
uci1 8:95a325df1f6b 1036 printf("cards off. skipping PLA and DAC setting\r\n");
uci1 12:d472f9811262 1037 #endif
uci1 0:664899e0b988 1038 }
uci1 0:664899e0b988 1039
uci1 0:664899e0b988 1040 // Majority Logic Trigger selection (# of cards)
uci1 0:664899e0b988 1041 SnBitUtils::SetChanNumBits(gConf.GetNumCardsMajLog() - 1u,
uci1 0:664899e0b988 1042 PIN_MajLogHiBit, PIN_MajLogLoBit);
uci1 0:664899e0b988 1043
uci1 0:664899e0b988 1044 // Enable thermal trigger?
uci1 0:664899e0b988 1045 PIN_enableThermTrig = gConf.IsThermTrigEnabled();
uci1 0:664899e0b988 1046
uci1 0:664899e0b988 1047 PIN_spi.format( 16, 1 ); // back to trigger mode
uci1 1:e392595b4b76 1048 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 1:e392595b4b76 1049
uci1 8:95a325df1f6b 1050 // make new output file
uci1 8:95a325df1f6b 1051 // put after PLA/DAC, in case they affect the power readings
uci1 8:95a325df1f6b 1052 wait_ms(200);
uci1 8:95a325df1f6b 1053 MakeOutputFile();
uci1 8:95a325df1f6b 1054
uci1 8:95a325df1f6b 1055 // TODO: change comm parameters
uci1 8:95a325df1f6b 1056 /*
uci1 8:95a325df1f6b 1057 printf("set comm params\r\n");
uci1 8:95a325df1f6b 1058 for (uint8_t cc=0; cc<kNcomms; cc++) {
uci1 8:95a325df1f6b 1059 if (gComms[cc]!=0) {
uci1 8:95a325df1f6b 1060 gComms[cc]->Set(gConf);
uci1 8:95a325df1f6b 1061 }
uci1 3:24c5f0f50bf1 1062 }
uci1 8:95a325df1f6b 1063 */
uci1 21:ce51bb0ba4a5 1064
uci1 21:ce51bb0ba4a5 1065 // reset tickers
uci1 21:ce51bb0ba4a5 1066 ResetAllTickers();
uci1 0:664899e0b988 1067
uci1 0:664899e0b988 1068 Watchdog::kick(); // don't reset!
uci1 8:95a325df1f6b 1069
uci1 12:d472f9811262 1070 #ifdef DEBUG
uci1 8:95a325df1f6b 1071 printf("set config done\r\n");
uci1 12:d472f9811262 1072 #endif
uci1 0:664899e0b988 1073 }
uci1 0:664899e0b988 1074
uci1 0:664899e0b988 1075 //
uci1 0:664899e0b988 1076 // readout functions
uci1 0:664899e0b988 1077 //
uci1 0:664899e0b988 1078 void WaitTrigAndSendClock() {
uci1 1:e392595b4b76 1079
uci1 12:d472f9811262 1080 #ifdef DEBUG
uci1 1:e392595b4b76 1081 printf("WaitTrigAndSendClock\r\n");
uci1 6:6f002d202f59 1082 printf("wait trig: (pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
uci1 6:6f002d202f59 1083 gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
uci1 6:6f002d202f59 1084 PIN_iridSbd_power.read(), PIN_afar_power.read());
uci1 21:ce51bb0ba4a5 1085 printf("cards powered=%d\r\n",(int)AreCardsPowered(true));
uci1 12:d472f9811262 1086 #endif
uci1 0:664899e0b988 1087
uci1 15:f2569d8e4176 1088 PIN_spi.format( 16, 1 ); // back to trigger mode
uci1 15:f2569d8e4176 1089 PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz
uci1 15:f2569d8e4176 1090
uci1 21:ce51bb0ba4a5 1091 if (AreCardsPowered(false)) {
uci1 8:95a325df1f6b 1092
uci1 21:ce51bb0ba4a5 1093 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1094 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
uci1 21:ce51bb0ba4a5 1095 #endif
uci1 8:95a325df1f6b 1096 if (gFirstEvt==false) {
uci1 8:95a325df1f6b 1097 PIN_DoNotRestartAllClocks = 0;
uci1 8:95a325df1f6b 1098 wait_us(1);
uci1 8:95a325df1f6b 1099 PIN_DoNotRestartAllClocks = 1;
uci1 8:95a325df1f6b 1100 //led3 = !led3; // toggle send clock led
uci1 8:95a325df1f6b 1101 } else {
uci1 8:95a325df1f6b 1102 gFirstEvt = false;
uci1 8:95a325df1f6b 1103 }
uci1 8:95a325df1f6b 1104
uci1 8:95a325df1f6b 1105 //
uci1 8:95a325df1f6b 1106 // wait for a trigger here.
uci1 8:95a325df1f6b 1107 //
uci1 12:d472f9811262 1108 #ifdef DEBUG
uci1 8:95a325df1f6b 1109 printf("starting wait for trig\r\n");
uci1 12:d472f9811262 1110 #endif
uci1 16:744ce85aede2 1111
uci1 8:95a325df1f6b 1112 gReadingOut = false; // this will allow forced triggers (see procForceTrigger())
uci1 8:95a325df1f6b 1113 while ( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 1114 if (gOpenCommWin || gCheckPower) {
uci1 12:d472f9811262 1115 #ifdef DEBUG
uci1 8:95a325df1f6b 1116 printf("break com=%d, pow=%d\r\n",gOpenCommWin,gCheckPower);
uci1 12:d472f9811262 1117 #endif
uci1 8:95a325df1f6b 1118 return; // break out to open comms or check power
uci1 8:95a325df1f6b 1119 }
uci1 0:664899e0b988 1120 }
uci1 21:ce51bb0ba4a5 1121 //PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers
uci1 8:95a325df1f6b 1122 gReadingOut = true; // disallow new forced triggers
uci1 15:f2569d8e4176 1123
uci1 21:ce51bb0ba4a5 1124 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1125 printf("after wait for trig. PIN_a_sf_clk=%d\r\n",
uci1 21:ce51bb0ba4a5 1126 PIN_a_sf_clk.read());
uci1 21:ce51bb0ba4a5 1127 #endif
uci1 21:ce51bb0ba4a5 1128
uci1 15:f2569d8e4176 1129 // we can't be interrupted before data arrives at the MB FPGA
uci1 15:f2569d8e4176 1130 //StopAllTickers();
uci1 15:f2569d8e4176 1131 /*
uci1 15:f2569d8e4176 1132 procForceTrigger();
uci1 15:f2569d8e4176 1133 procHeartbeat();
uci1 15:f2569d8e4176 1134 procPowerCheck();
uci1 15:f2569d8e4176 1135 procCommWin();
uci1 15:f2569d8e4176 1136 */
uci1 16:744ce85aede2 1137
uci1 16:744ce85aede2 1138 //wait_us(6);
uci1 16:744ce85aede2 1139
uci1 8:95a325df1f6b 1140 //
uci1 8:95a325df1f6b 1141 // collect data from daughter cards
uci1 8:95a325df1f6b 1142 //
uci1 8:95a325df1f6b 1143 // TODO: what if some card (set of channels) doesn't respond?
uci1 8:95a325df1f6b 1144 // currently, will wait forever?
uci1 8:95a325df1f6b 1145 // also, if ch1 is dead, will wait forever (due to FPGA code)
uci1 16:744ce85aede2 1146 gAdcToMBtimer.start();
uci1 16:744ce85aede2 1147 for( uint8_t i = 0; i < 128; ++i ) {
uci1 16:744ce85aede2 1148 while (PIN_a_sf_clk==1) {}
uci1 16:744ce85aede2 1149 while (PIN_a_sf_clk==0) {}
uci1 16:744ce85aede2 1150 /*
uci1 16:744ce85aede2 1151 if ((i == 10)&&(gEvtNum % 20)) {
uci1 16:744ce85aede2 1152 wait_us(8);
uci1 16:744ce85aede2 1153 }
uci1 16:744ce85aede2 1154 */
uci1 16:744ce85aede2 1155 PIN_ADC_CS = 0;
uci1 16:744ce85aede2 1156 PIN_spi.write( 0x00 );
uci1 16:744ce85aede2 1157 PIN_ADC_CS = 1;
uci1 16:744ce85aede2 1158 /*
uci1 8:95a325df1f6b 1159 if( PIN_a_sf_clk == 1 ) {
uci1 8:95a325df1f6b 1160 if( i == 0 )
uci1 8:95a325df1f6b 1161 wait_us( 1 );
uci1 8:95a325df1f6b 1162
uci1 8:95a325df1f6b 1163 PIN_ADC_CS = 0;
uci1 8:95a325df1f6b 1164 PIN_spi.write( 0x00 );
uci1 8:95a325df1f6b 1165 PIN_ADC_CS = 1;
uci1 8:95a325df1f6b 1166 } else {
uci1 8:95a325df1f6b 1167 i--;
uci1 8:95a325df1f6b 1168 }
uci1 16:744ce85aede2 1169 */
uci1 0:664899e0b988 1170 }
uci1 16:744ce85aede2 1171 gAdcToMBtimer.stop();
uci1 16:744ce85aede2 1172 #ifdef DEBUG
uci1 16:744ce85aede2 1173 printf("total time = %d us\r\n", gAdcToMBtimer.read_us());
uci1 16:744ce85aede2 1174 #endif
uci1 16:744ce85aede2 1175 if ( kAdcToMBtimeCut < gAdcToMBtimer.read_us() ) {
uci1 16:744ce85aede2 1176 gEvent.SetTrgBit(kAdcToMBflag);
uci1 16:744ce85aede2 1177 }
uci1 16:744ce85aede2 1178 gAdcToMBtimer.reset();
uci1 15:f2569d8e4176 1179
uci1 15:f2569d8e4176 1180 // restart the timers
uci1 15:f2569d8e4176 1181 //ResetAllTickers();
uci1 8:95a325df1f6b 1182 } else {
uci1 8:95a325df1f6b 1183 // cards have no power. don't try reading out
uci1 8:95a325df1f6b 1184 gReadingOut=false;
uci1 0:664899e0b988 1185 }
uci1 0:664899e0b988 1186 }
uci1 0:664899e0b988 1187
uci1 13:7a1fb885a8e4 1188 SnCommWin::ECommWinResult OpenCommWin(const bool forceReconfig) {
uci1 0:664899e0b988 1189 // loop through each comm mode:
uci1 0:664899e0b988 1190 // a) try to connect
uci1 0:664899e0b988 1191 // b) if connected, listen for config
uci1 0:664899e0b988 1192 // c) if config requests data, send it
uci1 16:744ce85aede2 1193 /*
uci1 16:744ce85aede2 1194 for (int i=0; i<5; i++) {
uci1 16:744ce85aede2 1195 led4=1;
uci1 16:744ce85aede2 1196 led3=1;
uci1 16:744ce85aede2 1197 wait(0.5);
uci1 16:744ce85aede2 1198 led4=0;
uci1 16:744ce85aede2 1199 led3=0;
uci1 16:744ce85aede2 1200 wait(0.5);
uci1 16:744ce85aede2 1201 }
uci1 16:744ce85aede2 1202 */
uci1 3:24c5f0f50bf1 1203 gLastCommWin = time(0);
uci1 13:7a1fb885a8e4 1204
uci1 0:664899e0b988 1205 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 0:664899e0b988 1206
uci1 21:ce51bb0ba4a5 1207 // get the trigger rates
uci1 21:ce51bb0ba4a5 1208 float thmrate=0, evtrate=0;
uci1 21:ce51bb0ba4a5 1209 GetRates(thmrate, evtrate);
uci1 22:f957c4f840ad 1210 #ifdef DEBUG
uci1 22:f957c4f840ad 1211 printf("thmrate=%g, evtrate=%g\r\n",thmrate,evtrate);
uci1 22:f957c4f840ad 1212 #endif
uci1 21:ce51bb0ba4a5 1213
uci1 21:ce51bb0ba4a5 1214 StopAllTickers();
uci1 13:7a1fb885a8e4 1215
uci1 13:7a1fb885a8e4 1216 if (gConf.GetCommWinDuration()==0) {
uci1 13:7a1fb885a8e4 1217 // TODO: set min so this is not possible
uci1 13:7a1fb885a8e4 1218 res = SnCommWin::kOkNoMsg;
uci1 13:7a1fb885a8e4 1219 } else {
uci1 13:7a1fb885a8e4 1220
uci1 13:7a1fb885a8e4 1221 gCommWinOpen = true;
uci1 1:e392595b4b76 1222 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1223
uci1 13:7a1fb885a8e4 1224 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1225 printf("opening comm window at %d\r\n", (int32_t)gLastCommWin);
uci1 16:744ce85aede2 1226 printf("duration=%u\r\n",gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1227 #endif
uci1 13:7a1fb885a8e4 1228
uci1 13:7a1fb885a8e4 1229 // close the file so that the data is all written out.
uci1 13:7a1fb885a8e4 1230 // and open it back up at the beginning (for reading)
uci1 12:d472f9811262 1231 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1232 printf("close & open file. gEvtNum=%u, gPowNum=%u\r\n",gEvtNum,gPowNum);
uci1 12:d472f9811262 1233 #endif
uci1 13:7a1fb885a8e4 1234 PIN_lockRegisters = 0; // unlock so we can talk to SD card.
uci1 13:7a1fb885a8e4 1235 SnSDUtils::CloseOutputFile(SnSDUtils::GetCurFile());
uci1 13:7a1fb885a8e4 1236 SnSDUtils::OpenExistingFile(SnSDUtils::GetCurFileName(), true);
uci1 13:7a1fb885a8e4 1237
uci1 21:ce51bb0ba4a5 1238 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1239 printf("setting power\r\n");
uci1 21:ce51bb0ba4a5 1240 #endif
uci1 13:7a1fb885a8e4 1241 // (probably) power down cards,amps and power up comms
uci1 13:7a1fb885a8e4 1242 SetPower(true);
uci1 13:7a1fb885a8e4 1243
uci1 21:ce51bb0ba4a5 1244 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1245 printf("start loop over comms\r\n");
uci1 21:ce51bb0ba4a5 1246 #endif
uci1 13:7a1fb885a8e4 1247 bool sendStat[kNcomms];
uci1 13:7a1fb885a8e4 1248 for (uint8_t i=0; i<kNcomms; i++) {
uci1 13:7a1fb885a8e4 1249 sendStat[i]=true;
uci1 13:7a1fb885a8e4 1250 }
uci1 13:7a1fb885a8e4 1251 bool* ss = sendStat;
uci1 13:7a1fb885a8e4 1252 SnCommWin** cw = gComms;
uci1 13:7a1fb885a8e4 1253 for (uint8_t i=0; ((time(0)-gLastCommWin)<gConf.GetCommWinDuration()); i++, cw++, ss++) {
uci1 1:e392595b4b76 1254 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1255 if (i==kNcomms) {
uci1 13:7a1fb885a8e4 1256 i=0;
uci1 13:7a1fb885a8e4 1257 cw = gComms;
uci1 13:7a1fb885a8e4 1258 ss = sendStat;
uci1 13:7a1fb885a8e4 1259 }
uci1 13:7a1fb885a8e4 1260 if ((*cw)==0) {
uci1 13:7a1fb885a8e4 1261 continue;
uci1 13:7a1fb885a8e4 1262 }
uci1 16:744ce85aede2 1263
uci1 16:744ce85aede2 1264 const uint32_t conto =
uci1 16:744ce85aede2 1265 (gConf.GetCommWinDuration() < (*cw)->GetConnectTimeout()) ?
uci1 16:744ce85aede2 1266 gConf.GetCommWinDuration() : (*cw)->GetConnectTimeout();
uci1 16:744ce85aede2 1267 const uint32_t listo =
uci1 16:744ce85aede2 1268 (gConf.GetCommWinDuration() < (*cw)->GetListenTimeout()) ?
uci1 16:744ce85aede2 1269 gConf.GetCommWinDuration() : (*cw)->GetListenTimeout();
uci1 16:744ce85aede2 1270
uci1 16:744ce85aede2 1271 // update power reading in case we want to send it in status
uci1 16:744ce85aede2 1272 GetAvePowerReading();
uci1 21:ce51bb0ba4a5 1273
uci1 13:7a1fb885a8e4 1274 // open window and (mabye) send status update
uci1 12:d472f9811262 1275 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1276 printf("calling OpenWindow. ss=%d\r\n",(int)(*ss));
uci1 21:ce51bb0ba4a5 1277 printf("gtt=%u, ct=%d, lcw=%d, dur=%u\r\n",gConf.GetTimeoutTime(gLastCommWin,conto),
uci1 13:7a1fb885a8e4 1278 time(0), gLastCommWin, gConf.GetCommWinDuration());
uci1 12:d472f9811262 1279 #endif
uci1 13:7a1fb885a8e4 1280 const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow(
uci1 21:ce51bb0ba4a5 1281 gConf.GetTimeoutTime(gLastCommWin, conto), *ss, gConf, gEvent, gPower,
uci1 13:7a1fb885a8e4 1282 SnSDUtils::GetCurSeqNum(), thmrate, evtrate,
uci1 13:7a1fb885a8e4 1283 gGenBuf);
uci1 13:7a1fb885a8e4 1284 if (conres>=SnCommWin::kConnected) {
uci1 1:e392595b4b76 1285 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1286 // connected. listen for config
uci1 13:7a1fb885a8e4 1287 *ss = false; // don't send status next time
uci1 12:d472f9811262 1288 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1289 printf("get conf gtt=%u\r\n",gConf.GetTimeoutTime(gLastCommWin, listo));
uci1 12:d472f9811262 1290 #endif
uci1 13:7a1fb885a8e4 1291 const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig(
uci1 21:ce51bb0ba4a5 1292 gConf, gConf.GetTimeoutTime(gLastCommWin, listo), gGenBuf, gBufSize);
uci1 16:744ce85aede2 1293
uci1 13:7a1fb885a8e4 1294 if (cfgres>=SnCommWin::kOkWithMsg) {
uci1 13:7a1fb885a8e4 1295 Watchdog::kick(); // don't reset!
uci1 16:744ce85aede2 1296
uci1 12:d472f9811262 1297 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1298 printf("received config!\r\n");
uci1 13:7a1fb885a8e4 1299 printf("send data = %d\r\n", gConf.GetCommSendData());
uci1 12:d472f9811262 1300 #endif
uci1 13:7a1fb885a8e4 1301 // send data if need be (files, some events, etc)
uci1 21:ce51bb0ba4a5 1302 const uint32_t winto = gConf.GetTimeoutTime(gLastCommWin,
uci1 21:ce51bb0ba4a5 1303 gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1304 const uint32_t gtt = gConf.IsObeyingTimeout() ? winto : 0;
uci1 13:7a1fb885a8e4 1305 if (gConf.GetCommSendData()!=0) {
uci1 12:d472f9811262 1306 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1307 printf("sending data, gtt=%u. lcw=%u, dur=%u, obey=%s\r\n",
uci1 21:ce51bb0ba4a5 1308 gConf.GetTimeoutTime(gLastCommWin, gConf.GetCommWinDuration()),
uci1 13:7a1fb885a8e4 1309 gLastCommWin, gConf.GetCommWinDuration(),
uci1 13:7a1fb885a8e4 1310 gConf.IsObeyingTimeout() ? "true" : "false");
uci1 12:d472f9811262 1311 #endif
uci1 16:744ce85aede2 1312
uci1 13:7a1fb885a8e4 1313 res = (*cw)->SendData(gConf, gEvent, gPower, gGenBuf, gBufSize,
uci1 21:ce51bb0ba4a5 1314 gtt, gConf.GetCommWinDuration());
uci1 13:7a1fb885a8e4 1315 } else {
uci1 13:7a1fb885a8e4 1316 // don't send anything
uci1 13:7a1fb885a8e4 1317 res = cfgres;
uci1 13:7a1fb885a8e4 1318 }
uci1 13:7a1fb885a8e4 1319 #ifdef DEBUG
uci1 13:7a1fb885a8e4 1320 printf("Got config!\r\n");
uci1 13:7a1fb885a8e4 1321 #endif
uci1 13:7a1fb885a8e4 1322 Watchdog::kick(); // don't reset!
uci1 13:7a1fb885a8e4 1323 break;
uci1 13:7a1fb885a8e4 1324 }
uci1 21:ce51bb0ba4a5 1325 } else {
uci1 21:ce51bb0ba4a5 1326 (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin, listo));
uci1 21:ce51bb0ba4a5 1327 } // if connected
uci1 13:7a1fb885a8e4 1328
uci1 13:7a1fb885a8e4 1329 Watchdog::kick(); // don't reset!
uci1 21:ce51bb0ba4a5 1330 } // end loop over comms
uci1 15:f2569d8e4176 1331 // close the connection(s)
uci1 15:f2569d8e4176 1332 cw = gComms;
uci1 15:f2569d8e4176 1333 for (uint8_t i=0; i<kNcomms; i++, cw++) {
uci1 15:f2569d8e4176 1334 if ((*cw)==0) {
uci1 15:f2569d8e4176 1335 continue;
uci1 15:f2569d8e4176 1336 }
uci1 16:744ce85aede2 1337 if ((*cw)->GetCommType()==SnConfigFrame::kIrid) {
uci1 16:744ce85aede2 1338 #ifdef DEBUG
uci1 16:744ce85aede2 1339 printf("try to set iridium time\r\n");
uci1 16:744ce85aede2 1340 #endif
uci1 16:744ce85aede2 1341 // set the clock before closing connection
uci1 16:744ce85aede2 1342 const bool con =
uci1 21:ce51bb0ba4a5 1343 (*cw)->Connect(gConf.GetTimeoutTime(gLastCommWin,
uci1 16:744ce85aede2 1344 gConf.GetCommWinDuration()));
uci1 16:744ce85aede2 1345 if (con) {
uci1 16:744ce85aede2 1346 const uint32_t nt = (*cw)->TrySetSysTimeUnix(
uci1 21:ce51bb0ba4a5 1347 gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
uci1 16:744ce85aede2 1348 }
uci1 16:744ce85aede2 1349 }
uci1 21:ce51bb0ba4a5 1350 (*cw)->CloseConn(gConf.GetTimeoutTime(gLastCommWin,gConf.GetCommWinDuration()));
uci1 15:f2569d8e4176 1351 Watchdog::kick(); // don't reset!
uci1 15:f2569d8e4176 1352 }
uci1 0:664899e0b988 1353 }
uci1 12:d472f9811262 1354
uci1 4:a91682e19d6b 1355 // (probably) power down comms and power up cards,amps
uci1 4:a91682e19d6b 1356 SetPower(false);
uci1 4:a91682e19d6b 1357
uci1 1:e392595b4b76 1358 // reset config with system powered (for DAC/PLA setting)
uci1 12:d472f9811262 1359 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1360 printf("calling SetConfigAndMakeOutputFile\r\n");
uci1 12:d472f9811262 1361 #endif
uci1 22:f957c4f840ad 1362
uci1 21:ce51bb0ba4a5 1363 SetConfigAndMakeOutputFile();
uci1 21:ce51bb0ba4a5 1364
uci1 12:d472f9811262 1365 #ifdef DEBUG
uci1 1:e392595b4b76 1366 printf("closing comm win at %d\r\n",(int32_t)time(0));
uci1 12:d472f9811262 1367 #endif
uci1 1:e392595b4b76 1368
uci1 0:664899e0b988 1369 gCommWinOpen = false;
uci1 0:664899e0b988 1370 return res;
uci1 0:664899e0b988 1371 }