fsl_spdif.h 25 KB

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