Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 7cfeaad

Browse files
author
IgnoreWarnings
committed
update read and write from std
1 parent 32db7d9 commit 7cfeaad

File tree

1 file changed

+83
-135
lines changed

1 file changed

+83
-135
lines changed

src/platform.cpp

Lines changed: 83 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -18,144 +18,92 @@ using namespace fpga;
1818

1919
static auto logger = villas::logging.get("PLATFORM CTRL");
2020

21-
void writeToDmaFromStdIn(std::shared_ptr<villas::fpga::ip::Dma> dma)
22-
{
23-
auto &alloc = villas::HostRam::getAllocator();
24-
const std::shared_ptr<villas::MemoryBlock> block
25-
= alloc.allocateBlock(0x200 * sizeof(float));
26-
villas::MemoryAccessor<float> mem = *block;
27-
dma->makeAccesibleFromVA(block);
28-
29-
logger->info(
30-
"Please enter values to write to the device, separated by ';'");
31-
32-
33-
// Read values from stdin
34-
std::string line;
35-
std::getline(std::cin, line);
36-
auto values = villas::utils::tokenize(line, ";");
37-
38-
size_t i = 0;
39-
for(auto &value : values) {
40-
if(value.empty())
41-
continue;
42-
43-
const float number = std::stof(value);
44-
mem[i++] = number;
45-
}
46-
47-
// Initiate write transfer
48-
bool state = dma->write(*block, i * sizeof(float));
49-
if(!state)
50-
logger->error("Failed to write to device");
51-
52-
auto writeComp = dma->writeComplete();
53-
54-
// logger->debug("Wrote {} bytes", writeComp.bytes);
55-
56-
// auto &alloc = villas::HostRam::getAllocator();
57-
58-
// const std::shared_ptr<villas::MemoryBlock> block[] = {
59-
// alloc.allocateBlock(0x200 * sizeof(uint32_t)),
60-
// alloc.allocateBlock(0x200 * sizeof(uint32_t))
61-
// };
62-
// villas::MemoryAccessor<int32_t> mem[] = {*block[0], *block[1]};
63-
64-
// for (auto b : block) {
65-
// dma->makeAccesibleFromVA(b);
66-
// }
67-
68-
// size_t cur = 0, next = 1;
69-
// std::ios::sync_with_stdio(false);
70-
// std::string line;
71-
// bool firstXfer = true;
72-
73-
// while(true) {
74-
// // Read values from stdin
75-
76-
// std::getline(std::cin, line);
77-
// auto values = villas::utils::tokenize(line, ";");
78-
79-
// size_t i = 0;
80-
// for (auto &value: values) {
81-
// if (value.empty()) continue;
82-
83-
// const float number = std::stof(value);
84-
// mem[cur][i++] = number;
85-
// }
86-
87-
// // Initiate write transfer
88-
// bool state = dma->write(*block[cur], i * sizeof(float));
89-
// if (!state)
90-
// logger->error("Failed to write to device");
91-
92-
// if (!firstXfer) {
93-
// auto bytesWritten = dma->writeComplete();
94-
// logger->debug("Wrote {} bytes", bytesWritten.bytes);
95-
// } else {
96-
// firstXfer = false;
97-
// }
98-
99-
// cur = next;
100-
// next = (next + 1) % (sizeof(mem) / sizeof(mem[0]));
101-
// }
21+
void writeToDmaFromStdIn(std::shared_ptr<villas::fpga::ip::Dma> dma) {
22+
auto &alloc = villas::HostRam::getAllocator();
23+
const std::shared_ptr<villas::MemoryBlock> block =
24+
alloc.allocateBlock(0x200 * sizeof(float));
25+
villas::MemoryAccessor<float> mem = *block;
26+
dma->makeAccesibleFromVA(block);
27+
28+
logger->info("Please enter values to write to the device, separated by ';'");
29+
30+
while (true) {
31+
// Read values from stdin
32+
std::string line;
33+
std::getline(std::cin, line);
34+
auto values = villas::utils::tokenize(line, ";");
35+
36+
size_t i = 0;
37+
for (auto &value : values) {
38+
if (value.empty())
39+
continue;
40+
41+
const float number = std::stof(value);
42+
mem[i++] = number;
43+
}
44+
45+
// Initiate write transfer
46+
bool state = dma->write(*block, i * sizeof(float));
47+
if (!state)
48+
logger->error("Failed to write to device");
49+
50+
auto writeComp = dma->writeComplete();
51+
logger->debug("Wrote {} bytes", writeComp.bytes);
52+
}
10253
}
10354

10455
void readFromDmaToStdOut(
10556
std::shared_ptr<villas::fpga::ip::Dma> dma,
106-
std::unique_ptr<fpga::BufferedSampleFormatter> formatter)
107-
{
108-
auto &alloc = villas::HostRam::getAllocator();
109-
110-
const std::shared_ptr<villas::MemoryBlock> block[]
111-
= { alloc.allocateBlock(0x200 * sizeof(uint32_t)),
112-
alloc.allocateBlock(0x200 * sizeof(uint32_t)) };
113-
villas::MemoryAccessor<int32_t> mem[] = { *block[0], *block[1] };
114-
115-
for(auto b : block) {
116-
dma->makeAccesibleFromVA(b);
117-
}
118-
119-
size_t cur = 0, next = 1;
120-
std::ios::sync_with_stdio(false);
121-
122-
// Setup read transfer
123-
dma->read(*block[0], block[0]->getSize());
124-
125-
while(true) {
126-
logger->trace("Read from stream and write to address {}:{:p}",
127-
block[next]->getAddrSpaceId(),
128-
block[next]->getOffset());
129-
// We could use the number of interrupts to determine if we
130-
// missed a chunk of data
131-
dma->read(*block[next], block[next]->getSize());
132-
auto c = dma->readComplete();
133-
134-
if(c.interrupts > 1) {
135-
logger->warn("Missed {} interrupts", c.interrupts - 1);
136-
}
137-
138-
logger->debug("bytes: {}, intrs: {}, bds: {}",
139-
c.bytes,
140-
c.interrupts,
141-
c.bds);
142-
try {
143-
for(size_t i = 0; i * 4 < c.bytes; i++) {
144-
int32_t ival = mem[cur][i];
145-
float fval = *(
146-
(float *) (&ival)); // cppcheck-suppress
147-
// invalidPointerCast
148-
formatter->format(fval);
149-
printf("%#x\n", ival);
150-
}
151-
formatter->output(std::cout);
152-
} catch(const std::exception &e) {
153-
logger->warn("Failed to output data: {}", e.what());
154-
}
155-
156-
cur = next;
157-
next = (next + 1) % (sizeof(mem) / sizeof(mem[0]));
158-
}
57+
std::unique_ptr<fpga::BufferedSampleFormatter> formatter) {
58+
auto &alloc = villas::HostRam::getAllocator();
59+
60+
const std::shared_ptr<villas::MemoryBlock> block[] = {
61+
alloc.allocateBlock(0x200 * sizeof(uint32_t)),
62+
alloc.allocateBlock(0x200 * sizeof(uint32_t))};
63+
villas::MemoryAccessor<int32_t> mem[] = {*block[0], *block[1]};
64+
65+
for (auto b : block) {
66+
dma->makeAccesibleFromVA(b);
67+
}
68+
69+
size_t cur = 0, next = 1;
70+
std::ios::sync_with_stdio(false);
71+
72+
// Setup read transfer
73+
dma->read(*block[0], block[0]->getSize());
74+
75+
while (true) {
76+
logger->trace("Read from stream and write to address {}:{:p}",
77+
block[next]->getAddrSpaceId(), block[next]->getOffset());
78+
// We could use the number of interrupts to determine if we missed a chunk of data
79+
dma->read(*block[next], block[next]->getSize());
80+
auto c = dma->readComplete();
81+
82+
if (c.interrupts > 1) {
83+
logger->warn("Missed {} interrupts", c.interrupts - 1);
84+
}
85+
86+
logger->debug("bytes: {}, intrs: {}, bds: {}", c.bytes, c.interrupts,
87+
c.bds);
88+
try {
89+
for (size_t i = 0; i * 4 < c.bytes; i++) {
90+
int32_t ival = mem[cur][i];
91+
#pragma GCC diagnostic push
92+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
93+
float fval =
94+
*((float *)(&ival)); // cppcheck-suppress invalidPointerCast
95+
#pragma GCC diagnostic pop
96+
formatter->format(fval);
97+
printf("%#x\n", ival);
98+
}
99+
formatter->output(std::cout);
100+
} catch (const std::exception &e) {
101+
logger->warn("Failed to output data: {}", e.what());
102+
}
103+
104+
cur = next;
105+
next = (next + 1) % (sizeof(mem) / sizeof(mem[0]));
106+
}
159107
}
160108

161109

@@ -284,7 +232,7 @@ int main()
284232
axi_switch->connectInternal("S00_AXIS", "M00_AXIS");
285233

286234
writeTest(dma);
287-
//readTest(dma);
235+
readTest(dma);
288236

289237
return 0;
290238
}

0 commit comments

Comments
 (0)