Skip to content

Commit a4e958a

Browse files
committed
[example] Update library example
1 parent d38cb3e commit a4e958a

File tree

1 file changed

+18
-76
lines changed

1 file changed

+18
-76
lines changed

src/Example.cxx

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,32 @@
99
// or submit itself to any jurisdiction.
1010

1111
/// \file Example.cxx
12-
/// \brief Example of pushing pages with the ReadoutCard C++ interface
12+
/// \brief Example usage of the BAR interface
1313
///
14-
/// \author Pascal Boeschoten (pascal.boeschoten@cern.ch)
14+
/// \author Kostas Alexopoulos (kostas.alexopoulos@cern.ch)
1515

1616
#include <iostream>
17-
#include <chrono>
18-
#include <thread>
19-
#include <boost/exception/diagnostic_information.hpp>
2017
#include "ReadoutCard/ChannelFactory.h"
21-
#include "ReadoutCard/Exception.h"
22-
#include "ReadoutCard/MemoryMappedFile.h"
2318

24-
using std::cout;
25-
using std::endl;
2619
using namespace o2;
2720

28-
int main(int, char**)
21+
int main()
2922
{
30-
try {
31-
32-
// Get the DMA channel object
33-
cout << "\n### Acquiring DMA channel object" << endl;
34-
35-
// Create a 10MiB file in 2MiB hugepage filesystem
36-
constexpr size_t superpageSize = 2 * 1024 * 1024;
37-
constexpr size_t superpageCount = 5;
38-
constexpr size_t bufferSize = superpageCount * superpageSize;
39-
roc::MemoryMappedFile file{ "/dev/hugepages/rorc_example.bin", bufferSize };
40-
41-
// Create parameters object for channel
42-
auto parameters = roc::Parameters()
43-
.setCardId(roc::SerialId{ -1, 0 }) // Dummy card
44-
.setChannelNumber(0) // DMA channel 0
45-
.setBufferParameters(roc::buffer_parameters::Memory{ file.getAddress(), bufferSize }); // Register our buffer
46-
47-
// Get the DMA channel
48-
std::shared_ptr<roc::DmaChannelInterface> channel = roc::ChannelFactory().getDmaChannel(parameters);
49-
50-
// Start the DMA
51-
cout << "\n### Starting DMA" << endl;
52-
channel->startDma();
53-
54-
// Keep track of time, so we don't wait forever for pages to arrive if things break
55-
const auto start = std::chrono::steady_clock::now();
56-
auto timeExceeded = [&]() { return ((std::chrono::steady_clock::now() - start) > std::chrono::seconds(5)); };
57-
58-
cout << "### Pushing pages" << endl;
59-
60-
// Queue up some superpages
61-
for (size_t i = 0; i < superpageCount; ++i) {
62-
auto offset = i * superpageSize;
63-
auto size = superpageSize;
64-
channel->pushSuperpage(roc::Superpage(offset, size));
65-
cout << "Pushed superpage " << i << '\n';
66-
}
67-
68-
while (true) {
69-
if (timeExceeded()) {
70-
cout << "Time was exceeded!\n";
71-
break;
72-
}
73-
if (channel->getReadyQueueSize() == 0) {
74-
cout << "Done!\n";
75-
break;
76-
}
77-
78-
// Does internal driver business, filling up superpages
79-
channel->fillSuperpages();
80-
81-
// Get superpage at front of queue
82-
auto superpage = channel->getSuperpage();
83-
if (superpage.isReady()) {
84-
channel->popSuperpage();
85-
cout << "Superpage " << (superpage.getOffset() / superpageSize) << " arrived\n";
86-
}
87-
88-
// Give the CPU some resting time
89-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
90-
}
91-
} catch (const std::exception& e) {
92-
// Most exceptions thrown from the library inherit from boost::exception and will contain information besides
93-
// the "what()" message that can help diagnose the problem. Here we print this information for the user.
94-
cout << boost::diagnostic_information(e) << endl;
23+
// See https://github.com/AliceO2Group/ReadoutCard#addressing for other addressing options
24+
auto parameters = roc::Parameters()
25+
.setCardId(roc::PciSequenceNumber{ "#1" })
26+
.setChannelNumber(2); // BAR numbre, Should always be 2
27+
auto bar = o2::roc::ChannelFactory().getBar(parameters);
28+
29+
uint32_t address = 0x00260004;
30+
31+
bar->writeRegister(address / 4, 0x42);
32+
auto reg = bar->readRegister(address / 4);
33+
if (reg == 0x42) {
34+
std::cout << "SUCCESS" << std::endl;
35+
return 0;
9536
}
9637

97-
return 0;
38+
std::cout << "FAILURE" << std::endl;
39+
return 1;
9840
}

0 commit comments

Comments
 (0)