From 7dd8631fa58c1c1b3d32ad710ce3b1b9b09e894b Mon Sep 17 00:00:00 2001 From: Satoru Yamada <satoru.yamada@kek.jp> Date: Mon, 15 Jun 2020 23:14:28 +0900 Subject: [PATCH] Separate check_hdr_trl part as a function. --- Pcie40Applications/main_pcie40_ul.cpp | 420 +++++++++++++------------- 1 file changed, 215 insertions(+), 205 deletions(-) diff --git a/Pcie40Applications/main_pcie40_ul.cpp b/Pcie40Applications/main_pcie40_ul.cpp index c0a5014..15f67df 100644 --- a/Pcie40Applications/main_pcie40_ul.cpp +++ b/Pcie40Applications/main_pcie40_ul.cpp @@ -12,6 +12,11 @@ extern "C" int ecs_write(int dev, int bar, unsigned add, int val); //#define DEBUG +#define OLD_RUN 1 +#define OLD_EVENT 2 +#define NO_TRAILER 3 + + const int CRC16_XMODEM_TABLE[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, @@ -102,7 +107,6 @@ int crc_calc_event( unsigned int* data, int start_pos ){ crc_calc( &crc, &exp_run ); first_crc = crc ; - #ifdef DEBUG printf("Start\n"); for( i = start_pos ; i < start_pos + size ; i++){ @@ -152,8 +156,8 @@ int crc_calc_event( unsigned int* data, int start_pos ){ }else{ good_flag = 1; - // printf("%.2d Good ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", - // link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); + // printf("%.2d Good ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", + // link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); } link_num++; @@ -161,11 +165,11 @@ int crc_calc_event( unsigned int* data, int start_pos ){ } if( bad ){ - for( i = 0 ; i < size ; i++){ - printf("%.8x ", data[ ( start_pos + i ) % DMASIZE ] ); - if( i % 8 == 7 ) printf("\n"); - } - printf("\n"); + // for( i = 0 ; i < size ; i++){ + // printf("%.8x ", data[ ( start_pos + i ) % DMASIZE ] ); + // if( i % 8 == 7 ) printf("\n"); + // } + // printf("\n"); good_flag = -2; } return good_flag; @@ -173,220 +177,226 @@ int crc_calc_event( unsigned int* data, int start_pos ){ } - int main (int argc ,char** argv) { - // Stop trigger - /* system( "ssh robbep@vme \"resetft -53\"" ) ; */ - /* sleep( 2 ) ; */ - - int res = ecs_open( 0 , 0 ) ; - if ( -1 == res ) printf("ERROR: Could not open device (BAR 0)\n") ; - else printf("SUCCESS: Device opened for DMA\n"); - res = ecs_open( 0 , 2 ) ; - // ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; - ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; - pcie40_b2dmabufferreset( 0 ) ; - // Start trigger - // fp = popen( "ssh robbep@vme \"trigft -53 pulse 1000 -1\"" , "r" ) ; - // sleep( 2 ) ; - /* while( fgets( buf , sizeof(buf) , fp ) != NULL ) { */ - /* // printf( "A= %d\n" , strncmp( exp , buf , 3 ) ) ; */ - /* // printf( strncmp( &buf[0] , 'exp' , 1 ) ) ; */ - /* tmp = strdup( buf ) ; */ - /* if ( strncmp( exp , buf , 3 ) == 0 ) { */ - /* //printf( '%s\n' , buf ) ; */ - /* // printf( "ICI\n" ) ; */ - /* ptr = strtok( tmp , " " ) ; */ - /* ptr = strtok( NULL , " " ) ; */ - /* ptr = strtok( NULL , " " ) ; */ - /* ptr = strtok( NULL , " " ) ; */ - /* // while ( ptr != NULL ) { */ - /* run = atoi( ptr ) ; */ - /* // ptr = strtok( NULL , " " ) ; */ - /* } */ - /* //} */ - /* } */ - /* pclose( fp ) ; */ - //sleep( 2 ) ; - - int run = 0 ; - FILE* fp; - char * ptr ; - char * tmp ; - char buf[1000] ; - char exp[] = "exp" ; - - unsigned int * data ; - double time_spent ; - int retry ; - int bad ; - int evn ; - evn = 0 ; - bad = 0 ; - retry = 0 ; - int pctime, putime ; - pctime = 0 ; - putime = 0 ; - - int i ; - int size, size_256, currentEventIndex, crc, nErr, nEvt, nRetry, nMissed ; - currentEventIndex = 0 ; - nErr = 0 ; - nEvt = 0 ; - nRetry = 0 ; - nMissed = 0 ; - int read_dma = 1 ; - clock_t begin = clock() ; - int size_zero_cnt = 0; - - int start_flag = 1; - - int magic; - int size_link; - - int offset; - unsigned int next_exp_run, cur_exp_run = 0, evenum, ctime, utime, first_eve = 0; - int start_pos = 0; - int cnt = 0, dma_cnt = 0; - int eve_cnt = 0, first_event_flag = 0, crc_err_eve_cnt = 0; +int print_line(unsigned int *data, int cur_pos ){ + + printf("pos %.8x : ", cur_pos); + for( int i = 0; i < 8 ; i++ ){ + printf("%.8x ", data[ ( cur_pos + i - ( cur_pos % 8 ) ) % DMASIZE ]); + } + printf("\n"); + + return 0; +} + +int check_hdr_trl( unsigned int *data, int cur_pos, int *event_flag, + unsigned int cur_exp_run, unsigned int cur_eve, unsigned int *next_exp_run, unsigned int *next_eve ){ + + unsigned int trailer = 0; + int size = data[ cur_pos ] ; + *next_exp_run = 0; + *event_flag = 0; + *next_eve = 0; + + if ( ( ( data[ ( cur_pos + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ + *next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ] ; + *next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; + trailer = data[ ( cur_pos + size + DMASIZE - 1 ) % DMASIZE ]; + if( *next_exp_run < cur_exp_run && cur_exp_run != 0 ){ + printf( "OLD-run's event : run %.8x eve %.8x ( cur run %.8x eve %.8x) : pos %.8x size %d\n", + *next_exp_run , *next_eve, cur_exp_run, cur_eve, cur_pos, size ); + *event_flag = OLD_RUN; + }else if( *next_eve <= cur_eve && cur_exp_run != 0 ){ + printf( "OLD or same event : run %.8x eve %.8x ( cur run %.8x eve %.8x) : pos %.8x size %d\n", + *next_exp_run , *next_eve, cur_exp_run, cur_eve, cur_pos, size ); + *event_flag = OLD_EVENT; + }else if( trailer != 0x7fff0006 ){ + printf( "No trailer [%.8x] : run %.8x eve %.8x ( cur run %.8x eve %.8x) : pos %.8x size %d\n", + trailer, *next_exp_run , *next_eve, cur_exp_run, cur_eve, cur_pos, size ); + print_line( data, ( cur_pos + size + DMASIZE - 1 ) % DMASIZE ); + *event_flag = NO_TRAILER; + }else{ + printf( "Good event : run %.8x eve %.8x ( cur run %.8x eve %.8x) : pos %.8x size %d\n", + *next_exp_run , *next_eve, cur_exp_run, cur_eve, cur_pos, size ); + } + print_line( data, cur_pos ); + return 1; + } + return 0; +} + + // if( dma_cnt % 1000 == 0 ){ + // printf( "7f7f is found. TRAILER %.8x eve %.8x exprun %.8x pos 0x%x dmacnt %d\n", + // data[ ( cur_pos + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ], + // next_eve, next_exp_run, ( cur_pos + j + 1 ) % DMASIZE, dma_cnt ); + // } + + +int main (int argc ,char** argv) { + + int res = ecs_open( 0 , 0 ) ; + if ( -1 == res ) printf("ERROR: Could not open device (BAR 0)\n") ; + else printf("SUCCESS: Device opened for DMA\n"); + res = ecs_open( 0 , 2 ) ; + // ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; + pcie40_b2dmabufferreset( 0 ) ; + + int run = 0 ; + FILE* fp; + char * ptr ; + char * tmp ; + char buf[1000] ; + char exp[] = "exp" ; + + unsigned int * data ; + double time_spent ; + int retry ; + int bad ; + int evn ; + evn = 0 ; + bad = 0 ; + retry = 0 ; + int pctime, putime ; + pctime = 0 ; + putime = 0 ; + + int i ; + int size, size_256, currentEventIndex, crc, nErr, nEvt, nRetry, nMissed ; + currentEventIndex = 0 ; + nErr = 0 ; + nEvt = 0 ; + nRetry = 0 ; + nMissed = 0 ; + + clock_t begin = clock() ; + int size_zero_cnt = 0; + + int start_flag = 1; + + int magic; + int size_link; + + int offset; + unsigned int next_exp_run, cur_exp_run = 0, evenum, ctime, utime, first_eve = 0; + int start_pos = 0; + int cnt = 0, dma_cnt = 0; + int eve_cnt = 0, first_event_flag = 0, crc_err_eve_cnt = 0; - int cur_pos = 0; - int ret = 0; - unsigned int cur_eve = 0, next_eve = 0; - int mod_pos = 0; - - // if ( ( nEvt % 1000 == 1 ) && ( size != 0 ) ) printf( "Event number %d\n", nEvt ) ; + int cur_pos = 0; + int ret = 0, event_flag = 0; + unsigned int cur_eve = 0, next_eve = 0; + int mod_pos = 0; + + // if ( ( nEvt % 1000 == 1 ) && ( size != 0 ) ) printf( "Event number %d\n", nEvt ) ; + + // + // Read the 1st data + // + res = pcie40_b2dmapointerread( 0 , &data ) ; + dma_cnt++; + if ( start_flag == 1 ){ + ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + start_flag = 0; + } + // + // Main loop + // + while( 1 ){ // - // Read the 1st data + // Search 7f7f header magic word. // - if ( read_dma == 1 ) { - // set busy - //res = ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; - //sleep(5); + while( 1 ){ + ret = check_hdr_trl( data, cur_pos, &event_flag, cur_exp_run, cur_eve, &next_exp_run, &next_eve ); + if( ret == 1 && event_flag == 0){ + break; + } + usleep(1000); // to avoid BUSY loop + + // + // Refresh DMA memory + // res = pcie40_b2dmapointerread( 0 , &data ) ; dma_cnt++; - if ( start_flag == 1 ){ - ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; - start_flag = 0; + int wait_flag = 0; + + // + // Scan to find x"7f7f...." + // + if( cnt > 100 ){ + cnt = 0; + int j = 0 ; + for ( j = 0 ; j < DMASIZE ; ++j ) { + ret = check_hdr_trl( data, cur_pos + j, &event_flag, cur_exp_run, + cur_eve, &next_exp_run, &next_eve ); + if( ret == 1 ){ + cur_pos = cur_pos + j; + break; + } + } } - //usleep( 5 ) ; - // release busy - //res = ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; - read_dma = 1 ; - } + cnt++; + } // - // Main loop + // Check event-data // - while( 1 ){ - while( 1 ){ - // if( ( data[ ( cur_pos + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ){ - // printf("HEAD %.8x TRAIL %.8x\n", data[ ( cur_pos + 1 ) % DMASIZE ], - // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] ); - // } - // if( !( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) ) ){ - if( !( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) - || data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ) ){ - break; - } - // usleep(1000); // to avoid BUSY loop - - // - // Refresh DMA memory - // - res = pcie40_b2dmapointerread( 0 , &data ) ; - dma_cnt++; - int wait_flag = 0; - - // - // Scan to find x"7f7f...." - // - if( cnt > 100 ){ - cnt = 0; - int j = 0 ; - for ( j = 0 ; j < DMASIZE ; ++j ) { -#ifdef DEBUG - printf("%.8x %d\n", data[ ( cur_pos + j + 1 ) % DMASIZE ], cur_pos + j ); + +#ifdef FULL_CHECK + if( ( data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 2 + DMASIZE ) % DMASIZE ] != 0x00000000 ) || + ( data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 3 + DMASIZE ) % DMASIZE ] != 0x7faf0000 ) ){ + printf("Full ! %.8x %.8x\n", + data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 3 + DMASIZE ) % DMASIZE ], + data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 2 + DMASIZE ) % DMASIZE ] ); + } #endif - if ( ( ( data[ ( cur_pos + j + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ - next_eve = data[ ( cur_pos + j + 3 ) % DMASIZE ]; - next_exp_run = data[ ( cur_pos + j + 2 ) % DMASIZE ] ; - - if( dma_cnt % 1000 == 0 ){ - printf( "7f7f is found. TRAILER %.8x eve %.8x exprun %.8x pos %d dmacnt %d\n", - data[ ( cur_pos + j + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ], - next_eve, next_exp_run, ( cur_pos + j + 1 ) % DMASIZE, dma_cnt ); - } - - if( next_eve > cur_eve && next_exp_run >= cur_exp_run ){ - /* if( data[ ( ( data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] ) - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ){ */ - /* usleep(1000); */ - /* break; */ - /* } */ - cur_pos = ( cur_pos + j + DMASIZE ) % DMASIZE; - size = data[ cur_pos ]; - // printf( "Detect new Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); - break; - }else{ - // printf( "Detect Old Event number prev %.8x new %.8x pos %d\n" , cur_eve , next_eve, ( cur_pos + j + DMASIZE ) % DMASIZE ); - break; - } - }else{ - continue; - } - } - } - cnt++; - } - size = data[ cur_pos ] ; - // - // Check event-data - // - next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; - next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ]; - - if( first_event_flag == 0 || cur_exp_run != next_exp_run ){ - first_event_flag = 1; - first_eve = next_eve; - // crc_err_eve_cnt = 0; - // eve_cnt = 0; - } - eve_cnt++; - ret = crc_calc_event( data, cur_pos ); - // printf("crc ret %d cnt %d\n", eve_cnt); - if( ret != 1 ){ - crc_err_eve_cnt++; - } + size = data[ cur_pos ] ; - if(ret != 1 || eve_cnt % 100 == 0 ){ - printf( "Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); - } - cur_eve = next_eve; - cur_exp_run = next_exp_run; - if(ret != 1 || eve_cnt % 100 == 0 ){ - printf("STATUS : tot eve %d read eve %d err_eve %d run %d\n", next_eve - first_eve, eve_cnt, crc_err_eve_cnt, cur_exp_run ); - } - mod_pos = ( cur_pos + size ) % 8; - if( mod_pos != 0 ){ - cur_pos = ( cur_pos + size + DMASIZE + ( 8 - mod_pos) ) % DMASIZE ; - }else{ - cur_pos = ( cur_pos + size + DMASIZE ) % DMASIZE ; - } + next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; + next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ]; + + if( first_event_flag == 0 || cur_exp_run != next_exp_run ){ + first_event_flag = 1; + first_eve = next_eve; + // crc_err_eve_cnt = 0; + // eve_cnt = 0; + } + eve_cnt++; + ret = crc_calc_event( data, cur_pos ); + // printf("crc ret %d cnt %d\n", eve_cnt); + if( ret != 1 ){ + crc_err_eve_cnt++; + } + if(ret != 1 || eve_cnt % 100 == 0 ){ + printf( "Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); + } + cur_eve = next_eve; + cur_exp_run = next_exp_run; + if(ret != 1 || eve_cnt % 100 == 0 ){ + printf("STATUS : tot eve %d read eve %d err_eve %d run %.8x\n", next_eve - first_eve, eve_cnt, crc_err_eve_cnt, cur_exp_run ); } + mod_pos = ( cur_pos + size ) % 8; + if( mod_pos != 0 ){ + cur_pos = ( cur_pos + size + DMASIZE + ( 8 - mod_pos) ) % DMASIZE ; + }else{ + cur_pos = ( cur_pos + size + DMASIZE ) % DMASIZE ; + } + + } - clock_t end = clock(); - time_spent += (double)(end - begin) / CLOCKS_PER_SEC; + clock_t end = clock(); + time_spent += (double)(end - begin) / CLOCKS_PER_SEC; - ecs_close( 0 , 0 ) ; + ecs_close( 0 , 0 ) ; - printf( "Timing = %f s\n" , time_spent ) ; - printf( "Number of events = %d \n", nEvt ) ; - printf( "Number of errors = %d \n", nErr ) ; - printf( "Number of retries = %d \n", nRetry ) ; - printf( "Number of missed = %d \n", nMissed ) ; - return 0 ; - } + printf( "Timing = %f s\n" , time_spent ) ; + printf( "Number of events = %d \n", nEvt ) ; + printf( "Number of errors = %d \n", nErr ) ; + printf( "Number of retries = %d \n", nRetry ) ; + printf( "Number of missed = %d \n", nMissed ) ; + return 0 ; +} -- GitLab