This commit is contained in:
maliakal_d 2021-09-03 13:17:27 +02:00
parent 9c9f2d5c70
commit 20deeb8dcc
4 changed files with 112 additions and 139 deletions

View File

@ -45,11 +45,6 @@ int startWritingFPGAprogram(FILE **filefp);
*/
int stopWritingFPGAprogram(FILE *filefp);
/**
* Write FPGA Program to flash
* @param fpgasrc source program
* @param fsize size of program
* @param filefp pointer to flash
* @return 0 for success, 1 for fail (cannot write)
*/
int writeFPGAProgram(char *fpgasrc, uint64_t fsize, FILE *filefp);
int startCopyingFPGAProgram(FILE **fd, uint64_t fsize, char *mess);
int writeFPGAProgram(uint64_t fsize, FILE *fd, char *src, char *msg,
char *mess);

View File

@ -5,14 +5,19 @@
#include <string.h>
#include <unistd.h> // usleep
#include <sys/sysinfo.h>
/* global variables */
#define MTDSIZE 10
#define MAX_TIME_FPGA_TOUCH_FLASH_US (10 * 1000 * 1000) // 10s
#define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof"
int gpioDefined = 0;
char mtdvalue[MTDSIZE] = {0};
extern int executeCommand(char *command, char *result, enum TLogLevel level);
void defineGPIOpins() {
if (!gpioDefined) {
// define the gpio pins
@ -70,11 +75,11 @@ int startWritingFPGAprogram(FILE **filefp) {
"awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd", "r");
if (fp == NULL) {
LOG(logERROR, ("popen returned NULL. Need that to get mtd drive.\n"));
return 1;
return FAIL;
}
if (fgets(output, sizeof(output), fp) == NULL) {
LOG(logERROR, ("fgets returned NULL. Need that to get mtd drive.\n"));
return 1;
return FAIL;
}
pclose(fp);
memset(mtdvalue, 0, MTDSIZE);
@ -82,7 +87,7 @@ int startWritingFPGAprogram(FILE **filefp) {
char *pch = strtok(output, ":");
if (pch == NULL) {
LOG(logERROR, ("Could not get mtd value\n"));
return 1;
return FAIL;
}
strcat(mtdvalue, pch);
LOG(logINFO, ("Flash drive found: %s\n", mtdvalue));
@ -93,11 +98,11 @@ int startWritingFPGAprogram(FILE **filefp) {
*filefp = fopen(mtdvalue, "w");
if (*filefp == NULL) {
LOG(logERROR, ("Unable to open %s in write mode\n", mtdvalue));
return 1;
return FAIL;
}
LOG(logINFO, ("Flash ready for writing\n"));
return 0;
return OK;
}
int stopWritingFPGAprogram(FILE *filefp) {
@ -120,7 +125,7 @@ int stopWritingFPGAprogram(FILE *filefp) {
usleep(1000);
timeSpent += 1000;
if (timeSpent >= MAX_TIME_FPGA_TOUCH_FLASH_US) {
return 1;
return FAIL;
}
FILE *sysFile = popen("cat /sys/class/gpio/gpio7/value", "r");
fgets(output, sizeof(output), sysFile);
@ -129,21 +134,60 @@ int stopWritingFPGAprogram(FILE *filefp) {
LOG(logDEBUG1, ("gpi07 returned %d\n", res));
}
LOG(logINFO, ("FPGA has picked up the program from flash\n"));
return 0;
return OK;
}
int writeFPGAProgram(char *fpgasrc, uint64_t fsize, FILE *filefp) {
LOG(logDEBUG1,
("Writing of FPGA Program\n"
"\taddress of fpgasrc:%p\n"
"\tfsize:%llu\n\tpointer:%p\n",
(void *)fpgasrc, (long long unsigned int)fsize, (void *)filefp));
if (fwrite((void *)fpgasrc, sizeof(char), fsize, filefp) != fsize) {
LOG(logERROR, ("Could not write FPGA source to flash (size:%llu)\n",
(long long unsigned int)fsize));
return 1;
int startCopyingFPGAProgram(FILE **fd, uint64_t fsize, char *mess) {
// delete old /var/tmp/file
{
char cmd[MAX_STR_LENGTH] = {0};
memset(cmd, 0, MAX_STR_LENGTH);
sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME);
char retvals[MAX_STR_LENGTH] = {0};
memset(retvals, 0, MAX_STR_LENGTH);
if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) {
strcpy(mess, retvals);
// LOG(logERROR, (mess)); already printed in executecommand
return FAIL;
}
LOG(logDEBUG1, ("program written to flash\n"));
return 0;
}
// check available memory to copy program
struct sysinfo info;
sysinfo(&info);
if (fsize >= info.freeram) {
sprintf(mess,
"Could not program fpga. Not enough memory to copy "
"program. [File size:l%dMB, free RAM: l%dMB]\n",
(fsize / (1024 * 1024)), (info.freeram / (1024 * 1024)));
LOG(logERROR, (mess));
return FAIL;
}
// open file to copy program
*fd = fopen(TEMP_PROG_FILE_NAME, "w");
if (*fd == NULL) {
sprintf(mess, "Unable to open %s in write mode\n", TEMP_PROG_FILE_NAME);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("%s ready to copy program\n", TEMP_PROG_FILE_NAME));
return OK;
}
int writeFPGAProgram(uint64_t fsize, FILE *fd, char *src, char* msg, char* mess) {
LOG(logDEBUG1,
("%s [fsize:%lu,fd:%p,src:%p\n", msg, (long long unsigned int)fsize, (void *)fd, (void *)src));
if (fwrite((void *)src, sizeof(char), fsize, fd) != fsize) {
sprintf(mess, "Could not %s (size:%lu)\n", msg, (long long unsigned int)fsize);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logDEBUG1, ("%s\n", msg));
return OK;
}

View File

@ -13,7 +13,6 @@
#include <arpa/inet.h>
#include <pthread.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <unistd.h>
// defined in the detector specific Makefile
@ -35,10 +34,6 @@ const enum detectorType myDetectorType = GOTTHARD2;
const enum detectorType myDetectorType = GENERIC;
#endif
#if defined(JUNGFRAUD) || defined(CHIPTESTBOARDD) || defined(MOENCHD)
#define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof"
#endif
// Global variables from communication_funcs
extern int lockStatus;
extern uint32_t lastClientIP;
@ -3716,6 +3711,10 @@ int program_fpga(int file_des) {
// free resources
free(fpgasrc);
}
if (ret == FAIL) {
LOG(logERROR, ("Program FPGA FAIL!\n"));
return FAIL;
}
#else // jungfrau, ctb, moench
@ -3726,109 +3725,67 @@ int program_fpga(int file_des) {
LOG(logINFOBLUE, ("Program size is: %lld\n",
(long long unsigned int)filesize));
// delete old /var/tmp/file
{
char cmd[MAX_STR_LENGTH] = {0};
memset(cmd, 0, MAX_STR_LENGTH);
sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME);
char retvals[MAX_STR_LENGTH] = {0};
memset(retvals, 0, MAX_STR_LENGTH);
ret = executeCommand(cmd, retvals, logDEBUG1);
if (ret == FAIL) {
strcpy(mess, retvals);
// LOG(logERROR, (mess)); already printed in executecommand
}
}
// check available memory to copy program
// open file and allocate memory for part program
FILE *fd = NULL;
ret = startCopyingFPGAProgram(&fd, filesize, mess);
char *src = NULL;
if (ret == OK) {
struct sysinfo info;
sysinfo(&info);
if (filesize >= info.freeram) {
ret = FAIL;
sprintf(mess,
"Could not program fpga. Not enough memory to copy "
"program. [File size:%dMb, free RAM: %dMb]\n",
(filesize / (1024 * 1024), (info.freeram / (1024 * 1024))));
src = malloc(MAX_FPGAPROGRAMSIZE);
if (src == NULL) {
strcpy(mess, "Could not allocate memory to get fpga program\n");
LOG(logERROR, (mess));
fclose(fd);
return FAIL;
}
}
// open file to copy
Server_SendResult(file_des, INT32, NULL, 0);
/*
size_t fsize = filesize;
fpgasrc = malloc(fsize);
if (fpgasrc == NULL) {
LOG(logERROR, ("Could not malloc\n"));
ret = FAIL;
}*/
// fpgasrc = malloc(filesize + 1);
char *fpgasrc = NULL;
FILE *fp = NULL;
uint64_t offset = 0;
// copying program part by part
uint64_t totalsize = filesize;
// writing to flash part by part
int clientSocketCrash = 0;
while (ret != FAIL && filesize) {
uint64_t unitprogramsize = MAX_FPGAPROGRAMSIZE; // 2mb
if (unitprogramsize > filesize) // less than 2mb
unitprogramsize = filesize;
LOG(logINFOBLUE,
("unit size to receive is:%lld [ooffset:%lld, filesize:%lld]\n",
("unit size to receive is:%lld [filesize:%lld]\n",
(long long unsigned int)unitprogramsize,
(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, src, unitprogramsize, OTHER) < 0) {
printSocketReadError();
clientSocketCrash = 1;
ret = FAIL;
LOG(logERROR, ("error, not receiverd\n"));
break;
}
// client has not crashed yet, so write to flash and send ret
else {
LOG(logINFOBLUE, ("receiverd\n"));
offset += unitprogramsize;
if (unitprogramsize - filesize == 0) {
src[unitprogramsize] = '\0';
filesize -= unitprogramsize;
unitprogramsize++;
} else
filesize -= unitprogramsize;
Server_SendResult(file_des, INT32, NULL, 0);
// print progress
LOG(logINFOBLUE,
("Writing to Flash:%d%%\r",
(int)(((double)(totalsize - filesize) / totalsize) *
100)));
fflush(stdout);
/*
// write part to flash
ret = writeFPGAProgram(fpgasrc, unitprogramsize, fp);
// copy program
ret = writeFPGAProgram(unitprogramsize, fd, src, "copy program to /var/tmp", mess);
Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) {
strcpy(mess, "Could not write to flash. Breaking out of "
"program receiving. Try to flash again "
"without rebooting.\n");
LOG(logERROR, (mess));
} else {
break;
}
// print progress
LOG(logINFO,
("Writing to Flash:%d%%\r",
("Copying Program to /var/tmp/:%d%%\r",
(int)(((double)(totalsize - filesize) / totalsize) *
100)));
fflush(stdout);
}
*/
}
if (ret == FAIL) {
free(src);
fclose(fd);
LOG(logERROR, ("Program FPGA FAIL!\n"));
return FAIL;
}
free(src);
fclose(fd);
/* if (ret != FAIL) {
fpgasrc[totalsize] = '\0';
}*/
@ -3864,25 +3821,11 @@ int program_fpga(int file_des) {
}
}
*/
// free resources
free(fpgasrc);
if (fp != NULL)
fclose(fp);
// send final ret (if no client crash)
if (clientSocketCrash == 0) {
Server_SendResult(file_des, INT32, NULL, 0);
}
#endif // end of Blackfin programming
if (ret == FAIL) {
LOG(logERROR, ("Program FPGA FAIL!\n"));
} else {
LOG(logINFOGREEN, ("Programming FPGA completed successfully\n"));
}
}
#endif
return ret;
}

View File

@ -3450,16 +3450,7 @@ void Module::programFPGAviaBlackfin(std::vector<char> buffer) {
}
filesize -= unitprogramsize;
currentPointer += unitprogramsize;
// print progress
printf(
"%d%%\r",
static_cast<int>(
(static_cast<double>(totalsize - filesize) / totalsize) * 100));
std::cout << std::flush;
}
std::cout << '\n';
/*
// error in detector at opening file pointer to flash