pcilib.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #ifndef _PCITOOL_PCILIB_H
  2. #define _PCITOOL_PCILIB_H
  3. #include <sys/time.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdarg.h>
  7. typedef struct pcilib_s pcilib_t;
  8. typedef struct pcilib_event_context_s pcilib_context_t;
  9. typedef uint32_t pcilib_version_t;
  10. typedef uint8_t pcilib_bar_t; /**< Type holding the PCI Bar number */
  11. typedef uint16_t pcilib_register_t; /**< Type holding the register position within the field listing registers in the model */
  12. typedef uint16_t pcilib_view_t; /**< Type holding the register view position within view listing in the model */
  13. typedef uint16_t pcilib_unit_t; /**< Type holding the value unit position within unit listing in the model */
  14. typedef uint32_t pcilib_register_addr_t; /**< Type holding the register address within address-space of BARs */
  15. typedef uint8_t pcilib_register_size_t; /**< Type holding the size in bits of the register */
  16. typedef uint32_t pcilib_register_value_t; /**< Type holding the register value */
  17. typedef uint8_t pcilib_dma_engine_addr_t;
  18. typedef uint8_t pcilib_dma_engine_t;
  19. typedef uint64_t pcilib_event_id_t;
  20. typedef uint32_t pcilib_event_t;
  21. typedef uint64_t pcilib_timeout_t; /**< In microseconds */
  22. typedef unsigned int pcilib_irq_hw_source_t;
  23. typedef uint32_t pcilib_irq_source_t;
  24. typedef struct _xmlNode pcilib_xml_node_t;
  25. typedef enum {
  26. PCILIB_LOG_DEBUG = 0, /**< Debug messages will be always printed as they should be filtered based on setting of corresponding environmental variable */
  27. PCILIB_LOG_INFO, /**< Informational message are suppresed by default */
  28. PCILIB_LOG_WARNING, /**< Warnings messages indicate that something unexpected happen, but application can continue */
  29. PCILIB_LOG_ERROR /**< The error which is impossible to handle on this level of library */
  30. } pcilib_log_priority_t;
  31. typedef enum {
  32. PCILIB_HOST_ENDIAN = 0,
  33. PCILIB_LITTLE_ENDIAN,
  34. PCILIB_BIG_ENDIAN
  35. } pcilib_endianess_t;
  36. typedef enum {
  37. PCILIB_TYPE_STRING = 0, /**< char* */
  38. PCILIB_TYPE_DOUBLE = 1, /**< double */
  39. PCILIB_TYPE_LONG = 2
  40. } pcilib_data_type_t;
  41. typedef enum {
  42. PCILIB_DMA_IRQ = 1,
  43. PCILIB_EVENT_IRQ = 2
  44. } pcilib_irq_type_t;
  45. typedef enum { /**< 0x8000 and up are reserved for driver-specific types */
  46. PCILIB_EVENT_DATA = 0, /**< default data format */
  47. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  48. } pcilib_event_data_type_t;
  49. typedef enum {
  50. PCILIB_DMA_TO_DEVICE = 1,
  51. PCILIB_DMA_FROM_DEVICE = 2,
  52. PCILIB_DMA_BIDIRECTIONAL = 3
  53. } pcilib_dma_direction_t;
  54. typedef enum {
  55. PCILIB_DMA_FLAGS_DEFAULT = 0,
  56. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  57. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  58. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  59. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  60. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16 /**< do not crash on errors, but return appropriate error codes */
  61. } pcilib_dma_flags_t;
  62. typedef enum {
  63. PCILIB_STREAMING_STOP = 0, /**< stop streaming */
  64. PCILIB_STREAMING_CONTINUE = 1, /**< wait the default DMA timeout for a new data */
  65. PCILIB_STREAMING_WAIT = 2, /**< wait the specified timeout for a new data */
  66. PCILIB_STREAMING_CHECK = 3, /**< do not wait for the data, bail out imideatly if no data ready */
  67. PCILIB_STREAMING_FAIL = 4, /**< fail if data is not available on timeout */
  68. PCILIB_STREAMING_REQ_FRAGMENT = 5, /**< only fragment of a packet is read, wait for next fragment and fail if no data during DMA timeout */
  69. PCILIB_STREAMING_REQ_PACKET = 6, /**< wait for next packet and fail if no data during the specified timeout */
  70. PCILIB_STREAMING_TIMEOUT_MASK = 3 /**< mask specifying all timeout modes */
  71. } pcilib_streaming_action_t;
  72. typedef enum {
  73. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  74. PCILIB_EVENT_FLAG_RAW_DATA_ONLY = 1, /**< Do not parse data, just read raw and pass it to rawdata callback. If passed to rawdata callback, idicates the data is not identified as event (most probably just padding) */
  75. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  76. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  77. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  78. } pcilib_event_flags_t;
  79. typedef enum {
  80. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  81. } pcilib_event_info_flags_t;
  82. typedef struct {
  83. pcilib_event_t type;
  84. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  85. uint64_t offset; /**< nanoseconds */
  86. struct timeval timestamp; /**< most accurate timestamp */
  87. pcilib_event_info_flags_t flags; /**< flags */
  88. } pcilib_event_info_t;
  89. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  90. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  91. #define PCILIB_BAR_NOBAR ((pcilib_bar_t)-2)
  92. #define PCILIB_BAR0 0
  93. #define PCILIB_BAR1 1
  94. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  95. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  96. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  97. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  98. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  99. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  100. #define PCILIB_EVENT0 1
  101. #define PCILIB_EVENT1 2
  102. #define PCILIB_EVENT2 4
  103. #define PCILIB_EVENT3 8
  104. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  105. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  106. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  107. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  108. #define PCILIB_TIMEOUT_IMMEDIATE 0
  109. #define PCILIB_IRQ_TYPE_ALL 0
  110. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  111. #define PCILIB_MODEL_DETECT NULL
  112. typedef void (*pcilib_logger_t)(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va);
  113. /**<
  114. * Callback function called when new data is read by DMA streaming function
  115. * @ctx - DMA Engine context
  116. * @flags - DMA Flags
  117. * @bufsize - size of data in bytes
  118. * @buf - data
  119. * @returns
  120. * <0 - error, stop streaming (the value is negative error code)
  121. * 0 - stop streaming (PCILIB_STREAMING_STOP)
  122. * 1 - wait DMA timeout and return gracefuly if no data (PCILIB_STREAMING_CONTINUE)
  123. * 2 - wait the specified timeout and return gracefuly if no data (PCILIB_STREAMING_WAIT)
  124. * 3 - check if more data is available without waiting, return gracefuly if not (PCILIB_STREAMING_CHECK)
  125. * 5 - wait DMA timeout and fail if no data (PCILIB_STREAMING_REQ_FRAGMENT)
  126. * 6 - wait the specified timeout and fail if no data (PCILIB_STREAMING_REQ_PACKET)
  127. */
  128. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  129. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user);
  130. typedef int (*pcilib_event_rawdata_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user);
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg);
  135. pcilib_t *pcilib_open(const char *device, const char *model);
  136. void pcilib_close(pcilib_t *ctx);
  137. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  138. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  139. // Interrupt API is preliminary and can be significantly changed in future
  140. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  141. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  142. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  143. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  144. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  145. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  146. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  147. char *pcilib_resolve_register_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr); // addr is offset if bar is specified
  148. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  149. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  150. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  151. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  152. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  153. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  154. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  155. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  156. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  157. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  158. int pcilib_stream_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, pcilib_dma_callback_t cb, void *cbattr);
  159. int pcilib_push_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *written_bytes);
  160. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *read_bytes);
  161. int pcilib_read_dma_custom(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *read_bytes);
  162. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *written_bytes);
  163. double pcilib_benchmark_dma(pcilib_t *ctx, pcilib_dma_engine_addr_t dma, uintptr_t addr, size_t size, size_t iterations, pcilib_dma_direction_t direction);
  164. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  165. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  166. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  167. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  168. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  169. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  170. int pcilib_read_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, pcilib_data_type_t value_type, size_t value_size, void *value);
  171. int pcilib_write_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, pcilib_data_type_t value_type, size_t value_size, void *value);
  172. int pcilib_reset(pcilib_t *ctx);
  173. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  174. /*
  175. * The recording of new events will be stopped after reaching max_events records
  176. * or when the specified amount of time is elapsed. However, the @pcilib_stop
  177. * function should be called still.
  178. * NOTE: This options may not be respected if the PCILIB_EVENT_FLAG_RAW_DATA_ONLY
  179. * is specified.
  180. */
  181. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  182. /*
  183. * Request auto-triggering while grabbing
  184. */
  185. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  186. /*
  187. * Request streaming the rawdata from the event engine. It is fastest way to acuqire data.
  188. * No memory copies will be performed and DMA buffers will be directly passed to the user
  189. * callback. However, to prevent data loss, no processing should be done on the data. The
  190. * user callback is only expected to copy data into the appropriate place and return control
  191. * to the event engine.
  192. * The performance can be boosted further by disabling any data processing within the event
  193. * engine. Just pass PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to the @pcilib_start function.
  194. */
  195. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  196. /*
  197. * Configures maximal number of preprocessing threads. Actual amount of threads
  198. * may be bigger. For instance, additionaly a real-time reader thread will be
  199. * executed for most of hardware
  200. */
  201. int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads);
  202. pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx);
  203. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags);
  204. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  205. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  206. int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info);
  207. int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *retsize);
  208. int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize);
  209. void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size_or_err);
  210. void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size_or_err);
  211. /*
  212. * This function is provided to find potentially corrupted data. If the data is overwritten by
  213. * the time return_data is called it will return error.
  214. */
  215. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  216. /*
  217. * @param data - will be allocated and shuld be freed if NULL, otherwise used and size should contain correct size.
  218. * In case of failure the content of data is undefined.
  219. * @param timeout - will be autotriggered if NULL
  220. */
  221. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout);
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* _PCITOOL_PCILIB_H */