fsl_sai.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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, 3)) /*!< Version 2.1.3 */
  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_MonoRight, /*!< Only Right channel have sound. */
  75. kSAI_MonoLeft /*!< Only left 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. bool isFrameSyncCompact; /*!< True means Frame sync length is configurable according to bitWidth, false means frame
  200. sync length is 64 times of bit clock. */
  201. } sai_transfer_format_t;
  202. /*! @brief SAI transfer structure */
  203. typedef struct _sai_transfer
  204. {
  205. uint8_t *data; /*!< Data start address to transfer. */
  206. size_t dataSize; /*!< Transfer size. */
  207. } sai_transfer_t;
  208. typedef struct _sai_handle sai_handle_t;
  209. /*! @brief SAI transfer callback prototype */
  210. typedef void (*sai_transfer_callback_t)(I2S_Type *base, sai_handle_t *handle, status_t status, void *userData);
  211. /*! @brief SAI handle structure */
  212. struct _sai_handle
  213. {
  214. uint32_t state; /*!< Transfer status */
  215. sai_transfer_callback_t callback; /*!< Callback function called at transfer event*/
  216. void *userData; /*!< Callback parameter passed to callback function*/
  217. uint8_t bitWidth; /*!< Bit width for transfer, 8/16/24/32 bits */
  218. uint8_t channel; /*!< Transfer channel */
  219. sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer */
  220. size_t transferSize[SAI_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
  221. volatile uint8_t queueUser; /*!< Index for user to queue transfer */
  222. volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
  223. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  224. uint8_t watermark; /*!< Watermark value */
  225. #endif
  226. };
  227. /*******************************************************************************
  228. * API
  229. ******************************************************************************/
  230. #if defined(__cplusplus)
  231. extern "C" {
  232. #endif /*_cplusplus*/
  233. /*!
  234. * @name Initialization and deinitialization
  235. * @{
  236. */
  237. /*!
  238. * @brief Initializes the SAI Tx peripheral.
  239. *
  240. * Ungates the SAI clock, resets the module, and configures SAI Tx with a configuration structure.
  241. * The configuration structure can be custom filled or set with default values by
  242. * SAI_TxGetDefaultConfig().
  243. *
  244. * @note This API should be called at the beginning of the application to use
  245. * the SAI driver. Otherwise, accessing the SAIM module can cause a hard fault
  246. * because the clock is not enabled.
  247. *
  248. * @param base SAI base pointer
  249. * @param config SAI configuration structure.
  250. */
  251. void SAI_TxInit(I2S_Type *base, const sai_config_t *config);
  252. /*!
  253. * @brief Initializes the the SAI Rx peripheral.
  254. *
  255. * Ungates the SAI clock, resets the module, and configures the SAI Rx with a configuration structure.
  256. * The configuration structure can be custom filled or set with default values by
  257. * SAI_RxGetDefaultConfig().
  258. *
  259. * @note This API should be called at the beginning of the application to use
  260. * the SAI driver. Otherwise, accessing the SAI module can cause a hard fault
  261. * because the clock is not enabled.
  262. *
  263. * @param base SAI base pointer
  264. * @param config SAI configuration structure.
  265. */
  266. void SAI_RxInit(I2S_Type *base, const sai_config_t *config);
  267. /*!
  268. * @brief Sets the SAI Tx configuration structure to default values.
  269. *
  270. * This API initializes the configuration structure for use in SAI_TxConfig().
  271. * The initialized structure can remain unchanged in SAI_TxConfig(), or it can be modified
  272. * before calling SAI_TxConfig().
  273. * This is an example.
  274. @code
  275. sai_config_t config;
  276. SAI_TxGetDefaultConfig(&config);
  277. @endcode
  278. *
  279. * @param config pointer to master configuration structure
  280. */
  281. void SAI_TxGetDefaultConfig(sai_config_t *config);
  282. /*!
  283. * @brief Sets the SAI Rx configuration structure to default values.
  284. *
  285. * This API initializes the configuration structure for use in SAI_RxConfig().
  286. * The initialized structure can remain unchanged in SAI_RxConfig() or it can be modified
  287. * before calling SAI_RxConfig().
  288. * This is an example.
  289. @code
  290. sai_config_t config;
  291. SAI_RxGetDefaultConfig(&config);
  292. @endcode
  293. *
  294. * @param config pointer to master configuration structure
  295. */
  296. void SAI_RxGetDefaultConfig(sai_config_t *config);
  297. /*!
  298. * @brief De-initializes the SAI peripheral.
  299. *
  300. * This API gates the SAI clock. The SAI module can't operate unless SAI_TxInit
  301. * or SAI_RxInit is called to enable the clock.
  302. *
  303. * @param base SAI base pointer
  304. */
  305. void SAI_Deinit(I2S_Type *base);
  306. /*!
  307. * @brief Resets the SAI Tx.
  308. *
  309. * This function enables the software reset and FIFO reset of SAI Tx. After reset, clear the reset bit.
  310. *
  311. * @param base SAI base pointer
  312. */
  313. void SAI_TxReset(I2S_Type *base);
  314. /*!
  315. * @brief Resets the SAI Rx.
  316. *
  317. * This function enables the software reset and FIFO reset of SAI Rx. After reset, clear the reset bit.
  318. *
  319. * @param base SAI base pointer
  320. */
  321. void SAI_RxReset(I2S_Type *base);
  322. /*!
  323. * @brief Enables/disables the SAI Tx.
  324. *
  325. * @param base SAI base pointer
  326. * @param enable True means enable SAI Tx, false means disable.
  327. */
  328. void SAI_TxEnable(I2S_Type *base, bool enable);
  329. /*!
  330. * @brief Enables/disables the SAI Rx.
  331. *
  332. * @param base SAI base pointer
  333. * @param enable True means enable SAI Rx, false means disable.
  334. */
  335. void SAI_RxEnable(I2S_Type *base, bool enable);
  336. /*! @} */
  337. /*!
  338. * @name Status
  339. * @{
  340. */
  341. /*!
  342. * @brief Gets the SAI Tx status flag state.
  343. *
  344. * @param base SAI base pointer
  345. * @return SAI Tx status flag value. Use the Status Mask to get the status value needed.
  346. */
  347. static inline uint32_t SAI_TxGetStatusFlag(I2S_Type *base)
  348. {
  349. return base->TCSR;
  350. }
  351. /*!
  352. * @brief Clears the SAI Tx status flag state.
  353. *
  354. * @param base SAI base pointer
  355. * @param mask State mask. It can be a combination of the following source if defined:
  356. * @arg kSAI_WordStartFlag
  357. * @arg kSAI_SyncErrorFlag
  358. * @arg kSAI_FIFOErrorFlag
  359. */
  360. static inline void SAI_TxClearStatusFlags(I2S_Type *base, uint32_t mask)
  361. {
  362. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  363. }
  364. /*!
  365. * @brief Gets the SAI Tx status flag state.
  366. *
  367. * @param base SAI base pointer
  368. * @return SAI Rx status flag value. Use the Status Mask to get the status value needed.
  369. */
  370. static inline uint32_t SAI_RxGetStatusFlag(I2S_Type *base)
  371. {
  372. return base->RCSR;
  373. }
  374. /*!
  375. * @brief Clears the SAI Rx status flag state.
  376. *
  377. * @param base SAI base pointer
  378. * @param mask State mask. It can be a combination of the following sources if defined.
  379. * @arg kSAI_WordStartFlag
  380. * @arg kSAI_SyncErrorFlag
  381. * @arg kSAI_FIFOErrorFlag
  382. */
  383. static inline void SAI_RxClearStatusFlags(I2S_Type *base, uint32_t mask)
  384. {
  385. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  386. }
  387. /*!
  388. * @brief Do software reset or FIFO reset .
  389. *
  390. * FIFO reset means clear all the data in the FIFO, and make the FIFO pointer both to 0.
  391. * Software reset means claer the Tx internal logic, including the bit clock, frame count etc. But software
  392. * reset will not clear any configuration registers like TCR1~TCR5.
  393. * This function will also clear all the error flags such as FIFO error, sync error etc.
  394. *
  395. * @param base SAI base pointer
  396. * @param type Reset type, FIFO reset or software reset
  397. */
  398. void SAI_TxSoftwareReset(I2S_Type *base, sai_reset_type_t type);
  399. /*!
  400. * @brief Do software reset or FIFO reset .
  401. *
  402. * FIFO reset means clear all the data in the FIFO, and make the FIFO pointer both to 0.
  403. * Software reset means claer the Rx internal logic, including the bit clock, frame count etc. But software
  404. * reset will not clear any configuration registers like RCR1~RCR5.
  405. * This function will also clear all the error flags such as FIFO error, sync error etc.
  406. *
  407. * @param base SAI base pointer
  408. * @param type Reset type, FIFO reset or software reset
  409. */
  410. void SAI_RxSoftwareReset(I2S_Type *base, sai_reset_type_t type);
  411. /*!
  412. * @brief Set the Tx channel FIFO enable mask.
  413. *
  414. * @param base SAI base pointer
  415. * @param mask Channel enable mask, 0 means all channel FIFO disabled, 1 means channel 0 enabled,
  416. * 3 means both channel 0 and channel 1 enabled.
  417. */
  418. void SAI_TxSetChannelFIFOMask(I2S_Type *base, uint8_t mask);
  419. /*!
  420. * @brief Set the Rx channel FIFO enable mask.
  421. *
  422. * @param base SAI base pointer
  423. * @param mask Channel enable mask, 0 means all channel FIFO disabled, 1 means channel 0 enabled,
  424. * 3 means both channel 0 and channel 1 enabled.
  425. */
  426. void SAI_RxSetChannelFIFOMask(I2S_Type *base, uint8_t mask);
  427. /*! @} */
  428. /*!
  429. * @name Interrupts
  430. * @{
  431. */
  432. /*!
  433. * @brief Enables the SAI Tx interrupt requests.
  434. *
  435. * @param base SAI base pointer
  436. * @param mask interrupt source
  437. * The parameter can be a combination of the following sources if defined.
  438. * @arg kSAI_WordStartInterruptEnable
  439. * @arg kSAI_SyncErrorInterruptEnable
  440. * @arg kSAI_FIFOWarningInterruptEnable
  441. * @arg kSAI_FIFORequestInterruptEnable
  442. * @arg kSAI_FIFOErrorInterruptEnable
  443. */
  444. static inline void SAI_TxEnableInterrupts(I2S_Type *base, uint32_t mask)
  445. {
  446. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  447. }
  448. /*!
  449. * @brief Enables the SAI Rx interrupt requests.
  450. *
  451. * @param base SAI base pointer
  452. * @param mask interrupt source
  453. * The parameter can be a combination of the following sources if defined.
  454. * @arg kSAI_WordStartInterruptEnable
  455. * @arg kSAI_SyncErrorInterruptEnable
  456. * @arg kSAI_FIFOWarningInterruptEnable
  457. * @arg kSAI_FIFORequestInterruptEnable
  458. * @arg kSAI_FIFOErrorInterruptEnable
  459. */
  460. static inline void SAI_RxEnableInterrupts(I2S_Type *base, uint32_t mask)
  461. {
  462. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  463. }
  464. /*!
  465. * @brief Disables the SAI Tx interrupt requests.
  466. *
  467. * @param base SAI base pointer
  468. * @param mask interrupt source
  469. * The parameter can be a combination of the following sources if defined.
  470. * @arg kSAI_WordStartInterruptEnable
  471. * @arg kSAI_SyncErrorInterruptEnable
  472. * @arg kSAI_FIFOWarningInterruptEnable
  473. * @arg kSAI_FIFORequestInterruptEnable
  474. * @arg kSAI_FIFOErrorInterruptEnable
  475. */
  476. static inline void SAI_TxDisableInterrupts(I2S_Type *base, uint32_t mask)
  477. {
  478. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
  479. }
  480. /*!
  481. * @brief Disables the SAI Rx interrupt requests.
  482. *
  483. * @param base SAI base pointer
  484. * @param mask interrupt source
  485. * The parameter can be a combination of the following sources if defined.
  486. * @arg kSAI_WordStartInterruptEnable
  487. * @arg kSAI_SyncErrorInterruptEnable
  488. * @arg kSAI_FIFOWarningInterruptEnable
  489. * @arg kSAI_FIFORequestInterruptEnable
  490. * @arg kSAI_FIFOErrorInterruptEnable
  491. */
  492. static inline void SAI_RxDisableInterrupts(I2S_Type *base, uint32_t mask)
  493. {
  494. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
  495. }
  496. /*! @} */
  497. /*!
  498. * @name DMA Control
  499. * @{
  500. */
  501. /*!
  502. * @brief Enables/disables the SAI Tx DMA requests.
  503. * @param base SAI base pointer
  504. * @param mask DMA source
  505. * The parameter can be combination of the following sources if defined.
  506. * @arg kSAI_FIFOWarningDMAEnable
  507. * @arg kSAI_FIFORequestDMAEnable
  508. * @param enable True means enable DMA, false means disable DMA.
  509. */
  510. static inline void SAI_TxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
  511. {
  512. if (enable)
  513. {
  514. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  515. }
  516. else
  517. {
  518. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
  519. }
  520. }
  521. /*!
  522. * @brief Enables/disables the SAI Rx DMA requests.
  523. * @param base SAI base pointer
  524. * @param mask DMA source
  525. * The parameter can be a combination of the following sources if defined.
  526. * @arg kSAI_FIFOWarningDMAEnable
  527. * @arg kSAI_FIFORequestDMAEnable
  528. * @param enable True means enable DMA, false means disable DMA.
  529. */
  530. static inline void SAI_RxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
  531. {
  532. if (enable)
  533. {
  534. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  535. }
  536. else
  537. {
  538. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
  539. }
  540. }
  541. /*!
  542. * @brief Gets the SAI Tx data register address.
  543. *
  544. * This API is used to provide a transfer address for the SAI DMA transfer configuration.
  545. *
  546. * @param base SAI base pointer.
  547. * @param channel Which data channel used.
  548. * @return data register address.
  549. */
  550. static inline uint32_t SAI_TxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
  551. {
  552. return (uint32_t)(&(base->TDR)[channel]);
  553. }
  554. /*!
  555. * @brief Gets the SAI Rx 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. */
  563. static inline uint32_t SAI_RxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
  564. {
  565. return (uint32_t)(&(base->RDR)[channel]);
  566. }
  567. /*! @} */
  568. /*!
  569. * @name Bus Operations
  570. * @{
  571. */
  572. /*!
  573. * @brief Configures the SAI Tx audio format.
  574. *
  575. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  576. * format to be transferred.
  577. *
  578. * @param base SAI base pointer.
  579. * @param format Pointer to the SAI audio data format structure.
  580. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  581. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
  582. * clock, this value should equal the masterClockHz.
  583. */
  584. void SAI_TxSetFormat(I2S_Type *base,
  585. sai_transfer_format_t *format,
  586. uint32_t mclkSourceClockHz,
  587. uint32_t bclkSourceClockHz);
  588. /*!
  589. * @brief Configures the SAI Rx audio format.
  590. *
  591. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  592. * format to be transferred.
  593. *
  594. * @param base SAI base pointer.
  595. * @param format Pointer to the SAI audio data format structure.
  596. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  597. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
  598. * clock, this value should equal the masterClockHz.
  599. */
  600. void SAI_RxSetFormat(I2S_Type *base,
  601. sai_transfer_format_t *format,
  602. uint32_t mclkSourceClockHz,
  603. uint32_t bclkSourceClockHz);
  604. /*!
  605. * @brief Sends data using a blocking method.
  606. *
  607. * @note This function blocks by polling until data is ready to be sent.
  608. *
  609. * @param base SAI base pointer.
  610. * @param channel Data channel used.
  611. * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
  612. * @param buffer Pointer to the data to be written.
  613. * @param size Bytes to be written.
  614. */
  615. void SAI_WriteBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
  616. /*!
  617. * @brief Writes data into SAI FIFO.
  618. *
  619. * @param base SAI base pointer.
  620. * @param channel Data channel used.
  621. * @param data Data needs to be written.
  622. */
  623. static inline void SAI_WriteData(I2S_Type *base, uint32_t channel, uint32_t data)
  624. {
  625. base->TDR[channel] = data;
  626. }
  627. /*!
  628. * @brief Receives data using a blocking method.
  629. *
  630. * @note This function blocks by polling until data is ready to be sent.
  631. *
  632. * @param base SAI base pointer.
  633. * @param channel Data channel used.
  634. * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
  635. * @param buffer Pointer to the data to be read.
  636. * @param size Bytes to be read.
  637. */
  638. void SAI_ReadBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
  639. /*!
  640. * @brief Reads data from the SAI FIFO.
  641. *
  642. * @param base SAI base pointer.
  643. * @param channel Data channel used.
  644. * @return Data in SAI FIFO.
  645. */
  646. static inline uint32_t SAI_ReadData(I2S_Type *base, uint32_t channel)
  647. {
  648. return base->RDR[channel];
  649. }
  650. /*! @} */
  651. /*!
  652. * @name Transactional
  653. * @{
  654. */
  655. /*!
  656. * @brief Initializes the SAI Tx handle.
  657. *
  658. * This function initializes the Tx handle for the SAI Tx transactional APIs. Call
  659. * this function once to get the handle initialized.
  660. *
  661. * @param base SAI base pointer
  662. * @param handle SAI handle pointer.
  663. * @param callback Pointer to the user callback function.
  664. * @param userData User parameter passed to the callback function
  665. */
  666. void SAI_TransferTxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
  667. /*!
  668. * @brief Initializes the SAI Rx handle.
  669. *
  670. * This function initializes the Rx handle for the SAI Rx transactional APIs. Call
  671. * this function once to get the handle initialized.
  672. *
  673. * @param base SAI base pointer.
  674. * @param handle SAI handle pointer.
  675. * @param callback Pointer to the user callback function.
  676. * @param userData User parameter passed to the callback function.
  677. */
  678. void SAI_TransferRxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
  679. /*!
  680. * @brief Configures the SAI Tx audio format.
  681. *
  682. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  683. * format to be transferred.
  684. *
  685. * @param base SAI base pointer.
  686. * @param handle SAI handle pointer.
  687. * @param format Pointer to the SAI audio data format structure.
  688. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  689. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
  690. * clock, this value should equal the masterClockHz in format.
  691. * @return Status of this function. Return value is the status_t.
  692. */
  693. status_t SAI_TransferTxSetFormat(I2S_Type *base,
  694. sai_handle_t *handle,
  695. sai_transfer_format_t *format,
  696. uint32_t mclkSourceClockHz,
  697. uint32_t bclkSourceClockHz);
  698. /*!
  699. * @brief Configures the SAI Rx audio format.
  700. *
  701. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  702. * format to be transferred.
  703. *
  704. * @param base SAI base pointer.
  705. * @param handle SAI handle pointer.
  706. * @param format Pointer to the SAI audio data format structure.
  707. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  708. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
  709. * clock, this value should equal the masterClockHz in format.
  710. * @return Status of this function. Return value is one of status_t.
  711. */
  712. status_t SAI_TransferRxSetFormat(I2S_Type *base,
  713. sai_handle_t *handle,
  714. sai_transfer_format_t *format,
  715. uint32_t mclkSourceClockHz,
  716. uint32_t bclkSourceClockHz);
  717. /*!
  718. * @brief Performs an interrupt non-blocking send transfer on SAI.
  719. *
  720. * @note This API returns immediately after the transfer initiates.
  721. * Call the SAI_TxGetTransferStatusIRQ to poll the transfer status and check whether
  722. * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
  723. * is finished.
  724. *
  725. * @param base SAI base pointer.
  726. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  727. * @param xfer Pointer to the sai_transfer_t structure.
  728. * @retval kStatus_Success Successfully started the data receive.
  729. * @retval kStatus_SAI_TxBusy Previous receive still not finished.
  730. * @retval kStatus_InvalidArgument The input parameter is invalid.
  731. */
  732. status_t SAI_TransferSendNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
  733. /*!
  734. * @brief Performs an interrupt non-blocking receive transfer on SAI.
  735. *
  736. * @note This API returns immediately after the transfer initiates.
  737. * Call the SAI_RxGetTransferStatusIRQ to poll the transfer status and check whether
  738. * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
  739. * is finished.
  740. *
  741. * @param base SAI base pointer
  742. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  743. * @param xfer Pointer to the sai_transfer_t structure.
  744. * @retval kStatus_Success Successfully started the data receive.
  745. * @retval kStatus_SAI_RxBusy Previous receive still not finished.
  746. * @retval kStatus_InvalidArgument The input parameter is invalid.
  747. */
  748. status_t SAI_TransferReceiveNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
  749. /*!
  750. * @brief Gets a set byte count.
  751. *
  752. * @param base SAI base pointer.
  753. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  754. * @param count Bytes count sent.
  755. * @retval kStatus_Success Succeed get the transfer count.
  756. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  757. */
  758. status_t SAI_TransferGetSendCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
  759. /*!
  760. * @brief Gets a received byte count.
  761. *
  762. * @param base SAI base pointer.
  763. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  764. * @param count Bytes count received.
  765. * @retval kStatus_Success Succeed get the transfer count.
  766. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  767. */
  768. status_t SAI_TransferGetReceiveCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
  769. /*!
  770. * @brief Aborts the current send.
  771. *
  772. * @note This API can be called any time when an interrupt non-blocking transfer initiates
  773. * to abort the transfer early.
  774. *
  775. * @param base SAI base pointer.
  776. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  777. */
  778. void SAI_TransferAbortSend(I2S_Type *base, sai_handle_t *handle);
  779. /*!
  780. * @brief Aborts the the current IRQ receive.
  781. *
  782. * @note This API can be called when an interrupt non-blocking transfer initiates
  783. * to abort the transfer early.
  784. *
  785. * @param base SAI base pointer
  786. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  787. */
  788. void SAI_TransferAbortReceive(I2S_Type *base, sai_handle_t *handle);
  789. /*!
  790. * @brief Terminate all SAI send.
  791. *
  792. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  793. * current transfer slot, please call SAI_TransferAbortSend.
  794. *
  795. * @param base SAI base pointer.
  796. * @param handle SAI eDMA handle pointer.
  797. */
  798. void SAI_TransferTerminateSend(I2S_Type *base, sai_handle_t *handle);
  799. /*!
  800. * @brief Terminate all SAI receive.
  801. *
  802. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  803. * current transfer slot, please call SAI_TransferAbortReceive.
  804. *
  805. * @param base SAI base pointer.
  806. * @param handle SAI eDMA handle pointer.
  807. */
  808. void SAI_TransferTerminateReceive(I2S_Type *base, sai_handle_t *handle);
  809. /*!
  810. * @brief Tx interrupt handler.
  811. *
  812. * @param base SAI base pointer.
  813. * @param handle Pointer to the sai_handle_t structure.
  814. */
  815. void SAI_TransferTxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
  816. /*!
  817. * @brief Tx interrupt handler.
  818. *
  819. * @param base SAI base pointer.
  820. * @param handle Pointer to the sai_handle_t structure.
  821. */
  822. void SAI_TransferRxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
  823. /*! @} */
  824. #if defined(__cplusplus)
  825. }
  826. #endif /*_cplusplus*/
  827. /*! @} */
  828. #endif /* _FSL_SAI_H_ */