From 86a856f6d54268f870156bddf701178c19a69e7a Mon Sep 17 00:00:00 2001
From: Ruud Overeem <overeem@astron.nl>
Date: Fri, 25 May 2007 14:28:37 +0000
Subject: [PATCH] BugID: 1000 Fixed bug in HBA write. I2C table was patched too
 many times.

---
 MAC/APL/PIC/RSPDriver/src/GetRCUCmd.cc        | 12 +--
 MAC/APL/PIC/RSPDriver/src/HBAProtocolWrite.cc | 97 +++++++++++--------
 MAC/APL/PIC/RSPDriver/src/RCUProtocolWrite.cc |  6 +-
 MAC/APL/PIC/RSPDriver/src/RCURead.cc          | 15 +--
 MAC/APL/PIC/RSPDriver/src/RCUResultRead.cc    |  9 +-
 MAC/APL/PIC/RSPDriver/src/RSPDriver.cc        | 39 +++-----
 MAC/APL/PIC/RSPDriver/src/rspctl.cc           |  2 +-
 7 files changed, 85 insertions(+), 95 deletions(-)

diff --git a/MAC/APL/PIC/RSPDriver/src/GetRCUCmd.cc b/MAC/APL/PIC/RSPDriver/src/GetRCUCmd.cc
index 1849359272e..fa2017d525c 100644
--- a/MAC/APL/PIC/RSPDriver/src/GetRCUCmd.cc
+++ b/MAC/APL/PIC/RSPDriver/src/GetRCUCmd.cc
@@ -61,16 +61,12 @@ void GetRCUCmd::ack(CacheBuffer& cache)
   ack.settings().resize(m_event->rcumask.count());
   
   int result_rcu = 0;
-  for (int cache_rcu = 0; cache_rcu < StationSettings::instance()->nrRcus(); cache_rcu++)
-  {
-    if (m_event->rcumask[cache_rcu])
-    {
-      if (cache_rcu < StationSettings::instance()->nrRcus())
-      {
+  for (int cache_rcu = 0; cache_rcu < StationSettings::instance()->nrRcus(); cache_rcu++) {
+    if (m_event->rcumask[cache_rcu]) {
+      if (cache_rcu < StationSettings::instance()->nrRcus()) {
 	ack.settings()(result_rcu) = cache.getRCUSettings()()(cache_rcu);
       }
-      else
-      {
+      else {
 	LOG_WARN(formatString("invalid RCU index %d, there are only %d RCU's", cache_rcu, 
 		StationSettings::instance()->nrRcus()));
       }
diff --git a/MAC/APL/PIC/RSPDriver/src/HBAProtocolWrite.cc b/MAC/APL/PIC/RSPDriver/src/HBAProtocolWrite.cc
index dae12a74699..9ffe9ae4e19 100644
--- a/MAC/APL/PIC/RSPDriver/src/HBAProtocolWrite.cc
+++ b/MAC/APL/PIC/RSPDriver/src/HBAProtocolWrite.cc
@@ -22,6 +22,7 @@
 
 #include <lofar_config.h>
 #include <Common/LofarLogger.h>
+#include <Common/hexdump.h>
 #include <APL/RSP_Protocol/EPA_Protocol.ph>
 
 #include <APL/RTCCommon/PSAccess.h>
@@ -53,40 +54,40 @@ namespace LOFAR {
     //
     int HBAProtocolWrite::i2c_protocol_patch_indices[] = {
       8, 9,
-      51, // stride is 17, server 1
-      51 + 17,
-      51 + 17 + 17,
-      51 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17,
-      51 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17 + 17, // server 16
+	  51 + ( 0*17), // stride is 17, server 1
+	  51 + ( 1*17),
+	  51 + ( 2*17),
+	  51 + ( 3*17),
+	  51 + ( 4*17),
+	  51 + ( 5*17),
+	  51 + ( 6*17),
+	  51 + ( 7*17),
+	  51 + ( 8*17),
+	  51 + ( 9*17),
+	  51 + (10*17),
+	  51 + (11*17),
+	  51 + (12*17),
+	  51 + (13*17),
+	  51 + (14*17),
+	  51 + (15*17), // server  16
     };
     int HBAProtocolWrite::i2c_result_patch_indices[] = {
-      4, // stride is 7, server 1
-      4 + 7,
-      4 + 7 + 7,
-      4 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7,
-      4 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 7, // server 16
+	   4 + ( 0*7), // stride is 7, server 1
+	   4 + ( 1*7),
+	   4 + ( 2*7),
+	   4 + ( 3*7),
+	   4 + ( 4*7),
+	   4 + ( 5*7),
+	   4 + ( 6*7),
+	   4 + ( 7*7),
+	   4 + ( 8*7),
+	   4 + ( 9*7),
+	   4 + (10*7),
+	   4 + (11*7),
+	   4 + (12*7),
+	   4 + (13*7),
+	   4 + (14*7),
+	   4 + (15*7), // server 16
     };
 
 #ifndef HBA_WRITE_DELAYS
@@ -266,18 +267,26 @@ HBAProtocolWrite::HBAProtocolWrite(GCFPortInterface& board_port, int board_id)
   // using nrBlpsPerBoard() because only the Y RCU's (odd numbered 1,3,5,...)
   // control both the X and Y delays of HBA
 {
-  memset(&m_hdr, 0, sizeof(MEPHeader));
+	static	bool	i2c_tables_patched = false;
+
+	memset(&m_hdr, 0, sizeof(MEPHeader));
 
 #ifdef HBA_WRITE_DELAYS
-  // add the hba server address offset to specified indices
-  for (int i = 0; i < (int)(sizeof(i2c_protocol_patch_indices)/sizeof(int)); i++) {
-    i2c_protocol[i2c_protocol_patch_indices[i]]
-      += GET_CONFIG("RSPDriver.HBA_SERVER_ADDRESS_OFFSET", i);
-  }
-  for (int i = 0; i < (int)(sizeof(i2c_result_patch_indices)/sizeof(int)); i++) {
-    i2c_result[i2c_result_patch_indices[i]]
-      += GET_CONFIG("RSPDriver.HBA_SERVER_ADDRESS_OFFSET", i);
-  }
+	LOG_INFO_STR("HBAProtocolWrite: " << board_id);
+	if (!i2c_tables_patched) {
+		// add the hba server address offset to specified indices
+		int		offset(GET_CONFIG("RSPDriver.HBA_SERVER_ADDRESS_OFFSET", i));
+		LOG_INFO_STR ("Patching i2c tables with offset " << offset);
+		// patch protocol table
+		for (int i = 0; i < (int)(sizeof(i2c_protocol_patch_indices)/sizeof(int)); i++) {
+			i2c_protocol[i2c_protocol_patch_indices[i]] += offset;
+		}
+		// patch result table
+		for (int i = 0; i < (int)(sizeof(i2c_result_patch_indices)/sizeof(int)); i++) {
+			i2c_result[i2c_result_patch_indices[i]] += offset;
+		}
+		i2c_tables_patched = true;
+	}
 #endif
 }
 
@@ -309,6 +318,7 @@ void HBAProtocolWrite::sendrequest()
 
   // delays for at least on HBA need to be written, and the RCUProtocol register is not in use by RCUProtocolWrite
 
+	LOG_INFO_STR("HBAsendrequest: " << getCurrentIndex());
   switch (getCurrentIndex() % N_WRITES) {
     
   case 0:
@@ -343,6 +353,9 @@ void HBAProtocolWrite::sendrequest()
       EPARcuProtocolEvent rcuprotocol;
       rcuprotocol.hdr.set(MEPHeader::RCU_PROTOCOLY_HDR, 1 << (getCurrentIndex() / N_WRITES), MEPHeader::WRITE, sizeof(i2c_protocol));
       rcuprotocol.protocol.setBuffer(i2c_protocol, sizeof(i2c_protocol));
+	  string tmpbuf;
+	  hexdump (tmpbuf, i2c_protocol, sizeof(i2c_protocol));
+	  LOG_INFO_STR("HBA WRITE: " << tmpbuf);
   
       m_hdr = rcuprotocol.hdr; // remember header to match with ack
       getBoardPort().send(rcuprotocol);
diff --git a/MAC/APL/PIC/RSPDriver/src/RCUProtocolWrite.cc b/MAC/APL/PIC/RSPDriver/src/RCUProtocolWrite.cc
index c8f90a948b7..07fbc720793 100644
--- a/MAC/APL/PIC/RSPDriver/src/RCUProtocolWrite.cc
+++ b/MAC/APL/PIC/RSPDriver/src/RCUProtocolWrite.cc
@@ -146,8 +146,7 @@ void RCUProtocolWrite::sendrequest_status()
 
 GCFEvent::TResult RCUProtocolWrite::handleack(GCFEvent& event, GCFPortInterface& /*port*/)
 {
-  if (EPA_WRITEACK != event.signal)
-  {
+  if (EPA_WRITEACK != event.signal) {
     LOG_WARN("RCUProtocolWrite::handleack:: unexpected ack");
     return GCFEvent::NOT_HANDLED;
   }
@@ -156,8 +155,7 @@ GCFEvent::TResult RCUProtocolWrite::handleack(GCFEvent& event, GCFPortInterface&
 
   uint8 global_rcu = (getBoardId() * StationSettings::instance()->nrRcusPerBoard()) + (getCurrentIndex() / N_WRITES);
 
-  if (!ack.hdr.isValidAck(m_hdr))
-  {
+  if (!ack.hdr.isValidAck(m_hdr)) {
     LOG_ERROR("RCUProtocolWrite::handleack: invalid ack");
     Cache::getInstance().getState().rcuprotocol().write_error(global_rcu);
     return GCFEvent::NOT_HANDLED;
diff --git a/MAC/APL/PIC/RSPDriver/src/RCURead.cc b/MAC/APL/PIC/RSPDriver/src/RCURead.cc
index 3e2217cdbeb..d62e5b880e1 100644
--- a/MAC/APL/PIC/RSPDriver/src/RCURead.cc
+++ b/MAC/APL/PIC/RSPDriver/src/RCURead.cc
@@ -63,16 +63,14 @@ void RCURead::sendrequest_status()
 
 GCFEvent::TResult RCURead::handleack(GCFEvent& event, GCFPortInterface& /*port*/)
 {
-  if (EPA_RCU_SETTINGS != event.signal)
-  {
+  if (EPA_RCU_SETTINGS != event.signal) {
     LOG_WARN("RCURead::handleack: unexpected ack");
     return GCFEvent::NOT_HANDLED;
   }
 
   EPARcuSettingsEvent rcusettings(event);
 
-  if (!rcusettings.hdr.isValidAck(m_hdr))
-  {
+  if (!rcusettings.hdr.isValidAck(m_hdr)) {
     LOG_ERROR("RCURead::handleack: invalid ack");
     return GCFEvent::NOT_HANDLED;
   }
@@ -83,16 +81,13 @@ GCFEvent::TResult RCURead::handleack(GCFEvent& event, GCFPortInterface& /*port*/
   RCUSettings::Control& x = Cache::getInstance().getBack().getRCUSettings()()((global_blp * 2));
   RCUSettings::Control& y = Cache::getInstance().getBack().getRCUSettings()()((global_blp * 2) + 1);
 
-  if (0 == GET_CONFIG("RSPDriver.LOOPBACK_MODE", i))
-  {
+  if (0 == GET_CONFIG("RSPDriver.LOOPBACK_MODE", i)) {
     EPA_Protocol::RCUHandler cachedvalue = { x.getDelay(), 0, y.getDelay(), 0 };
-    if (memcmp(&cachedvalue, &rcusettings.ap, sizeof(EPA_Protocol::RCUHandler)))
-    {
+    if (memcmp(&cachedvalue, &rcusettings.ap, sizeof(EPA_Protocol::RCUHandler))) {
       LOG_WARN("LOOPBACK CHECK FAILED: RCURead mismatch ");
     }
   }
-  else
-  {
+  else {
     x.setDelay(rcusettings.ap.input_delay_x);
     y.setDelay(rcusettings.ap.input_delay_y);
   }
diff --git a/MAC/APL/PIC/RSPDriver/src/RCUResultRead.cc b/MAC/APL/PIC/RSPDriver/src/RCUResultRead.cc
index 34699024f92..a4f13ebcbb4 100644
--- a/MAC/APL/PIC/RSPDriver/src/RCUResultRead.cc
+++ b/MAC/APL/PIC/RSPDriver/src/RCUResultRead.cc
@@ -63,7 +63,8 @@ void RCUResultRead::sendrequest()
   MEPHeader::FieldsType hdr;
   if (0 == global_rcu % MEPHeader::N_POL) {
     hdr = MEPHeader::RCU_RESULTX_HDR;
-  } else {
+  } 
+  else {
     hdr = MEPHeader::RCU_RESULTY_HDR;
   }
 
@@ -81,8 +82,7 @@ void RCUResultRead::sendrequest_status()
 
 GCFEvent::TResult RCUResultRead::handleack(GCFEvent& event, GCFPortInterface& /*port*/)
 {
-  if (EPA_RCU_RESULT != event.signal)
-  {
+  if (EPA_RCU_RESULT != event.signal) {
     LOG_WARN("RCUResultRead::handleack:: unexpected ack");
     return GCFEvent::NOT_HANDLED;
   }
@@ -91,8 +91,7 @@ GCFEvent::TResult RCUResultRead::handleack(GCFEvent& event, GCFPortInterface& /*
 
   uint8 global_rcu = (getBoardId() * StationSettings::instance()->nrRcusPerBoard()) + getCurrentIndex();
 
-  if (!ack.hdr.isValidAck(m_hdr))
-  {
+  if (!ack.hdr.isValidAck(m_hdr)) {
     LOG_ERROR("RCUResultRead::handleack: invalid ack");
     Cache::getInstance().getState().rcuprotocol().read_error(global_rcu);
     return GCFEvent::HANDLED;
diff --git a/MAC/APL/PIC/RSPDriver/src/RSPDriver.cc b/MAC/APL/PIC/RSPDriver/src/RSPDriver.cc
index 8325351dda7..57cef0398ba 100644
--- a/MAC/APL/PIC/RSPDriver/src/RSPDriver.cc
+++ b/MAC/APL/PIC/RSPDriver/src/RSPDriver.cc
@@ -1576,8 +1576,7 @@ void RSPDriver::rsp_setrcu(GCFEvent& event, GCFPortInterface& port)
 {
   Ptr<SetRCUCmd> command = new SetRCUCmd(event, port, Command::WRITE);
 
-  if (!command->validate())
-  {
+  if (!command->validate()) {
     LOG_ERROR("SETRCU: invalid parameter");
     
     RSPSetrcuackEvent ack;
@@ -1588,14 +1587,12 @@ void RSPDriver::rsp_setrcu(GCFEvent& event, GCFPortInterface& port)
   }
 
   // if timestamp == Timestamp(0,0) apply changes immediately
-  if (Timestamp(0,0) == command->getTimestamp())
-  {
+  if (Timestamp(0,0) == command->getTimestamp()) {
     LOG_INFO("applying RCU control immediately");
     command->apply(Cache::getInstance().getFront(), true);
     command->apply(Cache::getInstance().getBack(), false);
   }
-  else
-  {
+  else {
     (void)m_scheduler.enter(Ptr<Command>(&(*command)));
   }
   command->ack(Cache::getInstance().getFront());
@@ -1608,8 +1605,7 @@ void RSPDriver::rsp_getrcu(GCFEvent& event, GCFPortInterface& port)
 {
   Ptr<GetRCUCmd> command = new GetRCUCmd(event, port, Command::READ);
 
-  if (!command->validate())
-  {
+  if (!command->validate()) {
     LOG_ERROR("GETRCU: invalid parameter");
     
     RSPGetrcuackEvent ack;
@@ -1622,13 +1618,11 @@ void RSPDriver::rsp_getrcu(GCFEvent& event, GCFPortInterface& port)
   
   // if null timestamp get value from the cache and acknowledge immediately
   if ( (Timestamp(0,0) == command->getTimestamp())
-       && (true == command->readFromCache()))
-  {
+       && (true == command->readFromCache())) {
     command->setTimestamp(Cache::getInstance().getFront().getTimestamp());
     command->ack(Cache::getInstance().getFront());
   }
-  else
-  {
+  else {
     (void)m_scheduler.enter(Ptr<Command>(&(*command)));
   }
 }
@@ -1642,8 +1636,7 @@ void RSPDriver::rsp_subrcu(GCFEvent& event, GCFPortInterface& port)
   Ptr<UpdRCUCmd> command = new UpdRCUCmd(event, port, Command::READ);
   RSPSubrcuackEvent ack;
 
-  if (!command->validate())
-  {
+  if (!command->validate()) {
     LOG_ERROR("SUBRCU: invalid parameter");
     
     ack.timestamp = m_scheduler.getCurrentTime();
@@ -1653,13 +1646,11 @@ void RSPDriver::rsp_subrcu(GCFEvent& event, GCFPortInterface& port)
     port.send(ack);
     return;
   }
-  else
-  {
-    ack.timestamp = m_scheduler.getCurrentTime();
-    ack.status = SUCCESS;
-    ack.handle = (uint32)&(*command);
-    port.send(ack);
-  }
+
+  ack.timestamp = m_scheduler.getCurrentTime();
+  ack.status = SUCCESS;
+  ack.handle = (uint32)&(*command);
+  port.send(ack);
 
   (void)m_scheduler.enter(Ptr<Command>(&(*command)),
                           Scheduler::PERIODIC);
@@ -1677,12 +1668,10 @@ void RSPDriver::rsp_unsubrcu(GCFEvent& event, GCFPortInterface& port)
   ack.status = FAILURE;
   ack.handle = unsub.handle;
 
-  if (m_scheduler.remove_subscription(port, unsub.handle) > 0)
-  {
+  if (m_scheduler.remove_subscription(port, unsub.handle) > 0) {
     ack.status = SUCCESS;
   }
-  else
-  {
+  else {
     LOG_ERROR("UNSUBRCU: failed to remove subscription");
   }
 
diff --git a/MAC/APL/PIC/RSPDriver/src/rspctl.cc b/MAC/APL/PIC/RSPDriver/src/rspctl.cc
index a6242cf434c..86c8f2dc571 100644
--- a/MAC/APL/PIC/RSPDriver/src/rspctl.cc
+++ b/MAC/APL/PIC/RSPDriver/src/rspctl.cc
@@ -507,7 +507,7 @@ void HBACommand::send()
 
     gethba.timestamp = Timestamp(0,0);
     gethba.rcumask = getRCUMask();
-    gethba.cache = true;
+    gethba.cache = false;
 
     m_rspport.send(gethba);
   }
-- 
GitLab