/* */ /* File Name : adc.c */ /* Creation Date : Oct 31 2000 */ /* Author : Y.Nakaune */ /* Update History */ /* Nov 15 H.Kuashige : */ /* add DISABLE LAM in InitADC */ /* add ENABLE LAM for ReadyADC */ #include #include #include #include #include #include #include "camlib.h" #include "buf.h" #include "adc.h" static Evt_adc adc[ADC_MAX]; #define ST_MAX 25 static int ST_ADC[ST_MAX]; /*----------------------------------------------------- InitADC Initializing the Crate Controller ---------------------------------------------------*/ void InitADC( void ) { int status; int naf, q, x; u_short dummy; int n, i; if ( ( status = COPEN() ) != 0 ) exit_err( "ERR: CAMAC busy" ); if ( status ) fprintf( stderr, "Open crate controller, status=%d\n", status ); status = CSETCR(0); if ( status ) fprintf( stderr, "Set crate number to zero, status=%d\n", status ); status = CRESET(); if ( status ) fprintf( stderr, "Reset crate controller, status=%d\n", status ); status = CGENZ(); if ( status ) fprintf( stderr, "Initialize crate controller, status=%d\n", status ); status = CGENC(); if ( status ) fprintf( stderr, "Clear crate controller, status=%d\n", status ); status = CREMI(); if ( status ) fprintf( stderr, "Remove inhibit line, status=%d\n", status ); printf( "\n" ); /* check adc module and disable LAM */ for ( n = 0; n < number_naf_entry; n++ ) { naf = array_naf[n] + F_CLR_REGISTER ; status = CAMACW( naf, &dummy, &q, &x ); if ( x != 1 ) { printf( "No X-response for ADC[N =%2d], status=%d\n", ( ( naf >> 9 ) & 0x1F ), status ); exit_err( "ERR : CAMAC No-X response" ); } else { printf( "ADC[N =%2d A=%2d] is active\n",( ( naf >> 9 ) & 0x1F ),( ( naf >> 5 ) & 0x0F ) ); } } /* enable LAM for the first module */ /* naf = array_naf[0] + F_ENABLE_LAM; */ /* status = CAMACW( naf, &dummy, &q, &x ); */ for (n =0 ; n< ST_MAX; ++n){ ST_ADC[n] = 0; } for ( i = 0; i < number_naf_entry; i++ ) { n = (array_naf[i] >>9 ) & 0x1F; if (n < ST_MAX) ST_ADC[n] = -1; } } /*----------------------------------------------------- ReadyADC Clear All channel ---------------------------------------------------*/ void ReadyADC ( void ) { int n; int status, naf, q, x; u_short dummy; /* clear register */ for ( n = 1; n < ST_MAX; n++ ) { if (ST_ADC[n]) { naf = (n<<9) + F_CLR_REGISTER; status = CAMACW( naf, &dummy, &q, &x ); } } /* clear LAM */ naf = array_naf[0] + F_CLR_LAM; status = CAMACW( naf, &dummy, &q, &x ); } /*----------------------------------------------------- test_LAM Look At Me ---------------------------------------------------*/ int test_LAM() { u_short data; int q, x, status; int naf; naf = array_naf[0] + F_TEST_LAM; /* naf = array_naf[0] ; */ status = CAMACW( naf, &data, &q, &x); /* return 'true' if LAM is asserted */ return( q ); } /*----------------------------------------------------- ReadADC Read all ADC channel ---------------------------------------------------*/ void ReadADC( FILE *fp ) { int n = 0; int count; int status, q, x; u_short s_data; int adcsize; /* Initializing data buffer */ memset( adc, 0, sizeof(Evt_adc) * ADC_MAX ); /* Test Look-At-Me */ count = 10000; while ( !test_LAM() ) { #ifdef ENABLE_WAIT usleep(1); #endif if ( count-- == 0 ) { printf("CAMAC : Wait LAM time out !\n"); exit(1); } } /* Read All channel */ for ( n= 0; n < number_naf_entry; n++ ) { status = CAMACW( array_naf[n], &s_data, &q, &x ); adc[n].header = array_naf[n] >> 5; adc[n].content = s_data; } adcsize = sizeof(Evt_adc) * number_naf_entry; /* Write data size */ WriteFLG( ADC_FLG, fp ); fwrite( &adcsize, sizeof(int), 1, fp ); fwrite( adc, adcsize, 1, fp ); WriteFLG( ADC_END_FLG, fp ); } /*----------------------------------------------------- EndADC Close the crate controller, and so on ---------------------------------------------------*/ void EndADC( void ) { int status; status = CCLOSE(); if ( status ) { fprintf( stderr, "Close crate controller, status=%d\n", status ); } } /*----------------------------------------------------- InitScaler Initializing the CAMAC Scaler ---------------------------------------------------*/ void InitScaler( void ) { int status; int naf, q, x; u_short dummy; /* clear scaler*/ naf = ( SCALER_STATION << 9 ) + F_CLR_REGISTER; status = CAMACW( naf, &dummy, &q, &x ); } /*----------------------------------------------------- ReadScaler Read the CAMAC Scaler ---------------------------------------------------*/ void ReadScaler( FILE *fp ) { int a, status, ch; int naf, q, x; u_short s_data; /* read scaler*/ for ( ch = 0; ch < N_SCALER_CH; ch++ ) { naf = ( SCALER_STATION << 9 ) + ( ch << 5 ) + F_READ; status = CAMACW( naf, &s_data, &q, &x ); scaler_data[ch] = s_data; } /* SCALER Flg */ WriteFLG( SCALER_FLG, fp ); for ( a = 0; a < N_SCALER_CH; a++ ) { fwrite( &scaler_data[a], sizeof(int), 1, fp ); } WriteFLG( SCALER_END_FLG, fp ); } /*----------EOF-------------------------------------*/