1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef _FSL_SAI_H_
32 #define _FSL_SAI_H_
33
34 #include "fsl_common.h"
35
36 /*!
37 * @addtogroup sai
38 * @{
39 */
40
41 /*******************************************************************************
42 * Definitions
43 ******************************************************************************/
44
45 /*! @name Driver version */
46 /*@{*/
47 #define FSL_SAI_DRIVER_VERSION (MAKE_VERSION(2, 1, 2)) /*!< Version 2.1.2 */
48 /*@}*/
49
50 /*! @brief SAI return status*/
51 enum _sai_status_t
52 {
53 kStatus_SAI_TxBusy = MAKE_STATUS(kStatusGroup_SAI, 0), /*!< SAI Tx is busy. */
54 kStatus_SAI_RxBusy = MAKE_STATUS(kStatusGroup_SAI, 1), /*!< SAI Rx is busy. */
55 kStatus_SAI_TxError = MAKE_STATUS(kStatusGroup_SAI, 2), /*!< SAI Tx FIFO error. */
56 kStatus_SAI_RxError = MAKE_STATUS(kStatusGroup_SAI, 3), /*!< SAI Rx FIFO error. */
57 kStatus_SAI_QueueFull = MAKE_STATUS(kStatusGroup_SAI, 4), /*!< SAI transfer queue is full. */
58 kStatus_SAI_TxIdle = MAKE_STATUS(kStatusGroup_SAI, 5), /*!< SAI Tx is idle */
59 kStatus_SAI_RxIdle = MAKE_STATUS(kStatusGroup_SAI, 6) /*!< SAI Rx is idle */
60 };
61
62 /*! @brief Define the SAI bus type */
63 typedef enum _sai_protocol
64 {
65 kSAI_BusLeftJustified = 0x0U, /*!< Uses left justified format.*/
66 kSAI_BusRightJustified, /*!< Uses right justified format. */
67 kSAI_BusI2S, /*!< Uses I2S format. */
68 kSAI_BusPCMA, /*!< Uses I2S PCM A format.*/
69 kSAI_BusPCMB /*!< Uses I2S PCM B format. */
70 } sai_protocol_t;
71
72 /*! @brief Master or slave mode */
73 typedef enum _sai_master_slave
74 {
75 kSAI_Master = 0x0U, /*!< Master mode */
76 kSAI_Slave = 0x1U /*!< Slave mode */
77 } sai_master_slave_t;
78
79 /*! @brief Mono or stereo audio format */
80 typedef enum _sai_mono_stereo
81 {
82 kSAI_Stereo = 0x0U, /*!< Stereo sound. */
83 kSAI_MonoLeft, /*!< Only left channel have sound. */
84 kSAI_MonoRight /*!< Only Right channel have sound. */
85 } sai_mono_stereo_t;
86
87 /*! @brief Synchronous or asynchronous mode */
88 typedef enum _sai_sync_mode
89 {
90 kSAI_ModeAsync = 0x0U, /*!< Asynchronous mode */
91 kSAI_ModeSync, /*!< Synchronous mode (with receiver or transmit) */
92 kSAI_ModeSyncWithOtherTx, /*!< Synchronous with another SAI transmit */
93 kSAI_ModeSyncWithOtherRx /*!< Synchronous with another SAI receiver */
94 } sai_sync_mode_t;
95
96 /*! @brief Mater clock source */
97 typedef enum _sai_mclk_source
98 {
99 kSAI_MclkSourceSysclk = 0x0U, /*!< Master clock from the system clock */
100 kSAI_MclkSourceSelect1, /*!< Master clock from source 1 */
101 kSAI_MclkSourceSelect2, /*!< Master clock from source 2 */
102 kSAI_MclkSourceSelect3 /*!< Master clock from source 3 */
103 } sai_mclk_source_t;
104
105 /*! @brief Bit clock source */
106 typedef enum _sai_bclk_source
107 {
108 kSAI_BclkSourceBusclk = 0x0U, /*!< Bit clock using bus clock */
109 kSAI_BclkSourceMclkDiv, /*!< Bit clock using master clock divider */
110 kSAI_BclkSourceOtherSai0, /*!< Bit clock from other SAI device */
111 kSAI_BclkSourceOtherSai1 /*!< Bit clock from other SAI device */
112 } sai_bclk_source_t;
113
114 /*! @brief The SAI interrupt enable flag */
115 enum _sai_interrupt_enable_t
116 {
117 kSAI_WordStartInterruptEnable =
118 I2S_TCSR_WSIE_MASK, /*!< Word start flag, means the first word in a frame detected */
119 kSAI_SyncErrorInterruptEnable = I2S_TCSR_SEIE_MASK, /*!< Sync error flag, means the sync error is detected */
120 kSAI_FIFOWarningInterruptEnable = I2S_TCSR_FWIE_MASK, /*!< FIFO warning flag, means the FIFO is empty */
121 kSAI_FIFOErrorInterruptEnable = I2S_TCSR_FEIE_MASK, /*!< FIFO error flag */
122 #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
123 kSAI_FIFORequestInterruptEnable = I2S_TCSR_FRIE_MASK, /*!< FIFO request, means reached watermark */
124 #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
125 };
126
127 /*! @brief The DMA request sources */
128 enum _sai_dma_enable_t
129 {
130 kSAI_FIFOWarningDMAEnable = I2S_TCSR_FWDE_MASK, /*!< FIFO warning caused by the DMA request */
131 #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
132 kSAI_FIFORequestDMAEnable = I2S_TCSR_FRDE_MASK, /*!< FIFO request caused by the DMA request */
133 #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
134 };
135
136 /*! @brief The SAI status flag */
137 enum _sai_flags
138 {
139 kSAI_WordStartFlag = I2S_TCSR_WSF_MASK, /*!< Word start flag, means the first word in a frame detected */
140 kSAI_SyncErrorFlag = I2S_TCSR_SEF_MASK, /*!< Sync error flag, means the sync error is detected */
141 kSAI_FIFOErrorFlag = I2S_TCSR_FEF_MASK, /*!< FIFO error flag */
142 #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
143 kSAI_FIFORequestFlag = I2S_TCSR_FRF_MASK, /*!< FIFO request flag. */
144 #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
145 kSAI_FIFOWarningFlag = I2S_TCSR_FWF_MASK, /*!< FIFO warning flag */
146 };
147
148 /*! @brief The reset type */
149 typedef enum _sai_reset_type
150 {
151 kSAI_ResetTypeSoftware = I2S_TCSR_SR_MASK, /*!< Software reset, reset the logic state */
152 kSAI_ResetTypeFIFO = I2S_TCSR_FR_MASK, /*!< FIFO reset, reset the FIFO read and write pointer */
153 kSAI_ResetAll = I2S_TCSR_SR_MASK | I2S_TCSR_FR_MASK /*!< All reset. */
154 } sai_reset_type_t;
155
156 #if defined(FSL_FEATURE_SAI_HAS_FIFO_PACKING) && FSL_FEATURE_SAI_HAS_FIFO_PACKING
157 /*!
158 * @brief The SAI packing mode
159 * The mode includes 8 bit and 16 bit packing.
160 */
161 typedef enum _sai_fifo_packing
162 {
163 kSAI_FifoPackingDisabled = 0x0U, /*!< Packing disabled */
164 kSAI_FifoPacking8bit = 0x2U, /*!< 8 bit packing enabled */
165 kSAI_FifoPacking16bit = 0x3U /*!< 16bit packing enabled */
166 } sai_fifo_packing_t;
167 #endif /* FSL_FEATURE_SAI_HAS_FIFO_PACKING */
168
169 /*! @brief SAI user configuration structure */
170 typedef struct _sai_config
171 {
172 sai_protocol_t protocol; /*!< Audio bus protocol in SAI */
173 sai_sync_mode_t syncMode; /*!< SAI sync mode, control Tx/Rx clock sync */
174 #if defined(FSL_FEATURE_SAI_HAS_MCR) && (FSL_FEATURE_SAI_HAS_MCR)
175 bool mclkOutputEnable; /*!< Master clock output enable, true means master clock divider enabled */
176 #endif /* FSL_FEATURE_SAI_HAS_MCR */
177 sai_mclk_source_t mclkSource; /*!< Master Clock source */
178 sai_bclk_source_t bclkSource; /*!< Bit Clock source */
179 sai_master_slave_t masterSlave; /*!< Master or slave */
180 } sai_config_t;
181
182 /*!@brief SAI transfer queue size, user can refine it according to use case. */
183 #define SAI_XFER_QUEUE_SIZE (4)
184
185 /*! @brief Audio sample rate */
186 typedef enum _sai_sample_rate
187 {
188 kSAI_SampleRate8KHz = 8000U, /*!< Sample rate 8000 Hz */
189 kSAI_SampleRate11025Hz = 11025U, /*!< Sample rate 11025 Hz */
190 kSAI_SampleRate12KHz = 12000U, /*!< Sample rate 12000 Hz */
191 kSAI_SampleRate16KHz = 16000U, /*!< Sample rate 16000 Hz */
192 kSAI_SampleRate22050Hz = 22050U, /*!< Sample rate 22050 Hz */
193 kSAI_SampleRate24KHz = 24000U, /*!< Sample rate 24000 Hz */
194 kSAI_SampleRate32KHz = 32000U, /*!< Sample rate 32000 Hz */
195 kSAI_SampleRate44100Hz = 44100U, /*!< Sample rate 44100 Hz */
196 kSAI_SampleRate48KHz = 48000U, /*!< Sample rate 48000 Hz */
197 kSAI_SampleRate96KHz = 96000U /*!< Sample rate 96000 Hz */
198 } sai_sample_rate_t;
199
200 /*! @brief Audio word width */
201 typedef enum _sai_word_width
202 {
203 kSAI_WordWidth8bits = 8U, /*!< Audio data width 8 bits */
204 kSAI_WordWidth16bits = 16U, /*!< Audio data width 16 bits */
205 kSAI_WordWidth24bits = 24U, /*!< Audio data width 24 bits */
206 kSAI_WordWidth32bits = 32U /*!< Audio data width 32 bits */
207 } sai_word_width_t;
208
209 /*! @brief sai transfer format */
210 typedef struct _sai_transfer_format
211 {
212 uint32_t sampleRate_Hz; /*!< Sample rate of audio data */
213 uint32_t bitWidth; /*!< Data length of audio data, usually 8/16/24/32 bits */
214 sai_mono_stereo_t stereo; /*!< Mono or stereo */
215 uint32_t masterClockHz; /*!< Master clock frequency in Hz */
216 #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
217 uint8_t watermark; /*!< Watermark value */
218 #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
219 uint8_t channel; /*!< Data channel used in transfer.*/
220 sai_protocol_t protocol; /*!< Which audio protocol used */
221 } sai_transfer_format_t;
222
223 /*! @brief SAI transfer structure */
224 typedef struct _sai_transfer
225 {
226 uint8_t *data; /*!< Data start address to transfer. */
227 size_t dataSize; /*!< Transfer size. */
228 } sai_transfer_t;
229
230 typedef struct _sai_handle sai_handle_t;
231
232 /*! @brief SAI transfer callback prototype */
233 typedef void (*sai_transfer_callback_t)(I2S_Type *base, sai_handle_t *handle, status_t status, void *userData);
234
235 /*! @brief SAI handle structure */
236 struct _sai_handle
237 {
238 uint32_t state; /*!< Transfer status */
239 sai_transfer_callback_t callback; /*!< Callback function called at transfer event*/
240 void *userData; /*!< Callback parameter passed to callback function*/
241 uint8_t bitWidth; /*!< Bit width for transfer, 8/16/24/32 bits */
242 uint8_t channel; /*!< Transfer channel */
243 sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer */
244 size_t transferSize[SAI_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
245 volatile uint8_t queueUser; /*!< Index for user to queue transfer */
246 volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
247 #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
248 uint8_t watermark; /*!< Watermark value */
249 #endif
250 };
251
252 /*******************************************************************************
253 * API
254 ******************************************************************************/
255
256 #if defined(__cplusplus)
257 extern "C" {
258 #endif /*_cplusplus*/
259
260 /*!
261 * @name Initialization and deinitialization
262 * @{
263 */
264
265 /*!
266 * @brief Initializes the SAI Tx peripheral.
267 *
268 * Ungates the SAI clock, resets the module, and configures SAI Tx with a configuration structure.
269 * The configuration structure can be custom filled or set with default values by
270 * SAI_TxGetDefaultConfig().
271 *
272 * @note This API should be called at the beginning of the application to use
273 * the SAI driver. Otherwise, accessing the SAIM module can cause a hard fault
274 * because the clock is not enabled.
275 *
276 * @param base SAI base pointer
277 * @param config SAI configuration structure.
278 */
279 void SAI_TxInit(I2S_Type *base, const sai_config_t *config);
280
281 /*!
282 * @brief Initializes the the SAI Rx peripheral.
283 *
284 * Ungates the SAI clock, resets the module, and configures the SAI Rx with a configuration structure.
285 * The configuration structure can be custom filled or set with default values by
286 * SAI_RxGetDefaultConfig().
287 *
288 * @note This API should be called at the beginning of the application to use
289 * the SAI driver. Otherwise, accessing the SAI module can cause a hard fault
290 * because the clock is not enabled.
291 *
292 * @param base SAI base pointer
293 * @param config SAI configuration structure.
294 */
295 void SAI_RxInit(I2S_Type *base, const sai_config_t *config);
296
297 /*!
298 * @brief Sets the SAI Tx configuration structure to default values.
299 *
300 * This API initializes the configuration structure for use in SAI_TxConfig().
301 * The initialized structure can remain unchanged in SAI_TxConfig(), or it can be modified
302 * before calling SAI_TxConfig().
303 * This is an example.
304 @code
305 sai_config_t config;
306 SAI_TxGetDefaultConfig(&config);
307 @endcode
308 *
309 * @param config pointer to master configuration structure
310 */
311 void SAI_TxGetDefaultConfig(sai_config_t *config);
312
313 /*!
314 * @brief Sets the SAI Rx configuration structure to default values.
315 *
316 * This API initializes the configuration structure for use in SAI_RxConfig().
317 * The initialized structure can remain unchanged in SAI_RxConfig() or it can be modified
318 * before calling SAI_RxConfig().
319 * This is an example.
320 @code
321 sai_config_t config;
322 SAI_RxGetDefaultConfig(&config);
323 @endcode
324 *
325 * @param config pointer to master configuration structure
326 */
327 void SAI_RxGetDefaultConfig(sai_config_t *config);
328
329 /*!
330 * @brief De-initializes the SAI peripheral.
331 *
332 * This API gates the SAI clock. The SAI module can't operate unless SAI_TxInit
333 * or SAI_RxInit is called to enable the clock.
334 *
335 * @param base SAI base pointer
336 */
337 void SAI_Deinit(I2S_Type *base);
338
339 /*!
340 * @brief Resets the SAI Tx.
341 *
342 * This function enables the software reset and FIFO reset of SAI Tx. After reset, clear the reset bit.
343 *
344 * @param base SAI base pointer
345 */
346 void SAI_TxReset(I2S_Type *base);
347
348 /*!
349 * @brief Resets the SAI Rx.
350 *
351 * This function enables the software reset and FIFO reset of SAI Rx. After reset, clear the reset bit.
352 *
353 * @param base SAI base pointer
354 */
355 void SAI_RxReset(I2S_Type *base);
356
357 /*!
358 * @brief Enables/disables the SAI Tx.
359 *
360 * @param base SAI base pointer
361 * @param enable True means enable SAI Tx, false means disable.
362 */
363 void SAI_TxEnable(I2S_Type *base, bool enable);
364
365 /*!
366 * @brief Enables/disables the SAI Rx.
367 *
368 * @param base SAI base pointer
369 * @param enable True means enable SAI Rx, false means disable.
370 */
371 void SAI_RxEnable(I2S_Type *base, bool enable);
372
373 /*! @} */
374
375 /*!
376 * @name Status
377 * @{
378 */
379
380 /*!
381 * @brief Gets the SAI Tx status flag state.
382 *
383 * @param base SAI base pointer
384 * @return SAI Tx status flag value. Use the Status Mask to get the status value needed.
385 */
SAI_TxGetStatusFlag(I2S_Type * base)386 static inline uint32_t SAI_TxGetStatusFlag(I2S_Type *base)
387 {
388 return base->TCSR;
389 }
390
391 /*!
392 * @brief Clears the SAI Tx status flag state.
393 *
394 * @param base SAI base pointer
395 * @param mask State mask. It can be a combination of the following source if defined:
396 * @arg kSAI_WordStartFlag
397 * @arg kSAI_SyncErrorFlag
398 * @arg kSAI_FIFOErrorFlag
399 */
SAI_TxClearStatusFlags(I2S_Type * base,uint32_t mask)400 static inline void SAI_TxClearStatusFlags(I2S_Type *base, uint32_t mask)
401 {
402 base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
403 }
404
405 /*!
406 * @brief Gets the SAI Tx status flag state.
407 *
408 * @param base SAI base pointer
409 * @return SAI Rx status flag value. Use the Status Mask to get the status value needed.
410 */
SAI_RxGetStatusFlag(I2S_Type * base)411 static inline uint32_t SAI_RxGetStatusFlag(I2S_Type *base)
412 {
413 return base->RCSR;
414 }
415
416 /*!
417 * @brief Clears the SAI Rx status flag state.
418 *
419 * @param base SAI base pointer
420 * @param mask State mask. It can be a combination of the following sources if defined.
421 * @arg kSAI_WordStartFlag
422 * @arg kSAI_SyncErrorFlag
423 * @arg kSAI_FIFOErrorFlag
424 */
SAI_RxClearStatusFlags(I2S_Type * base,uint32_t mask)425 static inline void SAI_RxClearStatusFlags(I2S_Type *base, uint32_t mask)
426 {
427 base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
428 }
429
430 /*! @} */
431
432 /*!
433 * @name Interrupts
434 * @{
435 */
436
437 /*!
438 * @brief Enables the SAI Tx interrupt requests.
439 *
440 * @param base SAI base pointer
441 * @param mask interrupt source
442 * The parameter can be a combination of the following sources if defined.
443 * @arg kSAI_WordStartInterruptEnable
444 * @arg kSAI_SyncErrorInterruptEnable
445 * @arg kSAI_FIFOWarningInterruptEnable
446 * @arg kSAI_FIFORequestInterruptEnable
447 * @arg kSAI_FIFOErrorInterruptEnable
448 */
SAI_TxEnableInterrupts(I2S_Type * base,uint32_t mask)449 static inline void SAI_TxEnableInterrupts(I2S_Type *base, uint32_t mask)
450 {
451 base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
452 }
453
454 /*!
455 * @brief Enables the SAI Rx interrupt requests.
456 *
457 * @param base SAI base pointer
458 * @param mask interrupt source
459 * The parameter can be a combination of the following sources if defined.
460 * @arg kSAI_WordStartInterruptEnable
461 * @arg kSAI_SyncErrorInterruptEnable
462 * @arg kSAI_FIFOWarningInterruptEnable
463 * @arg kSAI_FIFORequestInterruptEnable
464 * @arg kSAI_FIFOErrorInterruptEnable
465 */
SAI_RxEnableInterrupts(I2S_Type * base,uint32_t mask)466 static inline void SAI_RxEnableInterrupts(I2S_Type *base, uint32_t mask)
467 {
468 base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
469 }
470
471 /*!
472 * @brief Disables the SAI Tx interrupt requests.
473 *
474 * @param base SAI base pointer
475 * @param mask interrupt source
476 * The parameter can be a combination of the following sources if defined.
477 * @arg kSAI_WordStartInterruptEnable
478 * @arg kSAI_SyncErrorInterruptEnable
479 * @arg kSAI_FIFOWarningInterruptEnable
480 * @arg kSAI_FIFORequestInterruptEnable
481 * @arg kSAI_FIFOErrorInterruptEnable
482 */
SAI_TxDisableInterrupts(I2S_Type * base,uint32_t mask)483 static inline void SAI_TxDisableInterrupts(I2S_Type *base, uint32_t mask)
484 {
485 base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
486 }
487
488 /*!
489 * @brief Disables the SAI Rx interrupt requests.
490 *
491 * @param base SAI base pointer
492 * @param mask interrupt source
493 * The parameter can be a combination of the following sources if defined.
494 * @arg kSAI_WordStartInterruptEnable
495 * @arg kSAI_SyncErrorInterruptEnable
496 * @arg kSAI_FIFOWarningInterruptEnable
497 * @arg kSAI_FIFORequestInterruptEnable
498 * @arg kSAI_FIFOErrorInterruptEnable
499 */
SAI_RxDisableInterrupts(I2S_Type * base,uint32_t mask)500 static inline void SAI_RxDisableInterrupts(I2S_Type *base, uint32_t mask)
501 {
502 base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
503 }
504
505 /*! @} */
506
507 /*!
508 * @name DMA Control
509 * @{
510 */
511
512 /*!
513 * @brief Enables/disables the SAI Tx DMA requests.
514 * @param base SAI base pointer
515 * @param mask DMA source
516 * The parameter can be combination of the following sources if defined.
517 * @arg kSAI_FIFOWarningDMAEnable
518 * @arg kSAI_FIFORequestDMAEnable
519 * @param enable True means enable DMA, false means disable DMA.
520 */
SAI_TxEnableDMA(I2S_Type * base,uint32_t mask,bool enable)521 static inline void SAI_TxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
522 {
523 if (enable)
524 {
525 base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
526 }
527 else
528 {
529 base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
530 }
531 }
532
533 /*!
534 * @brief Enables/disables the SAI Rx DMA requests.
535 * @param base SAI base pointer
536 * @param mask DMA source
537 * The parameter can be a combination of the following sources if defined.
538 * @arg kSAI_FIFOWarningDMAEnable
539 * @arg kSAI_FIFORequestDMAEnable
540 * @param enable True means enable DMA, false means disable DMA.
541 */
SAI_RxEnableDMA(I2S_Type * base,uint32_t mask,bool enable)542 static inline void SAI_RxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
543 {
544 if (enable)
545 {
546 base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
547 }
548 else
549 {
550 base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
551 }
552 }
553
554 /*!
555 * @brief Gets the SAI Tx data register address.
556 *
557 * This API is used to provide a transfer address for the SAI DMA transfer configuration.
558 *
559 * @param base SAI base pointer.
560 * @param channel Which data channel used.
561 * @return data register address.
562 */
SAI_TxGetDataRegisterAddress(I2S_Type * base,uint32_t channel)563 static inline uint32_t SAI_TxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
564 {
565 return (uint32_t)(&(base->TDR)[channel]);
566 }
567
568 /*!
569 * @brief Gets the SAI Rx data register address.
570 *
571 * This API is used to provide a transfer address for the SAI DMA transfer configuration.
572 *
573 * @param base SAI base pointer.
574 * @param channel Which data channel used.
575 * @return data register address.
576 */
SAI_RxGetDataRegisterAddress(I2S_Type * base,uint32_t channel)577 static inline uint32_t SAI_RxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
578 {
579 return (uint32_t)(&(base->RDR)[channel]);
580 }
581
582 /*! @} */
583
584 /*!
585 * @name Bus Operations
586 * @{
587 */
588
589 /*!
590 * @brief Configures the SAI Tx audio format.
591 *
592 * The audio format can be changed at run-time. This function configures the sample rate and audio data
593 * format to be transferred.
594 *
595 * @param base SAI base pointer.
596 * @param format Pointer to the SAI audio data format structure.
597 * @param mclkSourceClockHz SAI master clock source frequency in Hz.
598 * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
599 * clock, this value should equal the masterClockHz.
600 */
601 void SAI_TxSetFormat(I2S_Type *base,
602 sai_transfer_format_t *format,
603 uint32_t mclkSourceClockHz,
604 uint32_t bclkSourceClockHz);
605
606 /*!
607 * @brief Configures the SAI Rx audio format.
608 *
609 * The audio format can be changed at run-time. This function configures the sample rate and audio data
610 * format to be transferred.
611 *
612 * @param base SAI base pointer.
613 * @param format Pointer to the SAI audio data format structure.
614 * @param mclkSourceClockHz SAI master clock source frequency in Hz.
615 * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
616 * clock, this value should equal the masterClockHz.
617 */
618 void SAI_RxSetFormat(I2S_Type *base,
619 sai_transfer_format_t *format,
620 uint32_t mclkSourceClockHz,
621 uint32_t bclkSourceClockHz);
622
623 /*!
624 * @brief Sends data using a blocking method.
625 *
626 * @note This function blocks by polling until data is ready to be sent.
627 *
628 * @param base SAI base pointer.
629 * @param channel Data channel used.
630 * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
631 * @param buffer Pointer to the data to be written.
632 * @param size Bytes to be written.
633 */
634 void SAI_WriteBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
635
636 /*!
637 * @brief Writes data into SAI FIFO.
638 *
639 * @param base SAI base pointer.
640 * @param channel Data channel used.
641 * @param data Data needs to be written.
642 */
SAI_WriteData(I2S_Type * base,uint32_t channel,uint32_t data)643 static inline void SAI_WriteData(I2S_Type *base, uint32_t channel, uint32_t data)
644 {
645 base->TDR[channel] = data;
646 }
647
648 /*!
649 * @brief Receives data using a blocking method.
650 *
651 * @note This function blocks by polling until data is ready to be sent.
652 *
653 * @param base SAI base pointer.
654 * @param channel Data channel used.
655 * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
656 * @param buffer Pointer to the data to be read.
657 * @param size Bytes to be read.
658 */
659 void SAI_ReadBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
660
661 /*!
662 * @brief Reads data from the SAI FIFO.
663 *
664 * @param base SAI base pointer.
665 * @param channel Data channel used.
666 * @return Data in SAI FIFO.
667 */
SAI_ReadData(I2S_Type * base,uint32_t channel)668 static inline uint32_t SAI_ReadData(I2S_Type *base, uint32_t channel)
669 {
670 return base->RDR[channel];
671 }
672
673 /*! @} */
674
675 /*!
676 * @name Transactional
677 * @{
678 */
679
680 /*!
681 * @brief Initializes the SAI Tx handle.
682 *
683 * This function initializes the Tx handle for the SAI Tx transactional APIs. Call
684 * this function once to get the handle initialized.
685 *
686 * @param base SAI base pointer
687 * @param handle SAI handle pointer.
688 * @param callback Pointer to the user callback function.
689 * @param userData User parameter passed to the callback function
690 */
691 void SAI_TransferTxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
692
693 /*!
694 * @brief Initializes the SAI Rx handle.
695 *
696 * This function initializes the Rx handle for the SAI Rx transactional APIs. Call
697 * this function once to get the handle initialized.
698 *
699 * @param base SAI base pointer.
700 * @param handle SAI handle pointer.
701 * @param callback Pointer to the user callback function.
702 * @param userData User parameter passed to the callback function.
703 */
704 void SAI_TransferRxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
705
706 /*!
707 * @brief Configures the SAI Tx audio format.
708 *
709 * The audio format can be changed at run-time. This function configures the sample rate and audio data
710 * format to be transferred.
711 *
712 * @param base SAI base pointer.
713 * @param handle SAI handle pointer.
714 * @param format Pointer to the SAI audio data format structure.
715 * @param mclkSourceClockHz SAI master clock source frequency in Hz.
716 * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
717 * clock, this value should equal the masterClockHz in format.
718 * @return Status of this function. Return value is the status_t.
719 */
720 status_t SAI_TransferTxSetFormat(I2S_Type *base,
721 sai_handle_t *handle,
722 sai_transfer_format_t *format,
723 uint32_t mclkSourceClockHz,
724 uint32_t bclkSourceClockHz);
725
726 /*!
727 * @brief Configures the SAI Rx audio format.
728 *
729 * The audio format can be changed at run-time. This function configures the sample rate and audio data
730 * format to be transferred.
731 *
732 * @param base SAI base pointer.
733 * @param handle SAI handle pointer.
734 * @param format Pointer to the SAI audio data format structure.
735 * @param mclkSourceClockHz SAI master clock source frequency in Hz.
736 * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
737 * clock, this value should equal the masterClockHz in format.
738 * @return Status of this function. Return value is one of status_t.
739 */
740 status_t SAI_TransferRxSetFormat(I2S_Type *base,
741 sai_handle_t *handle,
742 sai_transfer_format_t *format,
743 uint32_t mclkSourceClockHz,
744 uint32_t bclkSourceClockHz);
745
746 /*!
747 * @brief Performs an interrupt non-blocking send transfer on SAI.
748 *
749 * @note This API returns immediately after the transfer initiates.
750 * Call the SAI_TxGetTransferStatusIRQ to poll the transfer status and check whether
751 * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
752 * is finished.
753 *
754 * @param base SAI base pointer.
755 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
756 * @param xfer Pointer to the sai_transfer_t structure.
757 * @retval kStatus_Success Successfully started the data receive.
758 * @retval kStatus_SAI_TxBusy Previous receive still not finished.
759 * @retval kStatus_InvalidArgument The input parameter is invalid.
760 */
761 status_t SAI_TransferSendNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
762
763 /*!
764 * @brief Performs an interrupt non-blocking receive transfer on SAI.
765 *
766 * @note This API returns immediately after the transfer initiates.
767 * Call the SAI_RxGetTransferStatusIRQ to poll the transfer status and check whether
768 * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
769 * is finished.
770 *
771 * @param base SAI base pointer
772 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
773 * @param xfer Pointer to the sai_transfer_t structure.
774 * @retval kStatus_Success Successfully started the data receive.
775 * @retval kStatus_SAI_RxBusy Previous receive still not finished.
776 * @retval kStatus_InvalidArgument The input parameter is invalid.
777 */
778 status_t SAI_TransferReceiveNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
779
780 /*!
781 * @brief Gets a set byte count.
782 *
783 * @param base SAI base pointer.
784 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
785 * @param count Bytes count sent.
786 * @retval kStatus_Success Succeed get the transfer count.
787 * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
788 */
789 status_t SAI_TransferGetSendCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
790
791 /*!
792 * @brief Gets a received byte count.
793 *
794 * @param base SAI base pointer.
795 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
796 * @param count Bytes count received.
797 * @retval kStatus_Success Succeed get the transfer count.
798 * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
799 */
800 status_t SAI_TransferGetReceiveCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
801
802 /*!
803 * @brief Aborts the current send.
804 *
805 * @note This API can be called any time when an interrupt non-blocking transfer initiates
806 * to abort the transfer early.
807 *
808 * @param base SAI base pointer.
809 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
810 */
811 void SAI_TransferAbortSend(I2S_Type *base, sai_handle_t *handle);
812
813 /*!
814 * @brief Aborts the the current IRQ receive.
815 *
816 * @note This API can be called when an interrupt non-blocking transfer initiates
817 * to abort the transfer early.
818 *
819 * @param base SAI base pointer
820 * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
821 */
822 void SAI_TransferAbortReceive(I2S_Type *base, sai_handle_t *handle);
823
824 /*!
825 * @brief Tx interrupt handler.
826 *
827 * @param base SAI base pointer.
828 * @param handle Pointer to the sai_handle_t structure.
829 */
830 void SAI_TransferTxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
831
832 /*!
833 * @brief Tx interrupt handler.
834 *
835 * @param base SAI base pointer.
836 * @param handle Pointer to the sai_handle_t structure.
837 */
838 void SAI_TransferRxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
839
840 /*! @} */
841
842 #if defined(__cplusplus)
843 }
844 #endif /*_cplusplus*/
845
846 /*! @} */
847
848 #endif /* _FSL_SAI_H_ */
849