/**************************************************** * RFControlBoard_availableInterface.c * * Source file for the available interface for RFControlBoard * The uplink interface and APIs are realized here * * Created by: Zheqiao Geng, gengzq@slac.stanford.edu * Created on: 2011.04.13 * Description: Initial creation * * Modified by: Zheqiao Geng * Modified on: 2011.07.05 * Description: Add a new command DRV_VERS for the RFCB_func_setupModule routine for * virtual function connection * * Modified by: Zheqiao Geng * Modified on: 2/26/2013 * Description: Extend the data definition to fit into the EICSYS firmware ****************************************************/ #include #include #include #include "RFControlBoard_availableInterface.h" /*====================================== * Global data for module management *======================================*/ /* A global list of the module instances */ static EPICSLIB_type_linkedList RFCB_gvar_moduleInstanceList; static int RFCB_gvar_moduleInstanceListInitalized = 0; /* A mutex may be required for this list ... */ /*====================================== * API Routines *======================================*/ /** * Create an instance of the module * Input: * moduleName : An unique name of the module instance * driverName : driver name string within the predefined drivers(SIS8300:EICSYS or SIS8300:STRUCK) * Return: * 0 : Successful * -1 : Failed */ int RFCB_API_createModule(const char *moduleName, const char * driverName) { RFCB_struc_moduleData *ptr_dataInstance = NULL; /* Check the input parameters */ if(!moduleName || !moduleName[0] || !driverName || !driverName[0]) { EPICSLIB_func_errlogPrintf("RFCB_API_createModule: Illegal module name or driver name\n"); return -1; } /* Check if the list initalized */ if(!RFCB_gvar_moduleInstanceListInitalized) { EPICSLIB_func_LinkedListInit(RFCB_gvar_moduleInstanceList); RFCB_gvar_moduleInstanceListInitalized = 1; } /* Create a new instance of the data structure */ ptr_dataInstance = (RFCB_struc_moduleData *)calloc(1, sizeof(RFCB_struc_moduleData)); if(ptr_dataInstance == NULL) { EPICSLIB_func_errlogPrintf("RFCB_API_createModule: Failed to create the data structure for the module of %s\n", moduleName); return -1; } /* Call the routine for the construction of the module */ if(RFCB_func_createModule(ptr_dataInstance, moduleName) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_createModule: Failed to construct the module instance of %s\n", moduleName); free(ptr_dataInstance); /* clean the garbage */ return -1; } /* connect different virtual function implementation */ if(strcmp("SIS8300:EICSYS", driverName) == 0) { ptr_dataInstance -> hwFunc.RFCB_func_open = RFCB_func_open_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_close = RFCB_func_close_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_readReg = RFCB_func_readReg_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_writeReg = RFCB_func_writeReg_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_readBuf = RFCB_func_readBuf_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_writeBuf = RFCB_func_writeBuf_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_configFPGA = RFCB_func_configFPGA_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_pullInt = RFCB_func_pullInt_sis8300_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_mapDMADataPool = RFCB_func_mapDMADataPool_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_unMapDMADataPool = RFCB_func_unMapDMADataPool_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_setDMAParam = RFCB_func_setDMAParam_eicsys; ptr_dataInstance -> hwFunc.RFCB_func_getDMAParam = RFCB_func_getDMAParam_eicsys; } else if(strcmp("SIS8300:STRUCK", driverName) == 0) { ptr_dataInstance -> hwFunc.RFCB_func_open = RFCB_func_open_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_close = RFCB_func_close_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_readReg = RFCB_func_readReg_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_writeReg = RFCB_func_writeReg_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_readBuf = RFCB_func_readBuf_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_writeBuf = RFCB_func_writeBuf_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_configFPGA = RFCB_func_configFPGA_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_pullInt = RFCB_func_pullInt_sis8300_struck; ptr_dataInstance -> hwFunc.RFCB_func_mapDMADataPool = NULL; ptr_dataInstance -> hwFunc.RFCB_func_unMapDMADataPool = NULL; ptr_dataInstance -> hwFunc.RFCB_func_setDMAParam = NULL; ptr_dataInstance -> hwFunc.RFCB_func_getDMAParam = NULL; } /* Add the successful module instance into the list */ EPICSLIB_func_LinkedListInsert(RFCB_gvar_moduleInstanceList, ptr_dataInstance -> node); return 0; } /** * Delete the module instance * Input: * moduleName : Name of the module instance to be deleted * Return: * 0 : Successful * -1 : Failed */ int RFCB_API_deleteModule(const char *moduleName) { RFCB_struc_moduleData *ptr_dataInstance = NULL; /* Check the input parameters */ if(!moduleName || !moduleName[0]) { EPICSLIB_func_errlogPrintf("RFCB_API_deleteModule: Illegal module name\n"); return -1; } /* Find the module from the list */ ptr_dataInstance = RFCB_API_getModule(moduleName); if(ptr_dataInstance == NULL) { EPICSLIB_func_errlogPrintf("RFCB_API_deleteModule: Failed to find the module of %s\n", moduleName); return -1; } /* Call the distruction routine of the module */ if(RFCB_func_destroyModule(ptr_dataInstance) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_deleteModule: Failed to delete the module of %s\n", moduleName); return -1; } /* Delete it */ EPICSLIB_func_LinkedListDelete(RFCB_gvar_moduleInstanceList, ptr_dataInstance -> node); free(ptr_dataInstance); return 0; } /** * Set up the parameters of the module instance, the command will include: * - DEV_NAME : Set the device name that this module instance will be connected to (items in /dev folder) * - DEV_NAME_SYS : device for system (EICSYS) * - DEV_NAME_USR : device for user (EICSYS) * - DEV_NAME_DMA : device for DMA (EICSYS) * - FMW_FILE : Set the FPGA firmware file * Input: * moduleName : Name of the module instance * cmd : Command listed above * dataStr : Data for the specified command * Return: * 0 : Successful * -1 : Failed */ int RFCB_API_setupModule(const char *moduleName, const char *cmd, const char *dataStr) { RFCB_struc_moduleData *ptr_dataInstance = NULL; /* Check the input parameters */ if(!moduleName || !moduleName[0]) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Illegal module name\n"); return -1; } /* Find the module instance */ ptr_dataInstance = RFCB_API_getModule(moduleName); if(ptr_dataInstance == NULL) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Failed to find the module of %s\n", moduleName); return -1; } /* Response to each command */ if(strcmp("DEV_NAME_SYS", cmd) == 0 || strcmp("DEV_NAME", cmd) == 0) { /* for usage of both drivers */ if(RFCB_func_setDeviceName(ptr_dataInstance, dataStr, RFCB_DEV_SYS) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Failed to set SYS device name\n"); return -1; } } else if(strcmp("DEV_NAME_USR", cmd) == 0) { if(RFCB_func_setDeviceName(ptr_dataInstance, dataStr, RFCB_DEV_USR) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Failed to set USR device name\n"); return -1; } } else if(strcmp("DEV_NAME_DMA", cmd) == 0) { if(RFCB_func_setDeviceName(ptr_dataInstance, dataStr, RFCB_DEV_DMA) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Failed to set DMA device name\n"); return -1; } } else if(strcmp("FMW_FILE", cmd) == 0) { if(RFCB_func_setFirmwareFile(ptr_dataInstance, dataStr) != 0) { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Failed to set firmware file name\n"); return -1; } } else { EPICSLIB_func_errlogPrintf("RFCB_API_setupModule: Invalid command\n"); return -1; } return 0; } /** * Find the module from the list * Input: * moduleName : Name of the module instance * Return: * NULL : If the module instance is not found * module addr : Successful */ RFCB_struc_moduleData *RFCB_API_getModule(const char *moduleName) { int var_moduleFound = 0; RFCB_struc_moduleData *ptr_moduleData = NULL; /* Check the input parameters */ if(!moduleName || !moduleName[0]) { EPICSLIB_func_errlogPrintf("RFCB_API_getModule: Illegal module name\n"); return NULL; } /* Look up the list */ for(ptr_moduleData = (RFCB_struc_moduleData *)EPICSLIB_func_LinkedListFindFirst(RFCB_gvar_moduleInstanceList); ptr_moduleData; ptr_moduleData = (RFCB_struc_moduleData *)EPICSLIB_func_LinkedListFindNext(ptr_moduleData -> node)) { if(strcmp(ptr_moduleData -> moduleName, moduleName) == 0) { var_moduleFound = 1; break; } } if(var_moduleFound) return ptr_moduleData; else return NULL; }