1 /*
2  * Copyright 2017 - 2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef FSL_SAI_SDMA_H_
9 #define FSL_SAI_SDMA_H_
10 
11 #include "fsl_sai.h"
12 #include "fsl_sdma.h"
13 
14 /*!
15  * @addtogroup sai_sdma SAI SDMA Driver
16  * @ingroup sai
17  * @{
18  */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 
24 /*! @name Driver version */
25 /*! @{ */
26 #define FSL_SAI_SDMA_DRIVER_VERSION (MAKE_VERSION(2, 6, 0)) /*!< Version 2.6.0 */
27 /*! @} */
28 
29 typedef struct _sai_sdma_handle sai_sdma_handle_t;
30 
31 /*! @brief SAI SDMA transfer callback function for finish and error */
32 typedef void (*sai_sdma_callback_t)(I2S_Type *base, sai_sdma_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_sdma_handle
36 {
37     sdma_handle_t *dmaHandle;     /*!< DMA handler for SAI send */
38     uint8_t bytesPerFrame;        /*!< Bytes in a frame */
39     uint8_t channel;              /*!< start data channel */
40     uint8_t channelNums;          /*!< total transfer channel numbers, used for multififo */
41     uint8_t channelMask;          /*!< enabled channel mask value, refernece _sai_channel_mask */
42     uint8_t fifoOffset;           /*!< fifo address offset between multifo*/
43     uint32_t count;               /*!< The transfer data count in a DMA request */
44     uint32_t state;               /*!< Internal state for SAI SDMA transfer */
45     uint32_t eventSource;         /*!< SAI event source number */
46     sai_sdma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
47     void *userData;               /*!< User callback parameter */
48     sdma_buffer_descriptor_t bdPool[SAI_XFER_QUEUE_SIZE]; /*!< BD pool for SDMA transfer. */
49     sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE];         /*!< Transfer queue storing queued transfer. */
50     size_t transferSize[SAI_XFER_QUEUE_SIZE];             /*!< Data bytes need to transfer */
51     volatile uint8_t queueUser;                           /*!< Index for user to queue transfer. */
52     volatile uint8_t queueDriver;                         /*!< Index for driver to get the transfer data and size */
53 };
54 
55 /*******************************************************************************
56  * APIs
57  ******************************************************************************/
58 #if defined(__cplusplus)
59 extern "C" {
60 #endif
61 /*!
62  * @name SDMA Transactional
63  * @{
64  */
65 
66 /*!
67  * @brief Initializes the SAI SDMA handle.
68  *
69  * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
70  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
71  *
72  * @param base SAI base pointer.
73  * @param handle SAI SDMA handle pointer.
74  * @param base SAI peripheral base address.
75  * @param callback Pointer to user callback function.
76  * @param userData User parameter passed to the callback function.
77  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
78  * @param eventSource SAI event source number.
79  */
80 void SAI_TransferTxCreateHandleSDMA(I2S_Type *base,
81                                     sai_sdma_handle_t *handle,
82                                     sai_sdma_callback_t callback,
83                                     void *userData,
84                                     sdma_handle_t *dmaHandle,
85                                     uint32_t eventSource);
86 
87 /*!
88  * @brief Initializes the SAI Rx SDMA handle.
89  *
90  * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
91  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
92  *
93  * @param base SAI base pointer.
94  * @param handle SAI SDMA handle pointer.
95  * @param base SAI peripheral base address.
96  * @param callback Pointer to user callback function.
97  * @param userData User parameter passed to the callback function.
98  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
99  * @param eventSource SAI event source number.
100  */
101 void SAI_TransferRxCreateHandleSDMA(I2S_Type *base,
102                                     sai_sdma_handle_t *handle,
103                                     sai_sdma_callback_t callback,
104                                     void *userData,
105                                     sdma_handle_t *dmaHandle,
106                                     uint32_t eventSource);
107 
108 /*!
109  * @brief Performs a non-blocking SAI transfer using DMA.
110  *
111  * @note This interface returns immediately after the transfer initiates. Call
112  * SAI_GetTransferStatus to poll the transfer status and check whether the SAI transfer is finished.
113  *
114  * @param base SAI base pointer.
115  * @param handle SAI SDMA handle pointer.
116  * @param xfer Pointer to the DMA transfer structure.
117  * @retval kStatus_Success Start a SAI SDMA send successfully.
118  * @retval kStatus_InvalidArgument The input argument is invalid.
119  * @retval kStatus_TxBusy SAI is busy sending data.
120  */
121 status_t SAI_TransferSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transfer_t *xfer);
122 
123 /*!
124  * @brief Performs a non-blocking SAI receive using SDMA.
125  *
126  * @note This interface returns immediately after the transfer initiates. Call
127  * the SAI_GetReceiveRemainingBytes to poll the transfer status and check whether the SAI transfer is finished.
128  *
129  * @param base SAI base pointer
130  * @param handle SAI SDMA handle pointer.
131  * @param xfer Pointer to DMA transfer structure.
132  * @retval kStatus_Success Start a SAI SDMA receive successfully.
133  * @retval kStatus_InvalidArgument The input argument is invalid.
134  * @retval kStatus_RxBusy SAI is busy receiving data.
135  */
136 status_t SAI_TransferReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transfer_t *xfer);
137 
138 /*!
139  * @brief Aborts a SAI transfer using SDMA.
140  *
141  * @param base SAI base pointer.
142  * @param handle SAI SDMA handle pointer.
143  */
144 void SAI_TransferAbortSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
145 
146 /*!
147  * @brief Aborts a SAI receive using SDMA.
148  *
149  * @param base SAI base pointer
150  * @param handle SAI SDMA handle pointer.
151  */
152 void SAI_TransferAbortReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
153 
154 /*!
155  * @brief Terminate all the SAI sdma receive transfer.
156  *
157  * @param base SAI base pointer.
158  * @param handle SAI SDMA handle pointer.
159  */
160 void SAI_TransferTerminateReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
161 
162 /*!
163  * @brief Terminate all the SAI sdma send transfer.
164  *
165  * @param base SAI base pointer.
166  * @param handle SAI SDMA handle pointer.
167  */
168 void SAI_TransferTerminateSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
169 
170 /*!
171  * brief Configures the SAI RX.
172  *
173  * param base SAI base pointer.
174  * param handle SAI SDMA handle pointer.
175  * param saiConig sai configurations.
176  */
177 void SAI_TransferRxSetConfigSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transceiver_t *saiConfig);
178 
179 /*!
180  * brief Configures the SAI Tx.
181  *
182  * param base SAI base pointer.
183  * param handle SAI SDMA handle pointer.
184  * param saiConig sai configurations.
185  */
186 void SAI_TransferTxSetConfigSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transceiver_t *saiConfig);
187 /*! @} */
188 
189 #if defined(__cplusplus)
190 }
191 #endif
192 
193 /*!
194  * @}
195  */
196 #endif
197