Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
main.cpp@0:664899e0b988, 2012-06-30 (annotated)
- Committer:
- uci1
- Date:
- Sat Jun 30 02:03:51 2012 +0000
- Revision:
- 0:664899e0b988
- Child:
- 1:e392595b4b76
first version. SD card writing and data readout works. communications not tested.
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 | 0:664899e0b988 | 3 | #include <stdint.h> |
uci1 | 0:664899e0b988 | 4 | #include "SDFileSystem.h" |
uci1 | 0:664899e0b988 | 5 | #include "MODSERIAL.h" |
uci1 | 0:664899e0b988 | 6 | #include "Watchdog.h" |
uci1 | 0:664899e0b988 | 7 | #include "SnConstants.h" |
uci1 | 0:664899e0b988 | 8 | #include "SnBitUtils.h" |
uci1 | 0:664899e0b988 | 9 | #include "SnSDUtils.h" |
uci1 | 0:664899e0b988 | 10 | #include "SnConfigFrame.h" |
uci1 | 0:664899e0b988 | 11 | #include "SnEventFrame.h" |
uci1 | 0:664899e0b988 | 12 | #include "SnStatusFrame.h" |
uci1 | 0:664899e0b988 | 13 | #include "SnCommWin.h" |
uci1 | 0:664899e0b988 | 14 | #include "SnCommAfar.h" |
uci1 | 0:664899e0b988 | 15 | #include "SnCommUsb.h" |
uci1 | 0:664899e0b988 | 16 | |
uci1 | 0:664899e0b988 | 17 | // |
uci1 | 0:664899e0b988 | 18 | // MBED PINS (ordered by number) |
uci1 | 0:664899e0b988 | 19 | // |
uci1 | 0:664899e0b988 | 20 | // leds (for debugging) |
uci1 | 0:664899e0b988 | 21 | DigitalOut led1(LED1); |
uci1 | 0:664899e0b988 | 22 | DigitalOut led2(LED2); |
uci1 | 0:664899e0b988 | 23 | DigitalOut led3(LED3); |
uci1 | 0:664899e0b988 | 24 | DigitalOut led4(LED4); |
uci1 | 0:664899e0b988 | 25 | // Set up power pins - Note that it's Zero for "on" |
uci1 | 0:664899e0b988 | 26 | DigitalOut PIN_turn_on_system(p17); |
uci1 | 0:664899e0b988 | 27 | DigitalOut PIN_turn_on_amps(p25); |
uci1 | 0:664899e0b988 | 28 | // Activate/select chip by falling edge |
uci1 | 0:664899e0b988 | 29 | DigitalOut PIN_ADC_CS( p9 ); |
uci1 | 0:664899e0b988 | 30 | // clock signal to activate PLA setting |
uci1 | 0:664899e0b988 | 31 | DigitalOut PIN_PLA_cs(p10); |
uci1 | 0:664899e0b988 | 32 | // To force a trigger |
uci1 | 0:664899e0b988 | 33 | DigitalOut PIN_forceTrigger(p11); //modification |
uci1 | 0:664899e0b988 | 34 | // To suppress thermal triggers |
uci1 | 0:664899e0b988 | 35 | DigitalOut PIN_enableThermTrig(p12); |
uci1 | 0:664899e0b988 | 36 | // Restart clock on all FPGAs. |
uci1 | 0:664899e0b988 | 37 | DigitalOut PIN_DoNotRestartAllClocks( p13 ); |
uci1 | 0:664899e0b988 | 38 | // This tells the DFPGAs to store the data on motherboard FPGA and |
uci1 | 0:664899e0b988 | 39 | // read it out. |
uci1 | 0:664899e0b988 | 40 | DigitalIn PIN_a_sf_clk( p14 ); |
uci1 | 0:664899e0b988 | 41 | DigitalIn PIN_rst_a_sf(p15); |
uci1 | 0:664899e0b988 | 42 | // Lock daughter card registeres (during data readout). |
uci1 | 0:664899e0b988 | 43 | DigitalOut PIN_lockRegisters( p20 ); |
uci1 | 0:664899e0b988 | 44 | // Majority logic pins |
uci1 | 0:664899e0b988 | 45 | DigitalOut PIN_MajLogHiBit(p22); |
uci1 | 0:664899e0b988 | 46 | DigitalOut PIN_MajLogLoBit(p23); |
uci1 | 0:664899e0b988 | 47 | // Tell FPGA to be ready to accept DAC values |
uci1 | 0:664899e0b988 | 48 | DigitalOut PIN_start_fpga(p26); |
uci1 | 0:664899e0b988 | 49 | // Two bits to the select the daughter card for readout |
uci1 | 0:664899e0b988 | 50 | DigitalOut PIN_selCardHiBit( p29 ); |
uci1 | 0:664899e0b988 | 51 | DigitalOut PIN_selCardLoBit( p30 ); |
uci1 | 0:664899e0b988 | 52 | // To launch a heartbeat pulse |
uci1 | 0:664899e0b988 | 53 | DigitalOut PIN_heartbeat(p24); |
uci1 | 0:664899e0b988 | 54 | // Setup SPI pins |
uci1 | 0:664899e0b988 | 55 | SPI PIN_spi( p5, p6, p7 ); |
uci1 | 0:664899e0b988 | 56 | // The SD card |
uci1 | 0:664899e0b988 | 57 | SDFileSystem sd(p5, p6, p7, p8, SnSDUtils::kSDsubDir+1); |
uci1 | 0:664899e0b988 | 58 | LocalFileSystem local("local"); |
uci1 | 0:664899e0b988 | 59 | |
uci1 | 0:664899e0b988 | 60 | |
uci1 | 0:664899e0b988 | 61 | // |
uci1 | 0:664899e0b988 | 62 | // fwd declare fcns |
uci1 | 0:664899e0b988 | 63 | // |
uci1 | 0:664899e0b988 | 64 | void ReadAllRegisters(); |
uci1 | 0:664899e0b988 | 65 | void ReadRegister(const uint8_t chan, int16_t* dev); |
uci1 | 0:664899e0b988 | 66 | void SaveEvent(const int32_t etms); |
uci1 | 0:664899e0b988 | 67 | void WaitTrigAndSendClock(); |
uci1 | 0:664899e0b988 | 68 | void SetConfig(); |
uci1 | 0:664899e0b988 | 69 | SnCommWin::ECommWinResult OpenCommWin(); |
uci1 | 0:664899e0b988 | 70 | |
uci1 | 0:664899e0b988 | 71 | // |
uci1 | 0:664899e0b988 | 72 | // globals |
uci1 | 0:664899e0b988 | 73 | // |
uci1 | 0:664899e0b988 | 74 | // readout objs |
uci1 | 0:664899e0b988 | 75 | static Ticker gForceTicker; |
uci1 | 0:664899e0b988 | 76 | static Ticker gIOticker; |
uci1 | 0:664899e0b988 | 77 | static Timer gEvtTimer; |
uci1 | 0:664899e0b988 | 78 | static SnConfigFrame gConf; |
uci1 | 0:664899e0b988 | 79 | static SnEventFrame gEvent; |
uci1 | 0:664899e0b988 | 80 | // parameters |
uci1 | 0:664899e0b988 | 81 | static bool gFirstEvt = true; |
uci1 | 0:664899e0b988 | 82 | static bool gReadingOut = false; |
uci1 | 0:664899e0b988 | 83 | static bool gCommWinOpen = false; // if it's open |
uci1 | 0:664899e0b988 | 84 | static bool gOpenCommWin = false; // if it should be opened |
uci1 | 0:664899e0b988 | 85 | static int32_t gEvtNum = 0; // num of evt written |
uci1 | 0:664899e0b988 | 86 | static int32_t gTrgNum[kNumTrgs] = {0}; // num of this type of trg received |
uci1 | 0:664899e0b988 | 87 | // i/o |
uci1 | 0:664899e0b988 | 88 | static Timer gIOtimer; |
uci1 | 0:664899e0b988 | 89 | static MODSERIAL gCpu( USBTX, USBRX ); // defined here so it might be used for debugging output |
uci1 | 0:664899e0b988 | 90 | static SnCommWin* gComms[kNcomms] = { new SnCommAfar, new SnCommUsb(&gCpu) }; // order => priority |
uci1 | 0:664899e0b988 | 91 | static FILE* gCurFile = 0; |
uci1 | 0:664899e0b988 | 92 | //static char gEvtBuf[SnEventFrame::kMaxSizeOf]; |
uci1 | 0:664899e0b988 | 93 | //static char gConfBuf[SnConfigFrame::kMaxSizeOf]; |
uci1 | 0:664899e0b988 | 94 | //static char gStatBuf[SnStatusFrame::kMaxSizeOf]; |
uci1 | 0:664899e0b988 | 95 | static char gGenBuf[SnStatusFrame::kMaxSizeOf]; // must be big enough for event or status or config! |
uci1 | 0:664899e0b988 | 96 | |
uci1 | 0:664899e0b988 | 97 | void procForceTrigger() { |
uci1 | 0:664899e0b988 | 98 | led1=!led1; |
uci1 | 0:664899e0b988 | 99 | if (gReadingOut==false && gCommWinOpen==false) { |
uci1 | 0:664899e0b988 | 100 | gEvent.SetTrgBit(kFrcTrg); |
uci1 | 0:664899e0b988 | 101 | gEvent.SetTrgNum((gTrgNum[kFrcTrg])++); |
uci1 | 0:664899e0b988 | 102 | PIN_forceTrigger = 1; // force a trigger |
uci1 | 0:664899e0b988 | 103 | } |
uci1 | 0:664899e0b988 | 104 | } |
uci1 | 0:664899e0b988 | 105 | |
uci1 | 0:664899e0b988 | 106 | void procCommWin() { |
uci1 | 0:664899e0b988 | 107 | if (gReadingOut==false && gCommWinOpen==false) { |
uci1 | 0:664899e0b988 | 108 | gOpenCommWin = true; |
uci1 | 0:664899e0b988 | 109 | } |
uci1 | 0:664899e0b988 | 110 | } |
uci1 | 0:664899e0b988 | 111 | |
uci1 | 0:664899e0b988 | 112 | |
uci1 | 0:664899e0b988 | 113 | // TODO: add block-id's to output file? (config block, event block, file header block, etc.) |
uci1 | 0:664899e0b988 | 114 | |
uci1 | 0:664899e0b988 | 115 | int main() { |
uci1 | 0:664899e0b988 | 116 | led2=1; |
uci1 | 0:664899e0b988 | 117 | //wait_ms(100); |
uci1 | 0:664899e0b988 | 118 | |
uci1 | 0:664899e0b988 | 119 | // a failsafe |
uci1 | 0:664899e0b988 | 120 | Watchdog::kick(kWDFailsafe); |
uci1 | 0:664899e0b988 | 121 | |
uci1 | 0:664899e0b988 | 122 | // Turn on all power |
uci1 | 0:664899e0b988 | 123 | // Note that zero means "on" |
uci1 | 0:664899e0b988 | 124 | PIN_turn_on_system=0; |
uci1 | 0:664899e0b988 | 125 | |
uci1 | 0:664899e0b988 | 126 | // block (thermal) triggers during configuration |
uci1 | 0:664899e0b988 | 127 | PIN_enableThermTrig = 0; |
uci1 | 0:664899e0b988 | 128 | PIN_ADC_CS = 1; |
uci1 | 0:664899e0b988 | 129 | PIN_DoNotRestartAllClocks = 1; |
uci1 | 0:664899e0b988 | 130 | PIN_forceTrigger = 0; |
uci1 | 0:664899e0b988 | 131 | wait_ms(20); |
uci1 | 0:664899e0b988 | 132 | |
uci1 | 0:664899e0b988 | 133 | // setup SPI |
uci1 | 0:664899e0b988 | 134 | PIN_spi.frequency( 10000000 ); // Max is 12.5 MHz |
uci1 | 0:664899e0b988 | 135 | |
uci1 | 0:664899e0b988 | 136 | gForceTicker.detach(); |
uci1 | 0:664899e0b988 | 137 | gFirstEvt = true; |
uci1 | 0:664899e0b988 | 138 | |
uci1 | 0:664899e0b988 | 139 | gConf.Reset(); |
uci1 | 0:664899e0b988 | 140 | |
uci1 | 0:664899e0b988 | 141 | // |
uci1 | 0:664899e0b988 | 142 | // get config |
uci1 | 0:664899e0b988 | 143 | // |
uci1 | 0:664899e0b988 | 144 | |
uci1 | 0:664899e0b988 | 145 | // TODO: communication window |
uci1 | 0:664899e0b988 | 146 | // TODO: call usb port setup |
uci1 | 0:664899e0b988 | 147 | //OpenCommWin(); |
uci1 | 0:664899e0b988 | 148 | // TODO: get config |
uci1 | 0:664899e0b988 | 149 | SetConfig(); |
uci1 | 0:664899e0b988 | 150 | |
uci1 | 0:664899e0b988 | 151 | // get ready to trigger |
uci1 | 0:664899e0b988 | 152 | PIN_spi.format( 16, 1 ); // change to data readout format |
uci1 | 0:664899e0b988 | 153 | |
uci1 | 0:664899e0b988 | 154 | // force a trigger every dForceTrigPeriod seconds |
uci1 | 0:664899e0b988 | 155 | gForceTicker.attach(&procForceTrigger, gConf.GetForceTrigPeriod()); |
uci1 | 0:664899e0b988 | 156 | |
uci1 | 0:664899e0b988 | 157 | led2=0; |
uci1 | 0:664899e0b988 | 158 | |
uci1 | 0:664899e0b988 | 159 | // the main event loop. wait for triggers in SendClock |
uci1 | 0:664899e0b988 | 160 | while( true ) |
uci1 | 0:664899e0b988 | 161 | { |
uci1 | 0:664899e0b988 | 162 | // in here, we wait for triggers from the MB-FPGA |
uci1 | 0:664899e0b988 | 163 | led4 = !led4; |
uci1 | 0:664899e0b988 | 164 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 165 | gEvtTimer.reset(); |
uci1 | 0:664899e0b988 | 166 | gEvtTimer.start(); |
uci1 | 0:664899e0b988 | 167 | PIN_lockRegisters = 0; // allow data to come from DFPGA |
uci1 | 0:664899e0b988 | 168 | WaitTrigAndSendClock(); |
uci1 | 0:664899e0b988 | 169 | PIN_lockRegisters = 1; // block registers during readout |
uci1 | 0:664899e0b988 | 170 | gEvtTimer.stop(); |
uci1 | 0:664899e0b988 | 171 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 172 | |
uci1 | 0:664899e0b988 | 173 | if (gOpenCommWin) { |
uci1 | 0:664899e0b988 | 174 | OpenCommWin(); |
uci1 | 0:664899e0b988 | 175 | gOpenCommWin=false; |
uci1 | 0:664899e0b988 | 176 | continue; |
uci1 | 0:664899e0b988 | 177 | } |
uci1 | 0:664899e0b988 | 178 | |
uci1 | 0:664899e0b988 | 179 | // |
uci1 | 0:664899e0b988 | 180 | // got trigger. read registers to mbed and build the event |
uci1 | 0:664899e0b988 | 181 | // |
uci1 | 0:664899e0b988 | 182 | |
uci1 | 0:664899e0b988 | 183 | // read data & calc CRC |
uci1 | 0:664899e0b988 | 184 | gEvent.ReadWaveforms(PIN_spi, PIN_selCardHiBit, PIN_selCardLoBit); |
uci1 | 0:664899e0b988 | 185 | gEvent.SetCurMbedTime(); |
uci1 | 0:664899e0b988 | 186 | // TODO: no way to check for external trigger? |
uci1 | 0:664899e0b988 | 187 | if (gEvent.IsForcedTrg()==false) { |
uci1 | 0:664899e0b988 | 188 | gEvent.SetTrgBit(kThmTrg); |
uci1 | 0:664899e0b988 | 189 | gEvent.SetTrgNum((gTrgNum[kThmTrg])++); |
uci1 | 0:664899e0b988 | 190 | } // else already set by procForceTrigger |
uci1 | 0:664899e0b988 | 191 | // (no need to calc if we throw this event away) |
uci1 | 0:664899e0b988 | 192 | |
uci1 | 0:664899e0b988 | 193 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 194 | |
uci1 | 0:664899e0b988 | 195 | const int32_t etms = gEvtTimer.read_ms(); |
uci1 | 0:664899e0b988 | 196 | if ( gEvent.IsForcedTrg() || |
uci1 | 0:664899e0b988 | 197 | (etms>gConf.GetEvtThrtlPeriodMs()) ) { |
uci1 | 0:664899e0b988 | 198 | |
uci1 | 0:664899e0b988 | 199 | PIN_lockRegisters = 0; // done reading, unlock so we can talk to SD card. |
uci1 | 0:664899e0b988 | 200 | SaveEvent(etms); |
uci1 | 0:664899e0b988 | 201 | |
uci1 | 0:664899e0b988 | 202 | } |
uci1 | 0:664899e0b988 | 203 | } |
uci1 | 0:664899e0b988 | 204 | |
uci1 | 0:664899e0b988 | 205 | } |
uci1 | 0:664899e0b988 | 206 | |
uci1 | 0:664899e0b988 | 207 | // |
uci1 | 0:664899e0b988 | 208 | // save the event |
uci1 | 0:664899e0b988 | 209 | // |
uci1 | 0:664899e0b988 | 210 | void SaveEvent(const int32_t etms) { |
uci1 | 0:664899e0b988 | 211 | // write the event |
uci1 | 0:664899e0b988 | 212 | |
uci1 | 0:664899e0b988 | 213 | // set the event number & dt |
uci1 | 0:664899e0b988 | 214 | gEvent.SetEvtNum(gEvtNum++); |
uci1 | 0:664899e0b988 | 215 | gEvent.SetDTms(etms); |
uci1 | 0:664899e0b988 | 216 | |
uci1 | 0:664899e0b988 | 217 | // save to SD |
uci1 | 0:664899e0b988 | 218 | SnSDUtils::WriteEventTo(gCurFile, gGenBuf, gEvent, gConf); |
uci1 | 0:664899e0b988 | 219 | |
uci1 | 0:664899e0b988 | 220 | // reset |
uci1 | 0:664899e0b988 | 221 | gEvent.ClearEvent(); |
uci1 | 0:664899e0b988 | 222 | |
uci1 | 0:664899e0b988 | 223 | if (gEvtNum==5) { |
uci1 | 0:664899e0b988 | 224 | fclose(gCurFile); |
uci1 | 0:664899e0b988 | 225 | while (1) { |
uci1 | 0:664899e0b988 | 226 | led3 = 1; |
uci1 | 0:664899e0b988 | 227 | wait(0.5); |
uci1 | 0:664899e0b988 | 228 | led3 = 0; |
uci1 | 0:664899e0b988 | 229 | wait(0.5); |
uci1 | 0:664899e0b988 | 230 | } |
uci1 | 0:664899e0b988 | 231 | } |
uci1 | 0:664899e0b988 | 232 | |
uci1 | 0:664899e0b988 | 233 | } |
uci1 | 0:664899e0b988 | 234 | |
uci1 | 0:664899e0b988 | 235 | // |
uci1 | 0:664899e0b988 | 236 | // set configuration |
uci1 | 0:664899e0b988 | 237 | // |
uci1 | 0:664899e0b988 | 238 | void SetConfig() { |
uci1 | 0:664899e0b988 | 239 | // restart watchdog |
uci1 | 0:664899e0b988 | 240 | Watchdog::kick(gConf.GetWatchdogPeriod()); |
uci1 | 0:664899e0b988 | 241 | |
uci1 | 0:664899e0b988 | 242 | // reset event, timers, trigger counters |
uci1 | 0:664899e0b988 | 243 | gEvent.ClearEvent(); |
uci1 | 0:664899e0b988 | 244 | gEvtNum = gConf.GetFirstEvt(); |
uci1 | 0:664899e0b988 | 245 | gEvtTimer.reset(); |
uci1 | 0:664899e0b988 | 246 | memset(gTrgNum, 0, sizeof(int32_t)*kNumTrgs); |
uci1 | 0:664899e0b988 | 247 | |
uci1 | 0:664899e0b988 | 248 | // make new output file |
uci1 | 0:664899e0b988 | 249 | SnSDUtils::CloseOutputFile(gCurFile); |
uci1 | 0:664899e0b988 | 250 | |
uci1 | 0:664899e0b988 | 251 | gCurFile = SnSDUtils::OpenNewOutputFile(gConf.GetMacAddress(), |
uci1 | 0:664899e0b988 | 252 | gConf.GetRun()); |
uci1 | 0:664899e0b988 | 253 | SnSDUtils::WriteFileHeader(gCurFile, gConf.GetMacAddress()); |
uci1 | 0:664899e0b988 | 254 | SnSDUtils::WriteConfig(gCurFile, gConf); |
uci1 | 0:664899e0b988 | 255 | |
uci1 | 0:664899e0b988 | 256 | // TODO: turn on amps individually, when that's possible |
uci1 | 0:664899e0b988 | 257 | if (gConf.IsEachAmpOn()) { |
uci1 | 0:664899e0b988 | 258 | PIN_turn_on_amps=0; |
uci1 | 0:664899e0b988 | 259 | } |
uci1 | 0:664899e0b988 | 260 | |
uci1 | 0:664899e0b988 | 261 | // Set PLA value(s) |
uci1 | 0:664899e0b988 | 262 | // TODO: send multiple values when FPGA code is ready for it |
uci1 | 0:664899e0b988 | 263 | // TODO: uncomment when using new version of FPGA code |
uci1 | 0:664899e0b988 | 264 | PIN_spi.format( 16, 0 ); // change mode for DAC & PLA value setting |
uci1 | 0:664899e0b988 | 265 | PIN_MajLogHiBit=1; |
uci1 | 0:664899e0b988 | 266 | PIN_MajLogLoBit=1; |
uci1 | 0:664899e0b988 | 267 | PIN_enableThermTrig=0; |
uci1 | 0:664899e0b988 | 268 | |
uci1 | 0:664899e0b988 | 269 | uint16_t hi, lo; |
uci1 | 0:664899e0b988 | 270 | PIN_PLA_cs=1; |
uci1 | 0:664899e0b988 | 271 | wait(3); |
uci1 | 0:664899e0b988 | 272 | for (uint8_t pi=0; pi<kNplas; pi++) { |
uci1 | 0:664899e0b988 | 273 | if (pi < gConf.GetNumPlas()) { |
uci1 | 0:664899e0b988 | 274 | SnConfigFrame::GetHiLoPlas(gConf.GetPla(pi), hi, lo); |
uci1 | 0:664899e0b988 | 275 | PIN_spi.write(hi); |
uci1 | 0:664899e0b988 | 276 | PIN_spi.write(lo); |
uci1 | 0:664899e0b988 | 277 | } else { |
uci1 | 0:664899e0b988 | 278 | PIN_spi.write(kNoTrigPla); // hi |
uci1 | 0:664899e0b988 | 279 | PIN_spi.write(kNoTrigPla); // lo |
uci1 | 0:664899e0b988 | 280 | } |
uci1 | 0:664899e0b988 | 281 | } |
uci1 | 0:664899e0b988 | 282 | wait(3); |
uci1 | 0:664899e0b988 | 283 | PIN_PLA_cs=0; |
uci1 | 0:664899e0b988 | 284 | wait(3); |
uci1 | 0:664899e0b988 | 285 | |
uci1 | 0:664899e0b988 | 286 | // DAC values |
uci1 | 0:664899e0b988 | 287 | // |
uci1 | 0:664899e0b988 | 288 | // first 12 bits = DAC value |
uci1 | 0:664899e0b988 | 289 | // next 2 bits = DAC ID |
uci1 | 0:664899e0b988 | 290 | // last 2 bits = dFPGA ID |
uci1 | 0:664899e0b988 | 291 | // |
uci1 | 0:664899e0b988 | 292 | // But FPGA uses "gray encoding" which means only 1 bit |
uci1 | 0:664899e0b988 | 293 | // can change at a time (of the last 4 bits). So even tho |
uci1 | 0:664899e0b988 | 294 | // the card/dac# is encoded, the order is also important |
uci1 | 0:664899e0b988 | 295 | // 0000 (dac0,card0), 0001 (dac0,card1), 0011 (dac0,card3), 0010 (dac0,card2), |
uci1 | 0:664899e0b988 | 296 | // 0110 (dac1,card2), 0111 (dac1,card3), 0101 (dac1,card1), etc. |
uci1 | 0:664899e0b988 | 297 | int dv=0; |
uci1 | 0:664899e0b988 | 298 | for (uint8_t i=0, gri=0; i<kTotDacs; i++) { |
uci1 | 0:664899e0b988 | 299 | // get the gray-codes for this iteration |
uci1 | 0:664899e0b988 | 300 | gri = SnBitUtils::binToGray(i); |
uci1 | 0:664899e0b988 | 301 | |
uci1 | 0:664899e0b988 | 302 | // build bit word |
uci1 | 0:664899e0b988 | 303 | dv = static_cast<int>(gConf.GetDac(gri & 0x0003u, gri >> 2u)); |
uci1 | 0:664899e0b988 | 304 | dv <<= 4u; |
uci1 | 0:664899e0b988 | 305 | dv |= gri; |
uci1 | 0:664899e0b988 | 306 | |
uci1 | 0:664899e0b988 | 307 | // send to FPGA |
uci1 | 0:664899e0b988 | 308 | PIN_start_fpga=1; |
uci1 | 0:664899e0b988 | 309 | PIN_spi.write(dv); |
uci1 | 0:664899e0b988 | 310 | PIN_start_fpga=0; |
uci1 | 0:664899e0b988 | 311 | } |
uci1 | 0:664899e0b988 | 312 | |
uci1 | 0:664899e0b988 | 313 | wait_ms(20); |
uci1 | 0:664899e0b988 | 314 | |
uci1 | 0:664899e0b988 | 315 | // Majority Logic Trigger selection (# of cards) |
uci1 | 0:664899e0b988 | 316 | SnBitUtils::SetChanNumBits(gConf.GetNumCardsMajLog() - 1u, |
uci1 | 0:664899e0b988 | 317 | PIN_MajLogHiBit, PIN_MajLogLoBit); |
uci1 | 0:664899e0b988 | 318 | |
uci1 | 0:664899e0b988 | 319 | // Enable thermal trigger? |
uci1 | 0:664899e0b988 | 320 | PIN_enableThermTrig = gConf.IsThermTrigEnabled(); |
uci1 | 0:664899e0b988 | 321 | |
uci1 | 0:664899e0b988 | 322 | PIN_spi.format( 16, 1 ); // back to trigger mode |
uci1 | 0:664899e0b988 | 323 | |
uci1 | 0:664899e0b988 | 324 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 325 | |
uci1 | 0:664899e0b988 | 326 | } |
uci1 | 0:664899e0b988 | 327 | |
uci1 | 0:664899e0b988 | 328 | // |
uci1 | 0:664899e0b988 | 329 | // readout functions |
uci1 | 0:664899e0b988 | 330 | // |
uci1 | 0:664899e0b988 | 331 | void WaitTrigAndSendClock() { |
uci1 | 0:664899e0b988 | 332 | |
uci1 | 0:664899e0b988 | 333 | if (gFirstEvt==false) { |
uci1 | 0:664899e0b988 | 334 | PIN_DoNotRestartAllClocks = 0; |
uci1 | 0:664899e0b988 | 335 | wait_us(1); |
uci1 | 0:664899e0b988 | 336 | PIN_DoNotRestartAllClocks = 1; |
uci1 | 0:664899e0b988 | 337 | //led3 = !led3; // toggle send clock led |
uci1 | 0:664899e0b988 | 338 | } else { |
uci1 | 0:664899e0b988 | 339 | gFirstEvt = false; |
uci1 | 0:664899e0b988 | 340 | } |
uci1 | 0:664899e0b988 | 341 | |
uci1 | 0:664899e0b988 | 342 | // |
uci1 | 0:664899e0b988 | 343 | // wait for a trigger here. |
uci1 | 0:664899e0b988 | 344 | // |
uci1 | 0:664899e0b988 | 345 | gReadingOut = false; // this will allow forced triggers (see procForceTrigger()) |
uci1 | 0:664899e0b988 | 346 | while ( PIN_a_sf_clk == 1 ) { |
uci1 | 0:664899e0b988 | 347 | if (gOpenCommWin) { |
uci1 | 0:664899e0b988 | 348 | // break out to open comms |
uci1 | 0:664899e0b988 | 349 | return; |
uci1 | 0:664899e0b988 | 350 | } |
uci1 | 0:664899e0b988 | 351 | } |
uci1 | 0:664899e0b988 | 352 | PIN_forceTrigger=0; // necessary for forced triggers, harmless for other triggers |
uci1 | 0:664899e0b988 | 353 | gReadingOut = true; // disallow new forced triggers |
uci1 | 0:664899e0b988 | 354 | |
uci1 | 0:664899e0b988 | 355 | // |
uci1 | 0:664899e0b988 | 356 | // collect data from daughter cards |
uci1 | 0:664899e0b988 | 357 | // |
uci1 | 0:664899e0b988 | 358 | |
uci1 | 0:664899e0b988 | 359 | // TODO: what if some card (set of channels) doesn't respond? |
uci1 | 0:664899e0b988 | 360 | // currently, will wait forever? |
uci1 | 0:664899e0b988 | 361 | // also, if ch1 is dead, will wait forever (due to FPGA code) |
uci1 | 0:664899e0b988 | 362 | |
uci1 | 0:664899e0b988 | 363 | for( uint8_t i = 0; i < kNsamps; i++ ) { |
uci1 | 0:664899e0b988 | 364 | if( PIN_a_sf_clk == 1 ) { |
uci1 | 0:664899e0b988 | 365 | if( i == 0 ) |
uci1 | 0:664899e0b988 | 366 | wait_us( 1 ); |
uci1 | 0:664899e0b988 | 367 | |
uci1 | 0:664899e0b988 | 368 | PIN_ADC_CS = 0; |
uci1 | 0:664899e0b988 | 369 | PIN_spi.write( 0x00 ); |
uci1 | 0:664899e0b988 | 370 | PIN_ADC_CS = 1; |
uci1 | 0:664899e0b988 | 371 | } else { |
uci1 | 0:664899e0b988 | 372 | i--; |
uci1 | 0:664899e0b988 | 373 | } |
uci1 | 0:664899e0b988 | 374 | } |
uci1 | 0:664899e0b988 | 375 | |
uci1 | 0:664899e0b988 | 376 | } |
uci1 | 0:664899e0b988 | 377 | |
uci1 | 0:664899e0b988 | 378 | SnCommWin::ECommWinResult OpenCommWin() { |
uci1 | 0:664899e0b988 | 379 | // loop through each comm mode: |
uci1 | 0:664899e0b988 | 380 | // a) try to connect |
uci1 | 0:664899e0b988 | 381 | // b) if connected, listen for config |
uci1 | 0:664899e0b988 | 382 | // c) if config requests data, send it |
uci1 | 0:664899e0b988 | 383 | |
uci1 | 0:664899e0b988 | 384 | gCommWinOpen = true; |
uci1 | 0:664899e0b988 | 385 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 386 | |
uci1 | 0:664899e0b988 | 387 | // TODO: power up comm systems |
uci1 | 0:664899e0b988 | 388 | |
uci1 | 0:664899e0b988 | 389 | gIOtimer.reset(); |
uci1 | 0:664899e0b988 | 390 | gIOtimer.start(); |
uci1 | 0:664899e0b988 | 391 | |
uci1 | 0:664899e0b988 | 392 | const uint32_t conto = (gConf.GetCommWinDuration() < kConnectTimeout) ? |
uci1 | 0:664899e0b988 | 393 | gConf.GetCommWinDuration() : kConnectTimeout; |
uci1 | 0:664899e0b988 | 394 | const uint32_t listo = (gConf.GetCommWinDuration() < kListenTimeout) ? |
uci1 | 0:664899e0b988 | 395 | gConf.GetCommWinDuration() : kListenTimeout; |
uci1 | 0:664899e0b988 | 396 | |
uci1 | 0:664899e0b988 | 397 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 0:664899e0b988 | 398 | |
uci1 | 0:664899e0b988 | 399 | SnCommWin** cw = gComms; |
uci1 | 0:664899e0b988 | 400 | for (uint8_t i=0; (i<kNcomms) && |
uci1 | 0:664899e0b988 | 401 | (gIOtimer.read()<gConf.GetCommWinDuration()); i++, cw++) { |
uci1 | 0:664899e0b988 | 402 | |
uci1 | 0:664899e0b988 | 403 | // open window and (mabye) send status update |
uci1 | 0:664899e0b988 | 404 | const SnCommWin::ECommWinResult conres = (*cw)->OpenWindow( |
uci1 | 0:664899e0b988 | 405 | gIOtimer, conto, true, gConf, gEvent, gGenBuf, gGenBuf); |
uci1 | 0:664899e0b988 | 406 | if (conres>=SnCommWin::kConnected) { |
uci1 | 0:664899e0b988 | 407 | // connected. listen for config |
uci1 | 0:664899e0b988 | 408 | const SnCommWin::ECommWinResult cfgres = (*cw)->GetConfig( |
uci1 | 0:664899e0b988 | 409 | gConf, gIOtimer, listo, gGenBuf); |
uci1 | 0:664899e0b988 | 410 | if (cfgres>=SnCommWin::kOkWithMsg) { |
uci1 | 0:664899e0b988 | 411 | // got config. set it up. |
uci1 | 0:664899e0b988 | 412 | SetConfig(); |
uci1 | 0:664899e0b988 | 413 | // send data if need be (files, some events, etc) |
uci1 | 0:664899e0b988 | 414 | if (gConf.GetCommSendData()!=0) { |
uci1 | 0:664899e0b988 | 415 | res = (*cw)->SendData(gConf, gEvent, gGenBuf, gGenBuf); |
uci1 | 0:664899e0b988 | 416 | } else { |
uci1 | 0:664899e0b988 | 417 | // don't send anything |
uci1 | 0:664899e0b988 | 418 | res = cfgres; |
uci1 | 0:664899e0b988 | 419 | } |
uci1 | 0:664899e0b988 | 420 | break; |
uci1 | 0:664899e0b988 | 421 | } |
uci1 | 0:664899e0b988 | 422 | } |
uci1 | 0:664899e0b988 | 423 | |
uci1 | 0:664899e0b988 | 424 | Watchdog::kick(); // don't reset! |
uci1 | 0:664899e0b988 | 425 | |
uci1 | 0:664899e0b988 | 426 | } |
uci1 | 0:664899e0b988 | 427 | |
uci1 | 0:664899e0b988 | 428 | gCommWinOpen = false; |
uci1 | 0:664899e0b988 | 429 | return res; |
uci1 | 0:664899e0b988 | 430 | } |