diff options
author | Suren A. Chilingaryan <csa@tir.suren.me> | 2024-04-04 01:08:26 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@tir.suren.me> | 2024-04-04 01:08:26 +0200 |
commit | be24c4d8751eaed09927596e9f0f218b03e2f1da (patch) | |
tree | 84ebcda1a4ae62f45a7e37c99d00fe6c25e33df8 /apps/stream-events.c | |
parent | d852b637800b0f91b9c921cda17311bc203939e5 (diff) | |
download | pcidev-be24c4d8751eaed09927596e9f0f218b03e2f1da.tar.gz pcidev-be24c4d8751eaed09927596e9f0f218b03e2f1da.tar.bz2 pcidev-be24c4d8751eaed09927596e9f0f218b03e2f1da.tar.xz pcidev-be24c4d8751eaed09927596e9f0f218b03e2f1da.zip |
Add sample apps to use event and dma engines
Diffstat (limited to 'apps/stream-events.c')
-rw-r--r-- | apps/stream-events.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/apps/stream-events.c b/apps/stream-events.c new file mode 100644 index 0000000..81d811f --- /dev/null +++ b/apps/stream-events.c @@ -0,0 +1,88 @@ +#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 evcb(pcilib_event_id_t evid, const pcilib_event_info_t *info, void *user) { + int err; + pcilib_t *pcilib = (pcilib_t*)user; + + void *data; + size_t datasize; + + static size_t packets = 0; + static size_t size = 0; + static struct timeval last = {0}; + struct timeval cur; + size_t us = 0; + + data = pcilib_get_data(pcilib, evid, PCILIB_EVENT_DATA, &datasize); + if (!data) pcilib_error("Error getting event data"); + + gettimeofday(&cur,NULL); + if (last.tv_sec > 0) + us = (cur.tv_sec - last.tv_sec)*1000000 + (cur.tv_usec - last.tv_usec); + + packets++; + size += datasize; + + if ((!us)||(StopFlag)||(us > 1000000)) { + printf("Received %zu packets with total size of %zu MB (last size: %zu)\n", packets, size/1024/1024, datasize); + memcpy(&last, &cur, sizeof(struct timeval)); + packets = 0; + size = 0; + } + + err = pcilib_return_data(pcilib, evid, PCILIB_EVENT_DATA, data); + if (err) pcilib_error("Error returning data, data is possibly corrupted"); + + return StopFlag?PCILIB_STREAMING_STOP:PCILIB_STREAMING_CONTINUE; +} + + +int main() { + int err; + + 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"); + +// pcilib_context_t *evdev = (pcilib_context_t*)pcilib_get_event_engine(pcilib); +// if (!evdev) pcilib_error("Failed to get event engine"); + + err = pcilib_start(pcilib, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT); + if (err) pcilib_error("Error (%i) starting event engine", err); + +// err = pcilib_trigger(pcilib, PCILIB_EVENT0, 0, NULL); +// if (err) pcilib_error("Error (%i) triggering event", err); + + err = pcilib_stream(pcilib, evcb, pcilib); + if (err) pcilib_error("Error (%i) streaming events", err); + + pcilib_stop(pcilib, PCILIB_EVENT_FLAGS_DEFAULT); +} |