fsl_i2s.h 16 KB

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