/* Add header */ #include #include #include #include #include #include /* for atan2 */ #include /* for DEBUGPRINT */ #include #include #include #include #include "drvBlm.h" #include "fcomBlm.h" #ifdef __rtems__ #include #endif #define BLM_EVENT 0x7a int blmReport(int station); int blmInitialise(void); struct drvet drvBlm = { 2, (DRVSUPFUN) blmReport, /* subroutine defined in this file */ (DRVSUPFUN) blmInitialise /* subroutine defined in this file */ }; epicsExportAddress(drvet, drvBlm); #ifdef DEBUG_PRINT int drvBlmFlag = 2; #endif static BlmStation blm_stations[BLM_NUM_STATIONS] = { 0 }; static const char * blm_station_names[BLM_NUM_STATIONS] = { [BLM_2101-1] = "BLM_2101", [BLM_2102-1] = "BLM_2102", [BLM_2401-1] = "BLM_2401", [BLM_2402-1] = "BLM_2402", [BLF_UND1-1] = "BLF_UND1" }; static BlmStation blmStationInitialize(int sta_idx) { int i; BlmStation sta_p; /* station index is 1-based (everywhere, for sake of consistency) */ sta_idx--; if ( sta_idx < 0 || sta_idx >= sizeof(blm_stations)/sizeof(blm_stations[0]) ) { /* invalid index */ return 0; } if ( blm_stations[sta_idx] ) { errlogPrintf("blmStationInitialize(): station %i already initialized\n", sta_idx+1); return 0; } sta_p = blm_stations[sta_idx] = callocMustSucceed( 1, sizeof(*sta_p), "blmStationInitialize(): no memory"); for ( i=0; ichnls)/sizeof(sta_p->chnls[0]); i++ ) { sta_p->chnls[i].frags = 0; sta_p->chnls[i].sum_acc = (BlmOutNumber)0; sta_p->chnls[i].sum = (BlmOutNumber)0; sta_p->chnls[i].tsSum.nsec = 0; sta_p->chnls[i].tsSum.secPastEpoch = 0; sta_p->chnls[i].tsImax.nsec = 0; sta_p->chnls[i].tsImax.secPastEpoch = 0; } sta_p->xid = 0; sta_p->chnlsDone = 0; sta_p->timedout = BLM_TIMEDOUT_ALL; sta_p->rawDataReady = epicsEventMustCreate(epicsEventEmpty); sta_p->mutex = epicsMutexMustCreate(); sta_p->epics_event_no = BLM_EVENT + sta_idx; sta_p->fcomProcessThread = epicsThreadMustCreate(blm_station_names[sta_idx], epicsThreadPriorityMax, epicsThreadGetStackSize(epicsThreadStackMedium), blmFcomTask, sta_p); return sta_p; } void blmFcomTaskResumeAll(void) { int i; for ( i=0; ifcomProcessThread ); #else rtems_task_resume( (rtems_id)blm_stations[i]->fcomProcessThread ); #endif } } } /*============================================================================= Name: blmReport(int station) Abs: =============================================================================*/ int blmReport(int station) { return 0; } /*============================================================================= Name: blmInitialise(void) Abs: =============================================================================*/ int blmInitialise(void) { int rc; if ( (rc = fcomBlmInit()) ) return rc; /* gBlenSection is a global variable which is set from the startup * script to describe which section this IOC is 'doing'. * This is ugly but we leave it as is since this is not a re-write * of the code... */ if ( ! blmStationInitialize( 2*gBlenSection + 1 ) ) return -1; return 0; } /*============================================================================= Name: blmGetStation(int station) Abs: =============================================================================*/ BlmStation blmGetStation(int station) { if ( station < 1 || station > BLM_NUM_STATIONS ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBlmFlag, ("blmGetStation: unrecognized station: %d\n", station)); #endif return 0; } /* station is one-based */ return blm_stations[station - 1]; } /*============================================================================= Name: blmGetSum Abs: =============================================================================*/ double blmGetVal(int station, int channel, epicsTimeStamp *ts_p) { double rval; BlmStation sta_p; unsigned tout; int i; if ( station < 1 || station > BLM_NUM_STATIONS ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBlmFlag, ("blmGetVal: unrecognized station: %d\n", station)); #endif return 0./0. /* NaN */; } if ( channel < 0 || channel >= 2*BLM_NUM_CHANNELS ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBlmFlag, ("blmGetVal: unrecognized channel: %d\n", channel)); #endif return 0./0. /* NaN */; } /* station is one-based */ station--; if ( ! (sta_p = blm_stations[station]) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBlmFlag, ("blmGetVal: station: %d not configured/initialized\n", station+1)); #endif return 0./0. /* NaN */; } i = channel % BLM_NUM_CHANNELS; epicsMutexMustLock( sta_p->mutex ); if ( channel < BLM_NUM_CHANNELS ) { rval = (double) sta_p->chnls[i].sum; if ( ts_p ) *ts_p = sta_p->chnls[i].tsSum; } else { rval = sta_p->chnls[i].imax; if ( ts_p ) *ts_p = sta_p->chnls[i].tsImax; } tout = sta_p->timedout; epicsMutexUnlock( sta_p->mutex ); /* return NaN if this channel is timed out */ if ( (tout & BLM_TIMEDOUT(channel)) ) { rval = 0./0.; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBlmFlag, ("blmGetVal: %s for %s ch%d: %g\n", channel < BLM_NUM_CHANNELS ? "sum" : "imax", blm_station_names[station], i, rval)); #endif return rval; } void blmDumpPrc(int s, int c) { BlmStation sta_p = blmGetStation(s); int i; if ( !sta_p || c < 0 || c >= sizeof(sta_p->chnls)/sizeof(sta_p->chnls[0]) ) return; for ( i = 0; i < sizeof(sta_p->chnls[0].prc)/sizeof(sta_p->chnls[0].prc[0]); i++ ) { errlogPrintf("%li ", (long)sta_p->chnls[c].prc[i]); } errlogPrintf("\n"); } void blmDumpVec(int s, int c) { BlmStation sta_p = blmGetStation(s); int i; if ( !sta_p || c < 0 || c >= sizeof(sta_p->chnls)/sizeof(sta_p->chnls[0]) ) return; for ( i = 0; i < sizeof(sta_p->chnls[0].vec)/sizeof(sta_p->chnls[0].vec[0]); i++ ) { errlogPrintf("%li ", (long)sta_p->chnls[c].vec[i]); } errlogPrintf("\n"); }