fsl_i2s.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_I2S_H_
  35. #define _FSL_I2S_H_
  36. #include "fsl_device_registers.h"
  37. #include "fsl_common.h"
  38. #include "fsl_flexcomm.h"
  39. /*******************************************************************************
  40. * Definitions
  41. ******************************************************************************/
  42. /*!
  43. * @addtogroup i2s_driver
  44. * @{
  45. */
  46. /*! @file */
  47. /*! @name Driver version */
  48. /*@{*/
  49. /*! @brief I2S driver version 2.0.1.
  50. *
  51. * Current version: 2.0.1
  52. *
  53. * Change log:
  54. * - Version 2.0.1
  55. * - Unify component full name to FLEXCOMM I2S(DMA) Driver
  56. * - Version 2.0.0
  57. * - initial version
  58. */
  59. #define FSL_I2S_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  60. /*@}*/
  61. #ifndef I2S_NUM_BUFFERS
  62. /*! @brief Number of buffers . */
  63. #define I2S_NUM_BUFFERS (4)
  64. #endif
  65. /*! @brief I2S status codes. */
  66. enum _i2s_status
  67. {
  68. kStatus_I2S_BufferComplete =
  69. MAKE_STATUS(kStatusGroup_I2S, 0), /*!< Transfer from/into a single buffer has completed */
  70. kStatus_I2S_Done = MAKE_STATUS(kStatusGroup_I2S, 1), /*!< All buffers transfers have completed */
  71. kStatus_I2S_Busy =
  72. MAKE_STATUS(kStatusGroup_I2S, 2), /*!< Already performing a transfer and cannot queue another buffer */
  73. };
  74. /*!
  75. * @brief I2S flags.
  76. *
  77. * @note These enums are meant to be OR'd together to form a bit mask.
  78. */
  79. typedef enum _i2s_flags
  80. {
  81. kI2S_TxErrorFlag = I2S_FIFOINTENSET_TXERR_MASK, /*!< TX error interrupt */
  82. kI2S_TxLevelFlag = I2S_FIFOINTENSET_TXLVL_MASK, /*!< TX level interrupt */
  83. kI2S_RxErrorFlag = I2S_FIFOINTENSET_RXERR_MASK, /*!< RX error interrupt */
  84. kI2S_RxLevelFlag = I2S_FIFOINTENSET_RXLVL_MASK /*!< RX level interrupt */
  85. } i2s_flags_t;
  86. /*! @brief Master / slave mode. */
  87. typedef enum _i2s_master_slave
  88. {
  89. kI2S_MasterSlaveNormalSlave = 0x0, /*!< Normal slave */
  90. kI2S_MasterSlaveWsSyncMaster = 0x1, /*!< WS synchronized master */
  91. kI2S_MasterSlaveExtSckMaster = 0x2, /*!< Master using existing SCK */
  92. kI2S_MasterSlaveNormalMaster = 0x3 /*!< Normal master */
  93. } i2s_master_slave_t;
  94. /*! @brief I2S mode. */
  95. typedef enum _i2s_mode
  96. {
  97. kI2S_ModeI2sClassic = 0x0, /*!< I2S classic mode */
  98. kI2S_ModeDspWs50 = 0x1, /*!< DSP mode, WS having 50% duty cycle */
  99. kI2S_ModeDspWsShort = 0x2, /*!< DSP mode, WS having one clock long pulse */
  100. kI2S_ModeDspWsLong = 0x3 /*!< DSP mode, WS having one data slot long pulse */
  101. } i2s_mode_t;
  102. /*! @brief I2S configuration structure. */
  103. typedef struct _i2s_config
  104. {
  105. i2s_master_slave_t masterSlave; /*!< Master / slave configuration */
  106. i2s_mode_t mode; /*!< I2S mode */
  107. bool rightLow; /*!< Right channel data in low portion of FIFO */
  108. bool leftJust; /*!< Left justify data in FIFO */
  109. bool pdmData; /*!< Data source is the D-Mic subsystem */
  110. bool sckPol; /*!< SCK polarity */
  111. bool wsPol; /*!< WS polarity */
  112. uint16_t divider; /*!< Flexcomm function clock divider (1 - 4096) */
  113. bool oneChannel; /*!< true mono, false stereo */
  114. uint8_t dataLength; /*!< Data length (4 - 32) */
  115. uint16_t frameLength; /*!< Frame width (4 - 512) */
  116. uint16_t position; /*!< Data position in the frame */
  117. uint8_t watermark; /*!< FIFO trigger level */
  118. bool txEmptyZero; /*!< Transmit zero when buffer becomes empty or last item */
  119. bool pack48; /*!< Packing format for 48-bit data (false - 24 bit values, true - alternating 32-bit and 16-bit
  120. values) */
  121. } i2s_config_t;
  122. /*! @brief Buffer to transfer from or receive audio data into. */
  123. typedef struct _i2s_transfer
  124. {
  125. volatile uint8_t *data; /*!< Pointer to data buffer. */
  126. volatile size_t dataSize; /*!< Buffer size in bytes. */
  127. } i2s_transfer_t;
  128. /*! @brief Transactional state of the intialized transfer or receive I2S operation. */
  129. typedef struct _i2s_handle i2s_handle_t;
  130. /*!
  131. * @brief Callback function invoked from transactional API
  132. * on completion of a single buffer transfer.
  133. *
  134. * @param base I2S base pointer.
  135. * @param handle pointer to I2S transaction.
  136. * @param completionStatus status of the transaction.
  137. * @param userData optional pointer to user arguments data.
  138. */
  139. typedef void (*i2s_transfer_callback_t)(I2S_Type *base,
  140. i2s_handle_t *handle,
  141. status_t completionStatus,
  142. void *userData);
  143. /*! @brief Members not to be accessed / modified outside of the driver. */
  144. struct _i2s_handle
  145. {
  146. uint32_t state; /*!< State of transfer */
  147. i2s_transfer_callback_t completionCallback; /*!< Callback function pointer */
  148. void *userData; /*!< Application data passed to callback */
  149. bool oneChannel; /*!< true mono, false stereo */
  150. uint8_t dataLength; /*!< Data length (4 - 32) */
  151. bool pack48; /*!< Packing format for 48-bit data (false - 24 bit values, true - alternating 32-bit and 16-bit
  152. values) */
  153. bool useFifo48H; /*!< When dataLength 17-24: true use FIFOWR48H, false use FIFOWR */
  154. volatile i2s_transfer_t i2sQueue[I2S_NUM_BUFFERS]; /*!< Transfer queue storing transfer buffers */
  155. volatile uint8_t queueUser; /*!< Queue index where user's next transfer will be stored */
  156. volatile uint8_t queueDriver; /*!< Queue index of buffer actually used by the driver */
  157. volatile uint32_t errorCount; /*!< Number of buffer underruns/overruns */
  158. volatile uint32_t transferCount; /*!< Number of bytes transferred */
  159. volatile uint8_t watermark; /*!< FIFO trigger level */
  160. };
  161. /*******************************************************************************
  162. * API
  163. ******************************************************************************/
  164. #if defined(__cplusplus)
  165. extern "C" {
  166. #endif
  167. /*!
  168. * @name Initialization and deinitialization
  169. * @{
  170. */
  171. /*!
  172. * @brief Initializes the FLEXCOMM peripheral for I2S transmit functionality.
  173. *
  174. * Ungates the FLEXCOMM clock and configures the module
  175. * for I2S transmission using a configuration structure.
  176. * The configuration structure can be custom filled or set with default values by
  177. * I2S_TxGetDefaultConfig().
  178. *
  179. * @note This API should be called at the beginning of the application to use
  180. * the I2S driver.
  181. *
  182. * @param base I2S base pointer.
  183. * @param config pointer to I2S configuration structure.
  184. */
  185. void I2S_TxInit(I2S_Type *base, const i2s_config_t *config);
  186. /*!
  187. * @brief Initializes the FLEXCOMM peripheral for I2S receive functionality.
  188. *
  189. * Ungates the FLEXCOMM clock and configures the module
  190. * for I2S receive using a configuration structure.
  191. * The configuration structure can be custom filled or set with default values by
  192. * I2S_RxGetDefaultConfig().
  193. *
  194. * @note This API should be called at the beginning of the application to use
  195. * the I2S driver.
  196. *
  197. * @param base I2S base pointer.
  198. * @param config pointer to I2S configuration structure.
  199. */
  200. void I2S_RxInit(I2S_Type *base, const i2s_config_t *config);
  201. /*!
  202. * @brief Sets the I2S Tx configuration structure to default values.
  203. *
  204. * This API initializes the configuration structure for use in I2S_TxInit().
  205. * The initialized structure can remain unchanged in I2S_TxInit(), or it can be modified
  206. * before calling I2S_TxInit().
  207. * Example:
  208. @code
  209. i2s_config_t config;
  210. I2S_TxGetDefaultConfig(&config);
  211. @endcode
  212. *
  213. * Default values:
  214. * @code
  215. * config->masterSlave = kI2S_MasterSlaveNormalMaster;
  216. * config->mode = kI2S_ModeI2sClassic;
  217. * config->rightLow = false;
  218. * config->leftJust = false;
  219. * config->pdmData = false;
  220. * config->sckPol = false;
  221. * config->wsPol = false;
  222. * config->divider = 1;
  223. * config->oneChannel = false;
  224. * config->dataLength = 16;
  225. * config->frameLength = 32;
  226. * config->position = 0;
  227. * config->watermark = 4;
  228. * config->txEmptyZero = true;
  229. * config->pack48 = false;
  230. * @endcode
  231. *
  232. * @param config pointer to I2S configuration structure.
  233. */
  234. void I2S_TxGetDefaultConfig(i2s_config_t *config);
  235. /*!
  236. * @brief Sets the I2S Rx configuration structure to default values.
  237. *
  238. * This API initializes the configuration structure for use in I2S_RxInit().
  239. * The initialized structure can remain unchanged in I2S_RxInit(), or it can be modified
  240. * before calling I2S_RxInit().
  241. * Example:
  242. @code
  243. i2s_config_t config;
  244. I2S_RxGetDefaultConfig(&config);
  245. @endcode
  246. *
  247. * Default values:
  248. * @code
  249. * config->masterSlave = kI2S_MasterSlaveNormalSlave;
  250. * config->mode = kI2S_ModeI2sClassic;
  251. * config->rightLow = false;
  252. * config->leftJust = false;
  253. * config->pdmData = false;
  254. * config->sckPol = false;
  255. * config->wsPol = false;
  256. * config->divider = 1;
  257. * config->oneChannel = false;
  258. * config->dataLength = 16;
  259. * config->frameLength = 32;
  260. * config->position = 0;
  261. * config->watermark = 4;
  262. * config->txEmptyZero = false;
  263. * config->pack48 = false;
  264. * @endcode
  265. *
  266. * @param config pointer to I2S configuration structure.
  267. */
  268. void I2S_RxGetDefaultConfig(i2s_config_t *config);
  269. /*!
  270. * @brief De-initializes the I2S peripheral.
  271. *
  272. * This API gates the FLEXCOMM clock. The I2S module can't operate unless I2S_TxInit
  273. * or I2S_RxInit is called to enable the clock.
  274. *
  275. * @param base I2S base pointer.
  276. */
  277. void I2S_Deinit(I2S_Type *base);
  278. /*! @} */
  279. /*!
  280. * @name Non-blocking API
  281. * @{
  282. */
  283. /*!
  284. * @brief Initializes handle for transfer of audio data.
  285. *
  286. * @param base I2S base pointer.
  287. * @param handle pointer to handle structure.
  288. * @param callback function to be called back when transfer is done or fails.
  289. * @param userData pointer to data passed to callback.
  290. */
  291. void I2S_TxTransferCreateHandle(I2S_Type *base, i2s_handle_t *handle, i2s_transfer_callback_t callback, void *userData);
  292. /*!
  293. * @brief Begins or queue sending of the given data.
  294. *
  295. * @param base I2S base pointer.
  296. * @param handle pointer to handle structure.
  297. * @param transfer data buffer.
  298. *
  299. * @retval kStatus_Success
  300. * @retval kStatus_I2S_Busy if all queue slots are occupied with unsent buffers.
  301. */
  302. status_t I2S_TxTransferNonBlocking(I2S_Type *base, i2s_handle_t *handle, i2s_transfer_t transfer);
  303. /*!
  304. * @brief Aborts sending of data.
  305. *
  306. * @param base I2S base pointer.
  307. * @param handle pointer to handle structure.
  308. */
  309. void I2S_TxTransferAbort(I2S_Type *base, i2s_handle_t *handle);
  310. /*!
  311. * @brief Initializes handle for reception of audio data.
  312. *
  313. * @param base I2S base pointer.
  314. * @param handle pointer to handle structure.
  315. * @param callback function to be called back when transfer is done or fails.
  316. * @param userData pointer to data passed to callback.
  317. */
  318. void I2S_RxTransferCreateHandle(I2S_Type *base, i2s_handle_t *handle, i2s_transfer_callback_t callback, void *userData);
  319. /*!
  320. * @brief Begins or queue reception of data into given buffer.
  321. *
  322. * @param base I2S base pointer.
  323. * @param handle pointer to handle structure.
  324. * @param transfer data buffer.
  325. *
  326. * @retval kStatus_Success
  327. * @retval kStatus_I2S_Busy if all queue slots are occupied with buffers which are not full.
  328. */
  329. status_t I2S_RxTransferNonBlocking(I2S_Type *base, i2s_handle_t *handle, i2s_transfer_t transfer);
  330. /*!
  331. * @brief Aborts receiving of data.
  332. *
  333. * @param base I2S base pointer.
  334. * @param handle pointer to handle structure.
  335. */
  336. void I2S_RxTransferAbort(I2S_Type *base, i2s_handle_t *handle);
  337. /*!
  338. * @brief Returns number of bytes transferred so far.
  339. *
  340. * @param base I2S base pointer.
  341. * @param handle pointer to handle structure.
  342. * @param[out] count number of bytes transferred so far by the non-blocking transaction.
  343. *
  344. * @retval kStatus_Success
  345. * @retval kStatus_NoTransferInProgress there is no non-blocking transaction currently in progress.
  346. */
  347. status_t I2S_TransferGetCount(I2S_Type *base, i2s_handle_t *handle, size_t *count);
  348. /*!
  349. * @brief Returns number of buffer underruns or overruns.
  350. *
  351. * @param base I2S base pointer.
  352. * @param handle pointer to handle structure.
  353. * @param[out] count number of transmit errors encountered so far by the non-blocking transaction.
  354. *
  355. * @retval kStatus_Success
  356. * @retval kStatus_NoTransferInProgress there is no non-blocking transaction currently in progress.
  357. */
  358. status_t I2S_TransferGetErrorCount(I2S_Type *base, i2s_handle_t *handle, size_t *count);
  359. /*! @} */
  360. /*!
  361. * @name Enable / disable
  362. * @{
  363. */
  364. /*!
  365. * @brief Enables I2S operation.
  366. *
  367. * @param base I2S base pointer.
  368. */
  369. static inline void I2S_Enable(I2S_Type *base)
  370. {
  371. base->CFG1 |= I2S_CFG1_MAINENABLE(1U);
  372. }
  373. /*!
  374. * @brief Disables I2S operation.
  375. *
  376. * @param base I2S base pointer.
  377. */
  378. static inline void I2S_Disable(I2S_Type *base)
  379. {
  380. base->CFG1 &= (~I2S_CFG1_MAINENABLE(1U));
  381. }
  382. /*! @} */
  383. /*!
  384. * @name Interrupts
  385. * @{
  386. */
  387. /*!
  388. * @brief Enables I2S FIFO interrupts.
  389. *
  390. * @param base I2S base pointer.
  391. * @param interruptMask bit mask of interrupts to enable. See #i2s_flags_t for the set
  392. * of constants that should be OR'd together to form the bit mask.
  393. */
  394. static inline void I2S_EnableInterrupts(I2S_Type *base, uint32_t interruptMask)
  395. {
  396. base->FIFOINTENSET = interruptMask;
  397. }
  398. /*!
  399. * @brief Disables I2S FIFO interrupts.
  400. *
  401. * @param base I2S base pointer.
  402. * @param interruptMask bit mask of interrupts to enable. See #i2s_flags_t for the set
  403. * of constants that should be OR'd together to form the bit mask.
  404. */
  405. static inline void I2S_DisableInterrupts(I2S_Type *base, uint32_t interruptMask)
  406. {
  407. base->FIFOINTENCLR = interruptMask;
  408. }
  409. /*!
  410. * @brief Returns the set of currently enabled I2S FIFO interrupts.
  411. *
  412. * @param base I2S base pointer.
  413. *
  414. * @return A bitmask composed of #i2s_flags_t enumerators OR'd together
  415. * to indicate the set of enabled interrupts.
  416. */
  417. static inline uint32_t I2S_GetEnabledInterrupts(I2S_Type *base)
  418. {
  419. return base->FIFOINTENSET;
  420. }
  421. /*!
  422. * @brief Invoked from interrupt handler when transmit FIFO level decreases.
  423. *
  424. * @param base I2S base pointer.
  425. * @param handle pointer to handle structure.
  426. */
  427. void I2S_TxHandleIRQ(I2S_Type *base, i2s_handle_t *handle);
  428. /*!
  429. * @brief Invoked from interrupt handler when receive FIFO level decreases.
  430. *
  431. * @param base I2S base pointer.
  432. * @param handle pointer to handle structure.
  433. */
  434. void I2S_RxHandleIRQ(I2S_Type *base, i2s_handle_t *handle);
  435. /*! @} */
  436. /*! @} */
  437. #if defined(__cplusplus)
  438. }
  439. #endif
  440. #endif /* _FSL_I2S_H_ */