1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2022 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef FSL_I2C_DMA_H_
9 #define FSL_I2C_DMA_H_
10 
11 #include "fsl_i2c.h"
12 #include "fsl_dma.h"
13 
14 /*!
15  * @addtogroup i2c_dma_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 /*! @brief I2C DMA driver version. */
26 #define FSL_I2C_DMA_DRIVER_VERSION (MAKE_VERSION(2, 3, 1))
27 /*! @} */
28 
29 /*! @brief Maximum lenght of single DMA transfer (determined by capability of the DMA engine) */
30 #define I2C_MAX_DMA_TRANSFER_COUNT 1024
31 
32 /*! @brief I2C master dma handle typedef. */
33 typedef struct _i2c_master_dma_handle i2c_master_dma_handle_t;
34 
35 /*! @brief I2C master dma transfer callback typedef. */
36 typedef void (*i2c_master_dma_transfer_callback_t)(I2C_Type *base,
37                                                    i2c_master_dma_handle_t *handle,
38                                                    status_t status,
39                                                    void *userData);
40 
41 /*! @brief Typedef for master dma handler. */
42 typedef void (*flexcomm_i2c_dma_master_irq_handler_t)(I2C_Type *base, i2c_master_dma_handle_t *handle);
43 
44 /*! @brief I2C master dma transfer structure. */
45 struct _i2c_master_dma_handle
46 {
47     uint8_t state;              /*!< Transfer state machine current state. */
48     uint32_t transferCount;     /*!< Indicates progress of the transfer */
49     uint32_t remainingBytesDMA; /*!< Remaining byte count to be transferred using DMA. */
50     uint8_t *buf;               /*!< Buffer pointer for current state. */
51     uint32_t remainingSubaddr;
52     uint8_t subaddrBuf[4];
53     bool checkAddrNack;             /*!< Whether to check the nack signal is detected during addressing. */
54     dma_handle_t *dmaHandle;        /*!< The DMA handler used. */
55     i2c_master_transfer_t transfer; /*!< Copy of the current transfer info. */
56     i2c_master_dma_transfer_callback_t completionCallback; /*!< Callback function called after dma transfer finished. */
57     void *userData;                                        /*!< Callback parameter passed to callback function. */
58 };
59 
60 /*******************************************************************************
61  * API
62  ******************************************************************************/
63 
64 #if defined(__cplusplus)
65 extern "C" {
66 #endif /*_cplusplus. */
67 
68 /*!
69  * @name I2C Block DMA Transfer Operation
70  * @{
71  */
72 
73 /*!
74  * @brief Init the I2C handle which is used in transactional functions
75  *
76  * @param base I2C peripheral base address
77  * @param handle pointer to i2c_master_dma_handle_t structure
78  * @param callback pointer to user callback function
79  * @param userData user param passed to the callback function
80  * @param dmaHandle DMA handle pointer
81  */
82 void I2C_MasterTransferCreateHandleDMA(I2C_Type *base,
83                                        i2c_master_dma_handle_t *handle,
84                                        i2c_master_dma_transfer_callback_t callback,
85                                        void *userData,
86                                        dma_handle_t *dmaHandle);
87 
88 /*!
89  * @brief Performs a master dma non-blocking transfer on the I2C bus
90  *
91  * @param base I2C peripheral base address
92  * @param handle pointer to i2c_master_dma_handle_t structure
93  * @param xfer pointer to transfer structure of i2c_master_transfer_t
94  * @retval kStatus_Success Sucessully complete the data transmission.
95  * @retval kStatus_I2C_Busy Previous transmission still not finished.
96  * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
97  * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
98  * @retval kStataus_I2C_Nak Transfer error, receive Nak during transfer.
99  */
100 status_t I2C_MasterTransferDMA(I2C_Type *base, i2c_master_dma_handle_t *handle, i2c_master_transfer_t *xfer);
101 
102 /*!
103  * @brief Get master transfer status during a dma non-blocking transfer
104  *
105  * @param base I2C peripheral base address
106  * @param handle pointer to i2c_master_dma_handle_t structure
107  * @param count Number of bytes transferred so far by the non-blocking transaction.
108  */
109 status_t I2C_MasterTransferGetCountDMA(I2C_Type *base, i2c_master_dma_handle_t *handle, size_t *count);
110 
111 /*!
112  * @brief Abort a master dma non-blocking transfer in a early time
113  *
114  * @param base I2C peripheral base address
115  * @param handle pointer to i2c_master_dma_handle_t structure
116  */
117 void I2C_MasterTransferAbortDMA(I2C_Type *base, i2c_master_dma_handle_t *handle);
118 
119 /*! @} */
120 #if defined(__cplusplus)
121 }
122 #endif /*_cplusplus. */
123 /*! @} */
124 #endif /*FSL_I2C_DMA_H_*/
125