private.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <pcilib/locking.h>
  7. #include "base.h"
  8. #include "ipecamera.h"
  9. #include "env.h"
  10. #define IPECAMERA_DEBUG
  11. #ifdef IPECAMERA_DEBUG
  12. //# define IPECAMERA_DEBUG_RAW_FRAMES //**< Store all raw frames */
  13. # define IPECAMERA_DEBUG_BROKEN_FRAMES //**< Store broken frames in the specified directory */
  14. # define IPECAMERA_DEBUG_RAW_PACKETS //**< Store all raw packets read from DMA grouped in frames */
  15. # define IPECAMERA_DEBUG_HARDWARE //**< Produce various debugging information about ipecamera operation */
  16. # define IPECAMERA_DEBUG_FRAME_HEADERS //**< Print frame headers & footers */
  17. # define IPECAMERA_DEBUG_API //**< Debug IPECamera API calls */
  18. #endif /* IPECAMERA_DEBUG */
  19. #define IPECAMERA_BUG_MISSING_PAYLOAD //**< CMOSIS fails to provide a first payload for each frame, therefore the frame is 32 bit shorter */
  20. #define IPECAMERA_BUG_MULTIFRAME_PACKETS //**< This is by design, start of packet comes directly after the end of last one in streaming mode */
  21. #define IPECAMERA_BUG_MULTIFRAME_HEADERS //**< UFO Camera operates with 32-byte entities, but some times there is 16-byte padding before the data which may result in spliting the header between 2 DMA packets. We still need to define a minimal number of bytes which are always in the same DMA packet (CMOSIS_ENTITY_SIZE) */
  22. #define IPECAMERA_BUG_REPEATING_DATA //**< 16 bytes repeated at frame offset 4096, the problem start/stop happenning on board restart */
  23. //#define IPECAMERA_BUG_STUCKED_BUSY //**< DMA may stuck in busy. According to Michele, the work-around is to disable triggering and read all data out. Not checked */
  24. //#define IPECAMERA_BUG_INCOMPLETE_PACKETS //**< Support incomplete packets, i.e. check for frame magic even if full frame size is not reached yet (slow) */
  25. //#define IPECAMERA_ANNOUNCE_READY //**< Announce new event only after the reconstruction is done */
  26. //#define IPECAMERA_CLEAN_ON_START //**< Read all the data from DMA before starting of recording */
  27. //#define IPECAMERA_ADJUST_BUFFER_SIZE //**< Adjust default buffer size based on the hardware capabilities (number of frames stored in the FPGA memory) */
  28. #define IPECAMERA_DEFAULT_BUFFER_SIZE 256 //**< number of buffers in a ring buffer, should be power of 2 */
  29. #define IPECAMERA_DEFAULT_CMOSIS20_BUFFER_SIZE 64 //*< overrides number of buffers for CMOSIS20 sensor to reduce memory consumption */
  30. #define IPECAMERA_RESERVE_BUFFERS 4 //**< Return Frame is Lost error, if requested frame will be overwritten after specified number of frames
  31. #define IPECAMERA_DMA_TIMEOUT 50000 //**< Default DMA timeout */
  32. #define IPECAMERA_TRIGGER_TIMEOUT 200000 //**< In trigger call allow specified timeout for camera to get out of busy state. Set 0 to fail immideatly */
  33. #define IPECAMERA_CMOSIS_RESET_DELAY 250000 //**< Michele thinks 250 should be enough, but reset failing in this case */
  34. #define IPECAMERA_CMOSIS_REGISTER_DELAY 250000 //**< Michele thinks 250 should be enough, but reset failing in this case */
  35. #define IPECAMERA_SPI_REGISTER_DELAY 10000 //**< Delay between consequitive access to the registers */
  36. #define IPECAMERA_NEXT_FRAME_DELAY 1000 //**< Michele requires 30000 to sync between End Of Readout and next Frame Req */
  37. #define IPECAMERA_TRIGGER_DELAY 0 //**< Defines how long the trigger bits should be set */
  38. #define IPECAMERA_READ_STATUS_DELAY 1000 //**< According to Uros, 1ms delay needed before consequitive reads from status registers */
  39. #define IPECAMERA_NOFRAME_SLEEP 100 //**< Sleep while polling for a new frame in reader */
  40. #define IPECAMERA_NOFRAME_PREPROC_SLEEP 100 //**< Sleep while polling for a new frame in pre-processor */
  41. #define IPECAMERA_EXPECTED_STATUS_4 0x08409FFFF
  42. #define IPECAMERA_EXPECTED_STATUS 0x08449FFFF
  43. #define IPECAMERA_END_OF_SEQUENCE 0x1F001001
  44. #define CMOSIS_FRAME_HEADER_SIZE (8 * sizeof(ipecamera_payload_t))
  45. #define CMOSIS_FRAME_TAIL_SIZE (8 * sizeof(ipecamera_payload_t))
  46. #ifdef IPECAMERA_BUG_MULTIFRAME_HEADERS
  47. # define CMOSIS_ENTITY_SIZE (4 * sizeof(ipecamera_payload_t)) //**< This normaly should be equal to 32 bytes like header and tail, but in fact is only 16 bytes */
  48. #else /* IPECAMERA_BUG_MULTIFRAME_HEADERS */
  49. # define CMOSIS_ENTITY_SIZE (8 * sizeof(ipecamera_payload_t)) //**< This normaly should be equal to 32 bytes like header and tail, but in fact is only 16 bytes */
  50. #endif /* IPECAMERA_BUG_MULTIFRAME_HEADERS */
  51. #define CMOSIS_MAX_CHANNELS 16
  52. #define CMOSIS_PIXELS_PER_CHANNEL 128
  53. #define CMOSIS_WIDTH (CMOSIS_MAX_CHANNELS * CMOSIS_PIXELS_PER_CHANNEL)
  54. //#define IPECAMERA_MAX_LINES 1088
  55. #define CMOSIS_MAX_LINES 2048
  56. /*
  57. #define CMOSIS20_MAX_CHANNELS 8
  58. #define CMOSIS20_PIXELS_PER_CHANNEL 640
  59. #define CMOSIS20_WIDTH (CMOSIS20_MAX_CHANNELS * CMOSIS20_PIXELS_PER_CHANNEL)
  60. */
  61. #define CMOSIS20_PIXELS_PER_CHANNEL 320
  62. #define CMOSIS20_WIDTH (CMOSIS_MAX_CHANNELS * CMOSIS20_PIXELS_PER_CHANNEL)
  63. #define CMOSIS20_MAX_LINES 3840
  64. #define IPECAMERA_FRAME_REQUEST 0x209 // 0x80000209 // 0x1E9
  65. #define IPECAMERA_IDLE 0x201 // 0x80000201 // 0x1E1
  66. #define IPECAMERA_START_INTERNAL_STIMULI 0x1F1
  67. #define IPECAMERA_MODE_16_CHAN_IO 0
  68. #define IPECAMERA_MODE_4_CHAN_IO 2
  69. #define IPECAMERA_MODE_12_BIT_ADC 2
  70. #define IPECAMERA_MODE_11_BIT_ADC 1
  71. #define IPECAMERA_MODE_10_BIT_ADC 0
  72. #ifdef IPECAMERA_DEBUG_RAW_FRAMES
  73. # define IPECAMERA_DEBUG_RAW_FRAMES_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  74. # define IPECAMERA_DEBUG_RAW_FRAMES_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  75. #else /* IPECAMERA_DEBUG_RAW_FRAMES */
  76. # define IPECAMERA_DEBUG_RAW_FRAMES_MESSAGE(function, ...)
  77. # define IPECAMERA_DEBUG_RAW_FRAMES_BUFFER(function, ...)
  78. #endif /* IPECAMERA_DEBUG_RAW_FRAMES */
  79. #ifdef IPECAMERA_DEBUG_BROKEN_FRAMES
  80. # define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  81. # define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  82. #else /* IPECAMERA_DEBUG_BROKEN_FRAMES */
  83. # define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...)
  84. # define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...)
  85. #endif /* IPECAMERA_DEBUG_BROKEN_FRAMES */
  86. #ifdef IPECAMERA_DEBUG_RAW_PACKETS
  87. # define IPECAMERA_DEBUG_RAW_PACKETS_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  88. # define IPECAMERA_DEBUG_RAW_PACKETS_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  89. #else /* IPECAMERA_DEBUG_RAW_PACKETS */
  90. # define IPECAMERA_DEBUG_RAW_PACKETS_MESSAGE(function, ...)
  91. # define IPECAMERA_DEBUG_RAW_PACKETS_BUFFER(function, ...)
  92. #endif /* IPECAMERA_DEBUG_RAW_PACKETS */
  93. #ifdef IPECAMERA_DEBUG_HARDWARE
  94. # define IPECAMERA_DEBUG_HARDWARE_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  95. # define IPECAMERA_DEBUG_HARDWARE_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  96. #else /* IPECAMERA_DEBUG_HARDWARE */
  97. # define IPECAMERA_DEBUG_HARDWARE_MESSAGE(function, ...)
  98. # define IPECAMERA_DEBUG_HARDWARE_BUFFER(function, ...)
  99. #endif /* IPECAMERA_DEBUG_HARDWARE */
  100. #ifdef IPECAMERA_DEBUG_FRAME_HEADERS
  101. # define IPECAMERA_DEBUG_FRAME_HEADERS_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  102. # define IPECAMERA_DEBUG_FRAME_HEADERS_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  103. #else /* IPECAMERA_DEBUG_RAW_FRAMES */
  104. # define IPECAMERA_DEBUG_FRAME_HEADERS_MESSAGE(function, ...)
  105. # define IPECAMERA_DEBUG_FRAME_HEADERS_BUFFER(function, ...)
  106. #endif /* IPECAMERA_DEBUG_RAW_FRAMES */
  107. #ifdef IPECAMERA_DEBUG_API
  108. # define IPECAMERA_DEBUG_API_MESSAGE(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
  109. # define IPECAMERA_DEBUG_API_BUFFER(function, ...) if (ipecamera_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
  110. #else /* IPECAMERA_DEBUG_API */
  111. # define IPECAMERA_DEBUG_API_MESSAGE(function, ...)
  112. # define IPECAMERA_DEBUG_API_BUFFER(function, ...)
  113. #endif /* IPECAMERA_DEBUG_API */
  114. #define ipecamera_debug(function, ...) \
  115. IPECAMERA_DEBUG_##function##_MESSAGE(IPECAMERA_DEBUG_##function, PCILIB_LOG_DEFAULT, __VA_ARGS__)
  116. #define ipecamera_debug_buffer(function, ...) \
  117. IPECAMERA_DEBUG_##function##_BUFFER(IPECAMERA_DEBUG_##function, __VA_ARGS__)
  118. typedef uint32_t ipecamera_payload_t;
  119. typedef enum {
  120. IPECAMERA_FIRMWARE_UNKNOWN = 0,
  121. IPECAMERA_FIRMWARE_UFO5 = 5,
  122. IPECAMERA_FIRMWARE_CMOSIS20 = 6
  123. } ipecamera_firmware_t;
  124. typedef enum {
  125. IPECAMERA_FORMAT_CMOSIS = 5,
  126. IPECAMERA_FORMAT_CMOSIS20 = 6,
  127. IPECAMERA_FORMAT_POLARIS = 7
  128. } ipecamera_format_t;
  129. typedef struct {
  130. pcilib_event_id_t evid;
  131. struct timeval timestamp;
  132. } ipecamera_autostop_t;
  133. typedef struct {
  134. size_t i;
  135. pthread_t thread;
  136. ipecamera_t *ipecamera;
  137. int started; /**< flag indicating that join & cleanup is required */
  138. } ipecamera_preprocessor_t;
  139. typedef struct {
  140. ipecamera_event_info_t event; /**< this structure is overwritten by the reader thread, we need a copy */
  141. pthread_rwlock_t mutex; /**< this mutex protects reconstructed buffers only, the raw data, event_info, etc. will be overwritten by reader thread anyway */
  142. } ipecamera_frame_t;
  143. struct ipecamera_s {
  144. pcilib_context_t event;
  145. UfoDecoder *ipedec;
  146. pcilib_lock_t *run_lock; /**< Lock protecting global camera operation */
  147. pcilib_lock_t *stream_lock; /**< Lock protecting stream/next_frame operations */
  148. pcilib_lock_t *trigger_lock; /**< Lock protecting stream/next_frame operations */
  149. int run_locked; /**< Flag indicating if camera is currently locked */
  150. int stream_locked; /**< Flag indicating if camera is currently locked */
  151. int trigger_locked; /**< Flag indicating if camera is currently locked */
  152. char *data;
  153. ipecamera_pixel_t *image;
  154. size_t size;
  155. volatile pcilib_event_id_t event_id;
  156. volatile pcilib_event_id_t preproc_id;
  157. pcilib_event_id_t reported_id;
  158. pcilib_dma_engine_t rdma;
  159. pcilib_register_t control_reg, status_reg;
  160. pcilib_register_t status2_reg, status3_reg;
  161. // pcilib_register_t n_lines_reg;
  162. // uint16_t line_reg;
  163. // pcilib_register_t exposure_reg;
  164. // pcilib_register_t flip_reg;
  165. pcilib_register_t firmware_version_reg;
  166. pcilib_register_t adc_resolution_reg;
  167. pcilib_register_t output_mode_reg;
  168. pcilib_register_t max_frames_reg;
  169. pcilib_register_t num_frames_reg;
  170. int started; /**< Camera is in grabbing mode (start function is called) */
  171. int streaming; /**< Camera is in streaming mode (we are within stream call) */
  172. int parse_data; /**< Indicates if some processing of the data is required, otherwise only rawdata_callback will be called */
  173. volatile int run_reader; /**< Instructs the reader thread to stop processing */
  174. volatile int run_streamer; /**< Indicates request to stop streaming events and can be set by reader_thread upon exit or by user request */
  175. volatile int run_preprocessors; /**< Instructs preprocessors to exit */
  176. ipecamera_autostop_t autostop;
  177. struct timeval autostop_time;
  178. struct timeval next_trigger; /**< The minimal delay between trigger signals is mandatory, this indicates time when next trigger is possible */
  179. size_t buffer_size; /**< How many images to store */
  180. size_t buffer_pos; /**< Current image offset in the buffer, due to synchronization reasons should not be used outside of reader_thread */
  181. size_t cur_size; /**< Already written part of data in bytes */
  182. size_t raw_size; /**< Expected maximum size of raw data in bytes */
  183. size_t padded_size; /**< Expected maximum size of buffer for raw data, including additional padding */
  184. size_t roi_raw_size; /**< Expected size (for currently configured ROI) of raw data in bytes */
  185. size_t roi_padded_size; /**< Expected size (for currently configured ROI) of buffer for raw data, including additional padding */
  186. size_t image_size; /**< Size of a single image in bytes */
  187. size_t max_frames; /**< Maximal number of frames what may be buffered in camera DDR memory */
  188. ipecamera_firmware_t firmware; /**< Firmware type */
  189. int cmosis_outputs; /**< Number of active cmosis outputs: 4 or 16 */
  190. int width, height;
  191. // void *raw_buffer;
  192. void *buffer;
  193. ipecamera_change_mask_t *cmask;
  194. ipecamera_frame_t *frame;
  195. #ifdef IPECAMERA_BUG_MULTIFRAME_HEADERS
  196. size_t saved_header_size; /**< If it happened that the frame header is split between 2 DMA packets, this variable holds the size of the part containing in the first packet */
  197. char saved_header[CMOSIS_FRAME_HEADER_SIZE]; /**< If it happened that the frame header is split between 2 DMA packets, this variable holds the part containing in the first packet */
  198. #endif /* IPECAMERA_BUG_MULTIFRAME_HEADERS */
  199. size_t data_line_size;
  200. ipecamera_image_dimensions_t dim;
  201. pthread_t rthread;
  202. size_t n_preproc;
  203. ipecamera_preprocessor_t *preproc;
  204. pthread_mutex_t preproc_mutex;
  205. int preproc_mutex_destroy;
  206. int frame_mutex_destroy;
  207. };
  208. #endif /* _IPECAMERA_PRIVATE_H */