summaryrefslogtreecommitdiffstats
path: root/private.h
blob: 74b89c2e9430de6eee8b6748618a1dbdd55e98bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef _PCIDEV_PRIVATE_H
#define _PCIDEV_PRIVATE_H

#include <pthread.h>
#include <pcilib/model.h>
#include <pcilib/debug.h>
#include <pcilib/locking.h>
#include "events.h"
#include "pcidev.h"
#include "env.h"

#define PCIDEV_DEBUG

#ifdef PCIDEV_DEBUG
# define PCIDEV_DEBUG_RAW_FRAMES		//**< Store all raw frames */
# define IPECAMERA_DEBUG_BROKEN_FRAMES		//**< Store broken frames in the specified directory */
# define PCIDEV_DEBUG_RAW_PACKETS		//**< Store all raw packets read from DMA grouped in frames */
# define PCIDEV_DEBUG_HARDWARE			//**< Produce various debugging information about pcidev operation */
# define IPECAMERA_DEBUG_FRAME_HEADERS		//**< Print frame headers & footers */
# define PCIDEV_DEBUG_API			//**< Debug IPECamera API calls */
#endif /* PCIDEV_DEBUG */

#define PCIDEV_DEFAULT_BUFFER_SIZE 256  	//**< number of buffers in a ring buffer, should be power of 2 */
#define PCIDEV_INTEGRATION_PERIOD 5		//**< the duration of integration period, in microseconds */
#define PCIDEV_EVENT_SIZE 256			//**< the size of event data in bytes, always 256 in this example */
#define PCIDEV_NOEVENT_SLEEP 100		//**< Sleep while polling for a new frame in reader */
#define PCIDEV_DMA_TIMEOUT 50000		//**< Default DMA timeout in microseconds */


#ifdef PCIDEV_DEBUG_RAW_FRAMES
# define PCIDEV_DEBUG_RAW_FRAMES_MESSAGE(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define PCIDEV_DEBUG_RAW_FRAMES_BUFFER(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* PCIDEV_DEBUG_RAW_FRAMES */
# define PCIDEV_DEBUG_RAW_FRAMES_MESSAGE(function, ...)
# define PCIDEV_DEBUG_RAW_FRAMES_BUFFER(function, ...)
#endif /* PCIDEV_DEBUG_RAW_FRAMES */

#ifdef IPECAMERA_DEBUG_BROKEN_FRAMES
# define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* IPECAMERA_DEBUG_BROKEN_FRAMES */
# define IPECAMERA_DEBUG_BROKEN_FRAMES_MESSAGE(function, ...)
# define IPECAMERA_DEBUG_BROKEN_FRAMES_BUFFER(function, ...)
#endif /* IPECAMERA_DEBUG_BROKEN_FRAMES */

#ifdef PCIDEV_DEBUG_RAW_PACKETS
# define PCIDEV_DEBUG_RAW_PACKETS_MESSAGE(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define PCIDEV_DEBUG_RAW_PACKETS_BUFFER(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* PCIDEV_DEBUG_RAW_PACKETS */
# define PCIDEV_DEBUG_RAW_PACKETS_MESSAGE(function, ...)
# define PCIDEV_DEBUG_RAW_PACKETS_BUFFER(function, ...)
#endif /* PCIDEV_DEBUG_RAW_PACKETS */

#ifdef PCIDEV_DEBUG_HARDWARE
# define PCIDEV_DEBUG_HARDWARE_MESSAGE(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define PCIDEV_DEBUG_HARDWARE_BUFFER(function, ...) if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* PCIDEV_DEBUG_HARDWARE */
# define PCIDEV_DEBUG_HARDWARE_MESSAGE(function, ...)
# define PCIDEV_DEBUG_HARDWARE_BUFFER(function, ...)
#endif /* PCIDEV_DEBUG_HARDWARE */

#ifdef PCIDEV_DEBUG_FRAME_HEADERS
# define PCIDEV_DEBUG_FRAME_HEADERS_MESSAGE(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define PCIDEV_DEBUG_FRAME_HEADERS_BUFFER(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* PCIDEV_DEBUG_RAW_FRAMES */
# define PCIDEV_DEBUG_FRAME_HEADERS_MESSAGE(function, ...)
# define PCIDEV_DEBUG_FRAME_HEADERS_BUFFER(function, ...)
#endif /* PCIDEV_DEBUG_RAW_FRAMES */

#ifdef PCIDEV_DEBUG_API
# define PCIDEV_DEBUG_API_MESSAGE(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
# define PCIDEV_DEBUG_API_BUFFER(function, ...)  if (pcidev_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
#else /* PCIDEV_DEBUG_API */
# define PCIDEV_DEBUG_API_MESSAGE(function, ...)
# define PCIDEV_DEBUG_API_BUFFER(function, ...)
#endif /* PCIDEV_DEBUG_API */


#define pcidev_debug(function, ...) \
    PCIDEV_DEBUG_##function##_MESSAGE(PCIDEV_DEBUG_##function, PCILIB_LOG_DEFAULT, __VA_ARGS__)

#define pcidev_debug_buffer(function, ...) \
    PCIDEV_DEBUG_##function##_BUFFER(PCIDEV_DEBUG_##function, __VA_ARGS__)


typedef uint32_t pcidev_payload_t;


typedef struct {
    pcilib_event_id_t evid;
    struct timeval timestamp;
} pcidev_autostop_t;

typedef struct {
    pcidev_event_info_t event;		/**< this structure is overwritten by the reader thread, we need a copy */
    pthread_rwlock_t mutex;		/**< this mutex protects reconstructed buffers only, the raw data, event_info, etc. will be overwritten by reader thread anyway */
} pcidev_event_t;

struct pcidev_s {
    pcilib_context_t pcictx;

    pcilib_lock_t *run_lock;		/**< Lock protecting global camera operation */
    pcilib_lock_t *stream_lock;		/**< Lock protecting stream/next_frame operations */
    pcilib_lock_t *trigger_lock;	/**< Lock protecting stream/next_frame operations */
    int run_locked;			/**< Flag indicating if camera is currently locked */
    int stream_locked;			/**< Flag indicating if camera is currently locked */
    int trigger_locked;			/**< Flag indicating if camera is currently locked */

    char *data;
    size_t size;

    volatile pcilib_event_id_t event_id;
    volatile pcilib_event_id_t preproc_id;
    pcilib_event_id_t reported_id;

    pcilib_dma_engine_t rdma;

    int started;			/**< Camera is in grabbing mode (start function is called) */
    int streaming;			/**< Camera is in streaming mode (we are within stream call) */
    int process_data;			/**< Indicates if some processing of the data is required, otherwise only rawdata_callback will be called */
    volatile int run_streamer;		/**< Indicates request to stop streaming events and can be set by reader_thread upon exit or by user request */
    
    pcidev_autostop_t autostop;
    struct timeval eio_timestamp;

    size_t buffer_size;			/**< How many images to store */
    size_t buffer_pos;			/**< Current image offset in the buffer, due to synchronization reasons should not be used outside of reader_thread */
    size_t event_size;			/**< Size of raw event data in bytes */
    
    void *buffer;			/**< Ring buffer for event data. Here we consider only a single type of events */
    pcidev_event_t *event;		/**< Description of events. */
    int event_mutex_destroy;
};

#endif /* _PCIDEV_PRIVATE_H */