#include #include #include #include #include #include #include #include #include #include #define NARR(arr) (sizeof(arr)/sizeof(arr[0])) #define ATT1 0 #define ATT2 1 #define CALA 2 static char *dbnms[] = { "ATT1", "ATT2", "CALA" }; typedef struct SubAttPvt_ { DBADDR dbas[NARR(dbnms)]; int latt[NARR(dbnms)]; } SubAttPvtRec, *SubAttPvt; static long subAttIni(subRecord *psub, long (*process)(subRecord*)) { SubAttPvt pvt; char nm[100]; char *col; int i; if ( !(psub->dpvt = (pvt = malloc(sizeof(*pvt)))) ) { epicsPrintf("subAttIni(): no memory\n"); goto bail; } strcpy(nm, psub->name); if ( !(col = strrchr(nm, ':')) ) { epicsPrintf("subAttIni(): FATAL ERROR -- no ':' found in '%s'\n", nm); goto bail; } col++; for ( i=0; idbas[i]) ) { epicsPrintf("subAttIni(): FATAL ERROR -- record '%s' not found\n", nm); goto bail; } pvt->latt[i] = -1; } return 0; bail: free(psub->dpvt); psub->dpvt = 0; return -1; } #define A1MAX(psub) ((int)(psub)->e) #define A2MAX(psub) ((int)(psub)->f) #define ACMAX 31 #define A1 (v[ATT1]) #define A2 (v[ATT2]) #define CA (v[CALA]) static long subAtt(subRecord *psub) { SubAttPvt pvt = psub->dpvt; int v[NARR(dbnms)]; int tot, a; int i; long rval = 0; if ( !pvt ) { epicsPrintf("subAtt(): CONFIG ERROR -- No dbaddrs\n"); return -1; } /* If nothing changed return. * Don't bother trying to save time * for other special cases since * 'A' is changed most frequently * and that potentially requires updating all * attenuators anyways... */ if ( psub->a == psub->la && psub->b == psub->lb && psub->c == psub->lc && psub->d == psub->ld ) return 0; a = (int)psub->a; if ( (tot = a + (int)psub->b) > A1MAX(psub) + A2MAX(psub) ) tot = A1MAX(psub) + A2MAX(psub); if ( tot < 0 ) tot = 0; /* breakpoint psub->d must be >= 0 */ if ( (A1 = ( tot - (int)psub->d )/2) < 0 ) { A1 = 0; A2 = tot; } else { if ( A1 > A1MAX(psub) ) A1 = A1MAX(psub); A2 = tot - A1; } /* At this point 0 <= a1 <= A1MAX * and 0 <= tot <= AMAX */ if ( A2 > A2MAX(psub) ) { A2 = A2MAX(psub); A1 = tot - A2MAX(psub); } /* Here 0 <= A1 <= A1MAX, 0 <= A2 <= A2MAX holds */ CA = (int)(psub->c + A1MAX(psub) + A2MAX(psub)) - a; if ( CA < 0 ) { CA = 0; } else if ( CA > ACMAX ) { CA = ACMAX; } for ( i=0; i < NARR(dbnms); i++ ) { if ( pvt->latt[i] != v[i] ) { if ( dbPutField( &pvt->dbas[i], DBR_LONG, &v[i], 1) ) { epicsPrintf("dbPutField to %s failed\n", dbnms[i]); rval = -1; } else { pvt->latt[i] = v[i]; } } } return rval; } epicsRegisterFunction(subAttIni); epicsRegisterFunction(subAtt); /* * file: subAttSetup.c * purpose: subroutine to set attenuator limits, based on AFE type * * initial version 10 Dec 2012, S. Hoobler (SONYA) * * Inputs: * * A = AFE firmware version * * Outputs: * * a = ATT1.HOPR * b = ATT1.DRVH * c = ATT2.HOPR * d = ATT2.DRVH * e = ATT.HOPR * f = ATT.DRVH * g = ATT.VAL * h = ATTC.D - ATT breakpoint * i = ATTC.E - ATT1 upper limit * j = ATTC.F - ATT2 upper limit * k = ATTO.DRVL * l = ATTO.LOPR * m = ATTO.DRVH * n = ATTO.HOPR * o = CLAO.DRVL * p = CLAO.LOPR * q = CLAO.DRVH * r = CLAO.HOPR * * * fw * description vers att1[dB] att2[dB] * ----------------------------------------------------------- * LCLS-I PAD AFE - initial 4 0-15 0-15 * LCLS-I PAD AFE - later >=32 0-15 0-31 * XTA AFE Chassis >64 0-15 0-31 * SLAC RTM AFE >=8 0-31 0-31 * * */ #include /* for struct aSubRecord */ #define AFE_PAD 0 #define AFE_CHASSIS 1 #define AFE_RTM 2 long subAttSetup(struct aSubRecord *psub) { epicsUInt32 vers, atth, bkpt = 20; epicsUInt32 att1h = 15; /* attenuator defaults */ epicsUInt32 att2h = 31; epicsUInt32 attol = -20, attoh = 45; epicsUInt32 claol = -35, claoh = 5; if( psub == NULL) return 0; vers = *(epicsUInt32 *)psub->a; if ( (vers >=8) && (vers < 32) ) { att1h = 31; attoh = 60; claol = -60; claoh = -10; bkpt = 23; } if ( vers < 5 ) { att2h = 15; attol = -10; attoh = 30; claol = -20; claoh = 20; } atth = att1h + att2h; *((epicsUInt32 *)psub->vala) = att1h; *((epicsUInt32 *)psub->valb) = att1h; *((epicsUInt32 *)psub->valc) = att2h; *((epicsUInt32 *)psub->vald) = att2h; *((epicsUInt32 *)psub->vale) = atth; *((epicsUInt32 *)psub->valf) = atth; *((epicsUInt32 *)psub->valg) = atth; *((epicsUInt32 *)psub->valh) = bkpt; *((epicsUInt32 *)psub->vali) = att1h; *((epicsUInt32 *)psub->valj) = att2h; *((epicsUInt32 *)psub->valk) = attol; *((epicsUInt32 *)psub->vall) = attol; *((epicsUInt32 *)psub->valm) = attoh; *((epicsUInt32 *)psub->valn) = attoh; *((epicsUInt32 *)psub->valo) = claol; *((epicsUInt32 *)psub->valp) = claol; *((epicsUInt32 *)psub->valq) = claoh; *((epicsUInt32 *)psub->valr) = claoh; return 0; } epicsRegisterFunction(subAttSetup);