private.h 8.9 KB

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