private.h 11 KB

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