Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
67:ec999336fcd1
Parent:
65:2cb3e99ce466
Child:
76:f8383f0292c2
--- a/main.cpp	Tue Dec 02 21:55:27 2014 +0000
+++ b/main.cpp	Tue Feb 03 00:04:30 2015 +0000
@@ -26,6 +26,17 @@
 #include <stdint.h>
 #include "SnConstants.h"
 
+#ifndef USE_INTERFACE_CHIP
+// to avoid calling the interface chip,
+// the mac address is hard coded (ugh!)
+extern "C" void mbed_mac_address(char * mac) {
+#ifdef DEBUG
+    printf("calling MY mbed_mac_address\r\n");
+#endif
+    memmove(mac, kDefaultMacAdress, sizeof(kDefaultMacAdress));
+};
+#endif
+
 #include "SDFileSystem.h"
 #ifdef USE_MODSERIAL
 #define MODSERIAL_RX_BUF_SIZE 512
@@ -72,10 +83,10 @@
 // MBED PINS (ordered by number)
 //
 // leds (for debugging)
-DigitalOut led1(LED1,0);
-DigitalOut led2(LED2,0);
-DigitalOut led3(LED3,0);
-DigitalOut led4(LED4,0);
+DigitalOut led1(LED1,1);
+DigitalOut led2(LED2,1);
+DigitalOut led3(LED3,1);
+DigitalOut led4(LED4,1);
 
 // Set up power pins - Note that it's Zero for "on" in ATWD2013, but high for "on" in SST2014
 #if CHIPBOARD==ATWD4CH
@@ -173,6 +184,9 @@
 
 // The SD card
 static SDFileSystem     sd(p5, p6, p7, p8, SnSDUtils::kSDdir+1); // no leading '/'
+
+// local file system is still created even if USE_INTERFACE_CHIP is not defined
+// this is done to allow the mbed to be reprogrammed remotely
 static LocalFileSystem  local((SnCommWin::kLocalDir)+1); // no leading '/'
 
 //
@@ -222,6 +236,7 @@
 static Timer          gAllTrgTimer;
 static Timer          gThmTrgTimer;
 static Timer          gAdcToMBtimer;
+
 static SnClockSetFrame       gClkSet;
 static SnSignalStrengthFrame gSigStr;
 #ifdef DISABLE_CONFIG_SAFETYNETS
@@ -270,6 +285,8 @@
 static SnCommAfarNetIfTwitter* gTwit     = 0;
 #endif
 
+
+
 void procForceTrigger() {
     if (gReadingOut==false && gCommWinOpen==false) {
         led3=!led3;
@@ -300,6 +317,7 @@
 #ifdef DEBUG
         printf("proc heartbeat\r\n");
 #endif
+        led3=!led3;
         //PIN_heartbeat = 0;
         PIN_heartbeat = 1; // heartbeat pulse
         PIN_heartbeat = 0;
@@ -313,6 +331,7 @@
 #ifdef DEBUG
     printf("proc power\r\n");
 #endif
+    led3=!led3;
     gCheckPower=true;
 }
 
@@ -320,6 +339,7 @@
 #ifdef DEBUG
     printf("proc temp check\r\n");
 #endif
+    led3=!led3;
     gCheckTemp=true;
 }
 
@@ -413,12 +433,16 @@
 
 void UpdateTemperature() {
     // ask chip to convert temperature
+#if CHIPBOARD==SST4CH
     PIN_therm.convert_temperature(true, DS1820::all_devices);
     gTemperature.SetTempAndTime( PIN_therm.temperature('c'), time(0) );
 #ifdef DEBUG
     printf("TTTTTTTT temp = %g at %u\r\n", gTemperature.GetTemperature(),
         gTemperature.GetTime());
 #endif
+#else
+    return; // do nothing
+#endif
 }
 
 void CheckTemp() {
@@ -767,6 +791,7 @@
 #if defined(SSNOTIFY) || defined(DEBUG)
         printf("\n\n\n\n\nmain: start\r\n");
 #endif
+        led1=led2=led3=led4=0;
 #ifdef USE_RTOS
         led1=1; Thread::wait(200);
         led1=0; led2=1; Thread::wait(200);
@@ -780,6 +805,9 @@
 #endif // USE_RTOS
         led4=0;
     }
+    
+    // signal startup before first comm win
+    led2 = led1 = 1;
 
     // don't initialize yet, but give SnSDUtils a pointer to
     // the function that initializes it, so it can call the fcn later
@@ -794,7 +822,6 @@
         PIN_iridSbd_power.read(), PIN_afar_power.read());
 #endif
 
-
     /*
     // Note: the brownout isn't useful since it sees either 5V or nothing on our board
     // set up the brownout interrupt
@@ -862,9 +889,7 @@
     gPowerCheckTicker   = new rtos::RtosTimer(&procPowerCheck);
     gTempCheckTicker    = new rtos::RtosTimer(&procTempCheck);
 #endif     // USE_RTOS
-    
-    led2=1;
-            
+                
     // set the clock to the BS time, if it's not set
     if ( (static_cast<int32_t>(time(0)))<0 ) {
         set_time(kBStime);
@@ -881,6 +906,16 @@
 #endif // USE_RTOS
     gFirstEvt = true;
 
+
+#ifdef DEBUG
+    printf("MAC=%012llX\r\n", (gConf.GetMacAddress())>>16); // 64 -> 48 bits
+    printf("my ip   = %s\r\n", gConf.GetMbedIP());
+    printf("my mask = %s\r\n", gConf.GetMbedMask());
+    printf("my gate = %s\r\n", gConf.GetMbedGate());
+    printf("remote server = %s : %hu\r\n",
+        gConf.GetRemoteServer(), gConf.GetRemotePort());
+#endif
+
     // (probably) power down comms and power up cards,amps
     SetPower(false);
     // check power again to see if voltages drooped
@@ -892,13 +927,12 @@
 #ifdef DEBUG
     printf("call OpenCommWin\r\n");
 #endif
+    led2 = led1 = 0; // back to "normal" for comm win
     OpenCommWin(true, true); // alwasy configure, even if no new config
     
     // get ready to trigger
     PIN_spi.format( 16, 1 ); // change to data readout format
     PIN_spi.frequency( 10000000 );  // Max is 12.5 MHz
-
-    led2=0;
     
     // read and cache the dCard power pin setting,
     // so we can use the cached value later
@@ -910,7 +944,7 @@
         // in here, we wait for triggers from the MB-FPGA
         Watchdog::kick(); // don't reset!
         
-        led1 = !led1;
+        led1 = 1; // signal normal running (i.e. waiting)
         
 #ifdef DEBUG
         printf("calling wait trig\r\n");
@@ -951,6 +985,9 @@
 #endif // EVT_TIME_PROFILE
 
         if (gReadingOut) {
+
+            led1 = 0; led4 = 1; // signal reading out
+
             const double ttms = gThmTrgTimer.read_us() / 1e3; // for rate calculation
             const double atms = gAllTrgTimer.read_us() / 1e3; // for throttle
             if (gEvent.IsForcedTrg()==false) {
@@ -966,8 +1003,6 @@
             // got trigger. read registers to mbed and build the event
             //
             
-            led4=1;
-
             // TODO: no way to check for external trigger?
             if (gEvent.IsForcedTrg()==false) {
                 gEvent.SetTrgBit(kThmTrg);
@@ -1006,7 +1041,6 @@
                 printf("gFirstEvt=%s\r\n",gFirstEvt?"true":"false");
 #endif
                 
-                led2=1;
                 /*
                 gRecentCountTime = static_cast<uint32_t>(time(0));
                 gRecentEvtNum    = gEvtNum;
@@ -1044,17 +1078,20 @@
                 PIN_ResetChips = 0;
 #endif
             }
+
+            led1 = 1; led4 = 0; // end signal reading out
+            
         }
 #ifdef DEBUG
         printf("past reading out\r\n");
 #endif
         
         if (gHrtbtFired) {
+            led1 = 1; led4 = 1; // signal saving transient
             SaveHeartbeat();
             gHrtbtFired=false;
+            led1 = 1; led4 = 0; // end signal saving transient
         }
-
-        led4=0; led2=0;
                 
 #ifdef EVT_TIME_PROFILE
         prof.stop(); befChkPow=prof.read_us(); prof.start();
@@ -1064,7 +1101,9 @@
 #ifdef DEBUG
             printf("call check power\r\n");
 #endif
+            led1 = 1; led4 = 1; // signal saving transient
             CheckPower(false);
+            led1 = 1; led4 = 0; // end signal saving transient
         }
 #ifdef EVT_TIME_PROFILE
         prof.stop(); aftChkPow=prof.read_us(); prof.start();
@@ -1074,7 +1113,9 @@
 #ifdef DEBUG
             printf("call check temp\r\n");
 #endif
+            led1 = 1; led4 = 1; // signal saving transient
             CheckTemp();
+            led1 = 1; led4 = 0; // end signal saving transient
         }
         
         // open comm win?
@@ -1086,7 +1127,9 @@
             SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                             gClkSet,
                                             false);
+            led1 = 0; // signal not waiting
             OpenCommWin();
+            led1 = 1; // end signal not waiting
             gOpenCommWin=false;
             gFirstEvt = true;
             gAllTrgTimer.reset();
@@ -1108,6 +1151,7 @@
             printf("seq complete. sngseq=%d\r\n",gConf.IsSingleSeqRunMode());
             printf("WriteTrigWaitWinTime (stop)\r\n");
 #endif
+            led1 = 1; led2 = 1; led4 = 1; // signal saving file
             SnSDUtils::WriteTrigWaitWinTime(SnSDUtils::GetCurFile(),
                                             gClkSet,
                                             false);
@@ -1116,6 +1160,7 @@
             gThmTrgTimer.reset();
             gAllTrgTimer.reset();
             etms=0;
+            led1 = 1; led2 = 0; led4 = 0; // end signal saving file
         }
 #ifdef EVT_TIME_PROFILE
         prof.stop(); aftNewSeq=prof.read_us(); prof.start();
@@ -1249,12 +1294,17 @@
 }
 
 bool PowerDownCommPeriph(const SnConfigFrame::EDatPackBit type) {
-    
+#ifdef DEBUG
+    printf("PowerDownCommPeriph: type=%d\r\n",(int)type);
+#endif    
     SnCommWin** cw = gComms;
     for (uint8_t i=0; i<kNcomms; ++i, ++cw) {
         if ((*cw)==0) {
             continue;
         } else if ((*cw)->GetCommType()==type) {
+#ifdef DEBUG
+            printf("calling PowerDown\r\n");
+#endif
             return (*cw)->PowerDown(gConf.GetTimeoutTime(time(0),
                                     gConf.GetCommWinConnectTO()));
         }
@@ -1265,79 +1315,125 @@
 //
 // power stuff
 //
-void SetPower(const bool isCommWin) {
+bool IsCurrentlyPowered(const SnConfigFrame::EPowerModeBit p,
+                        DigitalOut& pin) {
+    // check if the current pin value is equal to
+    // what it should be if the peripheral is on
+    return gConf.GetPowPinSetting(p, true) == pin.read();
+}
+
+void ChangeCardPower(const bool isCommWin) {
+    // change cards power
+    const SnConfigFrame::EPowerModeBit pbit = (isCommWin)
+        ? SnConfigFrame::kCardComWin
+        : SnConfigFrame::kCardDatTak;
+    const int value = gConf.GetPowPinSetting(pbit);
 #ifdef DEBUG
-    printf("set power. isCommWin=%s\r\n",(isCommWin)?"true":"false");
-    printf("bef: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
-        LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
-    printf("pcenet bef power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
-    printf("WD reset = %d\r\n",(int)Watchdog::didWatchdogReset());
+    printf("setting cards pin power to %d (comm=%d)\r\n",
+        value, static_cast<int>(isCommWin));
 #endif
-    
-    SnConfigFrame::EPowerModeBit cardpb(SnConfigFrame::kCardDatTak),
-                                 ampspb(SnConfigFrame::kAmpsDatTak),
-                                 iridpb(SnConfigFrame::kIridDatTak),
-                                 afarpb(SnConfigFrame::kAfarDatTak);
-    if (isCommWin) {
-        cardpb = SnConfigFrame::kCardComWin;
-        ampspb = SnConfigFrame::kAmpsComWin;
-        iridpb = SnConfigFrame::kIridComWin;
-        afarpb = SnConfigFrame::kAfarComWin;
-    }
-    // TODO: turn on amps individually, when that's possible
-    
-    // change cards power
-#ifdef DEBUG
-    printf("setting cards pin power\r\n");
-#endif
-    PIN_turn_on_system = gConf.GetPowPinSetting(cardpb);
+    PIN_turn_on_system = value;
 #ifdef USE_RTOS
     Thread::wait(10);
 #else
     wait_ms(10);
 #endif
+}
+
+void ChangeAmpsPower(const bool isCommWin) {
     // change amps power
+    const SnConfigFrame::EPowerModeBit pbit = (isCommWin)
+        ? SnConfigFrame::kAmpsComWin
+        : SnConfigFrame::kAmpsDatTak;
+    const int value = gConf.GetPowPinSetting(pbit);
 #ifdef DEBUG
-    printf("setting amps pin power\r\n");
+    printf("setting amps pin power to %d (comm=%d)\r\n",
+        value, static_cast<int>(isCommWin));
 #endif
-    PIN_turn_on_amps = gConf.GetPowPinSetting(ampspb);
+    PIN_turn_on_amps = value;
 #ifdef USE_RTOS
     Thread::wait(10);
 #else
     wait_ms(10);
 #endif
-    // change iridium power
-    // power down periph if going from on to off
+}
+
+void ChangeIridPower(const bool isCommWin) {
+    // change iridium power setting
+    // if going from ON to OFF, call the special
+    // power down procedure, so as not to lose
+    // the non-volatile memory settings
+
+    const SnConfigFrame::EPowerModeBit ibit = (isCommWin)
+        ? SnConfigFrame::kIridComWin
+        : SnConfigFrame::kIridDatTak;
+    const SnConfigFrame::EPowerModeBit abit = (isCommWin)
+        ? SnConfigFrame::kAfarComWin
+        : SnConfigFrame::kAfarDatTak;
+    
+    // these checks are complicated because the iridium might
+    // be powered off the Afar (12V) relay, 
+    // rather than the iridium (5V) relay
     const bool iridToOn = (kIridPwrFromAfar) ?
-        gConf.IsPoweredFor(afarpb) :
-        gConf.IsPoweredFor(iridpb);
-    const bool iridFromOn = PIN_iridSbd_power.read()==
-        (kIridPwrFromAfar ? gConf.GetPowPinSetting(afarpb, true)
-                          : gConf.GetPowPinSetting(iridpb, true));
+        (gConf.IsPoweredFor(abit) || gConf.IsPoweredFor(ibit)) :
+        gConf.IsPoweredFor(ibit);
+    const bool iridFromOn = (kIridPwrFromAfar) ?
+        IsCurrentlyPowered(abit, PIN_afar_power) :
+        IsCurrentlyPowered(ibit, PIN_iridSbd_power);
     if ( iridFromOn && (iridToOn==false) ) {
 #ifdef DEBUG
         printf("calling PowerDown for Iridium\r\n");
 #endif
         PowerDownCommPeriph(SnConfigFrame::kIrid);
     }
+    const int value = (kIridPwrFromAfar)
+        ? gConf.GetPowPinSetting(ibit, false) // leave the iridium relay off. use afar relay.
+        : gConf.GetPowPinSetting(ibit);
 #ifdef DEBUG
-    printf("setting iridium pin power\r\n");
+    printf("setting iridium pin power to %d (comm=%d)\r\n",
+        value, static_cast<int>(isCommWin));
 #endif
-    PIN_iridSbd_power = (kIridPwrFromAfar)
-        ? gConf.GetPowPinSetting(iridpb, false) // leave the iridium relay off. use afar relay.
-        : gConf.GetPowPinSetting(iridpb);
+    PIN_iridSbd_power = value;
 #ifdef USE_RTOS
     Thread::wait(10);
 #else
     wait_ms(10);
 #endif
+    
+}
+
+void ChangeAfarPower(const bool isCommWin) {
+    // change the AFAR relay power setting
+    // also power up or down the ethernet PHY port
+    // as appropriate
+    // the procedure is complicated by the fact that
+    // the iridium may be powered off the Afar (12V) relay,
+    // in which case we need to turn this relay on if
+    // *either* Afar or iridium want power
+#ifdef DEBUG
+    printf("bef: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
+        LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
+    printf("pcenet bef power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
+#endif
+    
+    const SnConfigFrame::EPowerModeBit ibit = (isCommWin)
+        ? SnConfigFrame::kIridComWin
+        : SnConfigFrame::kIridDatTak;
+    const SnConfigFrame::EPowerModeBit abit = (isCommWin)
+        ? SnConfigFrame::kAfarComWin
+        : SnConfigFrame::kAfarDatTak;
+
+    const bool afarFromOn = IsCurrentlyPowered(abit, PIN_afar_power);
+    const bool afarToOn = gConf.IsPoweredFor(abit) ||
+        (kIridPwrFromAfar && gConf.IsPoweredFor(ibit));
+
     // change ethernet PHY port power
 #ifdef DEBUG
-    printf("afar pin=%d, com powsetting=%d\r\n",PIN_afar_power.read(),
-        gConf.GetPowPinSetting(afarpb));
+    printf("afar pin=%d, afarFromOn=%d, afarToOn=%d\r\n",
+        PIN_afar_power.read(), static_cast<int>(afarFromOn),
+        static_cast<int>(afarToOn));
 #endif
-    if (gConf.IsPoweredFor(afarpb) ||
-        (kIridPwrFromAfar && gConf.IsPoweredFor(iridpb)) ) {
+    if (afarToOn) {
 #ifdef DEBUG
         printf("PHY cowin powering up\r\n");
 #endif
@@ -1354,13 +1450,6 @@
         // change afar power
         // power down periph if going from on to off
         // change afar power
-        int afon = gConf.GetPowPinSetting(afarpb, true);
-        if (kIridPwrFromAfar) {
-            // NOTE: the following only works because for the
-            // irid & afar pins, 1 = on!
-            afon |= gConf.GetPowPinSetting(iridpb, true);
-        }
-        const bool afarFromOn = (PIN_afar_power.read()==afon);
         if (afarFromOn) {
             PowerDownCommPeriph(SnConfigFrame::kAfar);
         }
@@ -1385,14 +1474,10 @@
 #else
     wait_ms(100);
 #endif
+
     // change afar power
-    int afpp = gConf.GetPowPinSetting(afarpb);
-    if (kIridPwrFromAfar) {
-        // NOTE: the following only works because for the
-        // irid & afar pins, 1 = on!
-        afpp |= gConf.GetPowPinSetting(iridpb);
-    }
-    PIN_afar_power = afpp;
+    const int value = gConf.GetPowPinSetting(abit, afarToOn);
+    PIN_afar_power = value;
 #ifdef USE_RTOS
     Thread::wait(1500);
 #else
@@ -1401,13 +1486,117 @@
 #ifdef DEBUG
     printf("aft: pconp=%u (%08x), pcenet=%u (%08x)\r\n",
         LPC_SC->PCONP, LPC_SC->PCONP, LPC1768_PCONP_PCENET, LPC1768_PCONP_PCENET);
+    printf("pcenet aft power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
 #endif
+    
+}
+
+void SetPower(const bool isCommWin) {
+#ifdef DEBUG
+    printf("set power. isCommWin=%s\r\n",(isCommWin)?"true":"false");
+    printf("WD reset = %d\r\n",(int)Watchdog::didWatchdogReset());
+    printf("power word (%hhu): ",gConf.GetPowerMode()); SnBitUtils::printBits(gConf.GetPowerMode(),true);
+    printf("IsPoweredFor CardDatTak = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kCardDatTak));
+    printf("IsPoweredFor AmpsDatTak = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kAmpsDatTak));
+    printf("IsPoweredFor IridDatTak = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kIridDatTak));
+    printf("IsPoweredFor AfarDatTak = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kAfarDatTak));
+    printf("IsPoweredFor CardComWin = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kCardComWin));
+    printf("IsPoweredFor AmpsComWin = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kAmpsComWin));
+    printf("IsPoweredFor IridComWin = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kIridComWin));
+    printf("IsPoweredFor AfarComWin = %d\r\n",(int)gConf.IsPoweredFor(SnConfigFrame::kAfarComWin));
+#endif
+    
+    const SnConfigFrame::EPowerModeBit cardpb = (isCommWin)
+        ? SnConfigFrame::kCardComWin
+        : SnConfigFrame::kCardDatTak;
+    const SnConfigFrame::EPowerModeBit ampspb = (isCommWin)
+        ? SnConfigFrame::kAmpsComWin
+        : SnConfigFrame::kAmpsDatTak;
+    const SnConfigFrame::EPowerModeBit iridpb = (isCommWin)
+        ? SnConfigFrame::kIridComWin
+        : SnConfigFrame::kIridDatTak;
+    const SnConfigFrame::EPowerModeBit afarpb = (isCommWin)
+        ? SnConfigFrame::kAfarComWin
+        : SnConfigFrame::kAfarDatTak;
+    // TODO: turn on amps individually, when that's possible
+    
+    //
+    // FIRST turn the things off that should be off.
+    // this prevents short periods of high power usage
+    //
+
+    // iridium is more complicated because it might
+    // be powered off of the afar line
+    const bool iridToOn = (kIridPwrFromAfar) ?
+        gConf.IsPoweredFor(afarpb) :
+        gConf.IsPoweredFor(iridpb);
+    const bool afarToOn = gConf.IsPoweredFor(afarpb) ||
+        (kIridPwrFromAfar && gConf.IsPoweredFor(iridpb));
+
+    if (gConf.IsPoweredFor(cardpb)==false) {
+#ifdef DEBUG
+        printf("power down cards\r\n");
+#endif
+        ChangeCardPower(isCommWin);
+    }
+    if (gConf.IsPoweredFor(ampspb)==false) {
+#ifdef DEBUG
+        printf("power down amps\r\n");
+        printf("ampspb=%d, is powered for =%d\r\n",
+            (int)ampspb,
+            (int)(gConf.IsPoweredFor(ampspb)));
+        printf("is powered amps dat tak = %d, amps com win = %d\r\n",
+            (int)(gConf.IsPoweredFor(SnConfigFrame::kAmpsDatTak)),
+            (int)(gConf.IsPoweredFor(SnConfigFrame::kAmpsComWin)));
+#endif
+        ChangeAmpsPower(isCommWin);
+    }
+    if (iridToOn==false) {
+#ifdef DEBUG
+        printf("power down irid\r\n");
+#endif
+        ChangeIridPower(isCommWin);
+    }
+    if (afarToOn==false) {
+#ifdef DEBUG
+        printf("power down afar\r\n");
+#endif
+        ChangeAfarPower(isCommWin);
+    }
+    
+    //
+    // THEN turn on things that want to go on
+    //
+    if (gConf.IsPoweredFor(cardpb)) {
+#ifdef DEBUG
+        printf("power ON cards\r\n");
+#endif
+        ChangeCardPower(isCommWin);
+    }
+    if (gConf.IsPoweredFor(ampspb)) {
+#ifdef DEBUG
+        printf("power ON amps\r\n");
+#endif
+        ChangeAmpsPower(isCommWin);
+    }
+    if (iridToOn) {
+#ifdef DEBUG
+        printf("power ON iridium\r\n");
+#endif
+        ChangeIridPower(isCommWin);
+    }
+    if (afarToOn) {
+#ifdef DEBUG
+        printf("power ON afar\r\n");
+#endif
+        ChangeAfarPower(isCommWin);
+    }
+    
 #ifdef DEBUG
     printf("power word (%hhu): ",gConf.GetPowerMode()); SnBitUtils::printBits(gConf.GetPowerMode(),true);
     printf("set power (iscom %d, pw %hhu): cards %d, amps %d, irid %d, afar %d\r\n",
         isCommWin, gConf.GetPowerMode(), PIN_turn_on_system.read(), PIN_turn_on_amps.read(),
         PIN_iridSbd_power.read(), PIN_afar_power.read());
-    printf("pcenet aft power: status=%d\r\n",Peripheral_GetStatus(LPC1768_PCONP_PCENET));
 #endif
 }
 
@@ -1666,7 +1855,6 @@
             wait_us(1);
             PIN_DoNotRestartAllClocks    = 1;
 #endif
-            //led3 = !led3; // toggle send clock led
         } else {
             gFirstEvt = false;
         }
@@ -1772,16 +1960,9 @@
     //  a) try to connect
     //  b) if connected, listen for config
     //  c) if config requests data, send it
-    /*
-    for (int i=0; i<5; i++) {
-        led4=1;
-        led3=1;
-        wait(0.5);
-        led4=0;
-        led3=0;
-        wait(0.5);
-    }
-    */
+    
+    led2 = 1;
+    
     gLastCommWin = time(0);
 
     SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
@@ -2147,6 +2328,8 @@
 #ifdef DEBUG
     printf("closing comm win at %d\r\n",(int32_t)time(0));
 #endif
+
+    led2 = 0;
     
     gCommWinOpen = false;
     return res;