/********************************************************************* Name: uov1740.c Created by: Pierre-A. Amaudruz / K.Olchanski implementation of the CAEN_DGTZLib functions Contents: V1740 64ch. 12bit 50Msps for Optical link $Id$ *********************************************************************/ #include #include #include #include #include "uov1740drv.h" #define LARGE_NUMBER 10000000 // Buffer organization map for number of samples uint32_t V1740_NSAMPLES_MODE[11] = { (1<<20), (1<<19), (1<<18), (1<<17), (1<<16), (1<<15) ,(1<<14), (1<<13), (1<<12), (1<<11), (1<<10)}; /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupSet(int handle, uint32_t channel, uint32_t what, uint32_t that) { uint32_t reg; reg = what | (channel << 8); return CAEN_DGTZ_WriteRegister(handle, reg, (that & 0xFFF)); } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupGet(int handle, uint32_t channel, uint32_t what, uint32_t *data) { uint32_t reg; reg = what | (channel << 8); return CAEN_DGTZ_ReadRegister(handle, reg, data); } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupThresholdSet(int handle, uint32_t channel, uint32_t threshold) { uint32_t reg; reg = V1740_GROUP_THRESHOLD | (channel << 8); printf("reg:0x%x, threshold:%x\n", reg, threshold); return CAEN_DGTZ_WriteRegister(handle, reg,(threshold & 0xFFF)); } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupDACSet(int handle, uint32_t channel, uint32_t dac) { uint32_t reg, status, ncount; if ((channel >= 0) && (channel < 8)) { reg = V1740_GROUP_STATUS | (channel << 8); ncount = 10000; do { CAEN_DGTZ_ReadRegister(handle, reg, &status); } while ((status & 0x04) && (ncount--)); if (ncount == 0) return -1; reg = V1740_GROUP_DAC | (channel << 8); printf("reg:0x%x, DAC:%x\n", reg, dac); return CAEN_DGTZ_WriteRegister(handle, reg, (dac & 0xFFFF)); } return -1; } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupDACGet(int handle, uint32_t channel, uint32_t *dac) { uint32_t reg; CAEN_DGTZ_ErrorCode sCAEN = -1; if ((channel >= 0) && (channel < 8)) { reg = V1740_GROUP_DAC | (channel << 8); sCAEN = CAEN_DGTZ_ReadRegister(handle, reg, dac); } return sCAEN; } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_AcqCtl(int handle, uint32_t operation) { uint32_t reg; CAEN_DGTZ_ErrorCode sCAEN; sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_ACQUISITION_CONTROL, ®); // printf("sCAEN:%d ACQ Acq Control:0x%x\n", sCAEN, reg); switch (operation) { case V1740_RUN_START: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, (reg | 0x4)); break; case V1740_RUN_STOP: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, (reg & ~( 0x4))); break; case V1740_REGISTER_RUN_MODE: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x0); break; case V1740_SIN_RUN_MODE: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x1); break; case V1740_SIN_GATE_RUN_MODE: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x2); break; case V1740_MULTI_BOARD_SYNC_MODE: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x3); break; case V1740_COUNT_ACCEPTED_TRIGGER: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, (reg & ~( 0x8))); break; case V1740_COUNT_ALL_TRIGGER: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, (reg | 0x8)); break; default: printf("operation not defined\n"); break; } return sCAEN; } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_GroupConfig(int handle, uint32_t operation) { CAEN_DGTZ_ErrorCode sCAEN; uint32_t reg, cfg; sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_GROUP_CONFIG, ®); sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_GROUP_CONFIG, &cfg); // printf("Group_config1: 0x%x\n", cfg); switch (operation) { case V1740_TRIGGER_UNDERTH: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_GROUP_CFG_BIT_SET, 0x40); break; case V1740_TRIGGER_OVERTH: sCAEN = CAEN_DGTZ_WriteRegister(handle, V1740_GROUP_CFG_BIT_CLR, 0x40); break; default: break; } sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_GROUP_CONFIG, &cfg); // printf("Group_config2: 0x%x\n", cfg); return sCAEN; } /*****************************************************************/ CAEN_DGTZ_ErrorCode ov1740_info(int handle, int *nchannels, uint32_t *data) { CAEN_DGTZ_ErrorCode sCAEN; int i, chanmask; uint32_t reg; // Evaluate the event size // Number of samples per channels sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_BUFFER_ORGANIZATION, ®); *data = V1740_NSAMPLES_MODE[reg]; // times the number of active channels sCAEN = CAEN_DGTZ_ReadRegister(handle, V1740_GROUP_EN_MASK, ®); chanmask = 0xff & reg; *nchannels = 0; for (i=0;i<8;i++) { if (chanmask & (1<Mode 1 : @param *mvme VME structure @param base Module base address @param mode Configuration mode number @return 0: OK. -1: Bad */ CAEN_DGTZ_ErrorCode ov1740_Setup(int handle, int mode) { switch (mode) { case 0x0: printf("--------------------------------------------\n"); printf("Setup Skip\n"); printf("--------------------------------------------\n"); case 0x1: printf("--------------------------------------------\n"); printf("Trigger from FP, 8ch, 1Ks, postTrigger 800\n"); printf("--------------------------------------------\n"); CAEN_DGTZ_WriteRegister(handle, V1740_BUFFER_ORGANIZATION, 0x0A); // 1K buffer CAEN_DGTZ_WriteRegister(handle, V1740_TRIG_SRCE_EN_MASK, 0x4000); // External Trigger CAEN_DGTZ_WriteRegister(handle, V1740_GROUP_EN_MASK, 0xFF); // 8ch enable CAEN_DGTZ_WriteRegister(handle, V1740_POST_TRIGGER_SETTING, 800); // PreTrigger (1K-800) CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x00); // Reset Acq Control printf("\n"); break; case 0x2: printf("--------------------------------------------\n"); printf("Trigger from LEMO\n"); printf("--------------------------------------------\n"); CAEN_DGTZ_WriteRegister(handle, V1740_BUFFER_ORGANIZATION, 1); printf("\n"); break; case 0x3: printf("--------------------------------------------\n"); printf("Trigger from FP, 8ch, 1Ks, postTrigger 800\n"); printf("--------------------------------------------\n"); CAEN_DGTZ_WriteRegister(handle, V1740_BUFFER_ORGANIZATION, 0x0A); // 1K buffer CAEN_DGTZ_WriteRegister(handle, V1740_TRIG_SRCE_EN_MASK, 0x4000); // External Trigger CAEN_DGTZ_WriteRegister(handle, V1740_GROUP_EN_MASK, 0xFF); // 8ch enable CAEN_DGTZ_WriteRegister(handle, V1740_POST_TRIGGER_SETTING, 800); // PreTrigger (1K-800) CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x00); // Reset Acq Control CAEN_DGTZ_WriteRegister(handle, V1740_ACQUISITION_CONTROL, 0x40); // External Clock source printf("\n"); break; default: printf("Unknown setup mode\n"); return -1; } return ov1740_Status(handle); } /*****************************************************************/ /*-PAA- For test purpose only */ #ifdef MAIN_ENABLE int main (int argc, char* argv[]) { CAEN_DGTZ_ErrorCode sCAEN; int handle[2]; uint32_t nw, l=0, d=0, h=0, Nh; uint32_t i, lcount, data[50000], temp, lam; int Nmodulo=10; int tcount=0, eloop=0; uint32_t *pdata, eStored, eSize; int loop, Nloop=10; int bshowData=0; int debug = 0; uint32_t pct=0, ct; struct timeval t1; int dt1, savelcount=0; /* get parameters */ /* parse command line parameters */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 'd') debug = 1; else if (strncmp(argv[i], "-s", 2) == 0) bshowData = 1; else if (argv[i][0] == '-') { if (i + 1 >= argc || argv[i + 1][0] == '-') goto usage; if (strncmp(argv[i], "-l", 2) == 0) Nloop = (atoi(argv[++i])); else if (strncmp(argv[i], "-o", 2) == 0) l = (atoi(argv[++i])); else if (strncmp(argv[i], "-b", 2) == 0) d = (atoi(argv[++i])); else if (strncmp(argv[i], "-m", 2) == 0) Nmodulo = (atoi(argv[++i])); else if (strncmp(argv[i], "-d", 2) == 0) d = (atoi(argv[++i])); } else { usage: printf("usage: ov1740 -l (loop count) \n"); printf(" -o link#\n"); printf(" -b board#\n"); printf(" -d daisy#\n"); printf(" -m modulo display\n"); printf(" -s show data\n\n"); return 0; } } printf("in ov1740\n"); #if 1 // // Open devices sCAEN = CAEN_DGTZ_OpenDevice(CAEN_DGTZ_PCIE_OpticalLink, l, d, 0, &(handle[h])); if (sCAEN != CAEN_DGTZ_Success) { handle[h] = -1; printf("CAEN_DGTZ_OpenDevice [l:%d, d:%d]: Error %d\n", l, d, sCAEN); } else { printf("Device found : Link:%d Daisy:%d Handle[%d]:%d\n", l, d, h, handle[h]); sCAEN = ov1740_Status(handle[h]); h++; } Nh = h; printf("Handles opened (%d)\n", Nh); #endif #if 0 // // Open devices for (h=0, l=0;l<1;l++) { for (d=0;d<2;d++) { // Open VME interface sCAEN = CAEN_DGTZ_OpenDevice(CAEN_DGTZ_PCIE_OpticalLink, l, d, 0, &(handle[h])); if (sCAEN != CAEN_DGTZ_Success) { handle[h] = -1; printf("CAEN_DGTZ_OpenDevice [l:%d, d:%d]: Error %d\n", l, d, sCAEN); } else { printf("Device found : Link:%d Daisy:%d Handle[%d]:%d\n", l, d, h, handle[h]); sCAEN = ov1740_Status(handle[h]); h++; } } } Nh = h; printf("Handles opened (%d)\n", Nh); #endif #if 1 for (h=0;h