diff options
Diffstat (limited to 'apps/stream-dma.c')
-rw-r--r-- | apps/stream-dma.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/apps/stream-dma.c b/apps/stream-dma.c new file mode 100644 index 0000000..3f1bcba --- /dev/null +++ b/apps/stream-dma.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <signal.h> + +#include <pcilib.h> +#include <pcilib/error.h> + +void log_error(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, va_list ap) { + vprintf(format, ap); + printf("\n"); + + if (prio == PCILIB_LOG_ERROR) { + printf("Exiting at [%s:%u]\n\n", file, line); + exit(-1); + } +} + + +static int StopFlag = 0; + +static void signal_exit_handler(int signo) { + if (++StopFlag > 2) + exit(-1); +} + +int datacb(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf) { + static size_t packets = 0; + static size_t size = 0; + static struct timeval last = {0}; + struct timeval cur; + size_t us = 0; + + gettimeofday(&cur,NULL); + if (last.tv_sec > 0) + us = (cur.tv_sec - last.tv_sec)*1000000 + (cur.tv_usec - last.tv_usec); + + packets++; + size += bufsize; + + if ((!us)||(StopFlag)||(us > 1000000)) { + printf("Received %zu buffers with total size of %zu GB (last ptr: %p, size: %zu)\n", packets, size/1024/1024/1024, buf, bufsize); + memcpy(&last, &cur, sizeof(struct timeval)); + packets = 0; + size = 0; + } + + return StopFlag?PCILIB_STREAMING_STOP:PCILIB_STREAMING_WAIT; +} + +int main() { + int err; + pcilib_dma_engine_t dmaid = 0; + + signal(SIGINT, signal_exit_handler); + + pcilib_set_logger(PCILIB_LOG_WARNING, &log_error, NULL); + + pcilib_t *pcilib = pcilib_open("/dev/fpga0", "pcidev"); + if (!pcilib) pcilib_error("Error opening device"); + + err = pcilib_start_dma (pcilib, dmaid, PCILIB_DMA_FLAGS_DEFAULT); + if (err) pcilib_error("Error %i starting DMA", err); + + err = pcilib_stream_dma (pcilib, dmaid, 0, 0, PCILIB_DMA_FLAGS_DEFAULT, PCILIB_TIMEOUT_INFINITE, datacb, NULL); + if (err) pcilib_error("Error %i streaming DMA", err); + + pcilib_stop_dma (pcilib, dmaid, PCILIB_DMA_FLAGS_DEFAULT); + + pcilib_stop(pcilib, PCILIB_EVENT_FLAGS_DEFAULT); +} |