fsl_sai.h 29 KB

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