jungfrau server works

This commit is contained in:
2018-11-02 13:50:00 +01:00
parent 92123069ce
commit b43be57b6e
33 changed files with 561 additions and 649 deletions

View File

@ -1,19 +1,17 @@
#ifndef PROGRAM_FPGA_H
#define PROGRAM_FPGA_H
#pragma once
#include "ansi.h"
#include <stdio.h>
#include <unistd.h> // usleep
#include <string.h>
/* global variables */
#define CTRL_SRVR_INIT_TIME_US (300 * 1000)
int gpioDefined=0;
#define MTDSIZE 10
char mtdvalue[MTDSIZE];
#define MTDSIZE 10
int gpioDefined = 0;
char mtdvalue[MTDSIZE] = {0};
/**
@ -27,9 +25,9 @@ void defineGPIOpins(){
//define their direction
system("echo in > /sys/class/gpio/gpio7/direction");
system("echo out > /sys/class/gpio/gpio9/direction");
printf("gpio pins defined\n");
FILE_LOG(logINFO, ("gpio pins defined\n"));
gpioDefined = 1;
}else printf("gpio pins already defined earlier\n");
}else FILE_LOG(logDEBUG1, ("gpio pins already defined earlier\n"));
}
/**
@ -54,7 +52,7 @@ void FPGATouchFlash(){
* Reset FPGA
*/
void resetFPGA(){
cprintf(BLUE,"\n*** Reseting FPGA ***\n");
FILE_LOG(logINFOBLUE, ("Reseting FPGA\n"));
FPGAdontTouchFlash();
FPGATouchFlash();
usleep(CTRL_SRVR_INIT_TIME_US);
@ -64,14 +62,12 @@ void resetFPGA(){
* Erasing flash
*/
void eraseFlash(){
#ifdef VERY_VERBOSE
printf("\nErasing Flash\n");
#endif
FILE_LOG(logDEBUG1, ("Erasing Flash\n"));
char command[255];
memset(command, 0, 255);
sprintf(command,"flash_eraseall %s",mtdvalue);
system(command);
printf("Flash erased\n");
FILE_LOG(logINFO, ("Flash erased\n"));
}
/**
@ -81,43 +77,40 @@ void eraseFlash(){
* @return 0 for success, 1 for fail (cannot open file for writing program)
*/
int startWritingFPGAprogram(FILE** filefp){
#ifdef VERY_VERBOSE
printf("\nStart Writing of FPGA program\n");
#endif
FILE_LOG(logDEBUG1, ("Start Writing of FPGA program\n"));
//getting the drive
char output[255];
memset(output, 0, 255);
FILE* fp = popen("awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd", "r");
if (fp == NULL) {
cprintf(RED,"popen returned NULL. Need that to get mtd drive.\n");
FILE_LOG(logERROR, ("popen returned NULL. Need that to get mtd drive.\n"));
return 1;
}
if (fgets(output, sizeof(output), fp) == NULL) {
cprintf(RED,"fgets returned NULL. Need that to get mtd drive.\n");
FILE_LOG(logERROR, ("fgets returned NULL. Need that to get mtd drive.\n"));
return 1;
}
pclose(fp);
//cprintf(RED,"output: %s\n", output);
memset(mtdvalue, 0, MTDSIZE);
strcpy(mtdvalue,"/dev/");
char* pch = strtok(output,":");
if(pch == NULL){
cprintf(RED,"Could not get mtd value\n");
FILE_LOG(logERROR, ("Could not get mtd value\n"));
return 1;
}
strcat(mtdvalue,pch);
printf ("\nFlash drive found: %s\n",mtdvalue);
FILE_LOG(logINFO, ("Flash drive found: %s\n", mtdvalue));
FPGAdontTouchFlash();
//writing the program to flash
*filefp = fopen(mtdvalue, "w");
if(*filefp == NULL){
cprintf(RED,"Unable to open %s in write mode\n",mtdvalue);
FILE_LOG(logERROR, ("Unable to open %s in write mode\n", mtdvalue));
return 1;
}
printf("Flash ready for writing\n");
FILE_LOG(logINFO, ("Flash ready for writing\n"));
return 0;
}
@ -128,9 +121,7 @@ int startWritingFPGAprogram(FILE** filefp){
* @param filefp pointer to flash
*/
void stopWritingFPGAprogram(FILE* filefp){
#ifdef VERY_VERBOSE
printf("\nStopping of writing FPGA program\n");
#endif
FILE_LOG(logDEBUG1, ("Stopping of writing FPGA program\n"));
int wait = 0;
if(filefp!= NULL){
@ -142,9 +133,7 @@ void stopWritingFPGAprogram(FILE* filefp){
FPGATouchFlash();
if(wait){
#ifdef VERY_VERBOSE
printf("Waiting for FPGA to program from flash\n");
#endif
FILE_LOG(logDEBUG1, ("Waiting for FPGA to program from flash\n"));
//waiting for success or done
char output[255];
int res=0;
@ -153,12 +142,10 @@ void stopWritingFPGAprogram(FILE* filefp){
fgets(output, sizeof(output), sysFile);
pclose(sysFile);
sscanf(output,"%d",&res);
#ifdef VERY_VERBOSE
printf("gpi07 returned %d\n",res);
#endif
FILE_LOG(logDEBUG1, ("gpi07 returned %d\n", res));
}
}
printf("FPGA has picked up the program from flash\n\n");
FILE_LOG(logINFO, ("FPGA has picked up the program from flash\n"));
}
@ -170,21 +157,15 @@ void stopWritingFPGAprogram(FILE* filefp){
* @return 0 for success, 1 for fail (cannot write)
*/
int writeFPGAProgram(char* fpgasrc, size_t fsize, FILE* filefp){
#ifdef VERY_VERBOSE
printf("\nWriting of FPGA Program\n");
cprintf(BLUE,"address of fpgasrc:%p\n",(void *)fpgasrc);
cprintf(BLUE,"fsize:%lu\n",fsize);
cprintf(BLUE,"pointer:%p\n",(void*)filefp);
#endif
FILE_LOG(logDEBUG1, ("Writing of FPGA Program\n"
"\taddress of fpgasrc:%p\n"
"\tfsize:%lu\n\tpointer:%p\n",
(void *)fpgasrc, fsize, (void*)filefp));
if(fwrite((void*)fpgasrc , sizeof(char) , fsize , filefp )!= fsize){
cprintf(RED,"Could not write FPGA source to flash (size:%lu)\n", fsize);
FILE_LOG(logERROR, ("Could not write FPGA source to flash (size:%lu)\n", fsize));
return 1;
}
#ifdef VERY_VERBOSE
cprintf(BLUE, "program written to flash\n");
#endif
FILE_LOG(logDEBUG1, ("program written to flash\n"));
return 0;
}
#endif //PROGRAM_FPGA_H