7 using namespace boost::unit_test_framework;
9 #define PCIEDEV_TEST_SLOT 0
10 #define LLRFDRV_TEST_SLOT 4
11 #define PCIEUNI_TEST_SLOT 6
20 #include <sys/types.h>
30 #define WORD_FIRMWARE_OFFSET 0x0
31 #define WORD_COMPILATION_OFFSET 0x4
32 #define WORD_USER_OFFSET 0xC
33 #define WORD_CLK_CNT_OFFSET 0x10
34 #define WORD_DUMMY_OFFSET 0x3C
35 #define DMMY_AS_ASCII 0x444D4D59
36 #define WORD_ADC_ENA_OFFSET 0x44
37 #define N_WORDS_DMA 25
38 #define PCIE_DEVICE "PCIE6"
39 #define LLRF_DEVICE "LLRF10"
41 #define PCIE_UNI_DEVICE "PCIE0"
42 #define NON_EXISTING_DEVICE "DUMMY9"
52 std::vector<std::string> usedNodes{
"mtcadummys0",
"llrfdummys4",
"noioctldummys5",
"pcieunidummys6"};
55 mkdir(
"/var/run/lock/mtcadummy",
57 for(
auto& node : usedNodes) {
58 std::string lockfile =
"/var/run/lock/mtcadummy/" + node;
61 int fd = open(lockfile.c_str(), O_WRONLY | O_CREAT, 0777);
63 std::cout <<
"Cannot open file '" << lockfile <<
"' for locking." << std::endl;
68 int res = flock(fd, LOCK_EX);
70 std::cout <<
"Cannot acquire lock on file 'shareddummyTest.dmap'." << std::endl;
77 for(
auto& node : usedNodes) {
78 std::string lockfile =
"/var/run/lock/mtcadummy/" + node;
79 unlink(lockfile.c_str());
96 static BackendFactory& factoryInstance = BackendFactory::getInstance();
106 static void testConstructor();
112 void testFailIfClosed();
115 void testCreateBackend();
124 void testWriteArea();
126 void testReadRegister();
127 void testWriteRegister();
132 void testReadDeviceInfo();
134 void testFailIfBackendClosed();
138 std::string _deviceFileName;
141 boost::shared_ptr<PcieBackend> _pcieBackendInstance;
145 std::string checkDmaValues(std::vector<int32_t>
const& dmaBuffer);
152 PcieBackendTestSuite(std::string
const& deviceFileName,
unsigned int slot) : test_suite(
"PcieBackend test suite") {
155 boost::shared_ptr<PcieBackendTest> pcieBackendTest(
new PcieBackendTest(deviceFileName, slot));
175 createBackendTestCase->depends_on(testConstructor);
176 openTestCase->depends_on(createBackendTestCase);
177 readTestCase->depends_on(openTestCase);
178 writeAreaTestCase->depends_on(readTestCase);
179 readRegisterTestCase->depends_on(writeAreaTestCase);
180 writeRegisterTestCase->depends_on(readRegisterTestCase);
181 readDMATestCase->depends_on(writeRegisterTestCase);
182 writeDMATestCase->depends_on(readDMATestCase);
183 readDeviceInfoTestCase->depends_on(writeDMATestCase);
184 closeTestCase->depends_on(readDeviceInfoTestCase);
186 add(testConstructor);
188 add(createBackendTestCase);
192 add(writeAreaTestCase);
194 add(readRegisterTestCase);
195 add(writeRegisterTestCase);
197 add(readDMATestCase);
198 add(writeDMATestCase);
200 add(readDeviceInfoTestCase);
211 framework::master_test_suite().p_name.value =
"PcieBackend test suite";
213 std::stringstream llrfdummyFileName;
220 std::stringstream mtcadummyFileName;
225 std::stringstream pcieunidummyFileName;
237 std::cout <<
"testConstructor" << std::endl;
239 BOOST_CHECK(pcieBackend.
isOpen() ==
false);
243 : _pcieBackend(deviceFileName), _deviceFileName(deviceFileName), _slot(slot) {}
245 std::string PcieBackendTest::checkDmaValues(std::vector<int32_t>
const& dmaBuffer) {
246 std::cout <<
"testDmaValues" << std::endl;
247 bool dmaValuesOK =
true;
249 for(i = 0; i < dmaBuffer.size(); ++i) {
250 if(dmaBuffer[i] !=
static_cast<int32_t
>(i * i)) {
257 return std::string();
260 std::stringstream errorMessage;
261 errorMessage <<
"Content of transferred DMA block is not valid. First wrong "
263 << i <<
" is " << dmaBuffer[i] << std::endl;
265 return errorMessage.str();
271 std::cout <<
"testReadDeviceInfo" << std::endl;
281 std::stringstream referenceInfo;
282 referenceInfo <<
"SLOT: " << _slot <<
" DRV VER: " << major <<
"." << minor;
284 std::string deviceInfo;
285 deviceInfo = _pcieBackendInstance->readDeviceInfo();
286 BOOST_CHECK_EQUAL(referenceInfo.str(), deviceInfo);
292 std::cout <<
"testReadDMA" << std::endl;
299 std::vector<int32_t> dmaUserBuffer(
N_WORDS_DMA, -1);
301 _pcieBackendInstance->read( 2, 0, &dmaUserBuffer[0],
N_WORDS_DMA *
sizeof(int32_t));
303 std::string errorMessage = checkDmaValues(dmaUserBuffer);
304 BOOST_CHECK_MESSAGE(errorMessage.empty(), errorMessage);
308 std::vector<int32_t> smallBuffer(20, -1);
309 static const unsigned int readOffset = 5;
310 _pcieBackendInstance->read(
311 2, readOffset *
sizeof(int32_t), &smallBuffer[0],
312 smallBuffer.size() *
sizeof(int32_t));
314 for(
size_t i = 0; i < smallBuffer.size(); ++i) {
315 BOOST_CHECK(smallBuffer[i] ==
static_cast<int32_t
>((i + readOffset) * (i + readOffset)));
322 std::cout <<
"testWriteDMA" << std::endl;
328 std::cout <<
"testRead" << std::endl;
335 int32_t firmwareContent;
337 int32_t compilationContent;
342 twoWords[0] = 0xFFFFFFFF;
343 twoWords[1] = 0xFFFFFFFF;
346 BOOST_CHECK((twoWords[0] == firmwareContent) && (twoWords[1] == compilationContent));
360 _pcieBackendInstance->read( 2, 0, &bar2Buffer[0],
N_WORDS_DMA *
sizeof(int32_t));
362 std::string errorMessage = checkDmaValues(bar2Buffer);
363 BOOST_CHECK_MESSAGE(errorMessage.empty(), errorMessage);
369 std::cout <<
"testWriteArea" << std::endl;
374 int32_t originalClockCounts[2];
375 int32_t increasedClockCounts[2];
376 int32_t readbackClockCounts[2];
378 _pcieBackendInstance->read( 0,
WORD_CLK_CNT_OFFSET, originalClockCounts, 2 *
sizeof(int32_t));
379 increasedClockCounts[0] = originalClockCounts[0] + 1;
380 increasedClockCounts[1] = originalClockCounts[1] + 1;
381 _pcieBackendInstance->write( 0,
WORD_CLK_CNT_OFFSET, increasedClockCounts, 2 *
sizeof(int32_t));
382 _pcieBackendInstance->read( 0,
WORD_CLK_CNT_OFFSET, readbackClockCounts, 2 *
sizeof(int32_t));
384 (increasedClockCounts[0] == readbackClockCounts[0]) && (increasedClockCounts[1] == readbackClockCounts[1]));
388 BOOST_CHECK_THROW(_pcieBackendInstance->write( 0,
WORD_CLK_CNT_OFFSET, originalClockCounts,
394 std::vector<int32_t> writeBuffer(
N_WORDS_DMA, 0xABCDEF01);
395 std::vector<int32_t> readbackBuffer(
N_WORDS_DMA, -1);
396 _pcieBackendInstance->write( 2, 0, &writeBuffer[0],
N_WORDS_DMA *
sizeof(int32_t));
397 _pcieBackendInstance->read( 2, 0, &readbackBuffer[0],
N_WORDS_DMA *
sizeof(int32_t));
398 BOOST_CHECK(readbackBuffer == writeBuffer);
404 std::cout <<
"testReadRegister" << std::endl;
409 int32_t dataWord = 0;
411 _pcieBackendInstance->open();
421 BOOST_CHECK_THROW(_pcieBackendInstance->getRegisterAccessor<
int>(
"#6/0x3C", 4, 0, {}),
ChimeraTK::logic_error);
427 std::cout <<
"testWriteRegister" << std::endl;
434 int32_t originalUserWord, newUserWord;
436 int32_t data = originalUserWord + 1;
440 BOOST_CHECK_EQUAL(originalUserWord + 1, newUserWord);
446 std::cout <<
"testClose" << std::endl;
448 _pcieBackendInstance->close();
450 BOOST_CHECK(_pcieBackendInstance->isOpen() ==
false);
452 _pcieBackendInstance->close();
453 BOOST_CHECK(_pcieBackendInstance->isOpen() ==
false);
459 std::cout <<
"testOpen" << std::endl;
460 _pcieBackendInstance->open();
461 BOOST_CHECK(_pcieBackendInstance->isOpen() ==
true);
463 _pcieBackendInstance->open();
464 BOOST_CHECK(_pcieBackendInstance->isOpen());
470 std::cout <<
"testCreateBackend" << std::endl;
474 _pcieBackendInstance = boost::dynamic_pointer_cast<PcieBackend>(factoryInstance.
createBackend(_deviceFileName));
475 BOOST_CHECK(_pcieBackendInstance !=
nullptr);
477 BOOST_CHECK(_pcieBackendInstance->isOpen() ==
false);
493 firstDevice.
open(
"PCIE0");
495 firstDevice.
write<
double>(
"WORD_USER", 48);
502 secondDevice.
open(
"(pci:pcieunidummys6?map=mtcadummy.map)");
503 BOOST_CHECK(secondDevice.
read<
double>(
"BOARD/WORD_USER") == 48);
507 secondDevice2.
open(
"(pci:pcieunidummys6?map=mtcadummy.map)");
508 BOOST_CHECK(secondDevice2.
read<
double>(
"BOARD/WORD_USER") == 48);
512 thirdDevice.
open(
"(pci:pcieunidummys6)");
513 BOOST_CHECK(thirdDevice.
read<int32_t>(
BAR() / 0 / 0xC) == 48 << 3);
520 fourthDevice.
open(
"PCIE_DOUBLEMAP");
521 BOOST_CHECK(fourthDevice.
read<
double>(
"BOARD/WORD_USER") == 48);
525 _pcieBackendInstance->close();