private.h 11 KB

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