/* * File Name : tmc.c * Creation Date : Nov 13 2000 * Author : Y.Nakaune */ #include #include #include #include #include "vmelib.h" #include "buf.h" #include "tmc.h" static TMC_FORM *mm[ TMC_MOD ]; /*---------------------------------------------------------- InitTMC ---------------------------------------------------------*/ void InitTMC( void ) { int i, j; volatile caddr_t vaddr; for ( i = 0; i < TMC_MOD; i++ ) { vaddr = vme_mapopen( VME_A24D16MODE, (caddr_t)( TMC_ADDR + ADDR_STEP * (long)i ), TMC_SIZE ); if ( (int)vaddr == -1 ) exit_err( "TMC cannot open!" ); fprintf( stderr, "TMC: memory map opened: %p\n", vaddr ); mm[i] = (TMC_FORM *)vaddr; mm[i]->Drun = 0; mm[i]->Dcstsp = CSTSP; mm[i]->Dedge = EDGE; #ifdef USE_TMC_DC if ( i == MOD_DC) { mm[i]->Dcount = TIMERANGE_DC; } else { mm[i]->Dcount = TIMERANGE; } #else mm[i]->Dcount = TIMERANGE; #endif mm[i]->Ddisp = DISP; mm[i]->Dmodule = i; mm[i]->dready = 0; mm[i]->Drun = 1; for ( j = 0; mm[i]->dready != 1; j++ ) { usleep( 10000L ); if( j == 100 ) exit_err( "TMC: failed initialize. Reset the Modules" ); } } } /* ---------------------------------------------------------- ReadyTMC ---------------------------------------------------------*/ void ReadyTMC( void ) { int i; for ( i = 0; i < TMC_MOD; i++ ) { mm[i]->dready = 0; } } /*---------------------------------------------------------- ReadTMC ---------------------------------------------------------*/ int ReadTMC( Cfg_tmc *cfg, FILE *fp ) { int i, j, nb, xy; int trigger[ DC_NUMBER ][ DC_XY ]; int eventsum = 0, datasize, eventnum[TMC_MOD]; short ch, time; Daq_tmc tmc; event *evt, *pevt; /* read data from TMC */ memset( &tmc, -1, sizeof(Daq_tmc) ); memset( trigger, 0, sizeof(trigger) ); for ( i = 0; i < TMC_MOD; i++ ) { while ( mm[i]->dready != 1 ) ; eventsum += ( eventnum[i] = ( mm[i]->ntotal - 5 )/ 2 ); } if( ( pevt = evt = (event *)malloc( eventsum * sizeof(event) ) ) == NULL ) exit_err( "TMC: Allocation error!" ); for( i = 0; i < TMC_MOD; i++ ){ for( j = 0; j < eventnum[i]; j++){ ch = 0x001f &( pevt->header = mm[i]->data[j].header ); time = 0x0fff &( pevt->content = mm[i]->data[j].content ); pevt++; if( tmc.tm[i][ch] == -1 ) tmc.tm[i][ch] = time; else continue; if ( ( nb = cfg->dc_nb[i][ch] ) >= 0 && ( xy = cfg->dc_xy[i][ch] ) >= 0 ) { trigger[ nb ][ xy ]++; } } } #if 0 /* Filtering here */ if ( level == 2 ) { for ( i = 0; i < DC_NUMBER; i++ ) { for ( k = 0; k < DC_XY; k++ ) { if ( trigger[i][k] != 1 ) { free(evt); return -1; } } } } else if ( level == 1 ) { for ( i = 0; i < DC_NUMBER; i++ ) { hit = 0; for ( k = 0; k < DC_XY; k++ ) { if ( trigger[i][k] > 0 ) hit++; } if( hit < 2 ){ free(evt); return -1; } } } #endif /* Write data to File */ WriteFLG( TMC_FLG, fp ); datasize = eventsum * sizeof(event); fwrite( &datasize, sizeof(int), 1, fp ); fwrite( evt, sizeof(event), eventsum, fp ); WriteFLG( TMC_END_FLG, fp ); free(evt); return 0; } /*---------------------------------------------------------- EndTMC ---------------------------------------------------------*/ void EndTMC( void ) { #ifdef VMELIB06_USE int i; for ( i = 0; i < TMC_MOD; i++ ) { vme_mapclose( (caddr_t)( TMC_ADDR + ADDR_STEP * (long)i ), VME_A24D16MODE ); } #else vme_mapclose(VME_A24D16MODE); #endif } /* ------EOF---------------------------------------------*/