/*============================================================================= Name: devAiLlrfPad.c Abs: Device support for Derived from $EPICS/base/src/rec/aiRecord.c Auth: 18-jun-2008, Dayle Kotturi (dayle): Initial version for LLRF VME -----------------------------------------------------------------------------*/ #if 0 #include "copyright_SLAC.h" /* SLAC copyright comments */ #endif /*----------------------------------------------------------------------------- Mod: (newest to oldest) xx-xxx-2008, Dayle Kotturi: xxxxxxxxxxxxxxxxxxx =============================================================================*/ #include #include #include #include #include #include #include #include #include #include #include #include /* for SCAN_IO_EVENT */ #include #include /* for aiRecord structure */ #include /* for epicsExportAddress */ #include /* for epicsThreadSleepQuantum */ #include /* for DEBUGPRINT */ #include #include #include "drvLlrfPad.h" #include "drvPadUdpComm.h" typedef struct aiLlrf_s { int routine; int station; int channel; int sample; } aiLlrf_t; typedef struct aiLlrfDpvt_s { DrvPadUdpCommDpvtRec drvPadUdpCommPart; aiLlrf_t llrfPart; } aiLlrfDpvt_t; static long init_record(); static long read_ai(); typedef struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read_ai; DEVSUPFUN special_linconv; /* LESSON LEARNED! if you don't put this in, (even when you don't use it), you get the iocInit error that device support for init_record is missing, even though record is created and read_ai processes!!! */ } AIDPADSET; AIDPADSET devAiLlrfPad = { 6, NULL, NULL, init_record, drvPadUdpCommGetIointInfo, read_ai, NULL }; epicsExportAddress(dset,devAiLlrfPad); #ifdef DEBUG_PRINT int devAiLlrfPadFlag = 2; #endif static long init_record(aiRecord *pai) { aiLlrf_t *paip; aiLlrfDpvt_t *dpvt; /* Allocate the memory for this records private data structure */ dpvt = malloc(sizeof(*dpvt)); if (dpvt == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiLlrfPadFlag, ("devAiLlrfPad: init_ai: %s: out of memory\n", pai->name)); #endif return S_dev_noMemory; } paip = &dpvt->llrfPart; /* Tell the record about its private data structure */ pai->dpvt = dpvt; switch(pai->inp.type) { case(INST_IO): sscanf(pai->inp.value.instio.string, "%d %d %d", &(paip->routine), &(paip->station), &(paip->channel)); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devAiLlrfPadFlag, ("devAiLlrfPad: init_ai: %s: init routine with routine %d station %d channel: %d\n", pai->name, paip->routine, paip->station, paip->channel)); #endif /* Do some range checks */ if ( paip->station < 1 || paip->station > LLRF_STATION_NUM_TYPES ) { errlogPrintf("devAiLlrfPad: %s: init_record() -- station # %i out of range (1..%i)\n", pai->name, paip->station, LLRF_STATION_NUM_TYPES); pai->pact = TRUE; return -1; } paip->sample=0; switch (paip->station) { case LLRF_PCAV1: /* Station LLRF_PH1 PAD (station 9) ch0 and ch1 are for PCAV1 and ch2 and ch3 are for PCAV2 */ case LLRF_PCAV2: /* Channel 0,1,2,3 are for sample 0 and channel 4,5,6,7 are for sample 1 */ case LLRF_PCAV3: case LLRF_PCAV4: if (paip->channel >= PAD_NUM_CHANNELS_ON_BOARD) paip->sample=1; break; } paip->channel = paip->channel % PAD_NUM_CHANNELS_ON_BOARD; if ( paip->channel < 0 || paip->channel >= PAD_NUM_CHANNELS_ON_BOARD ) { errlogPrintf("devAiLlrfPad: %s: init_record() -- channel # %i out of range (1..%i)\n", pai->name, paip->channel, PAD_NUM_CHANNELS_ON_BOARD); pai->pact = TRUE; return -1; } break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiLlrfPadFlag, ("devAiLlrfPad: init_ai: %s: unrecognized link type\n", pai->name)); #endif pai->pact = TRUE; return -1; } if ( drvPadUdpCommDpvtInit( &dpvt->drvPadUdpCommPart, paip->station ) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiLlrfPadFlag, ("devAiLlrfPad: init_ai: %s: aiLlrfDpvt failed\n", pai->name)); #endif return -1; } return 0; } static long read_ai(aiRecord *pai) { aiLlrf_t *paip; float v; epicsTimeStamp *ts_p; /* Set this to facilitate easy access to channel field */ if (pai->dpvt == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiLlrfPadFlag, ("devAiLlrfPad: read_ai: %s: dpvt is NULL\n", pai->name)); #endif return -1; } paip = &((aiLlrfDpvt_t*)pai->dpvt)->llrfPart; ts_p = epicsTimeEventDeviceTime == pai->tse ? &pai->time : 0; switch (paip->routine) { case IAVG: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, IAVG); break; case QAVG: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, QAVG); break; case PHASE: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PHASE); break; case AMPLITUDE: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, AMPLITUDE); break; case POWER: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, POWER); break; case OVRFLOW: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, OVRFLOW); break; case ALIGN_START: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, ALIGN_START); break; case ALIGN_END: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, ALIGN_END); break; case MIDPOINT: v = llrfGetPadIntegerValue(paip->station, paip->channel, paip->sample, ts_p, MIDPOINT); break; case ROTATEDI: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, ROTATEDI); break; case ROTATEDQ: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, ROTATEDQ); break; case ROTPHASE: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, ROTPHASE); break; case NONROTPHASE: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, NONROTPHASE); break; case PACT: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PACT); break; case AACT: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, AACT); break; case PDES: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PDES); break; case PDES2856: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PDES2856); break; case PDES68: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PDES68); break; /* case PDES119: v = llrfGetPadDoubleValue(paip->station, paip->channel, paip->sample, ts_p, PDES119); break; */ default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiLlrfPadFlag, ("devAiLlrfPad: read_ai: %s: unrecognized routine: %d", pai->name, paip->routine)); #endif return -1; } /* test for NAN */ if ( v != v ) { /* if val is NaN then there was a timeout */ recGblSetSevr( pai, TIMEOUT_ALARM, INVALID_ALARM ); } else { pai->val = v; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devAiLlrfPadFlag, ("devAiLlrfPad: read_ai: %s: stn: %d ch: %d has rval: %d gets val: %g\n", pai->name, paip->station, paip->channel, pai->rval, pai->val)); #endif pai->udf = FALSE; return 2; /* DON'T CONVERT */ }