diff --git a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer index b46df8aa2..26c0b2ccc 100755 Binary files a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer and b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer differ diff --git a/slsDetectorServers/slsDetectorServer/include/common.h b/slsDetectorServers/slsDetectorServer/include/common.h index 6d16426f8..fa0d172a9 100644 --- a/slsDetectorServers/slsDetectorServer/include/common.h +++ b/slsDetectorServers/slsDetectorServer/include/common.h @@ -35,5 +35,6 @@ int getModuleIdInFile(int *ret, char *mess, char *fileName); int setModuleIdInFile(char *mess, int arg, char *fileName); int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, ssize_t bytes); -int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname); +// fsize is specified only if it is less than intended size (drive) +int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_t fsize); int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h b/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h index 8a3a9cc65..32cd96254 100644 --- a/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h +++ b/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h @@ -2,6 +2,7 @@ #include #include +#include #define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof" @@ -17,13 +18,13 @@ void resetFPGA(); * open file to copy */ int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess); -int copyToFlash(char *clientChecksum, char *mess); +int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess); int getDrive(char *mess); /** Notify fpga not to touch flash, open src and flash drive to write */ int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess); int eraseFlash(char *mess); /* write from tmp file to flash */ -int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess); +int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess); /** Notify fpga to pick up firmware from flash and wait for status confirmation */ int waitForFPGAtoTouchFlash(char *mess); diff --git a/slsDetectorServers/slsDetectorServer/src/common.c b/slsDetectorServers/slsDetectorServer/src/common.c index 222341474..4f2f7b4db 100644 --- a/slsDetectorServers/slsDetectorServer/src/common.c +++ b/slsDetectorServers/slsDetectorServer/src/common.c @@ -181,6 +181,7 @@ int setModuleIdInFile(char *mess, int arg, char *fileName) { int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, ssize_t bytes) { + LOG(logINFO, ("\tVerifying Checksum...\n")); MD5_CTX c; if (!MD5_Init(&c)) { strcpy(mess, "Unable to calculate checksum (MD5_Init)\n"); @@ -195,7 +196,9 @@ int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, return verifyChecksum(mess, clientChecksum, &c); } -int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { +int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_t fsize) { + LOG(logINFO, ("\tVerifying Checksum...\n")); + FILE *fp = fopen(fname, "r"); if (fp == NULL) { sprintf(mess, "Unable to open %s in read mode to get checksum\n", @@ -211,8 +214,10 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { LOG(logERROR, (mess)); return FAIL; } - char buf[512]; - ssize_t bytes = fread(buf, 1, 512, fp); + const int readUnitSize = 128; + char buf[readUnitSize]; + ssize_t bytes = fread(buf, 1, readUnitSize, fp); + ssize_t totalBytesRead = bytes; while (bytes > 0) { if (!MD5_Update(&c, buf, bytes)) { fclose(fp); @@ -220,8 +225,26 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { LOG(logERROR, (mess)); return FAIL; } - bytes = fread(buf, 1, 512, fp); + // read only until a particular size (drive) + /*if (fsize != 0 && totalBytesRead >= fsize) { + LOG(logINFOBLUE, ("\tReached %lu bytes. Not reading more\n", totalBytesRead)); + break; + }*/ + bytes = fread(buf, 1, readUnitSize, fp); + totalBytesRead += bytes; } + // last null character (if size does not match) + /*if (fsize == totalBytesRead + 1) { + bytes = 1; + totalBytesRead += bytes; + if (!MD5_Update(&c, buf, bytes)) { + fclose(fp); + strcpy(mess, "Unable to calculate checksum (MD5_Update)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + }*/ + LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead)); fclose(fp); return verifyChecksum(mess, clientChecksum, &c); } @@ -255,6 +278,6 @@ int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c) { LOG(logERROR, (mess)); return FAIL; } - LOG(logINFO, ("\tChecksum verified from copied program\n")); + LOG(logINFO, ("\tChecksum verified\n")); return OK; } \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c index 88f976816..ecb3dbfea 100644 --- a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c +++ b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c @@ -9,10 +9,12 @@ #include /* global variables */ +// clang-format off #define MAX_TIME_FPGA_TOUCH_FLASH_US (10 * 1000 * 1000) // 10s -#define CMD_GET_FLASH \ - "awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd" +#define CMD_GET_FLASH "awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd" #define CMD_FPGA_PICKED_STATUS "cat /sys/class/gpio/gpio7/value" +#define FLASH_BUFFER_MEMORY_SIZE (128 * 1024) // 500 KB +// clang-format on #define FLASH_DRIVE_NAME_SIZE 16 char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0}; @@ -107,7 +109,7 @@ int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess) { return OK; } -int copyToFlash(char *clientChecksum, char *mess) { +int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) { if (getDrive(mess) == FAIL) { return FAIL; @@ -115,19 +117,21 @@ int copyToFlash(char *clientChecksum, char *mess) { FILE *flashfd = NULL; FILE *srcfd = NULL; - if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) { + if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) { return FAIL; } if (eraseFlash(mess) == FAIL) { + fclose(flashfd); + fclose(srcfd); return FAIL; } - if (writeToFlash(flashfd, srcfd, mess) == FAIL) { + if (writeToFlash(fsize, flashfd, srcfd, mess) == FAIL) { return FAIL; } - if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName) == FAIL) { + if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName, fsize) == FAIL) { return FAIL; } LOG(logINFO, ("Checksum in Flash verified\n")); @@ -155,7 +159,7 @@ int getDrive(char *mess) { char cmd[MAX_STR_LENGTH] = {0}; char retvals[MAX_STR_LENGTH] = {0}; strcpy(cmd, CMD_GET_FLASH); - if (executeCommand(cmd, retvals, logINFO) == FAIL) { + if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) { strcpy(mess, "Could not program fpga. (could not get flash drive: "); strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1); strcat(mess, "\n"); @@ -205,7 +209,7 @@ int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess) { } int eraseFlash(char *mess) { - LOG(logDEBUG1, ("Erasing Flash\n")); + LOG(logINFO, ("\tErasing Flash...\n")); #ifdef VIRTUAL return OK; @@ -225,10 +229,11 @@ int eraseFlash(char *mess) { return OK; } -int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) { +int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess) { LOG(logDEBUG1, ("writing to flash\n")); - char* buffer = malloc(MAX_FPGAPROGRAMSIZE); + + char* buffer = malloc(FLASH_BUFFER_MEMORY_SIZE); if (buffer == NULL) { fclose(flashfd); fclose(srcfd); @@ -237,13 +242,18 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) { LOG(logERROR, (mess)); return FAIL; } + LOG(logINFO, ("\tWriting to Flash...\n")); + int oldProgress = 0; ssize_t totalBytes = 0; - ssize_t bytes = fread(buffer, sizeof(char), MAX_FPGAPROGRAMSIZE, srcfd); + ssize_t bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd); + while (bytes > 0) { + ssize_t bytesWritten = - fwrite((void *)buffer, sizeof(char), bytes, flashfd); + fwrite((void*)buffer, sizeof(char), bytes, flashfd); totalBytes += bytesWritten; + if (bytesWritten != bytes) { free(buffer); fclose(flashfd); @@ -256,14 +266,32 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) { LOG(logERROR, (mess)); return FAIL; } - bytes = fread(buffer, sizeof(char), bytes, srcfd); - printf("."); + + // print progress + if (fsize > 0) { + int progress = (int)(((double)(totalBytes) / fsize) * 100); + if (oldProgress != progress) { + printf("%d%%\r", progress); + fflush(stdout); + oldProgress = progress; + } + } else printf("."); + + bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd); + } + if (fsize <= 0) { + printf("\n"); } - printf("\n"); free(buffer); fclose(flashfd); fclose(srcfd); LOG(logINFO, ("\tWrote %ld bytes to flash\n", totalBytes)); + + if (totalBytes != fsize) { + sprintf(mess, "Could not program fpga. Incorrect bytes written to flash %lu [expected: %lu]\n", totalBytes, fsize); + LOG(logERROR, (mess)); + return FAIL; + } return OK; } diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 28372f103..0dc781c80 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -14,6 +14,7 @@ #include #include #include +#include // defined in the detector specific Makefile #ifdef GOTTHARDD @@ -3735,7 +3736,9 @@ int program_fpga(int file_des) { src = malloc(MAX_FPGAPROGRAMSIZE); if (src == NULL) { fclose(fd); - strcpy(mess, "Could not allocate memory to get fpga program\n"); + struct sysinfo info; + sysinfo(&info); + sprintf(mess, "Could not allocate memory to get fpga program. Free space: %d MB\n", (int)(info.freeram/ (1024 * 1024))); LOG(logERROR, (mess)); ret = FAIL; } @@ -3792,7 +3795,7 @@ int program_fpga(int file_des) { // checksum of copied program if (ret == OK) { - ret = verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME); + ret = verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME, 0); } Server_SendResult(file_des, INT32, NULL, 0); if (ret == FAIL) { @@ -3801,7 +3804,7 @@ int program_fpga(int file_des) { } // copy to flash - ret = copyToFlash(checksum, mess); + ret = copyToFlash(totalsize, checksum, mess); Server_SendResult(file_des, INT32, NULL, 0); if (ret == FAIL) { LOG(logERROR, ("Program FPGA FAIL!\n")); diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index 7ffda2fa0..5086e077e 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -1241,7 +1241,7 @@ int DetectorImpl::kbhit() { } std::vector DetectorImpl::readProgrammingFile(const std::string &fname) { - // validate type of file +/* // validate type of file bool isPof = false; switch (multi_shm()->multiDetectorType) { case JUNGFRAU: @@ -1363,10 +1363,10 @@ std::vector DetectorImpl::readProgrammingFile(const std::string &fname) { if (close(dst) != 0) { throw RuntimeError("Program FPGA: Could not close destination file"); } - LOG(logDEBUG1) << "File has been converted to " << destfname; - + LOG(logINFOBLUE) << "File has been converted to " << destfname; +*/ // loading dst file to memory - FILE *fp = fopen(destfname, "r"); + FILE *fp = fopen("/tmp/SLS_DET_MCB.ZpfQln", "r");// if (fp == nullptr) { throw RuntimeError("Program FPGA: Could not open rawbin file"); } @@ -1389,7 +1389,7 @@ std::vector DetectorImpl::readProgrammingFile(const std::string &fname) { "Program FPGA: Could not close destination file after converting"); } - unlink(destfname); // delete temporary file + //unlink(destfname); // delete temporary file LOG(logDEBUG1) << "Successfully loaded the rawbin file to program memory"; LOG(logDEBUG1) << "Read file into memory"; return buffer; diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index aeb2f393c..92e6c4c99 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -3474,11 +3474,34 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { << " returned error: " << client.readErrorMessage(); throw RuntimeError(os.str()); } - if (moduleIndex == 0) { - LOG(logINFO) << "Checksum verified"; + LOG(logINFO) << "Checksum verified for module " << moduleIndex << " (" + << shm()->hostname << ")"; + + // erasing flash + { + LOG(logINFO) << "Erasing Flash for module " << moduleIndex << " (" + << 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"); } // copied to flash + LOG(logINFO) << "Writing to Flash to module " << moduleIndex << " (" + << shm()->hostname << ")"; if (client.Receive() == FAIL) { std::ostringstream os; os << "Detector " << moduleIndex << " (" << shm()->hostname << ")" diff --git a/slsSupportLib/include/sls/sls_detector_defs.h b/slsSupportLib/include/sls/sls_detector_defs.h index 85d10288f..2bb69285b 100644 --- a/slsSupportLib/include/sls/sls_detector_defs.h +++ b/slsSupportLib/include/sls/sls_detector_defs.h @@ -54,7 +54,8 @@ #define MAX_TRIMEN 100 /** maximum unit size of program sent to detector */ -#define MAX_FPGAPROGRAMSIZE (2 * 1024 * 1024) +//#define MAX_FPGAPROGRAMSIZE (2 * 1024 * 1024) +#define MAX_FPGAPROGRAMSIZE (128 * 1024) #define GET_FLAG -1 diff --git a/slsSupportLib/include/sls/versionAPI.h b/slsSupportLib/include/sls/versionAPI.h index f2ca9f43a..17d9f8949 100644 --- a/slsSupportLib/include/sls/versionAPI.h +++ b/slsSupportLib/include/sls/versionAPI.h @@ -7,7 +7,7 @@ #define APICTB 0x210909 #define APIGOTTHARD 0x210909 #define APIGOTTHARD2 0x210909 -#define APIJUNGFRAU 0x210909 #define APIMYTHEN3 0x210909 #define APIMOENCH 0x210909 #define APIEIGER 0x210909 +#define APIJUNGFRAU 0x210909