1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of the copyright holder nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef _FSL_I2C_DMA_H_
31 #define _FSL_I2C_DMA_H_
32 
33 #include "fsl_i2c.h"
34 #include "fsl_dmamux.h"
35 #include "fsl_dma.h"
36 
37 /*!
38  * @addtogroup i2c_dma_driver
39  * @{
40  */
41 
42 
43 /*******************************************************************************
44  * Definitions
45  ******************************************************************************/
46 
47 /*! @brief I2C master DMA handle typedef. */
48 typedef struct _i2c_master_dma_handle i2c_master_dma_handle_t;
49 
50 /*! @brief I2C master DMA transfer callback typedef. */
51 typedef void (*i2c_master_dma_transfer_callback_t)(I2C_Type *base,
52                                                    i2c_master_dma_handle_t *handle,
53                                                    status_t status,
54                                                    void *userData);
55 
56 /*! @brief I2C master DMA transfer structure. */
57 struct _i2c_master_dma_handle
58 {
59     i2c_master_transfer_t transfer;                        /*!< I2C master transfer struct. */
60     size_t transferSize;                                   /*!< Total bytes to be transferred. */
61     uint8_t state;                                         /*!< I2C master transfer status. */
62     dma_handle_t *dmaHandle;                               /*!< The DMA handler used. */
63     i2c_master_dma_transfer_callback_t completionCallback; /*!< A callback function called after the DMA transfer finished. */
64     void *userData;                                        /*!< A callback parameter passed to the callback function. */
65 };
66 
67 /*******************************************************************************
68  * API
69  ******************************************************************************/
70 
71 #if defined(__cplusplus)
72 extern "C" {
73 #endif /*_cplusplus. */
74 
75 /*!
76  * @name I2C Block DMA Transfer Operation
77  * @{
78  */
79 
80 /*!
81  * @brief Initializes the I2C handle which is used in transcational functions.
82  *
83  * @param base I2C peripheral base address
84  * @param handle Pointer to the i2c_master_dma_handle_t structure
85  * @param callback Pointer to the user callback function
86  * @param userData A user parameter passed to the callback function
87  * @param dmaHandle DMA handle pointer
88  */
89 void I2C_MasterTransferCreateHandleDMA(I2C_Type *base,
90                                        i2c_master_dma_handle_t *handle,
91                                        i2c_master_dma_transfer_callback_t callback,
92                                        void *userData,
93                                        dma_handle_t *dmaHandle);
94 
95 /*!
96  * @brief Performs a master DMA non-blocking transfer on the I2C bus.
97  *
98  * @param base I2C peripheral base address
99  * @param handle A pointer to the i2c_master_dma_handle_t structure
100  * @param xfer A pointer to the transfer structure of the i2c_master_transfer_t
101  * @retval kStatus_Success Sucessfully completes the data transmission.
102  * @retval kStatus_I2C_Busy A previous transmission is still not finished.
103  * @retval kStatus_I2C_Timeout A transfer error, waits for the signal timeout.
104  * @retval kStatus_I2C_ArbitrationLost A transfer error, arbitration lost.
105  * @retval kStataus_I2C_Nak A transfer error, receives NAK during transfer.
106  */
107 status_t I2C_MasterTransferDMA(I2C_Type *base, i2c_master_dma_handle_t *handle, i2c_master_transfer_t *xfer);
108 
109 /*!
110  * @brief Gets a master transfer status during a DMA non-blocking transfer.
111  *
112  * @param base I2C peripheral base address
113  * @param handle A pointer to the i2c_master_dma_handle_t structure
114  * @param count A number of bytes transferred so far by the non-blocking transaction.
115  */
116 status_t I2C_MasterTransferGetCountDMA(I2C_Type *base, i2c_master_dma_handle_t *handle, size_t *count);
117 
118 /*!
119  * @brief Aborts a master DMA non-blocking transfer early.
120  *
121  * @param base I2C peripheral base address
122  * @param handle A pointer to the i2c_master_dma_handle_t structure.
123  */
124 void I2C_MasterTransferAbortDMA(I2C_Type *base, i2c_master_dma_handle_t *handle);
125 
126 /* @} */
127 #if defined(__cplusplus)
128 }
129 #endif /*_cplusplus. */
130 /*@}*/
131 #endif /*_FSL_I2C_DMA_H_*/
132