/* $Id: devAaoVmeCommSim.c,v 1.4 2012/01/25 16:58:32 strauman Exp $ */ /* To fill in this template search for * Aao (record type) * VmeCommSim (device type) */ /*============================================================================= Name: devAaoVmeCommSim.c Abs: VmeCommSim Device support for Aao. Auth: 10-oct-2006, Till Straumann (TSS) Rev: -----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------- Mod: (see CVS log) =============================================================================*/ #include #include #include #include #include #include #include #include #include #define DEVSUPNAM "devAaoVmeCommSim" #define SIMLIMIT 4 /* Declare as a weak symbol so that we can choose to *not* link * with the VME digi comm module w/o errors. * The code determines at run-time if this symbol is NULL. */ int vmeDigiCommSetSimMode(int channel, void *buf, unsigned nbytes) __attribute__((weak)); static int (*set_sim_mode)(int, void*, unsigned) = vmeDigiCommSetSimMode; /* Currently unused */ static IOSCANPVT scanlist; typedef struct VmeCommSimDpvt_ts_ { IOSCANPVT *scan_p; unsigned nelm_orig, nelm_prev; } VmeCommSimDpvt_ts, *VmeCommSimDpvt_tps; static long report(int interest) { epicsPrintf(DEVSUPNAM": VmeCommSim device support for Aao\n"); return 0; } static long init(int after) { if ( !after ) { } return 0; } static long init_record(void *record_p) { aaoRecord *aao_ps = record_p; VmeCommSimDpvt_tps dpvt_ps; union { unsigned short s; unsigned char c[2]; } isBE = { s: 1 }; /* no point wasting memory if VME digi code not linked */ if ( ! set_sim_mode ) { aao_ps->pact = TRUE; return -1; } if ( !(dpvt_ps = aao_ps->dpvt = malloc(sizeof(*dpvt_ps))) ) { epicsPrintf(DEVSUPNAM": No memory to allocate DPVT\n"); aao_ps->pact = 1; return -1; } if ( VME_IO != aao_ps->out.type ) { epicsPrintf(DEVSUPNAM": OUT link not VME_IO\n"); aao_ps->pact = 1; return -1; } if ( ! isBE.c[1] ) { epicsPrintf(DEVSUPNAM": Support for little-endian CPU not implemented\n"); aao_ps->pact = 1; return -1; } else { /* FIXME: should remember endianness and byte-convert in the write_aao * routine */ } if ( aao_ps->ftvl != DBF_SHORT ) { epicsPrintf(DEVSUPNAM": FTVL should be SHORT -- overriding\n"); aao_ps->ftvl = DBF_SHORT; } aao_ps->bptr = malloc(aao_ps->nelm * sizeof(int16_t)); dpvt_ps->nelm_orig = dpvt_ps->nelm_prev = aao_ps->nelm; dpvt_ps->scan_p = &scanlist; return 0; } static long get_ioint_info(int cmd, struct dbCommon *record_ps, IOSCANPVT *pvt_ps) { VmeCommSimDpvt_tps dpvt_ps = record_ps->dpvt; *pvt_ps = *dpvt_ps->scan_p; return 0; } /* unused; would have to do byte-swapping on little-endian CPU here */ static long write_aao(void *record_p) { aaoRecord *aao_ps = record_p; VmeCommSimDpvt_tps dpvt_ps = aao_ps->dpvt; int i; /* If they write less than NELM elements then * pre 3.14.12 code changed NELM but 3.14.12 * (correctly) changes NORD. * We try to handle this transparently... */ if ( aao_ps->nelm != dpvt_ps->nelm_prev ) { /* pre-3.14.12; record support changed NELM */ aao_ps->nord = dpvt_ps->nelm_prev = aao_ps->nelm; aao_ps->nelm = dpvt_ps->nelm_orig; } else { /* 3.14.12 already sets NORD correctly */ } /* * if less than 4 samples are written then they want * to switch the simulation off (some clients don't * allow to send a zero-sized array). */ if ( aao_ps->nord < SIMLIMIT ) { aao_ps->nord = 0; } /* zero-pad -- in case they read the array back */ for ( i=aao_ps->nord; inelm; i++ ) { ((int16_t*)aao_ps->bptr)[i] = 0; } if ( set_sim_mode && set_sim_mode( aao_ps->out.value.vmeio.card, aao_ps->bptr, aao_ps->nord*sizeof(int16_t)) ) { epicsPrintf(DEVSUPNAM": vmeDigiCommSetSimMode failed!\n"); } return 0; } struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read; } devAaoVmeCommSim = { 5, report, init, init_record, get_ioint_info, write_aao }; epicsExportAddress(dset, devAaoVmeCommSim);