mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-02-03 23:18:41 +01:00
wip
This commit is contained in:
@@ -9,17 +9,21 @@
|
||||
#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"
|
||||
|
||||
#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_DRIVE_NAME_SIZE 16
|
||||
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0};
|
||||
int gpioDefined = 0;
|
||||
char mtdvalue[MTDSIZE] = {0};
|
||||
|
||||
extern int executeCommand(char *command, char *result, enum TLogLevel level);
|
||||
|
||||
void defineGPIOpins() {
|
||||
#ifdef VIRTUAL
|
||||
return;
|
||||
#endif
|
||||
if (!gpioDefined) {
|
||||
// define the gpio pins
|
||||
system("echo 7 > /sys/class/gpio/export");
|
||||
@@ -34,139 +38,61 @@ void defineGPIOpins() {
|
||||
}
|
||||
|
||||
void FPGAdontTouchFlash() {
|
||||
#ifdef VIRTUAL
|
||||
return;
|
||||
#endif
|
||||
// tell FPGA to not touch flash
|
||||
system("echo 0 > /sys/class/gpio/gpio9/value");
|
||||
// usleep(100*1000);
|
||||
}
|
||||
|
||||
void FPGATouchFlash() {
|
||||
#ifdef VIRTUAL
|
||||
return;
|
||||
#endif
|
||||
// tell FPGA to touch flash to program itself
|
||||
system("echo 1 > /sys/class/gpio/gpio9/value");
|
||||
}
|
||||
|
||||
void resetFPGA() {
|
||||
LOG(logINFOBLUE, ("Reseting FPGA\n"));
|
||||
#ifdef VIRTUAL
|
||||
return;
|
||||
#endif
|
||||
FPGAdontTouchFlash();
|
||||
FPGATouchFlash();
|
||||
usleep(CTRL_SRVR_INIT_TIME_US);
|
||||
}
|
||||
|
||||
void eraseFlash() {
|
||||
LOG(logDEBUG1, ("Erasing Flash\n"));
|
||||
char command[255];
|
||||
memset(command, 0, 255);
|
||||
sprintf(command, "flash_eraseall %s", mtdvalue);
|
||||
system(command);
|
||||
LOG(logINFO, ("Flash erased\n"));
|
||||
}
|
||||
|
||||
int startWritingFPGAprogram(FILE **filefp) {
|
||||
LOG(logDEBUG1, ("Start Writing of FPGA program\n"));
|
||||
|
||||
// getting the drive
|
||||
// root:/> cat /proc/mtd
|
||||
// dev: size erasesize name
|
||||
// mtd0: 00040000 00020000 "bootloader(nor)"
|
||||
// mtd1: 00100000 00020000 "linux kernel(nor)"
|
||||
// mtd2: 002c0000 00020000 "file system(nor)"
|
||||
// mtd3: 01000000 00010000 "bitfile(spi)"
|
||||
char output[255];
|
||||
memset(output, 0, 255);
|
||||
FILE *fp = popen(
|
||||
"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 FAIL;
|
||||
}
|
||||
if (fgets(output, sizeof(output), fp) == NULL) {
|
||||
LOG(logERROR, ("fgets returned NULL. Need that to get mtd drive.\n"));
|
||||
return FAIL;
|
||||
}
|
||||
pclose(fp);
|
||||
memset(mtdvalue, 0, MTDSIZE);
|
||||
strcpy(mtdvalue, "/dev/");
|
||||
char *pch = strtok(output, ":");
|
||||
if (pch == NULL) {
|
||||
LOG(logERROR, ("Could not get mtd value\n"));
|
||||
return FAIL;
|
||||
}
|
||||
strcat(mtdvalue, pch);
|
||||
LOG(logINFO, ("Flash drive found: %s\n", mtdvalue));
|
||||
|
||||
FPGAdontTouchFlash();
|
||||
|
||||
// writing the program to flash
|
||||
*filefp = fopen(mtdvalue, "w");
|
||||
if (*filefp == NULL) {
|
||||
LOG(logERROR, ("Unable to open %s in write mode\n", mtdvalue));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("Flash ready for writing\n"));
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int stopWritingFPGAprogram(FILE *filefp) {
|
||||
LOG(logDEBUG1, ("Stopping of writing FPGA program\n"));
|
||||
|
||||
if (filefp != NULL) {
|
||||
fclose(filefp);
|
||||
}
|
||||
|
||||
// touch and program
|
||||
FPGATouchFlash();
|
||||
|
||||
LOG(logINFO, ("Waiting for FPGA to program from flash\n"));
|
||||
// waiting for success or done
|
||||
char output[255];
|
||||
int res = 0;
|
||||
int timeSpent = 0;
|
||||
while (res == 0) {
|
||||
// time taken for fpga to pick up from flash
|
||||
usleep(1000);
|
||||
timeSpent += 1000;
|
||||
if (timeSpent >= MAX_TIME_FPGA_TOUCH_FLASH_US) {
|
||||
return FAIL;
|
||||
}
|
||||
FILE *sysFile = popen("cat /sys/class/gpio/gpio7/value", "r");
|
||||
fgets(output, sizeof(output), sysFile);
|
||||
pclose(sysFile);
|
||||
sscanf(output, "%d", &res);
|
||||
LOG(logDEBUG1, ("gpi07 returned %d\n", res));
|
||||
}
|
||||
LOG(logINFO, ("FPGA has picked up the program from flash\n"));
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int startCopyingFPGAProgram(FILE **fd, uint64_t fsize, char *mess) {
|
||||
int preparetoCopyFPGAProgram(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);
|
||||
sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME);
|
||||
if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) {
|
||||
strcpy(mess, retvals);
|
||||
// LOG(logERROR, (mess)); already printed in executecommand
|
||||
sprintf(mess,
|
||||
"Could not program fpga. (could not delete old file: %s)",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
// 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:%ldMB, free RAM: %ldMB]\n",
|
||||
(long int)(fsize / (1024 * 1024)),
|
||||
(long int)(info.freeram / (1024 * 1024)));
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
{
|
||||
struct sysinfo info;
|
||||
sysinfo(&info);
|
||||
if (fsize >= info.freeram) {
|
||||
sprintf(mess,
|
||||
"Could not program fpga. Not enough memory to copy "
|
||||
"program. [File size:%ldMB, free RAM: %ldMB]\n",
|
||||
(long int)(fsize / (1024 * 1024)),
|
||||
(long int)(info.freeram / (1024 * 1024)));
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
// open file to copy program
|
||||
@@ -180,19 +106,208 @@ int startCopyingFPGAProgram(FILE **fd, uint64_t fsize, char *mess) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
int writeFPGAProgram(FILE *fd, char *src, uint64_t fsize, char* msg, char* mess) {
|
||||
LOG(logDEBUG1,
|
||||
("%s [fsize:%lu,fd:%p,src:%p\n", msg, (long long unsigned int)fsize, (void *)fd, (void *)src));
|
||||
int copyToFlash(char *clientChecksum, char *mess) {
|
||||
|
||||
if (fwrite((void *)src, sizeof(char), fsize, fd) != fsize) {
|
||||
sprintf(mess, "Could not %s (size:%ld)\n", msg, (long int)fsize);
|
||||
LOG(logERROR, (mess));
|
||||
if (getDrive(mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logDEBUG1, ("%s\n", msg));
|
||||
|
||||
FILE *flashfd = NULL;
|
||||
FILE *srcfd = NULL;
|
||||
if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (eraseFlash(mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (writeToFlash(flashfd, srcfd, mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("Checksum in Flash verified\n"));
|
||||
|
||||
if (waitForFPGAtoTouchFlash(mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int verifyCheckSumofProgram(char* clientChecksum, char* mess) {
|
||||
return verifyChecksumFromFile(mess, clientChecksum, TEMP_PROG_FILE_NAME);
|
||||
}
|
||||
int getDrive(char *mess) {
|
||||
#ifdef VIRTUAL
|
||||
strcpy(flashDriveName, "/tmp/SLS_mtd3");
|
||||
return OK;
|
||||
#endif
|
||||
// getting the drive
|
||||
// root:/> cat /proc/mtd
|
||||
// dev: size erasesize name
|
||||
// mtd0: 00040000 00020000 "bootloader(nor)"
|
||||
// mtd1: 00100000 00020000 "linux kernel(nor)"
|
||||
// mtd2: 002c0000 00020000 "file system(nor)"
|
||||
// mtd3: 01000000 00010000 "bitfile(spi)"
|
||||
|
||||
char cmd[MAX_STR_LENGTH] = {0};
|
||||
char retvals[MAX_STR_LENGTH] = {0};
|
||||
sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME);
|
||||
if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) {
|
||||
sprintf(mess, "Could not program fpga. (could not delete old file: %s)",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
memset(retvals, 0, MAX_STR_LENGTH);
|
||||
if (executeCommand(CMD_GET_FLASH, retvals, logDEBUG1) == FAIL) {
|
||||
sprintf(mess, "Could not program fpga. (could not get flash drive: %s)",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
char *pch = strtok(retvals, ":");
|
||||
if (pch == NULL) {
|
||||
strcpy(mess, "Could not get mtd drive to flash (strtok fail).\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
strcpy(flashDriveName, "/dev/");
|
||||
strcat(flashDriveName, pch);
|
||||
LOG(logINFO, ("Flash drive found: %s\n", flashDriveName));
|
||||
return OK;
|
||||
}
|
||||
|
||||
int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess) {
|
||||
FPGAdontTouchFlash();
|
||||
|
||||
// open src file
|
||||
*srcfd = fopen(TEMP_PROG_FILE_NAME, "r");
|
||||
if (*srcfd == NULL) {
|
||||
sprintf(mess,
|
||||
"Could not flash. Unable to open temp program file %s in read "
|
||||
"mode\n",
|
||||
TEMP_PROG_FILE_NAME);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logDEBUG1, ("Temp file ready for reading\n"));
|
||||
|
||||
// open flash drive for writing
|
||||
*flashfd = fopen(flashDriveName, "w");
|
||||
if (*flashfd == NULL) {
|
||||
fclose(*srcfd);
|
||||
sprintf(mess, "Unable to open flash drive %s in write mode\n",
|
||||
flashDriveName);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("Flash ready for writing\n"));
|
||||
return OK;
|
||||
}
|
||||
|
||||
int eraseFlash(char *mess) {
|
||||
LOG(logDEBUG1, ("Erasing Flash\n"));
|
||||
|
||||
#ifdef VIRTUAL
|
||||
return OK;
|
||||
#endif
|
||||
char cmd[MAX_STR_LENGTH] = {0};
|
||||
char retvals[MAX_STR_LENGTH] = {0};
|
||||
sprintf(cmd, "flash_eraseall %s", flashDriveName);
|
||||
if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) {
|
||||
sprintf(mess, "Could not program fpga. (could not erase flash: %s)",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
LOG(logINFO, ("Flash erased\n"));
|
||||
return OK;
|
||||
}
|
||||
|
||||
int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
|
||||
LOG(logDEBUG1, ("writing to flash\n"));
|
||||
|
||||
char* buffer = malloc(MAX_FPGAPROGRAMSIZE);
|
||||
if (buffer == NULL) {
|
||||
fclose(flashfd);
|
||||
fclose(srcfd);
|
||||
strcpy(mess, "Could not program foga. Memory allocation to write to "
|
||||
"flash failed.\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
ssize_t totalBytes = 0;
|
||||
ssize_t bytes = fread(buffer, sizeof(char), MAX_FPGAPROGRAMSIZE, srcfd);
|
||||
while (bytes > 0) {
|
||||
ssize_t bytesWritten =
|
||||
fwrite((void *)buffer, sizeof(char), bytes, flashfd);
|
||||
totalBytes += bytesWritten;
|
||||
if (bytesWritten != bytes) {
|
||||
fclose(flashfd);
|
||||
fclose(srcfd);
|
||||
sprintf(mess,
|
||||
"Could not write to flash (bytes written:%ld, expected: "
|
||||
"%ld, total written:%ld)\n",
|
||||
(long int)bytesWritten, (long int)bytes,
|
||||
(long int)totalBytes);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
bytes = fread(buffer, sizeof(char), bytes, srcfd);
|
||||
}
|
||||
fclose(flashfd);
|
||||
fclose(srcfd);
|
||||
LOG(logINFO, ("Wrote %ld bytes to flash\n", totalBytes));
|
||||
return OK;
|
||||
}
|
||||
|
||||
int waitForFPGAtoTouchFlash(char* mess) {
|
||||
// touch and program
|
||||
FPGATouchFlash();
|
||||
|
||||
#ifdef VIRTUAL
|
||||
return OK;
|
||||
#endif
|
||||
LOG(logINFO, ("Waiting for FPGA to program from flash\n"));
|
||||
int timeSpent = 0;
|
||||
|
||||
int result = 0;
|
||||
while (result == 0) {
|
||||
// time taken for fpga to pick up from flash
|
||||
usleep(1000);
|
||||
timeSpent += 1000;
|
||||
if (timeSpent >= MAX_TIME_FPGA_TOUCH_FLASH_US) {
|
||||
sprintf(mess, "Could not program fpga. (exceeded max time allowed: %ds)\n",
|
||||
MAX_TIME_FPGA_TOUCH_FLASH_US/(1000 * 1000));
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
// read gpio status
|
||||
char retvals[MAX_STR_LENGTH] = {0};
|
||||
if (FAIL == executeCommand(CMD_FPGA_PICKED_STATUS, retvals, logDEBUG1)) {
|
||||
sprintf(mess, "Could not program fpga. (could not read gpio status: %s)\n",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
// convert to int
|
||||
if (sscanf(retvals, "%d", &result) != 1) {
|
||||
sprintf(mess, "Could not program fpga. (could not scan int for gpio status: %s)\n",
|
||||
retvals);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logDEBUG1, ("gpi07 returned %d\n", result));
|
||||
}
|
||||
LOG(logINFO, ("FPGA has picked up the program from flash\n"));
|
||||
return OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user