1 /*
2  * Copyright 2019-2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef FSL_ASRC_SDMA_H_
9 #define FSL_ASRC_SDMA_H_
10 
11 #include "fsl_asrc.h"
12 #include "fsl_sdma.h"
13 
14 /*!
15  * @addtogroup asrc_sdma
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 #define FSL_ASRC_SDMA_DRIVER_VERSION (MAKE_VERSION(2, 0, 3)) /*!< Version 2.0.3 */
26 /*! @} */
27 /*! @brief ASRC xfer queue size */
28 #ifndef ASRC_XFER_IN_QUEUE_SIZE
29 #define ASRC_XFER_IN_QUEUE_SIZE 4U
30 #endif
31 #ifndef ASRC_XFER_OUT_QUEUE_SIZE
32 #define ASRC_XFER_OUT_QUEUE_SIZE (ASRC_XFER_IN_QUEUE_SIZE * 2U)
33 #endif
34 
35 /*! @brief ASRC sdma handle prototype */
36 typedef struct _asrc_sdma_handle asrc_sdma_handle_t;
37 
38 /*! @brief ASRC SDMA transfer callback function for finish and error */
39 typedef void (*asrc_sdma_callback_t)(ASRC_Type *base, asrc_sdma_handle_t *handle, status_t status, void *userData);
40 /*! @brief ASRC trigger peripheral function pointer */
41 typedef void (*asrc_start_peripheral_t)(bool start);
42 /*! @brief destination peripheral configuration */
43 typedef struct _asrc_p2p_sdma_config
44 {
45     uint32_t eventSource;  /*!< peripheral event source */
46     uint8_t watermark;     /*!< peripheral watermark */
47     uint8_t channel;       /*!< peripheral channel number */
48     uint8_t fifoWidth;     /*!< peripheral fifo width */
49     bool enableContinuous; /*!< true is the amount of samples to be transferred is unknown and script will keep on
50                             transferring as long as both events are detected and script must be stopped by
51                             application, false is The amount of samples to be transferred is equal to the count
52                             field of mode word */
53     asrc_start_peripheral_t startPeripheral; /*!< trigger peripheral start */
54 } asrc_p2p_sdma_config_t;
55 
56 /*! @brief ASRC sdma in handle */
57 typedef struct _asrc_sdma_in_handle
58 {
59     sdma_handle_t *sdmaHandle;     /*!< DMA handler for ASRC */
60     uint32_t eventSource;          /*!< ASRC event source number */
61     asrc_sdma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
62     void *userData;                /*!< User callback parameter */
63     sdma_buffer_descriptor_t bdPool[ASRC_XFER_IN_QUEUE_SIZE]; /*!< BD pool for SDMA transfer. */
64     uint8_t asrcInWatermark;                                  /*!< The transfer data count in a DMA request */
65     uint8_t bytesPerSample;                                   /*!< Bytes in a sample */
66     uint32_t *asrcQueue[ASRC_XFER_IN_QUEUE_SIZE];             /*!< Transfer queue storing queued transfer. */
67     size_t sdmaTransferSize[ASRC_XFER_IN_QUEUE_SIZE];         /*!< Data bytes need to transfer */
68     volatile uint8_t queueUser;                               /*!< Index for user to queue transfer. */
69     volatile uint8_t queueDriver;                             /*!< Index for driver to get the transfer data and size */
70     const asrc_p2p_sdma_config_t *peripheralConfig;           /*!< peripheral configuration */
71     uint32_t state;                                           /*!< Internal state for ASRC SDMA transfer */
72 } asrc_sdma_in_handle_t;
73 
74 /*! @brief ASRC sdma out handle */
75 typedef struct _asrc_sdma_out_handle
76 {
77     sdma_handle_t *sdmaHandle;     /*!< DMA handler for ASRC */
78     void *userData;                /*!< User callback parameter */
79     uint32_t state;                /*!< Internal state for ASRC SDMA transfer */
80     uint8_t bytesPerSample;        /*!< Bytes in a sample */
81     uint32_t eventSource;          /*!< ASRC event source number */
82     asrc_sdma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
83     uint8_t asrcOutWatermark;      /*!< The transfer data count in a DMA request */
84     sdma_buffer_descriptor_t bdPool[ASRC_XFER_OUT_QUEUE_SIZE]; /*!< BD pool for SDMA transfer. */
85     uint32_t *asrcQueue[ASRC_XFER_OUT_QUEUE_SIZE];             /*!< Transfer queue storing queued transfer. */
86     size_t sdmaTransferSize[ASRC_XFER_OUT_QUEUE_SIZE];         /*!< Data bytes need to transfer */
87     volatile uint8_t queueUser;                                /*!< Index for user to queue transfer. */
88     volatile uint8_t queueDriver;                   /*!< Index for driver to get the transfer data and size */
89     const asrc_p2p_sdma_config_t *peripheralConfig; /*!< peripheral configuration */
90     uint32_t nonAlignSize;                          /*!< non align size */
91     void *nonAlignAddr;                             /*!< non align address */
92 
93 } asrc_sdma_out_handle_t;
94 
95 /*! @brief ASRC DMA transfer handle, users should not touch the content of the handle. */
96 struct _asrc_sdma_handle
97 {
98     asrc_sdma_in_handle_t inDMAHandle;   /*!< input dma handle */
99     asrc_sdma_out_handle_t outDMAHandle; /*!< output dma handle */
100 
101     asrc_context_t context; /*!< ASRC context number */
102     uint8_t dataChannels;   /*!< ASRC process data channel number */
103 };
104 
105 /*******************************************************************************
106  * APIs
107  ******************************************************************************/
108 #if defined(__cplusplus)
109 extern "C" {
110 #endif
111 /*!
112  * @name ASRC SDMA Transactional
113  * @{
114  */
115 
116 /*!
117  * @brief Initializes the ASRC input SDMA handle.
118  *
119  * This function initializes the ASRC input DMA handle, which can be used for other ASRC transactional APIs.
120  * Usually, for a specified ASRC context, call this API once to get the initialized handle.
121  *
122  * @param base ASRC base pointer.
123  * @param handle ASRC SDMA handle pointer.
124  * @param base ASRC peripheral base address.
125  * @param callback Pointer to user callback function.
126  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
127  * @param eventSource ASRC input sdma event source.
128  * @param context ASRC context number.
129  * @param periphConfig peripheral configurations, used for  case.
130  * @param userData User parameter passed to the callback function.
131  */
132 void ASRC_TransferInCreateHandleSDMA(ASRC_Type *base,
133                                      asrc_sdma_handle_t *handle,
134                                      asrc_sdma_callback_t callback,
135                                      sdma_handle_t *dmaHandle,
136                                      uint32_t eventSource,
137                                      asrc_context_t context,
138                                      const asrc_p2p_sdma_config_t *periphConfig,
139                                      void *userData);
140 
141 /*!
142  * @brief Initializes the ASRC output  SDMA handle.
143  *
144  * This function initializes the ASRC out DMA handle, which can be used for other ASRC transactional APIs.
145  * Usually, for a specified ASRC context, call this API once to get the initialized handle.
146  *
147  * @param base ASRC base pointer.
148  * @param handle ASRC SDMA handle pointer.
149  * @param callback ASRC outcallback.
150  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
151  * @param eventSource ASRC output event source.
152  * @param context ASRC context number.
153  * @param periphConfig peripheral configurations, used for  case.
154  * @param userData User parameter passed to the callback function.
155  */
156 void ASRC_TransferOutCreateHandleSDMA(ASRC_Type *base,
157                                       asrc_sdma_handle_t *handle,
158                                       asrc_sdma_callback_t callback,
159                                       sdma_handle_t *dmaHandle,
160                                       uint32_t eventSource,
161                                       asrc_context_t context,
162                                       const asrc_p2p_sdma_config_t *periphConfig,
163                                       void *userData);
164 
165 /*!
166  * @brief Configures the ASRC context.
167  *
168  * @param base ASRC base pointer.
169  * @param handle ASRC SDMA handle pointer.
170  * @param asrcConfig asrc context configurations.
171  */
172 status_t ASRC_TransferSetContextConfigSDMA(ASRC_Type *base,
173                                            asrc_sdma_handle_t *handle,
174                                            asrc_context_config_t *asrcConfig);
175 /*!
176  * @brief Performs a non-blocking ASRC transfer using DMA.
177  *
178  *
179  * @param base ASRC base pointer.
180  * @param handle ASRC SDMA handle pointer.
181  * @param xfer ASRC xfer configurations pointer.
182  * @retval kStatus_Success Start a ASRC SDMA send successfully.
183  * @retval kStatus_InvalidArgument The input argument is invalid.
184  * @retval kStatus_TxBusy ASRC is busy sending data.
185  */
186 status_t ASRC_TransferSDMA(ASRC_Type *base, asrc_sdma_handle_t *handle, asrc_transfer_t *xfer);
187 
188 /*!
189  * @brief Aborts a ASRC in transfer using SDMA.
190  *
191  * @param base ASRC base pointer.
192  * @param handle ASRC SDMA handle pointer.
193  */
194 void ASRC_TransferAbortInSDMA(ASRC_Type *base, asrc_sdma_handle_t *handle);
195 
196 /*!
197  * brief Aborts a ASRC out transfer using SDMA.
198  *
199  * param base ASRC base pointer.
200  * param handle ASRC SDMA handle pointer.
201  */
202 void ASRC_TransferAbortOutSDMA(ASRC_Type *base, asrc_sdma_handle_t *handle);
203 
204 /*! @} */
205 
206 #if defined(__cplusplus)
207 }
208 #endif
209 
210 /*!
211  * @}
212  */
213 #endif /*FSL_ASRC_SDMA_H_*/
214