/****************************************************************************** * * Title : commandFunc_readWriteVME.c * Version 0.0 * * Description: Reads/writes data from/to VME memory space. * Related files: * * Author: Lukas Tomasek, tomasekl@fzu.cz * ******************************************************************************/ /****************************************************************************** * Header files * ******************************************************************************/ #include #include #include "commandListDefinitions.h" #include "commandFunc_readWriteVME.h" #include "commandParamsUir.h" #include "globalDefinitions.h" #include "uirUtility.h" #include "mainUir.h" #include "vmeHpiUtility.h" #include "commandStatusMessage.h" #include "RWlists.h" /****************************************************************************** * Global functions * ******************************************************************************/ /*============================================================================= * commandFunction_readWriteVme() *============================================================================= * * * */ ERROR_ID commandFunction_readWriteVME(COMMAND_FUNC_OPTION funcOption, struct COMMAND *command, UINT8 slotNumber, FILE *file){ struct HOST *host; const int panel=global.panel.commandEdit[RW_VME_COMMAND_ID]; struct RW_VME_PARAMS *RWVme=&command->params.readWriteVme; int status; ERROR_ID errorId=SUCCESS; long fileSize; int option; UINT32 *buffer; char subdir[PATHNAME_LENGTH]; UINT32 vmeAddress; char inputText[500]; switch(funcOption){ case COMMAND_TO_LIST: GetCtrlVal(panel, RW_VMEP_VME_ACCESS, &RWVme->elementSize); GetCtrlVal(panel, RW_VMEP_READ_NOT_WRITE, &RWVme->readNotWrite); GetCtrlVal(panel, RW_VMEP_START_ADDR, &RWVme->startAddress); GetCtrlVal(panel, RW_VMEP_LENGTH, &RWVme->length); GetCtrlVal(panel, RW_VMEP_NO_BLOCK_ACCESS, &RWVme->noBlockAccess); if(RWVme->length==0) { status = MessagePopup ("WARNING", "DataLength==0!!"); UIR_STATUS_CHECK(status, MessagePopup()); return(PROGRAM_WARNING); } if(RWVme->readNotWrite==0){ /* write */ GetCtrlVal(panel, RW_VMEP_INP_OPTION, &RWVme->write.dataNotFile); if(RWVme->write.dataNotFile){ /* one word */ GetCtrlVal(panel, RW_VMEP_INPUT_DATA, &RWVme->write.data); }else{ /* from file */ GetCtrlVal(panel, RW_VMEP_INP_FILE, RWVme->write.fileName); if(RWVme->write.fileName[0]==0) { status = MessagePopup ("WARNING", "No InputDataFile selected!!"); UIR_STATUS_CHECK(status, MessagePopup()); return(PROGRAM_WARNING); } } }else{ /* read */ GetCtrlVal(panel, RW_VMEP_OUTPUT_OPT, &option); if(option){ GetCtrlVal(panel, RW_VMEP_WRITE_OR_APPEND, &RWVme->read.appendNotWrite); GetCtrlVal(panel, RW_VMEP_OUT_FILE, RWVme->read.fileName); } else { RWVme->read.fileName[0]=0; } GetCtrlVal(panel, RW_VMEP_COMP_OPTION, &RWVme->read.compareWithData); GetCtrlVal(panel, RW_VMEP_COMP_DATA, &RWVme->read.compData); } break; case LIST_TO_COMMAND: SetCtrlVal(panel, RW_VMEP_VME_ACCESS, RWVme->elementSize); SetCtrlVal(panel, RW_VMEP_READ_NOT_WRITE, RWVme->readNotWrite); SetCtrlVal(panel, RW_VMEP_START_ADDR, RWVme->startAddress); SetCtrlVal(panel, RW_VMEP_LENGTH, RWVme->length); SetCtrlVal(panel, RW_VMEP_NO_BLOCK_ACCESS, RWVme->noBlockAccess); if(RWVme->readNotWrite==0){ /* write */ SetCtrlVal(panel, RW_VMEP_INP_OPTION, RWVme->write.dataNotFile); status=SetInputMode(panel, RW_VMEP_INP_OPTION, 1); UIR_STATUS_CHECK(status, SetInputMode()); if(RWVme->write.dataNotFile){ /* one word */ SetCtrlVal(panel, RW_VMEP_INPUT_DATA, RWVme->write.data); status=SetInputMode(panel, RW_VMEP_INPUT_DATA, 1); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_INP_FILE, 0); UIR_STATUS_CHECK(status, SetInputMode()); }else{ /* from file */ SetCtrlVal(panel, RW_VMEP_INP_FILE, RWVme->write.fileName); status=SetInputMode(panel, RW_VMEP_INP_FILE, 1); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_INPUT_DATA, 0); UIR_STATUS_CHECK(status, SetInputMode()); } status=SetInputMode(panel, RW_VMEP_OUTPUT_OPT, 0); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_WRITE_OR_APPEND, 0); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_OUT_FILE, 0); UIR_STATUS_CHECK(status, SetInputMode()); SetCtrlVal(panel, RW_VMEP_COMP_DATA, 0); status=SetInputMode(panel, RW_VMEP_COMP_OPTION, 0); UIR_STATUS_CHECK(status, SetInputMode()); SetCtrlVal(panel, RW_VMEP_COMP_DATA, 0); status=SetInputMode(panel, RW_VMEP_COMP_DATA, 0); UIR_STATUS_CHECK(status, SetInputMode()); }else{ /* read */ SetCtrlVal(panel, RW_VMEP_WRITE_OR_APPEND, RWVme->read.appendNotWrite); status=SetInputMode(panel, RW_VMEP_INPUT_DATA, 0); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_INP_FILE, 0); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_INP_OPTION, 0); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_WRITE_OR_APPEND, 1); UIR_STATUS_CHECK(status, SetInputMode()); status=SetInputMode(panel, RW_VMEP_OUTPUT_OPT, 1); UIR_STATUS_CHECK(status, SetInputMode()); SetCtrlVal(panel, RW_VMEP_OUT_FILE, RWVme->read.fileName); if(RWVme->read.fileName[0]==0){ SetCtrlVal(panel, RW_VMEP_OUTPUT_OPT, 0); status=SetInputMode(panel, RW_VMEP_OUT_FILE, 0); UIR_STATUS_CHECK(status, SetInputMode()); }else{ SetCtrlVal(panel, RW_VMEP_OUTPUT_OPT, 1); status=SetInputMode(panel, RW_VMEP_OUT_FILE, 1); UIR_STATUS_CHECK(status, SetInputMode()); } SetCtrlVal(panel, RW_VMEP_COMP_OPTION, RWVme->read.compareWithData); status=SetInputMode(panel, RW_VMEP_COMP_OPTION, (RWVme->length<=4)); UIR_STATUS_CHECK(status, SetInputMode()); SetCtrlVal(panel, RW_VMEP_COMP_DATA, RWVme->read.compData); status=SetInputMode(panel, RW_VMEP_COMP_DATA, RWVme->read.compareWithData); UIR_STATUS_CHECK(status, SetInputMode()); } break; case COMMAND_EXECUTION: host=global.host[HOST_INDEX(slotNumber)]; vmeAddress=BASE_ADDRESS(slotNumber)+RWVme->startAddress; if(RWVme->readNotWrite){ /* read */ buffer=malloc(RWVme->length); if(buffer==NULL){ ERROR_CHECK(PROGRAM_ERROR, malloc()); return(COMMAND_ERROR); } if(RWVme->elementSize==RWVme->length){ errorId=vmeReadElement(vmeAddress, buffer, RWVme->elementSize); ERROR_CHECK(errorId, vmeReadElement()); }else{ errorId=RWvmeBlock(READ, vmeAddress, buffer, RWVme->length/RWVme->elementSize, RWVme->elementSize, VME_ADDR_AUTOINCREMENT, RWVme->noBlockAccess, 0); ERROR_CHECK(errorId, RWvmeBlock()); } if(errorId!=SUCCESS){ free(buffer); return(COMMAND_ERROR); } if(RWVme->read.fileName[0]!=0){ if(slotNumber==TIM_SLOT_NUMBER){ strcpy(subdir, global.rodDataDir[HOST_INDEX(slotNumber)]); }else{ strcpy(subdir, host->dataDir); } strcat(subdir, RWVme->read.fileName); errorId=writeToBinFile(subdir, RWVme->read.appendNotWrite, buffer, RWVme->length); ERROR_CHECK(errorId, writeToBinFile()); if(errorId!=SUCCESS){ free(buffer); return(COMMAND_ERROR); } } if(RWVme->read.compareWithData){ /* compare data */ if(*buffer!=RWVme->read.compData){ sprintf(inputText,"##READ VME DATA COMPARISON(@VMEaddr:0x%X) - read(0x%X) and comp(0x%X) data are NOT equal!!\n", vmeAddress,*buffer, RWVme->read.compData); if(slotNumber==TIM_SLOT_NUMBER){ programError(__FILE__, __LINE__, COMMAND_ERROR, "RW_VME()", errorId, inputText); }else{ errorId=commandStatusMessage(host, host->commandStatusFile, inputText); HOST_ERROR_CHECK(errorId, host, commandStatusMessage()); } free(buffer); return(COMMAND_ERROR); } } free(buffer); /* !!! */ }else{ /* write */ if(RWVme->write.dataNotFile){ if(RWVme->elementSize==RWVme->length){ errorId=vmeWriteElement(RWVme->write.data, vmeAddress, RWVme->elementSize); ERROR_CHECK(errorId, vmeWriteElement()); }else{ errorId=RWvmeBlock(WRITE, vmeAddress, &RWVme->write.data, RWVme->length/RWVme->elementSize, RWVme->elementSize, VME_ADDR_AUTOINCREMENT, RWVme->noBlockAccess, 0); ERROR_CHECK(errorId, RWvmeBlock()); } if(errorId!=SUCCESS){ return(COMMAND_ERROR); } }else{ buffer=malloc(RWVme->length); if(buffer==NULL){ ERROR_CHECK(PROGRAM_ERROR, malloc()); return(COMMAND_ERROR); } errorId=readFromBinFile(RWVme->write.fileName, buffer, RWVme->length); ERROR_CHECK(errorId, readFromBinFile()); if(errorId!=SUCCESS){ free(buffer); return(COMMAND_ERROR); } if(RWVme->elementSize==RWVme->length){ errorId=vmeWriteElement(*buffer, vmeAddress, RWVme->elementSize); ERROR_CHECK(errorId, vmeWriteElement()); if(errorId!=SUCCESS){ free(buffer); return(COMMAND_ERROR); } }else{ errorId=RWvmeBlock(WRITE, vmeAddress, buffer, RWVme->length/RWVme->elementSize, RWVme->elementSize, VME_ADDR_AUTOINCREMENT, RWVme->noBlockAccess, 0); ERROR_CHECK(errorId, RWvmeBlock()); if(errorId!=SUCCESS){ free(buffer); return(COMMAND_ERROR); } } free(buffer); /* !!! */ } if(vmeAddress==BASE_ADDRESS(host->slotNumber)+FPGA_CONTROL_2_REL_ADDR){ /* board reset; wait for VME init */ Sleep(1000); errorId=setHpicMaster(host->slotNumber); HOST_ERROR_CHECK(errorId, host, setHpicMaster()); if(errorId!=SUCCESS) return(COMMAND_ERROR); } } break; case SAVE_CMD_TO_FILE: fprintf(file, "elementSize= %d, noBlockAccess= %d, readNotWrite= %d, startAddress= 0x%X, length= 0x%X\n", RWVme->elementSize, RWVme->noBlockAccess, RWVme->readNotWrite, RWVme->startAddress, RWVme->length); if(RWVme->readNotWrite==0){ /* write */ fprintf(file, "dataNotFile= %d, data= 0x%X\n", RWVme->write.dataNotFile, RWVme->write.data); fprintf(file, "fileName[]= %s\n", RWVme->write.fileName); }else{ /* read */ fprintf(file, "compareWithData= %d, compData= 0x%X, appendNotWrite= %d\n", RWVme->read.compareWithData, RWVme->read.compData, RWVme->read.appendNotWrite); fprintf(file, "fileName[]= %s\n", RWVme->read.fileName); } break; case LOAD_CMD_FROM_FILE: status=fscanf(file, "elementSize= %d, noBlockAccess= %d, readNotWrite= %d, startAddress= 0x%X, length= 0x%X\n", (int*)&RWVme->elementSize, (int*)&RWVme->noBlockAccess, &RWVme->readNotWrite, &RWVme->startAddress, &RWVme->length); if(status==-1) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} if(RWVme->readNotWrite==0){ /* write */ status=fscanf(file, "dataNotFile= %d, data= 0x%X\n", &RWVme->write.dataNotFile, &RWVme->write.data); if(status==-1) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} status=readLineFromIndex(file, strlen("fileName[]= "), PATHNAME_LENGTH, RWVme->write.fileName); if(status!=0) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} }else{ /* read */ status=fscanf(file, "compareWithData= %d, compData= 0x%X, appendNotWrite= %d\n", &RWVme->read.compareWithData, &RWVme->read.compData, &RWVme->read.appendNotWrite); if(status==-1) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} status=readLineFromIndex(file, strlen("fileName[]= "), PATHNAME_LENGTH, RWVme->read.fileName); if(status!=0) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} } break; default: ; } return(SUCCESS); } /******************************************************************************/