/* compile lines: gcc -c -fPIC rorc2rcu_lib.c -I/date/rorc/ -I/date/fec/ -I/date/physmem gcc -shared rorc2rcu_lib.o /date/fec/Linux/fec2rorc_lib.o /date/rorc/Linux/rorc_ddl_q.o /date/rorc/Linux/rorc_lib_q.o /date/rorc/Linux/rorc_aux.o /date/physmem/Linux/physmem_lib.o -o librorc2rcu.so */ #include "rorc2rcu.h" #include #include #include //#include #define A_SIFSTATUS 0x0 #define A_SIFERRORS 0x10000 #define RCU_WRD_RD 0x20000 #define SIB_MAX_TRIES 1000 int sib_wait(chan_key *link) { int nempty; int sif_status; int tries=0; do { if (++tries>SIB_MAX_TRIES) return -1; sif_status=ddl_statusReadout(link,A_SIFSTATUS); if (sif_status<0) return -1; nempty=(sif_status>>4) & 0x01; if (nempty) usleep(100); } while (nempty); return 0; } int rcu_open(chan_key **link,int revision,int serial,int channel) { *link=get_channel(revision,serial,channel); if ((*link)->status != R_OPEN) return ((*link)->err); rorcReset(&((*link)->rorc),RORC_RESET_RORC); usleep(1000); rorcReset(&((*link)->rorc),RORC_RESET_DIU); usleep(1000); rorcReset(&((*link)->rorc),RORC_RESET_SIU); usleep(1000); rorcReset(&((*link)->rorc),RORC_RESET_DIU); usleep(1000); rorcReset(&((*link)->rorc),RORC_RESET_RORC); usleep(1000); if (rorcCheckLink(&((*link)->rorc)) != RORC_STATUS_OK) { printf("rcu_open(): SIU not seen. Can not clear SIU status!\n"); return 1; } else { int timeout = DDL_RESPONSE_TIME * ((*link)->rorc.pci_loop_per_usec); int status = ddlReadSiu(&((*link)->rorc), 0, timeout); //if (status != -1) printf("SIU status cleared. Status word: 0x%08x\n", status); status = ddlReadDiu(&((*link)->rorc), 0, timeout); //if (status != -1) printf("DIU status cleared. Status word: 0x%08x\n", status); } return 0; } int rcu_close(chan_key *link) { int ret; // printf("rcu_close(0x%08x)\n",link); ret=release_channel(link); // printf("rcu_close(0x%08x)==%d\n",link,ret); return ret; } int rcu_read(chan_key *link,int addr,unsigned int* data,int *nwords) { int ret; unsigned int *tmp; ret=ddl_sendCommand(link,RCU_WRD_RD|*nwords); if (ret) return ret; tmp=(unsigned int*)malloc((*nwords)*sizeof(unsigned int)); // usleep(10000); // printf("ddl_readBlock(link=0x%08x,addr=0x%04x,tmp=0x%08x,nwords=0x%08x) *nwords=%d\n",link,addr,tmp,nwords,*nwords); *nwords=ddl_readBlock(link,addr,tmp,*nwords); // usleep(10000); memcpy(data,tmp,(*nwords)*sizeof(unsigned int)); free(tmp); return 0; } int rcu_write(chan_key *link,int addr,unsigned int* data, int nwords) { int ret; unsigned int *tmp; ret=sib_wait(link); if (ret) return ret; tmp=(unsigned int*)malloc(nwords*sizeof(unsigned int)); if (tmp==NULL) { printf("Malloc failed?\n"); return 1; } memcpy(tmp,data,nwords*sizeof(unsigned int)); // usleep(10000); // printf("ddl_writeBlock(link=0x%08x,tmp=0x%08x,nwords=%d,addr=0x%04x\n",link,tmp,nwords,addr); ret=ddl_writeBlock(link,tmp,nwords,addr); // usleep(10000); free(tmp); return ret; } int rcu_core_cmd(chan_key *link,unsigned int cmd) { // printf("ddl_sendCommand(link=0x%08x,cmd=%0x04x)\n",link,cmd); return ddl_sendCommand(link,cmd); }