1 /*
2  * Copyright 2017-2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef FSL_CSI_H_
10 #define FSL_CSI_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup csi_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 #define FSL_CSI_DRIVER_VERSION (MAKE_VERSION(2, 1, 5))
26 /*! @} */
27 
28 #define CSI_REG_CR1(base)       (base)->CR1
29 #define CSI_REG_CR2(base)       (base)->CR2
30 #define CSI_REG_CR3(base)       (base)->CR3
31 #define CSI_REG_CR18(base)      (base)->CR18
32 #define CSI_REG_SR(base)        (base)->SR
33 #define CSI_REG_DMASA_FB1(base) (base)->DMASA_FB1
34 #define CSI_REG_DMASA_FB2(base) (base)->DMASA_FB2
35 #define CSI_REG_IMAG_PARA(base) (base)->IMAG_PARA
36 #define CSI_REG_FBUF_PARA(base) (base)->FBUF_PARA
37 
38 /*! @brief Size of the frame buffer queue used in CSI transactional function. */
39 #ifndef CSI_DRIVER_QUEUE_SIZE
40 #define CSI_DRIVER_QUEUE_SIZE 4U
41 #endif
42 
43 /*! @brief Enable fragment capture function or not. */
44 #ifndef CSI_DRIVER_FRAG_MODE
45 #define CSI_DRIVER_FRAG_MODE 0U
46 #endif
47 
48 /*
49  * The interrupt enable bits are in registers CSICR1[16:31], CSICR3[0:7],
50  * and CSICR18[2:9]. So merge them into an uint32_t value, place CSICR18 control
51  * bits to [8:15].
52  */
53 #define CSI_CR1_INT_EN_MASK  0xFFFF0000U
54 #define CSI_CR3_INT_EN_MASK  0x000000FFU
55 #define CSI_CR18_INT_EN_MASK 0x0000FF00U
56 
57 #if ((~CSI_CR1_INT_EN_MASK) &                                                                                   \
58      (CSI_CR1_EOF_INT_EN_MASK | CSI_CR1_COF_INT_EN_MASK | CSI_CR1_SF_OR_INTEN_MASK | CSI_CR1_RF_OR_INTEN_MASK | \
59       CSI_CR1_SFF_DMA_DONE_INTEN_MASK | CSI_CR1_STATFF_INTEN_MASK | CSI_CR1_FB2_DMA_DONE_INTEN_MASK |           \
60       CSI_CR1_FB1_DMA_DONE_INTEN_MASK | CSI_CR1_RXFF_INTEN_MASK | CSI_CR1_SOF_INTEN_MASK))
61 #error CSI_CR1_INT_EN_MASK could not cover all interrupt bits in CSICR1.
62 #endif
63 
64 #if ((~CSI_CR3_INT_EN_MASK) & (CSI_CR3_ECC_INT_EN_MASK | CSI_CR3_HRESP_ERR_EN_MASK))
65 #error CSI_CR3_INT_EN_MASK could not cover all interrupt bits in CSICR3.
66 #endif
67 
68 #if ((~CSI_CR18_INT_EN_MASK) &                                                                                   \
69      ((CSI_CR18_FIELD0_DONE_IE_MASK | CSI_CR18_DMA_FIELD1_DONE_IE_MASK | CSI_CR18_BASEADDR_CHANGE_ERROR_IE_MASK) \
70       << 6U))
71 #error CSI_CR18_INT_EN_MASK could not cover all interrupt bits in CSICR18.
72 #endif
73 
74 /*! @brief Error codes for the CSI driver. */
75 enum
76 {
77     kStatus_CSI_NoEmptyBuffer = MAKE_STATUS(kStatusGroup_CSI, 0), /*!< No empty frame buffer in queue to load to CSI. */
78     kStatus_CSI_NoFullBuffer  = MAKE_STATUS(kStatusGroup_CSI, 1), /*!< No full frame buffer in queue to read out. */
79     kStatus_CSI_QueueFull = MAKE_STATUS(kStatusGroup_CSI, 2), /*!< Queue is full, no room to save new empty buffer. */
80     kStatus_CSI_FrameDone = MAKE_STATUS(kStatusGroup_CSI, 3), /*!< New frame received and saved to queue. */
81 };
82 
83 /*!
84  * @brief CSI work mode.
85  *
86  * The CCIR656 interlace mode is not supported currently.
87  */
88 typedef enum _csi_work_mode
89 {
90     kCSI_GatedClockMode         = CSI_CR1_GCLK_MODE(1U), /*!< HSYNC, VSYNC, and PIXCLK signals are used. */
91     kCSI_NonGatedClockMode      = 0U,                    /*!< VSYNC, and PIXCLK signals are used. */
92     kCSI_CCIR656ProgressiveMode = CSI_CR1_CCIR_EN(1U),   /*!< CCIR656 progressive mode. */
93 } csi_work_mode_t;
94 
95 /*!
96  * @brief CSI data bus witdh.
97  */
98 typedef enum _csi_data_bus
99 {
100     kCSI_DataBus8Bit,  /*!< 8-bit data bus. */
101     kCSI_DataBus16Bit, /*!< 16-bit data bus. */
102     kCSI_DataBus24Bit, /*!< 24-bit data bus. */
103 } csi_data_bus_t;
104 
105 /*! @brief CSI signal polarity. */
106 enum _csi_polarity_flags
107 {
108     kCSI_HsyncActiveLow         = 0U,                     /*!< HSYNC is active low. */
109     kCSI_HsyncActiveHigh        = CSI_CR1_HSYNC_POL_MASK, /*!< HSYNC is active high. */
110     kCSI_DataLatchOnRisingEdge  = CSI_CR1_REDGE_MASK,     /*!< Pixel data latched at rising edge of pixel clock. */
111     kCSI_DataLatchOnFallingEdge = 0U,                     /*!< Pixel data latched at falling edge of pixel clock. */
112     kCSI_VsyncActiveHigh        = 0U,                     /*!< VSYNC is active high. */
113     kCSI_VsyncActiveLow         = CSI_CR1_SOF_POL_MASK,   /*!< VSYNC is active low. */
114 };
115 
116 /*! @brief Configuration to initialize the CSI module. */
117 typedef struct _csi_config
118 {
119     uint16_t width;           /*!< Pixels of the input frame. */
120     uint16_t height;          /*!< Lines of the input frame.  */
121     uint32_t polarityFlags;   /*!< Timing signal polarity flags, OR'ed value of @ref _csi_polarity_flags. */
122     uint8_t bytesPerPixel;    /*!< Bytes per pixel, valid values are:
123                                 - 2: Used for RGB565, YUV422, and so on.
124                                 - 4: Used for XRGB8888, XYUV444, and so on.
125                                 */
126     uint16_t linePitch_Bytes; /*!< Frame buffer line pitch, must be 8-byte aligned. */
127     csi_work_mode_t workMode; /*!< CSI work mode. */
128     csi_data_bus_t dataBus;   /*!< Data bus width. */
129     bool useExtVsync;         /*!< In CCIR656 progressive mode, set true to use external VSYNC signal, set false
130                                 to use internal VSYNC signal decoded from SOF. */
131 } csi_config_t;
132 
133 /*! @brief The CSI FIFO, used for FIFO operation. */
134 typedef enum _csi_fifo
135 {
136     kCSI_RxFifo   = (1U << 0U),  /*!< RXFIFO. */
137     kCSI_StatFifo = (1U << 1U),  /*!< STAT FIFO. */
138     kCSI_AllFifo  = 0x01 | 0x02, /*!< Both RXFIFO and STAT FIFO. */
139 } csi_fifo_t;
140 
141 /*! @brief CSI feature interrupt source. */
142 enum _csi_interrupt_enable
143 {
144     kCSI_EndOfFrameInterruptEnable       = CSI_CR1_EOF_INT_EN_MASK,         /*!< End of frame interrupt enable. */
145     kCSI_ChangeOfFieldInterruptEnable    = CSI_CR1_COF_INT_EN_MASK,         /*!< Change of field interrupt enable. */
146     kCSI_StatFifoOverrunInterruptEnable  = CSI_CR1_SF_OR_INTEN_MASK,        /*!< STAT FIFO overrun interrupt enable. */
147     kCSI_RxFifoOverrunInterruptEnable    = CSI_CR1_RF_OR_INTEN_MASK,        /*!< RXFIFO overrun interrupt enable. */
148     kCSI_StatFifoDmaDoneInterruptEnable  = CSI_CR1_SFF_DMA_DONE_INTEN_MASK, /*!< STAT FIFO DMA done interrupt enable. */
149     kCSI_StatFifoFullInterruptEnable     = CSI_CR1_STATFF_INTEN_MASK,       /*!< STAT FIFO full interrupt enable. */
150     kCSI_RxBuffer1DmaDoneInterruptEnable = CSI_CR1_FB2_DMA_DONE_INTEN_MASK, /*!< RX frame buffer 1 DMA transfer done. */
151     kCSI_RxBuffer0DmaDoneInterruptEnable = CSI_CR1_FB1_DMA_DONE_INTEN_MASK, /*!< RX frame buffer 0 DMA transfer done. */
152     kCSI_RxFifoFullInterruptEnable       = CSI_CR1_RXFF_INTEN_MASK,         /*!< RXFIFO full interrupt enable. */
153     kCSI_StartOfFrameInterruptEnable     = CSI_CR1_SOF_INTEN_MASK, /*!< Start of frame (SOF) interrupt enable. */
154 
155     kCSI_EccErrorInterruptEnable    = CSI_CR3_ECC_INT_EN_MASK,   /*!< ECC error detection interrupt enable. */
156     kCSI_AhbResErrorInterruptEnable = CSI_CR3_HRESP_ERR_EN_MASK, /*!< AHB response Error interrupt enable. */
157 
158     /*! The DMA output buffer base address changes before DMA completed. */
159     kCSI_BaseAddrChangeErrorInterruptEnable = CSI_CR18_BASEADDR_CHANGE_ERROR_IE_MASK << 6U,
160 
161     kCSI_Field0DoneInterruptEnable = CSI_CR18_FIELD0_DONE_IE_MASK << 6U,     /*!< Field 0 done interrupt enable. */
162     kCSI_Field1DoneInterruptEnable = CSI_CR18_DMA_FIELD1_DONE_IE_MASK << 6U, /*!< Field 1 done interrupt enable. */
163 };
164 
165 /*!
166  * @brief CSI status flags.
167  *
168  * The following status register flags can be cleared:
169  * - kCSI_EccErrorFlag
170  * - kCSI_AhbResErrorFlag
171  * - kCSI_ChangeOfFieldFlag
172  * - kCSI_StartOfFrameFlag
173  * - kCSI_EndOfFrameFlag
174  * - kCSI_RxBuffer1DmaDoneFlag
175  * - kCSI_RxBuffer0DmaDoneFlag
176  * - kCSI_StatFifoDmaDoneFlag
177  * - kCSI_StatFifoOverrunFlag
178  * - kCSI_RxFifoOverrunFlag
179  * - kCSI_Field0DoneFlag
180  * - kCSI_Field1DoneFlag
181  * - kCSI_BaseAddrChangeErrorFlag
182  */
183 enum _csi_flags
184 {
185     kCSI_RxFifoDataReadyFlag     = CSI_SR_DRDY_MASK,          /*!< RXFIFO data ready. */
186     kCSI_EccErrorFlag            = CSI_SR_ECC_INT_MASK,       /*!< ECC error detected. */
187     kCSI_AhbResErrorFlag         = CSI_SR_HRESP_ERR_INT_MASK, /*!< Hresponse (AHB bus response) Error. */
188     kCSI_ChangeOfFieldFlag       = CSI_SR_COF_INT_MASK,       /*!< Change of field. */
189     kCSI_Field0PresentFlag       = CSI_SR_F1_INT_MASK,        /*!< Field 0 present in CCIR mode. */
190     kCSI_Field1PresentFlag       = CSI_SR_F2_INT_MASK,        /*!< Field 1 present in CCIR mode. */
191     kCSI_StartOfFrameFlag        = CSI_SR_SOF_INT_MASK,       /*!< Start of frame (SOF) detected. */
192     kCSI_EndOfFrameFlag          = CSI_SR_EOF_INT_MASK,       /*!< End of frame (EOF) detected. */
193     kCSI_RxFifoFullFlag          = CSI_SR_RxFF_INT_MASK, /*!< RXFIFO full (Number of data reaches trigger level). */
194     kCSI_RxBuffer1DmaDoneFlag    = CSI_SR_DMA_TSF_DONE_FB2_MASK,       /*!< RX frame buffer 1 DMA transfer done. */
195     kCSI_RxBuffer0DmaDoneFlag    = CSI_SR_DMA_TSF_DONE_FB1_MASK,       /*!< RX frame buffer 0 DMA transfer done. */
196     kCSI_StatFifoFullFlag        = CSI_SR_STATFF_INT_MASK,             /*!< STAT FIFO full (Reach trigger level). */
197     kCSI_StatFifoDmaDoneFlag     = CSI_SR_DMA_TSF_DONE_SFF_MASK,       /*!< STAT FIFO DMA transfer done. */
198     kCSI_StatFifoOverrunFlag     = CSI_SR_SF_OR_INT_MASK,              /*!< STAT FIFO overrun. */
199     kCSI_RxFifoOverrunFlag       = CSI_SR_RF_OR_INT_MASK,              /*!< RXFIFO overrun. */
200     kCSI_Field0DoneFlag          = CSI_SR_DMA_FIELD0_DONE_MASK,        /*!< Field 0 transfer done. */
201     kCSI_Field1DoneFlag          = CSI_SR_DMA_FIELD1_DONE_MASK,        /*!< Field 1 transfer done. */
202     kCSI_BaseAddrChangeErrorFlag = CSI_SR_BASEADDR_CHHANGE_ERROR_MASK, /*!< The DMA output buffer base address
203                                                                                changes before DMA completed. */
204 };
205 
206 /* Forward declaration of the handle typedef. */
207 typedef struct _csi_handle csi_handle_t;
208 
209 /*!
210  * @brief CSI transfer callback function.
211  *
212  * When a new frame is received and saved to the frame buffer queue, the callback
213  * is called and the pass the status @ref kStatus_CSI_FrameDone to upper layer.
214  */
215 typedef void (*csi_transfer_callback_t)(CSI_Type *base, csi_handle_t *handle, status_t status, void *userData);
216 
217 typedef struct
218 {
219     uint32_t addr[CSI_DRIVER_QUEUE_SIZE];
220     int head;
221     int tail;
222 } buf_queue_t;
223 
224 /*!
225  * @brief CSI handle structure.
226  *
227  * Please see the user guide for the details of the CSI driver queue mechanism.
228  */
229 struct _csi_handle
230 {
231     buf_queue_t emptyBufferQueue;
232     buf_queue_t fullBufferQueue;
233 
234     volatile uint8_t activeBufferNum; /*!< How many frame buffers are in progress currently. */
235     volatile uint8_t dmaDoneBufferIdx; /*!< Index of the current full-filled framebuffer. */
236 
237     volatile bool transferStarted; /*!< User has called @ref CSI_TransferStart to start frame receiving. */
238 
239     csi_transfer_callback_t callback; /*!< Callback function. */
240     void *userData;                   /*!< CSI callback function parameter.*/
241 };
242 
243 #if CSI_DRIVER_FRAG_MODE
244 
245 /*! @brief Input pixel format when CSI works in fragment mode. */
246 typedef enum _csi_frag_input_pixel_format
247 {
248     kCSI_FragInputRGB565 = 0, /*!< Input pixel format is RGB565. */
249     kCSI_FragInputYUYV,       /*!< Input pixel format is YUV422 (Y-U-Y-V). */
250     kCSI_FragInputUYVY,       /*!< Input pixel format is YUV422 (U-Y-V-Y). */
251 } csi_frag_input_pixel_format_t;
252 
253 /*! @brief Configuration for CSI module to work in fragment mode. */
254 typedef struct _csi_frag_config
255 {
256     uint16_t width;           /*!< Pixels of the input frame. */
257     uint16_t height;          /*!< Lines of the input frame.  */
258     uint32_t polarityFlags;   /*!< Timing signal polarity flags, OR'ed value of @ref _csi_polarity_flags. */
259     csi_work_mode_t workMode; /*!< CSI work mode. */
260     csi_data_bus_t dataBus;   /*!< Data bus width. */
261     bool useExtVsync;         /*!< In CCIR656 progressive mode, set true to use external VSYNC signal, set false
262                                 to use internal VSYNC signal decoded from SOF. */
263     csi_frag_input_pixel_format_t inputFormat; /*!< Input pixel format. */
264 
265     uint32_t dmaBufferAddr0;  /*!< Buffer 0 used for CSI DMA, must be double word aligned. */
266     uint32_t dmaBufferAddr1;  /*!< Buffer 1 used for CSI DMA, must be double word aligned. */
267     uint16_t dmaBufferLine;   /*!< Lines of each DMA buffer. The size of DMA buffer 0 and
268                                    buffer 1 must be the same. Camera frame height must be
269                                    dividable by this value. */
270     bool isDmaBufferCachable; /*!< Is DMA buffer cachable or not. */
271 } csi_frag_config_t;
272 
273 /* Forward declaration of the handle typedef. */
274 typedef struct _csi_frag_handle csi_frag_handle_t;
275 
276 /*!
277  * @brief CSI fragment transfer callback function.
278  *
279  * When a new frame is received and saved to the frame buffer queue, the callback
280  * is called and the pass the status @ref kStatus_CSI_FrameDone to upper layer.
281  */
282 typedef void (*csi_frag_transfer_callback_t)(CSI_Type *base,
283                                              csi_frag_handle_t *handle,
284                                              status_t status,
285                                              void *userData);
286 
287 /*!
288  * @brief Function to copy data from CSI DMA buffer to user buffer.
289  */
290 typedef void (*csi_frag_copy_func_t)(void *pDest, const void *pSrc, size_t cnt);
291 
292 /*! @brief Handle for CSI module to work in fragment mode. */
293 struct _csi_frag_handle
294 {
295     uint16_t width;                            /*!< Pixels of the input frame. */
296     uint16_t height;                           /*!< Lines of the input frame.  */
297     uint16_t maxLinePerFrag;                   /*!< Max line saved per fragment. */
298     uint16_t linePerFrag;                      /*!< Actual line saved per fragment. */
299     uint16_t dmaBytePerLine;                   /*!< How many bytes DMA transfered each line. */
300     uint16_t datBytePerLine;                   /*!< How many bytes copied to user buffer each line. */
301     uint16_t dmaCurLine;                       /*!< Current line index in whole frame. */
302     uint16_t windowULX;                        /*!< X of windows upper left corner. */
303     uint16_t windowULY;                        /*!< Y of windows upper left corner. */
304     uint16_t windowLRX;                        /*!< X of windows lower right corner. */
305     uint16_t windowLRY;                        /*!< Y of windows lower right corner. */
306     uint32_t outputBuffer;                     /*!< Address of buffer to save the captured image. */
307     uint32_t datCurWriteAddr;                  /*!< Current write address to the user buffer. */
308     csi_frag_input_pixel_format_t inputFormat; /*!< Input pixel format. */
309 
310     csi_frag_transfer_callback_t callback; /*!< Callback function. */
311     void *userData;                        /*!< CSI callback function parameter.*/
312     csi_frag_copy_func_t copyFunc;         /*!< Function to copy data from CSI DMA buffer to user buffer. */
313     bool isDmaBufferCachable;              /*!< Is DMA buffer cachable or not. */
314 };
315 
316 /*! @brief Handle for CSI module to work in fragment mode. */
317 typedef struct _csi_frag_window
318 {
319     uint16_t windowULX; /*!< X of windows upper left corner. */
320     uint16_t windowULY; /*!< Y of windows upper left corner. */
321     uint16_t windowLRX; /*!< X of windows lower right corner. */
322     uint16_t windowLRY; /*!< Y of windows lower right corner. */
323 } csi_frag_window_t;
324 
325 /*! @brief Handle for CSI module to work in fragment mode. */
326 typedef struct _csi_frag_capture_config
327 {
328     bool outputGrayScale;      /*!< Output gray scale image or not, could only enable when input format is YUV. */
329     uint32_t buffer;           /*!< Buffer to save the captured image. */
330     csi_frag_window_t *window; /*!< Capture window. Capture full frame if set this to NULL. When output format is gray,
331                                     the window width must be multiple value of 8. */
332 } csi_frag_capture_config_t;
333 
334 #endif /* CSI_DRIVER_FRAG_MODE */
335 
336 /*******************************************************************************
337  * API
338  ******************************************************************************/
339 
340 #if defined(__cplusplus)
341 extern "C" {
342 #endif
343 
344 /*!
345  * @name Initialization and deinitialization
346  * @{
347  */
348 
349 /*!
350  * @brief Initialize the CSI.
351  *
352  * This function enables the CSI peripheral clock, and resets the CSI registers.
353  *
354  * @param base CSI peripheral base address.
355  * @param config Pointer to the configuration structure.
356  *
357  * @retval kStatus_Success Initialize successfully.
358  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
359  */
360 status_t CSI_Init(CSI_Type *base, const csi_config_t *config);
361 
362 /*!
363  * @brief De-initialize the CSI.
364  *
365  * This function disables the CSI peripheral clock.
366  *
367  * @param base CSI peripheral base address.
368  */
369 void CSI_Deinit(CSI_Type *base);
370 
371 /*!
372  * @brief Reset the CSI.
373  *
374  * This function resets the CSI peripheral registers to default status.
375  *
376  * @param base CSI peripheral base address.
377  */
378 void CSI_Reset(CSI_Type *base);
379 
380 /*!
381  * @brief Get the default configuration for to initialize the CSI.
382  *
383  * The default configuration value is:
384  *
385  * @code
386     config->width = 320U;
387     config->height = 240U;
388     config->polarityFlags = kCSI_HsyncActiveHigh | kCSI_DataLatchOnRisingEdge;
389     config->bytesPerPixel = 2U;
390     config->linePitch_Bytes = 320U * 2U;
391     config->workMode = kCSI_GatedClockMode;
392     config->dataBus = kCSI_DataBus8Bit;
393     config->useExtVsync = true;
394    @endcode
395  *
396  * @param config Pointer to the CSI configuration.
397  */
398 void CSI_GetDefaultConfig(csi_config_t *config);
399 
400 /*! @} */
401 
402 /*!
403  * @name Module operation
404  * @{
405  */
406 
407 /*!
408  * @brief Clear the CSI FIFO.
409  *
410  * This function clears the CSI FIFO.
411  *
412  * @param base CSI peripheral base address.
413  * @param fifo The FIFO to clear.
414  */
415 void CSI_ClearFifo(CSI_Type *base, csi_fifo_t fifo);
416 
417 /*!
418  * @brief Reflash the CSI FIFO DMA.
419  *
420  * This function reflashes the CSI FIFO DMA.
421  *
422  * For RXFIFO, there are two frame buffers. When the CSI module started, it saves
423  * the frames to frame buffer 0 then frame buffer 1, the two buffers will be
424  * written by turns. After reflash DMA using this function, the CSI is reset to
425  * save frame to buffer 0.
426  *
427  * @param base CSI peripheral base address.
428  * @param fifo The FIFO DMA to reflash.
429  */
430 void CSI_ReflashFifoDma(CSI_Type *base, csi_fifo_t fifo);
431 
432 /*!
433  * @brief Enable or disable the CSI FIFO DMA request.
434  *
435  * @param base CSI peripheral base address.
436  * @param fifo The FIFO DMA reques to enable or disable.
437  * @param enable True to enable, false to disable.
438  */
439 void CSI_EnableFifoDmaRequest(CSI_Type *base, csi_fifo_t fifo, bool enable);
440 
441 /*!
442  * @brief Start to receive data.
443  *
444  * @param base CSI peripheral base address.
445  */
CSI_Start(CSI_Type * base)446 static inline void CSI_Start(CSI_Type *base)
447 {
448     CSI_EnableFifoDmaRequest(base, kCSI_RxFifo, true);
449     CSI_REG_CR18(base) |= CSI_CR18_CSI_ENABLE_MASK;
450 }
451 
452 /*!
453  * @brief Stop to receiving data.
454  *
455  * @param base CSI peripheral base address.
456  */
CSI_Stop(CSI_Type * base)457 static inline void CSI_Stop(CSI_Type *base)
458 {
459     CSI_REG_CR18(base) &= ~CSI_CR18_CSI_ENABLE_MASK;
460     CSI_EnableFifoDmaRequest(base, kCSI_RxFifo, false);
461 }
462 
463 /*!
464  * @brief Set the RX frame buffer address.
465  *
466  * @param base CSI peripheral base address.
467  * @param index Buffer index.
468  * @param addr Frame buffer address to set.
469  */
470 void CSI_SetRxBufferAddr(CSI_Type *base, uint8_t index, uint32_t addr);
471 /*! @} */
472 
473 /*!
474  * @name Interrupts
475  * @{
476  */
477 
478 /*!
479  * @brief Enables CSI interrupt requests.
480  *
481  * @param base CSI peripheral base address.
482  * @param mask The interrupts to enable, pass in as OR'ed value of @ref _csi_interrupt_enable.
483  */
484 void CSI_EnableInterrupts(CSI_Type *base, uint32_t mask);
485 
486 /*!
487  * @brief Disable CSI interrupt requests.
488  *
489  * @param base CSI peripheral base address.
490  * @param mask The interrupts to disable, pass in as OR'ed value of @ref _csi_interrupt_enable.
491  */
492 void CSI_DisableInterrupts(CSI_Type *base, uint32_t mask);
493 
494 /*! @} */
495 
496 /*!
497  * @name Status
498  * @{
499  */
500 
501 /*!
502  * @brief Gets the CSI status flags.
503  *
504  * @param base CSI peripheral base address.
505  * @return status flag, it is OR'ed value of @ref _csi_flags.
506  */
CSI_GetStatusFlags(CSI_Type * base)507 static inline uint32_t CSI_GetStatusFlags(CSI_Type *base)
508 {
509     return CSI_REG_SR(base);
510 }
511 
512 /*!
513  * @brief Clears the CSI status flag.
514  *
515  * The flags to clear are passed in as OR'ed value of @ref _csi_flags. The following
516  * flags are cleared automatically by hardware:
517  *
518  * - @ref kCSI_RxFifoFullFlag,
519  * - @ref kCSI_StatFifoFullFlag,
520  * - @ref kCSI_Field0PresentFlag,
521  * - @ref kCSI_Field1PresentFlag,
522  * - @ref kCSI_RxFifoDataReadyFlag,
523  *
524  * @param base CSI peripheral base address.
525  * @param statusMask The status flags mask, OR'ed value of @ref _csi_flags.
526  */
CSI_ClearStatusFlags(CSI_Type * base,uint32_t statusMask)527 static inline void CSI_ClearStatusFlags(CSI_Type *base, uint32_t statusMask)
528 {
529     CSI_REG_SR(base) = statusMask;
530 }
531 /*! @} */
532 
533 #if !CSI_DRIVER_FRAG_MODE
534 /*!
535  * @name Transactional
536  * @{
537  */
538 
539 /*!
540  * @brief Initializes the CSI handle.
541  *
542  * This function initializes CSI handle, it should be called before any other
543  * CSI transactional functions.
544  *
545  * @param base CSI peripheral base address.
546  * @param handle Pointer to the handle structure.
547  * @param callback Callback function for CSI transfer.
548  * @param userData Callback function parameter.
549  *
550  * @retval kStatus_Success Handle created successfully.
551  */
552 status_t CSI_TransferCreateHandle(CSI_Type *base,
553                                   csi_handle_t *handle,
554                                   csi_transfer_callback_t callback,
555                                   void *userData);
556 
557 /*!
558  * @brief Start the transfer using transactional functions.
559  *
560  * When the empty frame buffers have been submit to CSI driver using function
561  * @ref CSI_TransferSubmitEmptyBuffer, user could call this function to start
562  * the transfer. The incoming frame will be saved to the empty frame buffer,
563  * and user could be optionally notified through callback function.
564  *
565  * @param base CSI peripheral base address.
566  * @param handle Pointer to the handle structure.
567  *
568  * @retval kStatus_Success Started successfully.
569  * @retval kStatus_CSI_NoEmptyBuffer Could not start because no empty frame buffer in queue.
570  */
571 status_t CSI_TransferStart(CSI_Type *base, csi_handle_t *handle);
572 
573 /*!
574  * @brief Stop the transfer using transactional functions.
575  *
576  * The driver does not clean the full frame buffers in queue. In other words, after
577  * calling this function, user still could get the full frame buffers in queue
578  * using function @ref CSI_TransferGetFullBuffer.
579  *
580  * @param base CSI peripheral base address.
581  * @param handle Pointer to the handle structure.
582  *
583  * @retval kStatus_Success Stoped successfully.
584  */
585 status_t CSI_TransferStop(CSI_Type *base, csi_handle_t *handle);
586 
587 /*!
588  * @brief Submit empty frame buffer to queue.
589  *
590  * This function could be called before @ref CSI_TransferStart or after @ref
591  * CSI_TransferStart. If there is no room in queue to store the empty frame
592  * buffer, this function returns error.
593  *
594  * @param base CSI peripheral base address.
595  * @param handle Pointer to the handle structure.
596  * @param frameBuffer Empty frame buffer to submit.
597  *
598  * @retval kStatus_Success Started successfully.
599  * @retval kStatus_CSI_QueueFull Could not submit because there is no room in queue.
600  */
601 status_t CSI_TransferSubmitEmptyBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t frameBuffer);
602 
603 /*!
604  * @brief Get one full frame buffer from queue.
605  *
606  * After the transfer started using function @ref CSI_TransferStart, the incoming
607  * frames will be saved to the empty frame buffers in queue. This function gets
608  * the full-filled frame buffer from the queue. If there is no full frame buffer
609  * in queue, this function returns error.
610  *
611  * @param base CSI peripheral base address.
612  * @param handle Pointer to the handle structure.
613  * @param frameBuffer Full frame buffer.
614  *
615  * @retval kStatus_Success Started successfully.
616  * @retval kStatus_CSI_NoFullBuffer There is no full frame buffer in queue.
617  */
618 status_t CSI_TransferGetFullBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t *frameBuffer);
619 
620 /*!
621  * @brief CSI IRQ handle function.
622  *
623  * This function handles the CSI IRQ request to work with CSI driver transactional
624  * APIs.
625  *
626  * @param base CSI peripheral base address.
627  * @param handle CSI handle pointer.
628  */
629 void CSI_TransferHandleIRQ(CSI_Type *base, csi_handle_t *handle);
630 /*! @} */
631 
632 #else
633 
634 /*!
635  * @name Fragment mode
636  * @{
637  */
638 
639 /*!
640  * @brief Initialize the CSI to work in fragment mode.
641  *
642  * This function enables the CSI peripheral clock, and resets the CSI registers.
643  *
644  * @param base CSI peripheral base address.
645  */
646 void CSI_FragModeInit(CSI_Type *base);
647 
648 /*!
649  * @brief De-initialize the CSI.
650  *
651  * This function disables the CSI peripheral clock.
652  *
653  * @param base CSI peripheral base address.
654  */
655 void CSI_FragModeDeinit(CSI_Type *base);
656 
657 /*!
658  * @brief Create handle for CSI work in fragment mode.
659  *
660  * @param base CSI peripheral base address.
661  * @param handle Pointer to the transactional handle.
662  * @param config Pointer to the configuration structure.
663  * @param callback Callback function for CSI transfer.
664  * @param userData Callback function parameter.
665  *
666  * @retval kStatus_Success Initialize successfully.
667  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
668  */
669 status_t CSI_FragModeCreateHandle(CSI_Type *base,
670                                   csi_frag_handle_t *handle,
671                                   const csi_frag_config_t *config,
672                                   csi_frag_transfer_callback_t callback,
673                                   void *userData);
674 
675 /*!
676  * @brief Start to capture a image.
677  *
678  * @param base CSI peripheral base address.
679  * @param handle Pointer to the transactional handle.
680  * @param config Pointer to the capture configuration.
681  *
682  * @retval kStatus_Success Initialize successfully.
683  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
684  */
685 status_t CSI_FragModeTransferCaptureImage(CSI_Type *base,
686                                           csi_frag_handle_t *handle,
687                                           const csi_frag_capture_config_t *config);
688 
689 /*!
690  * @brief Abort image capture.
691  *
692  * Abort image capture initialized by @ref CSI_FragModeTransferCaptureImage.
693  *
694  * @param base CSI peripheral base address.
695  * @param handle Pointer to the transactional handle.
696  */
697 void CSI_FragModeTransferAbortCaptureImage(CSI_Type *base, csi_frag_handle_t *handle);
698 
699 /*!
700  * @brief CSI IRQ handle function.
701  *
702  * This function handles the CSI IRQ request to work with CSI driver fragment mode
703  * APIs.
704  *
705  * @param base CSI peripheral base address.
706  * @param handle CSI handle pointer.
707  */
708 void CSI_FragModeTransferHandleIRQ(CSI_Type *base, csi_frag_handle_t *handle);
709 
710 /*! @} */
711 
712 #endif /* CSI_DRIVER_FRAG_MODE */
713 
714 #if defined(__cplusplus)
715 }
716 #endif
717 
718 /*! @}*/
719 
720 #endif /* FSL_CSI_H_ */
721