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 | |
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
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | apps/CMakeLists.txt | 12 | ||||
-rw-r--r-- | apps/grab-frame.c | 57 | ||||
-rw-r--r-- | apps/ipecam.c (renamed from apps/grab.c) | 0 | ||||
-rw-r--r-- | apps/stream-dma.c | 71 | ||||
-rw-r--r-- | apps/stream-events.c | 88 |
6 files changed, 227 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ed3e40..b67a503 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ VERSION_TO_VARS(${PCIDEV_VERSION} PCIDEV_VERSION_MAJOR PCIDEV_VERSION_MINOR PCID include(GNUInstallDirs) add_subdirectory(xml) -#add_subdirectory(apps) +add_subdirectory(apps) include_directories( ${CMAKE_SOURCE_DIR} diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 761bb2b..de45e38 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -8,6 +8,14 @@ link_directories( ${PCILIB_LIBRARY_DIRS} ) +add_executable(stream-dma stream-dma.c) +target_link_libraries(stream-dma ${PCILIB_LIBRARIES}) -add_executable(grab grab.c) -target_link_libraries(grab ${PCILIB_LIBRARIES} ipecamera) +add_executable(stream-events stream-events.c) +target_link_libraries(stream-events ${PCILIB_LIBRARIES}) + +add_executable(grab-frame grab-frame.c) +target_link_libraries(grab-frame ${PCILIB_LIBRARIES}) + +#add_executable(ipecam ipecam.c) +#target_link_libraries(ipecam ${PCILIB_LIBRARIES} ipecamera) diff --git a/apps/grab-frame.c b/apps/grab-frame.c new file mode 100644 index 0000000..0cbdc00 --- /dev/null +++ b/apps/grab-frame.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdlib.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); + } +} + + +int main() { + int err; + pcilib_event_id_t evid; + pcilib_event_info_t info; + size_t size; + void *data; + FILE *f; + + 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_get_next_event(pcilib, 100000, &evid, sizeof(info), (pcilib_event_info_t*)&info); + if (err) pcilib_error("Error (%i) while waiting for event", err); + + data = pcilib_get_data(pcilib, evid, PCILIB_EVENT_DATA, &size); + if (!data) pcilib_error("Error getting event data"); + + printf("Writting %zu bytes to /dev/null\n", size); + f = fopen("/dev/null", "w"); + if (f) { + fwrite(data, 1, size, f); + fclose(f); + } + + err = pcilib_return_data(pcilib, evid, PCILIB_EVENT_DATA, data); + if (err) pcilib_error("Error returning data, data is possibly corrupted"); + + pcilib_stop(pcilib, PCILIB_EVENT_FLAGS_DEFAULT); +} diff --git a/apps/grab.c b/apps/ipecam.c index b891e2f..b891e2f 100644 --- a/apps/grab.c +++ b/apps/ipecam.c 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); +} 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); +} |