Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 30 05:23:57 2012 +0000
Revision:
25:57b2627fe756
Parent:
23:ccf39298f205
Child:
27:efc4d654b139
AFAR comms. Upped baud to 921600. Store data in run-seq subdirs, max of 100 files per dir, to prevent station grinding to a halt due when 800 files are in one directory. Cache file size count. Stagger tickers. Fixed some gcc warnings.

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