From 87968ab73e0d7d59a16f9b801b032fa477f44849 Mon Sep 17 00:00:00 2001 From: Niklas Eiling Date: Mon, 30 Jan 2023 11:35:21 +0100 Subject: [PATCH] enable reading from stdin to DMA in villas-fpga-ctrl Signed-off-by: Niklas Eiling --- fpga/src/villas-fpga-ctrl.cpp | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/fpga/src/villas-fpga-ctrl.cpp b/fpga/src/villas-fpga-ctrl.cpp index 206d829e2..def5b2e9a 100644 --- a/fpga/src/villas-fpga-ctrl.cpp +++ b/fpga/src/villas-fpga-ctrl.cpp @@ -36,6 +36,55 @@ using namespace villas; static std::shared_ptr pciDevices; static auto logger = villas::logging.get("ctrl"); +void writeToDmaFromStdIn(std::shared_ptr dma) +{ + auto &alloc = villas::HostRam::getAllocator(); + + const std::shared_ptr block[] = { + alloc.allocateBlock(0x200 * sizeof(uint32_t)), + alloc.allocateBlock(0x200 * sizeof(uint32_t)) + }; + villas::MemoryAccessor mem[] = {*block[0], *block[1]}; + + for (auto b : block) { + dma->makeAccesibleFromVA(b); + } + + size_t cur = 0, next = 1; + std::ios::sync_with_stdio(false); + std::string line; + bool firstXfer = true; + + while(true) { + // Read values from stdin + + std::getline(std::cin, line); + auto values = villas::utils::tokenize(line, ";"); + + size_t i = 0; + for (auto &value: values) { + if (value.empty()) continue; + + const float number = std::stof(value); + mem[cur][i++] = number; + } + + // Initiate write transfer + bool state = dma->write(*block[cur], i * sizeof(float)); + if (!state) + logger->error("Failed to write to device"); + + if (!firstXfer) { + auto bytesWritten = dma->writeComplete(); + logger->debug("Wrote {} bytes", bytesWritten.bytes); + } else { + firstXfer = false; + } + + cur = next; + next = (next + 1) % (sizeof(mem) / sizeof(mem[0])); + } +} void readFromDmaToStdOut(std::shared_ptr dma, std::unique_ptr formatter) @@ -160,7 +209,7 @@ int main(int argc, char* argv[]) stdInThread = std::make_unique(readFromDmaToStdOut, dma, std::move(formatter)); } if (!noDma && parsedConnectString.isSrcStdin()) { - + writeToDmaFromStdIn(dma); } if (stdInThread) {