private.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _IPECAMERA_PRIVATE_H
  2. #define _IPECAMERA_PRIVATE_H
  3. #include <pthread.h>
  4. #include <pcilib/model.h>
  5. #include <pcilib/debug.h>
  6. #include "ipecamera.h"
  7. #define IPECAMERA_DEBUG
  8. #ifdef IPECAMERA_DEBUG
  9. //# define IPECAMERA_DEBUG_RAW_FRAMES
  10. # define IPECAMERA_DEBUG_BROKEN_FRAMES
  11. # define IPECAMERA_DEBUG_RAW_PACKETS
  12. #endif /* IPECAMERA_DEBUG */
  13. //#define IPECAMERA_BUG_MISSING_PAYLOAD //**< CMOSIS fails to provide a first payload for each frame, therefore the frame is 32 bit shorter */
  14. #define IPECAMERA_BUG_MULTIFRAME_PACKETS //**< This is by design, start of packet comes directly after the end of last one in streaming mode */
  15. //#define IPECAMERA_BUG_INCOMPLETE_PACKETS //**< Support incomplete packets, i.e. check for frame magic even if full frame size is not reached yet (slow) */
  16. //#define IPECAMERA_ANNOUNCE_READY //**< Announce new event only after the reconstruction is done */
  17. //#define IPECAMERA_CLEAN_ON_START //**< Read all the data from DMA before starting of recording */
  18. #define IPECAMERA_ADJUST_BUFFER_SIZE //**< Adjust default buffer size based on the hardware capabilities */
  19. #define IPECAMERA_DEFAULT_BUFFER_SIZE 64 //**< should be power of 2 */
  20. #define IPECAMERA_RESERVE_BUFFERS 2 //**< Return Frame is Lost error, if requested frame will be overwritten after specified number of frames
  21. #define IPECAMERA_DMA_TIMEOUT 50000 //**< Default DMA timeout */
  22. #define IPECAMERA_TRIGGER_TIMEOUT 200000 //**< In trigger call allow specified timeout for camera to get out of busy state. Set 0 to fail immideatly */
  23. #define IPECAMERA_CMOSIS_RESET_DELAY 250000 //**< Michele thinks 250 should be enough, but reset failing in this case */
  24. #define IPECAMERA_CMOSIS_REGISTER_DELAY 250000 //**< Michele thinks 250 should be enough, but reset failing in this case */
  25. #define IPECAMERA_SPI_REGISTER_DELAY 10000 //**< Delay between consequitive access to the registers */
  26. #define IPECAMERA_NEXT_FRAME_DELAY 1000 //**< Michele requires 30000 to sync between End Of Readout and next Frame Req */
  27. #define IPECAMERA_TRIGGER_DELAY 0 //**< Defines how long the trigger bits should be set */
  28. #define IPECAMERA_READ_STATUS_DELAY 1000 //**< According to Uros, 1ms delay needed before consequitive reads from status registers */
  29. #define IPECAMERA_NOFRAME_SLEEP 100 //**< Sleep while polling for a new frame in reader */
  30. #define IPECAMERA_NOFRAME_PREPROC_SLEEP 100 //**< Sleep while polling for a new frame in pre-processor */
  31. //#define IPECAMERA_MAX_LINES 1088
  32. #define IPECAMERA_MAX_LINES 2048
  33. #define IPECAMERA_EXPECTED_STATUS_4 0x08409FFFF
  34. #define IPECAMERA_EXPECTED_STATUS 0x08449FFFF
  35. #define IPECAMERA_END_OF_SEQUENCE 0x1F001001
  36. #define IPECAMERA_MAX_CHANNELS 16
  37. #define IPECAMERA_PIXELS_PER_CHANNEL 128
  38. #define IPECAMERA_WIDTH (IPECAMERA_MAX_CHANNELS * IPECAMERA_PIXELS_PER_CHANNEL)
  39. #define IPECAMERA_FRAME_REQUEST 0x80000209 // 0x1E9
  40. #define IPECAMERA_IDLE 0x80000201 // 0x1E1
  41. #define IPECAMERA_START_INTERNAL_STIMULI 0x1F1
  42. #define IPECAMERA_MODE_16_CHAN_IO 0
  43. #define IPECAMERA_MODE_4_CHAN_IO 2
  44. #define IPECAMERA_MODE_12_BIT_ADC 2
  45. #define IPECAMERA_MODE_11_BIT_ADC 1
  46. #define IPECAMERA_MODE_10_BIT_ADC 0
  47. #ifdef IPECAMERA_DEBUG_RAW_FRAMES
  48. # define IPECAMERA_DEBUG_RAW_FRAMES_MESSAGE(function, ...) pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__)
  49. # define IPECAMERA_DEBUG_RAW_FRAMES_BUFFER(function, ...) pcilib_debug_data_buffer (#function, __VA_ARGS__)
  50. #else /* IPECAMERA_DEBUG_RAW_FRAMES */
  51. # define IPECAMERA_DEBUG_RAW_FRAMES_MESSAGE(function, ...)
  52. # define IPECAMERA_DEBUG_RAW_FRAMES_BUFFER(function, ...)
  53. #endif /* IPECAMERA_DEBUG_RAW_FRAMES */
  54. #ifdef IPECAMERA_DEBUG_BROKEN_FRAMES
  55. # define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...) pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__)
  56. # define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...) pcilib_debug_data_buffer (#function, __VA_ARGS__)
  57. #else /* IPECAMERA_DEBUG_BROKEN_FRAMES */
  58. # define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...)
  59. # define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...)
  60. #endif /* IPECAMERA_DEBUG_BROKEN_FRAMES */
  61. #ifdef IPECAMERA_IPECAMERA_DEBUG_RAW_PACKETS
  62. # define IPECAMERA_DEBUG_RAW_PACKETS_MESSAGE(function, ...) pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__)
  63. # define IPECAMERA_DEBUG_RAW_PACKETS_BUFFER(function, ...) pcilib_debug_data_buffer (#function, __VA_ARGS__)
  64. #else /* IPECAMERA_IPECAMERA_DEBUG_RAW_PACKETS */
  65. # define IPECAMERA_DEBUG_RAW_PACKETS_MESSAGE(function, ...)
  66. # define IPECAMERA_DEBUG_RAW_PACKETS_BUFFER(function, ...)
  67. #endif /* IPECAMERA_IPECAMERA_DEBUG_RAW_PACKETS */
  68. #define ipecamera_debug(function, ...) \
  69. IPECAMERA_DEBUG_##function##_MESSAGE(IPECAMERA_DEBUG_##function, __VA_ARGS__)
  70. #define ipecamera_debug_buffer(function, ...) \
  71. IPECAMERA_DEBUG_##function##_BUFFER(IPECAMERA_DEBUG_##function, __VA_ARGS__)
  72. typedef uint32_t ipecamera_payload_t;
  73. typedef struct {
  74. pcilib_event_id_t evid;
  75. struct timeval timestamp;
  76. } ipecamera_autostop_t;
  77. typedef struct {
  78. size_t i;
  79. pthread_t thread;
  80. ipecamera_t *ipecamera;
  81. int started; /**< flag indicating that join & cleanup is required */
  82. } ipecamera_preprocessor_t;
  83. typedef struct {
  84. ipecamera_event_info_t event; /**< this structure is overwritten by the reader thread, we need a copy */
  85. pthread_rwlock_t mutex; /**< this mutex protects reconstructed buffers only, the raw data, event_info, etc. will be overwritten by reader thread anyway */
  86. } ipecamera_frame_t;
  87. struct ipecamera_s {
  88. pcilib_context_t event;
  89. UfoDecoder *ipedec;
  90. char *data;
  91. ipecamera_pixel_t *image;
  92. size_t size;
  93. pcilib_event_callback_t cb;
  94. void *cb_user;
  95. volatile pcilib_event_id_t event_id;
  96. volatile pcilib_event_id_t preproc_id;
  97. pcilib_event_id_t reported_id;
  98. pcilib_dma_engine_t rdma, wdma;
  99. pcilib_register_t control_reg, status_reg;
  100. pcilib_register_t status2_reg, status3_reg;
  101. pcilib_register_t n_lines_reg;
  102. uint16_t line_reg;
  103. pcilib_register_t exposure_reg;
  104. pcilib_register_t flip_reg;
  105. pcilib_register_t firmware_version_reg;
  106. pcilib_register_t adc_resolution_reg;
  107. pcilib_register_t output_mode_reg;
  108. pcilib_register_t max_frames_reg;
  109. pcilib_register_t num_frames_reg;
  110. int started; /**< Camera is in grabbing mode (start function is called) */
  111. int streaming; /**< Camera is in streaming mode (we are within stream call) */
  112. int parse_data; /**< Indicates if some processing of the data is required, otherwise only rawdata_callback will be called */
  113. volatile int run_reader; /**< Instructs the reader thread to stop processing */
  114. volatile int run_streamer; /**< Indicates request to stop streaming events and can be set by reader_thread upon exit or by user request */
  115. volatile int run_preprocessors; /**< Instructs preprocessors to exit */
  116. ipecamera_autostop_t autostop;
  117. struct timeval autostop_time;
  118. struct timeval next_trigger; /**< The minimal delay between trigger signals is mandatory, this indicates time when next trigger is possible */
  119. size_t buffer_size; /**< How many images to store */
  120. size_t buffer_pos; /**< Current image offset in the buffer, due to synchronization reasons should not be used outside of reader_thread */
  121. size_t cur_size; /**< Already written part of data in bytes */
  122. size_t raw_size; /**< Expected maximum size of raw data in bytes */
  123. size_t padded_size; /**< Expected maximum size of buffer for raw data, including additional padding */
  124. size_t roi_raw_size; /**< Expected size (for currently configured ROI) of raw data in bytes */
  125. size_t roi_padded_size; /**< Expected size (for currently configured ROI) of buffer for raw data, including additional padding */
  126. size_t image_size; /**< Size of a single image in bytes */
  127. size_t max_frames; /**< Maximal number of frames what may be buffered in camera DDR memory */
  128. int firmware; /**< Firmware version */
  129. int cmosis_outputs; /**< Number of active cmosis outputs: 4 or 16 */
  130. int width, height;
  131. // void *raw_buffer;
  132. void *buffer;
  133. ipecamera_change_mask_t *cmask;
  134. ipecamera_frame_t *frame;
  135. ipecamera_image_dimensions_t dim;
  136. pthread_t rthread;
  137. size_t n_preproc;
  138. ipecamera_preprocessor_t *preproc;
  139. pthread_mutex_t preproc_mutex;
  140. int preproc_mutex_destroy;
  141. int frame_mutex_destroy;
  142. };
  143. #endif /* _IPECAMERA_PRIVATE_H */