Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:58

0001 /*
0002  * Copyright 2017-2021 NXP
0003  * All rights reserved.
0004  *
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #ifndef _FSL_CSI_H_
0010 #define _FSL_CSI_H_
0011 
0012 #include "fsl_common.h"
0013 
0014 /*!
0015  * @addtogroup csi_driver
0016  * @{
0017  */
0018 
0019 /*******************************************************************************
0020  * Definitions
0021  ******************************************************************************/
0022 
0023 /*! @name Driver version */
0024 /*@{*/
0025 #define FSL_CSI_DRIVER_VERSION (MAKE_VERSION(2, 1, 5))
0026 /*@}*/
0027 
0028 #define CSI_REG_CR1(base)       (base)->CR1
0029 #define CSI_REG_CR2(base)       (base)->CR2
0030 #define CSI_REG_CR3(base)       (base)->CR3
0031 #define CSI_REG_CR18(base)      (base)->CR18
0032 #define CSI_REG_SR(base)        (base)->SR
0033 #define CSI_REG_DMASA_FB1(base) (base)->DMASA_FB1
0034 #define CSI_REG_DMASA_FB2(base) (base)->DMASA_FB2
0035 #define CSI_REG_IMAG_PARA(base) (base)->IMAG_PARA
0036 #define CSI_REG_FBUF_PARA(base) (base)->FBUF_PARA
0037 
0038 /*! @brief Size of the frame buffer queue used in CSI transactional function. */
0039 #ifndef CSI_DRIVER_QUEUE_SIZE
0040 #define CSI_DRIVER_QUEUE_SIZE 4U
0041 #endif
0042 
0043 /*! @brief Enable fragment capture function or not. */
0044 #ifndef CSI_DRIVER_FRAG_MODE
0045 #define CSI_DRIVER_FRAG_MODE 0U
0046 #endif
0047 
0048 /*
0049  * There is one empty room in queue, used to distinguish whether the queue
0050  * is full or empty. When header equals tail, the queue is empty; when header
0051  * equals tail + 1, the queue is full.
0052  */
0053 #define CSI_DRIVER_ACTUAL_QUEUE_SIZE (CSI_DRIVER_QUEUE_SIZE + 1U)
0054 
0055 /*
0056  * The queue max size is 254, so that the queue element index could use `uint8_t`.
0057  */
0058 #if (CSI_DRIVER_ACTUAL_QUEUE_SIZE > 254)
0059 #error Required queue size is too large
0060 #endif
0061 
0062 /*
0063  * The interrupt enable bits are in registers CSICR1[16:31], CSICR3[0:7],
0064  * and CSICR18[2:9]. So merge them into an uint32_t value, place CSICR18 control
0065  * bits to [8:15].
0066  */
0067 #define CSI_CR1_INT_EN_MASK  0xFFFF0000U
0068 #define CSI_CR3_INT_EN_MASK  0x000000FFU
0069 #define CSI_CR18_INT_EN_MASK 0x0000FF00U
0070 
0071 #if ((~CSI_CR1_INT_EN_MASK) &                                                                                   \
0072      (CSI_CR1_EOF_INT_EN_MASK | CSI_CR1_COF_INT_EN_MASK | CSI_CR1_SF_OR_INTEN_MASK | CSI_CR1_RF_OR_INTEN_MASK | \
0073       CSI_CR1_SFF_DMA_DONE_INTEN_MASK | CSI_CR1_STATFF_INTEN_MASK | CSI_CR1_FB2_DMA_DONE_INTEN_MASK |           \
0074       CSI_CR1_FB1_DMA_DONE_INTEN_MASK | CSI_CR1_RXFF_INTEN_MASK | CSI_CR1_SOF_INTEN_MASK))
0075 #error CSI_CR1_INT_EN_MASK could not cover all interrupt bits in CSICR1.
0076 #endif
0077 
0078 #if ((~CSI_CR3_INT_EN_MASK) & (CSI_CR3_ECC_INT_EN_MASK | CSI_CR3_HRESP_ERR_EN_MASK))
0079 #error CSI_CR3_INT_EN_MASK could not cover all interrupt bits in CSICR3.
0080 #endif
0081 
0082 #if ((~CSI_CR18_INT_EN_MASK) &                                                                                   \
0083      ((CSI_CR18_FIELD0_DONE_IE_MASK | CSI_CR18_DMA_FIELD1_DONE_IE_MASK | CSI_CR18_BASEADDR_CHANGE_ERROR_IE_MASK) \
0084       << 6U))
0085 #error CSI_CR18_INT_EN_MASK could not cover all interrupt bits in CSICR18.
0086 #endif
0087 
0088 /*! @brief Error codes for the CSI driver. */
0089 enum
0090 {
0091     kStatus_CSI_NoEmptyBuffer = MAKE_STATUS(kStatusGroup_CSI, 0), /*!< No empty frame buffer in queue to load to CSI. */
0092     kStatus_CSI_NoFullBuffer  = MAKE_STATUS(kStatusGroup_CSI, 1), /*!< No full frame buffer in queue to read out. */
0093     kStatus_CSI_QueueFull = MAKE_STATUS(kStatusGroup_CSI, 2), /*!< Queue is full, no room to save new empty buffer. */
0094     kStatus_CSI_FrameDone = MAKE_STATUS(kStatusGroup_CSI, 3), /*!< New frame received and saved to queue. */
0095 };
0096 
0097 /*!
0098  * @brief CSI work mode.
0099  *
0100  * The CCIR656 interlace mode is not supported currently.
0101  */
0102 typedef enum _csi_work_mode
0103 {
0104     kCSI_GatedClockMode         = CSI_CR1_GCLK_MODE(1U), /*!< HSYNC, VSYNC, and PIXCLK signals are used. */
0105     kCSI_NonGatedClockMode      = 0U,                    /*!< VSYNC, and PIXCLK signals are used. */
0106     kCSI_CCIR656ProgressiveMode = CSI_CR1_CCIR_EN(1U),   /*!< CCIR656 progressive mode. */
0107 } csi_work_mode_t;
0108 
0109 /*!
0110  * @brief CSI data bus witdh.
0111  */
0112 typedef enum _csi_data_bus
0113 {
0114     kCSI_DataBus8Bit,  /*!< 8-bit data bus. */
0115     kCSI_DataBus16Bit, /*!< 16-bit data bus. */
0116     kCSI_DataBus24Bit, /*!< 24-bit data bus. */
0117 } csi_data_bus_t;
0118 
0119 /*! @brief CSI signal polarity. */
0120 enum _csi_polarity_flags
0121 {
0122     kCSI_HsyncActiveLow         = 0U,                     /*!< HSYNC is active low. */
0123     kCSI_HsyncActiveHigh        = CSI_CR1_HSYNC_POL_MASK, /*!< HSYNC is active high. */
0124     kCSI_DataLatchOnRisingEdge  = CSI_CR1_REDGE_MASK,     /*!< Pixel data latched at rising edge of pixel clock. */
0125     kCSI_DataLatchOnFallingEdge = 0U,                     /*!< Pixel data latched at falling edge of pixel clock. */
0126     kCSI_VsyncActiveHigh        = 0U,                     /*!< VSYNC is active high. */
0127     kCSI_VsyncActiveLow         = CSI_CR1_SOF_POL_MASK,   /*!< VSYNC is active low. */
0128 };
0129 
0130 /*! @brief Configuration to initialize the CSI module. */
0131 typedef struct _csi_config
0132 {
0133     uint16_t width;           /*!< Pixels of the input frame. */
0134     uint16_t height;          /*!< Lines of the input frame.  */
0135     uint32_t polarityFlags;   /*!< Timing signal polarity flags, OR'ed value of @ref _csi_polarity_flags. */
0136     uint8_t bytesPerPixel;    /*!< Bytes per pixel, valid values are:
0137                                 - 2: Used for RGB565, YUV422, and so on.
0138                                 - 4: Used for XRGB8888, XYUV444, and so on.
0139                                 */
0140     uint16_t linePitch_Bytes; /*!< Frame buffer line pitch, must be 8-byte aligned. */
0141     csi_work_mode_t workMode; /*!< CSI work mode. */
0142     csi_data_bus_t dataBus;   /*!< Data bus width. */
0143     bool useExtVsync;         /*!< In CCIR656 progressive mode, set true to use external VSYNC signal, set false
0144                                 to use internal VSYNC signal decoded from SOF. */
0145 } csi_config_t;
0146 
0147 /*! @brief The CSI FIFO, used for FIFO operation. */
0148 typedef enum _csi_fifo
0149 {
0150     kCSI_RxFifo   = (1U << 0U),  /*!< RXFIFO. */
0151     kCSI_StatFifo = (1U << 1U),  /*!< STAT FIFO. */
0152     kCSI_AllFifo  = 0x01 | 0x02, /*!< Both RXFIFO and STAT FIFO. */
0153 } csi_fifo_t;
0154 
0155 /*! @brief CSI feature interrupt source. */
0156 enum _csi_interrupt_enable
0157 {
0158     kCSI_EndOfFrameInterruptEnable       = CSI_CR1_EOF_INT_EN_MASK,         /*!< End of frame interrupt enable. */
0159     kCSI_ChangeOfFieldInterruptEnable    = CSI_CR1_COF_INT_EN_MASK,         /*!< Change of field interrupt enable. */
0160     kCSI_StatFifoOverrunInterruptEnable  = CSI_CR1_SF_OR_INTEN_MASK,        /*!< STAT FIFO overrun interrupt enable. */
0161     kCSI_RxFifoOverrunInterruptEnable    = CSI_CR1_RF_OR_INTEN_MASK,        /*!< RXFIFO overrun interrupt enable. */
0162     kCSI_StatFifoDmaDoneInterruptEnable  = CSI_CR1_SFF_DMA_DONE_INTEN_MASK, /*!< STAT FIFO DMA done interrupt enable. */
0163     kCSI_StatFifoFullInterruptEnable     = CSI_CR1_STATFF_INTEN_MASK,       /*!< STAT FIFO full interrupt enable. */
0164     kCSI_RxBuffer1DmaDoneInterruptEnable = CSI_CR1_FB2_DMA_DONE_INTEN_MASK, /*!< RX frame buffer 1 DMA transfer done. */
0165     kCSI_RxBuffer0DmaDoneInterruptEnable = CSI_CR1_FB1_DMA_DONE_INTEN_MASK, /*!< RX frame buffer 0 DMA transfer done. */
0166     kCSI_RxFifoFullInterruptEnable       = CSI_CR1_RXFF_INTEN_MASK,         /*!< RXFIFO full interrupt enable. */
0167     kCSI_StartOfFrameInterruptEnable     = CSI_CR1_SOF_INTEN_MASK, /*!< Start of frame (SOF) interrupt enable. */
0168 
0169     kCSI_EccErrorInterruptEnable    = CSI_CR3_ECC_INT_EN_MASK,   /*!< ECC error detection interrupt enable. */
0170     kCSI_AhbResErrorInterruptEnable = CSI_CR3_HRESP_ERR_EN_MASK, /*!< AHB response Error interrupt enable. */
0171 
0172     /*! The DMA output buffer base address changes before DMA completed. */
0173     kCSI_BaseAddrChangeErrorInterruptEnable = CSI_CR18_BASEADDR_CHANGE_ERROR_IE_MASK << 6U,
0174 
0175     kCSI_Field0DoneInterruptEnable = CSI_CR18_FIELD0_DONE_IE_MASK << 6U,     /*!< Field 0 done interrupt enable. */
0176     kCSI_Field1DoneInterruptEnable = CSI_CR18_DMA_FIELD1_DONE_IE_MASK << 6U, /*!< Field 1 done interrupt enable. */
0177 };
0178 
0179 /*!
0180  * @brief CSI status flags.
0181  *
0182  * The following status register flags can be cleared:
0183  * - kCSI_EccErrorFlag
0184  * - kCSI_AhbResErrorFlag
0185  * - kCSI_ChangeOfFieldFlag
0186  * - kCSI_StartOfFrameFlag
0187  * - kCSI_EndOfFrameFlag
0188  * - kCSI_RxBuffer1DmaDoneFlag
0189  * - kCSI_RxBuffer0DmaDoneFlag
0190  * - kCSI_StatFifoDmaDoneFlag
0191  * - kCSI_StatFifoOverrunFlag
0192  * - kCSI_RxFifoOverrunFlag
0193  * - kCSI_Field0DoneFlag
0194  * - kCSI_Field1DoneFlag
0195  * - kCSI_BaseAddrChangeErrorFlag
0196  */
0197 enum _csi_flags
0198 {
0199     kCSI_RxFifoDataReadyFlag     = CSI_SR_DRDY_MASK,          /*!< RXFIFO data ready. */
0200     kCSI_EccErrorFlag            = CSI_SR_ECC_INT_MASK,       /*!< ECC error detected. */
0201     kCSI_AhbResErrorFlag         = CSI_SR_HRESP_ERR_INT_MASK, /*!< Hresponse (AHB bus response) Error. */
0202     kCSI_ChangeOfFieldFlag       = CSI_SR_COF_INT_MASK,       /*!< Change of field. */
0203     kCSI_Field0PresentFlag       = CSI_SR_F1_INT_MASK,        /*!< Field 0 present in CCIR mode. */
0204     kCSI_Field1PresentFlag       = CSI_SR_F2_INT_MASK,        /*!< Field 1 present in CCIR mode. */
0205     kCSI_StartOfFrameFlag        = CSI_SR_SOF_INT_MASK,       /*!< Start of frame (SOF) detected. */
0206     kCSI_EndOfFrameFlag          = CSI_SR_EOF_INT_MASK,       /*!< End of frame (EOF) detected. */
0207     kCSI_RxFifoFullFlag          = CSI_SR_RxFF_INT_MASK, /*!< RXFIFO full (Number of data reaches trigger level). */
0208     kCSI_RxBuffer1DmaDoneFlag    = CSI_SR_DMA_TSF_DONE_FB2_MASK,       /*!< RX frame buffer 1 DMA transfer done. */
0209     kCSI_RxBuffer0DmaDoneFlag    = CSI_SR_DMA_TSF_DONE_FB1_MASK,       /*!< RX frame buffer 0 DMA transfer done. */
0210     kCSI_StatFifoFullFlag        = CSI_SR_STATFF_INT_MASK,             /*!< STAT FIFO full (Reach trigger level). */
0211     kCSI_StatFifoDmaDoneFlag     = CSI_SR_DMA_TSF_DONE_SFF_MASK,       /*!< STAT FIFO DMA transfer done. */
0212     kCSI_StatFifoOverrunFlag     = CSI_SR_SF_OR_INT_MASK,              /*!< STAT FIFO overrun. */
0213     kCSI_RxFifoOverrunFlag       = CSI_SR_RF_OR_INT_MASK,              /*!< RXFIFO overrun. */
0214     kCSI_Field0DoneFlag          = CSI_SR_DMA_FIELD0_DONE_MASK,        /*!< Field 0 transfer done. */
0215     kCSI_Field1DoneFlag          = CSI_SR_DMA_FIELD1_DONE_MASK,        /*!< Field 1 transfer done. */
0216     kCSI_BaseAddrChangeErrorFlag = CSI_SR_BASEADDR_CHHANGE_ERROR_MASK, /*!< The DMA output buffer base address
0217                                                                                changes before DMA completed. */
0218 };
0219 
0220 /* Forward declaration of the handle typedef. */
0221 typedef struct _csi_handle csi_handle_t;
0222 
0223 /*!
0224  * @brief CSI transfer callback function.
0225  *
0226  * When a new frame is received and saved to the frame buffer queue, the callback
0227  * is called and the pass the status @ref kStatus_CSI_FrameDone to upper layer.
0228  */
0229 typedef void (*csi_transfer_callback_t)(CSI_Type *base, csi_handle_t *handle, status_t status, void *userData);
0230 
0231 /*!
0232  * @brief CSI handle structure.
0233  *
0234  * Please see the user guide for the details of the CSI driver queue mechanism.
0235  */
0236 struct _csi_handle
0237 {
0238     uint32_t frameBufferQueue[CSI_DRIVER_ACTUAL_QUEUE_SIZE]; /*!< Frame buffer queue. */
0239 
0240     volatile uint8_t queueWriteIdx;  /*!< Pointer to save incoming item. */
0241     volatile uint8_t queueReadIdx;   /*!< Pointer to read out the item. */
0242     void *volatile emptyBuffer;      /*!< Pointer to maintain the empty frame buffers. */
0243     volatile uint8_t emptyBufferCnt; /*!< Empty frame buffers count. */
0244 
0245     volatile uint8_t activeBufferNum; /*!< How many frame buffers are in progres currently. */
0246 
0247     volatile bool transferStarted; /*!< User has called @ref CSI_TransferStart to start frame receiving. */
0248 
0249     csi_transfer_callback_t callback; /*!< Callback function. */
0250     void *userData;                   /*!< CSI callback function parameter.*/
0251 };
0252 
0253 #if CSI_DRIVER_FRAG_MODE
0254 
0255 /*! @brief Input pixel format when CSI works in fragment mode. */
0256 typedef enum _csi_frag_input_pixel_format
0257 {
0258     kCSI_FragInputRGB565 = 0, /*!< Input pixel format is RGB565. */
0259     kCSI_FragInputYUYV,       /*!< Input pixel format is YUV422 (Y-U-Y-V). */
0260     kCSI_FragInputUYVY,       /*!< Input pixel format is YUV422 (U-Y-V-Y). */
0261 } csi_frag_input_pixel_format_t;
0262 
0263 /*! @brief Configuration for CSI module to work in fragment mode. */
0264 typedef struct _csi_frag_config
0265 {
0266     uint16_t width;           /*!< Pixels of the input frame. */
0267     uint16_t height;          /*!< Lines of the input frame.  */
0268     uint32_t polarityFlags;   /*!< Timing signal polarity flags, OR'ed value of @ref _csi_polarity_flags. */
0269     csi_work_mode_t workMode; /*!< CSI work mode. */
0270     csi_data_bus_t dataBus;   /*!< Data bus width. */
0271     bool useExtVsync;         /*!< In CCIR656 progressive mode, set true to use external VSYNC signal, set false
0272                                 to use internal VSYNC signal decoded from SOF. */
0273     csi_frag_input_pixel_format_t inputFormat; /*!< Input pixel format. */
0274 
0275     uint32_t dmaBufferAddr0;  /*!< Buffer 0 used for CSI DMA, must be double word aligned. */
0276     uint32_t dmaBufferAddr1;  /*!< Buffer 1 used for CSI DMA, must be double word aligned. */
0277     uint16_t dmaBufferLine;   /*!< Lines of each DMA buffer. The size of DMA buffer 0 and
0278                                    buffer 1 must be the same. Camera frame height must be
0279                                    dividable by this value. */
0280     bool isDmaBufferCachable; /*!< Is DMA buffer cachable or not. */
0281 } csi_frag_config_t;
0282 
0283 /* Forward declaration of the handle typedef. */
0284 typedef struct _csi_frag_handle csi_frag_handle_t;
0285 
0286 /*!
0287  * @brief CSI fragment transfer callback function.
0288  *
0289  * When a new frame is received and saved to the frame buffer queue, the callback
0290  * is called and the pass the status @ref kStatus_CSI_FrameDone to upper layer.
0291  */
0292 typedef void (*csi_frag_transfer_callback_t)(CSI_Type *base,
0293                                              csi_frag_handle_t *handle,
0294                                              status_t status,
0295                                              void *userData);
0296 
0297 /*!
0298  * @brief Function to copy data from CSI DMA buffer to user buffer.
0299  */
0300 typedef void (*csi_frag_copy_func_t)(void *pDest, const void *pSrc, size_t cnt);
0301 
0302 /*! @brief Handle for CSI module to work in fragment mode. */
0303 struct _csi_frag_handle
0304 {
0305     uint16_t width;                            /*!< Pixels of the input frame. */
0306     uint16_t height;                           /*!< Lines of the input frame.  */
0307     uint16_t maxLinePerFrag;                   /*!< Max line saved per fragment. */
0308     uint16_t linePerFrag;                      /*!< Actual line saved per fragment. */
0309     uint16_t dmaBytePerLine;                   /*!< How many bytes DMA transfered each line. */
0310     uint16_t datBytePerLine;                   /*!< How many bytes copied to user buffer each line. */
0311     uint16_t dmaCurLine;                       /*!< Current line index in whole frame. */
0312     uint16_t windowULX;                        /*!< X of windows upper left corner. */
0313     uint16_t windowULY;                        /*!< Y of windows upper left corner. */
0314     uint16_t windowLRX;                        /*!< X of windows lower right corner. */
0315     uint16_t windowLRY;                        /*!< Y of windows lower right corner. */
0316     uint32_t outputBuffer;                     /*!< Address of buffer to save the captured image. */
0317     uint32_t datCurWriteAddr;                  /*!< Current write address to the user buffer. */
0318     csi_frag_input_pixel_format_t inputFormat; /*!< Input pixel format. */
0319 
0320     csi_frag_transfer_callback_t callback; /*!< Callback function. */
0321     void *userData;                        /*!< CSI callback function parameter.*/
0322     csi_frag_copy_func_t copyFunc;         /*!< Function to copy data from CSI DMA buffer to user buffer. */
0323     bool isDmaBufferCachable;              /*!< Is DMA buffer cachable or not. */
0324 };
0325 
0326 /*! @brief Handle for CSI module to work in fragment mode. */
0327 typedef struct _csi_frag_window
0328 {
0329     uint16_t windowULX; /*!< X of windows upper left corner. */
0330     uint16_t windowULY; /*!< Y of windows upper left corner. */
0331     uint16_t windowLRX; /*!< X of windows lower right corner. */
0332     uint16_t windowLRY; /*!< Y of windows lower right corner. */
0333 } csi_frag_window_t;
0334 
0335 /*! @brief Handle for CSI module to work in fragment mode. */
0336 typedef struct _csi_frag_capture_config
0337 {
0338     bool outputGrayScale;      /*!< Output gray scale image or not, could only enable when input format is YUV. */
0339     uint32_t buffer;           /*!< Buffer to save the captured image. */
0340     csi_frag_window_t *window; /*!< Capture window. Capture full frame if set this to NULL. When output format is gray,
0341                                     the window width must be multiple value of 8. */
0342 } csi_frag_capture_config_t;
0343 
0344 #endif /* CSI_DRIVER_FRAG_MODE */
0345 
0346 /*******************************************************************************
0347  * API
0348  ******************************************************************************/
0349 
0350 #if defined(__cplusplus)
0351 extern "C" {
0352 #endif
0353 
0354 /*!
0355  * @name Initialization and deinitialization
0356  * @{
0357  */
0358 
0359 /*!
0360  * @brief Initialize the CSI.
0361  *
0362  * This function enables the CSI peripheral clock, and resets the CSI registers.
0363  *
0364  * @param base CSI peripheral base address.
0365  * @param config Pointer to the configuration structure.
0366  *
0367  * @retval kStatus_Success Initialize successfully.
0368  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
0369  */
0370 status_t CSI_Init(CSI_Type *base, const csi_config_t *config);
0371 
0372 /*!
0373  * @brief De-initialize the CSI.
0374  *
0375  * This function disables the CSI peripheral clock.
0376  *
0377  * @param base CSI peripheral base address.
0378  */
0379 void CSI_Deinit(CSI_Type *base);
0380 
0381 /*!
0382  * @brief Reset the CSI.
0383  *
0384  * This function resets the CSI peripheral registers to default status.
0385  *
0386  * @param base CSI peripheral base address.
0387  */
0388 void CSI_Reset(CSI_Type *base);
0389 
0390 /*!
0391  * @brief Get the default configuration for to initialize the CSI.
0392  *
0393  * The default configuration value is:
0394  *
0395  * @code
0396     config->width = 320U;
0397     config->height = 240U;
0398     config->polarityFlags = kCSI_HsyncActiveHigh | kCSI_DataLatchOnRisingEdge;
0399     config->bytesPerPixel = 2U;
0400     config->linePitch_Bytes = 320U * 2U;
0401     config->workMode = kCSI_GatedClockMode;
0402     config->dataBus = kCSI_DataBus8Bit;
0403     config->useExtVsync = true;
0404    @endcode
0405  *
0406  * @param config Pointer to the CSI configuration.
0407  */
0408 void CSI_GetDefaultConfig(csi_config_t *config);
0409 
0410 /* @} */
0411 
0412 /*!
0413  * @name Module operation
0414  * @{
0415  */
0416 
0417 /*!
0418  * @brief Clear the CSI FIFO.
0419  *
0420  * This function clears the CSI FIFO.
0421  *
0422  * @param base CSI peripheral base address.
0423  * @param fifo The FIFO to clear.
0424  */
0425 void CSI_ClearFifo(CSI_Type *base, csi_fifo_t fifo);
0426 
0427 /*!
0428  * @brief Reflash the CSI FIFO DMA.
0429  *
0430  * This function reflashes the CSI FIFO DMA.
0431  *
0432  * For RXFIFO, there are two frame buffers. When the CSI module started, it saves
0433  * the frames to frame buffer 0 then frame buffer 1, the two buffers will be
0434  * written by turns. After reflash DMA using this function, the CSI is reset to
0435  * save frame to buffer 0.
0436  *
0437  * @param base CSI peripheral base address.
0438  * @param fifo The FIFO DMA to reflash.
0439  */
0440 void CSI_ReflashFifoDma(CSI_Type *base, csi_fifo_t fifo);
0441 
0442 /*!
0443  * @brief Enable or disable the CSI FIFO DMA request.
0444  *
0445  * @param base CSI peripheral base address.
0446  * @param fifo The FIFO DMA reques to enable or disable.
0447  * @param enable True to enable, false to disable.
0448  */
0449 void CSI_EnableFifoDmaRequest(CSI_Type *base, csi_fifo_t fifo, bool enable);
0450 
0451 /*!
0452  * @brief Start to receive data.
0453  *
0454  * @param base CSI peripheral base address.
0455  */
0456 static inline void CSI_Start(CSI_Type *base)
0457 {
0458     CSI_EnableFifoDmaRequest(base, kCSI_RxFifo, true);
0459     CSI_REG_CR18(base) |= CSI_CR18_CSI_ENABLE_MASK;
0460 }
0461 
0462 /*!
0463  * @brief Stop to receiving data.
0464  *
0465  * @param base CSI peripheral base address.
0466  */
0467 static inline void CSI_Stop(CSI_Type *base)
0468 {
0469     CSI_REG_CR18(base) &= ~CSI_CR18_CSI_ENABLE_MASK;
0470     CSI_EnableFifoDmaRequest(base, kCSI_RxFifo, false);
0471 }
0472 
0473 /*!
0474  * @brief Set the RX frame buffer address.
0475  *
0476  * @param base CSI peripheral base address.
0477  * @param index Buffer index.
0478  * @param addr Frame buffer address to set.
0479  */
0480 void CSI_SetRxBufferAddr(CSI_Type *base, uint8_t index, uint32_t addr);
0481 /* @} */
0482 
0483 /*!
0484  * @name Interrupts
0485  * @{
0486  */
0487 
0488 /*!
0489  * @brief Enables CSI interrupt requests.
0490  *
0491  * @param base CSI peripheral base address.
0492  * @param mask The interrupts to enable, pass in as OR'ed value of @ref _csi_interrupt_enable.
0493  */
0494 void CSI_EnableInterrupts(CSI_Type *base, uint32_t mask);
0495 
0496 /*!
0497  * @brief Disable CSI interrupt requests.
0498  *
0499  * @param base CSI peripheral base address.
0500  * @param mask The interrupts to disable, pass in as OR'ed value of @ref _csi_interrupt_enable.
0501  */
0502 void CSI_DisableInterrupts(CSI_Type *base, uint32_t mask);
0503 
0504 /* @} */
0505 
0506 /*!
0507  * @name Status
0508  * @{
0509  */
0510 
0511 /*!
0512  * @brief Gets the CSI status flags.
0513  *
0514  * @param base CSI peripheral base address.
0515  * @return status flag, it is OR'ed value of @ref _csi_flags.
0516  */
0517 static inline uint32_t CSI_GetStatusFlags(CSI_Type *base)
0518 {
0519     return CSI_REG_SR(base);
0520 }
0521 
0522 /*!
0523  * @brief Clears the CSI status flag.
0524  *
0525  * The flags to clear are passed in as OR'ed value of @ref _csi_flags. The following
0526  * flags are cleared automatically by hardware:
0527  *
0528  * - @ref kCSI_RxFifoFullFlag,
0529  * - @ref kCSI_StatFifoFullFlag,
0530  * - @ref kCSI_Field0PresentFlag,
0531  * - @ref kCSI_Field1PresentFlag,
0532  * - @ref kCSI_RxFifoDataReadyFlag,
0533  *
0534  * @param base CSI peripheral base address.
0535  * @param statusMask The status flags mask, OR'ed value of @ref _csi_flags.
0536  */
0537 static inline void CSI_ClearStatusFlags(CSI_Type *base, uint32_t statusMask)
0538 {
0539     CSI_REG_SR(base) = statusMask;
0540 }
0541 /* @} */
0542 
0543 #if !CSI_DRIVER_FRAG_MODE
0544 /*!
0545  * @name Transactional
0546  * @{
0547  */
0548 
0549 /*!
0550  * @brief Initializes the CSI handle.
0551  *
0552  * This function initializes CSI handle, it should be called before any other
0553  * CSI transactional functions.
0554  *
0555  * @param base CSI peripheral base address.
0556  * @param handle Pointer to the handle structure.
0557  * @param callback Callback function for CSI transfer.
0558  * @param userData Callback function parameter.
0559  *
0560  * @retval kStatus_Success Handle created successfully.
0561  */
0562 status_t CSI_TransferCreateHandle(CSI_Type *base,
0563                                   csi_handle_t *handle,
0564                                   csi_transfer_callback_t callback,
0565                                   void *userData);
0566 
0567 /*!
0568  * @brief Start the transfer using transactional functions.
0569  *
0570  * When the empty frame buffers have been submit to CSI driver using function
0571  * @ref CSI_TransferSubmitEmptyBuffer, user could call this function to start
0572  * the transfer. The incoming frame will be saved to the empty frame buffer,
0573  * and user could be optionally notified through callback function.
0574  *
0575  * @param base CSI peripheral base address.
0576  * @param handle Pointer to the handle structure.
0577  *
0578  * @retval kStatus_Success Started successfully.
0579  * @retval kStatus_CSI_NoEmptyBuffer Could not start because no empty frame buffer in queue.
0580  */
0581 status_t CSI_TransferStart(CSI_Type *base, csi_handle_t *handle);
0582 
0583 /*!
0584  * @brief Stop the transfer using transactional functions.
0585  *
0586  * The driver does not clean the full frame buffers in queue. In other words, after
0587  * calling this function, user still could get the full frame buffers in queue
0588  * using function @ref CSI_TransferGetFullBuffer.
0589  *
0590  * @param base CSI peripheral base address.
0591  * @param handle Pointer to the handle structure.
0592  *
0593  * @retval kStatus_Success Stoped successfully.
0594  */
0595 status_t CSI_TransferStop(CSI_Type *base, csi_handle_t *handle);
0596 
0597 /*!
0598  * @brief Submit empty frame buffer to queue.
0599  *
0600  * This function could be called before @ref CSI_TransferStart or after @ref
0601  * CSI_TransferStart. If there is no room in queue to store the empty frame
0602  * buffer, this function returns error.
0603  *
0604  * @param base CSI peripheral base address.
0605  * @param handle Pointer to the handle structure.
0606  * @param frameBuffer Empty frame buffer to submit.
0607  *
0608  * @retval kStatus_Success Started successfully.
0609  * @retval kStatus_CSI_QueueFull Could not submit because there is no room in queue.
0610  */
0611 status_t CSI_TransferSubmitEmptyBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t frameBuffer);
0612 
0613 /*!
0614  * @brief Get one full frame buffer from queue.
0615  *
0616  * After the transfer started using function @ref CSI_TransferStart, the incoming
0617  * frames will be saved to the empty frame buffers in queue. This function gets
0618  * the full-filled frame buffer from the queue. If there is no full frame buffer
0619  * in queue, this function returns error.
0620  *
0621  * @param base CSI peripheral base address.
0622  * @param handle Pointer to the handle structure.
0623  * @param frameBuffer Full frame buffer.
0624  *
0625  * @retval kStatus_Success Started successfully.
0626  * @retval kStatus_CSI_NoFullBuffer There is no full frame buffer in queue.
0627  */
0628 status_t CSI_TransferGetFullBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t *frameBuffer);
0629 
0630 /*!
0631  * @brief CSI IRQ handle function.
0632  *
0633  * This function handles the CSI IRQ request to work with CSI driver transactional
0634  * APIs.
0635  *
0636  * @param base CSI peripheral base address.
0637  * @param handle CSI handle pointer.
0638  */
0639 void CSI_TransferHandleIRQ(CSI_Type *base, csi_handle_t *handle);
0640 /* @} */
0641 
0642 #else
0643 
0644 /*!
0645  * @name Fragment mode
0646  * @{
0647  */
0648 
0649 /*!
0650  * @brief Initialize the CSI to work in fragment mode.
0651  *
0652  * This function enables the CSI peripheral clock, and resets the CSI registers.
0653  *
0654  * @param base CSI peripheral base address.
0655  */
0656 void CSI_FragModeInit(CSI_Type *base);
0657 
0658 /*!
0659  * @brief De-initialize the CSI.
0660  *
0661  * This function disables the CSI peripheral clock.
0662  *
0663  * @param base CSI peripheral base address.
0664  */
0665 void CSI_FragModeDeinit(CSI_Type *base);
0666 
0667 /*!
0668  * @brief Create handle for CSI work in fragment mode.
0669  *
0670  * @param base CSI peripheral base address.
0671  * @param handle Pointer to the transactional handle.
0672  * @param config Pointer to the configuration structure.
0673  * @param callback Callback function for CSI transfer.
0674  * @param userData Callback function parameter.
0675  *
0676  * @retval kStatus_Success Initialize successfully.
0677  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
0678  */
0679 status_t CSI_FragModeCreateHandle(CSI_Type *base,
0680                                   csi_frag_handle_t *handle,
0681                                   const csi_frag_config_t *config,
0682                                   csi_frag_transfer_callback_t callback,
0683                                   void *userData);
0684 
0685 /*!
0686  * @brief Start to capture a image.
0687  *
0688  * @param base CSI peripheral base address.
0689  * @param handle Pointer to the transactional handle.
0690  * @param config Pointer to the capture configuration.
0691  *
0692  * @retval kStatus_Success Initialize successfully.
0693  * @retval kStatus_InvalidArgument Initialize failed because of invalid argument.
0694  */
0695 status_t CSI_FragModeTransferCaptureImage(CSI_Type *base,
0696                                           csi_frag_handle_t *handle,
0697                                           const csi_frag_capture_config_t *config);
0698 
0699 /*!
0700  * @brief Abort image capture.
0701  *
0702  * Abort image capture initialized by @ref CSI_FragModeTransferCaptureImage.
0703  *
0704  * @param base CSI peripheral base address.
0705  * @param handle Pointer to the transactional handle.
0706  */
0707 void CSI_FragModeTransferAbortCaptureImage(CSI_Type *base, csi_frag_handle_t *handle);
0708 
0709 /*!
0710  * @brief CSI IRQ handle function.
0711  *
0712  * This function handles the CSI IRQ request to work with CSI driver fragment mode
0713  * APIs.
0714  *
0715  * @param base CSI peripheral base address.
0716  * @param handle CSI handle pointer.
0717  */
0718 void CSI_FragModeTransferHandleIRQ(CSI_Type *base, csi_frag_handle_t *handle);
0719 
0720 /* @} */
0721 
0722 #endif /* CSI_DRIVER_FRAG_MODE */
0723 
0724 #if defined(__cplusplus)
0725 }
0726 #endif
0727 
0728 /*! @}*/
0729 
0730 #endif /* _FSL_CSI_H_ */