Line data Source code
1 : /**
2 :
3 : Low-level ipbus-over-axi4lite test
4 :
5 : Small test application to test low-level ipbus communication over axi4 lite using the
6 : uhal::Axi4Lite::MappedFile and manually injecting read and write ipbus packets.
7 :
8 : **/
9 : #include <fcntl.h>
10 : #include <sys/file.h>
11 : #include <sys/mman.h>
12 : #include <unistd.h>
13 :
14 : #include <cstdint>
15 : #include <cstring>
16 : #include <iomanip>
17 : #include <iostream>
18 : #include <string>
19 : #include <vector>
20 :
21 : #include "uhal/ClientInterface.hpp"
22 : #include "uhal/ProtocolIPbus.hpp"
23 : #include "uhal/log/exception.hpp"
24 : #include "uhal/log/log.hpp"
25 :
26 :
27 : #include "uhallibs/ProtocolAxi4Lite.hpp"
28 :
29 0 : int main(int /* argc */, char const* /* argv[] */) {
30 : /* code */
31 :
32 0 : std::string lBarFile = "/sys/bus/pci/devices/0000:41:00.0/resource0";
33 :
34 0 : std::cout << "Starting ipbus-axi4lite test on " << lBarFile << std::endl;
35 :
36 0 : uint32_t n_pages;
37 0 : uint32_t page_size;
38 0 : uint32_t next_write_index;
39 0 : uint32_t read_counts;
40 :
41 0 : uhallibs::Axi4Lite::MappedFile f(lBarFile, 0x40, PROT_WRITE);
42 :
43 0 : std::vector<uint32_t> lStats;
44 0 : f.open();
45 0 : f.lock();
46 :
47 : // Inject an ipbus read transation
48 : // Read the the status of the transport interface at address 0x0
49 0 : std::cout << "--- Stats ---" << std::endl;
50 0 : lStats.clear();
51 0 : f.read(0, 4, lStats);
52 :
53 0 : for (uint64_t i(0); i < lStats.size(); ++i) {
54 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i
55 0 : << " " << std::setw(8) << std::setfill('0') << lStats[i]
56 0 : << std::endl;
57 : }
58 :
59 0 : n_pages = lStats[0];
60 0 : page_size = lStats[1];
61 0 : next_write_index = lStats[2];
62 0 : read_counts = lStats[3];
63 :
64 0 : std::cout << "n_pages " << n_pages << std::endl;
65 0 : std::cout << "page_size " << page_size << std::endl;
66 0 : std::cout << "next_write_index " << next_write_index << std::endl;
67 0 : std::cout << "read_counts " << read_counts << std::endl;
68 0 : std::cout << "-------------" << std::endl;
69 0 : f.unlock();
70 0 : f.close();
71 :
72 : // Re-open the interface file
73 0 : uint32_t map_size = n_pages*page_size+4;
74 0 : f.setLength(map_size);
75 0 : f.open();
76 0 : f.lock();
77 :
78 0 : uint64_t write_base = next_write_index * page_size;
79 0 : std::cout << "write base : 0x" << std::hex << std::setw(8)
80 0 : << std::setfill('0') << write_base << std::endl;
81 :
82 0 : std::vector<uint32_t> lWriteVal = {
83 : 0x00010002,
84 : 0x200001F0,
85 : 0x2001010F,
86 0 : 0x00000001};
87 :
88 0 : for (uint64_t i(0); i < lWriteVal.size(); ++i) {
89 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i+write_base
90 0 : << " " << std::setw(8) << std::setfill('0') << lWriteVal[i]
91 0 : << std::endl;
92 : }
93 :
94 0 : f.write(write_base, lWriteVal);
95 :
96 : // Read back
97 0 : std::cout << "--- Stats ---" << std::endl;
98 0 : lStats.clear();
99 0 : f.read(0, 4, lStats);
100 :
101 0 : for (uint64_t i(0); i < lStats.size(); ++i) {
102 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i
103 0 : << " " << std::setw(8) << std::setfill('0') << lStats[i]
104 0 : << std::endl;
105 : }
106 :
107 0 : n_pages = lStats[0];
108 0 : page_size = lStats[1];
109 0 : next_write_index = lStats[2];
110 0 : read_counts = lStats[3];
111 :
112 0 : std::cout << "n_pages " << n_pages << std::endl;
113 0 : std::cout << "page_size " << page_size << std::endl;
114 0 : std::cout << "next_write_index " << next_write_index << std::endl;
115 0 : std::cout << "read_counts " << read_counts << std::endl;
116 0 : std::cout << "-------------" << std::endl;
117 :
118 0 : const size_t next_read_index = (next_write_index - 1 + n_pages) % n_pages;
119 0 : const size_t read_base = 4 + (next_read_index * page_size);
120 0 : std::cout << "read base : 0x" << std::hex << std::setw(8) << std::setfill('0')
121 0 : << read_base << std::endl;
122 :
123 0 : std::vector<uint32_t> lReadVal;
124 0 : f.read(read_base, 8, lReadVal);
125 :
126 0 : for (uint64_t i(0); i < lReadVal.size(); ++i) {
127 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i+read_base
128 0 : << " " << std::setw(8) << std::setfill('0') << lReadVal[i]
129 0 : << std::endl;
130 : }
131 :
132 0 : f.unlock();
133 0 : f.close();
134 :
135 0 : return 0;
136 0 : }
|