fsl_sai.h 32 KB

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