diff --git a/data_dev/app/src/dmaRate.cpp b/data_dev/app/src/dmaRate.cpp index 6616dd9..486509b 100644 --- a/data_dev/app/src/dmaRate.cpp +++ b/data_dev/app/src/dmaRate.cpp @@ -1,16 +1,18 @@ /** + *----------------------------------------------------------------------------- + * Company : SLAC National Accelerator Laboratory *----------------------------------------------------------------------------- * Description: - * This program will open up a AXIS DMA port and attempt to read data. - * ---------------------------------------------------------------------------- - * This file is part of the aes_stream_drivers package. It is subject to - * the license terms in the LICENSE.txt file found in the top-level directory - * of this distribution and at: + * Opens an AXIS DMA port and attempts to read data. + *----------------------------------------------------------------------------- + * This file is part of the aes_stream_drivers package. It is subject to the + * license terms in the LICENSE.txt file found in the top-level directory of + * this distribution and at: * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. * No part of the aes_stream_drivers package, including this file, may be * copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE.txt file. - * ---------------------------------------------------------------------------- + *----------------------------------------------------------------------------- **/ #include @@ -23,67 +25,67 @@ #include #include #include -//#include +// #include using namespace std; #define MAX_RET_CNT_C 1000 -const char * argp_program_version = "dmaRate 1.0"; -const char * argp_program_bug_address = "rherbst@slac.stanford.edu"; +const char *argp_program_version = "dmaRate 1.0"; +const char *argp_program_bug_address = "rherbst@slac.stanford.edu"; struct PrgArgs { - const char * path; - uint32_t count; + const char *path; + uint32_t count; }; -#define DEF_DEV_PATH "/dev/datadev_0" -#define DEF_COUNT 10000000 -static struct PrgArgs DefArgs = { DEF_DEV_PATH, DEF_COUNT }; +#define DEF_DEV_PATH "/dev/datadev_0" +#define DEF_COUNT 10000000 +static struct PrgArgs DefArgs = {DEF_DEV_PATH, DEF_COUNT}; -static char args_doc[] = ""; -static char doc[] = ""; +static char args_doc[] = ""; +static char doc[] = ""; -#define STRING(N) #N -#define XSTRING(N) STRING(N) +#define STRING(N) #N +#define XSTRING(N) STRING(N) static struct argp_option options[] = { - { "path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device to use. Default=" DEF_DEV_PATH, 0}, - { "count", 'c', "COUNT", OPTION_ARG_OPTIONAL, "Total iterations. Default=" XSTRING(DEF_COUNT),0}, + {"path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device. Default=" DEF_DEV_PATH, 0}, + {"count", 'c', "COUNT", OPTION_ARG_OPTIONAL, "Total iterations. Default=" XSTRING(DEF_COUNT), 0}, {0} }; -error_t parseArgs ( int key, char *arg, struct argp_state *state ) { +error_t parseArgs(int key, char *arg, struct argp_state *state) { struct PrgArgs *args = (struct PrgArgs *)state->input; switch(key) { - case 'p': args->path = arg; break; - case 'c': args->count = atoi(arg); break; + case 'p': args->path = arg; break; + case 'c': args->count = atoi(arg); break; default: return ARGP_ERR_UNKNOWN; break; } - return(0); + return 0; } -static struct argp argp = {options,parseArgs,args_doc,doc}; - -int main (int argc, char **argv) { - uint8_t mask[DMA_MASK_SIZE]; - int32_t ret; - int32_t s; - uint32_t rxFlags[MAX_RET_CNT_C]; - //PrbsData prbs(32,4,1,2,6,31); - void ** dmaBuffers; - uint32_t dmaSize; - uint32_t dmaCount; - uint32_t dmaIndex[MAX_RET_CNT_C]; - int32_t dmaRet[MAX_RET_CNT_C]; - int32_t x; - float last; - float rate; - float bw; - float duration; - int32_t max; - int32_t total; - - uint32_t getCnt = MAX_RET_CNT_C; +static struct argp argp = {options, parseArgs, args_doc, doc}; + +int main(int argc, char **argv) { + uint8_t mask[DMA_MASK_SIZE]; + int32_t ret; + int32_t s; + uint32_t rxFlags[MAX_RET_CNT_C]; + // PrbsData prbs(32,4,1,2,6,31); + void **dmaBuffers; + uint32_t dmaSize; + uint32_t dmaCount; + uint32_t dmaIndex[MAX_RET_CNT_C]; + int32_t dmaRet[MAX_RET_CNT_C]; + int32_t x; + float last; + float rate; + float bw; + float duration; + int32_t max; + int32_t total; + + uint32_t getCnt = MAX_RET_CNT_C; struct timeval sTime; struct timeval eTime; @@ -92,77 +94,81 @@ int main (int argc, char **argv) { struct PrgArgs args; - memcpy(&args,&DefArgs,sizeof(struct PrgArgs)); - argp_parse(&argp,argc,argv,0,0,&args); + // Initialize program arguments with default values + memcpy(&args, &DefArgs, sizeof(struct PrgArgs)); + argp_parse(&argp, argc, argv, 0, 0, &args); printf(" maxCnt size count duration rate bw Read uS Return uS\n"); + // Initialize DMA mask dmaInitMaskBytes(mask); - memset(mask,0xFF,DMA_MASK_SIZE); + memset(mask, 0xFF, DMA_MASK_SIZE); - if ( (s = open(args.path, O_RDWR)) <= 0 ) { - printf("Error opening %s\n",args.path); - return(1); + // Open device + if ((s = open(args.path, O_RDWR)) <= 0) { + printf("Error opening %s\n", args.path); + return 1; } -#if 1 - if ( (dmaBuffers = dmaMapDma(s,&dmaCount,&dmaSize)) == NULL ) { + // Map DMA buffers + if ((dmaBuffers = dmaMapDma(s, &dmaCount, &dmaSize)) == NULL) { printf("Failed to map dma buffers!\n"); - return(0); + return 0; } -#endif - if (dmaSetMaskBytes(s,mask) != 0 ) { + // Set DMA mask + if (dmaSetMaskBytes(s, mask) != 0) { printf("Failed to get receive dma!\n"); - return(0); + return 0; } - while(1) { - - bw = 0.0; - rate = 0.0; - last = 0.0; - max = 0; - total = 0; - gettimeofday(&sTime,NULL); - - while ( rate < args.count ) { - - // DMA Read - gettimeofday(&(pTime[0]),NULL); - ret = dmaReadBulkIndex(s,getCnt,dmaRet,dmaIndex,rxFlags,NULL,NULL); // 24 usec - gettimeofday(&(pTime[1]),NULL); - - for (x=0; x < ret; ++x) { - if ( (last = dmaRet[x]) > 0.0 ) { + // Main processing loop + while (1) { + bw = 0.0; + rate = 0.0; + last = 0.0; + max = 0; + total = 0; + gettimeofday(&sTime, NULL); + + while (rate < args.count) { + // Perform DMA Read + gettimeofday(&(pTime[0]), NULL); + ret = dmaReadBulkIndex(s, getCnt, dmaRet, dmaIndex, rxFlags, NULL, NULL); + gettimeofday(&(pTime[1]), NULL); + + // Process read data + for (x = 0; x < ret; ++x) { + if ((last = dmaRet[x]) > 0.0) { rate += 1.0; bw += (last * 8.0); } } - gettimeofday(&(pTime[2]),NULL); - if ( ret > 0 ) dmaRetIndexes(s,ret,dmaIndex); // 721 usec - gettimeofday(&(pTime[3]),NULL); + gettimeofday(&(pTime[2]), NULL); + if (ret > 0) dmaRetIndexes(s, ret, dmaIndex); + gettimeofday(&(pTime[3]), NULL); - if ( total == 0 ) if ( ret > max ) max = ret; - total += ret; // 0 usec + if (total == 0) if (ret > max) max = ret; + total += ret; } - gettimeofday(&eTime,NULL); - - timersub(&eTime,&sTime,&dTime); - duration = dTime.tv_sec + (float)dTime.tv_usec/1000000.0; + // Calculate duration and data rates + gettimeofday(&eTime, NULL); + timersub(&eTime, &sTime, &dTime); + duration = dTime.tv_sec + (float)dTime.tv_usec / 1000000.0; rate = rate / duration; - bw = bw / duration; + bw = bw / duration; - printf("%8i %1.3e %8i %1.2e %1.2e %1.2e %8li %8li \n",max,last,args.count,duration,rate,bw, - (pTime[1].tv_usec-pTime[0].tv_usec), (pTime[3].tv_usec-pTime[2].tv_usec)); + // Output results + printf("%8i %1.3e %8i %1.2e %1.2e %1.2e %8li %8li \n", + max, last, args.count, duration, rate, bw, + (pTime[1].tv_usec - pTime[0].tv_usec), (pTime[3].tv_usec - pTime[2].tv_usec)); rate = 0.0; - bw = 0.0; + bw = 0.0; } - return(0); + return 0; } - diff --git a/data_dev/app/src/dmaWrite.cpp b/data_dev/app/src/dmaWrite.cpp index d338ffe..513ff0a 100644 --- a/data_dev/app/src/dmaWrite.cpp +++ b/data_dev/app/src/dmaWrite.cpp @@ -1,16 +1,18 @@ /** + *----------------------------------------------------------------------------- + * Company : SLAC National Accelerator Laboratory *----------------------------------------------------------------------------- * Description: - * This program will open up a AXIS DMA port and attempt to write data. - * ---------------------------------------------------------------------------- - * This file is part of the aes_stream_drivers package. It is subject to - * the license terms in the LICENSE.txt file found in the top-level directory - * of this distribution and at: - * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. + * Opens an AXIS DMA port to write data using the aes_stream_drivers package. + *----------------------------------------------------------------------------- + * This file is part of the aes_stream_drivers package. It is subject to the + * license terms in the LICENSE.txt file found in the top-level directory of + * this distribution and at: + * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. * No part of the aes_stream_drivers package, including this file, may be * copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE.txt file. - * ---------------------------------------------------------------------------- + *----------------------------------------------------------------------------- **/ #include @@ -26,154 +28,165 @@ #include using namespace std; -const char * argp_program_version = "dmaWrite 1.0"; -const char * argp_program_bug_address = "rherbst@slac.stanford.edu"; +const char *argp_program_version = "dmaWrite 1.0"; +const char *argp_program_bug_address = "rherbst@slac.stanford.edu"; +// Program arguments structure struct PrgArgs { - const char * path; - uint32_t dest; - uint32_t size; - uint32_t count; - uint32_t prbsDis; - uint32_t idxEn; + const char *path; + uint32_t dest; + uint32_t size; + uint32_t count; + uint32_t prbsDis; + uint32_t idxEn; }; +// Default program arguments static struct PrgArgs DefArgs = { "/dev/datadev_0", 0, 1000, 1, 0, 0 }; -static char args_doc[] = "dest"; -static char doc[] = " Dest is passed as an integer."; +// Documentation for arguments +static char args_doc[] = "dest"; +static char doc[] = "Dest is passed as an integer."; +// Options structure static struct argp_option options[] = { - { "path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device to use. Default=/dev/datadev_0.",0}, - { "prbsdis", 'd', 0, OPTION_ARG_OPTIONAL, "Disable PRBS generation.",0}, - { "size", 's', "SIZE", OPTION_ARG_OPTIONAL, "Size of data to generate. Default=1000",0}, - { "count", 'c', "COUNT", OPTION_ARG_OPTIONAL, "Number of frames to generate. Default=1",0}, - { "indexen", 'i', 0, OPTION_ARG_OPTIONAL, "Use index based transmit buffers.",0}, - {0} + { "path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device to use. Default=/dev/datadev_0.", 0 }, + { "prbsdis", 'd', 0, OPTION_ARG_OPTIONAL, "Disable PRBS generation.", 0 }, + { "size", 's', "SIZE", OPTION_ARG_OPTIONAL, "Size of data to generate. Default=1000", 0 }, + { "count", 'c', "COUNT", OPTION_ARG_OPTIONAL, "Number of frames to generate. Default=1", 0 }, + { "indexen", 'i', 0, OPTION_ARG_OPTIONAL, "Use index based transmit buffers.", 0 }, + { 0 } }; -error_t parseArgs ( int key, char *arg, struct argp_state *state ) { +// Argument parsing function +error_t parseArgs(int key, char *arg, struct argp_state *state) { struct PrgArgs *args = (struct PrgArgs *)state->input; - switch(key) { + switch (key) { case 'p': args->path = arg; break; case 'd': args->prbsDis = 1; break; - case 's': args->size = strtol(arg,NULL,10); break; - case 'c': args->count = strtol(arg,NULL,10); break; + case 's': args->size = strtol(arg, NULL, 10); break; + case 'c': args->count = strtol(arg, NULL, 10); break; case 'i': args->idxEn = 1; break; case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: args->dest = strtol(arg,NULL,10); break; - default: argp_usage(state); break; - } - break; + if (state->arg_num == 0) { + args->dest = strtol(arg, NULL, 10); + } else { + argp_usage(state); + } + break; case ARGP_KEY_END: - if ( state->arg_num < 1) argp_usage(state); - break; - default: return ARGP_ERR_UNKNOWN; break; + if (state->arg_num < 1) argp_usage(state); + break; + default: + return ARGP_ERR_UNKNOWN; } - return(0); + return 0; } -static struct argp argp = {options,parseArgs,args_doc,doc}; - -int main (int argc, char **argv) { - int32_t s; - int32_t ret; - uint32_t count; - fd_set fds; - void * txData=NULL; - PrbsData prbs(32,4,1,2,6,31); - void ** dmaBuffers=NULL; - uint32_t dmaSize; - uint32_t dmaCount; - int32_t dmaIndex=-1; - bool prbValid; - struct timeval startTime; - struct timeval endTime; - struct timeval diffTime; - - struct timeval timeout; +// Definition of the argp structure to parse command line arguments +static struct argp argp = { options, parseArgs, args_doc, doc }; + +int main(int argc, char **argv) { + int32_t s; + int32_t ret; + uint32_t count; + fd_set fds; + void *txData = NULL; + PrbsData prbs(32, 4, 1, 2, 6, 31); // Example PRBS (Pseudo-Random Binary Sequence) generator initialization + void **dmaBuffers = NULL; + uint32_t dmaSize; + uint32_t dmaCount; + int32_t dmaIndex = -1; + bool prbValid; + struct timeval startTime, endTime, diffTime, timeout; struct PrgArgs args; - memcpy(&args,&DefArgs,sizeof(struct PrgArgs)); - argp_parse(&argp,argc,argv,0,0,&args); + // Initialize program arguments with default values + memcpy(&args, &DefArgs, sizeof(struct PrgArgs)); - if ( (s = open(args.path, O_RDWR)) <= 0 ) { - printf("Error opening %s\n",args.path); - return(1); + // Parse command line arguments + argp_parse(&argp, argc, argv, 0, 0, &args); + + // Open device or file + if ((s = open(args.path, O_RDWR)) <= 0) { + printf("Error opening %s\n", args.path); + return 1; } - if ( args.idxEn ) { - if ( (dmaBuffers = dmaMapDma(s,&dmaCount,&dmaSize)) == NULL ) { + // DMA or regular buffer setup based on idxEn flag + if (args.idxEn) { + if ((dmaBuffers = dmaMapDma(s, &dmaCount, &dmaSize)) == NULL) { printf("Failed to map dma buffers!\n"); - return(0); + return 0; } - } - else { - if ((txData = malloc(args.size)) == NULL ) { - printf("Failed to allocate rxData!\n"); - return(0); + } else { + if ((txData = malloc(args.size)) == NULL) { + printf("Failed to allocate txData!\n"); + return 0; } } prbValid = false; - count = 0; + count = 0; gettimeofday(&startTime, NULL); - do { - // Setup fds for select call + do { + // Setup file descriptor set for select call FD_ZERO(&fds); - FD_SET(s,&fds); - - // Setup select timeout for 1 second - timeout.tv_sec=2; - timeout.tv_usec=0; - - // Wait for Socket data ready - ret = select(s+1,NULL,&fds,NULL,&timeout); - if ( ret <= 0 ) { - printf("Write timeout\n"); - } - else { - - if ( args.idxEn ) { + FD_SET(s, &fds); + + // Setup select timeout for 2 seconds + timeout.tv_sec = 2; + timeout.tv_usec = 0; + + // Wait for socket or file descriptor to be ready for writing + ret = select(s + 1, NULL, &fds, NULL, &timeout); + if (ret <= 0) { + printf("Write timeout or error\n"); + } else { + // If using DMA, get next buffer index + if (args.idxEn) { dmaIndex = dmaGetIndex(s); - if ( dmaIndex < 0 ) continue; + if (dmaIndex < 0) continue; txData = dmaBuffers[dmaIndex]; } - // Gen data - if ( args.prbsDis == 0 && ! prbValid ) { - prbs.genData(txData,args.size); + // Generate and write data if PRBS is enabled and data is not yet valid + if (args.prbsDis == 0 && !prbValid) { + prbs.genData(txData, args.size); prbValid = true; } - // DMA Write - if ( args.idxEn ) ret = dmaWriteIndex(s,dmaIndex,args.size,0,args.dest); - else ret = dmaWrite(s,txData,args.size,0,args.dest); + // Perform the write operation, using DMA if enabled + if (args.idxEn) { + ret = dmaWriteIndex(s, dmaIndex, args.size, 0, args.dest); + } else { + ret = dmaWrite(s, txData, args.size, 0, args.dest); + } - if ( ret > 0 ) { + // On successful write, reset prbValid flag and increment count + if (ret > 0) { prbValid = false; count++; - //printf("Write ret=%i, Dest=%i, count=%i\n",ret,args.dest,count); } } - } while ( count < args.count ); + } while (count < args.count); + gettimeofday(&endTime, NULL); - if ( args.idxEn ) dmaUnMapDma(s,dmaBuffers); + // Clean up allocated resources + if (args.idxEn) dmaUnMapDma(s, dmaBuffers); else free(txData); - timersub(&endTime, &startTime, &diffTime); - + // Calculate and print write operation statistics + timersub(&endTime, &startTime, &diffTime); float duration = (float)diffTime.tv_sec + (float)diffTime.tv_usec / 1000000.0; float rate = count / duration; float period = 1.0 / rate; - printf("Write %i events in %f seconds, rate = %f, period = %f\n",args.count, duration, rate, period); + printf("Write %i events in %f seconds, rate = %f Hz, period = %f seconds\n", count, duration, rate, period); close(s); - return(0); + return 0; } - diff --git a/data_dev/app/src/setDebug.cpp b/data_dev/app/src/setDebug.cpp index 09f477a..1ef4a54 100755 --- a/data_dev/app/src/setDebug.cpp +++ b/data_dev/app/src/setDebug.cpp @@ -1,17 +1,20 @@ /** - * ---------------------------------------------------------------------------- + *----------------------------------------------------------------------------- + * Company : SLAC National Accelerator Laboratory + *----------------------------------------------------------------------------- * Description: - * This program set the driver debug level. - * ---------------------------------------------------------------------------- - * This file is part of the aes_stream_drivers package. It is subject to - * the license terms in the LICENSE.txt file found in the top-level directory - * of this distribution and at: - * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. + * This program sets the driver debug level. + *----------------------------------------------------------------------------- + * This file is part of the aes_stream_drivers package. It is subject to the + * license terms in the LICENSE.txt file found in the top-level directory of + * this distribution and at: + * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. * No part of the aes_stream_drivers package, including this file, may be * copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE.txt file. - * ---------------------------------------------------------------------------- + *----------------------------------------------------------------------------- **/ + #include #include #include @@ -22,62 +25,83 @@ #include #include #include + using namespace std; -const char * argp_program_version = "setDebug 1.0"; -const char * argp_program_bug_address = "rherbst@slac.stanford.edu"; +// Version and contact information +const char *argp_program_version = "setDebug 1.0"; +const char *argp_program_bug_address = "rherbst@slac.stanford.edu"; +// Struct to hold command-line arguments struct PrgArgs { - const char * path; - uint32_t level; + const char *path; + uint32_t level; }; +// Default command-line arguments static struct PrgArgs DefArgs = { "/dev/datadev_0", 0x00 }; -static char args_doc[] = "debugLevel"; -static char doc[] = "\n Debug level is either 0 or 1."; +// Documentation for arguments +static char args_doc[] = "debugLevel"; +static char doc[] = "\n Debug level is either 0 or 1."; +// Option descriptions static struct argp_option options[] = { - { "path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device to use. Default=/dev/datadev_0.",0}, - {0} + { "path", 'p', "PATH", OPTION_ARG_OPTIONAL, "Path of datadev device to use. Default=/dev/datadev_0.", 0 }, + { 0 } }; -error_t parseArgs ( int key, char *arg, struct argp_state *state ) { +// Parse a single option +error_t parseArgs(int key, char *arg, struct argp_state *state) { struct PrgArgs *args = (struct PrgArgs *)state->input; - switch(key) { - case 'p': args->path = arg; break; + switch (key) { + case 'p': + args->path = arg; + break; case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: args->level = strtol(arg,NULL,10); break; - default: argp_usage(state); break; - } - break; + if (state->arg_num == 0) { + args->level = strtol(arg, NULL, 10); + } else { + argp_usage(state); // Too many arguments + } + break; case ARGP_KEY_END: - if ( state->arg_num < 1) argp_usage(state); - break; - default: return ARGP_ERR_UNKNOWN; break; + if (state->arg_num < 1) { + argp_usage(state); // Not enough arguments + } + break; + default: + return ARGP_ERR_UNKNOWN; } - return(0); + return 0; } -static struct argp argp = {options,parseArgs,args_doc,doc}; +// argp parser +static struct argp argp = { options, parseArgs, args_doc, doc }; -int main (int argc, char **argv) { +int main(int argc, char **argv) { int s; + // Initialize argument structure with default values struct PrgArgs args; + memcpy(&args, &DefArgs, sizeof(struct PrgArgs)); - memcpy(&args,&DefArgs,sizeof(struct PrgArgs)); - argp_parse(&argp,argc,argv,0,0,&args); + // Parse command-line arguments + argp_parse(&argp, argc, argv, 0, 0, &args); - if ( (s = open(args.path, O_RDWR)) <= 0 ) { - printf("Error opening %s\n",args.path); - return(1); + // Open device + if ((s = open(args.path, O_RDWR)) <= 0) { + printf("Error opening %s\n", args.path); + return 1; } - printf("Setting debug level to %i\n",args.level); - dmaSetDebug(s,args.level); + // Set debug level + printf("Setting debug level to %i\n", args.level); + dmaSetDebug(s, args.level); + + // Close device close(s); -} + return 0; +} diff --git a/data_dev/app/src/test.cpp b/data_dev/app/src/test.cpp index eac71b7..7e5d108 100644 --- a/data_dev/app/src/test.cpp +++ b/data_dev/app/src/test.cpp @@ -1,7 +1,10 @@ /** + *----------------------------------------------------------------------------- + * Company : SLAC National Accelerator Laboratory *----------------------------------------------------------------------------- * Description: - * This program will open up a AXIS DMA port and attempt to write data. + * Opens an AXIS DMA port and attempts to write data using the DMA interface. + * Demonstrates the setup, execution, and timing of DMA write operations. * ---------------------------------------------------------------------------- * This file is part of the aes_stream_drivers package. It is subject to * the license terms in the LICENSE.txt file found in the top-level directory @@ -23,65 +26,67 @@ #include #include #include -using namespace std; - -int main (int argc, char **argv) { - int32_t s; - int32_t ret; - uint32_t count; - void * txData=NULL; - uint32_t dmaSize; - uint32_t dmaCount; - struct timeval startTime; - struct timeval endTime; - struct timeval diffTime; - - //dmaSize = 2097150; - //dmaSize = 655360*10; - //dmaSize = 655360*2; - //dmaSize = 655360; - //dmaSize = 655360/2; - //dmaSize = 655360/10; - dmaSize = 100; - dmaCount = 10000; +using namespace std; - if ( (s = open("/dev/datadev_0", O_RDWR)) <= 0 ) { - printf("Error opening\n"); - return(1); +int main(int argc, char **argv) { + int32_t s; + int32_t ret; + uint32_t count; + void *txData = NULL; + uint32_t dmaSize; + uint32_t dmaCount; + struct timeval startTime, endTime, diffTime; + + // Set DMA transaction size and count + dmaSize = 100; // Size of each DMA transaction in bytes + dmaCount = 10000; // Total number of DMA transactions to perform + + // Attempt to open the DMA device + if ((s = open("/dev/datadev_0", O_RDWR)) <= 0) { + printf("Error opening DMA device\n"); + return 1; } - if ((txData = malloc(dmaSize)) == NULL ) { - printf("Failed to allocate rxData!\n"); - return(0); + // Allocate memory for DMA data + if ((txData = malloc(dmaSize)) == NULL) { + printf("Failed to allocate memory for DMA data\n"); + return 0; } + count = 0; + // Get start time gettimeofday(&startTime, NULL); - do { - while ( (ret = dmaWrite(s,txData,dmaSize,0,0) ) == 0 ); + // Perform DMA write operations + do { + while ((ret = dmaWrite(s, txData, dmaSize, 0, 0)) == 0); - if ( ret < 0 ) { - printf("Got write error\n"); - return(0); + if (ret < 0) { + printf("DMA write error occurred\n"); + return 0; } + } while (++count < dmaCount); - } while ( ++count < dmaCount ); + // Get end time gettimeofday(&endTime, NULL); + // Free allocated memory free(txData); + // Calculate operation duration and performance metrics timersub(&endTime, &startTime, &diffTime); - - float duration = (float)diffTime.tv_sec + (float)diffTime.tv_usec / 1000000.0; + float duration = diffTime.tv_sec + diffTime.tv_usec / 1000000.0; float rate = count / duration; float period = 1.0 / rate; - float bw = ((float)dmaCount * (float)dmaSize) / duration; + float bw = (dmaCount * dmaSize) / duration; - printf("Wrote %i events of size %i in %f seconds, rate = %f hz, period = %f s, bw = %e B/s\n",dmaCount, dmaSize, duration, rate, period, bw); + // Display performance metrics + printf("Wrote %u events of size %u in %.3f seconds, rate = %.3f Hz, period = %.3f s, bandwidth = %.3e B/s\n", + dmaCount, dmaSize, duration, rate, period, bw); + // Close the DMA device close(s); - return(0); + return 0; } -