fsl_spdif.h 26 KB

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