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