From e0f686231a7ae0398bf44d2f15f81f5ae673dc9d Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Thu, 2 Sep 2021 15:37:28 +0200 Subject: [PATCH] wip --- .../src/slsDetectorServer_funcs.c | 99 ++++++++++++------- slsDetectorSoftware/src/Module.cpp | 76 ++++++++------ 2 files changed, 105 insertions(+), 70 deletions(-) diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index fdaab4dfb..9abbda4d4 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -3675,7 +3675,6 @@ int program_fpga(int file_des) { n = receiveData(file_des, mess, MAX_STR_LENGTH, OTHER); functionNotImplemented(); #else -#ifndef VIRTUAL // only set if (Server_VerifyLock() == OK) { @@ -3704,8 +3703,9 @@ int program_fpga(int file_des) { char *fpgasrc = malloc(filesize); if (receiveData(file_des, fpgasrc, filesize, OTHER) < 0) return printSocketReadError(); - +#ifndef VIRTUAL ret = eraseAndWriteToFlash(mess, fpgasrc, filesize); +#endif Server_SendResult(file_des, INT32, NULL, 0); // free resources @@ -3714,58 +3714,50 @@ int program_fpga(int file_des) { #else // jungfrau, ctb, moench uint64_t filesize = 0; - uint64_t totalsize = 0; - uint64_t unitprogramsize = 0; char *fpgasrc = NULL; - FILE *fp = NULL; + FILE *fp = NULL; + uint64_t offset = 0; + + // filesize - if (receiveData(file_des, &filesize, sizeof(filesize), INT32) < 0) + if (receiveData(file_des, &filesize, sizeof(filesize), INT64) < 0) return printSocketReadError(); - totalsize = filesize; - LOG(logDEBUG1, ("Total program size is: %lld\n", - (long long unsigned int)totalsize)); + LOG(logDEBUG1, ("Program size is: %lld\n", + (long long unsigned int)filesize)); + fpgasrc = malloc(filesize + 1); - // opening file pointer to flash and telling FPGA to not touch flash - if (startWritingFPGAprogram(&fp) != OK) { - ret = FAIL; - sprintf(mess, "Could not write to flash. Error at startup.\n"); - LOG(logERROR, (mess)); - } - Server_SendResult(file_des, INT32, NULL, 0); - - // erasing flash - if (ret != FAIL) { - eraseFlash(); - fpgasrc = malloc(MAX_FPGAPROGRAMSIZE); - } // writing to flash part by part int clientSocketCrash = 0; - while (ret != FAIL && filesize) { + while (ret != FAIL && offset < filesize) { - unitprogramsize = MAX_FPGAPROGRAMSIZE; // 2mb - if (unitprogramsize > filesize) // less than 2mb - unitprogramsize = filesize; - LOG(logDEBUG1, ("unit size to receive is:%lld\nfilesize:%lld\n", + uint64_t unitprogramsize = MAX_FPGAPROGRAMSIZE; // 2mb + if (offset + unitprogramsize > filesize) // less than 2mb + unitprogramsize = filesize - offset; + LOG(logDEBUG1, ("unit size to receive is:%lld [offset:%lld, filesize:%lld]\n", (long long unsigned int)unitprogramsize, - (long long unsigned int)filesize)); + (long long unsigned int)offset, (long long unsigned int)filesize)); // receive part of program - if (receiveData(file_des, fpgasrc, unitprogramsize, OTHER) < 0) { + if (receiveData(file_des, fpgasrc + offset, unitprogramsize, OTHER) < 0) { printSocketReadError(); clientSocketCrash = 1; ret = FAIL; } // client has not crashed yet, so write to flash and send ret else { - if (!(unitprogramsize - filesize)) { - fpgasrc[unitprogramsize] = '\0'; - filesize -= unitprogramsize; - unitprogramsize++; - } else - filesize -= unitprogramsize; - + offset += unitprogramsize; + Server_SendResult(file_des, INT32, NULL, 0); + + // print progress + LOG(logINFOBLUE, + ("Writing to Flash:%d%%\r", + (int)(((double)(filesize - offset) / filesize) * + 100))); + fflush(stdout); + + /* // write part to flash ret = writeFPGAProgram(fpgasrc, unitprogramsize, fp); Server_SendResult(file_des, INT32, NULL, 0); @@ -3782,8 +3774,38 @@ int program_fpga(int file_des) { 100))); fflush(stdout); } + */ } } + if (ret != FAIL) { + fpgasrc[filesize] = '\0'; + } + ++filesize; + + + + + + + +/* + + // opening file pointer to flash and telling FPGA to not touch flash + if (startWritingFPGAprogram(&fp) != OK) { + ret = FAIL; + sprintf(mess, "Could not write to flash. Error at startup.\n"); + LOG(logERROR, (mess)); + } + Server_SendResult(file_des, INT32, NULL, 0); + + + + + // erasing flash + if (ret != FAIL) { + eraseFlash(); + } + if (ret == OK) { LOG(logINFO, ("Done copying program\n")); @@ -3796,7 +3818,7 @@ int program_fpga(int file_des) { LOG(logERROR, (mess)); } } - +*/ // free resources free(fpgasrc); if (fp != NULL) @@ -3807,6 +3829,8 @@ int program_fpga(int file_des) { Server_SendResult(file_des, INT32, NULL, 0); } + + #endif // end of Blackfin programming if (ret == FAIL) { LOG(logERROR, ("Program FPGA FAIL!\n")); @@ -3814,7 +3838,6 @@ int program_fpga(int file_des) { LOG(logINFOGREEN, ("Programming FPGA completed successfully\n")); } } -#endif #endif return ret; } diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index dc3769cae..2d2782603 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -3417,37 +3417,9 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(F_PROGRAM_FPGA); client.Send(filesize); - // error in detector at opening file pointer to flash - if (client.Receive() == FAIL) { - std::ostringstream os; - os << "Detector " << moduleId << " (" << shm()->hostname << ")" - << " returned error: " << client.readErrorMessage(); - throw RuntimeError(os.str()); - } + printf("%d%%\r", 0); + std::cout << std::flush; - // erasing flash - LOG(logINFO) << "Erasing Flash for detector " << moduleId << " (" - << shm()->hostname << ")"; - printf("%d%%\r", 0); - std::cout << std::flush; - // erasing takes 65 seconds, printing here (otherwise need threads - // in server-unnecessary) - const int ERASE_TIME = 65; - int count = ERASE_TIME + 1; - while (count > 0) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - --count; - printf( - "%d%%\r", - static_cast( - (static_cast(ERASE_TIME - count) / ERASE_TIME) * 100)); - std::cout << std::flush; - } - printf("\n"); - LOG(logINFO) << "Writing to Flash to detector " << moduleId << " (" - << shm()->hostname << ")"; - printf("%d%%\r", 0); - std::cout << std::flush; // sending program in parts of 2mb each uint64_t unitprogramsize = 0; @@ -3481,7 +3453,9 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { } std::cout << '\n'; - // fpga has picked up from flash successfully + +/* + // error in detector at opening file pointer to flash if (client.Receive() == FAIL) { std::ostringstream os; os << "Detector " << moduleId << " (" << shm()->hostname << ")" @@ -3489,8 +3463,46 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { throw RuntimeError(os.str()); } + + + // erasing flash + LOG(logINFO) << "Erasing Flash for detector " << moduleId << " (" + << shm()->hostname << ")"; + printf("%d%%\r", 0); + std::cout << std::flush; + // erasing takes 65 seconds, printing here (otherwise need threads + // in server-unnecessary) + const int ERASE_TIME = 65; + int count = ERASE_TIME + 1; + while (count > 0) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + --count; + printf( + "%d%%\r", + static_cast( + (static_cast(ERASE_TIME - count) / ERASE_TIME) * 100)); + std::cout << std::flush; + } + + // fpga has written to flash successfully + if (client.Receive() == FAIL) { + std::ostringstream os; + os << "Detector " << moduleId << " (" << shm()->hostname << ")" + << " returned error: " << client.readErrorMessage(); + throw RuntimeError(os.str()); + } + + + // fpga has picked up from flash successfully + if (client.Receive() == FAIL) { + std::ostringstream os; + os << "Detector " << moduleId << " (" << shm()->hostname << ")" + << " returned error: " << client.readErrorMessage(); + throw RuntimeError(os.str()); + } +*/ LOG(logINFO) << "FPGA programmed successfully"; - rebootController(); + //rebootController(); } void Module::programFPGAviaNios(std::vector buffer) {