μHAL (v2.6.5)
Part of the IPbus software repository
test_rawclient.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23  Marc Magrans de Abril, CERN
24  email: marc.magrans.de.abril <AT> cern.ch
25 
26  Andrew Rose, Imperial College, London
27  email: awr01 <AT> imperial.ac.uk
28 
29  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
30  email: tom.williams <AT> cern.ch
31 
32 ---------------------------------------------------------------------------
33 */
34 
35 #include "uhal/uhal.hpp"
36 
38 #include "uhal/tests/fixtures.hpp"
39 #include "uhal/tests/tools.hpp"
40 
41 #include <boost/shared_ptr.hpp>
42 #include <boost/test/unit_test.hpp>
43 
44 #include <vector>
45 #include <algorithm>
46 #include <string>
47 #include <iostream>
48 #include <cstdlib>
49 #include <typeinfo>
50 
51 
52 namespace uhal {
53 namespace tests {
54 
55 
56 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(RawClientTestSuite, single_write_read, DummyHardwareFixture,
57 {
58  HwInterface hw = getHwInterface();
59 
60  ClientInterface* c = &hw.getClient();
61  uint32_t x = static_cast<uint32_t> ( rand() );
62  uint32_t addr = hw.getNode ( "REG" ).getAddress();
63  c->write ( addr,x );
64  ValWord< uint32_t > reg = c->read ( addr );
65  BOOST_CHECK ( !reg.valid() );
66  BOOST_CHECK_THROW ( reg.value(),uhal::exception::NonValidatedMemory );
67  c->dispatch();
68  BOOST_CHECK ( reg.valid() );
69  BOOST_CHECK_EQUAL ( reg.value(), x );
70  BOOST_CHECK_THROW ( c->write ( addr,0xF0000000, 0xF0 ) ,uhal::exception::BitsSetWhichAreForbiddenByBitMask );
71  BOOST_CHECK_THROW ( c->write ( addr,0xFF, 0x0F ) ,uhal::exception::BitsSetWhichAreForbiddenByBitMask );
72  uint32_t y = static_cast<uint32_t> ( rand() ) & 0xF;
73  c->write ( addr,y, 0xF );
74  ValWord< uint32_t > reg2 = c->read ( addr );
75  BOOST_CHECK ( !reg2.valid() );
76  BOOST_CHECK_THROW ( reg2.value(),uhal::exception::NonValidatedMemory );
77  c->dispatch();
78  BOOST_CHECK ( reg2.valid() );
79  BOOST_CHECK_EQUAL ( reg2.value(), ( ( x&~0xF ) |y ) );
80  ValWord< uint32_t > reg3 = c->read ( addr , 0xF );
81  BOOST_CHECK ( !reg3.valid() );
82  BOOST_CHECK_THROW ( reg3.value(),uhal::exception::NonValidatedMemory );
83  c->dispatch();
84  BOOST_CHECK ( reg3.valid() );
85  BOOST_CHECK_EQUAL ( reg3.value(), y );
86 }
87 )
88 
89 
91 {
92  const uint32_t N =1024*1024/4;
93  HwInterface hw = getHwInterface();
95  std::vector<uint32_t> xx;
96 
97  for ( size_t i=0; i!= N; ++i )
98  {
99  xx.push_back ( static_cast<uint32_t> ( rand() ) );
100  }
101 
102  uint32_t addr = hw.getNode ( "MEM" ).getAddress();
103  c->writeBlock ( addr, xx );
105  BOOST_CHECK ( !mem.valid() );
106  BOOST_CHECK_EQUAL ( mem.size(), N );
107  BOOST_CHECK_THROW ( mem.at ( 0 ),uhal::exception::NonValidatedMemory );
108  c->dispatch();
109  BOOST_CHECK ( mem.valid() );
110  BOOST_CHECK_EQUAL ( mem.size(), N );
112 
113  std::vector< uint32_t >::const_iterator j=xx.begin();
114  for ( ValVector< uint32_t >::const_iterator i ( mem.begin() ); i!=mem.end(); ++i , ++j )
115  {
116  correct_block_write_read = correct_block_write_read && ( *i == *j );
117  }
118 
119  BOOST_CHECK ( correct_block_write_read );
120 }
121 )
122 
123 
124 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(RawClientTestSuite, mem_rmw_bits, DummyHardwareFixture,
125 {
126  HwInterface hw = getHwInterface();
127  ClientInterface* c = &hw.getClient();
128  uint32_t addr = hw.getNode ( "REG_UPPER_MASK" ).getAddress();
129  uint32_t x1 = static_cast<uint32_t> ( rand() );
130  uint32_t x2 = static_cast<uint32_t> ( rand() );
131  uint32_t x3 = static_cast<uint32_t> ( rand() );
132  c->write ( addr,x1 );
133  ValWord< uint32_t > reg1 = c->rmw_bits ( addr,x2,x3 );
134  ValWord< uint32_t > reg2 = c->read ( addr );
135  c->dispatch();
136  BOOST_CHECK_EQUAL ( ( ( x1 & x2 ) | x3 ), reg2.value() );
137 
138  //IPBus 1.3 bug on RMW: https://svnweb.cern.ch/trac/cactus/ticket/179
139  if ( hw.uri().find ( "ipbusudp-1.3://" ) != std::string::npos ||
140  hw.uri().find ( "ipbustcp-1.3://" ) != std::string::npos ||
141  hw.uri().find ( "chtcp-1.3://" ) != std::string::npos )
142  {
143  BOOST_CHECK_EQUAL ( reg1.value(), ( ( x1 & x2 ) | x3 ) );
144  }
145  else
146  {
147  BOOST_CHECK_EQUAL ( reg1.value(), x1 );
148  }
149 }
150 )
151 
152 
154 {
155  const uint32_t N =1024;
156  HwInterface hw = getHwInterface();
157  ClientInterface* c = &hw.getClient();
158  uint32_t total = 0;
159  std::vector<uint32_t> xx;
160  bool IPbus1_3;
161 
162  //IPBus 1.3 bug on RMW: https://svnweb.cern.ch/trac/cactus/ticket/179
163  if ( hw.uri().find ( "ipbusudp-1.3://" ) != std::string::npos ||
164  hw.uri().find ( "ipbustcp-1.3://" ) != std::string::npos ||
165  hw.uri().find ( "chtcp-1.3://" ) != std::string::npos )
166  {
167  IPbus1_3=true;
168  }
169  else
170  {
171  IPbus1_3=false;
172  }
173 
174  uint32_t x ( 0x00000000 );
175 
176  for ( size_t i=0; i!= N; ++i )
177  {
178  if ( !IPbus1_3 )
179  {
180  total += x;
181  x = static_cast<uint32_t> ( rand() );
182  }
183  else
184  {
185  x = static_cast<uint32_t> ( rand() );
186  total += x;
187  }
188 
189  xx.push_back ( x );
190  }
191 
192  uint32_t addr = hw.getNode ( "SUBSYSTEM1.REG" ).getAddress();
193  c->write ( addr,xx[0] );
195 
196  for ( size_t i=1; i!= N; ++i )
197  {
198  reg = c->rmw_sum ( addr,xx[i] );
199  c->dispatch();
200  }
201 
202  BOOST_CHECK_EQUAL ( reg.value(), total );
203 }
204 )
205 
206 
207 } // end ns tests
208 } // end ns uhal
209 
ValWord< uint32_t > read(const uint32_t &aAddr)
Read a single, unmasked, unsigned word.
void dispatch()
Method to dispatch all IPbus packets which are in the queue of IPbusPacketInfo&#39;s and wait until all q...
T value() const
Return the value of the validated memory with check on validity.
Definition: ValMem.cpp:185
bool correct_block_write_read
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:59
std::vector< uint32_t >::const_iterator j
UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(BlockReadWriteTestSuite, block_write_read, DummyHardwareFixture, { std::vector< size_t > lDepths=getBlockUnitTestDepths();for(size_t i=0;i< lDepths.size();i++) { const size_t N=lDepths.at(i);BOOST_TEST_MESSAGE(" N = "<< N);HwInterface hw=getHwInterface();std::vector< uint32_t > xx;xx.reserve(N);for(size_t i=0;i!=N;++i) { xx.push_back(static_cast< uint32_t >(rand()));} hw.getNode("LARGE_MEM").writeBlock(xx);ValVector< uint32_t > mem=hw.getNode("LARGE_MEM").readBlock(N);BOOST_CHECK(!mem.valid());BOOST_CHECK_EQUAL(mem.size(), N);if(N > 0) { BOOST_CHECK_THROW(mem.at(0), uhal::exception::NonValidatedMemory);} BOOST_CHECK_THROW(mem.value(), uhal::exception::NonValidatedMemory);BOOST_CHECK_NO_THROW(hw.dispatch());BOOST_CHECK(mem.valid());BOOST_CHECK_EQUAL(mem.size(), N);if(N< N_10MB) { bool correct_block_write_read=true;std::vector< uint32_t >::const_iterator j=xx.begin();for(ValVector< uint32_t >::const_iterator i(mem.begin());i!=mem.end();++i,++j) { correct_block_write_read=correct_block_write_read &&(*i== *j);} BOOST_CHECK(correct_block_write_read);} } }) UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(BlockReadWriteTestSuite
const uint32_t & getAddress() const
Return the register address with which this node is associated.
Definition: Node.cpp:261
std::string uri() const
Return the url of the target for this client.
ValWord< uint32_t > rmw_sum(const uint32_t &aAddr, const int32_t &aAddend)
Read the value of a register, add the addend, set the register to this new value and return a copy of...
ValWord< uint32_t > reg
BOOST_CHECK(!mem.valid())
ValHeader write(const uint32_t &aAddr, const uint32_t &aValue)
Write a single, unmasked word to a register.
BOOST_CHECK_THROW(hw.getNode("REG").writeBlockOffset(xx, 0), uhal::exception::BulkTransferOffsetRequestedForSingleRegister)
const_iterator end() const
If the memory has previously been marked as valid, return a const iterator to the end (one past last ...
Definition: ValMem.cpp:389
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:290
HwInterface hw
ClientInterface * c
BOOST_CHECK_EQUAL(hw.getNode("SUBSYSTEM1.SUBMODULE.REG").getAddress(), hw.getNode("SUBSYSTEM1").getNode("SUBMODULE").getNode("REG").getAddress())
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:140
const_iterator begin() const
If the memory has previously been marked as valid, return a const iterator to the beginning of the un...
Definition: ValMem.cpp:374
ValVector< uint32_t > readBlock(const uint32_t &aAddr, const uint32_t &aSize, const defs::BlockReadWriteMode &aMode=defs::INCREMENTAL)
Read a block of unsigned data from a block of registers or a block-read port.
ClientInterface & getClient()
Get the underlying IPbus client.
Definition: HwInterface.cpp:79
std::vector< uint32_t > xx
Definition: test_block.cpp:238
ValHeader writeBlock(const uint32_t &aAddr, const std::vector< uint32_t > &aValues, const defs::BlockReadWriteMode &aMode=defs::INCREMENTAL)
Write a block of data to a block of registers or a block-write port.
std::size_t size() const
Return the size of the underlying memory.
Definition: ValMem.cpp:349
ValVector< uint32_t > mem
An abstract base class for defining the interface to the various IPbus clients as well as providing t...
ValWord< uint32_t > rmw_bits(const uint32_t &aAddr, const uint32_t &aANDterm, const uint32_t &aORterm)
Read the value of a register, apply the AND-term, apply the OR-term, set the register to this new val...
const T & at(std::size_t aIndex) const
If the memory has previously been marked as valid, give random access into memory.
Definition: ValMem.cpp:334
const Node & getNode() const
Ping the target for this client.