/*============================================================================= Name: devAiBlm.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 "alarm.h" #include "cvtTable.h" #include "dbDefs.h" #include "devLib.h" #include "dbAccess.h" #include "recSup.h" #include "callback.h" #include "devSup.h" #include "dbScan.h" /* for SCAN_IO_EVENT */ #include "link.h" #include "aiRecord.h" /* for aiRecord structure */ #include "epicsExport.h"/* for epicsExportAddress */ #include "epicsThread.h"/* for epicsThreadSleepQuantum */ #include /* for DEBUGPRINT */ #include "drvBlm.h" /* for blm* */ #include "errlog.h" #include "recGbl.h" #include typedef struct aiBlm_s { int routine; int station; int channel; } aiBlm_t; typedef struct aiBlmDpvt_s { DrvPadUdpCommDpvtRec drvPadUdpCommPart; aiBlm_t blmPart; } aiBlmDpvt_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!!! */ } AIDSET; AIDSET devAiBlm = { 6, NULL, NULL, init_record, drvPadUdpCommGetIointInfo, read_ai, NULL }; epicsExportAddress(dset,devAiBlm); #ifdef DEBUG_PRINT int devAiBlmFlag = 2; #endif static long init_record(aiRecord *pai) { aiBlm_t *paip; aiBlmDpvt_t *dpvt; /* Allocate the memory for this records private data structure */ dpvt = malloc(sizeof(*dpvt)); if (dpvt == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiBlmFlag, ("init_ai: %s: out of memory\n", pai->name)); #endif return S_dev_noMemory; } paip = &dpvt->blmPart; /* 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, devAiBlmFlag, ("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 > BLM_NUM_STATIONS ) { errlogPrintf("%s: init_record() -- station # %i out of range (1..%i)\n", pai->name, paip->station, BLM_NUM_STATIONS); pai->pact = TRUE; return -1; } if ( paip->channel < 0 || paip->channel >= 2*BLM_NUM_CHANNELS ) { errlogPrintf("%s: init_record() -- channel # %i out of range (0..%i)\n", pai->name, paip->channel, 2*BLM_NUM_CHANNELS-1); pai->pact = TRUE; return -1; } break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiBlmFlag, ("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, devAiBlmFlag, ("init_ai: %s: drvPadUdpCommDpvtInit failed\n", pai->name)); #endif return -1; } return 0; } static long read_ai(aiRecord *pai) { aiBlm_t *paip; double v; epicsTimeStamp *ts_p; /* Set this to facilitate easy access to channel field */ if (pai->dpvt == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiBlmFlag, ("read_ai: %s: dpvt is NULL\n", pai->name)); #endif return -1; } paip = &((aiBlmDpvt_t*)pai->dpvt)->blmPart; switch (paip->routine) { case 0: ts_p = epicsTimeEventDeviceTime == pai->tse ? &pai->time : 0; v = blmGetVal(paip->station, paip->channel, ts_p); /* 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, devAiBlmFlag, ("read_ai: %s: stn: %d avg phase is %g\n", pai->name, paip->station, pai->val)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devAiBlmFlag, ("read_ai: %s: unrecognized routine: %d", pai->name, paip->routine)); #endif return -1; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devAiBlmFlag, ("read_ai: %s: ch: %d has rval: %d gets val: %g\n", pai->name, paip->channel, pai->rval, pai->val)); #endif pai->udf = FALSE; return 2; /* DON'T CONVERT */ }