fsl_spdif.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * All rights reserved.
  5. *
  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 Freescale Semiconductor, Inc. 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_SPDIF_H_
  35. #define _FSL_SPDIF_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup spdif
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. #define FSL_SPDIF_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  47. /*@}*/
  48. /*! @brief SPDIF return status*/
  49. enum _spdif_status_t
  50. {
  51. kStatus_SPDIF_RxDPLLLocked = MAKE_STATUS(kStatusGroup_SPDIF, 0), /*!< SPDIF Rx PLL locked. */
  52. kStatus_SPDIF_TxFIFOError = MAKE_STATUS(kStatusGroup_SPDIF, 1), /*!< SPDIF Tx FIFO error. */
  53. kStatus_SPDIF_TxFIFOResync = MAKE_STATUS(kStatusGroup_SPDIF, 2), /*!< SPDIF Tx left and right FIFO resync. */
  54. kStatus_SPDIF_RxCnew = MAKE_STATUS(kStatusGroup_SPDIF, 3), /*!< SPDIF Rx status channel value updated. */
  55. kStatus_SPDIF_ValidatyNoGood = MAKE_STATUS(kStatusGroup_SPDIF, 4), /*!< SPDIF validaty flag not good. */
  56. kStatus_SPDIF_RxIllegalSymbol = MAKE_STATUS(kStatusGroup_SPDIF, 5), /*!< SPDIF Rx receive illegal symbol. */
  57. kStatus_SPDIF_RxParityBitError = MAKE_STATUS(kStatusGroup_SPDIF, 6), /*!< SPDIF Rx parity bit error. */
  58. kStatus_SPDIF_UChannelOverrun = MAKE_STATUS(kStatusGroup_SPDIF, 7), /*!< SPDIF receive U channel overrun. */
  59. kStatus_SPDIF_QChannelOverrun = MAKE_STATUS(kStatusGroup_SPDIF, 8), /*!< SPDIF receive Q channel overrun. */
  60. kStatus_SPDIF_UQChannelSync = MAKE_STATUS(kStatusGroup_SPDIF, 9), /*!< SPDIF U/Q channel sync found. */
  61. kStatus_SPDIF_UQChannelFrameError = MAKE_STATUS(kStatusGroup_SPDIF, 10), /*!< SPDIF U/Q channel frame error. */
  62. kStatus_SPDIF_RxFIFOError = MAKE_STATUS(kStatusGroup_SPDIF, 11), /*!< SPDIF Rx FIFO error. */
  63. kStatus_SPDIF_RxFIFOResync = MAKE_STATUS(kStatusGroup_SPDIF, 12), /*!< SPDIF Rx left and right FIFO resync. */
  64. kStatus_SPDIF_LockLoss = MAKE_STATUS(kStatusGroup_SPDIF, 13), /*!< SPDIF Rx PLL clock lock loss. */
  65. kStatus_SPDIF_TxIdle = MAKE_STATUS(kStatusGroup_SPDIF, 14), /*!< SPDIF Tx is idle */
  66. kStatus_SPDIF_RxIdle = MAKE_STATUS(kStatusGroup_SPDIF, 15), /*!< SPDIF Rx is idle */
  67. kStatus_SPDIF_QueueFull = MAKE_STATUS(kStatusGroup_SPDIF, 16) /*!< SPDIF queue full */
  68. };
  69. /*! @brief SPDIF Rx FIFO full falg select, it decides when assert the rx full flag */
  70. typedef enum _spdif_rxfull_select
  71. {
  72. kSPDIF_RxFull1Sample = 0x0u, /*!< Rx full at least 1 sample in left and right FIFO */
  73. kSPDIF_RxFull4Samples, /*!< Rx full at least 4 sample in left and right FIFO*/
  74. kSPDIF_RxFull8Samples, /*!< Rx full at least 8 sample in left and right FIFO*/
  75. kSPDIF_RxFull16Samples, /*!< Rx full at least 16 sample in left and right FIFO*/
  76. } spdif_rxfull_select_t;
  77. /*! @brief SPDIF tx FIFO EMPTY falg select, it decides when assert the tx empty flag */
  78. typedef enum _spdif_txempty_select
  79. {
  80. kSPDIF_TxEmpty0Sample = 0x0u, /*!< Tx empty at most 0 sample in left and right FIFO */
  81. kSPDIF_TxEmpty4Samples, /*!< Tx empty at most 4 sample in left and right FIFO*/
  82. kSPDIF_TxEmpty8Samples, /*!< Tx empty at most 8 sample in left and right FIFO*/
  83. kSPDIF_TxEmpty12Samples, /*!< Tx empty at most 12 sample in left and right FIFO*/
  84. } spdif_txempty_select_t;
  85. /*! @brief SPDIF U channel source */
  86. typedef enum _spdif_uchannel_source
  87. {
  88. kSPDIF_NoUChannel = 0x0U, /*!< No embedded U channel */
  89. kSPDIF_UChannelFromRx = 0x1U, /*!< U channel from receiver, it is CD mode */
  90. kSPDIF_UChannelFromTx = 0x3U, /*!< U channel from on chip tx */
  91. } spdif_uchannel_source_t;
  92. /*! @brief SPDIF clock gain*/
  93. typedef enum _spdif_gain_select
  94. {
  95. kSPDIF_GAIN_24 = 0x0U, /*!< Gain select is 24 */
  96. kSPDIF_GAIN_16, /*!< Gain select is 16 */
  97. kSPDIF_GAIN_12, /*!< Gain select is 12 */
  98. kSPDIF_GAIN_8, /*!< Gain select is 8 */
  99. kSPDIF_GAIN_6, /*!< Gain select is 6 */
  100. kSPDIF_GAIN_4, /*!< Gain select is 4 */
  101. kSPDIF_GAIN_3, /*!< Gain select is 3 */
  102. } spdif_gain_select_t;
  103. /*! @brief SPDIF tx data source */
  104. typedef enum _spdif_tx_source
  105. {
  106. kSPDIF_txFromReceiver = 0x1U, /*!< Tx data directly through SPDIF receiver */
  107. kSPDIF_txNormal = 0x5U, /*!< Normal operation, data from processor */
  108. } spdif_tx_source_t;
  109. /*! @brief SPDIF tx data source */
  110. typedef enum _spdif_validity_config
  111. {
  112. kSPDIF_validityFlagAlwaysSet = 0x0U, /*!< Outgoing validity flags always set */
  113. kSPDIF_validityFlagAlwaysClear, /*!< Outgoing validity flags always clear */
  114. } spdif_validity_config_t;
  115. /*! @brief The SPDIF interrupt enable flag */
  116. enum _spdif_interrupt_enable_t
  117. {
  118. kSPDIF_RxDPLLLocked = SPDIF_SIE_LOCK_MASK, /*!< SPDIF DPLL locked */
  119. kSPDIF_TxFIFOError = SPDIF_SIE_TXUNOV_MASK, /*!< Tx FIFO underrun or overrun */
  120. kSPDIF_TxFIFOResync = SPDIF_SIE_TXRESYN_MASK, /*!< Tx FIFO left and right channel resync */
  121. kSPDIF_RxControlChannelChange = SPDIF_SIE_CNEW_MASK, /*!< SPDIF Rx control channel value changed */
  122. kSPDIF_ValidityFlagNoGood = SPDIF_SIE_VALNOGOOD_MASK, /*!< SPDIF validity flag no good */
  123. kSPDIF_RxIllegalSymbol = SPDIF_SIE_SYMERR_MASK, /*!< SPDIF receiver found illegal symbol */
  124. kSPDIF_RxParityBitError = SPDIF_SIE_BITERR_MASK, /*!< SPDIF receiver found parity bit error */
  125. kSPDIF_UChannelReceiveRegisterFull = SPDIF_SIE_URXFUL_MASK, /*!< SPDIF U channel revceive register full */
  126. kSPDIF_UChannelReceiveRegisterOverrun = SPDIF_SIE_URXOV_MASK, /*!< SPDIF U channel receive register overrun */
  127. kSPDIF_QChannelReceiveRegisterFull = SPDIF_SIE_QRXFUL_MASK, /*!< SPDIF Q channel receive reigster full */
  128. kSPDIF_QChannelReceiveRegisterOverrun = SPDIF_SIE_QRXOV_MASK, /*!< SPDIF Q channel receive register overrun */
  129. kSPDIF_UQChannelSync = SPDIF_SIE_UQSYNC_MASK, /*!< SPDIF U/Q channel sync found */
  130. kSPDIF_UQChannelFrameError = SPDIF_SIE_UQERR_MASK, /*!< SPDIF U/Q channel frame error */
  131. kSPDIF_RxFIFOError = SPDIF_SIE_RXFIFOUNOV_MASK, /*!< SPDIF Rx FIFO underrun/overrun */
  132. kSPDIF_RxFIFOResync = SPDIF_SIE_RXFIFORESYN_MASK, /*!< SPDIF Rx left and right FIFO resync */
  133. kSPDIF_LockLoss = SPDIF_SIE_LOCKLOSS_MASK, /*!< SPDIF receiver loss of lock */
  134. kSPDIF_TxFIFOEmpty = SPDIF_SIE_TXEM_MASK, /*!< SPDIF Tx FIFO empty */
  135. kSPDIF_RxFIFOFull = SPDIF_SIE_RXFIFOFUL_MASK /*!< SPDIF Rx FIFO full */
  136. };
  137. /*! @brief The DMA request sources */
  138. enum _spdif_dma_enable_t
  139. {
  140. kSPDIF_RxDMAEnable = SPDIF_SCR_DMA_RX_EN_MASK, /*!< Rx FIFO full */
  141. kSPDIF_TxDMAEnable = SPDIF_SCR_DMA_TX_EN_MASK, /*!< Tx FIFO empty */
  142. };
  143. /*! @brief SPDIF user configuration structure */
  144. typedef struct _spdif_config
  145. {
  146. bool isTxAutoSync; /*!< If auto sync mechanism open */
  147. bool isRxAutoSync; /*!< If auto sync mechanism open */
  148. uint8_t DPLLClkSource; /*!< SPDIF DPLL clock source, range from 0~15, meaning is chip-specific */
  149. uint8_t txClkSource; /*!< SPDIF tx clock source, range from 0~7, meaning is chip-specific */
  150. spdif_rxfull_select_t rxFullSelect; /*!< SPDIF rx buffer full select */
  151. spdif_txempty_select_t txFullSelect; /*!< SPDIF tx buffer empty select */
  152. spdif_uchannel_source_t uChannelSrc; /*!< U channel source */
  153. spdif_tx_source_t txSource; /*!< SPDIF tx data source */
  154. spdif_validity_config_t validityConfig; /*!< Validity flag config */
  155. spdif_gain_select_t gain; /*!< Rx receive clock measure gain parameter. */
  156. } spdif_config_t;
  157. /*!@brief SPDIF transfer queue size, user can refine it according to use case. */
  158. #define SPDIF_XFER_QUEUE_SIZE (4)
  159. /*! @brief SPDIF transfer structure */
  160. typedef struct _spdif_transfer
  161. {
  162. uint8_t *data; /*!< Data start address to transfer. */
  163. uint8_t *qdata; /*!< Data buffer for Q channel */
  164. uint8_t *udata; /*!< Data buffer for C channel */
  165. size_t dataSize; /*!< Transfer size. */
  166. } spdif_transfer_t;
  167. typedef struct _spdif_handle spdif_handle_t;
  168. /*! @brief SPDIF transfer callback prototype */
  169. typedef void (*spdif_transfer_callback_t)(SPDIF_Type *base, spdif_handle_t *handle, status_t status, void *userData);
  170. /*! @brief SPDIF handle structure */
  171. struct _spdif_handle
  172. {
  173. uint32_t state; /*!< Transfer status */
  174. spdif_transfer_callback_t callback; /*!< Callback function called at transfer event*/
  175. void *userData; /*!< Callback parameter passed to callback function*/
  176. spdif_transfer_t spdifQueue[SPDIF_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer */
  177. size_t transferSize[SPDIF_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
  178. volatile uint8_t queueUser; /*!< Index for user to queue transfer */
  179. volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
  180. uint8_t watermark; /*!< Watermark value */
  181. };
  182. /*******************************************************************************
  183. * API
  184. ******************************************************************************/
  185. #if defined(__cplusplus)
  186. extern "C" {
  187. #endif /*_cplusplus*/
  188. /*!
  189. * @name Initialization and deinitialization
  190. * @{
  191. */
  192. /*!
  193. * @brief Initializes the SPDIF peripheral.
  194. *
  195. * Ungates the SPDIF clock, resets the module, and configures SPDIF with a configuration structure.
  196. * The configuration structure can be custom filled or set with default values by
  197. * SPDIF_GetDefaultConfig().
  198. *
  199. * @note This API should be called at the beginning of the application to use
  200. * the SPDIF driver. Otherwise, accessing the SPDIF module can cause a hard fault
  201. * because the clock is not enabled.
  202. *
  203. * @param base SPDIF base pointer
  204. * @param config SPDIF configuration structure.
  205. */
  206. void SPDIF_Init(SPDIF_Type *base, const spdif_config_t *config);
  207. /*!
  208. * @brief Sets the SPDIF configuration structure to default values.
  209. *
  210. * This API initializes the configuration structure for use in SPDIF_Init.
  211. * The initialized structure can remain unchanged in SPDIF_Init, or it can be modified
  212. * before calling SPDIF_Init.
  213. * This is an example.
  214. @code
  215. spdif_config_t config;
  216. SPDIF_GetDefaultConfig(&config);
  217. @endcode
  218. *
  219. * @param config pointer to master configuration structure
  220. */
  221. void SPDIF_GetDefaultConfig(spdif_config_t *config);
  222. /*!
  223. * @brief De-initializes the SPDIF peripheral.
  224. *
  225. * This API gates the SPDIF clock. The SPDIF module can't operate unless SPDIF_Init is called to enable the clock.
  226. *
  227. * @param base SPDIF base pointer
  228. */
  229. void SPDIF_Deinit(SPDIF_Type *base);
  230. /*!
  231. * @brief Resets the SPDIF Tx.
  232. *
  233. * This function makes Tx FIFO in reset mode.
  234. *
  235. * @param base SPDIF base pointer
  236. */
  237. static inline void SPDIF_TxFIFOReset(SPDIF_Type *base)
  238. {
  239. base->SCR |= SPDIF_SCR_RXFIFO_RST_MASK;
  240. }
  241. /*!
  242. * @brief Resets the SPDIF Rx.
  243. *
  244. * This function enables the software reset and FIFO reset of SPDIF Rx. After reset, clear the reset bit.
  245. *
  246. * @param base SPDIF base pointer
  247. */
  248. static inline void SPDIF_RxFIFOReset(SPDIF_Type *base)
  249. {
  250. base->SCR |= SPDIF_SCR_RXFIFO_RST_MASK;
  251. }
  252. /*!
  253. * @brief Enables/disables the SPDIF Tx.
  254. *
  255. * @param base SPDIF base pointer
  256. * @param enable True means enable SPDIF Tx, false means disable.
  257. */
  258. void SPDIF_TxEnable(SPDIF_Type *base, bool enable);
  259. /*!
  260. * @brief Enables/disables the SPDIF Rx.
  261. *
  262. * @param base SPDIF base pointer
  263. * @param enable True means enable SPDIF Rx, false means disable.
  264. */
  265. static inline void SPDIF_RxEnable(SPDIF_Type *base, bool enable)
  266. {
  267. if (enable)
  268. {
  269. /* Open Rx FIFO */
  270. base->SCR &= ~(SPDIF_SCR_RXFIFO_CTRL_MASK | SPDIF_SCR_RXFIFO_OFF_ON_MASK);
  271. }
  272. else
  273. {
  274. base->SCR |= SPDIF_SCR_RXFIFO_OFF_ON_MASK;
  275. }
  276. }
  277. /*! @} */
  278. /*!
  279. * @name Status
  280. * @{
  281. */
  282. /*!
  283. * @brief Gets the SPDIF status flag state.
  284. *
  285. * @param base SPDIF base pointer
  286. * @return SPDIF status flag value. Use the _spdif_interrupt_enable_t to get the status value needed.
  287. */
  288. static inline uint32_t SPDIF_GetStatusFlag(SPDIF_Type *base)
  289. {
  290. return base->SIS;
  291. }
  292. /*!
  293. * @brief Clears the SPDIF status flag state.
  294. *
  295. * @param base SPDIF base pointer
  296. * @param mask State mask. It can be a combination of the _spdif_interrupt_enable_t member. Notice these members
  297. * cannot be included, as these flags cannot be cleared by writing 1 to these bits:
  298. * @arg kSPDIF_UChannelReceiveRegisterFull
  299. * @arg kSPDIF_QChannelReceiveRegisterFull
  300. * @arg kSPDIF_TxFIFOEmpty
  301. * @arg kSPDIF_RxFIFOFull
  302. */
  303. static inline void SPDIF_ClearStatusFlags(SPDIF_Type *base, uint32_t mask)
  304. {
  305. base->SIC = mask;
  306. }
  307. /*! @} */
  308. /*!
  309. * @name Interrupts
  310. * @{
  311. */
  312. /*!
  313. * @brief Enables the SPDIF Tx interrupt requests.
  314. *
  315. * @param base SPDIF base pointer
  316. * @param mask interrupt source
  317. * The parameter can be a combination of the following sources if defined.
  318. * @arg kSPDIF_WordStartInterruptEnable
  319. * @arg kSPDIF_SyncErrorInterruptEnable
  320. * @arg kSPDIF_FIFOWarningInterruptEnable
  321. * @arg kSPDIF_FIFORequestInterruptEnable
  322. * @arg kSPDIF_FIFOErrorInterruptEnable
  323. */
  324. static inline void SPDIF_EnableInterrupts(SPDIF_Type *base, uint32_t mask)
  325. {
  326. base->SIE |= mask;
  327. }
  328. /*!
  329. * @brief Disables the SPDIF Tx interrupt requests.
  330. *
  331. * @param base SPDIF base pointer
  332. * @param mask interrupt source
  333. * The parameter can be a combination of the following sources if defined.
  334. * @arg kSPDIF_WordStartInterruptEnable
  335. * @arg kSPDIF_SyncErrorInterruptEnable
  336. * @arg kSPDIF_FIFOWarningInterruptEnable
  337. * @arg kSPDIF_FIFORequestInterruptEnable
  338. * @arg kSPDIF_FIFOErrorInterruptEnable
  339. */
  340. static inline void SPDIF_DisableInterrupts(SPDIF_Type *base, uint32_t mask)
  341. {
  342. base->SIE &= ~mask;
  343. }
  344. /*! @} */
  345. /*!
  346. * @name DMA Control
  347. * @{
  348. */
  349. /*!
  350. * @brief Enables/disables the SPDIF DMA requests.
  351. * @param base SPDIF base pointer
  352. * @param mask SPDIF DMA enable mask, The parameter can be a combination of the following sources if defined
  353. * @arg kSPDIF_RxDMAEnable
  354. * @arg kSPDIF_TxDMAEnable
  355. * @param enable True means enable DMA, false means disable DMA.
  356. */
  357. static inline void SPDIF_EnableDMA(SPDIF_Type *base, uint32_t mask, bool enable)
  358. {
  359. if (enable)
  360. {
  361. base->SCR |= mask;
  362. }
  363. else
  364. {
  365. base->SCR &= ~mask;
  366. }
  367. }
  368. /*!
  369. * @brief Gets the SPDIF Tx left data register address.
  370. *
  371. * This API is used to provide a transfer address for the SPDIF DMA transfer configuration.
  372. *
  373. * @param base SPDIF base pointer.
  374. * @return data register address.
  375. */
  376. static inline uint32_t SPDIF_TxGetLeftDataRegisterAddress(SPDIF_Type *base)
  377. {
  378. return (uint32_t)(&(base->STL));
  379. }
  380. /*!
  381. * @brief Gets the SPDIF Tx right data register address.
  382. *
  383. * This API is used to provide a transfer address for the SPDIF DMA transfer configuration.
  384. *
  385. * @param base SPDIF base pointer.
  386. * @return data register address.
  387. */
  388. static inline uint32_t SPDIF_TxGetRightDataRegisterAddress(SPDIF_Type *base)
  389. {
  390. return (uint32_t)(&(base->STR));
  391. }
  392. /*!
  393. * @brief Gets the SPDIF Rx left data register address.
  394. *
  395. * This API is used to provide a transfer address for the SPDIF DMA transfer configuration.
  396. *
  397. * @param base SPDIF base pointer.
  398. * @return data register address.
  399. */
  400. static inline uint32_t SPDIF_RxGetLeftDataRegisterAddress(SPDIF_Type *base)
  401. {
  402. return (uint32_t)(&(base->SRL));
  403. }
  404. /*!
  405. * @brief Gets the SPDIF Rx right data register address.
  406. *
  407. * This API is used to provide a transfer address for the SPDIF DMA transfer configuration.
  408. *
  409. * @param base SPDIF base pointer.
  410. * @return data register address.
  411. */
  412. static inline uint32_t SPDIF_RxGetRightDataRegisterAddress(SPDIF_Type *base)
  413. {
  414. return (uint32_t)(&(base->SRR));
  415. }
  416. /*! @} */
  417. /*!
  418. * @name Bus Operations
  419. * @{
  420. */
  421. /*!
  422. * @brief Configures the SPDIF Tx sample rate.
  423. *
  424. * The audio format can be changed at run-time. This function configures the sample rate.
  425. *
  426. * @param base SPDIF base pointer.
  427. * @param sampleRate_Hz SPDIF sample rate frequency in Hz.
  428. * @param sourceClockFreq_Hz SPDIF tx clock source frequency in Hz.
  429. */
  430. void SPDIF_TxSetSampleRate(SPDIF_Type *base, uint32_t sampleRate_Hz, uint32_t sourceClockFreq_Hz);
  431. /*!
  432. * @brief Configures the SPDIF Rx audio format.
  433. *
  434. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  435. * format to be transferred.
  436. *
  437. * @param base SPDIF base pointer.
  438. * @param clockSourceFreq_Hz SPDIF system clock frequency in hz.
  439. */
  440. uint32_t SPDIF_GetRxSampleRate(SPDIF_Type *base, uint32_t clockSourceFreq_Hz);
  441. /*!
  442. * @brief Sends data using a blocking method.
  443. *
  444. * @note This function blocks by polling until data is ready to be sent.
  445. *
  446. * @param base SPDIF base pointer.
  447. * @param buffer Pointer to the data to be written.
  448. * @param size Bytes to be written.
  449. */
  450. void SPDIF_WriteBlocking(SPDIF_Type *base, uint8_t *buffer, uint32_t size);
  451. /*!
  452. * @brief Writes data into SPDIF FIFO.
  453. *
  454. * @param base SPDIF base pointer.
  455. * @param data Data needs to be written.
  456. */
  457. static inline void SPDIF_WriteLeftData(SPDIF_Type *base, uint32_t data)
  458. {
  459. base->STL = data;
  460. }
  461. /*!
  462. * @brief Writes data into SPDIF FIFO.
  463. *
  464. * @param base SPDIF base pointer.
  465. * @param data Data needs to be written.
  466. */
  467. static inline void SPDIF_WriteRightData(SPDIF_Type *base, uint32_t data)
  468. {
  469. base->STR = data;
  470. }
  471. /*!
  472. * @brief Writes data into SPDIF FIFO.
  473. *
  474. * @param base SPDIF base pointer.
  475. * @param data Data needs to be written.
  476. */
  477. static inline void SPDIF_WriteChannelStatusHigh(SPDIF_Type *base, uint32_t data)
  478. {
  479. base->STCSCH = data;
  480. }
  481. /*!
  482. * @brief Writes data into SPDIF FIFO.
  483. *
  484. * @param base SPDIF base pointer.
  485. * @param data Data needs to be written.
  486. */
  487. static inline void SPDIF_WriteChannelStatusLow(SPDIF_Type *base, uint32_t data)
  488. {
  489. base->STCSCL = data;
  490. }
  491. /*!
  492. * @brief Receives data using a blocking method.
  493. *
  494. * @note This function blocks by polling until data is ready to be sent.
  495. *
  496. * @param base SPDIF base pointer.
  497. * @param buffer Pointer to the data to be read.
  498. * @param size Bytes to be read.
  499. */
  500. void SPDIF_ReadBlocking(SPDIF_Type *base, uint8_t *buffer, uint32_t size);
  501. /*!
  502. * @brief Reads data from the SPDIF FIFO.
  503. *
  504. * @param base SPDIF base pointer.
  505. * @return Data in SPDIF FIFO.
  506. */
  507. static inline uint32_t SPDIF_ReadLeftData(SPDIF_Type *base)
  508. {
  509. return base->SRL;
  510. }
  511. /*!
  512. * @brief Reads data from the SPDIF FIFO.
  513. *.
  514. * @param base SPDIF base pointer.
  515. * @return Data in SPDIF FIFO.
  516. */
  517. static inline uint32_t SPDIF_ReadRightData(SPDIF_Type *base)
  518. {
  519. return base->SRR;
  520. }
  521. /*!
  522. * @brief Reads data from the SPDIF FIFO.
  523. *
  524. * @param base SPDIF base pointer.
  525. * @return Data in SPDIF FIFO.
  526. */
  527. static inline uint32_t SPDIF_ReadChannelStatusHigh(SPDIF_Type *base)
  528. {
  529. return base->SRCSH;
  530. }
  531. /*!
  532. * @brief Reads data from the SPDIF FIFO.
  533. *.
  534. * @param base SPDIF base pointer.
  535. * @return Data in SPDIF FIFO.
  536. */
  537. static inline uint32_t SPDIF_ReadChannelStatusLow(SPDIF_Type *base)
  538. {
  539. return base->SRCSL;
  540. }
  541. /*!
  542. * @brief Reads data from the SPDIF FIFO.
  543. *
  544. * @param base SPDIF base pointer.
  545. * @return Data in SPDIF FIFO.
  546. */
  547. static inline uint32_t SPDIF_ReadQChannel(SPDIF_Type *base)
  548. {
  549. return base->SRQ;
  550. }
  551. /*!
  552. * @brief Reads data from the SPDIF FIFO.
  553. *.
  554. * @param base SPDIF base pointer.
  555. * @return Data in SPDIF FIFO.
  556. */
  557. static inline uint32_t SPDIF_ReadUChannel(SPDIF_Type *base)
  558. {
  559. return base->SRU;
  560. }
  561. /*! @} */
  562. /*!
  563. * @name Transactional
  564. * @{
  565. */
  566. /*!
  567. * @brief Initializes the SPDIF Tx handle.
  568. *
  569. * This function initializes the Tx handle for the SPDIF Tx transactional APIs. Call
  570. * this function once to get the handle initialized.
  571. *
  572. * @param base SPDIF base pointer
  573. * @param handle SPDIF handle pointer.
  574. * @param callback Pointer to the user callback function.
  575. * @param userData User parameter passed to the callback function
  576. */
  577. void SPDIF_TransferTxCreateHandle(SPDIF_Type *base,
  578. spdif_handle_t *handle,
  579. spdif_transfer_callback_t callback,
  580. void *userData);
  581. /*!
  582. * @brief Initializes the SPDIF Rx handle.
  583. *
  584. * This function initializes the Rx handle for the SPDIF Rx transactional APIs. Call
  585. * this function once to get the handle initialized.
  586. *
  587. * @param base SPDIF base pointer.
  588. * @param handle SPDIF handle pointer.
  589. * @param callback Pointer to the user callback function.
  590. * @param userData User parameter passed to the callback function.
  591. */
  592. void SPDIF_TransferRxCreateHandle(SPDIF_Type *base,
  593. spdif_handle_t *handle,
  594. spdif_transfer_callback_t callback,
  595. void *userData);
  596. /*!
  597. * @brief Performs an interrupt non-blocking send transfer on SPDIF.
  598. *
  599. * @note This API returns immediately after the transfer initiates.
  600. * Call the SPDIF_TxGetTransferStatusIRQ to poll the transfer status and check whether
  601. * the transfer is finished. If the return status is not kStatus_SPDIF_Busy, the transfer
  602. * is finished.
  603. *
  604. * @param base SPDIF base pointer.
  605. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  606. * @param xfer Pointer to the spdif_transfer_t structure.
  607. * @retval kStatus_Success Successfully started the data receive.
  608. * @retval kStatus_SPDIF_TxBusy Previous receive still not finished.
  609. * @retval kStatus_InvalidArgument The input parameter is invalid.
  610. */
  611. status_t SPDIF_TransferSendNonBlocking(SPDIF_Type *base, spdif_handle_t *handle, spdif_transfer_t *xfer);
  612. /*!
  613. * @brief Performs an interrupt non-blocking receive transfer on SPDIF.
  614. *
  615. * @note This API returns immediately after the transfer initiates.
  616. * Call the SPDIF_RxGetTransferStatusIRQ to poll the transfer status and check whether
  617. * the transfer is finished. If the return status is not kStatus_SPDIF_Busy, the transfer
  618. * is finished.
  619. *
  620. * @param base SPDIF base pointer
  621. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  622. * @param xfer Pointer to the spdif_transfer_t structure.
  623. * @retval kStatus_Success Successfully started the data receive.
  624. * @retval kStatus_SPDIF_RxBusy Previous receive still not finished.
  625. * @retval kStatus_InvalidArgument The input parameter is invalid.
  626. */
  627. status_t SPDIF_TransferReceiveNonBlocking(SPDIF_Type *base, spdif_handle_t *handle, spdif_transfer_t *xfer);
  628. /*!
  629. * @brief Gets a set byte count.
  630. *
  631. * @param base SPDIF base pointer.
  632. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  633. * @param count Bytes count sent.
  634. * @retval kStatus_Success Succeed get the transfer count.
  635. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  636. */
  637. status_t SPDIF_TransferGetSendCount(SPDIF_Type *base, spdif_handle_t *handle, size_t *count);
  638. /*!
  639. * @brief Gets a received byte count.
  640. *
  641. * @param base SPDIF base pointer.
  642. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  643. * @param count Bytes count received.
  644. * @retval kStatus_Success Succeed get the transfer count.
  645. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  646. */
  647. status_t SPDIF_TransferGetReceiveCount(SPDIF_Type *base, spdif_handle_t *handle, size_t *count);
  648. /*!
  649. * @brief Aborts the current send.
  650. *
  651. * @note This API can be called any time when an interrupt non-blocking transfer initiates
  652. * to abort the transfer early.
  653. *
  654. * @param base SPDIF base pointer.
  655. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  656. */
  657. void SPDIF_TransferAbortSend(SPDIF_Type *base, spdif_handle_t *handle);
  658. /*!
  659. * @brief Aborts the current IRQ receive.
  660. *
  661. * @note This API can be called when an interrupt non-blocking transfer initiates
  662. * to abort the transfer early.
  663. *
  664. * @param base SPDIF base pointer
  665. * @param handle Pointer to the spdif_handle_t structure which stores the transfer state.
  666. */
  667. void SPDIF_TransferAbortReceive(SPDIF_Type *base, spdif_handle_t *handle);
  668. /*!
  669. * @brief Tx interrupt handler.
  670. *
  671. * @param base SPDIF base pointer.
  672. * @param handle Pointer to the spdif_handle_t structure.
  673. */
  674. void SPDIF_TransferTxHandleIRQ(SPDIF_Type *base, spdif_handle_t *handle);
  675. /*!
  676. * @brief Tx interrupt handler.
  677. *
  678. * @param base SPDIF base pointer.
  679. * @param handle Pointer to the spdif_handle_t structure.
  680. */
  681. void SPDIF_TransferRxHandleIRQ(SPDIF_Type *base, spdif_handle_t *handle);
  682. /*! @} */
  683. #if defined(__cplusplus)
  684. }
  685. #endif /*_cplusplus*/
  686. /*! @} */
  687. #endif /* _FSL_SPDIF_H_ */