From be24c4d8751eaed09927596e9f0f218b03e2f1da Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Thu, 4 Apr 2024 01:08:26 +0200 Subject: Add sample apps to use event and dma engines --- CMakeLists.txt | 2 +- apps/CMakeLists.txt | 12 +++++-- apps/grab-frame.c | 57 ++++++++++++++++++++++++++++++++++ apps/grab.c | 63 ------------------------------------- apps/ipecam.c | 63 +++++++++++++++++++++++++++++++++++++ apps/stream-dma.c | 71 ++++++++++++++++++++++++++++++++++++++++++ apps/stream-events.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 290 insertions(+), 66 deletions(-) create mode 100644 apps/grab-frame.c delete mode 100644 apps/grab.c create mode 100644 apps/ipecam.c create mode 100644 apps/stream-dma.c create mode 100644 apps/stream-events.c 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 +#include + +#include +#include + +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/grab.c deleted file mode 100644 index b891e2f..0000000 --- a/apps/grab.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include - -#include -#include - -#include - -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; - ipecamera_event_info_t info; - ipecamera_t *ipecamera; - size_t size; - void *data; - FILE *f; - - pcilib_set_logger(PCILIB_LOG_WARNING, &log_error, NULL); - - pcilib_t *pcilib = pcilib_open("/dev/fpga0", "ipecamera"); - if (!pcilib) pcilib_error("Error opening device"); - - ipecamera = (ipecamera_t*)pcilib_get_event_engine(pcilib); - if (!ipecamera) pcilib_error("Failed to get ipecamera event engine"); - - err = ipecamera_set_buffer_size(ipecamera, 8); - if (err) pcilib_error("Error (%i) setting buffer size", err); - - 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/ipecam.c b/apps/ipecam.c new file mode 100644 index 0000000..b891e2f --- /dev/null +++ b/apps/ipecam.c @@ -0,0 +1,63 @@ +#include +#include + +#include +#include + +#include + +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; + ipecamera_event_info_t info; + ipecamera_t *ipecamera; + size_t size; + void *data; + FILE *f; + + pcilib_set_logger(PCILIB_LOG_WARNING, &log_error, NULL); + + pcilib_t *pcilib = pcilib_open("/dev/fpga0", "ipecamera"); + if (!pcilib) pcilib_error("Error opening device"); + + ipecamera = (ipecamera_t*)pcilib_get_event_engine(pcilib); + if (!ipecamera) pcilib_error("Failed to get ipecamera event engine"); + + err = ipecamera_set_buffer_size(ipecamera, 8); + if (err) pcilib_error("Error (%i) setting buffer size", err); + + 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/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 +#include +#include +#include + +#include +#include + +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 +#include +#include +#include + +#include +#include + +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); +} -- cgit v1.2.1