From 82df51d4f51f5ac5ed3e7d9da5f56e2b1812f3d5 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 16 Jan 2021 11:21:22 -0800 Subject: [PATCH] Renamed file fx2.XXX to fpgalink.XXX Created a base class FpgaLink to encapsulate the general fpgalink functionality (could be used to create a avr cable) Converted the printf calls to printError and printInfo calls. --- CMakeLists.txt | 6 +- src/{fx2.cpp => fpgalink.cpp} | 148 +++++++++++++++++++--------------- src/{fx2.hpp => fpgalink.hpp} | 22 +++-- src/jtag.cpp | 2 +- 4 files changed, 100 insertions(+), 78 deletions(-) rename src/{fx2.cpp => fpgalink.cpp} (79%) rename src/{fx2.hpp => fpgalink.hpp} (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71b7f36..32fe788 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,7 +146,7 @@ target_link_libraries(openFPGALoader if (ENABLE_FPGALINK) find_library(LIBFPGALINK libfpgalink) if (${LIBFPGALINK} STREQUAL "LIBFPGALINK-NOTFOUND") - message("libfpgalink not found, disabling fx2 support") + message("libfpgalink not found, disabling fpgalink support") set(ENABLE_FPGALINK OFF) else() message("libfpgalink found: ${LIBFPGALINK}") @@ -155,8 +155,8 @@ if (ENABLE_FPGALINK) target_include_directories(openFPGALoader PRIVATE ${INC_DIR}) target_link_libraries(openFPGALoader ${LIBFPGALINK}) target_sources(openFPGALoader PRIVATE - src/fx2.cpp - src/fx2.hpp) + src/fpgalink.cpp + src/fpgalink.hpp) endif() endif() diff --git a/src/fx2.cpp b/src/fpgalink.cpp similarity index 79% rename from src/fx2.cpp rename to src/fpgalink.cpp index 3077514..185a0f8 100644 --- a/src/fx2.cpp +++ b/src/fpgalink.cpp @@ -23,8 +23,10 @@ #include #include #include +#include -#include "FX2.hpp" +#include "display.hpp" +#include "fpgalink.hpp" #include "makestuff/libfpgalink.h" @@ -36,7 +38,8 @@ using namespace std; #define CHECK_STATUS(fStatus)\ if (fStatus) \ { \ - cerr << error << endl;\ + printError("Error: ", false);\ + printError(error);\ flFreeError(error);\ fStatus = progClose(handle, &error);\ if (error)\ @@ -46,9 +49,73 @@ using namespace std; throw std::exception();\ } +FpgaLink::FpgaLink(bool verbose): + _verbose(verbose) +{ + +} + + +int FpgaLink::writeTMS(uint8_t *tms, int len, bool flush_buffer) +{ + (void)flush_buffer; + FLStatus fStatus; + const char *error = NULL; + uint32_t bitPattern = 0; + int ulen = len; + int i = 0; + while (ulen > 0) + { + {bitPattern = tms[i++];} + ulen-=8; + if (ulen > 0) {bitPattern |= tms[i++] << 8;} + ulen-=8; + if (ulen > 0) {bitPattern |= tms[i++] << 16;} + ulen-=8; + if (ulen > 0) {bitPattern |= tms[i++] << 24;} + ulen-=8; + fStatus = jtagClockFSM(handle, bitPattern, (ulen > 0) ? 32 : 32 + ulen, &error); + CHECK_STATUS(fStatus); + } + + return len; +} + +int FpgaLink::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) +{ + (void)tms; + (void)tdi; + FLStatus fStatus; + const char *error = NULL; + fStatus = jtagClocks(handle, clk_len, &error); + CHECK_STATUS(fStatus); + return EXIT_SUCCESS; +} + +int FpgaLink::flush() +{ + return 0; +} + +int FpgaLink::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) +{ + FLStatus fStatus; + const char *error = NULL; + if (rx) + fStatus = jtagShiftInOut(handle, len, tx, rx, end, &error); + else + { + fStatus = jtagShiftInOnly(handle, len, tx, end, &error); + } + + CHECK_STATUS(fStatus); + + return EXIT_SUCCESS; +} + FX2Cable::FX2Cable(bool verbose, const jtag_pins_conf_t *pin_conf, const char *ivp, const char *vp): - _verbose(verbose) + FpgaLink::FpgaLink(verbose) { FLStatus fStatus; const char *error = NULL; @@ -57,22 +124,24 @@ FX2Cable::FX2Cable(bool verbose, const jtag_pins_conf_t *pin_conf, const char *i char portConfig[10]; vp = "1D50:602B"; + std::string svp(vp); - printf("Attempting to open connection to FPGALink device %s...\n", vp); + printInfo("Attempting to open connection to FPGALink device " + svp +"...", false); fStatus = flOpen(vp, &handle, NULL); if ( fStatus ) { { int count = 60; uint8 flag; ivp = "04b4:8613"; - printf("Loading firmware into %s...\n", ivp); + std::string sivp(ivp); + printInfo("Loading firmware into " + sivp, true); fStatus = flLoadStandardFirmware(ivp, vp, &error); CHECK_STATUS(fStatus); - printf("Awaiting renumeration"); + printInfo("Awaiting renumeration"); flSleep(1000); do { - printf("."); + printInfo(".", false); fflush(stdout); fStatus = flIsDeviceAvailable(vp, &flag, &error); CHECK_STATUS(fStatus); @@ -81,17 +150,20 @@ FX2Cable::FX2Cable(bool verbose, const jtag_pins_conf_t *pin_conf, const char *i } while ( !flag && count ); printf("\n"); if ( !flag ) { - fprintf(stderr, "FPGALink device did not renumerate properly as %s\n", vp); + printError("Error: FPGALink device did not renumerate properly as " + svp, true); flClose(handle); handle = NULL; throw std::exception(); } - printf("Attempting to open connection to FPGLink device %s again...\n", vp); + printInfo("Attempting to open connection to FPGLink device " + svp + " again...", false); fStatus = flOpen(vp, &handle, &error); CHECK_STATUS(fStatus); + printInfo("success", true); } - } + } else { + printInfo("success", true); + } transcode_pin_config(pin_conf, portConfig); fStatus = progOpen(handle, portConfig, &error); @@ -117,59 +189,3 @@ FX2Cable::~FX2Cable() } } -int FX2Cable::writeTMS(uint8_t *tms, int len, bool flush_buffer) -{ - (void)flush_buffer; - FLStatus fStatus; - const char *error = NULL; - uint32_t bitPattern = 0; - int ulen = len; - int i = 0; - while (ulen > 0) - { - {bitPattern = tms[i++];} - ulen-=8; - if (ulen > 0) {bitPattern |= tms[i++] << 8;} - ulen-=8; - if (ulen > 0) {bitPattern |= tms[i++] << 16;} - ulen-=8; - if (ulen > 0) {bitPattern |= tms[i++] << 24;} - ulen-=8; - fStatus = jtagClockFSM(handle, bitPattern, (ulen > 0) ? 32 : 32 + ulen, &error); - CHECK_STATUS(fStatus); - } - - return len; -} - -int FX2Cable::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) -{ - (void)tms; - (void)tdi; - FLStatus fStatus; - const char *error = NULL; - fStatus = jtagClocks(handle, clk_len, &error); - CHECK_STATUS(fStatus); - return EXIT_SUCCESS; -} - -int FX2Cable::flush() -{ - return 0; -} - -int FX2Cable::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) -{ - FLStatus fStatus; - const char *error = NULL; - if (rx) - fStatus = jtagShiftInOut(handle, len, tx, rx, end, &error); - else - { - fStatus = jtagShiftInOnly(handle, len, tx, end, &error); - } - - CHECK_STATUS(fStatus); - - return EXIT_SUCCESS; -} diff --git a/src/fx2.hpp b/src/fpgalink.hpp similarity index 94% rename from src/fx2.hpp rename to src/fpgalink.hpp index 11f8dae..47a25f4 100644 --- a/src/fx2.hpp +++ b/src/fpgalink.hpp @@ -30,11 +30,10 @@ * \brief concrete class between jtag implementation and FX2 cable */ -class FX2Cable : public JtagInterface { - public: - FX2Cable(bool verbose, const jtag_pins_conf_t *pin_conf, const char *ivp = NULL, const char *vp = NULL); - virtual ~FX2Cable(); +class FpgaLink : public JtagInterface{ +public: + FpgaLink(bool verbose); int setClkFreq(uint32_t clkHZ) override{(void)clkHZ; return 0;} int setClkFreq(uint32_t clkHZ, char use_divide_by_5) override {(void)clkHZ; (void)use_divide_by_5;return 0;} @@ -56,11 +55,18 @@ class FX2Cable : public JtagInterface { int flush() override; - private: + protected: bool _verbose; - - int write(uint8_t *in_buf, uint8_t *out_buf, int len, int rd_len); - void transcode_pin_config(const jtag_pins_conf_t *pin_conf, char* buffer); struct FLContext *handle = NULL; }; + + +class FX2Cable : public FpgaLink { + public: + FX2Cable(bool verbose, const jtag_pins_conf_t *pin_conf, const char *ivp = NULL, const char *vp = NULL); + virtual ~FX2Cable(); + + private: + void transcode_pin_config(const jtag_pins_conf_t *pin_conf, char* buffer); +}; #endif // SRC_FX2CABLE_HPP_ diff --git a/src/jtag.cpp b/src/jtag.cpp index a307ff9..d539dac 100644 --- a/src/jtag.cpp +++ b/src/jtag.cpp @@ -34,7 +34,7 @@ #include "usbBlaster.hpp" #ifdef USE_LIBFPGALINK -#include "fx2.hpp" +#include "fpgalink.hpp" #endif using namespace std;