ICP  1
QxtrmInterface.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "QxtrmInterface.h"
3 #include "icputils.h"
4 
5 QxtrmInterface::QxtrmInterface(DAEstatus& dstatus, uint32_t* dau_memory, uint32_t nwords_memory, uint32_t* dau_registers, uint32_t nwords_registers)
6  : ISISVME(), m_init_done(false), m_device(""), m_trigger_func(NULL), m_trigger_func_arg(NULL), m_qxtrm(NULL),
7  m_dau_memory(dau_memory), m_nwords_memory(nwords_memory), m_dau_registers(dau_registers), m_nwords_registers(nwords_registers)
8 {
9  InitializeCriticalSection(&m_visa_critical);
10  setLoggerName("QxtrmInterface");
11  reinit(dstatus);
12 }
13 
15 {
16  return m_init_done;
17 }
18 
19 
21 {
23  m_init_done = false;
24  LOGSTR_INFORMATION("Calling QxtrmInterface::reinit()");
25  close(dstatus);
26 
27  std::string app_dir = Poco::Util::Application::instance().config().getString("application.dir");
28  std::string config_file = app_dir + "..\\..\\..\\qxtrm_config.xml";
29  if ( access(config_file.c_str(), 04) == -1 )
30  {
31  config_file = app_dir + "qxtrm_config.xml";
32  }
33  std::cerr << "Using config file " << config_file << std::endl;
34  std::cerr << "Initialising Qxtrm connection ... ";
35  if (m_dau_memory == NULL)
36  {
37  m_qxtrm = new Qxtrm_driver(config_file, "host", 0, 1, true);
38  }
39  else
40  {
41  m_qxtrm = new Qxtrm_driver(config_file, "host_loopback", 0, 1, true);
42  std::cerr << "Creating channel eth1_RDMA1_loop ... ";
43  Poco::SharedPtr<Qxtrm_channel> rdma_mem_loop = m_qxtrm->createChannel("eth1_RDMA1_loop");
44  std::cerr << "Creating channel eth1_RDMA2_loop ... ";
45  Poco::SharedPtr<Qxtrm_channel> rdma_reg_loop = m_qxtrm->createChannel("eth1_RDMA2_loop");
46  rdma_mem_loop->RDMARegisterMemory(m_dau_memory, m_nwords_memory * 4, 0x0);
47  rdma_reg_loop->RDMARegisterMemory(m_dau_registers, m_nwords_registers * 4, 0x0);
48  }
49  std::cerr << "DONE" << std::endl;
50  std::cerr << "Creating channel eth1_RDMA1 ... ";
51  m_rdma_mem = m_qxtrm->createChannel("eth1_RDMA1");
52  std::cerr << "DONE" << std::endl;
53  std::cerr << "Creating channel eth1_RDMA2 ... ";
54  m_rdma_reg = m_qxtrm->createChannel("eth1_RDMA2");
55  std::cerr << "DONE" << std::endl;
56  // reset interface
57 // m_rdma_reg->RDMAWrite(1, 0x00030010);
58 // m_rdma_reg->RDMAWrite(1, 0x00030014);
59 // m_rdma_reg->RDMAWrite(1, 0x00030018);
60 // m_rdma_reg->RDMAWrite(1, 0x0003001C);
61  m_qxtrm->channelStatus(std::cerr);
62  m_qxtrm->channelTransferStats(std::cerr);
63 
64 
65  m_init_done = true;
66  if (m_device != "")
67  {
68  open(m_device.c_str(), dstatus);
69  }
70  return ISISVME::Success;
71 }
72 
73 
74 
75 int QxtrmInterface::open(const char* device, DAEstatus& dstatus)
76 {
78  std::ostringstream message;
79  m_device = device;
80  return ISISVME::Success;
81 }
82 
83 int QxtrmInterface::lockDevice(int timeout, DAEstatus& dstatus)
84 {
85  return ISISVME::Success;
86 }
87 
89 {
90  return ISISVME::Success;
91 }
92 
93 template <typename F, typename T>
94 int QxtrmInterface::retrySingle(F pFunc, unsigned long address, T data, TransferProps props, DAEstatus& dstatus)
95 {
96  int current_sev = dstatus.severity();
97  int i, stat = ISISVME::Error;
98  for(i=0; i<m_tries; i++)
99  {
100  if (i > 0)
101  {
102  Sleep(m_retry_time);
104  if (i > 2)
105  {
106  LOGSTR_WARNING("retrySingle: address 0x" << std::hex << address << std::dec);
107  reinit(dstatus);
108  }
109  }
110  stat = (this->*pFunc)(address, data, props, dstatus);
111  if (stat == ISISVME::Success)
112  break;
113  }
114  if ( !(dstatus >= current_sev) )
115  {
116  dstatus.resetSeverityToAtLeast(current_sev);
117  }
118  return stat;
119 }
120 
121 template <typename F, typename T>
122 int QxtrmInterface::retryBlock(F pFunc, unsigned long address, T data, long nitems, TransferProps props, DAEstatus& dstatus)
123 {
124  int i, stat = ISISVME::Error;
125  int current_sev = dstatus.severity();
126  for(i=0; i<m_tries; i++)
127  {
128  if (i > 0)
129  {
130  Sleep(m_retry_time);
132  if (i > 2)
133  {
134  LOGSTR_WARNING("retryBlock: " << nitems << " items from address 0x" << std::hex << address << std::dec);
135  reinit(dstatus);
136  }
137  }
138  stat = (this->*pFunc)(address, data, nitems, props, dstatus);
139  if (stat == ISISVME::Success)
140  break;
141  }
142  if ( !(dstatus >= current_sev) )
143  {
144  dstatus.resetSeverityToAtLeast(current_sev);
145  }
146  return stat;
147 }
148 
149 int QxtrmInterface::readU16(unsigned long address, isisU16_t* data16, TransferProps props, DAEstatus& dstatus)
150 {
151  return retrySingle(&QxtrmInterface::readU16impl, address, data16, props, dstatus);
152 }
153 
154 int QxtrmInterface::readU16noRetry(unsigned long address, isisU16_t* data16, TransferProps props, DAEstatus& dstatus)
155 {
156  return readU16impl(address, data16, props, dstatus);
157 }
158 
159 template <typename T>
160 int QxtrmInterface::readUimpl(unsigned long address, T* data, int nitems, TransferProps props, DAEstatus& dstatus)
161 {
162  ICPCritical cs(&m_visa_critical); // just for safety - visa should be threadsafe
163  int stat = ISISVME::Success;
164  try
165  {
167  {
168  m_rdma_reg->RDMARead(data, nitems * sizeof(T), address);
169  }
170  else
171  {
172  m_rdma_mem->RDMARead(data, nitems * sizeof(T), address);
173  }
174  }
175  catch(const std::exception& ex)
176  {
177  dstatus.add(FAC_DAE, SEV_ERROR, ERRTYPE_OUTOFMEM, ex.what());
178  stat = ISISVME::Error;
179  }
180  return stat;
181 }
182 
183 int QxtrmInterface::readU16impl(unsigned long address, isisU16_t* data16, TransferProps props, DAEstatus& dstatus)
184 {
185  return readUimpl(address, data16, 1, props, dstatus);
186 }
187 
188 int QxtrmInterface::readU32(unsigned long address, isisU32_t* data32, TransferProps props, DAEstatus& dstatus)
189 {
190  return retrySingle(&QxtrmInterface::readU32impl, address, data32, props, dstatus);
191 }
192 
193 int QxtrmInterface::readU32noRetry(unsigned long address, isisU32_t* data32, TransferProps props, DAEstatus& dstatus)
194 {
195  return readU32impl(address, data32, props, dstatus);
196 }
197 
198 int QxtrmInterface::readU32impl(unsigned long address, isisU32_t* data32, TransferProps props, DAEstatus& dstatus)
199 {
200  return readUimpl(address, data32, 1, props, dstatus);
201 }
202 
203 int QxtrmInterface::readBlockU16(unsigned long address, isisU16_t* data16,
204  long nitems, TransferProps props, DAEstatus& dstatus)
205 {
206  return retryBlock(&QxtrmInterface::readBlockU16impl, address, data16, nitems, props, dstatus);
207 }
208 
209 // nove nitems of 2 bytes
210 // need to check the use of nitems_max in particular is address increment 2*n or 4*n?
211 int QxtrmInterface::readBlockU16impl(unsigned long address, isisU16_t* data16,
212  long nitems, TransferProps props, DAEstatus& dstatus)
213 {
214  return readUimpl(address, data16, nitems, props, dstatus);
215 }
216 
217 int QxtrmInterface::readBlockU32(unsigned long address, isisU32_t* data32,
218  long nitems, TransferProps props, DAEstatus& dstatus)
219 {
220  return retryBlock(&QxtrmInterface::readBlockU32impl, address, data32, nitems, props, dstatus);
221 }
222 
223 // nove nitems of 4 bytes
224 int QxtrmInterface::readBlockU32impl(unsigned long address, isisU32_t* data32, long nitems, TransferProps props, DAEstatus& dstatus)
225 {
226  return readUimpl(address, data32, nitems, props, dstatus);
227 }
228 
229 int QxtrmInterface::writeU16(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus& dstatus)
230 {
231  return retrySingle(&QxtrmInterface::writeU16impl, address, data16, props, dstatus);
232 }
233 
234 template <typename T>
235 int QxtrmInterface::writeUimpl(unsigned long address, T* data, int nitems, TransferProps props, DAEstatus& dstatus)
236 {
237  ICPCritical cs(&m_visa_critical); // just for safety - visa should be threadsafe
238  int stat = ISISVME::Success;
239  try
240  {
242  {
243  m_rdma_reg->RDMAWrite(data, nitems * sizeof(T), address);
244  }
245  else
246  {
247  m_rdma_mem->RDMAWrite(data, nitems * sizeof(T), address);
248  }
249  }
250  catch(const std::exception& ex)
251  {
252  dstatus.add(FAC_DAE, SEV_ERROR, ERRTYPE_OUTOFMEM, ex.what());
253  stat = ISISVME::Error;
254  }
255  return stat;
256 }
257 
258 int QxtrmInterface::writeU16impl(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus& dstatus)
259 {
260  return writeUimpl(address, &data16, 1, props, dstatus);
261 }
262 
263 int QxtrmInterface::writeU32(unsigned long address, isisU32_t data32, TransferProps props, DAEstatus& dstatus)
264 {
265  return retrySingle(&QxtrmInterface::writeU32impl, address, data32, props, dstatus);
266 }
267 
268 int QxtrmInterface::writeU32impl(unsigned long address, isisU32_t data32, TransferProps props, DAEstatus& dstatus)
269 {
270  return writeUimpl(address, &data32, 1, props, dstatus);
271 }
272 
273 
274 int QxtrmInterface::writeBlockU16(unsigned long address, isisU16_t* data16, long nitems, TransferProps props, DAEstatus& dstatus)
275 {
276  return retryBlock(&QxtrmInterface::writeBlockU16impl, address, data16, nitems, props, dstatus);
277 }
278 
279 
280 // nove nitems of 2 bytes
281 // need to check the use of nitems_max in particular is address increment 2*n or 4*n?
282 int QxtrmInterface::writeBlockU16impl(unsigned long address, isisU16_t* data16, long nitems, TransferProps props, DAEstatus& dstatus)
283 {
284  return writeUimpl(address, data16, nitems, props, dstatus);
285 }
286 
287 
288 int QxtrmInterface::writeBlockU32(unsigned long address, isisU32_t* data32, long nitems, TransferProps props, DAEstatus& dstatus)
289 {
290  return retryBlock(&QxtrmInterface::writeBlockU32impl, address, data32, nitems, props, dstatus);
291 }
292 
293 // nove nitems of 4 bytes
294 int QxtrmInterface::writeBlockU32impl(unsigned long address, isisU32_t* data32, long nitems, TransferProps props, DAEstatus& dstatus)
295 {
296  return writeUimpl(address, data32, nitems, props, dstatus);
297 }
298 
299 int QxtrmInterface::writeBlockU64(unsigned long address, uint64_t* data64, long nitems, TransferProps props, DAEstatus& dstatus)
300 {
301  return retryBlock(&QxtrmInterface::writeBlockU64impl, address, data64, nitems, props, dstatus);
302 }
303 
304 int QxtrmInterface::writeBlockU64impl(unsigned long address, uint64_t* data64, long nitems, TransferProps props, DAEstatus& dstatus)
305 {
306  return writeBlockU32impl(address, reinterpret_cast<isisU32_t*>(data64), 2 * nitems, props, dstatus);
307 }
308 
309 int QxtrmInterface::readBlockU64(unsigned long address, uint64_t* data64,
310  long nitems, TransferProps props, DAEstatus& dstatus)
311 {
312  return retryBlock(&QxtrmInterface::readBlockU64impl, address, data64, nitems, props, dstatus);
313 }
314 
315 int QxtrmInterface::readBlockU64impl(unsigned long address, uint64_t* data64, long nitems, TransferProps props, DAEstatus& dstatus)
316 {
317  return readBlockU32impl(address, reinterpret_cast<isisU32_t*>(data64), 2 * nitems, props, dstatus);
318 }
319 
320 int QxtrmInterface::writeBlock(unsigned long address, isisU32_t* data, long nitems, TransferProps props, DAEstatus& status)
321 {
322  return writeBlockU32(address, data, nitems, props, status);
323 }
324 
325 int QxtrmInterface::writeBlock(unsigned long address, uint64_t* data, long nitems, TransferProps props, DAEstatus& status)
326 {
327  return writeBlockU64(address, data, nitems, props, status);
328 }
329 
330 int QxtrmInterface::readBlock(unsigned long address, isisU32_t* data, long nitems, TransferProps props, DAEstatus& status)
331 {
332  return readBlockU32(address, data, nitems, props, status);
333 }
334 
335 int QxtrmInterface::readBlock(unsigned long address, uint64_t* data, long nitems, TransferProps props, DAEstatus& status)
336 {
337  return readBlockU64(address, data, nitems, props, status);
338 }
339 
341 {
343  unlockDevice(dstatus);
344  m_rdma_mem = NULL;
345  m_rdma_reg = NULL;
346  if (m_qxtrm != NULL)
347  {
348  delete m_qxtrm;
349  m_qxtrm = NULL;
350  }
351  m_init_done = false;
352  return ISISVME::Success;
353 }
354 
355 
356 
358 {
359  DAEstatus dstatus;
360  close(dstatus);
361  DeleteCriticalSection(&m_visa_critical);
362 }
363 
364 const int QxtrmInterface::m_tries = 5;
365 const int QxtrmInterface::m_retry_time = 1000;
Poco::SharedPtr< Qxtrm_channel > createChannel(const std::string &chan_name)
int writeBlockU64impl(unsigned long address, uint64_t *data64, long nitems, TransferProps props, DAEstatus &dstatus)
virtual ~QxtrmInterface()
int writeBlock(unsigned long address, isisU32_t *data, long nitems, TransferProps props, DAEstatus &status)
uint32_t TransferProps
combination of TransferProp values
Definition: isisvme.h:16
int writeBlockU16(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &dstatus)
#define FAC_DAE
#define ERRTYPE_OUTOFMEM
int writeU32(unsigned long address, isisU32_t data32, TransferProps props, DAEstatus &dstatus)
int readU16noRetry(unsigned long address, isisU16_t *data16, TransferProps props, DAEstatus &dstatus)
int writeBlockU32(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &dstatus)
#define SEV_WARNING
uint32_t m_nwords_memory
int writeUimpl(unsigned long address, T *data, int nitems, TransferProps props, DAEstatus &dstatus)
int readU16impl(unsigned long address, isisU16_t *data16, TransferProps props, DAEstatus &dstatus)
int writeBlockU16impl(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &dstatus)
int retryBlock(F pFunc, unsigned long address, T data, long nitems, TransferProps props, DAEstatus &dstatus)
unsigned long isisU32_t
Definition: isisvme_types.h:8
int readBlockU32(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &dstatus)
int add(DAEstatus &dstatus, bool clear)
Definition: DAEstatus.cpp:131
int readBlockU64(unsigned long address, uint64_t *data64, long nitems, TransferProps props, DAEstatus &dstatus)
const char * device()
int close(DAEstatus &dstatus)
int retrySingle(F pFunc, unsigned long address, T data, TransferProps props, DAEstatus &dstatus)
Qxtrm_driver * m_qxtrm
int readBlockU64impl(unsigned long address, uint64_t *data64, long nitems, TransferProps props, DAEstatus &dstatus)
#define LOGSTR_WARNING(__arg)
Definition: IsisBase.h:92
int readUimpl(unsigned long address, T *data, int nitems, TransferProps props, DAEstatus &dstatus)
Poco::SharedPtr< Qxtrm_channel > m_rdma_mem
std::ostream & channelTransferStats(std::ostream &os) const
#define SEV_ERROR
std::ostream & channelStatus(std::ostream &os) const
int writeU16impl(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus &dstatus)
QxtrmInterface(DAEstatus &dstatus, uint32_t *dau_memory=NULL, uint32_t nwords_memory=0, uint32_t *dau_registers=NULL, uint32_t nwords_registers=0)
#define LOGSTR_INFORMATION(__arg)
Definition: IsisBase.h:78
static const int m_tries
number of times to try a read/write
static const int m_retry_time
milliseconds between retries
unsigned short isisU16_t
Definition: isisvme_types.h:7
int lockDevice(int timeout, DAEstatus &dstatus)
int resetSeverityToAtLeast(int severity)
Definition: DAEstatus.cpp:184
uint32_t * m_dau_registers
int readBlockU16(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &dstatus)
std::string m_device
Poco::SharedPtr< Qxtrm_channel > m_rdma_reg
CRITICAL_SECTION m_visa_critical
int readU32(unsigned long address, isisU32_t *data32, TransferProps props, DAEstatus &dstatus)
int severity()
Definition: DAEstatus.h:142
int readU32impl(unsigned long address, isisU32_t *data32, TransferProps props, DAEstatus &dstatus)
int writeBlockU32impl(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &dstatus)
void setLoggerName(const std::string &logger_name)
Definition: IsisBase.h:17
int readBlockU16impl(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &dstatus)
int readU16(unsigned long address, isisU16_t *data16, TransferProps props, DAEstatus &dstatus)
uint32_t * m_dau_memory
int unlockDevice(DAEstatus &dstatus)
int readBlock(unsigned long address, isisU32_t *data, long nitems, TransferProps props, DAEstatus &status)
int resetSeverityToAtMost(int severity)
Definition: DAEstatus.cpp:167
int readU32noRetry(unsigned long address, isisU32_t *data32, TransferProps props, DAEstatus &dstatus)
int readBlockU32impl(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &dstatus)
int writeU32impl(unsigned long address, isisU32_t data32, TransferProps propsn, DAEstatus &dstatus)
int writeBlockU64(unsigned long address, uint64_t *data64, long nitems, TransferProps props, DAEstatus &dstatus)
int open(const char *device, DAEstatus &dstatus)
uint32_t m_nwords_registers
int writeU16(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus &dstatus)
int reinit(DAEstatus &dstatus)