pcilib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_INVALID = 0, /**< uninitialized */
  38. PCILIB_TYPE_DEFAULT = 0, /**< default type */
  39. PCILIB_TYPE_STRING = 1, /**< char* */
  40. PCILIB_TYPE_DOUBLE = 2, /**< double */
  41. PCILIB_TYPE_LONG = 3
  42. } pcilib_value_type_t;
  43. typedef enum {
  44. PCILIB_DMA_IRQ = 1,
  45. PCILIB_EVENT_IRQ = 2
  46. } pcilib_irq_type_t;
  47. typedef enum { /**< 0x8000 and up are reserved for driver-specific types */
  48. PCILIB_EVENT_DATA = 0, /**< default data format */
  49. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  50. } pcilib_event_data_type_t;
  51. typedef enum {
  52. PCILIB_DMA_TO_DEVICE = 1,
  53. PCILIB_DMA_FROM_DEVICE = 2,
  54. PCILIB_DMA_BIDIRECTIONAL = 3
  55. } pcilib_dma_direction_t;
  56. typedef enum {
  57. PCILIB_DMA_FLAGS_DEFAULT = 0,
  58. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  59. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  60. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  61. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  62. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16 /**< do not crash on errors, but return appropriate error codes */
  63. } pcilib_dma_flags_t;
  64. typedef enum {
  65. PCILIB_STREAMING_STOP = 0, /**< stop streaming */
  66. PCILIB_STREAMING_CONTINUE = 1, /**< wait the default DMA timeout for a new data */
  67. PCILIB_STREAMING_WAIT = 2, /**< wait the specified timeout for a new data */
  68. PCILIB_STREAMING_CHECK = 3, /**< do not wait for the data, bail out imideatly if no data ready */
  69. PCILIB_STREAMING_FAIL = 4, /**< fail if data is not available on timeout */
  70. PCILIB_STREAMING_REQ_FRAGMENT = 5, /**< only fragment of a packet is read, wait for next fragment and fail if no data during DMA timeout */
  71. PCILIB_STREAMING_REQ_PACKET = 6, /**< wait for next packet and fail if no data during the specified timeout */
  72. PCILIB_STREAMING_TIMEOUT_MASK = 3 /**< mask specifying all timeout modes */
  73. } pcilib_streaming_action_t;
  74. typedef enum {
  75. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  76. 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) */
  77. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  78. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  79. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  80. } pcilib_event_flags_t;
  81. typedef enum {
  82. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  83. } pcilib_event_info_flags_t;
  84. typedef struct {
  85. pcilib_event_t type;
  86. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  87. uint64_t offset; /**< nanoseconds */
  88. struct timeval timestamp; /**< most accurate timestamp */
  89. pcilib_event_info_flags_t flags; /**< flags */
  90. } pcilib_event_info_t;
  91. typedef struct {
  92. pcilib_value_type_t type;
  93. const char *unit;
  94. const char *format;
  95. union {
  96. long ival;
  97. double fval;
  98. char *sval;
  99. };
  100. // This is a private part
  101. size_t size;
  102. void *data;
  103. char str[16];
  104. } pcilib_value_t;
  105. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  106. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  107. #define PCILIB_BAR_NOBAR ((pcilib_bar_t)-2)
  108. #define PCILIB_BAR0 0
  109. #define PCILIB_BAR1 1
  110. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  111. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  112. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  113. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  114. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  115. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  116. #define PCILIB_EVENT0 1
  117. #define PCILIB_EVENT1 2
  118. #define PCILIB_EVENT2 4
  119. #define PCILIB_EVENT3 8
  120. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  121. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  122. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  123. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  124. #define PCILIB_TIMEOUT_IMMEDIATE 0
  125. #define PCILIB_IRQ_TYPE_ALL 0
  126. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  127. #define PCILIB_MODEL_DETECT NULL
  128. typedef void (*pcilib_logger_t)(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va);
  129. /**<
  130. * Callback function called when new data is read by DMA streaming function
  131. * @ctx - DMA Engine context
  132. * @flags - DMA Flags
  133. * @bufsize - size of data in bytes
  134. * @buf - data
  135. * @returns
  136. * <0 - error, stop streaming (the value is negative error code)
  137. * 0 - stop streaming (PCILIB_STREAMING_STOP)
  138. * 1 - wait DMA timeout and return gracefuly if no data (PCILIB_STREAMING_CONTINUE)
  139. * 2 - wait the specified timeout and return gracefuly if no data (PCILIB_STREAMING_WAIT)
  140. * 3 - check if more data is available without waiting, return gracefuly if not (PCILIB_STREAMING_CHECK)
  141. * 5 - wait DMA timeout and fail if no data (PCILIB_STREAMING_REQ_FRAGMENT)
  142. * 6 - wait the specified timeout and fail if no data (PCILIB_STREAMING_REQ_PACKET)
  143. */
  144. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  145. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user);
  146. 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);
  147. #ifdef __cplusplus
  148. extern "C" {
  149. #endif
  150. int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg);
  151. pcilib_t *pcilib_open(const char *device, const char *model);
  152. void pcilib_close(pcilib_t *ctx);
  153. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  154. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  155. // Interrupt API is preliminary and can be significantly changed in future
  156. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  157. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  158. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  159. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  160. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  161. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  162. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  163. char *pcilib_resolve_register_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr); // addr is offset if bar is specified
  164. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  165. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  166. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  167. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  168. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  169. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  170. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  171. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  172. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  173. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  174. 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);
  175. 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);
  176. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *read_bytes);
  177. 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);
  178. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *written_bytes);
  179. 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);
  180. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  181. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  182. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  183. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  184. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  185. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  186. void pcilib_clean_value(pcilib_t *ctx, pcilib_value_t *val);
  187. int pcilib_copy_value(pcilib_t *ctx, pcilib_value_t *dst, const pcilib_value_t *src);
  188. int pcilib_set_value_from_float(pcilib_t *ctx, pcilib_value_t *val, double fval);
  189. int pcilib_set_value_from_int(pcilib_t *ctx, pcilib_value_t *val, long ival);
  190. int pcilib_convert_value_unit(pcilib_t *ctx, pcilib_value_t *val, const char *unit_name);
  191. int pcilib_convert_value_type(pcilib_t *ctx, pcilib_value_t *val, pcilib_value_type_t type);
  192. int pcilib_read_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, pcilib_value_t *value);
  193. int pcilib_write_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, const pcilib_value_t *value);
  194. int pcilib_reset(pcilib_t *ctx);
  195. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  196. /*
  197. * The recording of new events will be stopped after reaching max_events records
  198. * or when the specified amount of time is elapsed. However, the @pcilib_stop
  199. * function should be called still.
  200. * NOTE: This options may not be respected if the PCILIB_EVENT_FLAG_RAW_DATA_ONLY
  201. * is specified.
  202. */
  203. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  204. /*
  205. * Request auto-triggering while grabbing
  206. */
  207. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  208. /*
  209. * Request streaming the rawdata from the event engine. It is fastest way to acuqire data.
  210. * No memory copies will be performed and DMA buffers will be directly passed to the user
  211. * callback. However, to prevent data loss, no processing should be done on the data. The
  212. * user callback is only expected to copy data into the appropriate place and return control
  213. * to the event engine.
  214. * The performance can be boosted further by disabling any data processing within the event
  215. * engine. Just pass PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to the @pcilib_start function.
  216. */
  217. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  218. /*
  219. * Configures maximal number of preprocessing threads. Actual amount of threads
  220. * may be bigger. For instance, additionaly a real-time reader thread will be
  221. * executed for most of hardware
  222. */
  223. int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads);
  224. pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx);
  225. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags);
  226. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  227. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  228. 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);
  229. 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);
  230. 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);
  231. 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);
  232. 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);
  233. /*
  234. * This function is provided to find potentially corrupted data. If the data is overwritten by
  235. * the time return_data is called it will return error.
  236. */
  237. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  238. /*
  239. * @param data - will be allocated and shuld be freed if NULL, otherwise used and size should contain correct size.
  240. * In case of failure the content of data is undefined.
  241. * @param timeout - will be autotriggered if NULL
  242. */
  243. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout);
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* _PCITOOL_PCILIB_H */