1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_LPI2C_EDMA_H_
9 #define _FSL_LPI2C_EDMA_H_
10 
11 #include "fsl_lpi2c.h"
12 #include "fsl_edma.h"
13 
14 /*******************************************************************************
15  * Definitions
16  ******************************************************************************/
17 
18 /*! @name Driver version */
19 /*@{*/
20 /*! @brief LPI2C EDMA driver version 2.1.11. */
21 #define FSL_LPI2C_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 1, 11))
22 /*@}*/
23 
24 /*!
25  * @addtogroup lpi2c_master_edma_driver
26  * @{
27  */
28 
29 /* Forward declaration of the transfer descriptor and handle typedefs. */
30 typedef struct _lpi2c_master_edma_handle lpi2c_master_edma_handle_t;
31 
32 /*!
33  * @brief Master DMA completion callback function pointer type.
34  *
35  * This callback is used only for the non-blocking master transfer API. Specify the callback you wish to use
36  * in the call to LPI2C_MasterCreateEDMAHandle().
37  *
38  * @param base The LPI2C peripheral base address.
39  * @param handle Handle associated with the completed transfer.
40  * @param completionStatus Either kStatus_Success or an error code describing how the transfer completed.
41  * @param userData Arbitrary pointer-sized value passed from the application.
42  */
43 typedef void (*lpi2c_master_edma_transfer_callback_t)(LPI2C_Type *base,
44                                                       lpi2c_master_edma_handle_t *handle,
45                                                       status_t completionStatus,
46                                                       void *userData);
47 
48 /*!
49  * @brief Driver handle for master DMA APIs.
50  * @note The contents of this structure are private and subject to change.
51  */
52 struct _lpi2c_master_edma_handle
53 {
54     LPI2C_Type *base;                 /*!< LPI2C base pointer. */
55     bool isBusy;                      /*!< Transfer state machine current state. */
56     uint8_t nbytes;                   /*!< eDMA minor byte transfer count initially configured. */
57     uint16_t commandBuffer[7];        /*!< LPI2C command sequence. */
58     lpi2c_master_transfer_t transfer; /*!< Copy of the current transfer info. */
59     lpi2c_master_edma_transfer_callback_t completionCallback; /*!< Callback function pointer. */
60     void *userData;                                           /*!< Application data passed to callback. */
61     edma_handle_t *rx;                                        /*!< Handle for receive DMA channel. */
62     edma_handle_t *tx;                                        /*!< Handle for transmit DMA channel. */
63     edma_tcd_t tcds[3]; /*!< Software TCD. Three are allocated to provide enough room to align to 32-bytes. */
64 };
65 
66 /*! @} */
67 
68 /*******************************************************************************
69  * API
70  ******************************************************************************/
71 
72 #if defined(__cplusplus)
73 extern "C" {
74 #endif
75 
76 /*!
77  * @addtogroup lpi2c_master_edma_driver
78  * @{
79  */
80 
81 /*! @name Master DMA */
82 /*@{*/
83 
84 /*!
85  * @brief Create a new handle for the LPI2C master DMA APIs.
86  *
87  * The creation of a handle is for use with the DMA APIs. Once a handle
88  * is created, there is not a corresponding destroy handle. If the user wants to
89  * terminate a transfer, the LPI2C_MasterTransferAbortEDMA() API shall be called.
90  *
91  * For devices where the LPI2C send and receive DMA requests are OR'd together, the @a txDmaHandle
92  * parameter is ignored and may be set to NULL.
93  *
94  * @param base The LPI2C peripheral base address.
95  * @param[out] handle Pointer to the LPI2C master driver handle.
96  * @param rxDmaHandle Handle for the eDMA receive channel. Created by the user prior to calling this function.
97  * @param txDmaHandle Handle for the eDMA transmit channel. Created by the user prior to calling this function.
98  * @param callback User provided pointer to the asynchronous callback function.
99  * @param userData User provided pointer to the application callback data.
100  */
101 void LPI2C_MasterCreateEDMAHandle(LPI2C_Type *base,
102                                   lpi2c_master_edma_handle_t *handle,
103                                   edma_handle_t *rxDmaHandle,
104                                   edma_handle_t *txDmaHandle,
105                                   lpi2c_master_edma_transfer_callback_t callback,
106                                   void *userData);
107 
108 /*!
109  * @brief Performs a non-blocking DMA-based transaction on the I2C bus.
110  *
111  * The callback specified when the @a handle was created is invoked when the transaction has
112  * completed.
113  *
114  * @param base The LPI2C peripheral base address.
115  * @param handle Pointer to the LPI2C master driver handle.
116  * @param transfer The pointer to the transfer descriptor.
117  * @retval kStatus_Success The transaction was started successfully.
118  * @retval #kStatus_LPI2C_Busy Either another master is currently utilizing the bus, or another DMA
119  *      transaction is already in progress.
120  */
121 status_t LPI2C_MasterTransferEDMA(LPI2C_Type *base,
122                                   lpi2c_master_edma_handle_t *handle,
123                                   lpi2c_master_transfer_t *transfer);
124 
125 /*!
126  * @brief Returns number of bytes transferred so far.
127  *
128  * @param base The LPI2C peripheral base address.
129  * @param handle Pointer to the LPI2C master driver handle.
130  * @param[out] count Number of bytes transferred so far by the non-blocking transaction.
131  * @retval kStatus_Success
132  * @retval kStatus_NoTransferInProgress There is not a DMA transaction currently in progress.
133  */
134 status_t LPI2C_MasterTransferGetCountEDMA(LPI2C_Type *base, lpi2c_master_edma_handle_t *handle, size_t *count);
135 
136 /*!
137  * @brief Terminates a non-blocking LPI2C master transmission early.
138  *
139  * @note It is not safe to call this function from an IRQ handler that has a higher priority than the
140  *      eDMA peripheral's IRQ priority.
141  *
142  * @param base The LPI2C peripheral base address.
143  * @param handle Pointer to the LPI2C master driver handle.
144  * @retval kStatus_Success A transaction was successfully aborted.
145  * @retval #kStatus_LPI2C_Idle There is not a DMA transaction currently in progress.
146  */
147 status_t LPI2C_MasterTransferAbortEDMA(LPI2C_Type *base, lpi2c_master_edma_handle_t *handle);
148 
149 /*@}*/
150 
151 /*! @} */
152 
153 #if defined(__cplusplus)
154 }
155 #endif
156 
157 #endif /* _FSL_LPI2C_EDMA_H_ */
158