Index: trunk/FACT++/src/fadctrl.cc
===================================================================
--- trunk/FACT++/src/fadctrl.cc	(revision 11626)
+++ trunk/FACT++/src/fadctrl.cc	(revision 11627)
@@ -373,5 +373,6 @@
         case FAD::kCmdDwrite:      fBufEventHeader.Enable(FAD::EventHeader::kDwrite,      on);  break;
         case FAD::kCmdTriggerLine: fBufEventHeader.Enable(FAD::EventHeader::kTriggerLine, on);  break;
-        case FAD::kCmdBusy:        fBufEventHeader.Enable(FAD::EventHeader::kBusy,        on);  break;
+        case FAD::kCmdBusyOn:      fBufEventHeader.Enable(FAD::EventHeader::kBusyOn,      on);  break;
+        case FAD::kCmdBusyOff:     fBufEventHeader.Enable(FAD::EventHeader::kBusyOff,     on);  break;
         case FAD::kCmdContTrigger: fBufEventHeader.Enable(FAD::EventHeader::kContTrigger, on);  break;
         case FAD::kCmdSocket:      fBufEventHeader.Enable(FAD::EventHeader::kSock17,      !on); break;
@@ -1083,20 +1084,20 @@
         T::Message(str.str());
 
+        const FAD::Configuration &conf = fTargetConfig->second;
+
         for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
         {
-            const FAD::Configuration &conf = fTargetConfig->second;
-
             ConnectionFAD &fad  = *it->second;
 
+            fad.Cmd(FAD::kCmdBusyOn,      true);  // continously on
             fad.Cmd(FAD::kCmdTriggerLine, false);
             fad.Cmd(FAD::kCmdContTrigger, false);
             fad.Cmd(FAD::kCmdSocket,      true);
-            fad.Cmd(FAD::kCmdBusy,        true);
+            fad.Cmd(FAD::kCmdBusyOff,     true);  // normal when BusyOn==0
 
             fad.Cmd(FAD::kCmdDwrite,      conf.fDwrite);
             fad.Cmd(FAD::kCmdDrsEnable,   conf.fDenable);
-            fad.Cmd(FAD::kCmdContTrigger, conf.fContinousTrigger);
-
-            for (int i=0; i<FAD::kNumDac; i++)
+
+            for (int i=1; i<4/*FAD::kNumDac*/; i++)
                 fad.CmdSetDacValue(i, conf.fDac[i]);
 
@@ -1109,7 +1110,17 @@
             fad.Cmd(FAD::kCmdResetEventCounter);
             //fad.Cmd(FAD::kCmdSingleTrigger);
-        }
-
-        return FAD::kConfiguring;
+            //fad.Cmd(FAD::kCmdTriggerLine, true);
+        }
+
+        // Now the old run is stopped already. So all other servers can start a new run
+        // (Note that we might need another step which only checks if the continous trigger
+        //  is wwitched off, too)
+        fDimStartRun.Update(int64_t(runno));
+
+        T::Info(" ==> TODO: Insert/update run configuration in database!");
+
+        fConfigTimer = Time();
+
+        return FAD::kConfiguring1;
     }
 
@@ -1412,30 +1423,43 @@
         if (nconnecting1==0 && nconnected1>0 && nconnected2==nconnected1)
         {
-            if (T::GetCurrentState()==FAD::kConfigured && IsRunStarted())
-                return FAD::kConnected;
-
-            if (nconfigured!=nconnected1)
-            {
-
-                if (T::GetCurrentState()==FAD::kConfiguring ||
-                    T::GetCurrentState()==FAD::kConfigured)
-                    // Stay in Configured until at least one new
-                    // event has been received
-                    return T::GetCurrentState();
-
-                return FAD::kConnected;
-            }
-
-            if (T::GetCurrentState()==FAD::kConfiguring)
-            {
+            if (T::GetCurrentState()==FAD::kConfiguring1)
+            {
+                // We need some delay so that the FAD is not busy anymore
+                // and really sees the software trigger
+                // FIXME: Do we need this to be configurable?
+                if (Time()-fConfigTimer<boost::posix_time::milliseconds(100))
+                    return FAD::kConfiguring1;
+
+                for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
+                    it->second->Cmd(FAD::kCmdSingleTrigger);
+
+                return FAD::kConfiguring2;
+            }
+
+            // If all boards are configured and we are configuring
+            // go on and start the FADs
+            if (T::GetCurrentState()==FAD::kConfiguring2)
+            {
+                // If not all boards have yet received the proper
+                // configuration
+                if (nconfigured!=nconnected1)
+                    return FAD::kConfiguring2;
+
+                // FIXME: Distinguish between not all boards have received
+                // the configuration and the configuration is not consistent
+
                 for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
                 {
-                    //const Configuration &conf = fTargetConfig->second;
-
                     ConnectionFAD &fad  = *it->second;
 
+                    // Make sure that after switching on the trigger line
+                    // there needs to be some waiting before all boards
+                    // can be assumed to be listening
                     fad.Cmd(FAD::kCmdResetEventCounter);
                     fad.Cmd(FAD::kCmdSocket,      false);
                     fad.Cmd(FAD::kCmdTriggerLine, true);
+                    if (fTargetConfig->second.fContinousTrigger)
+                        fad.Cmd(FAD::kCmdContTrigger, true);
+                    fad.Cmd(FAD::kCmdBusyOn,      false);  // continously on
 
                     // FIXME: How do we find out when the FADs
@@ -1443,4 +1467,14 @@
                 }
                 return FAD::kConfigured;
+            }
+
+            if (T::GetCurrentState()==FAD::kConfigured)
+            {
+                // Stay in Configured as long as we have a valid
+                // configuration and the run has not yet been started
+                // (means the the event builder has received its
+                // first event)
+                if (!IsRunStarted() && nconfigured==nconnected1)
+                    return FAD::kConfigured;
             }
 
@@ -1483,4 +1517,5 @@
 
 
+    DimDescribedService fDimStartRun;
     DimDescribedService fDimConnection;
 
@@ -1502,4 +1537,5 @@
         T(out, "FAD_CONTROL"), EventBuilderWrapper(*static_cast<MessageImp*>(this)), ba::io_service::work(static_cast<ba::io_service&>(*this)),
         fStatus1(40), fStatus2(40), fStatusC(40), fStatusT(false),
+        fDimStartRun("FAD_CONTROL/START_RUN", "X:1", ""),
         fDimConnection("FAD_CONTROL/CONNECTIONS", "C:40;C:1", "")
     {
@@ -1511,4 +1547,6 @@
         // deletion and creation of threads and more.
 
+        fDimStartRun.Update(int64_t(-1));
+
         // State names
         T::AddStateName(FAD::kOffline, "Disengaged",
@@ -1524,5 +1562,8 @@
                         "All enabled FAD boards are connected..");
 
-        T::AddStateName(FAD::kConfiguring, "Configuring",
+        T::AddStateName(FAD::kConfiguring1, "Configuring1",
+                        ".");
+
+        T::AddStateName(FAD::kConfiguring2, "Configuring2",
                         ".");
 
@@ -1544,7 +1585,10 @@
             (bind(&StateMachineFAD::CmdEnable, this, placeholders::_1, FAD::kCmdSrclk))
             ("Set SRCLK");
-        T::AddEvent("ENABLE_BUSY", "B:1", FAD::kConnecting, FAD::kConnected)
-            (bind(&StateMachineFAD::CmdEnable, this, placeholders::_1, FAD::kCmdBusy))
-            ("Set BUSY");
+        T::AddEvent("ENABLE_BUSY_OFF", "B:1", FAD::kConnecting, FAD::kConnected)
+            (bind(&StateMachineFAD::CmdEnable, this, placeholders::_1, FAD::kCmdBusyOff))
+            ("Set BUSY continously low");
+        T::AddEvent("ENABLE_BUSY_ON", "B:1", FAD::kConnecting, FAD::kConnected)
+            (bind(&StateMachineFAD::CmdEnable, this, placeholders::_1, FAD::kCmdBusyOn))
+            ("Set BUSY constantly high (has priority over BUSY_OFF)");
         T::AddEvent("ENABLE_SCLK", "B:1", FAD::kConnecting, FAD::kConnected)
             (bind(&StateMachineFAD::CmdEnable, this, placeholders::_1, FAD::kCmdSclk))
@@ -1622,5 +1666,5 @@
             ("");
 
-        T::AddEvent("RESET_CONFIGURE", FAD::kConfiguring, FAD::kConfigured)
+        T::AddEvent("RESET_CONFIGURE", FAD::kConfiguring1, FAD::kConfiguring2, FAD::kConfigured)
             (bind(&StateMachineFAD::ResetConfig, this))
             ("");
@@ -1789,4 +1833,6 @@
     Configs fConfigs;
     Configs::const_iterator fTargetConfig;
+
+    Time fConfigTimer;
 
 
