stm32f4xx_spdifrx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_spdifrx.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 06-March-2015
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Serial Audio Interface (SPDIFRX):
  9. * + Initialization and Configuration
  10. * + Data transfers functions
  11. * + DMA transfers management
  12. * + Interrupts and flags management
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  17. *
  18. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  19. * You may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at:
  21. *
  22. * http://www.st.com/software_license_agreement_liberty_v2
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. *
  30. ******************************************************************************
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32f4xx_spdifrx.h"
  34. #include "stm32f4xx_rcc.h"
  35. /** @addtogroup STM32F4xx_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @defgroup SPDIFRX
  39. * @brief SPDIFRX driver modules
  40. * @{
  41. */
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. #define CR_CLEAR_MASK 0x000000FE7
  45. /* Private macro -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Private functions ---------------------------------------------------------*/
  49. /** @defgroup SPDIFRX_Private_Functions
  50. * @{
  51. */
  52. /** @defgroup SPDIFRX_Group1 Initialization and Configuration functions
  53. * @brief Initialization and Configuration functions
  54. *
  55. @verbatim
  56. ===============================================================================
  57. ##### Initialization and Configuration functions #####
  58. ===============================================================================
  59. [..]
  60. This section provides a set of functions allowing to initialize the SPDIFRX Audio
  61. Block Mode, Audio Protocol, Data size, Synchronization between audio block,
  62. Master clock Divider, FIFO threshold, Frame configuration, slot configuration,
  63. Tristate mode, Companding mode and Mute mode.
  64. [..]
  65. The SPDIFRX_Init(), SPDIFRX_FrameInit() and SPDIFRX_SlotInit() functions follows the SPDIFRX Block
  66. configuration procedures for Master mode and Slave mode (details for these procedures
  67. are available in reference manual(RMxxxx).
  68. @endverbatim
  69. * @{
  70. */
  71. /**
  72. * @brief Deinitialize the SPDIFRXx peripheral registers to their default reset values.
  73. * @param void
  74. * @retval None
  75. */
  76. void SPDIFRX_DeInit(void)
  77. {
  78. /* Enable SPDIFRX reset state */
  79. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPDIFRX, ENABLE);
  80. /* Release SPDIFRX from reset state */
  81. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPDIFRX, DISABLE);
  82. }
  83. /**
  84. * @brief Initializes the SPDIFRX peripheral according to the specified
  85. * parameters in the SPDIFRX_InitStruct.
  86. *
  87. * @note SPDIFRX clock is generated from a specific output of the PLLSPDIFRX or a specific
  88. * output of the PLLI2S or from an alternate function bypassing the PLL I2S.
  89. *
  90. * @param SPDIFRX_InitStruct: pointer to a SPDIFRX_InitTypeDef structure that
  91. * contains the configuration information for the specified SPDIFRX Block peripheral.
  92. * @retval None
  93. */
  94. void SPDIFRX_Init(SPDIFRX_InitTypeDef* SPDIFRX_InitStruct)
  95. {
  96. uint32_t tmpreg = 0;
  97. /* Check the SPDIFRX parameters */
  98. assert_param(IS_STEREO_MODE(SPDIFRX_InitStruct->SPDIFRX_StereoMode));
  99. assert_param(IS_SPDIFRX_INPUT_SELECT(SPDIFRX_InitStruct->SPDIFRX_InputSelection));
  100. assert_param(IS_SPDIFRX_MAX_RETRIES(SPDIFRX_InitStruct->SPDIFRX_Retries));
  101. assert_param(IS_SPDIFRX_WAIT_FOR_ACTIVITY(SPDIFRX_InitStruct->SPDIFRX_WaitForActivity));
  102. assert_param(IS_SPDIFRX_CHANNEL(SPDIFRX_InitStruct->SPDIFRX_ChannelSelection));
  103. assert_param(IS_SPDIFRX_DATA_FORMAT(SPDIFRX_InitStruct->SPDIFRX_DataFormat));
  104. /* SPDIFRX CR Configuration */
  105. /* Get the SPDIFRX CR value */
  106. tmpreg = SPDIFRX->CR;
  107. /* Clear INSEL, WFA, NBTR, CHSEL, DRFMT and RXSTEO bits */
  108. tmpreg &= CR_CLEAR_MASK;
  109. /* Configure SPDIFRX: Input selection, Maximum allowed re-tries during synchronization phase,
  110. wait for activity, Channel Selection, Data samples format and stereo/mono mode */
  111. /* Set INSEL bits according to SPDIFRX_InputSelection value */
  112. /* Set WFA bit according to SPDIFRX_WaitForActivity value */
  113. /* Set NBTR bit according to SPDIFRX_Retries value */
  114. /* Set CHSEL bit according to SPDIFRX_ChannelSelection value */
  115. /* Set DRFMT bits according to SPDIFRX_DataFormat value */
  116. /* Set RXSTEO bit according to SPDIFRX_StereoMode value */
  117. tmpreg |= (uint32_t)(SPDIFRX_InitStruct->SPDIFRX_InputSelection | SPDIFRX_InitStruct->SPDIFRX_WaitForActivity |
  118. SPDIFRX_InitStruct->SPDIFRX_Retries | SPDIFRX_InitStruct->SPDIFRX_ChannelSelection |
  119. SPDIFRX_InitStruct->SPDIFRX_DataFormat | SPDIFRX_InitStruct->SPDIFRX_StereoMode
  120. );
  121. /* Write to SPDIFRX CR */
  122. SPDIFRX->CR = tmpreg;
  123. }
  124. /**
  125. * @brief Fills each SPDIFRX_InitStruct member with its default value.
  126. * @param SPDIFRX_InitStruct: pointer to a SPDIFRX_InitTypeDef structure which will
  127. * be initialized.
  128. * @retval None
  129. */
  130. void SPDIFRX_StructInit(SPDIFRX_InitTypeDef* SPDIFRX_InitStruct)
  131. {
  132. /* Reset SPDIFRX init structure parameters values */
  133. /* Initialize the PDIF_InputSelection member */
  134. SPDIFRX_InitStruct->SPDIFRX_InputSelection = SPDIFRX_Input_IN0;
  135. /* Initialize the SPDIFRX_WaitForActivity member */
  136. SPDIFRX_InitStruct->SPDIFRX_WaitForActivity = SPDIFRX_WaitForActivity_On;
  137. /* Initialize the SPDIFRX_Retries member */
  138. SPDIFRX_InitStruct->SPDIFRX_Retries = SPDIFRX_16MAX_RETRIES;
  139. /* Initialize the SPDIFRX_ChannelSelection member */
  140. SPDIFRX_InitStruct->SPDIFRX_ChannelSelection = SPDIFRX_Select_Channel_A;
  141. /* Initialize the SPDIFRX_DataFormat member */
  142. SPDIFRX_InitStruct->SPDIFRX_DataFormat = SPDIFRX_MSB_DataFormat;
  143. /* Initialize the SPDIFRX_StereoMode member */
  144. SPDIFRX_InitStruct->SPDIFRX_StereoMode = SPDIFRX_StereoMode_Enabled;
  145. }
  146. /**
  147. * @brief Enables or disables the SPDIFRX frame x bit.
  148. * @param NewState: new state of the selected SPDIFRX frame bit.
  149. * This parameter can be: ENABLE or DISABLE.
  150. * @retval None
  151. */
  152. void SPDIFRX_SetPreambleTypeBit(FunctionalState NewState)
  153. {
  154. /* Check the parameters */
  155. assert_param(IS_FUNCTIONAL_STATE(NewState));
  156. if (NewState != DISABLE)
  157. {
  158. /* Enable the selected SPDIFRX frame bit */
  159. SPDIFRX->CR |= SPDIFRX_CR_PTMSK;
  160. }
  161. else
  162. {
  163. /* Disable the selected SPDIFRX frame bit */
  164. SPDIFRX->CR &= ~(SPDIFRX_CR_PTMSK);
  165. }
  166. }
  167. /**
  168. * @brief Enables or disables the SPDIFRX frame x bit.
  169. * @param NewState: new state of the selected SPDIFRX frame bit.
  170. * This parameter can be: ENABLE or DISABLE.
  171. * @retval None
  172. */
  173. void SPDIFRX_SetUserDataChannelStatusBits(FunctionalState NewState)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_FUNCTIONAL_STATE(NewState));
  177. if (NewState != DISABLE)
  178. {
  179. /* Enable the selected SPDIFRX frame bit */
  180. SPDIFRX->CR |= SPDIFRX_CR_CUMSK;
  181. }
  182. else
  183. {
  184. /* Disable the selected SPDIFRX frame bit */
  185. SPDIFRX->CR &= ~(SPDIFRX_CR_CUMSK);
  186. }
  187. }
  188. /**
  189. * @brief Enables or disables the SPDIFRX frame x bit.
  190. * @param NewState: new state of the selected SPDIFRX frame bit.
  191. * This parameter can be: ENABLE or DISABLE.
  192. * @retval None
  193. */
  194. void SPDIFRX_SetValidityBit(FunctionalState NewState)
  195. {
  196. /* Check the parameters */
  197. assert_param(IS_FUNCTIONAL_STATE(NewState));
  198. if (NewState != DISABLE)
  199. {
  200. /* Enable the selected SPDIFRX frame bit */
  201. SPDIFRX->CR |= SPDIFRX_CR_VMSK;
  202. }
  203. else
  204. {
  205. /* Disable the selected SPDIFRX frame bit */
  206. SPDIFRX->CR &= ~(SPDIFRX_CR_VMSK);
  207. }
  208. }
  209. /**
  210. * @brief Enables or disables the SPDIFRX frame x bit.
  211. * @param NewState: new state of the selected SPDIFRX frame bit.
  212. * This parameter can be: ENABLE or DISABLE.
  213. * @retval None
  214. */
  215. void SPDIFRX_SetParityBit(FunctionalState NewState)
  216. {
  217. /* Check the parameters */
  218. assert_param(IS_FUNCTIONAL_STATE(NewState));
  219. if (NewState != DISABLE)
  220. {
  221. /* Enable the selected SPDIFRX frame bit */
  222. SPDIFRX->CR |= SPDIFRX_CR_PMSK;
  223. }
  224. else
  225. {
  226. /* Disable the selected SPDIFRX frame bit */
  227. SPDIFRX->CR &= ~(SPDIFRX_CR_PMSK);
  228. }
  229. }
  230. /**
  231. * @brief Enables or disables the SPDIFRX DMA interface (RX).
  232. * @param NewState: new state of the selected SPDIFRX DMA transfer request.
  233. * This parameter can be: ENABLE or DISABLE.
  234. * @retval None
  235. */
  236. void SPDIFRX_RxDMACmd(FunctionalState NewState)
  237. {
  238. /* Check the parameters */
  239. assert_param(IS_FUNCTIONAL_STATE(NewState));
  240. if (NewState != DISABLE)
  241. {
  242. /* Enable the selected SPDIFRX DMA requests */
  243. SPDIFRX->CR |= SPDIFRX_CR_RXDMAEN;
  244. }
  245. else
  246. {
  247. /* Disable the selected SPDIFRX DMA requests */
  248. SPDIFRX->CR &= ~(SPDIFRX_CR_RXDMAEN);
  249. }
  250. }
  251. /**
  252. * @brief Enables or disables the SPDIFRX DMA interface (Control Buffer).
  253. * @param NewState: new state of the selected SPDIFRX DMA transfer request.
  254. * This parameter can be: ENABLE or DISABLE.
  255. * @retval None
  256. */
  257. void SPDIFRX_CbDMACmd(FunctionalState NewState)
  258. {
  259. /* Check the parameters */
  260. assert_param(IS_FUNCTIONAL_STATE(NewState));
  261. if (NewState != DISABLE)
  262. {
  263. /* Enable the selected SPDIFRX DMA requests */
  264. SPDIFRX->CR |= SPDIFRX_CR_CBDMAEN;
  265. }
  266. else
  267. {
  268. /* Disable the selected SPDIFRX DMA requests */
  269. SPDIFRX->CR &= ~(SPDIFRX_CR_CBDMAEN);
  270. }
  271. }
  272. /**
  273. * @brief Enables or disables the SPDIFRX peripheral.
  274. * @param SPDIFRX_State: specifies the SPDIFRX peripheral state.
  275. * This parameter can be one of the following values:
  276. * @arg SPDIFRX_STATE_IDLE : Disable SPDIFRX-RX (STATE_IDLE)
  277. * @arg SPDIFRX_STATE_SYNC : Enable SPDIFRX-RX Synchronization only
  278. * @arg SPDIFRX_STATE_RCV : Enable SPDIFRX Receiver
  279. * @retval None
  280. */
  281. void SPDIFRX_Cmd(uint32_t SPDIFRX_State)
  282. {
  283. /* Check the parameters */
  284. assert_param(IS_SPDIFRX_STATE(SPDIFRX_State));
  285. /* Clear SPDIFRXEN bits */
  286. SPDIFRX->CR &= ~(SPDIFRX_CR_SPDIFEN);
  287. /* Set new SPDIFRXEN value */
  288. SPDIFRX->CR |= SPDIFRX_State;
  289. }
  290. /**
  291. * @brief Enables or disables the specified SPDIFRX Block interrupts.
  292. * @param SPDIFRX_IT: specifies the SPDIFRX interrupt source to be enabled or disabled.
  293. * This parameter can be one of the following values:
  294. * @arg SPDIFRX_IT_RXNE: RXNE interrupt enable
  295. * @arg SPDIFRX_IT_CSRNE: Control Buffer Ready Interrupt Enable
  296. * @arg SPDIFRX_IT_PERRIE: Parity error interrupt enable
  297. * @arg SPDIFRX_IT_OVRIE: Overrun error Interrupt Enable
  298. * @arg SPDIFRX_IT_SBLKIE: Synchronization Block Detected Interrupt Enable
  299. * @arg SPDIFRX_IT_SYNCDIE: Synchronization Done
  300. * @arg SPDIFRX_IT_IFEIE: Serial Interface Error Interrupt Enable
  301. * @param NewState: new state of the specified SPDIFRX interrupt.
  302. * This parameter can be: ENABLE or DISABLE.
  303. * @retval None
  304. */
  305. void SPDIFRX_ITConfig(uint32_t SPDIFRX_IT, FunctionalState NewState)
  306. {
  307. /* Check the parameters */
  308. assert_param(IS_FUNCTIONAL_STATE(NewState));
  309. assert_param(IS_SPDIFRX_CONFIG_IT(SPDIFRX_IT));
  310. if (NewState != DISABLE)
  311. {
  312. /* Enable the selected SPDIFRX interrupt */
  313. SPDIFRX->IMR |= SPDIFRX_IT;
  314. }
  315. else
  316. {
  317. /* Disable the selected SPDIFRX interrupt */
  318. SPDIFRX->IMR &= ~(SPDIFRX_IT);
  319. }
  320. }
  321. /**
  322. * @brief Checks whether the specified SPDIFRX flag is set or not.
  323. * @param SPDIFRX_FLAG: specifies the SPDIFRX flag to check.
  324. * This parameter can be one of the following values:
  325. * @arg SPDIFRX_FLAG_RXNE: Read data register not empty flag.
  326. * @arg SPDIFRX_FLAG_CSRNE: The Control Buffer register is not empty flag.
  327. * @arg SPDIFRX_FLAG_PERR: Parity error flag.
  328. * @arg SPDIFRX_FLAG_OVR: Overrun error flag.
  329. * @arg SPDIFRX_FLAG_SBD: Synchronization Block Detected flag.
  330. * @arg SPDIFRX_FLAG_SYNCD: Synchronization Done flag.
  331. * @arg SPDIFRX_FLAG_FERR: Framing error flag.
  332. * @arg SPDIFRX_FLAG_SERR: Synchronization error flag.
  333. * @arg SPDIFRX_FLAG_TERR: Time-out error flag.
  334. * @retval The new state of SPDIFRX_FLAG (SET or RESET).
  335. */
  336. FlagStatus SPDIFRX_GetFlagStatus(uint32_t SPDIFRX_FLAG)
  337. {
  338. FlagStatus bitstatus = RESET;
  339. /* Check the parameters */
  340. assert_param(IS_SPDIFRX_FLAG(SPDIFRX_FLAG));
  341. /* Check the status of the specified SPDIFRX flag */
  342. if ((SPDIFRX->SR & SPDIFRX_FLAG) != (uint32_t)RESET)
  343. {
  344. /* SPDIFRX_FLAG is set */
  345. bitstatus = SET;
  346. }
  347. else
  348. {
  349. /* SPDIFRX_FLAG is reset */
  350. bitstatus = RESET;
  351. }
  352. /* Return the SPDIFRX_FLAG status */
  353. return bitstatus;
  354. }
  355. /**
  356. * @brief Clears the specified SPDIFRX flag.
  357. * @param SPDIFRX_FLAG: specifies the SPDIFRX flag to check.
  358. * This parameter can be one of the following values:
  359. * @arg SPDIFRX_FLAG_PERR: Parity error flag.
  360. * @arg SPDIFRX_FLAG_OVR: Overrun error flag.
  361. * @arg SPDIFRX_FLAG_SBD: Synchronization Block Detected flag.
  362. * @arg SPDIFRX_FLAG_SYNCD: Synchronization Done flag.
  363. *
  364. * @retval None
  365. */
  366. void SPDIFRX_ClearFlag(uint32_t SPDIFRX_FLAG)
  367. {
  368. /* Check the parameters */
  369. assert_param(IS_SPDIFRX_CLEAR_FLAG(SPDIFRX_FLAG));
  370. /* Clear the selected SPDIFRX Block flag */
  371. SPDIFRX->IFCR |= SPDIFRX_FLAG;
  372. }
  373. /**
  374. * @brief Checks whether the specified SPDIFRX interrupt has occurred or not.
  375. * @param SPDIFRX_IT: specifies the SPDIFRX interrupt source to be enabled or disabled.
  376. * This parameter can be one of the following values:
  377. * @arg SPDIFRX_IT_RXNE: RXNE interrupt enable
  378. * @arg SPDIFRX_IT_CSRNE: Control Buffer Ready Interrupt Enable
  379. * @arg SPDIFRX_IT_PERRIE: Parity error interrupt enable
  380. * @arg SPDIFRX_IT_OVRIE: Overrun error Interrupt Enable
  381. * @arg SPDIFRX_IT_SBLKIE: Synchronization Block Detected Interrupt Enable
  382. * @arg SPDIFRX_IT_SYNCDIE: Synchronization Done
  383. * @arg SPDIFRX_IT_IFEIE: Serial Interface Error Interrupt Enable
  384. * @retval The new state of SPDIFRX_IT (SET or RESET).
  385. */
  386. ITStatus SPDIFRX_GetITStatus(uint32_t SPDIFRX_IT)
  387. {
  388. ITStatus bitstatus = RESET;
  389. uint32_t enablestatus = 0;
  390. /* Check the parameters */
  391. assert_param(IS_SPDIFRX_CONFIG_IT(SPDIFRX_IT));
  392. /* Get the SPDIFRX_IT enable bit status */
  393. enablestatus = (SPDIFRX->IMR & SPDIFRX_IT) ;
  394. /* Check the status of the specified SPDIFRX interrupt */
  395. if (((SPDIFRX->SR & SPDIFRX_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  396. {
  397. /* SPDIFRX_IT is set */
  398. bitstatus = SET;
  399. }
  400. else
  401. {
  402. /* SPDIFRX_IT is reset */
  403. bitstatus = RESET;
  404. }
  405. /* Return the SPDIFRX_IT status */
  406. return bitstatus;
  407. }
  408. /**
  409. * @brief Clears the SPDIFRX interrupt pending bit.
  410. * @param SAI_IT: specifies the SPDIFRX interrupt pending bit to clear.
  411. * This parameter can be one of the following values:
  412. * @arg SPDIFRX_IT_MUTEDET: MUTE detection interrupt.
  413. * @arg SPDIFRX_IT_OVRUDR: overrun/underrun interrupt.
  414. * @arg SPDIFRX_IT_WCKCFG: wrong clock configuration interrupt.
  415. * @arg SPDIFRX_IT_CNRDY: codec not ready interrupt.
  416. * @arg SPDIFRX_IT_AFSDET: anticipated frame synchronization detection interrupt.
  417. * @arg SPDIFRX_IT_LFSDET: late frame synchronization detection interrupt.
  418. *
  419. * @note FREQ (FIFO Request) flag is cleared :
  420. * - When the audio block is transmitter and the FIFO is full or the FIFO
  421. * has one data (one buffer mode) depending the bit FTH in the
  422. * SPDIFRX_xCR2 register.
  423. * - When the audio block is receiver and the FIFO is not empty
  424. *
  425. * @retval None
  426. */
  427. void SPDIFRX_ClearITPendingBit(uint32_t SPDIFRX_IT)
  428. {
  429. /* Check the parameters */
  430. assert_param(IS_SPDIFRX_CLEAR_FLAG(SPDIFRX_IT));
  431. /* Clear the selected SPDIFRX interrupt pending bit */
  432. SPDIFRX->IFCR |= SPDIFRX_IT;
  433. }
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. /**
  441. * @}
  442. */
  443. /**
  444. * @}
  445. */
  446. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/