#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* We need features that epicsThread does not provide */ #include #include #include #include #include /* For processing EPICS records */ IOSCANPVT bpmDataScan[MAX_BPM]; /* Min. size of 'header' */ #define MINHDRSZ (sizeof(GenDigiPktHdrRec) + UDPCOMM_DATA_ALGN_OFF) /* Up-align to UDPCOMM alignment boundary */ #define __DO_ALIGN(x,a) ( ( (x) + (a) - 1 ) & ~ ((a)-1) ) #define HDRSZ __DO_ALIGN(MINHDRSZ, UDPCOMM_DATA_ALGN) #define PADSZ (HDRSZ - MINHDRSZ) #define MAX_DIGIS MAX_BPM #define MAX_PART 2 /* partitions per digitizer */ #define VEC_SIZE 16 /* A very ugly hack; udpComm should define a 'none' kind but it currently doesn't. * OTOH, PadDataCavity is currently unused */ #define KIND_NONE PadDataCavity /* If more samples are desired then this can be increased; could even be bigger than a page * but then some of the code below (checking against page size and initializing the buffer * free list) must be changed. */ #define PKT_TOT_SZ 2048 #define NPKTS_DFLT 10 /* per digitizer */ #define QDEPTH (MAX_BPM*2 + MAX_BPM/2) #define WORKROOM 1024 typedef struct GenDigiRec_ *GenDigi; typedef struct GenDigiPartRec_ { unsigned chnl; unsigned num; size_t nbytes; GenDigi digi; } GenDigiPartRec, *GenDigiPart; typedef struct GenDigiRec_ { GenDigiPartRec partitions[MAX_PART]; int numParts; char *devnam; epicsMutexId mtx; /* protects packet free list */ volatile int running; epicsEventId syncStart; epicsEventId syncStop; void *freeList; GenDigiChannelSel channelSelector; epicsThreadId tid; size_t nbytesMax; /* same value among all partitions */ PadDataKind kindMax; int dataReqIsCM; /* Desired format of data shipped to udpComm (CM=column-major) */ int frontEndIsRM; /* Actual format supported by front-end (RM=row-major) */ AfeData afe; unsigned fclk; GenDigiFrontEnd frontEnd; void *frontEndPvt; } GenDigiRec; #define STMD 10 typedef struct GenDigiPktHdrRec_ { struct GenDigiPktHdrRec_ *next; GenDigi digi; void **stk; int refcnt; } GenDigiPktHdrRec, *GenDigiPktHdr; typedef struct GenDigiPktRec_ { GenDigiPktHdrRec digiInfo; uint8_t pad[PADSZ]; union { uint8_t raw[PKT_TOT_SZ - PADSZ - sizeof(GenDigiPktHdrRec)]; PadReplyRec padrply; } pkt; } __attribute__((may_alias)) GenDigiPktRec, *GenDigiPkt; typedef union PadReqU_ { char raw[sizeof(PadRequestRec) + sizeof(PadCommandRec)]; struct { PadRequestRec hdr; PadCommandRec cmd; } req; } PadReqU; void *genDigiFERegistryID = &genDigiFERegistryID; static GenDigiPart genDigiPartitions[MAX_DIGIS] = { 0 }; static MsgQ sckq = 0; volatile DrvPadUdpCommHWTime digiMaxDiff = 0; volatile DrvPadUdpCommHWTime digiMinDiff = -1; static struct { uint32_t timestampHi; uint32_t timestampLo; uint32_t xid; } timestampInfo; static void hdrInitAdd(GenDigiPktHdr h, GenDigi digi) { h->refcnt = 0; h->digi = digi; h->stk = calloc( STMD, sizeof(*h->stk) ); epicsMutexLock( digi->mtx ); h->next = digi->freeList; digi->freeList = h; epicsMutexUnlock( digi->mtx ); } static int genDigi_padIoReq(int fd, int chnl, int type, uint32_t xid, uint32_t tsHi, uint32_t tsLo, void *cmdData, UdpCommPkt *rply, int timeout_ms); #define CHNL_STOP 0 #define CHNL_KILL (-1) static void chnlStop(GenDigi digi, int partition, int val); static void chnlThread(void *arg); static void freeDigi(GenDigi digi) { if ( ! digi ) { return; } if ( digi->tid ) { epicsMutexLock( digi->mtx ); chnlStop( digi, -1, CHNL_KILL ); epicsMutexUnlock( digi->mtx ); } if ( digi->mtx ) epicsMutexDestroy( digi->mtx ); if ( digi->syncStart ) epicsEventDestroy( digi->syncStart ); digi->frontEnd->close( digi->frontEndPvt ); if ( digi->devnam ) free( digi->devnam ); free( digi ); } /* we maintain a per-digi (rather than a lockfree + global) free list for historical reasons */ static void allocBufs(GenDigi digi, int nPkts) { GenDigiPkt rbuf; int i; if ( nPkts <= 0 ) nPkts = NPKTS_DFLT; if ( sizeof(GenDigiPktRec) != PKT_TOT_SZ ) { cantProceed("Internal error: unexpected package size (bad padding?)\n"); } /* allocate packet buffers */ i=0; while ( i < nPkts ) { rbuf = malloc( sizeof(*rbuf) ); memset(rbuf, 0xaa, sizeof(*rbuf)); if ( ! rbuf ) { cantProceed("Unable to allocate buffer #%i: %s\n", i, strerror(errno)); } hdrInitAdd( &rbuf->digiInfo, digi ); i++; } } static GenDigi createDigiComm(const char *devnam, unsigned long fclk, int nPkts, unsigned udpCommChnl, AfeAttachFunc afe_attach, GenDigiFrontEnd frontEnd ) { GenDigi digi = 0; int i; const char * cp; if ( ! (digi = calloc(1, sizeof(*digi))) ) { fprintf(stderr,"Out of memory\n"); return NULL; } digi->numParts = 0; digi->channelSelector = 0ULL; digi->devnam = strdup(devnam); digi->mtx = epicsMutexMustCreate(); digi->running = 0; digi->syncStart = epicsEventMustCreate( epicsEventEmpty ); digi->syncStop = 0; digi->fclk = fclk; digi->dataReqIsCM = 0; digi->frontEnd = frontEnd; digi->frontEndIsRM = 1; /* Default to row-major hardware */ for (i=0; ipartitions)/sizeof(digi->partitions[0]); i++ ) { digi->partitions[i].num = i; } if ( digi->frontEnd->open(devnam, fclk, &digi->frontEndPvt, udpCommChnl) ) { /* 'open' is supposed to print error messages */ goto bail; } /* Front-end can optionally override default RM hardware data format */ if ( digi->frontEnd->setFormat ) digi->frontEnd->setFormat(digi->frontEndPvt, &digi->frontEndIsRM); { GenDigiFrontEnd fe = digi->frontEnd; digi->kindMax = (0 == fe->arm( digi->frontEndPvt, PadDataCalGrn, AFE_CALM_GRN ) ? PadDataCalGrn : PadDataBpm); fe->disarm( digi->frontEndPvt ); } digi->nbytesMax = 0; if ( afe_attach ) { digi->afe = afe_attach( udpCommChnl, digi->frontEnd->afeWriteRead, digi->frontEndPvt ); if ( ! digi->afe ) cantProceed("Unable to attach AFE\n"); } cp = strrchr(devnam, '/'); cp = cp ? cp + 1 : devnam; allocBufs( digi, nPkts ); digi->tid = epicsThreadMustCreate( cp, epicsThreadPriorityHigh, epicsThreadGetStackSize( epicsThreadStackMedium ), chnlThread, digi ); /* Success */ return digi; bail: freeDigi( digi ); return NULL; } /* prepend 'a_in' to 'b_in' making sure selections don't overlap. If they do, return 0 */ static GenDigiChannelSel mergeSel(GenDigiChannelSel a_in, GenDigiChannelSel b_in) { GenDigiChannelSel a = a_in; GenDigiChannelSel b = b_in; /* check for overlap */ for ( a = a_in; a; a >>= 4 ) { for ( b = b_in; b; b >>= 4 ) { if ( (a & 0xf) == (b & 0xf) ) { return 0; } } } /* now prepend a_in to b_in */ /* first reverse order of 'b' */ b = 0; while ( b_in ) { b = (b<<4) | (b_in & 0xf); b_in >>= 4; } /* now shift into a_in */ while ( b ) { a_in = (a_in<<4) | (b & 0xf); b >>= 4; } return a_in; } /* This routine is not thread-safe (intended to be called from single-threaded * initialization script) */ int genDigiCommConfig(unsigned udpCommChnl, const char *devnam, unsigned sel_in, unsigned long fclk, int nPkts, AfeAttachFunc afe_attach, GenDigiFrontEnd frontEnd) { GenDigiChannelSel sel; GenDigiPart part; GenDigi digi = NULL, newDigi = NULL; unsigned i; int rval = -1; /* Only support 4 channels */ sel = sel_in & 0xffff; if ( udpCommChnl >= MAX_DIGIS ) { fprintf(stderr,"Channel number too big\n"); return -1; } if ( genDigiPartitions[udpCommChnl] ) { fprintf(stderr,"udpComm Channel %i already configured\n", udpCommChnl); return -1; } /* Look for another partition of the same digitizer */ for ( i=0; idigi->devnam, devnam ) ) { /* found one; use to create new partition */ digi = part->digi; break; } } if ( ! digi ) { newDigi = digi = createDigiComm( devnam, fclk, nPkts, udpCommChnl, afe_attach, frontEnd ); if ( ! digi ) goto bail; } else { if ( digi->frontEnd != frontEnd ) { fprintf(stderr,"genDigiComm: all partitions must use the same front-end!\n"); goto bail; } if ( afe_attach ) { fprintf(stderr,"genDigiComm: 'afe_attach' not supported for partitioned digitizer!\n"); goto bail; } if ( fclk != digi->fclk ) { fprintf(stderr,"genDigiComm: all digitizer partitions must use the same 'fclk'\n"); goto bail; } sel = mergeSel( sel, digi->channelSelector ); if ( 0 == sel ) { fprintf(stderr,"genDigiComm: partitions must not use overlapping channel selectors\n"); goto bail; } } /* create a new partition */ if ( MAX_PART == digi->numParts ) { fprintf(stderr,"genDigiComm: too many partitions requested\n"); goto bail; } part = & digi->partitions[digi->numParts]; part->chnl = udpCommChnl; part->nbytes = 0; if ( digi->frontEnd->validSel( digi->frontEndPvt, sel ) ) { goto bail; } digi->channelSelector = sel; part->digi = digi; digi->numParts++; genDigiPartitions[udpCommChnl] = part; if ( ! newDigi ) allocBufs( digi, nPkts ); /* else createDigiComm already did it (prior to thread creation) */ rval = 0; newDigi = 0; bail: freeDigi(newDigi); return rval; } static GenDigi getDigi(unsigned udpCommChnl) { GenDigiPart part; if ( udpCommChnl >= MAX_DIGIS || ! (part = genDigiPartitions[udpCommChnl]) ) return 0; return part->digi; } int genDigiCommWriteReg(unsigned udpCommChnl, unsigned reg, uint32_t val) { GenDigi digi; if ( ! (digi = getDigi( udpCommChnl )) ) { return -1; } return digi->frontEnd->writeReg( digi->frontEndPvt, reg, val ); } int genDigiCommReadReg(unsigned udpCommChnl, unsigned reg, uint32_t *val_p) { GenDigi digi; if ( ! (digi = getDigi( udpCommChnl )) ) { return -1; } return digi->frontEnd->readReg( digi->frontEndPvt, reg, val_p ); } int genDigiCommWriteParm(unsigned udpCommChnl, char *reg, double val) { GenDigi digi; if ( ! (digi = getDigi( udpCommChnl )) ) { return -1; } return digi->frontEnd->writeParm( digi->frontEndPvt, reg, val ); } int genDigiCommReadParm(unsigned udpCommChnl, char *reg, double *val_p) { GenDigi digi; if ( ! (digi = getDigi( udpCommChnl )) ) { return -1; } return digi->frontEnd->readParm( digi->frontEndPvt, reg, val_p ); } int genDigiCommReadSig(unsigned udpCommChnl, unsigned signal, double *val, uint16_t *stat, FrontEndTimestamp *ts_p) { GenDigi digi; if ( ! (digi = getDigi( udpCommChnl )) ) { return -1; } return digi->frontEnd->readSig( digi->frontEndPvt, signal, val, stat, ts_p); } static UdpCommPkt allocPkt(GenDigi digi) { GenDigiPkt rval; epicsMutexLock( digi->mtx ); if ( (rval = digi->freeList) ) { digi->freeList = rval->digiInfo.next; assert( 0 == rval->digiInfo.refcnt ); rval->digiInfo.refcnt = 1; rval->digiInfo.next = 0; } epicsMutexUnlock( digi->mtx ); return (UdpCommPkt)rval; } static void nullhdl(int unused) { /* we just need a handler so that a signal may interrupt/wake a * chnlThread blocked in 'read'. * If there is no handler then the process would be aborted... */ } static int genDigi_setup(const char *arg, uint32_t *peer_ip_p, int *peer_port_p) { struct sigaction sa, oa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = nullhdl; sigemptyset( &sa.sa_mask ); sa.sa_flags = 0; if ( sigaction( SIGUSR1, &sa, &oa ) ) { fprintf(stderr,"genDigi_setup(): sigaction(SIGUSR1) failed: %s\n", strerror(errno)); return -1; } if ( SIG_DFL != oa.sa_handler ) { fprintf(stderr,"genDigi_setup(): SIGUSR1 seems to be already in use -- recompile with other signal\n"); sigaction( SIGUSR1, &oa, 0 ); } return 0; } static int given = 0; static int genDigi_open(int port) { switch ( port ) { case PADPROTO_STRM_PORT: if ( sckq ) break; sckq = qinit( QDEPTH, QDEPTH ); if ( 0 == sckq ) break; return 1; default: if ( given & 1 ) break; given |= 1; return 0; } return -1; } static int genDigi_close(int fd) { switch ( fd ) { case 0: if ( given & 1 ) { given &= ~1; return 0; } break; case 1: qfini( sckq ); sckq = 0; return 0; default: break; } return -1; } static int genDigi_connect(int fd, uint32_t peer_ip, int peer_port) { return 0; } static int genDigi_send(int fd, void *buf_p, int len) { PadReqU *r = buf_p; int i,sz,j; PadCommand cmd; if ( fd != 0 ) { fprintf(stderr,"genDigi_send(genDigiComm) unsupported fd\n"); return -1; } if ( len < sizeof(r->req.hdr) ) { return -1; } if ( PADPROTO_VERSION4 != r->req.hdr.version ) { fprintf(stderr,"genDigi_send(genDigiComm) unsupported PAD protocol version\n"); return -1; } sz = r->req.hdr.cmdSize; if ( r->req.hdr.nCmds < 0 ) { fprintf(stderr,"genDigi_send(genDigiComm) command numbers < 0 not implemented\n"); return -1; } if ( MAX_DIGIS < r->req.hdr.nCmds ) { fprintf(stderr,"genDigi_send(genDigiComm) max. command number mismatch\n"); return -1; } /* Ugly hack to pass this info :-( */ timestampInfo.timestampHi = r->req.hdr.timestampHi; timestampInfo.timestampLo = r->req.hdr.timestampLo; for ( i=0, j=0; ireq.hdr.nCmds; i++, j+=sz ) { cmd = (PadCommand)&r->req.hdr.data[j]; if ( genDigi_padIoReq( fd, i, cmd->type, r->req.hdr.xid, ntohl(timestampInfo.timestampHi), ntohl(timestampInfo.timestampLo), cmd, 0, -1) ) return -1; } return len; } static void * genDigi_bufptr(UdpCommPkt pkt) { return &((GenDigiPkt)pkt)->pkt; } static UdpCommPkt genDigi_recv(int fd, int timeout_ms) { UdpCommPkt rval = 0; if ( fd != 1 ) { fprintf(stderr,"genDigi_recv() bad socket fd\n"); return 0; } if ( ! sckq ) return 0; if ( timeout_ms < 0 ) { qrecv( sckq, (void**)&rval ); } else { cantProceed("genDigi_recv(): finite timeout not implemented\n"); } return rval; } static UdpCommPkt genDigi_alloc(int fd) { /* cannot implement this since allocation is per digitizer; * it's not currently used by drvPadUdpComm anyways - maybe * we should remove it */ fprintf(stderr,"genDigi_alloc: UNIMPLEMENTED\n"); return 0; } int sisRef(GenDigiPkt pkt) { return pkt->digiInfo.refcnt; } static void genDigi_creatref(UdpCommPkt raw_pkt) { GenDigi digi; GenDigiPkt pkt = (GenDigiPkt)raw_pkt; if ( !pkt ) cantProceed("NULL pointer passed to genDigi_creatref!\n"); digi = pkt->digiInfo.digi; epicsMutexLock( digi->mtx ); pkt->digiInfo.refcnt++; epicsMutexUnlock( digi->mtx ); } static void genDigi_free(UdpCommPkt raw_pkt) { GenDigi digi; GenDigiPkt pkt = (GenDigiPkt)raw_pkt; if ( pkt ) { digi = pkt->digiInfo.digi; epicsMutexLock( digi->mtx ); if ( --pkt->digiInfo.refcnt < 0 ) { cantProceed("Internal error - packet reference count < 0\n"); } if ( 0 == pkt->digiInfo.refcnt ) { pkt->digiInfo.next = digi->freeList; digi->freeList = pkt; } epicsMutexUnlock( digi->mtx ); } } static void chnlThread(void *arg) { GenDigi digi = arg; UdpCommPkt p[MAX_PART]; PadReply rply; PadDataKind kind, nextKind, okind; unsigned n,i; unsigned nbytesTot; ssize_t got; uint8_t *src; unsigned lineSz; uint8_t *raw_buf; unsigned area_size; uint8_t *alloced_buf = 0; FrontEndTimestamp ts; unsigned nextCal = AFE_CALM_GRN; /* Set first cal cycle to green to match genDigiCommConfig (arbitrary) */ area_size = getpagesize(); area_size = ( PKT_TOT_SZ * MAX_PART + WORKROOM + (area_size - 1) ) & ~(area_size - 1); if ( ! (alloced_buf = malloc(area_size)) ) { cantProceed("Unable to allocate intermediate buffer area\n"); } for ( n=0; nrunning >= 0 ) { epicsEventWait( digi->syncStart ); /* Could destroy 'syncStart' here -- but careful with chnlStop(digi, CHNL_KILL) */ kind = nextKind = PadDataBpm; digi->frontEnd->arm( digi->frontEndPvt, kind, nextCal ); retry_alloc: while ( digi->running > 0 ) { for ( n=0; nnbytesMax * digi->numParts; raw_buf = alloced_buf; /* 'read' routine can optionally overwrite ts and kind */ ts.timestampHi = timestampInfo.timestampHi; ts.timestampLo = timestampInfo.timestampLo; got = digi->frontEnd->read( digi->frontEndPvt, &raw_buf, area_size, nbytesTot, &ts, &kind ); if ( PadDataBpm == kind ) { DrvPadUdpCommHWTime now = drvPadUdpCommHWTime(); if ( now > digiMaxDiff ) digiMaxDiff = now; if ( now < digiMinDiff ) digiMinDiff = now; } okind = kind; if ( got != nbytesTot ) { if ( got > 0 ) { fprintf(stderr,"genDigi: incomplete packet read (%d of %d)\n", (int)got, (int)nbytesTot); } if ( got != 0 ) continue; /* 'got' must be zero here; this means that something arrived (drvPadUdpComm should * maintain 'good' connection status) but it is not to be used. */ okind = KIND_NONE; } /* Now we want to use consistent values */ epicsMutexLock( digi->mtx ); src = raw_buf; lineSz = digi->nbytesMax / PADRPLY_STRM_NCHANNELS; for ( n = 0; n < digi->numParts; n++ ) { if ( ! (digi->running & (1<partitions[n].num)) ) { src += PADRPLY_STRM_NCHANNELS * lineSz; continue; } rply = &((GenDigiPkt)p[n])->pkt.padrply; rply->version = PADPROTO_VERSION4; rply->type = PADCMD_STRM; rply->chnl = digi->partitions[n].chnl; rply->nBytes = htons(digi->partitions[n].nbytes + sizeof(PadReplyRec)); rply->timestampHi = ts.timestampHi; rply->timestampLo = ts.timestampLo; rply->xid = timestampInfo.xid; rply->stat = 0; rply->strm_cmd_idx = 0; rply->strm_cmd_flags = PADCMD_STRM_FLAG_LE | PADRPLY_STRM_FLAG_TYPE_SET(okind); if ( digi->dataReqIsCM ) { rply->strm_cmd_flags |= PADCMD_STRM_FLAG_CM; } if ( KIND_NONE != okind ) { if ( digi->frontEndIsRM ) { if ( digi->dataReqIsCM ) { int16_t *dst = (int16_t*)rply->data; int16_t *lsrc[PADRPLY_STRM_NCHANNELS]; unsigned sz = digi->partitions[n].nbytes / PADRPLY_STRM_NCHANNELS; for ( i=0; idata; unsigned sz = digi->partitions[n].nbytes / PADRPLY_STRM_NCHANNELS; for ( i=0; idataReqIsCM ) { memcpy(rply->data, src, digi->partitions[n].nbytes); } else { int16_t *dst = (int16_t*)rply->data; int16_t *lsrc = (int16_t*)src; /* Divide by 2 to scale sz from bytes to int16_t */ unsigned sz = digi->partitions[n].nbytes / PADRPLY_STRM_NCHANNELS/2; for ( lsrc=(int16_t*)src; lsrc < ((int16_t*)src) + PADRPLY_STRM_NCHANNELS; lsrc++ ) { for ( i=0; imtx ); /* nextKind is type of data on next acquisition, assumes beam interleaved with cal * nextCal is flavor (red/grn/none) of next cal cycle independent of beam data */ if ( digi->afe ) { switch ( digi->afe->calMode ) { case AFE_CALM_TOGGLE: nextKind = kind + 1; switch ( kind ) { case PadDataCalRed: nextCal = AFE_CALM_GRN; break; case PadDataCalGrn: nextCal = AFE_CALM_RED; break; default: /* Arbitrarily choose green as starting * cal mode after transition from * NONE to TOGGLE */ if ( nextCal == AFE_CALM_NONE ) nextCal = AFE_CALM_GRN; break; } break; case AFE_CALM_RED: nextKind = PadDataBpm == kind ? PadDataCalRed : PadDataBpm; nextCal = AFE_CALM_RED; break; case AFE_CALM_GRN: nextKind = PadDataBpm == kind ? PadDataCalGrn : PadDataBpm; nextCal = AFE_CALM_GRN; break; default: nextKind = PadDataBpm; nextCal = AFE_CALM_NONE; break; } if ( nextKind > digi->kindMax ) nextKind = 0; if ( nextKind != kind ) { digi->frontEnd->arm( digi->frontEndPvt, nextKind, nextCal ); kind = nextKind; } } } /* Could re-create 'syncStart' here */ epicsEventSignal( digi->syncStop ); } if ( alloced_buf ) free( alloced_buf ); for ( n=0; nrunning) >= 0 ) { digi->running |= (1<syncStart ); } } } /* call with mutex held */ static void chnlStop(GenDigi digi, int part, int val) { if ( val ) { /* make sure it is started */ epicsEventSignal( digi->syncStart ); } if ( digi->running > 0 ) { digi->running &= ~(1<running || val ) { digi->syncStop = epicsEventMustCreate( epicsEventEmpty ); if ( val ) digi->running = val; epicsMutexUnlock( digi->mtx ); do { /* could be that the target receives the signal after checking 'running' * but before really in the 'read' system call; signal is lost * in this case and the wait will time out; try again */ pthread_kill( epicsThreadGetPosixThreadId(digi->tid), SIGUSR1 ); } while ( epicsEventWaitWithTimeout( digi->syncStop, 1.0 ) ); epicsMutexLock( digi->mtx ); epicsEventDestroy( digi->syncStop ); digi->syncStop = 0; } } } static int maybe_change_state(GenDigiPart part, unsigned nbytes, int dataReqIsCM, int start) { int new_running; unsigned nbytesMax, nbytesOrig; GenDigi digi = part->digi; int j,st; unsigned new_nsamples; /* check here (normal case); no need to enter and take mutex */ if ( part->nbytes != nbytes || !!start != !!(digi->running & (1<num)) || digi->dataReqIsCM != dataReqIsCM ) { epicsMutexLock( digi->mtx ); digi->dataReqIsCM = dataReqIsCM; new_running = start ? digi->running | (1<num) : digi->running & ~(1<num); nbytesOrig = part->nbytes; part->nbytes = nbytes; nbytesMax = 0; /* we could also change the channel mask when switching partitions on and off * but we keep it simple for now. */ for ( j=0; jnumParts; j++ ) { if ( digi->partitions[j].nbytes > nbytesMax ) { nbytesMax = digi->partitions[j].nbytes; } if ( nbytesMax != digi->nbytesMax ) { if ( nbytesMax > sizeof( ((GenDigiPkt)0)->pkt.raw ) - sizeof(PadReplyRec) ) { fprintf(stderr,"genDigi_padIoReq(START) - requested too many samples; won't fit\n"); part->nbytes = nbytesOrig; epicsMutexUnlock( digi->mtx ); return -1; } new_nsamples = nbytesMax / PADRPLY_STRM_NCHANNELS / sizeof(int16_t); /* SetCount is not thread-safe */ st = digi->frontEnd->setCount(digi->frontEndPvt, digi->channelSelector, new_nsamples); if ( st ) { fprintf(stderr,"genDigi_padIoReq(START) - setting sample count failed\n"); part->nbytes = nbytesOrig; epicsMutexUnlock( digi->mtx ); return -1; } digi->nbytesMax = nbytesMax; } } if ( digi->running != new_running ) { if ( start ) chnlStart( digi, part->num ); else chnlStop( part->digi, part->num, CHNL_STOP ); } epicsMutexUnlock( digi->mtx ); } return 0; } static int genDigi_padIoReq(int sd, int chnl, int type, uint32_t xid, uint32_t tsHi, uint32_t tsLo, void *cmdData, UdpCommPkt *wantReply, int timeout_ms) { int i,min,max; PadReply rply; GenDigiPart part; if ( sd != 0 ) { fprintf(stderr,"genDigi_padIoReq() unsupported sd\n"); return -1; } if ( wantReply && PADCMD_SQRY != type ) { fprintf(stderr,"genDigi_padIoReq() does not implement replies other than queries\n"); return -1; } if ( chnl >= MAX_DIGIS ) { fprintf(stderr,"genDigi_padIoReq() channel # too big (%i)\n", chnl); return -1; } if ( chnl < 0 ) { min=0; max=MAX_DIGIS; } else { min=chnl; max=chnl+1; } timestampInfo.xid = xid; timestampInfo.timestampHi = htonl(tsHi); timestampInfo.timestampLo = htonl(tsLo); switch ( PADCMD_GET(type) ) { case PADCMD_STRM: { PadStrmCommandRec *scmd_p = cmdData; unsigned nsmpls = ntohl(scmd_p->nsamples); unsigned nbytes = nsmpls * PADRPLY_STRM_NCHANNELS * sizeof(int16_t); int dataReqIsCM; if ( (PADCMD_STRM_FLAG_32 | PADCMD_STRM_FLAG_C1) & scmd_p->flags ) { fprintf(stderr,"genDigi_padIoReq() does not implement 32-bit data nor single-channel mode\n"); return -1; } if ( ! (PADCMD_STRM_FLAG_LE & scmd_p->flags) ) { fprintf(stderr,"genDigi_padIoReq() does not implement big-endian mode\n"); return -1; } dataReqIsCM = !! (PADCMD_STRM_FLAG_CM & scmd_p->flags); if ( (nsmpls & 0xf) ) { fprintf(stderr,"genDigi_padIoReq(STRM) - number of samples MUST be a multiple of 16\n"); return -1; } for ( i=min; idigi )) ) { /* no available packets */ return -1; } rply = &((GenDigiPkt)*wantReply)->pkt.padrply; rply->version = PADPROTO_VERSION4; rply->type = PADCMD_SQRY | PADCMD_RPLY; rply->chnl = min; rply->stat = 0; rply->timestampHi = timestampInfo.timestampHi; rply->timestampLo = timestampInfo.timestampLo; rply->xid = xid; rply->nBytes = htons(sizeof(*rply)); /* we DO support little-endian, 16-bit, 4-channel mode ONLY */ rply->strm_sqry_sup_on = PADCMD_STRM_FLAG_LE | PADCMD_STRM_FLAG_CM; rply->strm_sqry_sup_off = PADCMD_STRM_FLAG_32 | PADCMD_STRM_FLAG_C1; break; case PADCMD_STOP: for ( i=min; inbytes, part->digi->dataReqIsCM, 0 /* STOP */ ) ) return -1; } break; case PADCMD_SIM: { PadSimCommandRec *scmd_p = cmdData; for ( i=min; idigi->numParts > 1 ) { fprintf(stderr,"genDigi_padIoReq(SIM) channel #%i -- simulation of multiple partitions not supported.\n", chnl); return -1; } if ( ! part->digi->frontEnd->setSim ) { fprintf(stderr,"genDigi_padIoReq(SIM) channel #%i -- digitizer front-end doesn't support simulation.\n", chnl); return -1; } part->digi->frontEnd->setSim( part->digi->frontEndPvt, ntohl(scmd_p->a), ntohl(scmd_p->b), ntohl(scmd_p->c), ntohl(scmd_p->d), (scmd_p->flags & PADCMD_SIM_FLAG_NOSEND) ); } } break; case PADCMD_NOP: break; default: fprintf(stderr,"genDigi_padIoReq() does not implement cmd type %i\n",type); return -1; } return 0; } DrvPadUdpCommIORec io_ops = { /* int (*setup) (const char *arg, uint32_t *peer_ip_p, int *peer_port_p); */ setup: genDigi_setup, /*int (*open) (int port); */ open: genDigi_open, /*int (*close) (int fd); */ close: genDigi_close, /*int (*connect) (int fd, uint32_t peer_ip, int peer_port); */ connect: genDigi_connect, /*int (*send) (int fd, void *buf_p, int len); */ send: genDigi_send, /*UdpCommPkt (*recv) (int fd, int timeout_ms); */ recv: genDigi_recv, /*void * (*bufptr) (UdpCommPkt); */ bufptr: genDigi_bufptr, /*UdpCommPkt (*alloc) (int fd); */ alloc: genDigi_alloc, /*void (*creatref) (UdpCommPkt); */ creatref: genDigi_creatref, /* void (*free) (UdpCommPkt); */ free: genDigi_free, /* This last one is ugly but does different things * with the VME digitizer as opposed to the PAD. * Again: this is historic evolution not clean design from scratch... int (*padIoReq) (int fd, int chnl, int type, uint32_t xid, uint32_t tsHi, uint32_t tsLo, void *cmdData, UdpCommPkt *rply, int timeout_ms); */ padIoReq: genDigi_padIoReq, }; DrvPadUdpCommIO drvGenDigiCommIO = &io_ops; // Keep this one around for backwards compatibility (DEPRECATED) DrvPadUdpCommIO drvSis8300DigiCommIO = &io_ops; static const struct iocshArg args[] = { { "udpComm channel", iocshArgInt }, { "genDigi device file", iocshArgString }, { "genDigi digitizer channel selector", iocshArgInt }, { "digitizer clock frequency (in Hz) - currently only 125MHz or 109MHz supported", iocshArgInt }, { "number of packet buffers (per channel; optional)", iocshArgInt }, { "name of AFE attach function", iocshArgString }, { "name of front-end module", iocshArgString } }; static const struct iocshArg *bloatp[sizeof(args)/sizeof(args[0])]; static const struct iocshFuncDef genDigiCommConfigDesc = { "genDigiCommConfig", sizeof(args)/sizeof(args[0]), bloatp }; static void genDigiCommConfigFunc(const iocshArgBuf *args) { AfeAttachFunc f = 0; GenDigiFrontEnd frontEnd = 0; int i; if ( !args[1].sval ) { epicsPrintf("genDigiCommConfig -- need non-NULL device file name arg\n"); return; } if ( args[5].sval && args[5].sval[0] ) { if ( strcmp(args[5].sval,"NULL") && ( 1 != sscanf(args[5].sval,"%i",&i) || 0 != i) ) { if ( ! (f = afeAttachFind( args[5].sval )) ) { errlogPrintf("genDigiCommConfig: AFE attach function '%s' not found!\n", args[5].sval); return; } } } if ( ! args[6].sval || ! args[6].sval[0] ) { errlogPrintf("genDigiCommConfig: need a digitizer front-end identifier argument\n"); return; } else { if ( ! (frontEnd = registryFind( genDigiFERegistryID, args[6].sval )) ) { errlogPrintf("genDigiCommConfig: front-end '%s' not found!\n", args[6].sval); return; } } if ( genDigiCommConfig(args[0].ival, args[1].sval, (unsigned)args[2].ival, (unsigned)args[3].ival, args[4].ival, f, frontEnd) ) { epicsPrintf("genDigiCommConfig() FAILED for channel %i\n", args[0].ival); } } static void genDigiCommRegistrar(void) { int i; for ( i=0; i