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