/**************************************************** * RFControlBoard_main.c * * Source file for the RFControlBoard * * Created by: Zheqiao Geng, gengzq@slac.stanford.edu * Created on: 2011.04.12 * Description: Initial creation * * Modified by: Zheqiao Geng * Modified on: 2011.07.05 * Description: Use the virtual function to access the hardware * * Modified by: Zheqiao Geng * Modified on: 2/26/2013 * Description: Extend the data definition to fit into the EICSYS firmware ****************************************************/ #include #include #include #include #include "RFControlBoard_main.h" /*====================================== * Public Routines *======================================*/ /** * Construction of the instance of the module * Input: * arg : Data structure of the module instance * moduleName : Name of the module instance * Return: * 0 : Successful * -1 : Failed */ int RFCB_func_createModule(RFCB_struc_moduleData *arg, const char *moduleName) { /* Check the input */ if(!arg || !moduleName || !moduleName[0]) return -1; /* clean the buffer */ memset((void *)arg, 0, sizeof(RFCB_struc_moduleData)); /* Init the data structure */ strcpy(arg -> moduleName, moduleName); arg -> mutexCreated = 1; /* the local mutex is created here */ arg -> localMutex = EPICSLIB_func_mutexMustCreate(); return 0; } /** * Distruction of the instance of the module * Input: * arg : Data structure of the module instance * Return: * 0 : Successful * -1 : Failed */ int RFCB_func_destroyModule(RFCB_struc_moduleData *arg) { int i; /* Check the input */ if(!arg) return -1; /* Close the device */ for(i = 0; i < RFCB_CONST_MAX_DEV_NUM; i ++) { if(arg -> deviceOpened[i] && arg -> deviceFp[i]) { RFCB_func_closeBoard(arg, (RFCB_enum_devSpace)i); arg -> deviceOpened[i] = 0; } } /* delete the mutex */ if(arg -> mutexCreated && arg -> localMutex) { EPICSLIB_func_mutexDestroy(arg -> localMutex); arg -> mutexCreated = 0; } /* unmap the kernel DMA buffer if necessary */ if(arg -> DMADataPool && arg -> hwFunc.RFCB_func_unMapDMADataPool) arg -> hwFunc.RFCB_func_unMapDMADataPool(arg -> deviceFp[2],arg -> DMADataPool); return 0; } /** * Set the device name * Input: * arg : Data structure of the module instance * deviceName : Device name to be connected to * dev : selection of the device to be set * Return: * 0 : Successful * -1 : Failed */ int RFCB_func_setDeviceName(RFCB_struc_moduleData *arg, const char *deviceName, RFCB_enum_devSpace dev) { int devID = (int)dev; /* Check the input */ if(!arg || !deviceName ||!deviceName[0]) return -1; /* close the device if it is already opened */ if(arg -> deviceOpened[devID] && arg -> deviceFp[devID]) { RFCB_func_closeBoard(arg, dev); arg -> deviceOpened[devID] = 0; } /* save the device name and open it */ strcpy(arg -> deviceName[devID], deviceName); RFCB_func_openBoard(arg, deviceName, dev); if(!arg -> deviceFp[devID]) { arg -> deviceOpened[devID] = 0; return -1; } else { arg -> deviceOpened[devID] = 1; /* map the DMA kernel buffer to user space */ if(dev == RFCB_DEV_DMA) { if(RFCB_func_setupDMA(arg, 0, RFCB_EICSYS_CONST_DMA_POOL_SIZE) != 0) return -1; /* set the DMA to full size first */ } } return 0; } /** * Set the firmware file name * Input: * arg : Data structure of the module instance * firmwareFileName : Full path of the firmware file * Return: * 0 : Successful * -1 : Failed */ int RFCB_func_setFirmwareFile(RFCB_struc_moduleData *arg, const char *firmwareFileName) { /* Check the input */ if(!arg || !firmwareFileName ||!firmwareFileName[0]) return -1; /* Save the firmware file name (without automatic configuring the FPGA for safety) */ strcpy(arg -> firmwareFileName, firmwareFileName); return 0; } /** * Open the board * Input: * arg : Data structure of the module instance * deviceName : Device name in /dev folder * dev : selection of the device to be set */ void RFCB_func_openBoard(RFCB_struc_moduleData *arg, const char *deviceName, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg) return; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_open) arg -> deviceFp[devID] = arg -> hwFunc.RFCB_func_open(deviceName); EPICSLIB_func_mutexUnlock(arg -> localMutex); } /** * Close the board * Input: * arg : Data structure of the module instance * dev : selection of the device to be set */ void RFCB_func_closeBoard(RFCB_struc_moduleData *arg, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg) return; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_close) arg -> hwFunc.RFCB_func_close(arg -> deviceFp[devID]); EPICSLIB_func_mutexUnlock(arg -> localMutex); } /** * Read register from the board * Input: * arg : Data structure of the module instance * addr : Address of the register * dev : selection of the device to be used * Output: * data : Data in the register */ void RFCB_func_readRegister(RFCB_struc_moduleData *arg, unsigned int addr, unsigned int *data, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg || !data) return; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_readReg) arg -> hwFunc.RFCB_func_readReg(arg -> deviceFp[devID], addr, data); EPICSLIB_func_mutexUnlock(arg -> localMutex); } /** * Write register to the board * Input: * arg : Data structure of the module instance * addr : Address of the register * data : Data to be written in the register * dev : selection of the device to be used */ void RFCB_func_writeRegister(RFCB_struc_moduleData *arg, unsigned int addr, unsigned int data, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg) return; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_writeReg) arg -> hwFunc.RFCB_func_writeReg(arg -> deviceFp[devID], addr, data); EPICSLIB_func_mutexUnlock(arg -> localMutex); } /** * Read buffer from the board * Input: * arg : Data structure of the module instance * addr : Address of the buffer * pno : Number of the point in the buffer * dev : selection of the device to be used * Output: * data : Data in the buffer */ void RFCB_func_readBuffer(RFCB_struc_moduleData *arg, unsigned int addr, unsigned int pno, unsigned int *data, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg || !data || pno < 1) return; /* set up the transfer, and remap the memory */ if(dev == RFCB_DEV_DMA) { if(RFCB_func_setupDMA(arg, addr, pno * 4) != 0) return; /* please not that the pno is for 32-bit data */ } /* get the data */ EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_readBuf) arg -> hwFunc.RFCB_func_readBuf(arg -> deviceFp[devID], addr, pno, data); EPICSLIB_func_mutexUnlock(arg -> localMutex); /* copy the data out for EICSYS firmware */ if(arg -> DMADataPool) memcpy((void *)data, arg -> DMADataPool, pno * sizeof(unsigned int)); /* print the data for debugging */ /*int i; unsigned int data_print; printf("-------------------------------\n"); printf("The data is:\n"); for(i = 0; i < pno; i++) { data_print = *((unsigned int*)arg -> DMADataPool + i); printf("0x%4X \n", data_print); }*/ } /** * Write buffer to the board * Input: * arg : Data structure of the module instance * addr : Address of the buffer * pno : Number of the point in the buffer * data : Data to be written to the buffer * dev : selection of the device to be used */ void RFCB_func_writeBuffer(RFCB_struc_moduleData *arg, unsigned int addr, unsigned int pno, unsigned int *data, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg || !data || pno < 1) return; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_writeBuf) arg -> hwFunc.RFCB_func_writeBuf(arg -> deviceFp[devID], addr, pno, data); EPICSLIB_func_mutexUnlock(arg -> localMutex); } /** * Configue the FPGA * Input: * arg : Data structure of the module instance */ int RFCB_func_configureFPGA(RFCB_struc_moduleData *arg) { int status; if(!arg) return -1; EPICSLIB_func_mutexMustLock(arg -> localMutex); if(arg -> hwFunc.RFCB_func_configFPGA) status = arg -> hwFunc.RFCB_func_configFPGA(arg -> deviceFp[0], arg -> firmwareFileName); EPICSLIB_func_mutexUnlock(arg -> localMutex); return status; } /** * Pull the interrupt. When calling this routine, the caller will be suspended until the interrupt comes * Input: * arg : Data structure of the module instance * dev : the device to be used (SYS for struck driver and DMA for EICSYS driver) * return: * 0: user interrupt * 1: DMA interrupt * -1: failure */ int RFCB_func_pullInterrupt(RFCB_struc_moduleData *arg, RFCB_enum_devSpace dev) { int devID = (int)dev; int status; if(!arg) return -1; /*EPICSLIB_func_mutexMustLock(arg -> localMutex);*/ if(arg -> hwFunc.RFCB_func_pullInt) status = arg -> hwFunc.RFCB_func_pullInt(arg -> deviceFp[devID]); /*EPICSLIB_func_mutexUnlock(arg -> localMutex);*/ return status; } /** * Get the status of the module */ void RFCB_func_getModuleStatus(RFCB_struc_moduleData *arg, char *deviceName, int *deviceOpened, RFCB_enum_devSpace dev) { int devID = (int)dev; if(!arg || !deviceName || !deviceOpened) return; strcpy(deviceName, arg -> deviceName[devID]); *deviceOpened = arg -> deviceOpened[devID]; } /** * Setup the DMA (redo the map because the DMA transfer size may be different). This is a specific function for EICSYS driver on DMA device */ int RFCB_func_setupDMA(RFCB_struc_moduleData *arg, unsigned int offset, unsigned int size) { if(!arg) return -1; /* unmap if already mapped */ if(arg -> DMADataPool && arg -> hwFunc.RFCB_func_unMapDMADataPool) arg -> hwFunc.RFCB_func_unMapDMADataPool(arg -> deviceFp[2], arg -> DMADataPool); /* setup new DMA parameters */ if(arg -> hwFunc.RFCB_func_setDMAParam) { if(arg -> hwFunc.RFCB_func_setDMAParam(arg -> deviceFp[2], offset, size) != 0) return -1; } /* do the map again */ if(arg -> hwFunc.RFCB_func_mapDMADataPool) arg -> DMADataPool = arg -> hwFunc.RFCB_func_mapDMADataPool(arg -> deviceFp[2]); if(arg -> DMADataPool) return 0; else return -1; } /** * Get the pointer of the DMA data pool and the size of the transfer */ void* RFCB_func_getDMADataPoolPtr(RFCB_struc_moduleData *arg, unsigned int *DMADataTransferSize) { if(!arg || !DMADataTransferSize) return NULL; if(arg -> hwFunc.RFCB_func_getDMAParam) { if(arg -> hwFunc.RFCB_func_getDMAParam(arg -> deviceFp[2], DMADataTransferSize) != 0) return NULL; } return arg -> DMADataPool; }