#include #include #include #include #include #include #include /* for ntohs used by padProto macros */ #include #include "digiHasOvfl.h" /* Start out ignoring the overflow (if supported) */ volatile int digiHasOvfl = DIGI_OVFL_SUP_INIT; static PadReply pst = 0; static epicsMutexId mtx; static epicsEventId evt; int digiHasOvflTestRequest(PadReply rply) { if ( DIGI_OVFL_SUP_INIT == digiHasOvfl ) { epicsMutexLock( mtx ); if ( DIGI_OVFL_SUP_INIT == digiHasOvfl ) { digiHasOvfl = DIGI_OVFL_SUP_UNKNOWN; padReplyRef( rply ); pst = rply; epicsEventSignal( evt ); } epicsMutexUnlock( mtx ); } return DIGI_HAS_OVFL_SUP; } /* Heuristic test to find out if the digitizer uses the LSB to signal * an overflow condition */ int digiHasOvflTest(PadReply rply) { int i,sz,lsb,msb,n; if ( (1 << PADRPLY_STRM_LD_SZ(rply)) != (sz = sizeof(int16_t)) ) { epicsPrintf("digiHasOvfl: sample size != int16 not implemented; ignore OVFL FORCED\n"); return 0; } /* look for a sample which is not close to full-range but has LSB set */ if ( (rply->strm_cmd_flags & PADCMD_STRM_FLAG_LE) ) { msb = 1; lsb = 0; } else { msb = 0; lsb = 1; } for ( i=n=0; idata[i+msb]) <= 10 ) { n++; if ( (rply->data[i+lsb] & 1) ) { /* Found one; LSB is apparently NOT used to signal overflow */ return 0; } } } /* none found; if we checked at least 40 samples then we're good */ return n >= 40 ? 1 : DIGI_OVFL_SUP_INIT; } static void hasOvflThread(void *arg) { PadReply rply; int res; do { epicsEventWait( evt ); epicsMutexLock( mtx ); rply = pst; pst = 0; epicsMutexUnlock( mtx ); res = digiHasOvflTest( rply ); epicsMutexLock( mtx ); digiHasOvfl = res; epicsMutexUnlock( mtx ); padReplyFree( rply ); } while ( DIGI_OVFL_SUP_INIT == res ); epicsEventDestroy( evt ); /* Destroying the mutex is not strictly legal here: * digiHasOvflTestRequest(): * - thread A could just have passed the first IF test and * then been suspended. * - thread B passes, takes mutex and schedules a job * - we execute finish, destroy the mutex * - thread A resumes and locks a dead mutex. */ } int digiHasOvflTestInit() { mtx = epicsMutexMustCreate(); evt = epicsEventMustCreate( epicsEventEmpty ); epicsThreadMustCreate("hasOvflTest", epicsThreadPriorityLow, epicsThreadGetStackSize( epicsThreadStackSmall ), hasOvflThread, 0); return 0; }