1 /*
2  * Copyright 2019-2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef FSL_ASRC_H_
9 #define FSL_ASRC_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup asrc_driver
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*! @name Driver version */
23 /*! @{ */
24 #define FSL_ASRC_DRIVER_VERSION (MAKE_VERSION(2, 1, 3)) /*!< Version 2.1.3 */
25 /*! @} */
26 
27 #ifndef ASRC_XFER_QUEUE_SIZE
28 /*!@brief ASRC transfer queue size, user can refine it according to use case. */
29 #define ASRC_XFER_QUEUE_SIZE (4U)
30 #endif
31 /*!@brief ASRC channel pair count */
32 #define FSL_ASRC_CHANNEL_PAIR_COUNT (4U)
33 /*! @brief ASRC FIFO depth */
34 #define FSL_ASRC_CHANNEL_PAIR_FIFO_DEPTH (64U)
35 
36 /*! @brief ASRC register access macro */
37 #define ASRC_ASRCTR_AT_MASK(index)    ((uint32_t)1U << (ASRC_ASRCTR_ATSA_SHIFT + (uint32_t)(index)))
38 #define ASRC_ASRCTR_RATIO_MASK(index) ((uint32_t)3U << (ASRC_ASRCTR_IDRA_SHIFT + (uint32_t)(index)*2U))
39 #define ASRC_ASRCTR_RATIO(ratio, index) \
40     (((uint32_t)((uint32_t)(ratio) << (ASRC_ASRCTR_IDRA_SHIFT + (uint32_t)(index)*2U))) & ASRC_ASRCTR_RATIO_MASK(index))
41 #define ASRC_ASRIER_INPUT_INTERRUPT_MASK(index)     ((uint32_t)1U << (ASRC_ASRIER_ADIEA_SHIFT + (uint32_t)(index)))
42 #define ASRC_ASRIER_OUTPUTPUT_INTERRUPT_MASK(index) ((uint32_t)1U << (ASRC_ASRIER_ADOEA_SHIFT + (uint32_t)(index)))
43 #define ASRC_ASRCNCR_CHANNEL_COUNTER_MASK(index)    ((uint32_t)0xFU << (ASRC_ASRCNCR_ANCA_SHIFT + (uint32_t)(index)*4U))
44 #define ASRC_ASRCNCR_CHANNEL_COUNTER(counter, index)                                       \
45     ((uint32_t)((uint32_t)(counter) << (ASRC_ASRCNCR_ANCA_SHIFT + (uint32_t)(index)*4U)) & \
46      ASRC_ASRCNCR_CHANNEL_COUNTER_MASK(index))
47 #define ASRC_ASRCFG_PRE_MODE_MASK(index) ((uint32_t)3U << (ASRC_ASRCFG_PREMODA_SHIFT + (uint32_t)(index)*4U))
48 #define ASRC_ASRCFG_PRE_MODE(mode, index)                                                 \
49     ((uint32_t)((uint32_t)(mode) << (ASRC_ASRCFG_PREMODA_SHIFT + (uint32_t)(index)*4U)) & \
50      ASRC_ASRCFG_PRE_MODE_MASK(index))
51 #define ASRC_ASRCFG_POST_MODE_MASK(index) ((uint32_t)3U << (ASRC_ASRCFG_POSTMODA_SHIFT + (uint32_t)(index)*4U))
52 #define ASRC_ASRCFG_POST_MODE(mode, index)                                                 \
53     ((uint32_t)((uint32_t)(mode) << (ASRC_ASRCFG_POSTMODA_SHIFT + (uint32_t)(index)*4U)) & \
54      ASRC_ASRCFG_POST_MODE_MASK(index))
55 #define ASRC_ASRCFG_INIT_DONE_MASK(index)          ((uint32_t)1U << (ASRC_ASRCFG_INIRQA_SHIFT + (uint32_t)(index)))
56 #define ASRC_ASRCSR_INPUT_CLOCK_SOURCE_MASK(index) ((uint32_t)0xFU << (ASRC_ASRCSR_AICSA_SHIFT + (uint32_t)(index)*4U))
57 #define ASRC_ASRCSR_INPUT_CLOCK_SOURCE(source, index)                                     \
58     ((uint32_t)((uint32_t)(source) << (ASRC_ASRCSR_AICSA_SHIFT + (uint32_t)(index)*4U)) & \
59      ASRC_ASRCSR_INPUT_CLOCK_SOURCE_MASK(index))
60 #define ASRC_ASRCSR_OUTPUT_CLOCK_SOURCE_MASK(index) ((uint32_t)0xFU << (ASRC_ASRCSR_AOCSA_SHIFT + (uint32_t)(index)*4U))
61 #define ASRC_ASRCSR_OUTPUT_CLOCK_SOURCE(source, index)                                    \
62     ((uint32_t)((uint32_t)(source) << (ASRC_ASRCSR_AOCSA_SHIFT + (uint32_t)(index)*4U)) & \
63      ASRC_ASRCSR_OUTPUT_CLOCK_SOURCE_MASK(index))
64 
65 #define ASRC_ASRCDR_INPUT_PRESCALER_MASK(index) \
66     ((uint32_t)(index) < 2U ? ((uint32_t)7U << (ASRC_ASRCDR1_AICPA_SHIFT + (uint32_t)(index)*6U)) : 7U)
67 #define ASRC_ASRCDR_INPUT_PRESCALER(prescaler, index)                                                              \
68     (((index) < 2U ? ((uint32_t)(prescaler) << (ASRC_ASRCDR1_AICPA_SHIFT + (uint32_t)(index)*6U)) : (prescaler)) & \
69      ASRC_ASRCDR1_INPUT_PRESCALER_MASK(index))
70 #define ASRC_ASRCDR_INPUT_DIVIDER_MASK(index)                                                       \
71     ((uint32_t)(index) < 2U ? ((uint32_t)7U << (ASRC_ASRCDR1_AICDA_SHIFT + (uint32_t)(index)*6U)) : \
72                               (7U << ASRC_ASRCDR1_AICDA_SHIFT))
73 #define ASRC_ASRCDR_INPUT_DIVIDER(divider, index)                                                           \
74     (((uint32_t)(index) < 2U ? ((uint32_t)(divider) << (ASRC_ASRCDR1_AICDA_SHIFT + (uint32_t)(index)*6U)) : \
75                                ((uint32_t)(divider) << ASRC_ASRCDR1_AICDA_SHIFT)) &                         \
76      ASRC_ASRCDR_INPUT_DIVIDER_MASK(index))
77 #define ASRC_ASRCDR_OUTPUT_PRESCALER_MASK(index) \
78     ((uint32_t)(index) < 2U ? ((uint32_t)7U << (ASRC_ASRCDR1_AOCPA_SHIFT + (uint32_t)(index)*6U)) : (7U << 6U))
79 #define ASRC_ASRCDR_OUTPUT_PRESCALER(prescaler, index)                                                        \
80     (((uint32_t)(index) < 2U ? ((uint32_t)(prescaler) << (ASRC_ASRCDR1_AOCPA_SHIFT + (uint32_t)(index)*6U)) : \
81                                ((uint32_t)(prescaler) << 6U)) &                                               \
82      ASRC_ASRCDR_OUTPUT_PRESCALER_MASK(index))
83 #define ASRC_ASRCDR_OUTPUT_DIVIDER_MASK(index) \
84     ((uint32_t)(index) < 2U ? ((uint32_t)7U << (ASRC_ASRCDR1_AOCDA_SHIFT + (uint32_t)(index)*6U)) : (7UL << 9U))
85 #define ASRC_ASRCDR_OUTPUT_DIVIDER(divider, index)                                                          \
86     (((uint32_t)(index) < 2U ? ((uint32_t)(divider) << (ASRC_ASRCDR1_AOCDA_SHIFT + (uint32_t)(index)*6U)) : \
87                                ((uint32_t)(divider) << 9U)) &                                               \
88      ASRC_ASRCDR_OUTPUT_DIVIDER_MASK(index))
89 
90 #define ASCR_ASRCDR_OUTPUT_CLOCK_DIVIDER_PRESCALER(value, index)                                          \
91     (((uint32_t)(index) < 2U ? ((uint32_t)(value) << (ASRC_ASRCDR1_AOCPA_SHIFT + (uint32_t)(index)*6U)) : \
92                                ((uint32_t)(value) << 6U)))
93 #define ASCR_ASRCDR_INPUT_CLOCK_DIVIDER_PRESCALER(value, index) \
94     (((uint32_t)(index) < 2U ? ((uint32_t)(value) << ((uint32_t)(index)*6U)) : ((uint32_t)(value))))
95 
96 #define ASRC_IDEAL_RATIO_HIGH(base, index)    *(volatile uint32_t *)((uint32_t)(&(base)->ASRIDRHA) + (uint32_t)(index)*8U)
97 #define ASRC_IDEAL_RATIO_LOW(base, index)     *(volatile uint32_t *)((uint32_t)(&(base)->ASRIDRLA) + (uint32_t)(index)*8U)
98 #define ASRC_ASRMCR(base, index)              *(volatile uint32_t *)((uint32_t)(&(base)->ASRMCRA) + (uint32_t)(index)*8U)
99 #define ASRC_ASRMCR1(base, index)             *(volatile uint32_t *)((uint32_t)(&((base)->ASRMCR1[(index)])))
100 #define ASRC_ASRDI(base, index)               *(volatile uint32_t *)((uint32_t)(&(base)->ASRDIA) + (uint32_t)(index)*8U)
101 #define ASRC_ASRDO(base, index)               *(volatile uint32_t *)((uint32_t)(&(base)->ASRDOA) + (uint32_t)(index)*8U)
102 #define ASRC_ASRDI_ADDR(base, index)          (volatile uint32_t *)((uint32_t)(&(base)->ASRDIA) + (uint32_t)(index)*8U)
103 #define ASRC_ASRDO_ADDR(base, index)          (volatile uint32_t *)((uint32_t)(&(base)->ASRDOA) + (uint32_t)(index)*8U)
104 #define ASRC_ASRFST_ADDR(base, index)         (*(volatile uint32_t *)((uint32_t)(&(base)->ASRFSTA) + (uint32_t)(index)*8U))
105 #define ASRC_GET_CHANNEL_COUNTER(base, index) (((base)->ASRCNCR >> ((uint32_t)(index)*4U)) & 0xFU)
106 
107 /*! @brief ASRC return status
108  * @anchor _asrc_status_t
109  */
110 enum
111 {
112     kStatus_ASRCIdle                       = MAKE_STATUS(kStatusGroup_ASRC, 0),  /*!< ASRC is idle. */
113     kStatus_ASRCInIdle                     = MAKE_STATUS(kStatusGroup_ASRC, 1),  /*!< ASRC in is idle. */
114     kStatus_ASRCOutIdle                    = MAKE_STATUS(kStatusGroup_ASRC, 2),  /*!< ASRC out is idle. */
115     kStatus_ASRCBusy                       = MAKE_STATUS(kStatusGroup_ASRC, 3),  /*!< ASRC  is busy. */
116     kStatus_ASRCInvalidArgument            = MAKE_STATUS(kStatusGroup_ASRC, 4),  /*!< ASRC invalid argument. */
117     kStatus_ASRCClockConfigureFailed       = MAKE_STATUS(kStatusGroup_ASRC, 5),  /*!< ASRC clock configure failed */
118     kStatus_ASRCChannelPairConfigureFailed = MAKE_STATUS(kStatusGroup_ASRC, 6),  /*!< ASRC clock configure failed */
119     kStatus_ASRCConvertError               = MAKE_STATUS(kStatusGroup_ASRC, 7),  /*!< ASRC clock configure failed */
120     kStatus_ASRCNotSupport                 = MAKE_STATUS(kStatusGroup_ASRC, 8),  /*!< ASRC not support */
121     kStatus_ASRCQueueFull                  = MAKE_STATUS(kStatusGroup_ASRC, 9),  /*!< ASRC queue is full */
122     kStatus_ASRCOutQueueIdle               = MAKE_STATUS(kStatusGroup_ASRC, 10), /*!< ASRC out queue is idle */
123     kStatus_ASRCInQueueIdle                = MAKE_STATUS(kStatusGroup_ASRC, 11), /*!< ASRC in queue is idle */
124 };
125 
126 /*! @brief ASRC channel pair mask */
127 typedef enum _asrc_channel_pair
128 {
129     kASRC_ChannelPairA = 0, /*!< channel pair A value */
130     kASRC_ChannelPairB = 1, /*!< channel pair B value */
131     kASRC_ChannelPairC = 2, /*!< channel pair C value */
132 } asrc_channel_pair_t;
133 
134 /*! @brief ASRC support sample rate
135  * @anchor _asrc_sample_rate
136  */
137 enum
138 {
139     kASRC_SampleRate_8000HZ   = 8000U,   /*!< asrc sample rate 8KHZ */
140     kASRC_SampleRate_11025HZ  = 11025U,  /*!< asrc sample rate 11.025KHZ */
141     kASRC_SampleRate_12000HZ  = 12000U,  /*!< asrc sample rate 12KHZ */
142     kASRC_SampleRate_16000HZ  = 16000U,  /*!< asrc sample rate 16KHZ */
143     kASRC_SampleRate_22050HZ  = 22050U,  /*!< asrc sample rate 22.05KHZ */
144     kASRC_SampleRate_24000HZ  = 24000U,  /*!< asrc sample rate 24KHZ */
145     kASRC_SampleRate_30000HZ  = 30000U,  /*!< asrc sample rate 30KHZ */
146     kASRC_SampleRate_32000HZ  = 32000U,  /*!< asrc sample rate 32KHZ */
147     kASRC_SampleRate_44100HZ  = 44100U,  /*!< asrc sample rate 44.1KHZ */
148     kASRC_SampleRate_48000HZ  = 48000U,  /*!< asrc sample rate 48KHZ */
149     kASRC_SampleRate_64000HZ  = 64000U,  /*!< asrc sample rate 64KHZ */
150     kASRC_SampleRate_88200HZ  = 88200U,  /*!< asrc sample rate 88.2KHZ */
151     kASRC_SampleRate_96000HZ  = 96000U,  /*!< asrc sample rate 96KHZ */
152     kASRC_SampleRate_128000HZ = 128000U, /*!< asrc sample rate 128KHZ */
153     kASRC_SampleRate_176400HZ = 176400U, /*!< asrc sample rate 176.4KHZ */
154     kASRC_SampleRate_192000HZ = 192000U, /*!< asrc sample rate 192KHZ */
155 };
156 
157 /*! @brief The ASRC interrupt enable flag
158  * @anchor _asrc_interrupt_mask
159  */
160 enum
161 {
162     kASRC_FPInWaitStateInterruptEnable = ASRC_ASRIER_AFPWE_MASK, /*!< FP in wait state mask */
163     kASRC_OverLoadInterruptMask        = ASRC_ASRIER_AOLIE_MASK, /*!< overload interrupt mask */
164     kASRC_DataOutputCInterruptMask     = ASRC_ASRIER_ADOEC_MASK, /*!< data output c interrupt mask */
165     kASRC_DataOutputBInterruptMask     = ASRC_ASRIER_ADOEB_MASK, /*!< data output b interrupt mask */
166     kASRC_DataOutputAInterruptMask     = ASRC_ASRIER_ADOEA_MASK, /*!< data output a interrupt mask */
167     kASRC_DataInputCInterruptMask      = ASRC_ASRIER_ADIEC_MASK, /*!< data input c interrupt mask */
168     kASRC_DataInputBInterruptMask      = ASRC_ASRIER_ADIEB_MASK, /*!< data input b interrupt mask */
169     kASRC_DataInputAInterruptMask      = ASRC_ASRIER_ADIEA_MASK, /*!< data input a interrupt mask */
170 };
171 
172 /*! @brief The ASRC interrupt status
173  * @anchor _asrc_interrupt_status
174  */
175 enum
176 {
177     kASRC_StatusDSLCounterReady     = ASRC_ASRSTR_DSLCNT_MASK, /*!< DSL counter   */
178     kASRC_StatusTaskQueueOverLoad   = ASRC_ASRSTR_ATQOL_MASK,  /*!< task queue overload */
179     kASRC_StatusPairCOutputOverLoad = ASRC_ASRSTR_AOOLC_MASK,  /*!< pair c output overload  */
180     kASRC_StatusPairBOutputOverLoad = ASRC_ASRSTR_AOOLB_MASK,  /*!< pair b output overload  */
181     kASRC_StatusPairAOutputOverLoad = ASRC_ASRSTR_AOOLA_MASK,  /*!< pair a output overload */
182     kASRC_StatusPairCInputOverLoad  = ASRC_ASRSTR_AIOLC_MASK,  /*!< pair c input overload */
183     kASRC_StatusPairBInputOverLoad  = ASRC_ASRSTR_AIOLB_MASK,  /*!<pair b input overload */
184     kASRC_StatusPairAInputOverLoad  = ASRC_ASRSTR_AIOLA_MASK,  /*!< pair a input overload */
185     kASRC_StatusPairCOutputOverflow = ASRC_ASRSTR_AODOC_MASK,  /*!< pair c output overflow */
186     kASRC_StatusPairBOutputOverflow = ASRC_ASRSTR_AODOB_MASK,  /*!< pair b output overflow */
187     kASRC_StatusPairAOutputOverflow = ASRC_ASRSTR_AODOA_MASK,  /*!< pair a output overflow */
188     kASRC_StatusPairCInputUnderflow = ASRC_ASRSTR_AIDUC_MASK,  /*!< pair c input underflow*/
189     kASRC_StatusPairBInputUnderflow = ASRC_ASRSTR_AIDUB_MASK,  /*!< pair b input under flow */
190     kASRC_StatusPairAInputUnderflow = ASRC_ASRSTR_AIDUA_MASK,  /*!< pair a input underflow*/
191     kASRC_StatusFPInWaitState       = ASRC_ASRSTR_FPWT_MASK,   /*!< FP in wait state */
192     kASRC_StatusOverloadError       = ASRC_ASRSTR_AOLE_MASK,   /*!< overload error */
193 
194     kASRC_StatusInputError = kASRC_StatusPairCInputOverLoad | kASRC_StatusPairBInputOverLoad |
195                              kASRC_StatusPairAInputOverLoad | kASRC_StatusPairCInputUnderflow |
196                              kASRC_StatusPairBInputUnderflow |
197                              kASRC_StatusPairAInputUnderflow, /*!< input error status */
198 
199     kASRC_StatusOutputError = kASRC_StatusPairCOutputOverLoad | kASRC_StatusPairBOutputOverLoad |
200                               kASRC_StatusPairAOutputOverLoad | kASRC_StatusPairCOutputOverflow |
201                               kASRC_StatusPairBOutputOverflow |
202                               kASRC_StatusPairAOutputOverflow, /*!< output error status */
203 
204     kASRC_StatusPairCOutputReady = ASRC_ASRSTR_AODFC_MASK, /*!< pair c output ready */
205     kASRC_StatusPairBOutputReady = ASRC_ASRSTR_AODFB_MASK, /*!< pair b output ready */
206     kASRC_StatusPairAOutputReady = ASRC_ASRSTR_AODFA_MASK, /*!< pair a output ready */
207     kASRC_StatusPairCInputReady  = ASRC_ASRSTR_AIDEC_MASK, /*!< pair c input ready */
208     kASRC_StatusPairBInputReady  = ASRC_ASRSTR_AIDEB_MASK, /*!< pair b input ready */
209     kASRC_StatusPairAInputReady  = ASRC_ASRSTR_AIDEA_MASK, /*!< pair a input ready */
210     kASRC_StatusPairAInterrupt   = kASRC_StatusPairAInputReady | kASRC_StatusPairAOutputReady, /*!< pair A interrupt */
211     kASRC_StatusPairBInterrupt   = kASRC_StatusPairBInputReady | kASRC_StatusPairBOutputReady, /*!< pair B interrupt */
212     kASRC_StatusPairCInterrupt   = kASRC_StatusPairCInputReady | kASRC_StatusPairCOutputReady, /*!< pair C interrupt */
213 
214 };
215 
216 /*! @brief ASRC channel pair status
217  * @anchor _asrc_channel_pair_status
218  */
219 enum
220 {
221     kASRC_OutputFifoNearFull = ASRC_ASRFSTA_OAFA_MASK, /*!< channel pair output fifo near full */
222     kASRC_InputFifoNearEmpty = ASRC_ASRFSTA_IAEA_MASK, /*!< channel pair input fifo near empty */
223 };
224 
225 /*! @brief ASRC ideal ratio */
226 typedef enum _asrc_ratio
227 {
228     kASRC_RatioNotUsed = 0U, /*!< ideal ratio not used */
229     kASRC_RatioUseInternalMeasured =
230         2U, /*!< ideal ratio use internal measure ratio, can be used for real time streaming audio */
231     kASRC_RatioUseIdealRatio =
232         3U, /*!< ideal ratio use manual configure ratio, can be used for the non-real time streaming audio */
233 } asrc_ratio_t;
234 
235 /*! @brief Number of channels in audio data */
236 typedef enum _asrc_audio_channel
237 {
238     kASRC_ChannelsNumber1  = 1U,  /*!< channel number is 1 */
239     kASRC_ChannelsNumber2  = 2U,  /*!< channel number is 2 */
240     kASRC_ChannelsNumber3  = 3U,  /*!< channel number is 3 */
241     kASRC_ChannelsNumber4  = 4U,  /*!< channel number is 4 */
242     kASRC_ChannelsNumber5  = 5U,  /*!< channel number is 5 */
243     kASRC_ChannelsNumber6  = 6U,  /*!< channel number is 6 */
244     kASRC_ChannelsNumber7  = 7U,  /*!< channel number is 7 */
245     kASRC_ChannelsNumber8  = 8U,  /*!< channel number is 8 */
246     kASRC_ChannelsNumber9  = 9U,  /*!< channel number is 9 */
247     kASRC_ChannelsNumber10 = 10U, /*!< channel number is 10 */
248 } asrc_audio_channel_t;
249 
250 /*! @brief data width */
251 typedef enum _asrc_data_width
252 {
253     kASRC_DataWidth24Bit = 0U, /*!< data width 24bit */
254     kASRC_DataWidth16Bit = 1U, /*!< data width 16bit */
255     kASRC_DataWidth8Bit  = 2U, /*!< data width 8bit */
256 } asrc_data_width_t;
257 
258 /*! @brief data alignment */
259 typedef enum _asrc_data_align
260 {
261     kASRC_DataAlignMSB = 1U, /*!< data alignment MSB */
262     kASRC_DataAlignLSB = 0U, /*!< data alignment LSB */
263 } asrc_data_align_t;
264 
265 /*! @brief sign extension */
266 typedef enum _asrc_sign_extension
267 {
268     kASRC_NoSignExtension = 0U, /*!< no sign extension */
269     kASRC_SignExtension   = 1U, /*!< sign extension */
270 } asrc_sign_extension_t;
271 
272 /*! @brief asrc channel pair configuation */
273 typedef struct _asrc_channel_pair_config
274 {
275     asrc_audio_channel_t audioDataChannels; /*!< audio data channel numbers */
276     asrc_clock_source_t
277         inClockSource;         /*!< input clock source, reference the clock source definition in SOC header file */
278     uint32_t inSourceClock_Hz; /*!< input source clock frequency */
279 
280     asrc_clock_source_t
281         outClockSource;         /*!< output clock source, reference the clock source definition in SOC header file */
282     uint32_t outSourceClock_Hz; /*!< output source clock frequency */
283 
284     asrc_ratio_t sampleRateRatio; /*!< sample rate ratio type */
285 
286     asrc_data_width_t inDataWidth; /*!< input data width */
287     asrc_data_align_t inDataAlign; /*!< input data alignment */
288 
289     asrc_data_width_t outDataWidth;         /*!< output data width */
290     asrc_data_align_t outDataAlign;         /*!< output data alignment */
291     asrc_sign_extension_t outSignExtension; /*!< output extension */
292 
293     uint8_t outFifoThreshold; /*!< output fifo threshold */
294     uint8_t inFifoThreshold;  /*!< input fifo threshold */
295 
296     bool bufStallWhenFifoEmptyFull; /*!< stall Pair A conversion in case of Buffer near empty full condition */
297 
298 } asrc_channel_pair_config_t;
299 
300 /*! @brief SAI transfer structure */
301 typedef struct _asrc_transfer
302 {
303     void *inData;       /*!< Data address to convert.*/
304     size_t inDataSize;  /*!< input data size. */
305     void *outData;      /*!< Data address to store converted data */
306     size_t outDataSize; /*!< output data size. */
307 } asrc_transfer_t;
308 
309 /*! @brief asrc handler */
310 typedef struct _asrc_handle asrc_handle_t;
311 
312 /*! @brief ASRC transfer callback prototype */
313 typedef void (*asrc_transfer_callback_t)(ASRC_Type *base, asrc_handle_t *handle, status_t status, void *userData);
314 
315 /*! @brief asrc in handler */
316 typedef struct _asrc_in_handle
317 {
318     asrc_transfer_callback_t callback;            /*!< Callback function called at convert complete */
319     uint32_t sampleWidth;                         /*!< data width */
320     uint32_t sampleMask;                          /*!< data mask */
321     uint32_t fifoThreshold;                       /*!< fifo threshold */
322     uint8_t *asrcQueue[ASRC_XFER_QUEUE_SIZE];     /*!< Transfer queue storing queued transfer */
323     size_t transferSamples[ASRC_XFER_QUEUE_SIZE]; /*!< Data bytes need to convert */
324     volatile uint8_t queueUser;                   /*!< Index for user to queue transfer */
325     volatile uint8_t queueDriver;                 /*!< Index for driver to get the transfer data and size */
326 } asrc_in_handle_t;
327 
328 /*! @brief output handler */
329 typedef struct _asrc_out_handle
330 {
331     asrc_transfer_callback_t callback;            /*!< Callback function called at convert complete */
332     uint32_t sampleWidth;                         /*!< data width */
333     uint32_t fifoThreshold;                       /*!< fifo threshold */
334     uint8_t *asrcQueue[ASRC_XFER_QUEUE_SIZE];     /*!< Transfer queue storing queued transfer */
335     size_t transferSamples[ASRC_XFER_QUEUE_SIZE]; /*!< Data bytes need to convert */
336     volatile uint8_t queueUser;                   /*!< Index for user to queue transfer */
337     volatile uint8_t queueDriver;                 /*!< Index for driver to get the transfer data and size */
338 } asrc_out_handle_t;
339 
340 /*! @brief ASRC handle structure */
341 struct _asrc_handle
342 {
343     ASRC_Type *base; /*!< base address */
344 
345     uint32_t state; /*!< Transfer status */
346     void *userData; /*!< Callback parameter passed to callback function*/
347 
348     asrc_audio_channel_t audioDataChannels; /*!< audio channel number */
349     asrc_channel_pair_t channelPair;        /*!< channel pair mask */
350 
351     asrc_in_handle_t in;   /*!< asrc input handler */
352     asrc_out_handle_t out; /*!< asrc output handler */
353 };
354 /*******************************************************************************
355  * API
356  ******************************************************************************/
357 
358 #if defined(__cplusplus)
359 extern "C" {
360 #endif /*_cplusplus*/
361 
362 /*!
363  * @name Initialization and deinitialization
364  * @{
365  */
366 
367 /*!
368  * @brief Get instance number of the ASRC peripheral.
369  *
370  * @param base ASRC base pointer.
371  */
372 uint32_t ASRC_GetInstance(ASRC_Type *base);
373 
374 /*!
375  * brief Initializes the asrc peripheral.
376  *
377  * This API gates the asrc clock. The asrc module can't operate unless ASRC_Init is called to enable the clock.
378  *
379  * param base asrc base pointer.
380  * param asrcPeripheralClock_Hz peripheral clock of ASRC.
381  */
382 void ASRC_Init(ASRC_Type *base, uint32_t asrcPeripheralClock_Hz);
383 
384 /*!
385  * @brief De-initializes the ASRC peripheral.
386  *
387  * This API gates the ASRC clock and disable ASRC module. The ASRC module can't operate unless ASRC_Init
388  *
389  * @param base ASRC base pointer.
390  */
391 void ASRC_Deinit(ASRC_Type *base);
392 
393 /*!
394  * @brief Do software reset .
395  *
396  * This software reset bit is self-clear bit, it will generate a software reset signal inside ASRC.
397  * After 9 cycles of the ASRC processing clock, this reset process will stop and this bit will cleared
398  * automatically.
399  *
400  * @param base ASRC base pointer
401  */
402 void ASRC_SoftwareReset(ASRC_Type *base);
403 
404 /*!
405  * @brief ASRC configure channel pair.
406  *
407  * @param base ASRC base pointer.
408  * @param channelPair index of channel pair, reference _asrc_channel_pair.
409  * @param config ASRC channel pair configuration pointer.
410  * @param inputSampleRate input audio data sample rate.
411  * @param outputSampleRate output audio data sample rate.
412  */
413 status_t ASRC_SetChannelPairConfig(ASRC_Type *base,
414                                    asrc_channel_pair_t channelPair,
415                                    asrc_channel_pair_config_t *config,
416                                    uint32_t inputSampleRate,
417                                    uint32_t outputSampleRate);
418 
419 /*!
420  * @brief Get output sample buffer size.
421  *
422  * @note This API is depends on the ASRC output configuration, should be called after the ASRC_SetChannelPairConfig.
423  *
424  * @param base asrc base pointer.
425  * @param channelPair ASRC channel pair number.
426  * @param inSampleRate input sample rate.
427  * @param outSampleRate output sample rate.
428  * @param inSamplesize input sampleS size.
429  * @retval output buffer size in byte.
430  */
431 uint32_t ASRC_GetOutSamplesSize(ASRC_Type *base,
432                                 asrc_channel_pair_t channelPair,
433                                 uint32_t inSampleRate,
434                                 uint32_t outSampleRate,
435                                 uint32_t inSamplesize);
436 
437 /*!
438  * @brief Map register sample width to real sample width.
439  *
440  * @note This API is depends on the ASRC configuration, should be called after the ASRC_SetChannelPairConfig.
441  * @param base asrc base pointer.
442  * @param channelPair asrc channel pair index.
443  * @param inWidth ASRC channel pair number.
444  * @param outWidth input sample rate.
445  * @retval input sample mask value.
446  */
447 uint32_t ASRC_MapSamplesWidth(ASRC_Type *base, asrc_channel_pair_t channelPair, uint32_t *inWidth, uint32_t *outWidth);
448 
449 /*!
450  * @brief Get left samples in fifo.
451  *
452  * @param base asrc base pointer.
453  * @param channelPair ASRC channel pair number.
454  * @param buffer input sample numbers.
455  * @param outSampleWidth output sample width.
456  * @param remainSamples output sample rate.
457  * @retval remain samples number.
458  */
459 uint32_t ASRC_GetRemainFifoSamples(ASRC_Type *base,
460                                    asrc_channel_pair_t channelPair,
461                                    uint32_t *buffer,
462                                    uint32_t outSampleWidth,
463                                    uint32_t remainSamples);
464 
465 /*!
466  * @brief ASRC module enable.
467  *
468  * @param base ASRC base pointer.
469  * @param enable true is enable, false is disable
470  */
ASRC_ModuleEnable(ASRC_Type * base,bool enable)471 static inline void ASRC_ModuleEnable(ASRC_Type *base, bool enable)
472 {
473     if (enable)
474     {
475         base->ASRCTR |= ASRC_ASRCTR_ASRCEN_MASK;
476     }
477     else
478     {
479         base->ASRCTR &= ~ASRC_ASRCTR_ASRCEN_MASK;
480     }
481 }
482 
483 /*!
484  * @brief ASRC enable channel pair.
485  *
486  * @param base ASRC base pointer.
487  * @param channelPair channel pair mask value, reference _asrc_channel_pair_mask.
488  * @param enable true is enable, false is disable.
489  */
ASRC_ChannelPairEnable(ASRC_Type * base,asrc_channel_pair_t channelPair,bool enable)490 static inline void ASRC_ChannelPairEnable(ASRC_Type *base, asrc_channel_pair_t channelPair, bool enable)
491 {
492     if (enable)
493     {
494         base->ASRCTR |= 1UL << ((uint32_t)channelPair + 1U);
495     }
496     else
497     {
498         base->ASRCTR &= ~(1UL << ((uint32_t)channelPair + 1U));
499     }
500 }
501 /*! @} */
502 
503 /*!
504  * @name Interrupts
505  * @{
506  */
507 
508 /*!
509  * @brief ASRC interrupt enable
510  * This function enable the ASRC interrupt with the provided mask.
511  *
512  * @param base ASRC peripheral base address.
513  * @param mask The interrupts to enable. Logical OR of @ref _asrc_interrupt_mask.
514  */
ASRC_EnableInterrupt(ASRC_Type * base,uint32_t mask)515 static inline void ASRC_EnableInterrupt(ASRC_Type *base, uint32_t mask)
516 {
517     base->ASRIER |= mask;
518 }
519 
520 /*!
521  * @brief ASRC interrupt disable
522  * This function disable the ASRC interrupt with the provided mask.
523  *
524  * @param base ASRC peripheral base address.
525  * @param mask The interrupts to disable. Logical OR of @ref _asrc_interrupt_mask.
526  */
ASRC_DisableInterrupt(ASRC_Type * base,uint32_t mask)527 static inline void ASRC_DisableInterrupt(ASRC_Type *base, uint32_t mask)
528 {
529     base->ASRIER &= ~mask;
530 }
531 
532 /*! @} */
533 
534 /*!
535  * @name Status
536  * @{
537  */
538 
539 /*!
540  * @brief Gets the ASRC status flag state.
541  *
542  * @param base ASRC base pointer
543  * @return ASRC Tx status flag value. Use the Status Mask to get the status value needed.
544  */
ASRC_GetStatus(ASRC_Type * base)545 static inline uint32_t ASRC_GetStatus(ASRC_Type *base)
546 {
547     return base->ASRSTR;
548 }
549 
550 /*!
551  * @brief Gets the ASRC channel pair initialization state.
552  *
553  * @param base ASRC base pointer
554  * @param channel ASRC channel pair.
555  * @return ASRC Tx status flag value. Use the Status Mask to get the status value needed.
556  */
ASRC_GetChannelPairInitialStatus(ASRC_Type * base,asrc_channel_pair_t channel)557 static inline bool ASRC_GetChannelPairInitialStatus(ASRC_Type *base, asrc_channel_pair_t channel)
558 {
559     return ((base->ASRCFG >> ASRC_ASRCFG_INIRQA_SHIFT) & (1U << (uint32_t)channel)) == 0U ? false : true;
560 }
561 
562 /*!
563  * @brief Gets the ASRC channel A fifo a status flag state.
564  *
565  * @param base ASRC base pointer
566  * @param channelPair ASRC channel pair.
567  * @return ASRC channel pair a fifo status flag value. Use the Status Mask to get the status value needed.
568  */
ASRC_GetChannelPairFifoStatus(ASRC_Type * base,asrc_channel_pair_t channelPair)569 static inline uint32_t ASRC_GetChannelPairFifoStatus(ASRC_Type *base, asrc_channel_pair_t channelPair)
570 {
571     return ASRC_ASRMCR(base, channelPair) & ((uint32_t)kASRC_OutputFifoNearFull | (uint32_t)kASRC_InputFifoNearEmpty);
572 }
573 
574 /*! @} */
575 
576 /*!
577  * @name Bus Operations
578  * @{
579  */
580 
581 /*!
582  * @brief Writes data into ASRC channel pair FIFO.
583  * Note: ASRC fifo width is 24bit.
584  * @param base ASRC base pointer.
585  * @param channelPair ASRC channel pair.
586  * @param data Data needs to be written.
587  */
ASRC_ChannelPairWriteData(ASRC_Type * base,asrc_channel_pair_t channelPair,uint32_t data)588 static inline void ASRC_ChannelPairWriteData(ASRC_Type *base, asrc_channel_pair_t channelPair, uint32_t data)
589 {
590     ASRC_ASRDI(base, channelPair) = data;
591 }
592 
593 /*!
594  * @brief Read data from ASRC channel pair FIFO.
595  * Note: ASRC fifo width is 24bit.
596  *
597  * @param base ASRC base pointer.
598  * @param channelPair ASRC channel pair.
599  * @retval value read from fifo.
600  */
ASRC_ChannelPairReadData(ASRC_Type * base,asrc_channel_pair_t channelPair)601 static inline uint32_t ASRC_ChannelPairReadData(ASRC_Type *base, asrc_channel_pair_t channelPair)
602 {
603     return ASRC_ASRDO(base, channelPair);
604 }
605 
606 /*!
607  * @brief Get input data fifo address.
608  * Note: ASRC fifo width is 24bit.
609  *
610  * @param base ASRC base pointer.
611  * @param channelPair ASRC channel pair.
612  */
ASRC_GetInputDataRegisterAddress(ASRC_Type * base,asrc_channel_pair_t channelPair)613 static inline uint32_t ASRC_GetInputDataRegisterAddress(ASRC_Type *base, asrc_channel_pair_t channelPair)
614 {
615     return (uint32_t)ASRC_ASRDI_ADDR(base, channelPair);
616 }
617 
618 /*!
619  * @brief Get output data fifo address.
620  * Note: ASRC fifo width is 24bit.
621  *
622  * @param base ASRC base pointer.
623  * @param channelPair ASRC channel pair.
624  */
ASRC_GetOutputDataRegisterAddress(ASRC_Type * base,asrc_channel_pair_t channelPair)625 static inline uint32_t ASRC_GetOutputDataRegisterAddress(ASRC_Type *base, asrc_channel_pair_t channelPair)
626 {
627     return (uint32_t)ASRC_ASRDO_ADDR(base, channelPair);
628 }
629 
630 /*!
631  * @brief ASRC configure ideal ratio.
632  * The ideal ratio should be used when input clock source is not avalible.
633  *
634  * @param base ASRC base pointer.
635  * @param channelPair ASRC channel pair.
636  * @param inputSampleRate input audio data sample rate.
637  * @param outputSampleRate output audio data sample rate.
638  */
639 status_t ASRC_SetIdealRatioConfig(ASRC_Type *base,
640                                   asrc_channel_pair_t channelPair,
641                                   uint32_t inputSampleRate,
642                                   uint32_t outputSampleRate);
643 
644 /*! @} */
645 
646 /*!
647  * @name Transactional
648  * @{
649  */
650 
651 /*!
652  * @brief ASRC configure channel pair.
653  *
654  * @param base ASRC base pointer.
655  * @param handle ASRC transactional handle pointer.
656  * @param config ASRC channel pair configuration pointer.
657  * @param inputSampleRate input audio data sample rate.
658  * @param outputSampleRate output audio data sample rate.
659  */
660 status_t ASRC_TransferSetChannelPairConfig(ASRC_Type *base,
661                                            asrc_handle_t *handle,
662                                            asrc_channel_pair_config_t *config,
663                                            uint32_t inputSampleRate,
664                                            uint32_t outputSampleRate);
665 
666 /*!
667  * @brief Initializes the ASRC handle.
668  *
669  * This function initializes the handle for the ASRC transactional APIs. Call
670  * this function once to get the handle initialized.
671  *
672  * @param base ASRC base pointer
673  * @param handle ASRC handle pointer.
674  * @param channelPair ASRC channel pair.
675  * @param inCallback Pointer to the user callback function.
676  * @param outCallback Pointer to the user callback function.
677  * @param userData User parameter passed to the callback function
678  */
679 void ASRC_TransferCreateHandle(ASRC_Type *base,
680                                asrc_handle_t *handle,
681                                asrc_channel_pair_t channelPair,
682                                asrc_transfer_callback_t inCallback,
683                                asrc_transfer_callback_t outCallback,
684                                void *userData);
685 
686 /*!
687  * @brief Performs an interrupt non-blocking convert on asrc.
688  *
689  * @note This API returns immediately after the transfer initiates, application should check the wait and check the
690  * callback status.
691  *
692  * @param base asrc base pointer.
693  * @param handle Pointer to the asrc_handle_t structure which stores the transfer state.
694  * @param xfer Pointer to the ASRC_transfer_t structure.
695  * @retval kStatus_Success Successfully started the data receive.
696  * @retval kStatus_ASRCBusy Previous receive still not finished.
697  */
698 status_t ASRC_TransferNonBlocking(ASRC_Type *base, asrc_handle_t *handle, asrc_transfer_t *xfer);
699 
700 /*!
701  * @brief Performs an blocking convert on asrc.
702  *
703  * @note This API returns immediately after the convert finished.
704  *
705  * @param base asrc base pointer.
706  * @param channelPair channel pair index.
707  * @param xfer Pointer to the ASRC_transfer_t structure.
708  * @retval kStatus_Success Successfully started the data receive.
709  */
710 status_t ASRC_TransferBlocking(ASRC_Type *base, asrc_channel_pair_t channelPair, asrc_transfer_t *xfer);
711 
712 /*!
713  * @brief Get converted byte count.
714  *
715  * @param base ASRC base pointer.
716  * @param handle Pointer to the asrc_handle_t structure which stores the transfer state.
717  * @param count Bytes count sent.
718  * @retval kStatus_Success Succeed get the transfer count.
719  * @retval kStatus_ASRCIdle There is not a non-blocking transaction currently in progress.
720  */
721 status_t ASRC_TransferGetConvertedCount(ASRC_Type *base, asrc_handle_t *handle, size_t *count);
722 
723 /*!
724  * @brief Aborts the current convert.
725  *
726  * @note This API can be called any time when an interrupt non-blocking transfer initiates
727  * to abort the transfer early.
728  *
729  * @param base ASRC base pointer.
730  * @param handle Pointer to the asrc_handle_t structure which stores the transfer state.
731  */
732 void ASRC_TransferAbortConvert(ASRC_Type *base, asrc_handle_t *handle);
733 
734 /*!
735  * @brief Terminate all ASRC convert.
736  *
737  * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
738  * current transfer slot, please call ASRC_TransferAbortConvert.
739  *
740  * @param base ASRC base pointer.
741  * @param handle ASRC eDMA handle pointer.
742  */
743 void ASRC_TransferTerminateConvert(ASRC_Type *base, asrc_handle_t *handle);
744 
745 /*!
746  * @brief ASRC convert interrupt handler.
747  *
748  * @param base ASRC base pointer.
749  * @param handle Pointer to the asrc_handle_t structure.
750  */
751 void ASRC_TransferHandleIRQ(ASRC_Type *base, asrc_handle_t *handle);
752 
753 /*! @} */
754 
755 #if defined(__cplusplus)
756 }
757 #endif /*_cplusplus*/
758 
759 /*! @} */
760 
761 #endif /* FSL_ASRC_H_ */
762