pcilib.h 18 KB

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