stm32f7xx_hal_spdifrx.c 40 KB

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