You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1259 lines
40 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_spdifrx.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the SPDIFRX audio interface:
  9. * + Initialization and Configuration
  10. * + Data transfers functions
  11. * + DMA transfers management
  12. * + Interrupts and flags management
  13. @verbatim
  14. ===============================================================================
  15. ##### How to use this driver #####
  16. ===============================================================================
  17. [..]
  18. The SPDIFRX HAL driver can be used as follow:
  19. (#) Declare SPDIFRX_HandleTypeDef handle structure.
  20. (#) Initialize the SPDIFRX low level resources by implement the HAL_SPDIFRX_MspInit() API:
  21. (##) Enable the SPDIFRX interface clock.
  22. (##) SPDIFRX pins configuration:
  23. (+++) Enable the clock for the SPDIFRX GPIOs.
  24. (+++) Configure these SPDIFRX pins as alternate function pull-up.
  25. (##) NVIC configuration if you need to use interrupt process (HAL_SPDIFRX_ReceiveControlFlow_IT() and HAL_SPDIFRX_ReceiveDataFlow_IT() API's).
  26. (+++) Configure the SPDIFRX interrupt priority.
  27. (+++) Enable the NVIC SPDIFRX IRQ handle.
  28. (##) DMA Configuration if you need to use DMA process (HAL_SPDIFRX_ReceiveDataFlow_DMA() and HAL_SPDIFRX_ReceiveControlFlow_DMA() API's).
  29. (+++) Declare a DMA handle structure for the reception of the Data Flow channel.
  30. (+++) Declare a DMA handle structure for the reception of the Control Flow channel.
  31. (+++) Enable the DMAx interface clock.
  32. (+++) Configure the declared DMA handle structure CtrlRx/DataRx with the required parameters.
  33. (+++) Configure the DMA Channel.
  34. (+++) Associate the initialized DMA handle to the SPDIFRX DMA CtrlRx/DataRx handle.
  35. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  36. DMA CtrlRx/DataRx channel.
  37. (#) Program the input selection, re-tries number, wait for activity, channel status selection, data format, stereo mode and masking of user bits
  38. using HAL_SPDIFRX_Init() function.
  39. -@- The specific SPDIFRX interrupts (RXNE/CSRNE and Error Interrupts) will be managed using the macros
  40. __SPDIFRX_ENABLE_IT() and __SPDIFRX_DISABLE_IT() inside the receive process.
  41. -@- Make sure that ck_spdif clock is configured.
  42. (#) Three operation modes are available within this driver :
  43. *** Polling mode for reception operation (for debug purpose) ***
  44. ================================================================
  45. [..]
  46. (+) Receive data flow in blocking mode using HAL_SPDIFRX_ReceiveDataFlow()
  47. (+) Receive control flow of data in blocking mode using HAL_SPDIFRX_ReceiveControlFlow()
  48. *** Interrupt mode for reception operation ***
  49. =========================================
  50. [..]
  51. (+) Receive an amount of data (Data Flow) in non blocking mode using HAL_SPDIFRX_ReceiveDataFlow_IT()
  52. (+) Receive an amount of data (Control Flow) in non blocking mode using HAL_SPDIFRX_ReceiveControlFlow_IT()
  53. (+) At reception end of half transfer HAL_SPDIFRX_RxHalfCpltCallback is executed and user can
  54. add his own code by customization of function pointer HAL_SPDIFRX_RxHalfCpltCallback
  55. (+) At reception end of transfer HAL_SPDIFRX_RxCpltCallback is executed and user can
  56. add his own code by customization of function pointer HAL_SPDIFRX_RxCpltCallback
  57. (+) In case of transfer Error, HAL_SPDIFRX_ErrorCallback() function is executed and user can
  58. add his own code by customization of function pointer HAL_SPDIFRX_ErrorCallback
  59. *** DMA mode for reception operation ***
  60. ========================================
  61. [..]
  62. (+) Receive an amount of data (Data Flow) in non blocking mode (DMA) using HAL_SPDIFRX_ReceiveDataFlow_DMA()
  63. (+) Receive an amount of data (Control Flow) in non blocking mode (DMA) using HAL_SPDIFRX_ReceiveControlFlow_DMA()
  64. (+) At reception end of half transfer HAL_SPDIFRX_RxHalfCpltCallback is executed and user can
  65. add his own code by customization of function pointer HAL_SPDIFRX_RxHalfCpltCallback
  66. (+) At reception end of transfer HAL_SPDIFRX_RxCpltCallback is executed and user can
  67. add his own code by customization of function pointer HAL_SPDIFRX_RxCpltCallback
  68. (+) In case of transfer Error, HAL_SPDIFRX_ErrorCallback() function is executed and user can
  69. add his own code by customization of function pointer HAL_SPDIFRX_ErrorCallback
  70. (+) Stop the DMA Transfer using HAL_SPDIFRX_DMAStop()
  71. *** SPDIFRX HAL driver macros list ***
  72. =============================================
  73. [..]
  74. Below the list of most used macros in USART HAL driver.
  75. (+) __HAL_SPDIFRX_IDLE: Disable the specified SPDIFRX peripheral (IDEL State)
  76. (+) __HAL_SPDIFRX_SYNC: Enable the synchronization state of the specified SPDIFRX peripheral (SYNC State)
  77. (+) __HAL_SPDIFRX_RCV: Enable the receive state of the specified SPDIFRX peripheral (RCV State)
  78. (+) __HAL_SPDIFRX_ENABLE_IT : Enable the specified SPDIFRX interrupts
  79. (+) __HAL_SPDIFRX_DISABLE_IT : Disable the specified SPDIFRX interrupts
  80. (+) __HAL_SPDIFRX_GET_FLAG: Check whether the specified SPDIFRX flag is set or not.
  81. [..]
  82. (@) You can refer to the SPDIFRX HAL driver header file for more useful macros
  83. @endverbatim
  84. ******************************************************************************
  85. * @attention
  86. *
  87. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  88. *
  89. * Redistribution and use in source and binary forms, with or without modification,
  90. * are permitted provided that the following conditions are met:
  91. * 1. Redistributions of source code must retain the above copyright notice,
  92. * this list of conditions and the following disclaimer.
  93. * 2. Redistributions in binary form must reproduce the above copyright notice,
  94. * this list of conditions and the following disclaimer in the documentation
  95. * and/or other materials provided with the distribution.
  96. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  97. * may be used to endorse or promote products derived from this software
  98. * without specific prior written permission.
  99. *
  100. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  101. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  102. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  103. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  104. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  105. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  106. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  107. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  108. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  109. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  110. *
  111. ******************************************************************************
  112. */
  113. /* Includes ------------------------------------------------------------------*/
  114. #include "stm32f7xx_hal.h"
  115. /** @addtogroup STM32F7xx_HAL_Driver
  116. * @{
  117. */
  118. /** @defgroup SPDIFRX SPDIFRX
  119. * @brief SPDIFRX HAL module driver
  120. * @{
  121. */
  122. #ifdef HAL_SPDIFRX_MODULE_ENABLED
  123. #if defined (SPDIFRX)
  124. /* Private typedef -----------------------------------------------------------*/
  125. /* Private define ------------------------------------------------------------*/
  126. #define SPDIFRX_TIMEOUT_VALUE 0xFFFF
  127. /* Private macro -------------------------------------------------------------*/
  128. /* Private variables ---------------------------------------------------------*/
  129. /* Private function prototypes -----------------------------------------------*/
  130. /** @addtogroup SPDIFRX_Private_Functions
  131. * @{
  132. */
  133. static void SPDIFRX_DMARxCplt(DMA_HandleTypeDef *hdma);
  134. static void SPDIFRX_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  135. static void SPDIFRX_DMACxCplt(DMA_HandleTypeDef *hdma);
  136. static void SPDIFRX_DMACxHalfCplt(DMA_HandleTypeDef *hdma);
  137. static void SPDIFRX_DMAError(DMA_HandleTypeDef *hdma);
  138. static void SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif);
  139. static void SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif);
  140. static HAL_StatusTypeDef SPDIFRX_WaitOnFlagUntilTimeout(SPDIFRX_HandleTypeDef *hspdif, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t tickstart);
  141. /**
  142. * @}
  143. */
  144. /* Exported functions ---------------------------------------------------------*/
  145. /** @defgroup SPDIFRX_Exported_Functions SPDIFRX Exported Functions
  146. * @{
  147. */
  148. /** @defgroup SPDIFRX_Exported_Functions_Group1 Initialization and de-initialization functions
  149. * @brief Initialization and Configuration functions
  150. *
  151. @verbatim
  152. ===============================================================================
  153. ##### Initialization and de-initialization functions #####
  154. ===============================================================================
  155. [..] This subsection provides a set of functions allowing to initialize and
  156. de-initialize the SPDIFRX peripheral:
  157. (+) User must Implement HAL_SPDIFRX_MspInit() function in which he configures
  158. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  159. (+) Call the function HAL_SPDIFRX_Init() to configure the SPDIFRX peripheral with
  160. the selected configuration:
  161. (++) Input Selection (IN0, IN1,...)
  162. (++) Maximum allowed re-tries during synchronization phase
  163. (++) Wait for activity on SPDIF selected input
  164. (++) Channel status selection (from channel A or B)
  165. (++) Data format (LSB, MSB, ...)
  166. (++) Stereo mode
  167. (++) User bits masking (PT,C,U,V,...)
  168. (+) Call the function HAL_SPDIFRX_DeInit() to restore the default configuration
  169. of the selected SPDIFRXx peripheral.
  170. @endverbatim
  171. * @{
  172. */
  173. /**
  174. * @brief Initializes the SPDIFRX according to the specified parameters
  175. * in the SPDIFRX_InitTypeDef and create the associated handle.
  176. * @param hspdif: SPDIFRX handle
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef HAL_SPDIFRX_Init(SPDIFRX_HandleTypeDef *hspdif)
  180. {
  181. uint32_t tmpreg = 0;
  182. /* Check the SPDIFRX handle allocation */
  183. if(hspdif == NULL)
  184. {
  185. return HAL_ERROR;
  186. }
  187. /* Check the SPDIFRX parameters */
  188. assert_param(IS_STEREO_MODE(hspdif->Init.StereoMode));
  189. assert_param(IS_SPDIFRX_INPUT_SELECT(hspdif->Init.InputSelection));
  190. assert_param(IS_SPDIFRX_MAX_RETRIES(hspdif->Init.Retries));
  191. assert_param(IS_SPDIFRX_WAIT_FOR_ACTIVITY(hspdif->Init.WaitForActivity));
  192. assert_param(IS_SPDIFRX_CHANNEL(hspdif->Init.ChannelSelection));
  193. assert_param(IS_SPDIFRX_DATA_FORMAT(hspdif->Init.DataFormat));
  194. assert_param(IS_PREAMBLE_TYPE_MASK(hspdif->Init.PreambleTypeMask));
  195. assert_param(IS_CHANNEL_STATUS_MASK(hspdif->Init.ChannelStatusMask));
  196. assert_param(IS_VALIDITY_MASK(hspdif->Init.ValidityBitMask));
  197. assert_param(IS_PARITY_ERROR_MASK(hspdif->Init.ParityErrorMask));
  198. if(hspdif->State == HAL_SPDIFRX_STATE_RESET)
  199. {
  200. /* Allocate lock resource and initialize it */
  201. hspdif->Lock = HAL_UNLOCKED;
  202. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  203. HAL_SPDIFRX_MspInit(hspdif);
  204. }
  205. /* SPDIFRX peripheral state is BUSY*/
  206. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  207. /* Disable SPDIFRX interface (IDLE State) */
  208. __HAL_SPDIFRX_IDLE(hspdif);
  209. /* Reset the old SPDIFRX CR configuration */
  210. tmpreg = hspdif->Instance->CR;
  211. tmpreg &= ~((uint16_t) SPDIFRX_CR_RXSTEO | SPDIFRX_CR_DRFMT | SPDIFRX_CR_PMSK |
  212. SPDIFRX_CR_VMSK | SPDIFRX_CR_CUMSK | SPDIFRX_CR_PTMSK |
  213. SPDIFRX_CR_CHSEL | SPDIFRX_CR_NBTR | SPDIFRX_CR_WFA | SPDIFRX_CR_INSEL);
  214. /* Sets the new configuration of the SPDIFRX peripheral */
  215. tmpreg |= ((uint16_t) hspdif->Init.StereoMode |
  216. hspdif->Init.InputSelection |
  217. hspdif->Init.Retries |
  218. hspdif->Init.WaitForActivity |
  219. hspdif->Init.ChannelSelection |
  220. hspdif->Init.DataFormat |
  221. hspdif->Init.PreambleTypeMask |
  222. hspdif->Init.ChannelStatusMask |
  223. hspdif->Init.ValidityBitMask |
  224. hspdif->Init.ParityErrorMask);
  225. hspdif->Instance->CR = tmpreg;
  226. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  227. /* SPDIFRX peripheral state is READY*/
  228. hspdif->State = HAL_SPDIFRX_STATE_READY;
  229. return HAL_OK;
  230. }
  231. /**
  232. * @brief DeInitializes the SPDIFRX peripheral
  233. * @param hspdif: SPDIFRX handle
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_SPDIFRX_DeInit(SPDIFRX_HandleTypeDef *hspdif)
  237. {
  238. /* Check the SPDIFRX handle allocation */
  239. if(hspdif == NULL)
  240. {
  241. return HAL_ERROR;
  242. }
  243. /* Check the parameters */
  244. assert_param(IS_SPDIFRX_ALL_INSTANCE(hspdif->Instance));
  245. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  246. /* Disable SPDIFRX interface (IDLE state) */
  247. __HAL_SPDIFRX_IDLE(hspdif);
  248. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  249. HAL_SPDIFRX_MspDeInit(hspdif);
  250. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  251. /* SPDIFRX peripheral state is RESET*/
  252. hspdif->State = HAL_SPDIFRX_STATE_RESET;
  253. /* Release Lock */
  254. __HAL_UNLOCK(hspdif);
  255. return HAL_OK;
  256. }
  257. /**
  258. * @brief SPDIFRX MSP Init
  259. * @param hspdif: SPDIFRX handle
  260. * @retval None
  261. */
  262. __weak void HAL_SPDIFRX_MspInit(SPDIFRX_HandleTypeDef *hspdif)
  263. {
  264. /* Prevent unused argument(s) compilation warning */
  265. UNUSED(hspdif);
  266. /* NOTE : This function Should not be modified, when the callback is needed,
  267. the HAL_SPDIFRX_MspInit could be implemented in the user file
  268. */
  269. }
  270. /**
  271. * @brief SPDIFRX MSP DeInit
  272. * @param hspdif: SPDIFRX handle
  273. * @retval None
  274. */
  275. __weak void HAL_SPDIFRX_MspDeInit(SPDIFRX_HandleTypeDef *hspdif)
  276. {
  277. /* Prevent unused argument(s) compilation warning */
  278. UNUSED(hspdif);
  279. /* NOTE : This function Should not be modified, when the callback is needed,
  280. the HAL_SPDIFRX_MspDeInit could be implemented in the user file
  281. */
  282. }
  283. /**
  284. * @brief Sets the SPDIFRX dtat format according to the specified parameters
  285. * in the SPDIFRX_InitTypeDef.
  286. * @param hspdif: SPDIFRX handle
  287. * @param sDataFormat: SPDIFRX data format
  288. * @retval HAL status
  289. */
  290. HAL_StatusTypeDef HAL_SPDIFRX_SetDataFormat(SPDIFRX_HandleTypeDef *hspdif, SPDIFRX_SetDataFormatTypeDef sDataFormat)
  291. {
  292. uint32_t tmpreg = 0;
  293. /* Check the SPDIFRX handle allocation */
  294. if(hspdif == NULL)
  295. {
  296. return HAL_ERROR;
  297. }
  298. /* Check the SPDIFRX parameters */
  299. assert_param(IS_STEREO_MODE(sDataFormat.StereoMode));
  300. assert_param(IS_SPDIFRX_DATA_FORMAT(sDataFormat.DataFormat));
  301. assert_param(IS_PREAMBLE_TYPE_MASK(sDataFormat.PreambleTypeMask));
  302. assert_param(IS_CHANNEL_STATUS_MASK(sDataFormat.ChannelStatusMask));
  303. assert_param(IS_VALIDITY_MASK(sDataFormat.ValidityBitMask));
  304. assert_param(IS_PARITY_ERROR_MASK(sDataFormat.ParityErrorMask));
  305. /* Reset the old SPDIFRX CR configuration */
  306. tmpreg = hspdif->Instance->CR;
  307. if(((tmpreg & SPDIFRX_STATE_RCV) == SPDIFRX_STATE_RCV) &&
  308. (((tmpreg & SPDIFRX_CR_DRFMT) != sDataFormat.DataFormat) ||
  309. ((tmpreg & SPDIFRX_CR_RXSTEO) != sDataFormat.StereoMode)))
  310. {
  311. return HAL_ERROR;
  312. }
  313. tmpreg &= ~((uint16_t) SPDIFRX_CR_RXSTEO | SPDIFRX_CR_DRFMT | SPDIFRX_CR_PMSK |
  314. SPDIFRX_CR_VMSK | SPDIFRX_CR_CUMSK | SPDIFRX_CR_PTMSK);
  315. /* Sets the new configuration of the SPDIFRX peripheral */
  316. tmpreg |= ((uint16_t) sDataFormat.StereoMode |
  317. sDataFormat.DataFormat |
  318. sDataFormat.PreambleTypeMask |
  319. sDataFormat.ChannelStatusMask |
  320. sDataFormat.ValidityBitMask |
  321. sDataFormat.ParityErrorMask);
  322. hspdif->Instance->CR = tmpreg;
  323. return HAL_OK;
  324. }
  325. /**
  326. * @}
  327. */
  328. /** @defgroup SPDIFRX_Exported_Functions_Group2 IO operation functions
  329. * @brief Data transfers functions
  330. *
  331. @verbatim
  332. ===============================================================================
  333. ##### IO operation functions #####
  334. ===============================================================================
  335. [..]
  336. This subsection provides a set of functions allowing to manage the SPDIFRX data
  337. transfers.
  338. (#) There is two mode of transfer:
  339. (++) Blocking mode : The communication is performed in the polling mode.
  340. The status of all data processing is returned by the same function
  341. after finishing transfer.
  342. (++) No-Blocking mode : The communication is performed using Interrupts
  343. or DMA. These functions return the status of the transfer start-up.
  344. The end of the data processing will be indicated through the
  345. dedicated SPDIFRX IRQ when using Interrupt mode or the DMA IRQ when
  346. using DMA mode.
  347. (#) Blocking mode functions are :
  348. (++) HAL_SPDIFRX_ReceiveDataFlow()
  349. (++) HAL_SPDIFRX_ReceiveControlFlow()
  350. (+@) Do not use blocking mode to receive both control and data flow at the same time.
  351. (#) No-Blocking mode functions with Interrupt are :
  352. (++) HAL_SPDIFRX_ReceiveControlFlow_IT()
  353. (++) HAL_SPDIFRX_ReceiveDataFlow_IT()
  354. (#) No-Blocking mode functions with DMA are :
  355. (++) HAL_SPDIFRX_ReceiveControlFlow_DMA()
  356. (++) HAL_SPDIFRX_ReceiveDataFlow_DMA()
  357. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  358. (++) HAL_SPDIFRX_RxCpltCallback()
  359. (++) HAL_SPDIFRX_ErrorCallback()
  360. @endverbatim
  361. * @{
  362. */
  363. /**
  364. * @brief Receives an amount of data (Data Flow) in blocking mode.
  365. * @param hspdif: pointer to SPDIFRX_HandleTypeDef structure that contains
  366. * the configuration information for SPDIFRX module.
  367. * @param pData: Pointer to data buffer
  368. * @param Size: Amount of data to be received
  369. * @param Timeout: Timeout duration
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size, uint32_t Timeout)
  373. {
  374. uint32_t tickstart = 0U;
  375. if((pData == NULL ) || (Size == 0U))
  376. {
  377. return HAL_ERROR;
  378. }
  379. if(hspdif->State == HAL_SPDIFRX_STATE_READY)
  380. {
  381. /* Process Locked */
  382. __HAL_LOCK(hspdif);
  383. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  384. /* Start synchronisation */
  385. __HAL_SPDIFRX_SYNC(hspdif);
  386. /* Get tick */
  387. tickstart = HAL_GetTick();
  388. /* Wait until SYNCD flag is set */
  389. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, Timeout, tickstart) != HAL_OK)
  390. {
  391. return HAL_TIMEOUT;
  392. }
  393. /* Start reception */
  394. __HAL_SPDIFRX_RCV(hspdif);
  395. /* Receive data flow */
  396. while(Size > 0U)
  397. {
  398. /* Get tick */
  399. tickstart = HAL_GetTick();
  400. /* Wait until RXNE flag is set */
  401. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  402. {
  403. return HAL_TIMEOUT;
  404. }
  405. (*pData++) = hspdif->Instance->DR;
  406. Size--;
  407. }
  408. /* SPDIFRX ready */
  409. hspdif->State = HAL_SPDIFRX_STATE_READY;
  410. /* Process Unlocked */
  411. __HAL_UNLOCK(hspdif);
  412. return HAL_OK;
  413. }
  414. else
  415. {
  416. return HAL_BUSY;
  417. }
  418. }
  419. /**
  420. * @brief Receives an amount of data (Control Flow) in blocking mode.
  421. * @param hspdif: pointer to a SPDIFRX_HandleTypeDef structure that contains
  422. * the configuration information for SPDIFRX module.
  423. * @param pData: Pointer to data buffer
  424. * @param Size: Amount of data to be received
  425. * @param Timeout: Timeout duration
  426. * @retval HAL status
  427. */
  428. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size, uint32_t Timeout)
  429. {
  430. uint32_t tickstart = 0U;
  431. if((pData == NULL ) || (Size == 0U))
  432. {
  433. return HAL_ERROR;
  434. }
  435. if(hspdif->State == HAL_SPDIFRX_STATE_READY)
  436. {
  437. /* Process Locked */
  438. __HAL_LOCK(hspdif);
  439. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  440. /* Start synchronization */
  441. __HAL_SPDIFRX_SYNC(hspdif);
  442. /* Get tick */
  443. tickstart = HAL_GetTick();
  444. /* Wait until SYNCD flag is set */
  445. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, Timeout, tickstart) != HAL_OK)
  446. {
  447. return HAL_TIMEOUT;
  448. }
  449. /* Start reception */
  450. __HAL_SPDIFRX_RCV(hspdif);
  451. /* Receive control flow */
  452. while(Size > 0U)
  453. {
  454. /* Get tick */
  455. tickstart = HAL_GetTick();
  456. /* Wait until CSRNE flag is set */
  457. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_CSRNE, RESET, Timeout, tickstart) != HAL_OK)
  458. {
  459. return HAL_TIMEOUT;
  460. }
  461. (*pData++) = hspdif->Instance->CSR;
  462. Size--;
  463. }
  464. /* SPDIFRX ready */
  465. hspdif->State = HAL_SPDIFRX_STATE_READY;
  466. /* Process Unlocked */
  467. __HAL_UNLOCK(hspdif);
  468. return HAL_OK;
  469. }
  470. else
  471. {
  472. return HAL_BUSY;
  473. }
  474. }
  475. /**
  476. * @brief Receive an amount of data (Data Flow) in non-blocking mode with Interrupt
  477. * @param hspdif: SPDIFRX handle
  478. * @param pData: a 32-bit pointer to the Receive data buffer.
  479. * @param Size: number of data sample to be received .
  480. * @retval HAL status
  481. */
  482. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  483. {
  484. uint32_t tickstart = 0U;
  485. if((hspdif->State == HAL_SPDIFRX_STATE_READY) || (hspdif->State == HAL_SPDIFRX_STATE_BUSY_CX))
  486. {
  487. if((pData == NULL) || (Size == 0U))
  488. {
  489. return HAL_ERROR;
  490. }
  491. /* Process Locked */
  492. __HAL_LOCK(hspdif);
  493. hspdif->pRxBuffPtr = pData;
  494. hspdif->RxXferSize = Size;
  495. hspdif->RxXferCount = Size;
  496. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  497. /* Check if a receive process is ongoing or not */
  498. hspdif->State = HAL_SPDIFRX_STATE_BUSY_RX;
  499. /* Enable the SPDIFRX PE Error Interrupt */
  500. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  501. /* Enable the SPDIFRX OVR Error Interrupt */
  502. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  503. /* Process Unlocked */
  504. __HAL_UNLOCK(hspdif);
  505. /* Enable the SPDIFRX RXNE interrupt */
  506. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  507. if (((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_SYNC) || ((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != 0x00U))
  508. {
  509. /* Start synchronization */
  510. __HAL_SPDIFRX_SYNC(hspdif);
  511. /* Get tick */
  512. tickstart = HAL_GetTick();
  513. /* Wait until SYNCD flag is set */
  514. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, SPDIFRX_TIMEOUT_VALUE, tickstart) != HAL_OK)
  515. {
  516. return HAL_TIMEOUT;
  517. }
  518. /* Start reception */
  519. __HAL_SPDIFRX_RCV(hspdif);
  520. }
  521. return HAL_OK;
  522. }
  523. else
  524. {
  525. return HAL_BUSY;
  526. }
  527. }
  528. /**
  529. * @brief Receive an amount of data (Control Flow) with Interrupt
  530. * @param hspdif: SPDIFRX handle
  531. * @param pData: a 32-bit pointer to the Receive data buffer.
  532. * @param Size: number of data sample (Control Flow) to be received :
  533. * @retval HAL status
  534. */
  535. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  536. {
  537. uint32_t tickstart = 0U;
  538. if((hspdif->State == HAL_SPDIFRX_STATE_READY) || (hspdif->State == HAL_SPDIFRX_STATE_BUSY_RX))
  539. {
  540. if((pData == NULL ) || (Size == 0U))
  541. {
  542. return HAL_ERROR;
  543. }
  544. /* Process Locked */
  545. __HAL_LOCK(hspdif);
  546. hspdif->pCsBuffPtr = pData;
  547. hspdif->CsXferSize = Size;
  548. hspdif->CsXferCount = Size;
  549. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  550. /* Check if a receive process is ongoing or not */
  551. hspdif->State = HAL_SPDIFRX_STATE_BUSY_CX;
  552. /* Enable the SPDIFRX PE Error Interrupt */
  553. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  554. /* Enable the SPDIFRX OVR Error Interrupt */
  555. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(hspdif);
  558. /* Enable the SPDIFRX CSRNE interrupt */
  559. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  560. if (((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_SYNC) || ((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != 0x00U))
  561. {
  562. /* Start synchronization */
  563. __HAL_SPDIFRX_SYNC(hspdif);
  564. /* Get tick */
  565. tickstart = HAL_GetTick();
  566. /* Wait until SYNCD flag is set */
  567. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, SPDIFRX_TIMEOUT_VALUE, tickstart) != HAL_OK)
  568. {
  569. return HAL_TIMEOUT;
  570. }
  571. /* Start reception */
  572. __HAL_SPDIFRX_RCV(hspdif);
  573. }
  574. return HAL_OK;
  575. }
  576. else
  577. {
  578. return HAL_BUSY;
  579. }
  580. }
  581. /**
  582. * @brief Receive an amount of data (Data Flow) mode with DMA
  583. * @param hspdif: SPDIFRX handle
  584. * @param pData: a 32-bit pointer to the Receive data buffer.
  585. * @param Size: number of data sample to be received :
  586. * @retval HAL status
  587. */
  588. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow_DMA(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  589. {
  590. uint32_t tickstart = 0U;
  591. if((pData == NULL) || (Size == 0U))
  592. {
  593. return HAL_ERROR;
  594. }
  595. if((hspdif->State == HAL_SPDIFRX_STATE_READY) || (hspdif->State == HAL_SPDIFRX_STATE_BUSY_CX))
  596. {
  597. hspdif->pRxBuffPtr = pData;
  598. hspdif->RxXferSize = Size;
  599. hspdif->RxXferCount = Size;
  600. /* Process Locked */
  601. __HAL_LOCK(hspdif);
  602. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  603. hspdif->State = HAL_SPDIFRX_STATE_BUSY_RX;
  604. /* Set the SPDIFRX Rx DMA Half transfer complete callback */
  605. hspdif->hdmaDrRx->XferHalfCpltCallback = SPDIFRX_DMARxHalfCplt;
  606. /* Set the SPDIFRX Rx DMA transfer complete callback */
  607. hspdif->hdmaDrRx->XferCpltCallback = SPDIFRX_DMARxCplt;
  608. /* Set the DMA error callback */
  609. hspdif->hdmaDrRx->XferErrorCallback = SPDIFRX_DMAError;
  610. /* Enable the DMA request */
  611. HAL_DMA_Start_IT(hspdif->hdmaDrRx, (uint32_t)&hspdif->Instance->DR, (uint32_t)hspdif->pRxBuffPtr, Size);
  612. /* Enable RXDMAEN bit in SPDIFRX CR register for data flow reception*/
  613. hspdif->Instance->CR |= SPDIFRX_CR_RXDMAEN;
  614. if (((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_SYNC) || ((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != 0x00U))
  615. {
  616. /* Start synchronization */
  617. __HAL_SPDIFRX_SYNC(hspdif);
  618. /* Get tick */
  619. tickstart = HAL_GetTick();
  620. /* Wait until SYNCD flag is set */
  621. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, SPDIFRX_TIMEOUT_VALUE, tickstart) != HAL_OK)
  622. {
  623. return HAL_TIMEOUT;
  624. }
  625. /* Start reception */
  626. __HAL_SPDIFRX_RCV(hspdif);
  627. }
  628. /* Process Unlocked */
  629. __HAL_UNLOCK(hspdif);
  630. return HAL_OK;
  631. }
  632. else
  633. {
  634. return HAL_BUSY;
  635. }
  636. }
  637. /**
  638. * @brief Receive an amount of data (Control Flow) with DMA
  639. * @param hspdif: SPDIFRX handle
  640. * @param pData: a 32-bit pointer to the Receive data buffer.
  641. * @param Size: number of data (Control Flow) sample to be received :
  642. * @retval HAL status
  643. */
  644. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow_DMA(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  645. {
  646. uint32_t tickstart = 0U;
  647. if((pData == NULL) || (Size == 0U))
  648. {
  649. return HAL_ERROR;
  650. }
  651. if((hspdif->State == HAL_SPDIFRX_STATE_READY) || (hspdif->State == HAL_SPDIFRX_STATE_BUSY_RX))
  652. {
  653. hspdif->pCsBuffPtr = pData;
  654. hspdif->CsXferSize = Size;
  655. hspdif->CsXferCount = Size;
  656. /* Process Locked */
  657. __HAL_LOCK(hspdif);
  658. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  659. hspdif->State = HAL_SPDIFRX_STATE_BUSY_CX;
  660. /* Set the SPDIFRX Rx DMA Half transfer complete callback */
  661. hspdif->hdmaCsRx->XferHalfCpltCallback = SPDIFRX_DMACxHalfCplt;
  662. /* Set the SPDIFRX Rx DMA transfer complete callback */
  663. hspdif->hdmaCsRx->XferCpltCallback = SPDIFRX_DMACxCplt;
  664. /* Set the DMA error callback */
  665. hspdif->hdmaCsRx->XferErrorCallback = SPDIFRX_DMAError;
  666. /* Enable the DMA request */
  667. HAL_DMA_Start_IT(hspdif->hdmaCsRx, (uint32_t)&hspdif->Instance->CSR, (uint32_t)hspdif->pCsBuffPtr, Size);
  668. /* Enable CBDMAEN bit in SPDIFRX CR register for control flow reception*/
  669. hspdif->Instance->CR |= SPDIFRX_CR_CBDMAEN;
  670. if (((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_SYNC) || ((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != 0x00U))
  671. {
  672. /* Start synchronization */
  673. __HAL_SPDIFRX_SYNC(hspdif);
  674. /* Get tick */
  675. tickstart = HAL_GetTick();
  676. /* Wait until SYNCD flag is set */
  677. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, SPDIFRX_TIMEOUT_VALUE, tickstart) != HAL_OK)
  678. {
  679. return HAL_TIMEOUT;
  680. }
  681. /* Start reception */
  682. __HAL_SPDIFRX_RCV(hspdif);
  683. }
  684. /* Process Unlocked */
  685. __HAL_UNLOCK(hspdif);
  686. return HAL_OK;
  687. }
  688. else
  689. {
  690. return HAL_BUSY;
  691. }
  692. }
  693. /**
  694. * @brief stop the audio stream receive from the Media.
  695. * @param hspdif: SPDIFRX handle
  696. * @retval None
  697. */
  698. HAL_StatusTypeDef HAL_SPDIFRX_DMAStop(SPDIFRX_HandleTypeDef *hspdif)
  699. {
  700. /* Process Locked */
  701. __HAL_LOCK(hspdif);
  702. /* Disable the SPDIFRX DMA requests */
  703. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_RXDMAEN);
  704. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_CBDMAEN);
  705. /* Disable the SPDIFRX DMA channel */
  706. __HAL_DMA_DISABLE(hspdif->hdmaDrRx);
  707. __HAL_DMA_DISABLE(hspdif->hdmaCsRx);
  708. /* Disable SPDIFRX peripheral */
  709. __HAL_SPDIFRX_IDLE(hspdif);
  710. hspdif->State = HAL_SPDIFRX_STATE_READY;
  711. /* Process Unlocked */
  712. __HAL_UNLOCK(hspdif);
  713. return HAL_OK;
  714. }
  715. /**
  716. * @brief This function handles SPDIFRX interrupt request.
  717. * @param hspdif: SPDIFRX handle
  718. * @retval HAL status
  719. */
  720. void HAL_SPDIFRX_IRQHandler(SPDIFRX_HandleTypeDef *hspdif)
  721. {
  722. /* SPDIFRX in mode Data Flow Reception ------------------------------------------------*/
  723. if((__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_RXNE) != RESET) && (__HAL_SPDIFRX_GET_IT_SOURCE(hspdif, SPDIFRX_IT_RXNE) != RESET))
  724. {
  725. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_RXNE);
  726. SPDIFRX_ReceiveDataFlow_IT(hspdif);
  727. }
  728. /* SPDIFRX in mode Control Flow Reception ------------------------------------------------*/
  729. if((__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_CSRNE) != RESET) && (__HAL_SPDIFRX_GET_IT_SOURCE(hspdif, SPDIFRX_IT_CSRNE) != RESET))
  730. {
  731. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_CSRNE);
  732. SPDIFRX_ReceiveControlFlow_IT(hspdif);
  733. }
  734. /* SPDIFRX Overrun error interrupt occurred ---------------------------------*/
  735. if((__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_OVR) != RESET) && (__HAL_SPDIFRX_GET_IT_SOURCE(hspdif, SPDIFRX_IT_OVRIE) != RESET))
  736. {
  737. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_FLAG_OVR);
  738. /* Change the SPDIFRX error code */
  739. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_OVR;
  740. /* the transfer is not stopped */
  741. HAL_SPDIFRX_ErrorCallback(hspdif);
  742. }
  743. /* SPDIFRX Parity error interrupt occurred ---------------------------------*/
  744. if((__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_PERR) != RESET) && (__HAL_SPDIFRX_GET_IT_SOURCE(hspdif, SPDIFRX_IT_PERRIE) != RESET))
  745. {
  746. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_FLAG_PERR);
  747. /* Change the SPDIFRX error code */
  748. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_PE;
  749. /* the transfer is not stopped */
  750. HAL_SPDIFRX_ErrorCallback(hspdif);
  751. }
  752. }
  753. /**
  754. * @brief Rx Transfer (Data flow) half completed callbacks
  755. * @param hspdif: SPDIFRX handle
  756. * @retval None
  757. */
  758. __weak void HAL_SPDIFRX_RxHalfCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  759. {
  760. /* Prevent unused argument(s) compilation warning */
  761. UNUSED(hspdif);
  762. /* NOTE : This function Should not be modified, when the callback is needed,
  763. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  764. */
  765. }
  766. /**
  767. * @brief Rx Transfer (Data flow) completed callbacks
  768. * @param hspdif: SPDIFRX handle
  769. * @retval None
  770. */
  771. __weak void HAL_SPDIFRX_RxCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  772. {
  773. /* Prevent unused argument(s) compilation warning */
  774. UNUSED(hspdif);
  775. /* NOTE : This function Should not be modified, when the callback is needed,
  776. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  777. */
  778. }
  779. /**
  780. * @brief Rx (Control flow) Transfer half completed callbacks
  781. * @param hspdif: SPDIFRX handle
  782. * @retval None
  783. */
  784. __weak void HAL_SPDIFRX_CxHalfCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  785. {
  786. /* Prevent unused argument(s) compilation warning */
  787. UNUSED(hspdif);
  788. /* NOTE : This function Should not be modified, when the callback is needed,
  789. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  790. */
  791. }
  792. /**
  793. * @brief Rx Transfer (Control flow) completed callbacks
  794. * @param hspdif: SPDIFRX handle
  795. * @retval None
  796. */
  797. __weak void HAL_SPDIFRX_CxCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  798. {
  799. /* Prevent unused argument(s) compilation warning */
  800. UNUSED(hspdif);
  801. /* NOTE : This function Should not be modified, when the callback is needed,
  802. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  803. */
  804. }
  805. /**
  806. * @brief SPDIFRX error callbacks
  807. * @param hspdif: SPDIFRX handle
  808. * @retval None
  809. */
  810. __weak void HAL_SPDIFRX_ErrorCallback(SPDIFRX_HandleTypeDef *hspdif)
  811. {
  812. /* Prevent unused argument(s) compilation warning */
  813. UNUSED(hspdif);
  814. /* NOTE : This function Should not be modified, when the callback is needed,
  815. the HAL_SPDIFRX_ErrorCallback could be implemented in the user file
  816. */
  817. }
  818. /**
  819. * @}
  820. */
  821. /** @defgroup SPDIFRX_Exported_Functions_Group3 Peripheral State and Errors functions
  822. * @brief Peripheral State functions
  823. *
  824. @verbatim
  825. ===============================================================================
  826. ##### Peripheral State and Errors functions #####
  827. ===============================================================================
  828. [..]
  829. This subsection permit to get in run-time the status of the peripheral
  830. and the data flow.
  831. @endverbatim
  832. * @{
  833. */
  834. /**
  835. * @brief Return the SPDIFRX state
  836. * @param hspdif : SPDIFRX handle
  837. * @retval HAL state
  838. */
  839. HAL_SPDIFRX_StateTypeDef HAL_SPDIFRX_GetState(SPDIFRX_HandleTypeDef *hspdif)
  840. {
  841. return hspdif->State;
  842. }
  843. /**
  844. * @brief Return the SPDIFRX error code
  845. * @param hspdif : SPDIFRX handle
  846. * @retval SPDIFRX Error Code
  847. */
  848. uint32_t HAL_SPDIFRX_GetError(SPDIFRX_HandleTypeDef *hspdif)
  849. {
  850. return hspdif->ErrorCode;
  851. }
  852. /**
  853. * @}
  854. */
  855. /**
  856. * @brief DMA SPDIFRX receive process (Data flow) complete callback
  857. * @param hdma : DMA handle
  858. * @retval None
  859. */
  860. static void SPDIFRX_DMARxCplt(DMA_HandleTypeDef *hdma)
  861. {
  862. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  863. /* Disable Rx DMA Request */
  864. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  865. {
  866. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_RXDMAEN);
  867. hspdif->RxXferCount = 0;
  868. hspdif->State = HAL_SPDIFRX_STATE_READY;
  869. }
  870. HAL_SPDIFRX_RxCpltCallback(hspdif);
  871. }
  872. /**
  873. * @brief DMA SPDIFRX receive process (Data flow) half complete callback
  874. * @param hdma : DMA handle
  875. * @retval None
  876. */
  877. static void SPDIFRX_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  878. {
  879. SPDIFRX_HandleTypeDef* hspdif = (SPDIFRX_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  880. HAL_SPDIFRX_RxHalfCpltCallback(hspdif);
  881. }
  882. /**
  883. * @brief DMA SPDIFRX receive process (Control flow) complete callback
  884. * @param hdma : DMA handle
  885. * @retval None
  886. */
  887. static void SPDIFRX_DMACxCplt(DMA_HandleTypeDef *hdma)
  888. {
  889. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  890. /* Disable Cb DMA Request */
  891. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_CBDMAEN);
  892. hspdif->CsXferCount = 0;
  893. hspdif->State = HAL_SPDIFRX_STATE_READY;
  894. HAL_SPDIFRX_CxCpltCallback(hspdif);
  895. }
  896. /**
  897. * @brief DMA SPDIFRX receive process (Control flow) half complete callback
  898. * @param hdma : DMA handle
  899. * @retval None
  900. */
  901. static void SPDIFRX_DMACxHalfCplt(DMA_HandleTypeDef *hdma)
  902. {
  903. SPDIFRX_HandleTypeDef* hspdif = (SPDIFRX_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  904. HAL_SPDIFRX_CxHalfCpltCallback(hspdif);
  905. }
  906. /**
  907. * @brief DMA SPDIFRX communication error callback
  908. * @param hdma : DMA handle
  909. * @retval None
  910. */
  911. static void SPDIFRX_DMAError(DMA_HandleTypeDef *hdma)
  912. {
  913. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  914. /* Disable Rx and Cb DMA Request */
  915. hspdif->Instance->CR &= (uint16_t)(~(SPDIFRX_CR_RXDMAEN | SPDIFRX_CR_CBDMAEN));
  916. hspdif->RxXferCount = 0;
  917. hspdif->State= HAL_SPDIFRX_STATE_READY;
  918. /* Set the error code and execute error callback*/
  919. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_DMA;
  920. HAL_SPDIFRX_ErrorCallback(hspdif);
  921. }
  922. /**
  923. * @brief Receive an amount of data (Data Flow) with Interrupt
  924. * @param hspdif: SPDIFRX handle
  925. * @retval None
  926. */
  927. static void SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif)
  928. {
  929. /* Receive data */
  930. (*hspdif->pRxBuffPtr++) = hspdif->Instance->DR;
  931. hspdif->RxXferCount--;
  932. if(hspdif->RxXferCount == 0)
  933. {
  934. /* Disable RXNE/PE and OVR interrupts */
  935. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE | SPDIFRX_IT_PERRIE | SPDIFRX_IT_RXNE);
  936. hspdif->State = HAL_SPDIFRX_STATE_READY;
  937. /* Process Unlocked */
  938. __HAL_UNLOCK(hspdif);
  939. HAL_SPDIFRX_RxCpltCallback(hspdif);
  940. }
  941. }
  942. /**
  943. * @brief Receive an amount of data (Control Flow) with Interrupt
  944. * @param hspdif: SPDIFRX handle
  945. * @retval None
  946. */
  947. static void SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif)
  948. {
  949. /* Receive data */
  950. (*hspdif->pCsBuffPtr++) = hspdif->Instance->CSR;
  951. hspdif->CsXferCount--;
  952. if(hspdif->CsXferCount == 0)
  953. {
  954. /* Disable CSRNE interrupt */
  955. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  956. hspdif->State = HAL_SPDIFRX_STATE_READY;
  957. /* Process Unlocked */
  958. __HAL_UNLOCK(hspdif);
  959. HAL_SPDIFRX_CxCpltCallback(hspdif);
  960. }
  961. }
  962. /**
  963. * @brief This function handles SPDIFRX Communication Timeout.
  964. * @param hspdif: SPDIFRX handle
  965. * @param Flag: Flag checked
  966. * @param Status: Value of the flag expected
  967. * @param Timeout: Duration of the timeout
  968. * @param tickstart: Tick start value
  969. * @retval HAL status
  970. */
  971. static HAL_StatusTypeDef SPDIFRX_WaitOnFlagUntilTimeout(SPDIFRX_HandleTypeDef *hspdif, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t tickstart)
  972. {
  973. /* Wait until flag is set */
  974. if(Status == RESET)
  975. {
  976. while(__HAL_SPDIFRX_GET_FLAG(hspdif, Flag) == RESET)
  977. {
  978. /* Check for the Timeout */
  979. if(Timeout != HAL_MAX_DELAY)
  980. {
  981. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  982. {
  983. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  984. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  985. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  986. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  987. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  988. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  989. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  990. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  991. hspdif->State= HAL_SPDIFRX_STATE_READY;
  992. /* Process Unlocked */
  993. __HAL_UNLOCK(hspdif);
  994. return HAL_TIMEOUT;
  995. }
  996. }
  997. }
  998. }
  999. else
  1000. {
  1001. while(__HAL_SPDIFRX_GET_FLAG(hspdif, Flag) != RESET)
  1002. {
  1003. /* Check for the Timeout */
  1004. if(Timeout != HAL_MAX_DELAY)
  1005. {
  1006. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1007. {
  1008. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1009. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  1010. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  1011. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  1012. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  1013. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  1014. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  1015. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  1016. hspdif->State= HAL_SPDIFRX_STATE_READY;
  1017. /* Process Unlocked */
  1018. __HAL_UNLOCK(hspdif);
  1019. return HAL_TIMEOUT;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. return HAL_OK;
  1025. }
  1026. /**
  1027. * @}
  1028. */
  1029. #endif /* SPDIFRX */
  1030. #endif /* HAL_SPDIFRX_MODULE_ENABLED */
  1031. /**
  1032. * @}
  1033. */
  1034. /**
  1035. * @}
  1036. */
  1037. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/