1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017,2024 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef FSL_SAI_DMA_H_
9 #define FSL_SAI_DMA_H_
10 
11 #include "fsl_dma.h"
12 #include "fsl_sai.h"
13 
14 /*!
15  * @addtogroup sai_dma SAI DMA Driver
16  * @ingroup sai
17  * @{
18  */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 
24 /*! @name Driver version */
25 /*! @{ */
26 #define FSL_SAI_DMA_DRIVER_VERSION (MAKE_VERSION(2, 4, 0)) /*!< Version 2.4.0 */
27 /*! @} */
28 
29 typedef struct _sai_dma_handle sai_dma_handle_t;
30 
31 /*! @brief Define SAI DMA callback */
32 typedef void (*sai_dma_callback_t)(I2S_Type *base, sai_dma_handle_t *handle, status_t status, void *userData);
33 
34 /*! @brief SAI DMA transfer handle, users should not touch the content of the handle.*/
35 struct _sai_dma_handle
36 {
37     dma_handle_t *dmaHandle;                      /*!< DMA handler for SAI send */
38     uint8_t bytesPerFrame;                        /*!< Bytes in a frame */
39     uint8_t channel;                              /*!< Which Data channel SAI use */
40     uint32_t state;                               /*!< SAI DMA transfer internal state */
41     sai_dma_callback_t callback;                  /*!< Callback for users while transfer finish or error occurred */
42     void *userData;                               /*!< User callback parameter */
43     sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer. */
44     size_t transferSize[SAI_XFER_QUEUE_SIZE];     /*!< Data bytes need to transfer */
45     volatile uint8_t queueUser;                   /*!< Index for user to queue transfer. */
46     volatile uint8_t queueDriver;                 /*!< Index for driver to get the transfer data and size */
47 };
48 
49 /*******************************************************************************
50  * APIs
51  ******************************************************************************/
52 #if defined(__cplusplus)
53 extern "C" {
54 #endif
55 /*!
56  * @name DMA Transactional
57  * @{
58  */
59 
60 /*!
61  * @brief Initializes the SAI master DMA handle.
62  *
63  * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
64  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
65  *
66  * @param base SAI base pointer.
67  * @param handle SAI DMA handle pointer.
68  * @param base SAI peripheral base address.
69  * @param callback Pointer to user callback function.
70  * @param userData User parameter passed to the callback function.
71  * @param dmaHandle DMA handle pointer, this handle shall be static allocated by users.
72  */
73 void SAI_TransferTxCreateHandleDMA(
74     I2S_Type *base, sai_dma_handle_t *handle, sai_dma_callback_t callback, void *userData, dma_handle_t *dmaHandle);
75 
76 /*!
77  * @brief Initializes the SAI slave DMA handle.
78  *
79  * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
80  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
81  *
82  * @param base SAI base pointer.
83  * @param handle SAI DMA handle pointer.
84  * @param base SAI peripheral base address.
85  * @param callback Pointer to user callback function.
86  * @param userData User parameter passed to the callback function.
87  * @param dmaHandle DMA handle pointer, this handle shall be static allocated by users.
88  */
89 void SAI_TransferRxCreateHandleDMA(
90     I2S_Type *base, sai_dma_handle_t *handle, sai_dma_callback_t callback, void *userData, dma_handle_t *dmaHandle);
91 
92 /*!
93  * @brief Performs a non-blocking SAI transfer using DMA.
94  *
95  * @note This interface returns immediately after the transfer initiates. Call
96  * the SAI_GetTransferStatus to poll the transfer status to check whether the SAI transfer finished.
97  *
98  * @param base SAI base pointer.
99  * @param handle SAI DMA handle pointer.
100  * @param xfer Pointer to DMA transfer structure.
101  * @retval kStatus_Success Successfully start the data receive.
102  * @retval kStatus_SAI_TxBusy Previous receive still not finished.
103  * @retval kStatus_InvalidArgument The input parameter is invalid.
104  */
105 status_t SAI_TransferSendDMA(I2S_Type *base, sai_dma_handle_t *handle, sai_transfer_t *xfer);
106 
107 /*!
108  * @brief Performs a non-blocking SAI transfer using DMA.
109  *
110  * @note This interface returns immediately after transfer initiates. Call
111  * SAI_GetTransferStatus to poll the transfer status to check whether the SAI transfer is finished.
112  *
113  * @param base SAI base pointer
114  * @param handle SAI DMA handle pointer.
115  * @param xfer Pointer to DMA transfer structure.
116  * @retval kStatus_Success Successfully start the data receive.
117  * @retval kStatus_SAI_RxBusy Previous receive still not finished.
118  * @retval kStatus_InvalidArgument The input parameter is invalid.
119  */
120 status_t SAI_TransferReceiveDMA(I2S_Type *base, sai_dma_handle_t *handle, sai_transfer_t *xfer);
121 
122 /*!
123  * @brief Aborts a SAI transfer using DMA.
124  *
125  * @param base SAI base pointer.
126  * @param handle SAI DMA handle pointer.
127  */
128 void SAI_TransferAbortSendDMA(I2S_Type *base, sai_dma_handle_t *handle);
129 
130 /*!
131  * @brief Aborts a SAI transfer using DMA.
132  *
133  * @param base SAI base pointer.
134  * @param handle SAI DMA handle pointer.
135  */
136 void SAI_TransferAbortReceiveDMA(I2S_Type *base, sai_dma_handle_t *handle);
137 
138 /*!
139  * @brief Gets byte count sent by SAI.
140  *
141  * @param base SAI base pointer.
142  * @param handle SAI DMA handle pointer.
143  * @param count Bytes count sent by SAI.
144  * @retval kStatus_Success Succeed get the transfer count.
145  * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
146  */
147 status_t SAI_TransferGetSendCountDMA(I2S_Type *base, sai_dma_handle_t *handle, size_t *count);
148 
149 /*!
150  * @brief Gets byte count received by SAI.
151  *
152  * @param base SAI base pointer.
153  * @param handle SAI DMA handle pointer.
154  * @param count Bytes count received by SAI.
155  * @retval kStatus_Success Succeed get the transfer count.
156  * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
157  */
158 status_t SAI_TransferGetReceiveCountDMA(I2S_Type *base, sai_dma_handle_t *handle, size_t *count);
159 
160 /*!
161  * @brief Configures the SAI Tx.
162  *
163  *
164  * @param base SAI base pointer.
165  * @param handle SAI DMA handle pointer.
166  * @param saiConfig sai configurations.
167  */
168 void SAI_TransferTxSetConfigDMA(I2S_Type *base, sai_dma_handle_t *handle, sai_transceiver_t *saiConfig);
169 
170 /*!
171  * @brief Configures the SAI Rx.
172  *
173  *
174  * @param base SAI base pointer.
175  * @param handle SAI DMA handle pointer.
176  * @param saiConfig sai configurations.
177  */
178 void SAI_TransferRxSetConfigDMA(I2S_Type *base, sai_dma_handle_t *handle, sai_transceiver_t *saiConfig);
179 
180 /*! @} */
181 
182 #if defined(__cplusplus)
183 }
184 #endif
185 
186 /*!
187  * @}
188  */
189 #endif
190