From 7847658548b71017bdfac09cb42a3391113a1767 Mon Sep 17 00:00:00 2001 From: Niklas Eiling Date: Mon, 20 Mar 2023 15:35:38 +0100 Subject: [PATCH] fix output formatting not being able to print numbers larger than 9 Signed-off-by: Niklas Eiling --- fpga/include/villas/fpga/utils.hpp | 14 ++-- fpga/lib/ips/dma.cpp | 2 +- fpga/src/villas-fpga-ctrl.cpp | 101 +++++++++++++++++++---------- 3 files changed, 76 insertions(+), 41 deletions(-) diff --git a/fpga/include/villas/fpga/utils.hpp b/fpga/include/villas/fpga/utils.hpp index a6e4a3139..5e93465cf 100644 --- a/fpga/include/villas/fpga/utils.hpp +++ b/fpga/include/villas/fpga/utils.hpp @@ -86,14 +86,16 @@ public: virtual void format(float value) override { - if (std::snprintf(nextBufPos(), formatStringSize+1, formatString, value) > (int)formatStringSize) { - throw RuntimeError("Output buffer too small"); + size_t chars; + if ((chars = std::snprintf(nextBufPos(), formatStringSize+1, formatString, value)) > (int)formatStringSize) { + throw RuntimeError("Output buffer too small. Expected " + std::to_string(formatStringSize) + + " characters, got " + std::to_string(chars)); } } protected: - static constexpr char formatString[] = "%7f\n"; - static constexpr size_t formatStringSize = 9; + static constexpr char formatString[] = "%013.6f\n"; + static constexpr size_t formatStringSize = 14; }; class BufferedSampleFormatterLong : public BufferedSampleFormatter { @@ -111,8 +113,8 @@ public: } protected: - static constexpr char formatString[] = "%05zd: %7f\n"; - static constexpr size_t formatStringSize = 16; + static constexpr char formatString[] = "%05zd: %013.6f\n"; + static constexpr size_t formatStringSize = 22; size_t sampleCnt; }; diff --git a/fpga/lib/ips/dma.cpp b/fpga/lib/ips/dma.cpp index 378145f47..b2a6600ed 100644 --- a/fpga/lib/ips/dma.cpp +++ b/fpga/lib/ips/dma.cpp @@ -243,7 +243,7 @@ bool Dma::write(const MemoryBlock &mem, size_t len) if (buf == nullptr) throw RuntimeError("Buffer was null"); - logger->debug("Write to stream from address {:p}", buf); + logger->trace("Write to stream from address {:p}", buf); return hasScatterGather() ? writeScatterGather(buf, len) : writeSimple(buf, len); } diff --git a/fpga/src/villas-fpga-ctrl.cpp b/fpga/src/villas-fpga-ctrl.cpp index def5b2e9a..c88a7c32d 100644 --- a/fpga/src/villas-fpga-ctrl.cpp +++ b/fpga/src/villas-fpga-ctrl.cpp @@ -39,25 +39,15 @@ 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(float)); + villas::MemoryAccessor mem = *block; + dma->makeAccesibleFromVA(block); - const std::shared_ptr block[] = { - alloc.allocateBlock(0x200 * sizeof(uint32_t)), - alloc.allocateBlock(0x200 * sizeof(uint32_t)) - }; - villas::MemoryAccessor mem[] = {*block[0], *block[1]}; + logger->info("Please enter values to write to the device, separated by ';'"); - 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) { + while (true) { // Read values from stdin - + std::string line; std::getline(std::cin, line); auto values = villas::utils::tokenize(line, ";"); @@ -66,24 +56,63 @@ void writeToDmaFromStdIn(std::shared_ptr dma) if (value.empty()) continue; const float number = std::stof(value); - mem[cur][i++] = number; + mem[i++] = number; } // Initiate write transfer - bool state = dma->write(*block[cur], i * sizeof(float)); + bool state = dma->write(*block, 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])); + auto writeComp = dma->writeComplete(); + logger->debug("Wrote {} bytes", writeComp.bytes); } + // 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, @@ -117,15 +146,19 @@ void readFromDmaToStdOut(std::shared_ptr dma, logger->warn("Missed {} interrupts", c.interrupts - 1); } - logger->trace("bytes: {}, intrs: {}, bds: {}", + logger->debug("bytes: {}, intrs: {}, bds: {}", c.bytes, c.interrupts, c.bds); - - for (size_t i = 0; i*4 < c.bytes; i++) { - int32_t ival = mem[cur][i]; - float fval = *((float*)(&ival)); // cppcheck-suppress invalidPointerCast - formatter->format(fval); + try { + for (size_t i = 0; i*4 < c.bytes; i++) { + int32_t ival = mem[cur][i]; + float fval = *((float*)(&ival)); // cppcheck-suppress invalidPointerCast + formatter->format(fval); + printf("%#x\n", ival); + } + formatter->output(std::cout); + } catch (const std::exception &e) { + logger->warn("Failed to output data: {}", e.what()); } - formatter->output(std::cout); cur = next; next = (next + 1) % (sizeof(mem) / sizeof(mem[0]));