/* example of: /etc/udev/rules.d 99-usb-serial.rules KERNEL=="ttyUSB*", ATTRS{idVendor}=="0557", SYMLINK+="5to1card_0" KERNEL=="ttyUSB*", ATTRS{idVendor}=="1a86", SYMLINK+="chipaddress_1" Compile with: gcc -Wall -o lvcard2 lvcard2.c before this program set the baud rate to 57600, 8 bits, 1 stop bit, no parity: stty -F 57600 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe To show settings: stty -a -F */ #include #include #include #include #include #include #include #include #include #include #include // Default device name #define DEVICE "/dev/lvcard2_1" // testcard commands, end with // // An read analog port n (n=F read all) // C0 : read CPLD port0 // C1 : exist CPLD port1? // DI DacInit (default progam values) // DA mcmDacreadAll // DW mcmDacWrite // DR mcmDacRead // DE DAC exist? // VD Voltage Disable // VE Voltage Enable // TR mcm Temperature Read // TE mcm TMP112 exist? // TL Lvcard temperature read // H Help // ? Print program version and status // // int action = 0; int logdata = 0; int readadc = 0; int readdac = 0; int rdmcmtemp = 0; int rdlvtemp = 0; int cpldid = 0; int power = 0; int mcmaddress = 1; // two ascii digits 00-99 int settings = 0; int debug = 1; int nbloops = 1; int mssleep = 20; char replybuf[1024]; char csvfile[256]; char datestring[128]; char mydev[64]; int v_1v5,c_1v5,v_2v5,c_2v5; int c_1v5_adc,c_2v5_adc; int v_mcm1v5,v_mcm2v5; int dacdecay,dacrefp,daccm,dacrefn; int decay = 250; // 2*250 mV int refn = 75; // 2*75 int refp = 675; // 2*675 int refcm = 375; // 2*375 int setdac = 0; // set refvolt: bit 0:decay,1: refp,2:refcm,3:refn #define _dac_decay_bit 0 #define _dac_refp_bit 1 #define _dac_refcm_bit 2 #define _dac_refn_bit 3 #define NO_ACTION 0 #define LVCARD_READADC 2 #define LVCARD_SWITCH_ON 3 #define LVCARD_SWITCH_OFF 4 #define LVCARD_MCM_INIT 5 #define LVCARD_MCM_TEMP 6 #define LVCARD_MCM_DACWR 7 #define LVCARD_MCM_DACRD 8 #define LVCARD_READ_TEMP 9 #define LVCARD_VERSION 10 #define _mcm_maxtemp 45.0 #define _lvcard_maxtemp 45.0 #define _mcm_c15v_constant 1.25 #define _mcm_c25v_constant 0.10 int getParameters(int argc, char **argv); int sendcommand(int devfd, char *cmd, char *reply); int readadcLvcard(int devfd); int DecodeAdc(char *reply); int switchoffMCM(int devfd); int switchonMCM(int devfd); int switchonoffMCM(int devfd,int onoff); int initMCM(int devfd); int dacreadMCM(int devfd); int dacwriteMCM(int devfd); int tempreadMCM(int devfd); int tempreadLvcard(int devfd); unsigned int versionLvcard(int devfd); void printHelp(); void printHelp() { puts("Available commands"); puts("=================="); puts("--cpldid CPLD id (4 char HEX)"); puts("--csvfile "); puts("--device "); puts("--debug some more output/0=none"); puts("--power 0-disable/1-enable"); puts("--help"); puts("--readadc read processor adcs"); puts("--decay set Decay value"); puts("--refn set RefN value"); puts("--refcm set CM value"); puts("--refp set RefP value"); puts("--readdac read DAC settings"); puts("--loops "); puts("--mcmaddress "); puts("--mcmini default (in processor) DAC settings"); puts("--mcmtemp read MCM temperature sensor"); puts("--lvtemp read LVCARD temp"); puts("--settings setting for the test"); puts("--usleep between loops"); puts("--version read LVCARD version"); puts("=================="); puts("NOTE: combinations of: --decay --refn --refp --refcm is one action"); } int getParameters(int argc, char **argv) { const char *short_options = "cC:dD:e:f:hiI:J:l:mM:N:o:O:P:rRs:t:u:T:v"; int option_index = 0; int c; int nbarg = 0; int nbactions = 0; const struct option long_options[]= { {"csvfile",1,0,'f'}, {"device",1,0,'d'}, {"debug",1,0,'e'}, {"poweron",1,0,'O'}, {"poweroff",1,0,'o'}, {"help",0,0,'h'}, {"readadc",1,0,'r'}, {"decay",1,0,'D'}, {"loops",1,0,'l'}, {"refn",1,0,'N'}, {"refp",1,0,'P'}, {"refcm",1,0,'C'}, {"cpldid",1,0,'c'}, {"readdac",1,0,'R'}, {"mcmini",0,0,'m'}, {"mcmaddress",1,0,'M'}, {"mcmtemp",1,0,'t'}, {"lvtemp",1,0,'T'}, {"settings",1,0,'s'}, {"usleep",1,0,'u'}, {"version",0,0,'v'}, {0,0,0,0}, }; action = 0; // default read only logdata = 0; setdac = 0; // write to dac settings = 0; strcpy(mydev,DEVICE); if (argc > 1) { while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != EOF) { switch (c) { case 'f': nbarg++; strcpy(csvfile,optarg); logdata = 1; break; case 'd': nbarg++; strcpy(mydev,optarg); break; case 'D': // DAC Decay nbarg++; setdac |= 1; sscanf(optarg,"%d",&decay); break; case 'e': nbarg++; sscanf(optarg,"%d",&debug); break; case 'N': // DAC RefN nbarg++; setdac |= 8; sscanf(optarg,"%d",&refn); break; case 'c': // cpldid nbarg++; sscanf(optarg,"%X",&cpldid); break; case 'C': // DAC RefC nbarg++; setdac |= 4; sscanf(optarg,"%d",&refcm); break; case 'P': // DAC RefP nbarg++; setdac |= 2; sscanf(optarg,"%d",&refp); break; case 'h': printHelp(); return (2); break; case 'l': sscanf(optarg,"%d",&nbloops); break; case 'm': nbarg++; action = LVCARD_MCM_INIT; nbactions++; break; case 'M': // mcmaddress nbarg++; sscanf(optarg,"%d",&mcmaddress); break; case 'o': nbarg++; sscanf(optarg,"%d",&power); action = LVCARD_SWITCH_OFF; nbactions++; break; case 'O': nbarg++; sscanf(optarg,"%d",&power); action = LVCARD_SWITCH_ON; nbactions++; break; case 't': nbarg++; sscanf(optarg,"%d",&rdmcmtemp); action = LVCARD_MCM_TEMP; nbactions++; break; case 'T': nbarg++; sscanf(optarg,"%d",&rdlvtemp); action = LVCARD_READ_TEMP; nbactions++; break; case 'u': sscanf(optarg,"%d",&mssleep); break; case 'r': nbarg++; sscanf(optarg,"%d",&readadc); action = LVCARD_READADC; nbactions++; break; case 'R': nbarg++; action = LVCARD_MCM_DACRD; sscanf(optarg,"%d",&readdac); nbactions++; break; case 's': nbarg++; sscanf(optarg,"%d",&settings); break; case 'v': nbarg++; action = LVCARD_VERSION; nbactions++; break; default: printf("%s: Unknown option %s\n",__FUNCTION__,argv[optind-1]); return 1; } } } if ((mcmaddress < 0) || (mcmaddress > 99)) { printf("mcm address must be within 0-99 is %d\n",mcmaddress); } if ((setdac != 0) && (action != LVCARD_MCM_DACRD)) { nbactions++; action = LVCARD_MCM_DACWR; } if (action == LVCARD_MCM_DACRD) { if ((setdac != 0) && (setdac != 0xF)) { printf("Must give all refvalues for checking read dac values\n"); return 1; } } if (nbactions != 1) { printHelp(); printf("Must have one and only one action specified, you gave %d\n",nbactions); return 1; } return 0; } int sendcommand(int devfd, char *cmd, char *reply) { int istat; int iret; int pos = 0; // printf("Command %s %ld \n",cmd,strlen(cmd)); istat = write(devfd,cmd,strlen(cmd)); if (istat < 0 ) { printf("%s: WRITE returned %d error %s\n",__FUNCTION__,istat,strerror(errno)); return (4); } else if (istat < strlen("cmd")) { printf("%s: WRITE returned length %d expected %ld\n",__FUNCTION__,istat,strlen(cmd)); return (5); } usleep(40000); do { iret=read(devfd,&reply[pos],sizeof(reply)); if ( (iret != -1) && iret ) { pos += iret; reply[pos] = '\0'; // printf("%d %d %s\n",iret,pos,reply); } } while(iret > 0); return(0); } int sendcommandPoll(int devfd, char *cmd, char *reply) { int size = sizeof(replybuf); int istat; int iret; int pos = 0; struct pollfd pollLvcard; pollLvcard.fd = devfd; pollLvcard.events = POLLIN; reply[pos] = '\0'; int timeout = 10; // wait 10 ms istat = write(devfd,cmd,strlen(cmd)); if (istat < 0 ) { printf("%s: WRITE returned %d error %s\n",__FUNCTION__,istat,strerror(errno)); return (4); } else if (istat < strlen("cmd")) { printf("%s: WRITE returned length %d expected %ld\n",__FUNCTION__,istat,strlen(cmd)); return (5); } pollLvcard.revents = 0; istat = poll(&pollLvcard,1,timeout); if (istat <= 0) {// timeout (0) or error (-1) return istat; } do { usleep(1000); iret=read(devfd,&reply[pos],size); if ( (iret != -1) && iret ) { pos += iret; reply[pos] = '\0'; size -= pos; if (size < 0) { //printf("Buffer exceeded\n"); return -2; // buffer exceeded } // printf("IRET %d %d %s\n",iret,pos,reply); } } while(iret > 0); // return(pos); return iret; } int readadcLvcard(int devfd) { char cmd[64]; int iret; sprintf(cmd,"M%02dAF\n",mcmaddress); //ADC Full if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: command failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } if (debug == 1) puts(replybuf); iret = DecodeAdc(replybuf); return iret; } // = 1 - power is off before run #define _v1v5_1_max 10 #define _v2v5_1_max 10 #define _c1v5_1_max 20 #define _c2v5_1_max 20 // Nominal voltages after power on #define _v1v5_min 1400 #define _v1v5_max 1600 #define _v2v5_min 2400 #define _v2v5_max 2600 // = 2 - after power on; saltro bias off, pasa shutdown #define _c1v5_2_min 200 #define _c1v5_2_max 400 #define _c2v5_2_min 40 #define _c2v5_2_max 100 // = 3,4,5 - after power on before/after run; saltro bias on, pasa on #define _c1v5_3_min 2500 #define _c1v5_3_max 3500 #define _c2v5_3_min 40 #define _c2v5_3_max 100 // Voltages on MCM after power off #define _vmcm1v5_1_max 10 #define _vmcm2v5_1_max 10 #define _vmcm1v5_10_max 10 #define _vmcm2v5_10_max 10 // Voltages on MCM after power on/off; saltro bias off, pasa shutdown #define _vmcm1v5_min 1400 #define _vmcm1v5_max 1600 #define _vmcm2v5_min 2400 #define _vmcm2v5_max 2600 // ref voltages - to be set properly for different DAC settings // read DAC and calculate range int DecodeAdc(char *reply) { char cmdhead[24],cmdtrail[24]; char *ptr; char tmpbuf[512]; unsigned int errorcode; unsigned int nberrors = 0; unsigned int foundflag = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int n; int expectedaddress; errorcode = 0; v_1v5 = 0; c_1v5 = 0; v_2v5 = 0; c_2v5 = 0; v_mcm1v5 = 0; v_mcm2v5 = 0; strncpy(tmpbuf,reply,sizeof(tmpbuf)); // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,readadc,v_1v5,c_1v5,c_1v5_adc,v_2v5,c_2v5,c_2v5_adc,v_mcm1v5,v_mcm2v5,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); trailfound = 1; ptr = strtok(tmpbuf,"\n"); // Returned command while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; // if (headfound) { if (!strncmp("V_1V5:",ptr,6)) { sscanf(ptr,"V_1V5:%d",&v_1v5); foundflag |= 0x0001; } else if (!strncmp("C_1V5:",ptr,6)) { sscanf(ptr,"C_1V5:%d",&c_1v5_adc); c_1v5 = _mcm_c15v_constant * c_1v5_adc; // 2mA per mV foundflag |= 0x0002; } else if (!strncmp("V_2V5:",ptr,6)) { sscanf(ptr,"V_2V5:%d",&v_2v5); foundflag |= 0x0004; } else if (!strncmp("C_2V5:",ptr,6)) { sscanf(ptr,"C_2V5:%d",&c_2v5_adc); c_2v5 = _mcm_c25v_constant * c_2v5_adc; foundflag |= 0x0008; } else if (!strncmp("V_MCM1V5:",ptr,9)) { sscanf(ptr,"V_MCM1V5:%d",&v_mcm1v5); foundflag |= 0x0010; } else if (!strncmp("V_MCM2V5:",ptr,9)) { sscanf(ptr,"V_MCM2V5:%d",&v_mcm2v5); foundflag |= 0x0020; } //} else if (!strncmp(cmdhead,ptr,4)) { headfound = 1; nbheads++; if (trailfound == 0) errorcode |= 8; foundflag = 0; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (foundflag != 0x3F) errorcode |= 2; if (headfound == 0) errorcode |= 4; if (errorcode == 0) { // readadc // = 0 - no action - test // = 1,10 - power is off before/after run // = 2 - after power on before run; saltro bias off, pasa shutdown // = 3 - after power on before run; saltro bias on, pasa on // = 4 - before start of run; saltro bias on, pasa on // = 5 - after end of run; saltro bias on, pasa on // = 9 - before power off; saltro bias off, pasa shutdown // 10 - after power off switch (readadc) { case 0: break; case 1: // 1 - power is off before run case 10: // 10 - after power off if (v_1v5 > _v1v5_1_max) errorcode |= 0x000100; if (v_2v5 > _v2v5_1_max) errorcode |= 0x000200; if (v_mcm1v5 > _vmcm1v5_1_max) errorcode |= 0x000400; if (v_mcm2v5 > _vmcm2v5_1_max) errorcode |= 0x000800; if (c_1v5 > _c1v5_1_max) errorcode |= 0x001000; if (c_2v5 > _c2v5_1_max) errorcode |= 0x002000; break; case 2: // 2 - after power on; saltro bias off, pasa shutdown case 9: // 9 - before power off; saltro bias off, pasa shutdown if ((v_1v5 < _v1v5_min) || (v_1v5 > _v1v5_max)) errorcode |= 0x000100; if ((v_2v5 < _v2v5_min) || (v_2v5 > _v2v5_max)) errorcode |= 0x000200; if ((v_mcm1v5 < _vmcm1v5_min) || (v_mcm1v5 > _vmcm1v5_max)) errorcode |= 0x000400; if ((v_mcm2v5 < _vmcm2v5_min) || (v_mcm2v5 > _vmcm2v5_max)) errorcode |= 0x000800; if ((c_1v5 < _c1v5_2_min) || (c_1v5 > _c1v5_2_max)) errorcode |= 0x001000; if ((c_2v5 < _c2v5_2_min) || (c_2v5 > _c2v5_2_max)) errorcode |= 0x002000; break; case 3: // 3 - after power on before run; saltro bias on, pasa on case 4: // 4 - before start of run; saltro bias on, pasa on case 5: // 5 - after end of run; saltro bias on, pasa on if ((v_1v5 < _v1v5_min) || (v_1v5 > _v1v5_max)) errorcode |= 0x000100; if ((v_2v5 < _v2v5_min) || (v_2v5 > _v2v5_max)) errorcode |= 0x000200; if ((v_mcm1v5 < _vmcm1v5_min) || (v_mcm1v5 > _vmcm1v5_max)) errorcode |= 0x000400; if ((v_mcm2v5 < _vmcm2v5_min) || (v_mcm2v5 > _vmcm2v5_max)) errorcode |= 0x000800; if ((c_1v5 < _c1v5_3_min) || (c_1v5 > _c1v5_3_max)) errorcode |= 0x001000; if ((c_2v5 < _c2v5_3_min) || (c_2v5 > _c2v5_3_max)) errorcode |= 0x002000; break; } } if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%08X\n", datestring,expectedaddress,cpldid,action,readadc,v_1v5,c_1v5,c_1v5_adc,v_2v5,c_2v5,c_2v5_adc,v_mcm1v5,v_mcm2v5,settings,errorcode); } printf("%s C1V5-%d C2V5-%d V1V5-%d V2V5-%d MCM1V5-%d MCM2V5-%d E-%08X\n",cmdhead,c_1v5,c_2v5,v_1v5,v_2v5,v_mcm1v5,v_mcm2v5,errorcode); headfound = 0; foundflag = 0; trailfound = 1; nbtrails++; nberrors |= errorcode; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } // if (strncmp(cmdtrail,ptr,5)) //printf("%d %X %d %s %s\n",headfound,foundflag,trailfound,ptr,cmdtrail); //else puts("FOUND"); ptr = strtok(NULL,"\n"); } if (logdata != 0) fclose(fcsv); if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: errorcode %08X\n",__FUNCTION__,nberrors); return nberrors; if (errorcode != 0) { printf("%s: ERROR %08X %08X\n",__FUNCTION__,errorcode,foundflag); } return errorcode; } int switchonoffMCM(int devfd,int onoff) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; unsigned int errorcode = 0; unsigned int nberrors = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int expectedaddress; if (onoff == LVCARD_SWITCH_OFF) { sprintf(cmd,"M%02dVD\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: Switch off failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } } else if (onoff == LVCARD_SWITCH_ON) { sprintf(cmd,"M%02dVE\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: Switch on failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } } if (debug == 1) puts(replybuf); int n; char *ptr; char tmpbuf[512]; strncpy(tmpbuf,replybuf,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,power,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); errorcode = 0; trailfound = 1; while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (!strncmp(cmdhead,ptr,4)) { headfound = 1; nbheads++; if (trailfound == 0) errorcode |= 8; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (headfound == 0) errorcode |= 4; nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%d,%08X\n", datestring,expectedaddress,cpldid,onoff,power,settings,errorcode); } printf("%s E-%08X\n",cmdhead,errorcode); headfound = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: action %d errorcode %08X\n",__FUNCTION__,onoff,nberrors); return nberrors; } int initMCM(int devfd) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; unsigned int errorcode = 0; unsigned int nberrors = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int expectedaddress; sprintf(cmd,"M%02dDI\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: DAC init failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } if (debug == 1) puts(replybuf); int n; char *ptr; char tmpbuf[512]; strncpy(tmpbuf,replybuf,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,decay,refn,refp,refcm,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); errorcode = 0; trailfound = 1; while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (!strncmp("DI?",ptr,3) && headfound) { errorcode |= 0x80; } else if (!strncmp(cmdhead,ptr,4)) { headfound = 1; nbheads++; if (trailfound == 0) errorcode |= 8; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (headfound == 0) errorcode |= 4; nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%d,%d,%d,%d,%08X\n", datestring,expectedaddress,cpldid,LVCARD_MCM_INIT,decay,refn,refp,refcm,settings,errorcode); } printf("%s E-%08X\n",cmdhead,errorcode); headfound = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: action %d errorcode %08X\n",__FUNCTION__,LVCARD_MCM_INIT,nberrors); return nberrors; } int dacreadMCM(int devfd) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; unsigned int errorcode = 0; unsigned int nberrors = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int foundflag = 0; int expectedaddress; // Send command to read temperature on LVCARD sprintf(cmd,"M%02dDA\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: DAC read all failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } if (debug == 1) puts(replybuf); int n; char *ptr; char tmpbuf[512]; strncpy(tmpbuf,replybuf,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,readdac,decay,refn,refp,refcm,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); errorcode = 0; trailfound = 1; foundflag = 0; dacdecay = dacrefn = daccm = dacrefp = 0; while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (!strncmp("DECAY:",ptr,6) && headfound) { sscanf(ptr,"DECAY:%d",&dacdecay); foundflag |= 1; } else if (!strncmp("REFN:",ptr,5) && headfound) { sscanf(ptr,"REFN:%d",&dacrefn); foundflag |= 2; } else if (!strncmp("CM:",ptr,3) && headfound) { sscanf(ptr,"CM:%d",&daccm); foundflag |= 4; } else if (!strncmp("REFP:",ptr,5) && headfound) { sscanf(ptr,"REFP:%d",&dacrefp); foundflag |= 8; } else if (!strncmp(cmdhead,ptr,4)) { headfound = 1; nbheads++; if (trailfound == 0) errorcode |= 8; foundflag = 0; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (foundflag != 0x0F) errorcode |= 2; if (headfound == 0) errorcode |= 4; if (dacdecay != decay) errorcode |= 0x100; if (dacrefn != refn) errorcode |= 0x200; if (dacrefp != refp) errorcode |= 0x400; if (daccm != refcm) errorcode |= 0x800; nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%d,%d,%d,%d,%d,%08X\n", datestring,expectedaddress,cpldid,LVCARD_MCM_DACRD,readdac,dacdecay,dacrefn,dacrefp,daccm,settings,errorcode); } printf("%s decay-%d(%d) refn-%d(%d) cm-%d(%d) refp-%d(%d) E-%08X\n",cmdhead,dacdecay,decay,dacrefn,refn,daccm,refcm,dacrefp,refp,errorcode); headfound = 0; foundflag = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (logdata != 0) fclose(fcsv); if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: errorcode %08X\n",__FUNCTION__,nberrors); return nberrors; } int dacwriteMCM(int devfd) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; int valuewr,valuerd; int dacnb; int n; char *ptr; char tmpbuf[512]; unsigned int errorcode = 0; unsigned int nberrors = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int expectedaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,dacnb,valuewr,dacnb,valuerd,settings,errorcode\n"); } } // decay,refp,refcm,refn for ( int i = 0; i < 4; i++) { if (setdac & (1< _lvcard_maxtemp) errorcode |= 0x100; } nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%d,%d,%08X\n", datestring,expectedaddress,cpldid,LVCARD_READ_TEMP,rdlvtemp,temperature,settings,errorcode); } printf("%s T-%d E-%08X\n",cmdhead,temperature,errorcode); headfound = 0; tempfound = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (logdata != 0) fclose(fcsv); if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: errorcode %08X\n",__FUNCTION__,nberrors); return nberrors; } int tempreadMCM(int devfd) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; unsigned int errorcode = 0; unsigned int nberrors = 0; int headfound = 0; int trailfound = 0; int nbheads = 0; int nbtrails = 0; int tempfound = 0; float temperature; int expectedaddress; // Send command to read temperature on MCM sprintf(cmd,"M%02dTR\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: Tempread failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } if (debug == 1) puts(replybuf); int n; char *ptr; char tmpbuf[512]; strncpy(tmpbuf,replybuf,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,rdmcmtemp,temp,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); errorcode = 0; trailfound = 1; while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (!strncmp("TR:",ptr,3) && headfound) { sscanf(ptr,"TR:%f",&temperature); tempfound = 1; } if (!strncmp("TR?",ptr,3) && headfound) { errorcode |= 0x80; } else if (!strncmp(cmdhead,ptr,4)) { headfound = 1; nbheads++; if (trailfound == 0) errorcode |= 8; tempfound = 0; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (tempfound == 0) errorcode |= 2; if (headfound == 0) errorcode |= 4; if (tempfound == 1) { if (temperature > _mcm_maxtemp) errorcode |= 0x100; } nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%d,%7.3f,%d,%08X\n", datestring,expectedaddress,cpldid,LVCARD_MCM_TEMP,rdmcmtemp,temperature,settings,errorcode); } printf("%s T-%.3f E-%08X\n",cmdhead,temperature,errorcode); headfound = 0; tempfound = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (logdata != 0) fclose(fcsv); if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: errorcode %08X\n",__FUNCTION__,nberrors); return nberrors; } unsigned int versionLvcard(int devfd) { char cmd[64],cmdhead[64],cmdtrail[64]; int iret; unsigned int errorcode = 0; unsigned int nberrors = 0; int nbheads = 0; int nbtrails = 0; int headfound = 0; int trailfound = 0; int versionfound = 0; int id; int voltage; char version[64]; int expectedaddress; // Send command to read temperature on LVCARD sprintf(cmd,"M%02d?\n",mcmaddress); if ((iret = sendcommand(devfd,cmd,replybuf)) != 0) { printf("%s: Versionread failed %d: %s - EXITING\n",__FUNCTION__,iret,replybuf); return 1; } if (debug == 1) puts(replybuf); int n; char *ptr; char tmpbuf[512]; strncpy(tmpbuf,replybuf,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command // if broadcast then we expect all addresses to reply if (mcmaddress == 0 ) expectedaddress = 1; else expectedaddress = mcmaddress; // Open csv file - append if exist else create new file with header line FILE *fcsv; if (logdata != 0) { struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (!csvexist) fprintf(fcsv,"date,time,mcm,cpld,action,version,id,voltage,settings,errorcode\n"); } } // header line and trailer to expect sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); errorcode = 0; trailfound = 1; versionfound = 0; while (ptr != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (!strncmp("VERSION:",ptr,8) && headfound) { sscanf(ptr,"VERSION:%s",version); versionfound |= 1; } else if (!strncmp("ID:",ptr,3) && headfound) { sscanf(ptr,"ID:%04X",&id); versionfound |= 2; } else if (!strncmp("VOLTAGE:",ptr,8) && headfound) { sscanf(ptr,"VOLTAGE:%d",&voltage); versionfound |= 4; } else if (!strncmp(cmdhead,ptr,4)) { nbheads++; headfound = 1; if (trailfound == 0) errorcode |= 8; versionfound = 0; trailfound = 0; } else if (!strncmp(cmdtrail,ptr,5)) { if (versionfound != 7) errorcode |= 2; if (headfound == 0) errorcode |= 4; nberrors |= errorcode; if (logdata != 0) { fprintf(fcsv,"%s,%2X,%4X,%d,%s,%04X,%d,%d,%08X\n", datestring,expectedaddress,cpldid,action,version,id,voltage,settings,errorcode); } printf("%s V-%s ID-%04X V-%d E-%08X\n",cmdhead,version,id,voltage,errorcode); headfound = 0; versionfound = 0; trailfound = 1; nbtrails++; errorcode = 0; // next header line and trailer to expect expectedaddress++; sprintf(cmdhead,"M%02d:",expectedaddress); sprintf(cmdtrail,"M%02dOK",expectedaddress); } ptr = strtok(NULL,"\n"); } if (logdata != 0) fclose(fcsv); if (nbheads == 0) nberrors |= 0x10; if (nbtrails == 0) nberrors |= 0x20; if (nbheads != nbtrails) nberrors |= 0x40; if (nberrors != 0) printf("%s: errorcode %08X\n",__FUNCTION__,nberrors); return nberrors; } int flushDev(int devfd) { char reply[500]; int iret; int pos = 0; do { iret=read(devfd,&reply[pos],sizeof(reply)); if ( (iret != -1) && iret ) { pos += iret; reply[pos] = '\0'; // printf("%d %d %s\n",iret,pos,reply); } } while(iret > 0); return pos; } int main(int argc, char *argv[]) { time_t ticks; struct tm *mytime; int devfd; int iret = 0; int nberrors = 0; if (getParameters(argc,argv) != 0) exit(1); // printf("ACTION: %d, logdata%d settings %d\n",action,logdata,settings); // if (logdata != 0) printf("CSVFILE: %s\n",csvfile); ticks = time(NULL); mytime = localtime(&ticks); strftime(datestring,sizeof(datestring),"%Y%m%d,%H%M%S",mytime); // printf("TIME %s\n",datestring); // Open device devfd = open(mydev,O_RDWR | O_NONBLOCK); if (devfd < 0 ) { printf("OPEN device %s returned %d error %s\n",mydev,devfd,strerror(errno)); return (3); } iret = flushDev(devfd); do { if (nbloops != 1) printf("LOOP %d SLEEP %d\n",nbloops,mssleep); if (action == LVCARD_READADC) { iret = readadcLvcard(devfd); if (debug != 0) printf("READADC %d\n",iret); } else if (action == LVCARD_SWITCH_ON) { iret = switchonoffMCM(devfd,action); if (debug != 0) printf("SWITCH ON %d\n",iret); } else if (action == LVCARD_SWITCH_OFF) { iret = switchonoffMCM(devfd,action); if (debug != 0) printf("SWITCH OFF %d\n",iret); } else if (action == LVCARD_MCM_INIT) { iret = initMCM(devfd); if (debug != 0) printf("MCMINIT %d\n",iret); } else if (action == LVCARD_MCM_DACRD) { iret = dacreadMCM(devfd); if (debug != 0) printf("MCMDACRD %d\n",iret); } else if (action == LVCARD_MCM_DACWR) { iret = dacwriteMCM(devfd); if (debug != 0) printf("MCMDACWR %d\n",iret); } else if (action == LVCARD_MCM_TEMP) { iret = tempreadMCM(devfd); if (debug != 0) printf("MCMTEMPRD %d\n",iret); } else if (action == LVCARD_READ_TEMP) { iret = tempreadLvcard(devfd); if (debug != 0) printf("LVCARD %d TEMPRD %d\n",mcmaddress,iret); } else if (action == LVCARD_VERSION) { iret = versionLvcard(devfd); if (debug != 0) printf("LVCARD %d VERSION %d\n",mcmaddress,iret); } else { printf("Unkown action: %d\n",action); iret = 1; } if (nbloops != 1) usleep(mssleep * 1000); if (iret != 0 ) nberrors++; } while (--nbloops); close(devfd); printf("Number of errors %d\n",nberrors); return iret; }