1 #ifndef _BFLB_CAM_H
2 #define _BFLB_CAM_H
3 
4 #include "bflb_core.h"
5 
6 /** @addtogroup LHAL
7   * @{
8   */
9 
10 /** @addtogroup CAM
11   * @{
12   */
13 
14 /** @defgroup CAM_INPUT_FORMAT CAMERA input format definition
15   * @{
16   */
17 #define CAM_INPUT_FORMAT_YUV422_YUYV 0
18 #define CAM_INPUT_FORMAT_YUV422_YVYU 1
19 #define CAM_INPUT_FORMAT_YUV422_UYVY 2
20 #define CAM_INPUT_FORMAT_YUV422_VYUY 3
21 #define CAM_INPUT_FORMAT_GRAY        4
22 #define CAM_INPUT_FORMAT_RGB565      5
23 #define CAM_INPUT_FORMAT_BGR565      6
24 #define CAM_INPUT_FORMAT_RGB888      7
25 #define CAM_INPUT_FORMAT_BGR888      8
26 /**
27   * @}
28   */
29 
30 /** @defgroup CAM_OUTPUT_FORMAT CAMERA output format definition
31   * @{
32   */
33 #define CAM_OUTPUT_FORMAT_AUTO             0
34 #define CAM_OUTPUT_FORMAT_YUV422           1
35 #define CAM_OUTPUT_FORMAT_GRAY             2
36 #define CAM_OUTPUT_FORMAT_YUV422_UV        3
37 #define CAM_OUTPUT_FORMAT_YUV420_UV        4
38 #define CAM_OUTPUT_FORMAT_RGB565_OR_BGR565 5
39 #define CAM_OUTPUT_FORMAT_RGB888_OR_BGR888 6
40 #if !defined(BL702)
41 #define CAM_OUTPUT_FORMAT_RGB888_TO_RGB565   7
42 #define CAM_OUTPUT_FORMAT_RGB888_TO_BGR565   8
43 #define CAM_OUTPUT_FORMAT_RGB888_TO_RGBA8888 9
44 #endif
45 
46 /**
47   * @}
48   */
49 
50 /** @defgroup CAM_INTSTS CAMERA interrupt status definition
51   * @{
52   */
53 #if defined(BL702)
54 #define CAM_INTSTS_NORMAL           (1 << 0)
55 #define CAM_INTSTS_MEMORY_OVERWRITE (1 << 2)
56 #define CAM_INTSTS_FRAME_OVERWRITE  (1 << 4)
57 #define CAM_INTSTS_FIFO_OVERWRITE   (1 << 6)
58 #define CAM_INTSTS_HSYNC_MISMATCH   (1 << 8)
59 #define CAM_INTSTS_VSYNC_MISMATCH   (1 << 9)
60 #else
61 #define CAM_INTSTS_NORMAL           (1 << 12)
62 #define CAM_INTSTS_MEMORY_OVERWRITE (1 << 13)
63 #define CAM_INTSTS_FRAME_OVERWRITE  (1 << 14)
64 #define CAM_INTSTS_FIFO_OVERWRITE   (1 << 15)
65 #define CAM_INTSTS_HSYNC_MISMATCH   (1 << 21)
66 #define CAM_INTSTS_VSYNC_MISMATCH   (1 << 22)
67 #endif
68 /**
69   * @}
70   */
71 
72 /** @defgroup CAM_INTMASK CAMERA interrupt mask definition
73   * @{
74   */
75 #if defined(BL702)
76 #define CAM_INTMASK_NORMAL           (1 << 0)
77 #define CAM_INTMASK_MEMORY_OVERWRITE (1 << 2)
78 #define CAM_INTMASK_FRAME_OVERWRITE  (1 << 3)
79 #define CAM_INTMASK_FIFO_OVERWRITE   (1 << 4)
80 #define CAM_INTMASK_HSYNC_MISMATCH   (1 << 5)
81 #define CAM_INTMASK_VSYNC_MISMATCH   (1 << 6)
82 #else
83 #define CAM_INTMASK_NORMAL           (1 << 8)
84 #define CAM_INTMASK_MEMORY_OVERWRITE (1 << 9)
85 #define CAM_INTMASK_FRAME_OVERWRITE  (1 << 10)
86 #define CAM_INTMASK_FIFO_OVERWRITE   (1 << 11)
87 #define CAM_INTMASK_HSYNC_MISMATCH   (1 << 6)
88 #define CAM_INTMASK_VSYNC_MISMATCH   (1 << 7)
89 #endif
90 /**
91   * @}
92   */
93 
94 /** @defgroup CAM_INTCLR CAMERA interrupt clear definition
95   * @{
96   */
97 #define CAM_INTCLR_NORMAL           (1 << 4)
98 #define CAM_INTCLR_MEMORY_OVERWRITE (1 << 5)
99 #define CAM_INTCLR_FRAME_OVERWRITE  (1 << 6)
100 #define CAM_INTCLR_FIFO_OVERWRITE   (1 << 7)
101 #define CAM_INTCLR_HSYNC_MISMATCH   (1 << 8)
102 #define CAM_INTCLR_VSYNC_MISMATCH   (1 << 9)
103 /**
104   * @}
105   */
106 
107 /** @defgroup CAM_POLARITY CAMERA hsync/vsync polarity active level definition
108   * @{
109   */
110 #define CAM_POLARITY_ACTIVE_LOW  0
111 #define CAM_POLARITY_ACTIVE_HIGH 1
112 /**
113   * @}
114   */
115 
116 /** @defgroup CAM_INPUT_SOURCE CAMERA input source definition
117   * @{
118   */
119 #define CAM_INPUT_SOURCE_DVP 0
120 #if defined(BL808)
121 #define CAM_INPUT_SOURCE_CSI 1
122 #endif
123 /**
124   * @}
125   */
126 
127 /** @defgroup CAM_BURST CAMERA burst length definition
128   * @{
129   */
130 #define CAM_BURST_INCR1  0
131 #define CAM_BURST_INCR4  1
132 #define CAM_BURST_INCR8  2
133 #define CAM_BURST_INCR16 3
134 #if !defined(BL702)
135 #define CAM_BURST_INCR32 5
136 #define CAM_BURST_INCR64 6
137 #endif
138 /**
139   * @}
140   */
141 
142 /** @defgroup CAM_CMD CAMERA feature control cmd definition
143   * @{
144   */
145 #define CAM_CMD_SET_VSYNC_POLARITY 1
146 #define CAM_CMD_SET_HSYNC_POLARITY 2
147 #define CAM_CMD_SET_BURST          3
148 #if !defined(BL702)
149 #define CAM_CMD_SET_RGBA8888_ALPHA 4
150 #define CAM_CMD_GET_FRAME_ID       5
151 #endif
152 #define CAM_CMD_WRAP_MODE                6
153 #define CAM_CMD_COUNT_TRIGGER_NORMAL_INT 7
154 #if !defined(BL702)
155 #define CAM_CMD_FRAME_ID_RESET         8
156 #define CAM_CMD_INVERSE_VSYNC_POLARITY 9
157 #define CAM_CMD_INVERSE_HSYNC_POLARITY 10
158 #endif
159 
160 /**
161   * @}
162   */
163 
164 /**
165  * @brief CAM configuration structure
166  *
167  * @param input_format   CAM input format, use @ref CAM_INPUT_FORMAT
168  * @param resolution_x   CAM resolution x
169  * @param resolution_y   CAM resolution y
170  * @param h_blank        CAM Hsync blank
171  * @param pixel_clock    CAM pixel clock
172  * @param with_mjpeg     CAM with mjpeg or not
173  * @param input_source   CAM input source, use @ref CAM_INPUT_SOURCE
174  * @param output_format  CAM output format, use @ref CAM_OUTPUT_FORMAT
175  * @param output_bufaddr CAM output buffer address , must be align 16
176  * @param output_bufsize CAM output buffer size, should not be less than one frame size
177  */
178 struct bflb_cam_config_s {
179     uint8_t input_format;
180     uint16_t resolution_x;
181     uint16_t resolution_y;
182     uint16_t h_blank;
183     uint32_t pixel_clock;
184     bool with_mjpeg;
185     uint8_t input_source;
186     uint8_t output_format;
187     uint32_t output_bufaddr;
188     uint32_t output_bufsize;
189 };
190 
191 #ifdef __cplusplus
192 extern "C" {
193 #endif
194 
195 /**
196  * @brief Initialize cam.
197  *
198  * @param [in] dev device handle
199  * @param [in] config pointer to cam configure structure
200  */
201 void bflb_cam_init(struct bflb_device_s *dev, const struct bflb_cam_config_s *config);
202 
203 /**
204  * @brief Enable cam.
205  *
206  * @param [in] dev device handle
207  */
208 void bflb_cam_start(struct bflb_device_s *dev);
209 
210 /**
211  * @brief Disable cam.
212  *
213  * @param [in] dev device handle
214  */
215 void bflb_cam_stop(struct bflb_device_s *dev);
216 
217 /**
218  * @brief Mask or unmask cam interrupt.
219  *
220  * @param [in] dev device handle
221  * @param [in] int_type cam interrupt mask type, use @ref CAM_INTMASK
222  * @param [in] mask mask or unmask
223  */
224 void bflb_cam_int_mask(struct bflb_device_s *dev, uint32_t int_type, bool mask);
225 
226 /**
227  * @brief Clear cam interrupt.
228  *
229  * @param [in] dev device handle
230  * @param [in] int_type int_type cam interrupt clear type, use @ref CAM_INTCLR
231  */
232 void bflb_cam_int_clear(struct bflb_device_s *dev, uint32_t int_type);
233 
234 /**
235  * @brief Crop vsync.
236  *
237  * @param [in] dev device handle
238  * @param [in] start_line start line of window
239  * @param [in] end_line end line of window, not include
240  */
241 void bflb_cam_crop_vsync(struct bflb_device_s *dev, uint16_t start_line, uint16_t end_line);
242 
243 /**
244  * @brief Crop hsync.
245  *
246  * @param [in] dev device handle
247  * @param [in] start_pixel start pixel of each line
248  * @param [in] end_pixel end pixel of each line, not include
249  */
250 void bflb_cam_crop_hsync(struct bflb_device_s *dev, uint16_t start_pixel, uint16_t end_pixel);
251 
252 /**
253  * @brief Pop one frame.
254  *
255  * @param [in] dev device handle
256  */
257 void bflb_cam_pop_one_frame(struct bflb_device_s *dev);
258 
259 #if !defined(BL702)
260 /**
261  * @brief Swap input order of y and uv.
262  *
263  * @param [in] dev device handle
264  * @param [in] enable enable or disable
265  */
266 void bflb_cam_swap_input_yu_order(struct bflb_device_s *dev, bool enable);
267 
268 /**
269  * @brief Set frame filter, if frame_count = 3, frame_valid = 101b, second frame will be dropped every 3 frames.
270  *
271  * @param [in] dev device handle
272  * @param [in] frame_count frame filter period
273  * @param [in] frame_valid frame valid
274  */
275 void bflb_cam_filter_frame_period(struct bflb_device_s *dev, uint8_t frame_count, uint32_t frame_valid);
276 #endif
277 
278 /**
279  * @brief Get frame count.
280  *
281  * @param [in] dev device handle
282  * @return Frame count
283  */
284 uint8_t bflb_cam_get_frame_count(struct bflb_device_s *dev);
285 
286 /**
287  * @brief Get frame information.
288  *
289  * @param [in] dev device handle
290  * @param [out] pic pointer to frame start address
291  * @return Size of frame
292  */
293 uint32_t bflb_cam_get_frame_info(struct bflb_device_s *dev, uint8_t **pic);
294 
295 /**
296  * @brief Get interrupt status.
297  *
298  * @param [in] dev device handle
299  * @return Interrupt status
300  */
301 uint32_t bflb_cam_get_intstatus(struct bflb_device_s *dev);
302 
303 /**
304  * @brief Control cam feature.
305  *
306  * @param [in] dev device handle
307  * @param [in] cmd feature command
308  * @param [in] arg user data
309  * @return A negated errno value on failure
310  */
311 int bflb_cam_feature_control(struct bflb_device_s *dev, int cmd, size_t arg);
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 /**
318   * @}
319   */
320 
321 /**
322   * @}
323   */
324 
325 #endif
326