fsl_sai.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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, 4)) /*!< Version 2.1.4 */
  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 SAI data order, MSB or LSB */
  82. typedef enum _sai_data_order
  83. {
  84. kSAI_DataLSB = 0x0U, /*!< LSB bit transferred first */
  85. kSAI_DataMSB /*!< MSB bit transferred first */
  86. } sai_data_order_t;
  87. /*! @brief SAI clock polarity, active high or low */
  88. typedef enum _sai_clock_polarity
  89. {
  90. kSAI_PolarityActiveHigh = 0x0U, /*!< Clock active high */
  91. kSAI_PolarityActiveLow /*!< Clock active low */
  92. } sai_clock_polarity_t;
  93. /*! @brief Synchronous or asynchronous mode */
  94. typedef enum _sai_sync_mode
  95. {
  96. kSAI_ModeAsync = 0x0U, /*!< Asynchronous mode */
  97. kSAI_ModeSync, /*!< Synchronous mode (with receiver or transmit) */
  98. kSAI_ModeSyncWithOtherTx, /*!< Synchronous with another SAI transmit */
  99. kSAI_ModeSyncWithOtherRx /*!< Synchronous with another SAI receiver */
  100. } sai_sync_mode_t;
  101. /*! @brief Mater clock source */
  102. typedef enum _sai_mclk_source
  103. {
  104. kSAI_MclkSourceSysclk = 0x0U, /*!< Master clock from the system clock */
  105. kSAI_MclkSourceSelect1, /*!< Master clock from source 1 */
  106. kSAI_MclkSourceSelect2, /*!< Master clock from source 2 */
  107. kSAI_MclkSourceSelect3 /*!< Master clock from source 3 */
  108. } sai_mclk_source_t;
  109. /*! @brief Bit clock source */
  110. typedef enum _sai_bclk_source
  111. {
  112. kSAI_BclkSourceBusclk = 0x0U, /*!< Bit clock using bus clock */
  113. kSAI_BclkSourceMclkDiv, /*!< Bit clock using master clock divider */
  114. kSAI_BclkSourceOtherSai0, /*!< Bit clock from other SAI device */
  115. kSAI_BclkSourceOtherSai1 /*!< Bit clock from other SAI device */
  116. } sai_bclk_source_t;
  117. /*! @brief The SAI interrupt enable flag */
  118. enum _sai_interrupt_enable_t
  119. {
  120. kSAI_WordStartInterruptEnable =
  121. I2S_TCSR_WSIE_MASK, /*!< Word start flag, means the first word in a frame detected */
  122. kSAI_SyncErrorInterruptEnable = I2S_TCSR_SEIE_MASK, /*!< Sync error flag, means the sync error is detected */
  123. kSAI_FIFOWarningInterruptEnable = I2S_TCSR_FWIE_MASK, /*!< FIFO warning flag, means the FIFO is empty */
  124. kSAI_FIFOErrorInterruptEnable = I2S_TCSR_FEIE_MASK, /*!< FIFO error flag */
  125. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  126. kSAI_FIFORequestInterruptEnable = I2S_TCSR_FRIE_MASK, /*!< FIFO request, means reached watermark */
  127. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  128. };
  129. /*! @brief The DMA request sources */
  130. enum _sai_dma_enable_t
  131. {
  132. kSAI_FIFOWarningDMAEnable = I2S_TCSR_FWDE_MASK, /*!< FIFO warning caused by the DMA request */
  133. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  134. kSAI_FIFORequestDMAEnable = I2S_TCSR_FRDE_MASK, /*!< FIFO request caused by the DMA request */
  135. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  136. };
  137. /*! @brief The SAI status flag */
  138. enum _sai_flags
  139. {
  140. kSAI_WordStartFlag = I2S_TCSR_WSF_MASK, /*!< Word start flag, means the first word in a frame detected */
  141. kSAI_SyncErrorFlag = I2S_TCSR_SEF_MASK, /*!< Sync error flag, means the sync error is detected */
  142. kSAI_FIFOErrorFlag = I2S_TCSR_FEF_MASK, /*!< FIFO error flag */
  143. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  144. kSAI_FIFORequestFlag = I2S_TCSR_FRF_MASK, /*!< FIFO request flag. */
  145. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  146. kSAI_FIFOWarningFlag = I2S_TCSR_FWF_MASK, /*!< FIFO warning flag */
  147. };
  148. /*! @brief The reset type */
  149. typedef enum _sai_reset_type
  150. {
  151. kSAI_ResetTypeSoftware = I2S_TCSR_SR_MASK, /*!< Software reset, reset the logic state */
  152. kSAI_ResetTypeFIFO = I2S_TCSR_FR_MASK, /*!< FIFO reset, reset the FIFO read and write pointer */
  153. kSAI_ResetAll = I2S_TCSR_SR_MASK | I2S_TCSR_FR_MASK /*!< All reset. */
  154. } sai_reset_type_t;
  155. #if defined(FSL_FEATURE_SAI_HAS_FIFO_PACKING) && FSL_FEATURE_SAI_HAS_FIFO_PACKING
  156. /*!
  157. * @brief The SAI packing mode
  158. * The mode includes 8 bit and 16 bit packing.
  159. */
  160. typedef enum _sai_fifo_packing
  161. {
  162. kSAI_FifoPackingDisabled = 0x0U, /*!< Packing disabled */
  163. kSAI_FifoPacking8bit = 0x2U, /*!< 8 bit packing enabled */
  164. kSAI_FifoPacking16bit = 0x3U /*!< 16bit packing enabled */
  165. } sai_fifo_packing_t;
  166. #endif /* FSL_FEATURE_SAI_HAS_FIFO_PACKING */
  167. /*! @brief SAI user configuration structure */
  168. typedef struct _sai_config
  169. {
  170. sai_protocol_t protocol; /*!< Audio bus protocol in SAI */
  171. sai_sync_mode_t syncMode; /*!< SAI sync mode, control Tx/Rx clock sync */
  172. #if defined(FSL_FEATURE_SAI_HAS_MCR) && (FSL_FEATURE_SAI_HAS_MCR)
  173. bool mclkOutputEnable; /*!< Master clock output enable, true means master clock divider enabled */
  174. #endif /* FSL_FEATURE_SAI_HAS_MCR */
  175. sai_mclk_source_t mclkSource; /*!< Master Clock source */
  176. sai_bclk_source_t bclkSource; /*!< Bit Clock source */
  177. sai_master_slave_t masterSlave; /*!< Master or slave */
  178. } sai_config_t;
  179. /*!@brief SAI transfer queue size, user can refine it according to use case. */
  180. #define SAI_XFER_QUEUE_SIZE (4)
  181. /*! @brief Audio sample rate */
  182. typedef enum _sai_sample_rate
  183. {
  184. kSAI_SampleRate8KHz = 8000U, /*!< Sample rate 8000 Hz */
  185. kSAI_SampleRate11025Hz = 11025U, /*!< Sample rate 11025 Hz */
  186. kSAI_SampleRate12KHz = 12000U, /*!< Sample rate 12000 Hz */
  187. kSAI_SampleRate16KHz = 16000U, /*!< Sample rate 16000 Hz */
  188. kSAI_SampleRate22050Hz = 22050U, /*!< Sample rate 22050 Hz */
  189. kSAI_SampleRate24KHz = 24000U, /*!< Sample rate 24000 Hz */
  190. kSAI_SampleRate32KHz = 32000U, /*!< Sample rate 32000 Hz */
  191. kSAI_SampleRate44100Hz = 44100U, /*!< Sample rate 44100 Hz */
  192. kSAI_SampleRate48KHz = 48000U, /*!< Sample rate 48000 Hz */
  193. kSAI_SampleRate96KHz = 96000U /*!< Sample rate 96000 Hz */
  194. } sai_sample_rate_t;
  195. /*! @brief Audio word width */
  196. typedef enum _sai_word_width
  197. {
  198. kSAI_WordWidth8bits = 8U, /*!< Audio data width 8 bits */
  199. kSAI_WordWidth16bits = 16U, /*!< Audio data width 16 bits */
  200. kSAI_WordWidth24bits = 24U, /*!< Audio data width 24 bits */
  201. kSAI_WordWidth32bits = 32U /*!< Audio data width 32 bits */
  202. } sai_word_width_t;
  203. /*! @brief sai transfer format */
  204. typedef struct _sai_transfer_format
  205. {
  206. uint32_t sampleRate_Hz; /*!< Sample rate of audio data */
  207. uint32_t bitWidth; /*!< Data length of audio data, usually 8/16/24/32 bits */
  208. sai_mono_stereo_t stereo; /*!< Mono or stereo */
  209. uint32_t masterClockHz; /*!< Master clock frequency in Hz */
  210. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  211. uint8_t watermark; /*!< Watermark value */
  212. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  213. uint8_t channel; /*!< Data channel used in transfer.*/
  214. sai_protocol_t protocol; /*!< Which audio protocol used */
  215. bool isFrameSyncCompact; /*!< True means Frame sync length is configurable according to bitWidth, false means frame
  216. sync length is 64 times of bit clock. */
  217. } sai_transfer_format_t;
  218. /*! @brief SAI transfer structure */
  219. typedef struct _sai_transfer
  220. {
  221. uint8_t *data; /*!< Data start address to transfer. */
  222. size_t dataSize; /*!< Transfer size. */
  223. } sai_transfer_t;
  224. typedef struct _sai_handle sai_handle_t;
  225. /*! @brief SAI transfer callback prototype */
  226. typedef void (*sai_transfer_callback_t)(I2S_Type *base, sai_handle_t *handle, status_t status, void *userData);
  227. /*! @brief SAI handle structure */
  228. struct _sai_handle
  229. {
  230. uint32_t state; /*!< Transfer status */
  231. sai_transfer_callback_t callback; /*!< Callback function called at transfer event*/
  232. void *userData; /*!< Callback parameter passed to callback function*/
  233. uint8_t bitWidth; /*!< Bit width for transfer, 8/16/24/32 bits */
  234. uint8_t channel; /*!< Transfer channel */
  235. sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer */
  236. size_t transferSize[SAI_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
  237. volatile uint8_t queueUser; /*!< Index for user to queue transfer */
  238. volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
  239. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  240. uint8_t watermark; /*!< Watermark value */
  241. #endif
  242. };
  243. /*******************************************************************************
  244. * API
  245. ******************************************************************************/
  246. #if defined(__cplusplus)
  247. extern "C" {
  248. #endif /*_cplusplus*/
  249. /*!
  250. * @name Initialization and deinitialization
  251. * @{
  252. */
  253. /*!
  254. * @brief Initializes the SAI Tx peripheral.
  255. *
  256. * Ungates the SAI clock, resets the module, and configures SAI Tx with a configuration structure.
  257. * The configuration structure can be custom filled or set with default values by
  258. * SAI_TxGetDefaultConfig().
  259. *
  260. * @note This API should be called at the beginning of the application to use
  261. * the SAI driver. Otherwise, accessing the SAIM module can cause a hard fault
  262. * because the clock is not enabled.
  263. *
  264. * @param base SAI base pointer
  265. * @param config SAI configuration structure.
  266. */
  267. void SAI_TxInit(I2S_Type *base, const sai_config_t *config);
  268. /*!
  269. * @brief Initializes the SAI Rx peripheral.
  270. *
  271. * Ungates the SAI clock, resets the module, and configures the SAI Rx with a configuration structure.
  272. * The configuration structure can be custom filled or set with default values by
  273. * SAI_RxGetDefaultConfig().
  274. *
  275. * @note This API should be called at the beginning of the application to use
  276. * the SAI driver. Otherwise, accessing the SAI module can cause a hard fault
  277. * because the clock is not enabled.
  278. *
  279. * @param base SAI base pointer
  280. * @param config SAI configuration structure.
  281. */
  282. void SAI_RxInit(I2S_Type *base, const sai_config_t *config);
  283. /*!
  284. * @brief Sets the SAI Tx configuration structure to default values.
  285. *
  286. * This API initializes the configuration structure for use in SAI_TxConfig().
  287. * The initialized structure can remain unchanged in SAI_TxConfig(), or it can be modified
  288. * before calling SAI_TxConfig().
  289. * This is an example.
  290. @code
  291. sai_config_t config;
  292. SAI_TxGetDefaultConfig(&config);
  293. @endcode
  294. *
  295. * @param config pointer to master configuration structure
  296. */
  297. void SAI_TxGetDefaultConfig(sai_config_t *config);
  298. /*!
  299. * @brief Sets the SAI Rx configuration structure to default values.
  300. *
  301. * This API initializes the configuration structure for use in SAI_RxConfig().
  302. * The initialized structure can remain unchanged in SAI_RxConfig() or it can be modified
  303. * before calling SAI_RxConfig().
  304. * This is an example.
  305. @code
  306. sai_config_t config;
  307. SAI_RxGetDefaultConfig(&config);
  308. @endcode
  309. *
  310. * @param config pointer to master configuration structure
  311. */
  312. void SAI_RxGetDefaultConfig(sai_config_t *config);
  313. /*!
  314. * @brief De-initializes the SAI peripheral.
  315. *
  316. * This API gates the SAI clock. The SAI module can't operate unless SAI_TxInit
  317. * or SAI_RxInit is called to enable the clock.
  318. *
  319. * @param base SAI base pointer
  320. */
  321. void SAI_Deinit(I2S_Type *base);
  322. /*!
  323. * @brief Resets the SAI Tx.
  324. *
  325. * This function enables the software reset and FIFO reset of SAI Tx. After reset, clear the reset bit.
  326. *
  327. * @param base SAI base pointer
  328. */
  329. void SAI_TxReset(I2S_Type *base);
  330. /*!
  331. * @brief Resets the SAI Rx.
  332. *
  333. * This function enables the software reset and FIFO reset of SAI Rx. After reset, clear the reset bit.
  334. *
  335. * @param base SAI base pointer
  336. */
  337. void SAI_RxReset(I2S_Type *base);
  338. /*!
  339. * @brief Enables/disables the SAI Tx.
  340. *
  341. * @param base SAI base pointer
  342. * @param enable True means enable SAI Tx, false means disable.
  343. */
  344. void SAI_TxEnable(I2S_Type *base, bool enable);
  345. /*!
  346. * @brief Enables/disables the SAI Rx.
  347. *
  348. * @param base SAI base pointer
  349. * @param enable True means enable SAI Rx, false means disable.
  350. */
  351. void SAI_RxEnable(I2S_Type *base, bool enable);
  352. /*! @} */
  353. /*!
  354. * @name Status
  355. * @{
  356. */
  357. /*!
  358. * @brief Gets the SAI Tx status flag state.
  359. *
  360. * @param base SAI base pointer
  361. * @return SAI Tx status flag value. Use the Status Mask to get the status value needed.
  362. */
  363. static inline uint32_t SAI_TxGetStatusFlag(I2S_Type *base)
  364. {
  365. return base->TCSR;
  366. }
  367. /*!
  368. * @brief Clears the SAI Tx status flag state.
  369. *
  370. * @param base SAI base pointer
  371. * @param mask State mask. It can be a combination of the following source if defined:
  372. * @arg kSAI_WordStartFlag
  373. * @arg kSAI_SyncErrorFlag
  374. * @arg kSAI_FIFOErrorFlag
  375. */
  376. static inline void SAI_TxClearStatusFlags(I2S_Type *base, uint32_t mask)
  377. {
  378. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  379. }
  380. /*!
  381. * @brief Gets the SAI Tx status flag state.
  382. *
  383. * @param base SAI base pointer
  384. * @return SAI Rx status flag value. Use the Status Mask to get the status value needed.
  385. */
  386. static inline uint32_t SAI_RxGetStatusFlag(I2S_Type *base)
  387. {
  388. return base->RCSR;
  389. }
  390. /*!
  391. * @brief Clears the SAI Rx status flag state.
  392. *
  393. * @param base SAI base pointer
  394. * @param mask State mask. It can be a combination of the following sources if defined.
  395. * @arg kSAI_WordStartFlag
  396. * @arg kSAI_SyncErrorFlag
  397. * @arg kSAI_FIFOErrorFlag
  398. */
  399. static inline void SAI_RxClearStatusFlags(I2S_Type *base, uint32_t mask)
  400. {
  401. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  402. }
  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 Tx internal logic, including the bit clock, frame count etc. But software
  408. * reset will not clear any configuration registers like TCR1~TCR5.
  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_TxSoftwareReset(I2S_Type *base, sai_reset_type_t type);
  415. /*!
  416. * @brief Do software reset or FIFO reset .
  417. *
  418. * FIFO reset means clear all the data in the FIFO, and make the FIFO pointer both to 0.
  419. * Software reset means claer the Rx internal logic, including the bit clock, frame count etc. But software
  420. * reset will not clear any configuration registers like RCR1~RCR5.
  421. * This function will also clear all the error flags such as FIFO error, sync error etc.
  422. *
  423. * @param base SAI base pointer
  424. * @param type Reset type, FIFO reset or software reset
  425. */
  426. void SAI_RxSoftwareReset(I2S_Type *base, sai_reset_type_t type);
  427. /*!
  428. * @brief Set the Tx channel FIFO enable mask.
  429. *
  430. * @param base SAI base pointer
  431. * @param mask Channel enable mask, 0 means all channel FIFO disabled, 1 means channel 0 enabled,
  432. * 3 means both channel 0 and channel 1 enabled.
  433. */
  434. void SAI_TxSetChannelFIFOMask(I2S_Type *base, uint8_t mask);
  435. /*!
  436. * @brief Set the Rx channel FIFO enable mask.
  437. *
  438. * @param base SAI base pointer
  439. * @param mask Channel enable mask, 0 means all channel FIFO disabled, 1 means channel 0 enabled,
  440. * 3 means both channel 0 and channel 1 enabled.
  441. */
  442. void SAI_RxSetChannelFIFOMask(I2S_Type *base, uint8_t mask);
  443. /*!
  444. * @brief Set the Tx data order.
  445. *
  446. * @param base SAI base pointer
  447. * @param order Data order MSB or LSB
  448. */
  449. void SAI_TxSetDataOrder(I2S_Type *base, sai_data_order_t order);
  450. /*!
  451. * @brief Set the Rx data order.
  452. *
  453. * @param base SAI base pointer
  454. * @param order Data order MSB or LSB
  455. */
  456. void SAI_RxSetDataOrder(I2S_Type *base, sai_data_order_t order);
  457. /*!
  458. * @brief Set the Tx data order.
  459. *
  460. * @param base SAI base pointer
  461. * @param order Data order MSB or LSB
  462. */
  463. void SAI_TxSetBitClockPolarity(I2S_Type *base, sai_clock_polarity_t polarity);
  464. /*!
  465. * @brief Set the Rx data order.
  466. *
  467. * @param base SAI base pointer
  468. * @param order Data order MSB or LSB
  469. */
  470. void SAI_RxSetBitClockPolarity(I2S_Type *base, sai_clock_polarity_t polarity);
  471. /*!
  472. * @brief Set the Tx data order.
  473. *
  474. * @param base SAI base pointer
  475. * @param order Data order MSB or LSB
  476. */
  477. void SAI_TxSetFrameSyncPolarity(I2S_Type *base, sai_clock_polarity_t polarity);
  478. /*!
  479. * @brief Set the Rx data order.
  480. *
  481. * @param base SAI base pointer
  482. * @param order Data order MSB or LSB
  483. */
  484. void SAI_RxSetFrameSyncPolarity(I2S_Type *base, sai_clock_polarity_t polarity);
  485. #if defined(FSL_FEATURE_SAI_HAS_FIFO_PACKING) && FSL_FEATURE_SAI_HAS_FIFO_PACKING
  486. /*!
  487. * @brief Set Tx FIFO packing feature.
  488. *
  489. * @param base SAI base pointer.
  490. * @param pack FIFO pack type. It is element of sai_fifo_packing_t.
  491. */
  492. void SAI_TxSetFIFOPacking(I2S_Type *base, sai_fifo_packing_t pack);
  493. /*!
  494. * @brief Set Rx FIFO packing feature.
  495. *
  496. * @param base SAI base pointer.
  497. * @param pack FIFO pack type. It is element of sai_fifo_packing_t.
  498. */
  499. void SAI_RxSetFIFOPacking(I2S_Type *base, sai_fifo_packing_t pack);
  500. #endif /* FSL_FEATURE_SAI_HAS_FIFO_PACKING */
  501. #if defined(FSL_FEATURE_SAI_HAS_FIFO_FUNCTION_AFTER_ERROR) && FSL_FEATURE_SAI_HAS_FIFO_FUNCTION_AFTER_ERROR
  502. /*!
  503. * @brief Set Tx FIFO error continue.
  504. *
  505. * FIFO error continue mode means SAI will keep running while FIFO error occured. If this feature
  506. * not enabled, SAI will hang and users need to clear FEF flag in TCSR register.
  507. *
  508. * @param base SAI base pointer.
  509. * @param isEnabled Is FIFO error continue enabled, true means enable, false means disable.
  510. */
  511. static inline void SAI_TxSetFIFOErrorContinue(I2S_Type *base, bool isEnabled)
  512. {
  513. if (isEnabled)
  514. {
  515. base->TCR4 |= I2S_TCR4_FCONT_MASK;
  516. }
  517. else
  518. {
  519. base->TCR4 &= ~I2S_TCR4_FCONT_MASK;
  520. }
  521. }
  522. /*!
  523. * @brief Set Rx FIFO error continue.
  524. *
  525. * FIFO error continue mode means SAI will keep running while FIFO error occured. If this feature
  526. * not enabled, SAI will hang and users need to clear FEF flag in RCSR register.
  527. *
  528. * @param base SAI base pointer.
  529. * @param isEnabled Is FIFO error continue enabled, true means enable, false means disable.
  530. */
  531. static inline void SAI_RxSetFIFOErrorContinue(I2S_Type *base, bool isEnabled)
  532. {
  533. if (isEnabled)
  534. {
  535. base->RCR4 |= I2S_RCR4_FCONT_MASK;
  536. }
  537. else
  538. {
  539. base->RCR4 &= ~I2S_RCR4_FCONT_MASK;
  540. }
  541. }
  542. #endif
  543. /*! @} */
  544. /*!
  545. * @name Interrupts
  546. * @{
  547. */
  548. /*!
  549. * @brief Enables the SAI Tx interrupt requests.
  550. *
  551. * @param base SAI base pointer
  552. * @param mask interrupt source
  553. * The parameter can be a combination of the following sources if defined.
  554. * @arg kSAI_WordStartInterruptEnable
  555. * @arg kSAI_SyncErrorInterruptEnable
  556. * @arg kSAI_FIFOWarningInterruptEnable
  557. * @arg kSAI_FIFORequestInterruptEnable
  558. * @arg kSAI_FIFOErrorInterruptEnable
  559. */
  560. static inline void SAI_TxEnableInterrupts(I2S_Type *base, uint32_t mask)
  561. {
  562. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  563. }
  564. /*!
  565. * @brief Enables the SAI Rx interrupt requests.
  566. *
  567. * @param base SAI base pointer
  568. * @param mask interrupt source
  569. * The parameter can be a combination of the following sources if defined.
  570. * @arg kSAI_WordStartInterruptEnable
  571. * @arg kSAI_SyncErrorInterruptEnable
  572. * @arg kSAI_FIFOWarningInterruptEnable
  573. * @arg kSAI_FIFORequestInterruptEnable
  574. * @arg kSAI_FIFOErrorInterruptEnable
  575. */
  576. static inline void SAI_RxEnableInterrupts(I2S_Type *base, uint32_t mask)
  577. {
  578. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  579. }
  580. /*!
  581. * @brief Disables the SAI Tx interrupt requests.
  582. *
  583. * @param base SAI base pointer
  584. * @param mask interrupt source
  585. * The parameter can be a combination of the following sources if defined.
  586. * @arg kSAI_WordStartInterruptEnable
  587. * @arg kSAI_SyncErrorInterruptEnable
  588. * @arg kSAI_FIFOWarningInterruptEnable
  589. * @arg kSAI_FIFORequestInterruptEnable
  590. * @arg kSAI_FIFOErrorInterruptEnable
  591. */
  592. static inline void SAI_TxDisableInterrupts(I2S_Type *base, uint32_t mask)
  593. {
  594. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
  595. }
  596. /*!
  597. * @brief Disables the SAI Rx interrupt requests.
  598. *
  599. * @param base SAI base pointer
  600. * @param mask interrupt source
  601. * The parameter can be a combination of the following sources if defined.
  602. * @arg kSAI_WordStartInterruptEnable
  603. * @arg kSAI_SyncErrorInterruptEnable
  604. * @arg kSAI_FIFOWarningInterruptEnable
  605. * @arg kSAI_FIFORequestInterruptEnable
  606. * @arg kSAI_FIFOErrorInterruptEnable
  607. */
  608. static inline void SAI_RxDisableInterrupts(I2S_Type *base, uint32_t mask)
  609. {
  610. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
  611. }
  612. /*! @} */
  613. /*!
  614. * @name DMA Control
  615. * @{
  616. */
  617. /*!
  618. * @brief Enables/disables the SAI Tx DMA requests.
  619. * @param base SAI base pointer
  620. * @param mask DMA source
  621. * The parameter can be combination of the following sources if defined.
  622. * @arg kSAI_FIFOWarningDMAEnable
  623. * @arg kSAI_FIFORequestDMAEnable
  624. * @param enable True means enable DMA, false means disable DMA.
  625. */
  626. static inline void SAI_TxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
  627. {
  628. if (enable)
  629. {
  630. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) | mask);
  631. }
  632. else
  633. {
  634. base->TCSR = ((base->TCSR & 0xFFE3FFFFU) & (~mask));
  635. }
  636. }
  637. /*!
  638. * @brief Enables/disables the SAI Rx DMA requests.
  639. * @param base SAI base pointer
  640. * @param mask DMA source
  641. * The parameter can be a combination of the following sources if defined.
  642. * @arg kSAI_FIFOWarningDMAEnable
  643. * @arg kSAI_FIFORequestDMAEnable
  644. * @param enable True means enable DMA, false means disable DMA.
  645. */
  646. static inline void SAI_RxEnableDMA(I2S_Type *base, uint32_t mask, bool enable)
  647. {
  648. if (enable)
  649. {
  650. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) | mask);
  651. }
  652. else
  653. {
  654. base->RCSR = ((base->RCSR & 0xFFE3FFFFU) & (~mask));
  655. }
  656. }
  657. /*!
  658. * @brief Gets the SAI Tx data register address.
  659. *
  660. * This API is used to provide a transfer address for the SAI DMA transfer configuration.
  661. *
  662. * @param base SAI base pointer.
  663. * @param channel Which data channel used.
  664. * @return data register address.
  665. */
  666. static inline uint32_t SAI_TxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
  667. {
  668. return (uint32_t)(&(base->TDR)[channel]);
  669. }
  670. /*!
  671. * @brief Gets the SAI Rx data register address.
  672. *
  673. * This API is used to provide a transfer address for the SAI DMA transfer configuration.
  674. *
  675. * @param base SAI base pointer.
  676. * @param channel Which data channel used.
  677. * @return data register address.
  678. */
  679. static inline uint32_t SAI_RxGetDataRegisterAddress(I2S_Type *base, uint32_t channel)
  680. {
  681. return (uint32_t)(&(base->RDR)[channel]);
  682. }
  683. /*! @} */
  684. /*!
  685. * @name Bus Operations
  686. * @{
  687. */
  688. /*!
  689. * @brief Configures the SAI Tx audio format.
  690. *
  691. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  692. * format to be transferred.
  693. *
  694. * @param base SAI base pointer.
  695. * @param format Pointer to the SAI audio data format structure.
  696. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  697. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
  698. * clock, this value should equal the masterClockHz.
  699. */
  700. void SAI_TxSetFormat(I2S_Type *base,
  701. sai_transfer_format_t *format,
  702. uint32_t mclkSourceClockHz,
  703. uint32_t bclkSourceClockHz);
  704. /*!
  705. * @brief Configures the SAI Rx audio format.
  706. *
  707. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  708. * format to be transferred.
  709. *
  710. * @param base SAI base pointer.
  711. * @param format Pointer to the SAI audio data format structure.
  712. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  713. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If the bit clock source is a master
  714. * clock, this value should equal the masterClockHz.
  715. */
  716. void SAI_RxSetFormat(I2S_Type *base,
  717. sai_transfer_format_t *format,
  718. uint32_t mclkSourceClockHz,
  719. uint32_t bclkSourceClockHz);
  720. /*!
  721. * @brief Sends data using a blocking method.
  722. *
  723. * @note This function blocks by polling until data is ready to be sent.
  724. *
  725. * @param base SAI base pointer.
  726. * @param channel Data channel used.
  727. * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
  728. * @param buffer Pointer to the data to be written.
  729. * @param size Bytes to be written.
  730. */
  731. void SAI_WriteBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
  732. /*!
  733. * @brief Writes data into SAI FIFO.
  734. *
  735. * @param base SAI base pointer.
  736. * @param channel Data channel used.
  737. * @param data Data needs to be written.
  738. */
  739. static inline void SAI_WriteData(I2S_Type *base, uint32_t channel, uint32_t data)
  740. {
  741. base->TDR[channel] = data;
  742. }
  743. /*!
  744. * @brief Receives data using a blocking method.
  745. *
  746. * @note This function blocks by polling until data is ready to be sent.
  747. *
  748. * @param base SAI base pointer.
  749. * @param channel Data channel used.
  750. * @param bitWidth How many bits in an audio word; usually 8/16/24/32 bits.
  751. * @param buffer Pointer to the data to be read.
  752. * @param size Bytes to be read.
  753. */
  754. void SAI_ReadBlocking(I2S_Type *base, uint32_t channel, uint32_t bitWidth, uint8_t *buffer, uint32_t size);
  755. /*!
  756. * @brief Reads data from the SAI FIFO.
  757. *
  758. * @param base SAI base pointer.
  759. * @param channel Data channel used.
  760. * @return Data in SAI FIFO.
  761. */
  762. static inline uint32_t SAI_ReadData(I2S_Type *base, uint32_t channel)
  763. {
  764. return base->RDR[channel];
  765. }
  766. /*! @} */
  767. /*!
  768. * @name Transactional
  769. * @{
  770. */
  771. /*!
  772. * @brief Initializes the SAI Tx handle.
  773. *
  774. * This function initializes the Tx handle for the SAI Tx transactional APIs. Call
  775. * this function once to get the handle initialized.
  776. *
  777. * @param base SAI base pointer
  778. * @param handle SAI handle pointer.
  779. * @param callback Pointer to the user callback function.
  780. * @param userData User parameter passed to the callback function
  781. */
  782. void SAI_TransferTxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
  783. /*!
  784. * @brief Initializes the SAI Rx handle.
  785. *
  786. * This function initializes the Rx handle for the SAI Rx transactional APIs. Call
  787. * this function once to get the handle initialized.
  788. *
  789. * @param base SAI base pointer.
  790. * @param handle SAI handle pointer.
  791. * @param callback Pointer to the user callback function.
  792. * @param userData User parameter passed to the callback function.
  793. */
  794. void SAI_TransferRxCreateHandle(I2S_Type *base, sai_handle_t *handle, sai_transfer_callback_t callback, void *userData);
  795. /*!
  796. * @brief Configures the SAI Tx audio format.
  797. *
  798. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  799. * format to be transferred.
  800. *
  801. * @param base SAI base pointer.
  802. * @param handle SAI handle pointer.
  803. * @param format Pointer to the SAI audio data format structure.
  804. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  805. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
  806. * clock, this value should equal the masterClockHz in format.
  807. * @return Status of this function. Return value is the status_t.
  808. */
  809. status_t SAI_TransferTxSetFormat(I2S_Type *base,
  810. sai_handle_t *handle,
  811. sai_transfer_format_t *format,
  812. uint32_t mclkSourceClockHz,
  813. uint32_t bclkSourceClockHz);
  814. /*!
  815. * @brief Configures the SAI Rx audio format.
  816. *
  817. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  818. * format to be transferred.
  819. *
  820. * @param base SAI base pointer.
  821. * @param handle SAI handle pointer.
  822. * @param format Pointer to the SAI audio data format structure.
  823. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  824. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is a master
  825. * clock, this value should equal the masterClockHz in format.
  826. * @return Status of this function. Return value is one of status_t.
  827. */
  828. status_t SAI_TransferRxSetFormat(I2S_Type *base,
  829. sai_handle_t *handle,
  830. sai_transfer_format_t *format,
  831. uint32_t mclkSourceClockHz,
  832. uint32_t bclkSourceClockHz);
  833. /*!
  834. * @brief Performs an interrupt non-blocking send transfer on SAI.
  835. *
  836. * @note This API returns immediately after the transfer initiates.
  837. * Call the SAI_TxGetTransferStatusIRQ to poll the transfer status and check whether
  838. * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
  839. * is finished.
  840. *
  841. * @param base SAI base pointer.
  842. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  843. * @param xfer Pointer to the sai_transfer_t structure.
  844. * @retval kStatus_Success Successfully started the data receive.
  845. * @retval kStatus_SAI_TxBusy Previous receive still not finished.
  846. * @retval kStatus_InvalidArgument The input parameter is invalid.
  847. */
  848. status_t SAI_TransferSendNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
  849. /*!
  850. * @brief Performs an interrupt non-blocking receive transfer on SAI.
  851. *
  852. * @note This API returns immediately after the transfer initiates.
  853. * Call the SAI_RxGetTransferStatusIRQ to poll the transfer status and check whether
  854. * the transfer is finished. If the return status is not kStatus_SAI_Busy, the transfer
  855. * is finished.
  856. *
  857. * @param base SAI base pointer
  858. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  859. * @param xfer Pointer to the sai_transfer_t structure.
  860. * @retval kStatus_Success Successfully started the data receive.
  861. * @retval kStatus_SAI_RxBusy Previous receive still not finished.
  862. * @retval kStatus_InvalidArgument The input parameter is invalid.
  863. */
  864. status_t SAI_TransferReceiveNonBlocking(I2S_Type *base, sai_handle_t *handle, sai_transfer_t *xfer);
  865. /*!
  866. * @brief Gets a set byte count.
  867. *
  868. * @param base SAI base pointer.
  869. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  870. * @param count Bytes count sent.
  871. * @retval kStatus_Success Succeed get the transfer count.
  872. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  873. */
  874. status_t SAI_TransferGetSendCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
  875. /*!
  876. * @brief Gets a received byte count.
  877. *
  878. * @param base SAI base pointer.
  879. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  880. * @param count Bytes count received.
  881. * @retval kStatus_Success Succeed get the transfer count.
  882. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  883. */
  884. status_t SAI_TransferGetReceiveCount(I2S_Type *base, sai_handle_t *handle, size_t *count);
  885. /*!
  886. * @brief Aborts the current send.
  887. *
  888. * @note This API can be called any time when an interrupt non-blocking transfer initiates
  889. * to abort the transfer early.
  890. *
  891. * @param base SAI base pointer.
  892. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  893. */
  894. void SAI_TransferAbortSend(I2S_Type *base, sai_handle_t *handle);
  895. /*!
  896. * @brief Aborts the current IRQ receive.
  897. *
  898. * @note This API can be called when an interrupt non-blocking transfer initiates
  899. * to abort the transfer early.
  900. *
  901. * @param base SAI base pointer
  902. * @param handle Pointer to the sai_handle_t structure which stores the transfer state.
  903. */
  904. void SAI_TransferAbortReceive(I2S_Type *base, sai_handle_t *handle);
  905. /*!
  906. * @brief Terminate all SAI send.
  907. *
  908. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  909. * current transfer slot, please call SAI_TransferAbortSend.
  910. *
  911. * @param base SAI base pointer.
  912. * @param handle SAI eDMA handle pointer.
  913. */
  914. void SAI_TransferTerminateSend(I2S_Type *base, sai_handle_t *handle);
  915. /*!
  916. * @brief Terminate all SAI receive.
  917. *
  918. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  919. * current transfer slot, please call SAI_TransferAbortReceive.
  920. *
  921. * @param base SAI base pointer.
  922. * @param handle SAI eDMA handle pointer.
  923. */
  924. void SAI_TransferTerminateReceive(I2S_Type *base, sai_handle_t *handle);
  925. /*!
  926. * @brief Tx interrupt handler.
  927. *
  928. * @param base SAI base pointer.
  929. * @param handle Pointer to the sai_handle_t structure.
  930. */
  931. void SAI_TransferTxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
  932. /*!
  933. * @brief Tx interrupt handler.
  934. *
  935. * @param base SAI base pointer.
  936. * @param handle Pointer to the sai_handle_t structure.
  937. */
  938. void SAI_TransferRxHandleIRQ(I2S_Type *base, sai_handle_t *handle);
  939. /*! @} */
  940. #if defined(__cplusplus)
  941. }
  942. #endif /*_cplusplus*/
  943. /*! @} */
  944. #endif /* _FSL_SAI_H_ */