stm32f4xx_hal_spdifrx.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 SPDIFRX 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. *** Callback registration ***
  82. =============================================
  83. The compilation define USE_HAL_SPDIFRX_REGISTER_CALLBACKS when set to 1
  84. allows the user to configure dynamically the driver callbacks.
  85. Use HAL_SPDIFRX_RegisterCallback() function to register an interrupt callback.
  86. The HAL_SPDIFRX_RegisterCallback() function allows to register the following callbacks:
  87. (+) RxHalfCpltCallback : SPDIFRX Data flow half completed callback.
  88. (+) RxCpltCallback : SPDIFRX Data flow completed callback.
  89. (+) CxHalfCpltCallback : SPDIFRX Control flow half completed callback.
  90. (+) CxCpltCallback : SPDIFRX Control flow completed callback.
  91. (+) ErrorCallback : SPDIFRX error callback.
  92. (+) MspInitCallback : SPDIFRX MspInit.
  93. (+) MspDeInitCallback : SPDIFRX MspDeInit.
  94. This function takes as parameters the HAL peripheral handle, the Callback ID
  95. and a pointer to the user callback function.
  96. Use HAL_SPDIFRX_UnRegisterCallback() function to reset a callback to the default
  97. weak function.
  98. The HAL_SPDIFRX_UnRegisterCallback() function takes as parameters the HAL peripheral handle,
  99. and the Callback ID.
  100. This function allows to reset the following callbacks:
  101. (+) RxHalfCpltCallback : SPDIFRX Data flow half completed callback.
  102. (+) RxCpltCallback : SPDIFRX Data flow completed callback.
  103. (+) CxHalfCpltCallback : SPDIFRX Control flow half completed callback.
  104. (+) CxCpltCallback : SPDIFRX Control flow completed callback.
  105. (+) ErrorCallback : SPDIFRX error callback.
  106. (+) MspInitCallback : SPDIFRX MspInit.
  107. (+) MspDeInitCallback : SPDIFRX MspDeInit.
  108. By default, after the HAL_SPDIFRX_Init() and when the state is HAL_SPDIFRX_STATE_RESET
  109. all callbacks are set to the corresponding weak functions :
  110. HAL_SPDIFRX_RxHalfCpltCallback() , HAL_SPDIFRX_RxCpltCallback(), HAL_SPDIFRX_CxHalfCpltCallback(),
  111. HAL_SPDIFRX_CxCpltCallback() and HAL_SPDIFRX_ErrorCallback()
  112. Exception done for MspInit and MspDeInit functions that are
  113. reset to the legacy weak function in the HAL_SPDIFRX_Init()/ HAL_SPDIFRX_DeInit() only when
  114. these callbacks pointers are NULL (not registered beforehand).
  115. If not, MspInit or MspDeInit callbacks pointers are not null, the HAL_SPDIFRX_Init() / HAL_SPDIFRX_DeInit()
  116. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  117. Callbacks can be registered/unregistered in HAL_SPDIFRX_STATE_READY state only.
  118. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  119. in HAL_SPDIFRX_STATE_READY or HAL_SPDIFRX_STATE_RESET state,
  120. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  121. In that case first register the MspInit/MspDeInit user callbacks
  122. using HAL_SPDIFRX_RegisterCallback() before calling HAL_SPDIFRX_DeInit()
  123. or HAL_SPDIFRX_Init() function.
  124. When The compilation define USE_HAL_SPDIFRX_REGISTER_CALLBACKS is set to 0 or
  125. not defined, the callback registration feature is not available and all callbacks
  126. are set to the corresponding weak functions.
  127. @endverbatim
  128. ******************************************************************************
  129. * @attention
  130. *
  131. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  132. * All rights reserved.</center></h2>
  133. *
  134. * This software component is licensed by ST under BSD 3-Clause license,
  135. * the "License"; You may not use this file except in compliance with the
  136. * License. You may obtain a copy of the License at:
  137. * opensource.org/licenses/BSD-3-Clause
  138. *
  139. ******************************************************************************
  140. */
  141. /* Includes ------------------------------------------------------------------*/
  142. #include "stm32f4xx_hal.h"
  143. /** @addtogroup STM32F4xx_HAL_Driver
  144. * @{
  145. */
  146. /** @defgroup SPDIFRX SPDIFRX
  147. * @brief SPDIFRX HAL module driver
  148. * @{
  149. */
  150. #ifdef HAL_SPDIFRX_MODULE_ENABLED
  151. #if defined (SPDIFRX)
  152. #if defined(STM32F446xx)
  153. /* Private typedef -----------------------------------------------------------*/
  154. /* Private define ------------------------------------------------------------*/
  155. #define SPDIFRX_TIMEOUT_VALUE 0xFFFFU
  156. /* Private macro -------------------------------------------------------------*/
  157. /* Private variables ---------------------------------------------------------*/
  158. /* Private function prototypes -----------------------------------------------*/
  159. /** @addtogroup SPDIFRX_Private_Functions
  160. * @{
  161. */
  162. static void SPDIFRX_DMARxCplt(DMA_HandleTypeDef *hdma);
  163. static void SPDIFRX_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  164. static void SPDIFRX_DMACxCplt(DMA_HandleTypeDef *hdma);
  165. static void SPDIFRX_DMACxHalfCplt(DMA_HandleTypeDef *hdma);
  166. static void SPDIFRX_DMAError(DMA_HandleTypeDef *hdma);
  167. static void SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif);
  168. static void SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif);
  169. static HAL_StatusTypeDef SPDIFRX_WaitOnFlagUntilTimeout(SPDIFRX_HandleTypeDef *hspdif, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t tickstart);
  170. /**
  171. * @}
  172. */
  173. /* Exported functions ---------------------------------------------------------*/
  174. /** @defgroup SPDIFRX_Exported_Functions SPDIFRX Exported Functions
  175. * @{
  176. */
  177. /** @defgroup SPDIFRX_Exported_Functions_Group1 Initialization and de-initialization functions
  178. * @brief Initialization and Configuration functions
  179. *
  180. @verbatim
  181. ===============================================================================
  182. ##### Initialization and de-initialization functions #####
  183. ===============================================================================
  184. [..] This subsection provides a set of functions allowing to initialize and
  185. de-initialize the SPDIFRX peripheral:
  186. (+) User must Implement HAL_SPDIFRX_MspInit() function in which he configures
  187. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  188. (+) Call the function HAL_SPDIFRX_Init() to configure the SPDIFRX peripheral with
  189. the selected configuration:
  190. (++) Input Selection (IN0, IN1,...)
  191. (++) Maximum allowed re-tries during synchronization phase
  192. (++) Wait for activity on SPDIF selected input
  193. (++) Channel status selection (from channel A or B)
  194. (++) Data format (LSB, MSB, ...)
  195. (++) Stereo mode
  196. (++) User bits masking (PT,C,U,V,...)
  197. (+) Call the function HAL_SPDIFRX_DeInit() to restore the default configuration
  198. of the selected SPDIFRXx peripheral.
  199. @endverbatim
  200. * @{
  201. */
  202. /**
  203. * @brief Initializes the SPDIFRX according to the specified parameters
  204. * in the SPDIFRX_InitTypeDef and create the associated handle.
  205. * @param hspdif SPDIFRX handle
  206. * @retval HAL status
  207. */
  208. HAL_StatusTypeDef HAL_SPDIFRX_Init(SPDIFRX_HandleTypeDef *hspdif)
  209. {
  210. uint32_t tmpreg;
  211. /* Check the SPDIFRX handle allocation */
  212. if(hspdif == NULL)
  213. {
  214. return HAL_ERROR;
  215. }
  216. /* Check the SPDIFRX parameters */
  217. assert_param(IS_STEREO_MODE(hspdif->Init.StereoMode));
  218. assert_param(IS_SPDIFRX_INPUT_SELECT(hspdif->Init.InputSelection));
  219. assert_param(IS_SPDIFRX_MAX_RETRIES(hspdif->Init.Retries));
  220. assert_param(IS_SPDIFRX_WAIT_FOR_ACTIVITY(hspdif->Init.WaitForActivity));
  221. assert_param(IS_SPDIFRX_CHANNEL(hspdif->Init.ChannelSelection));
  222. assert_param(IS_SPDIFRX_DATA_FORMAT(hspdif->Init.DataFormat));
  223. assert_param(IS_PREAMBLE_TYPE_MASK(hspdif->Init.PreambleTypeMask));
  224. assert_param(IS_CHANNEL_STATUS_MASK(hspdif->Init.ChannelStatusMask));
  225. assert_param(IS_VALIDITY_MASK(hspdif->Init.ValidityBitMask));
  226. assert_param(IS_PARITY_ERROR_MASK(hspdif->Init.ParityErrorMask));
  227. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  228. if(hspdif->State == HAL_SPDIFRX_STATE_RESET)
  229. {
  230. /* Allocate lock resource and initialize it */
  231. hspdif->Lock = HAL_UNLOCKED;
  232. hspdif->RxHalfCpltCallback = HAL_SPDIFRX_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  233. hspdif->RxCpltCallback = HAL_SPDIFRX_RxCpltCallback; /* Legacy weak RxCpltCallback */
  234. hspdif->CxHalfCpltCallback = HAL_SPDIFRX_CxHalfCpltCallback; /* Legacy weak CxHalfCpltCallback */
  235. hspdif->CxCpltCallback = HAL_SPDIFRX_CxCpltCallback; /* Legacy weak CxCpltCallback */
  236. hspdif->ErrorCallback = HAL_SPDIFRX_ErrorCallback; /* Legacy weak ErrorCallback */
  237. if(hspdif->MspInitCallback == NULL)
  238. {
  239. hspdif->MspInitCallback = HAL_SPDIFRX_MspInit; /* Legacy weak MspInit */
  240. }
  241. /* Init the low level hardware */
  242. hspdif->MspInitCallback(hspdif);
  243. }
  244. #else
  245. if(hspdif->State == HAL_SPDIFRX_STATE_RESET)
  246. {
  247. /* Allocate lock resource and initialize it */
  248. hspdif->Lock = HAL_UNLOCKED;
  249. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  250. HAL_SPDIFRX_MspInit(hspdif);
  251. }
  252. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  253. /* SPDIFRX peripheral state is BUSY */
  254. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  255. /* Disable SPDIFRX interface (IDLE State) */
  256. __HAL_SPDIFRX_IDLE(hspdif);
  257. /* Reset the old SPDIFRX CR configuration */
  258. tmpreg = hspdif->Instance->CR;
  259. tmpreg &= ~(SPDIFRX_CR_RXSTEO | SPDIFRX_CR_DRFMT | SPDIFRX_CR_PMSK |
  260. SPDIFRX_CR_VMSK | SPDIFRX_CR_CUMSK | SPDIFRX_CR_PTMSK |
  261. SPDIFRX_CR_CHSEL | SPDIFRX_CR_NBTR | SPDIFRX_CR_WFA |
  262. SPDIFRX_CR_INSEL);
  263. /* Sets the new configuration of the SPDIFRX peripheral */
  264. tmpreg |= (hspdif->Init.StereoMode |
  265. hspdif->Init.InputSelection |
  266. hspdif->Init.Retries |
  267. hspdif->Init.WaitForActivity |
  268. hspdif->Init.ChannelSelection |
  269. hspdif->Init.DataFormat |
  270. hspdif->Init.PreambleTypeMask |
  271. hspdif->Init.ChannelStatusMask |
  272. hspdif->Init.ValidityBitMask |
  273. hspdif->Init.ParityErrorMask
  274. );
  275. hspdif->Instance->CR = tmpreg;
  276. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  277. /* SPDIFRX peripheral state is READY*/
  278. hspdif->State = HAL_SPDIFRX_STATE_READY;
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief DeInitializes the SPDIFRX peripheral
  283. * @param hspdif SPDIFRX handle
  284. * @retval HAL status
  285. */
  286. HAL_StatusTypeDef HAL_SPDIFRX_DeInit(SPDIFRX_HandleTypeDef *hspdif)
  287. {
  288. /* Check the SPDIFRX handle allocation */
  289. if(hspdif == NULL)
  290. {
  291. return HAL_ERROR;
  292. }
  293. /* Check the parameters */
  294. assert_param(IS_SPDIFRX_ALL_INSTANCE(hspdif->Instance));
  295. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  296. /* Disable SPDIFRX interface (IDLE state) */
  297. __HAL_SPDIFRX_IDLE(hspdif);
  298. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  299. if(hspdif->MspDeInitCallback == NULL)
  300. {
  301. hspdif->MspDeInitCallback = HAL_SPDIFRX_MspDeInit; /* Legacy weak MspDeInit */
  302. }
  303. /* DeInit the low level hardware */
  304. hspdif->MspDeInitCallback(hspdif);
  305. #else
  306. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  307. HAL_SPDIFRX_MspDeInit(hspdif);
  308. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  309. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  310. /* SPDIFRX peripheral state is RESET*/
  311. hspdif->State = HAL_SPDIFRX_STATE_RESET;
  312. /* Release Lock */
  313. __HAL_UNLOCK(hspdif);
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief SPDIFRX MSP Init
  318. * @param hspdif SPDIFRX handle
  319. * @retval None
  320. */
  321. __weak void HAL_SPDIFRX_MspInit(SPDIFRX_HandleTypeDef *hspdif)
  322. {
  323. /* Prevent unused argument(s) compilation warning */
  324. UNUSED(hspdif);
  325. /* NOTE : This function Should not be modified, when the callback is needed,
  326. the HAL_SPDIFRX_MspInit could be implemented in the user file
  327. */
  328. }
  329. /**
  330. * @brief SPDIFRX MSP DeInit
  331. * @param hspdif SPDIFRX handle
  332. * @retval None
  333. */
  334. __weak void HAL_SPDIFRX_MspDeInit(SPDIFRX_HandleTypeDef *hspdif)
  335. {
  336. /* Prevent unused argument(s) compilation warning */
  337. UNUSED(hspdif);
  338. /* NOTE : This function Should not be modified, when the callback is needed,
  339. the HAL_SPDIFRX_MspDeInit could be implemented in the user file
  340. */
  341. }
  342. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  343. /**
  344. * @brief Register a User SPDIFRX Callback
  345. * To be used instead of the weak predefined callback
  346. * @param hspdif SPDIFRX handle
  347. * @param CallbackID ID of the callback to be registered
  348. * This parameter can be one of the following values:
  349. * @arg @ref HAL_SPDIFRX_RX_HALF_CB_ID SPDIFRX Data flow half completed callback ID
  350. * @arg @ref HAL_SPDIFRX_RX_CPLT_CB_ID SPDIFRX Data flow completed callback ID
  351. * @arg @ref HAL_SPDIFRX_CX_HALF_CB_ID SPDIFRX Control flow half completed callback ID
  352. * @arg @ref HAL_SPDIFRX_CX_CPLT_CB_ID SPDIFRX Control flow completed callback ID
  353. * @arg @ref HAL_SPDIFRX_ERROR_CB_ID SPDIFRX error callback ID
  354. * @arg @ref HAL_SPDIFRX_MSPINIT_CB_ID MspInit callback ID
  355. * @arg @ref HAL_SPDIFRX_MSPDEINIT_CB_ID MspDeInit callback ID
  356. * @param pCallback pointer to the Callback function
  357. * @retval HAL status
  358. */
  359. HAL_StatusTypeDef HAL_SPDIFRX_RegisterCallback(SPDIFRX_HandleTypeDef *hspdif, HAL_SPDIFRX_CallbackIDTypeDef CallbackID, pSPDIFRX_CallbackTypeDef pCallback)
  360. {
  361. HAL_StatusTypeDef status = HAL_OK;
  362. if(pCallback == NULL)
  363. {
  364. /* Update the error code */
  365. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  366. return HAL_ERROR;
  367. }
  368. /* Process locked */
  369. __HAL_LOCK(hspdif);
  370. if(HAL_SPDIFRX_STATE_READY == hspdif->State)
  371. {
  372. switch (CallbackID)
  373. {
  374. case HAL_SPDIFRX_RX_HALF_CB_ID :
  375. hspdif->RxHalfCpltCallback = pCallback;
  376. break;
  377. case HAL_SPDIFRX_RX_CPLT_CB_ID :
  378. hspdif->RxCpltCallback = pCallback;
  379. break;
  380. case HAL_SPDIFRX_CX_HALF_CB_ID :
  381. hspdif->CxHalfCpltCallback = pCallback;
  382. break;
  383. case HAL_SPDIFRX_CX_CPLT_CB_ID :
  384. hspdif->CxCpltCallback = pCallback;
  385. break;
  386. case HAL_SPDIFRX_ERROR_CB_ID :
  387. hspdif->ErrorCallback = pCallback;
  388. break;
  389. case HAL_SPDIFRX_MSPINIT_CB_ID :
  390. hspdif->MspInitCallback = pCallback;
  391. break;
  392. case HAL_SPDIFRX_MSPDEINIT_CB_ID :
  393. hspdif->MspDeInitCallback = pCallback;
  394. break;
  395. default :
  396. /* Update the error code */
  397. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  398. /* Return error status */
  399. status = HAL_ERROR;
  400. break;
  401. }
  402. }
  403. else if(HAL_SPDIFRX_STATE_RESET == hspdif->State)
  404. {
  405. switch (CallbackID)
  406. {
  407. case HAL_SPDIFRX_MSPINIT_CB_ID :
  408. hspdif->MspInitCallback = pCallback;
  409. break;
  410. case HAL_SPDIFRX_MSPDEINIT_CB_ID :
  411. hspdif->MspDeInitCallback = pCallback;
  412. break;
  413. default :
  414. /* Update the error code */
  415. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  416. /* Return error status */
  417. status = HAL_ERROR;
  418. break;
  419. }
  420. }
  421. else
  422. {
  423. /* Update the error code */
  424. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  425. /* Return error status */
  426. status = HAL_ERROR;
  427. }
  428. /* Release Lock */
  429. __HAL_UNLOCK(hspdif);
  430. return status;
  431. }
  432. /**
  433. * @brief Unregister a SPDIFRX Callback
  434. * SPDIFRX callabck is redirected to the weak predefined callback
  435. * @param hspdif SPDIFRX handle
  436. * @param CallbackID ID of the callback to be unregistered
  437. * This parameter can be one of the following values:
  438. * @arg @ref HAL_SPDIFRX_RX_HALF_CB_ID SPDIFRX Data flow half completed callback ID
  439. * @arg @ref HAL_SPDIFRX_RX_CPLT_CB_ID SPDIFRX Data flow completed callback ID
  440. * @arg @ref HAL_SPDIFRX_CX_HALF_CB_ID SPDIFRX Control flow half completed callback ID
  441. * @arg @ref HAL_SPDIFRX_CX_CPLT_CB_ID SPDIFRX Control flow completed callback ID
  442. * @arg @ref HAL_SPDIFRX_ERROR_CB_ID SPDIFRX error callback ID
  443. * @arg @ref HAL_SPDIFRX_MSPINIT_CB_ID MspInit callback ID
  444. * @arg @ref HAL_SPDIFRX_MSPDEINIT_CB_ID MspDeInit callback ID
  445. * @retval HAL status
  446. */
  447. HAL_StatusTypeDef HAL_SPDIFRX_UnRegisterCallback(SPDIFRX_HandleTypeDef *hspdif, HAL_SPDIFRX_CallbackIDTypeDef CallbackID)
  448. {
  449. HAL_StatusTypeDef status = HAL_OK;
  450. /* Process locked */
  451. __HAL_LOCK(hspdif);
  452. if(HAL_SPDIFRX_STATE_READY == hspdif->State)
  453. {
  454. switch (CallbackID)
  455. {
  456. case HAL_SPDIFRX_RX_HALF_CB_ID :
  457. hspdif->RxHalfCpltCallback = HAL_SPDIFRX_RxHalfCpltCallback;
  458. break;
  459. case HAL_SPDIFRX_RX_CPLT_CB_ID :
  460. hspdif->RxCpltCallback = HAL_SPDIFRX_RxCpltCallback;
  461. break;
  462. case HAL_SPDIFRX_CX_HALF_CB_ID :
  463. hspdif->CxHalfCpltCallback = HAL_SPDIFRX_CxHalfCpltCallback;
  464. break;
  465. case HAL_SPDIFRX_CX_CPLT_CB_ID :
  466. hspdif->CxCpltCallback = HAL_SPDIFRX_CxCpltCallback;
  467. break;
  468. case HAL_SPDIFRX_ERROR_CB_ID :
  469. hspdif->ErrorCallback = HAL_SPDIFRX_ErrorCallback;
  470. break;
  471. default :
  472. /* Update the error code */
  473. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  474. /* Return error status */
  475. status = HAL_ERROR;
  476. break;
  477. }
  478. }
  479. else if(HAL_SPDIFRX_STATE_RESET == hspdif->State)
  480. {
  481. switch (CallbackID)
  482. {
  483. case HAL_SPDIFRX_MSPINIT_CB_ID :
  484. hspdif->MspInitCallback = HAL_SPDIFRX_MspInit; /* Legacy weak MspInit */
  485. break;
  486. case HAL_SPDIFRX_MSPDEINIT_CB_ID :
  487. hspdif->MspDeInitCallback = HAL_SPDIFRX_MspDeInit; /* Legacy weak MspInit */
  488. break;
  489. default :
  490. /* Update the error code */
  491. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  492. /* Return error status */
  493. status = HAL_ERROR;
  494. break;
  495. }
  496. }
  497. else
  498. {
  499. /* Update the error code */
  500. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_INVALID_CALLBACK;
  501. /* Return error status */
  502. status = HAL_ERROR;
  503. }
  504. /* Release Lock */
  505. __HAL_UNLOCK(hspdif);
  506. return status;
  507. }
  508. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  509. /**
  510. * @brief Set the SPDIFRX data format according to the specified parameters in the SPDIFRX_InitTypeDef.
  511. * @param hspdif SPDIFRX handle
  512. * @param sDataFormat SPDIFRX data format
  513. * @retval HAL status
  514. */
  515. HAL_StatusTypeDef HAL_SPDIFRX_SetDataFormat(SPDIFRX_HandleTypeDef *hspdif, SPDIFRX_SetDataFormatTypeDef sDataFormat)
  516. {
  517. uint32_t tmpreg;
  518. /* Check the SPDIFRX handle allocation */
  519. if(hspdif == NULL)
  520. {
  521. return HAL_ERROR;
  522. }
  523. /* Check the SPDIFRX parameters */
  524. assert_param(IS_STEREO_MODE(sDataFormat.StereoMode));
  525. assert_param(IS_SPDIFRX_DATA_FORMAT(sDataFormat.DataFormat));
  526. assert_param(IS_PREAMBLE_TYPE_MASK(sDataFormat.PreambleTypeMask));
  527. assert_param(IS_CHANNEL_STATUS_MASK(sDataFormat.ChannelStatusMask));
  528. assert_param(IS_VALIDITY_MASK(sDataFormat.ValidityBitMask));
  529. assert_param(IS_PARITY_ERROR_MASK(sDataFormat.ParityErrorMask));
  530. /* Reset the old SPDIFRX CR configuration */
  531. tmpreg = hspdif->Instance->CR;
  532. if(((tmpreg & SPDIFRX_STATE_RCV) == SPDIFRX_STATE_RCV) &&
  533. (((tmpreg & SPDIFRX_CR_DRFMT) != sDataFormat.DataFormat) ||
  534. ((tmpreg & SPDIFRX_CR_RXSTEO) != sDataFormat.StereoMode)))
  535. {
  536. return HAL_ERROR;
  537. }
  538. tmpreg &= ~(SPDIFRX_CR_RXSTEO | SPDIFRX_CR_DRFMT | SPDIFRX_CR_PMSK |
  539. SPDIFRX_CR_VMSK | SPDIFRX_CR_CUMSK | SPDIFRX_CR_PTMSK);
  540. /* Configure the new data format */
  541. tmpreg |= (sDataFormat.StereoMode |
  542. sDataFormat.DataFormat |
  543. sDataFormat.PreambleTypeMask |
  544. sDataFormat.ChannelStatusMask |
  545. sDataFormat.ValidityBitMask |
  546. sDataFormat.ParityErrorMask);
  547. hspdif->Instance->CR = tmpreg;
  548. return HAL_OK;
  549. }
  550. /**
  551. * @}
  552. */
  553. /** @defgroup SPDIFRX_Exported_Functions_Group2 IO operation functions
  554. * @brief Data transfers functions
  555. *
  556. @verbatim
  557. ===============================================================================
  558. ##### IO operation functions #####
  559. ===============================================================================
  560. [..]
  561. This subsection provides a set of functions allowing to manage the SPDIFRX data
  562. transfers.
  563. (#) There is two mode of transfer:
  564. (++) Blocking mode : The communication is performed in the polling mode.
  565. The status of all data processing is returned by the same function
  566. after finishing transfer.
  567. (++) No-Blocking mode : The communication is performed using Interrupts
  568. or DMA. These functions return the status of the transfer start-up.
  569. The end of the data processing will be indicated through the
  570. dedicated SPDIFRX IRQ when using Interrupt mode or the DMA IRQ when
  571. using DMA mode.
  572. (#) Blocking mode functions are :
  573. (++) HAL_SPDIFRX_ReceiveDataFlow()
  574. (++) HAL_SPDIFRX_ReceiveControlFlow()
  575. (+@) Do not use blocking mode to receive both control and data flow at the same time.
  576. (#) No-Blocking mode functions with Interrupt are :
  577. (++) HAL_SPDIFRX_ReceiveControlFlow_IT()
  578. (++) HAL_SPDIFRX_ReceiveDataFlow_IT()
  579. (#) No-Blocking mode functions with DMA are :
  580. (++) HAL_SPDIFRX_ReceiveControlFlow_DMA()
  581. (++) HAL_SPDIFRX_ReceiveDataFlow_DMA()
  582. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  583. (++) HAL_SPDIFRX_RxCpltCallback()
  584. (++) HAL_SPDIFRX_CxCpltCallback()
  585. @endverbatim
  586. * @{
  587. */
  588. /**
  589. * @brief Receives an amount of data (Data Flow) in blocking mode.
  590. * @param hspdif pointer to SPDIFRX_HandleTypeDef structure that contains
  591. * the configuration information for SPDIFRX module.
  592. * @param pData Pointer to data buffer
  593. * @param Size Amount of data to be received
  594. * @param Timeout Timeout duration
  595. * @retval HAL status
  596. */
  597. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size, uint32_t Timeout)
  598. {
  599. uint32_t tickstart;
  600. uint16_t sizeCounter = Size;
  601. uint32_t *pTmpBuf = pData;
  602. if((pData == NULL ) || (Size == 0U))
  603. {
  604. return HAL_ERROR;
  605. }
  606. if(hspdif->State == HAL_SPDIFRX_STATE_READY)
  607. {
  608. /* Process Locked */
  609. __HAL_LOCK(hspdif);
  610. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  611. /* Start synchronisation */
  612. __HAL_SPDIFRX_SYNC(hspdif);
  613. /* Get tick */
  614. tickstart = HAL_GetTick();
  615. /* Wait until SYNCD flag is set */
  616. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_SYNCD, RESET, Timeout, tickstart) != HAL_OK)
  617. {
  618. return HAL_TIMEOUT;
  619. }
  620. /* Start reception */
  621. __HAL_SPDIFRX_RCV(hspdif);
  622. /* Receive data flow */
  623. while(sizeCounter > 0U)
  624. {
  625. /* Get tick */
  626. tickstart = HAL_GetTick();
  627. /* Wait until RXNE flag is set */
  628. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  629. {
  630. return HAL_TIMEOUT;
  631. }
  632. (*pTmpBuf) = hspdif->Instance->DR;
  633. pTmpBuf++;
  634. sizeCounter--;
  635. }
  636. /* SPDIFRX ready */
  637. hspdif->State = HAL_SPDIFRX_STATE_READY;
  638. /* Process Unlocked */
  639. __HAL_UNLOCK(hspdif);
  640. return HAL_OK;
  641. }
  642. else
  643. {
  644. return HAL_BUSY;
  645. }
  646. }
  647. /**
  648. * @brief Receives an amount of data (Control Flow) in blocking mode.
  649. * @param hspdif pointer to a SPDIFRX_HandleTypeDef structure that contains
  650. * the configuration information for SPDIFRX module.
  651. * @param pData Pointer to data buffer
  652. * @param Size Amount of data to be received
  653. * @param Timeout Timeout duration
  654. * @retval HAL status
  655. */
  656. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size, uint32_t Timeout)
  657. {
  658. uint32_t tickstart;
  659. uint16_t sizeCounter = Size;
  660. uint32_t *pTmpBuf = pData;
  661. if((pData == NULL ) || (Size == 0U))
  662. {
  663. return HAL_ERROR;
  664. }
  665. if(hspdif->State == HAL_SPDIFRX_STATE_READY)
  666. {
  667. /* Process Locked */
  668. __HAL_LOCK(hspdif);
  669. hspdif->State = HAL_SPDIFRX_STATE_BUSY;
  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, Timeout, tickstart) != HAL_OK)
  676. {
  677. return HAL_TIMEOUT;
  678. }
  679. /* Start reception */
  680. __HAL_SPDIFRX_RCV(hspdif);
  681. /* Receive control flow */
  682. while(sizeCounter > 0U)
  683. {
  684. /* Get tick */
  685. tickstart = HAL_GetTick();
  686. /* Wait until CSRNE flag is set */
  687. if(SPDIFRX_WaitOnFlagUntilTimeout(hspdif, SPDIFRX_FLAG_CSRNE, RESET, Timeout, tickstart) != HAL_OK)
  688. {
  689. return HAL_TIMEOUT;
  690. }
  691. (*pTmpBuf) = hspdif->Instance->CSR;
  692. pTmpBuf++;
  693. sizeCounter--;
  694. }
  695. /* SPDIFRX ready */
  696. hspdif->State = HAL_SPDIFRX_STATE_READY;
  697. /* Process Unlocked */
  698. __HAL_UNLOCK(hspdif);
  699. return HAL_OK;
  700. }
  701. else
  702. {
  703. return HAL_BUSY;
  704. }
  705. }
  706. /**
  707. * @brief Receive an amount of data (Data Flow) in non-blocking mode with Interrupt
  708. * @param hspdif SPDIFRX handle
  709. * @param pData a 32-bit pointer to the Receive data buffer.
  710. * @param Size number of data sample to be received .
  711. * @retval HAL status
  712. */
  713. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  714. {
  715. uint32_t count = SPDIFRX_TIMEOUT_VALUE * (SystemCoreClock / 24U / 1000U);
  716. const HAL_SPDIFRX_StateTypeDef tempState = hspdif->State;
  717. if((tempState == HAL_SPDIFRX_STATE_READY) || (tempState == HAL_SPDIFRX_STATE_BUSY_CX))
  718. {
  719. if((pData == NULL) || (Size == 0U))
  720. {
  721. return HAL_ERROR;
  722. }
  723. /* Process Locked */
  724. __HAL_LOCK(hspdif);
  725. hspdif->pRxBuffPtr = pData;
  726. hspdif->RxXferSize = Size;
  727. hspdif->RxXferCount = Size;
  728. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  729. /* Check if a receive process is ongoing or not */
  730. hspdif->State = HAL_SPDIFRX_STATE_BUSY_RX;
  731. /* Enable the SPDIFRX PE Error Interrupt */
  732. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  733. /* Enable the SPDIFRX OVR Error Interrupt */
  734. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  735. /* Enable the SPDIFRX RXNE interrupt */
  736. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  737. if((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_RCV)
  738. {
  739. /* Start synchronization */
  740. __HAL_SPDIFRX_SYNC(hspdif);
  741. /* Wait until SYNCD flag is set */
  742. do
  743. {
  744. if (count == 0U)
  745. {
  746. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  747. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  748. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  749. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  750. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  751. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  752. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  753. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  754. hspdif->State= HAL_SPDIFRX_STATE_READY;
  755. /* Process Unlocked */
  756. __HAL_UNLOCK(hspdif);
  757. return HAL_TIMEOUT;
  758. }
  759. count--;
  760. } while (__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_SYNCD) == RESET);
  761. /* Start reception */
  762. __HAL_SPDIFRX_RCV(hspdif);
  763. }
  764. /* Process Unlocked */
  765. __HAL_UNLOCK(hspdif);
  766. return HAL_OK;
  767. }
  768. else
  769. {
  770. return HAL_BUSY;
  771. }
  772. }
  773. /**
  774. * @brief Receive an amount of data (Control Flow) with Interrupt
  775. * @param hspdif SPDIFRX handle
  776. * @param pData a 32-bit pointer to the Receive data buffer.
  777. * @param Size number of data sample (Control Flow) to be received
  778. * @retval HAL status
  779. */
  780. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  781. {
  782. uint32_t count = SPDIFRX_TIMEOUT_VALUE * (SystemCoreClock / 24U / 1000U);
  783. const HAL_SPDIFRX_StateTypeDef tempState = hspdif->State;
  784. if((tempState == HAL_SPDIFRX_STATE_READY) || (tempState == HAL_SPDIFRX_STATE_BUSY_RX))
  785. {
  786. if((pData == NULL ) || (Size == 0U))
  787. {
  788. return HAL_ERROR;
  789. }
  790. /* Process Locked */
  791. __HAL_LOCK(hspdif);
  792. hspdif->pCsBuffPtr = pData;
  793. hspdif->CsXferSize = Size;
  794. hspdif->CsXferCount = Size;
  795. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  796. /* Check if a receive process is ongoing or not */
  797. hspdif->State = HAL_SPDIFRX_STATE_BUSY_CX;
  798. /* Enable the SPDIFRX PE Error Interrupt */
  799. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  800. /* Enable the SPDIFRX OVR Error Interrupt */
  801. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  802. /* Enable the SPDIFRX CSRNE interrupt */
  803. __HAL_SPDIFRX_ENABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  804. if((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_RCV)
  805. {
  806. /* Start synchronization */
  807. __HAL_SPDIFRX_SYNC(hspdif);
  808. /* Wait until SYNCD flag is set */
  809. do
  810. {
  811. if (count == 0U)
  812. {
  813. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  814. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  815. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  816. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  817. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  818. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  819. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  820. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  821. hspdif->State= HAL_SPDIFRX_STATE_READY;
  822. /* Process Unlocked */
  823. __HAL_UNLOCK(hspdif);
  824. return HAL_TIMEOUT;
  825. }
  826. count--;
  827. } while (__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_SYNCD) == RESET);
  828. /* Start reception */
  829. __HAL_SPDIFRX_RCV(hspdif);
  830. }
  831. /* Process Unlocked */
  832. __HAL_UNLOCK(hspdif);
  833. return HAL_OK;
  834. }
  835. else
  836. {
  837. return HAL_BUSY;
  838. }
  839. }
  840. /**
  841. * @brief Receive an amount of data (Data Flow) mode with DMA
  842. * @param hspdif SPDIFRX handle
  843. * @param pData a 32-bit pointer to the Receive data buffer.
  844. * @param Size number of data sample to be received
  845. * @retval HAL status
  846. */
  847. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveDataFlow_DMA(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  848. {
  849. uint32_t count = SPDIFRX_TIMEOUT_VALUE * (SystemCoreClock / 24U / 1000U);
  850. const HAL_SPDIFRX_StateTypeDef tempState = hspdif->State;
  851. if((pData == NULL) || (Size == 0U))
  852. {
  853. return HAL_ERROR;
  854. }
  855. if((tempState == HAL_SPDIFRX_STATE_READY) || (tempState == HAL_SPDIFRX_STATE_BUSY_CX))
  856. {
  857. /* Process Locked */
  858. __HAL_LOCK(hspdif);
  859. hspdif->pRxBuffPtr = pData;
  860. hspdif->RxXferSize = Size;
  861. hspdif->RxXferCount = Size;
  862. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  863. hspdif->State = HAL_SPDIFRX_STATE_BUSY_RX;
  864. /* Set the SPDIFRX Rx DMA Half transfer complete callback */
  865. hspdif->hdmaDrRx->XferHalfCpltCallback = SPDIFRX_DMARxHalfCplt;
  866. /* Set the SPDIFRX Rx DMA transfer complete callback */
  867. hspdif->hdmaDrRx->XferCpltCallback = SPDIFRX_DMARxCplt;
  868. /* Set the DMA error callback */
  869. hspdif->hdmaDrRx->XferErrorCallback = SPDIFRX_DMAError;
  870. /* Enable the DMA request */
  871. if(HAL_DMA_Start_IT(hspdif->hdmaDrRx, (uint32_t)&hspdif->Instance->DR, (uint32_t)hspdif->pRxBuffPtr, Size) != HAL_OK)
  872. {
  873. /* Set SPDIFRX error */
  874. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_DMA;
  875. /* Set SPDIFRX state */
  876. hspdif->State = HAL_SPDIFRX_STATE_ERROR;
  877. /* Process Unlocked */
  878. __HAL_UNLOCK(hspdif);
  879. return HAL_ERROR;
  880. }
  881. /* Enable RXDMAEN bit in SPDIFRX CR register for data flow reception*/
  882. hspdif->Instance->CR |= SPDIFRX_CR_RXDMAEN;
  883. if((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_RCV)
  884. {
  885. /* Start synchronization */
  886. __HAL_SPDIFRX_SYNC(hspdif);
  887. /* Wait until SYNCD flag is set */
  888. do
  889. {
  890. if (count == 0U)
  891. {
  892. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  893. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  894. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  895. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  896. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  897. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  898. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  899. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  900. hspdif->State= HAL_SPDIFRX_STATE_READY;
  901. /* Process Unlocked */
  902. __HAL_UNLOCK(hspdif);
  903. return HAL_TIMEOUT;
  904. }
  905. count--;
  906. } while (__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_SYNCD) == RESET);
  907. /* Start reception */
  908. __HAL_SPDIFRX_RCV(hspdif);
  909. }
  910. /* Process Unlocked */
  911. __HAL_UNLOCK(hspdif);
  912. return HAL_OK;
  913. }
  914. else
  915. {
  916. return HAL_BUSY;
  917. }
  918. }
  919. /**
  920. * @brief Receive an amount of data (Control Flow) with DMA
  921. * @param hspdif SPDIFRX handle
  922. * @param pData a 32-bit pointer to the Receive data buffer.
  923. * @param Size number of data (Control Flow) sample to be received
  924. * @retval HAL status
  925. */
  926. HAL_StatusTypeDef HAL_SPDIFRX_ReceiveControlFlow_DMA(SPDIFRX_HandleTypeDef *hspdif, uint32_t *pData, uint16_t Size)
  927. {
  928. uint32_t count = SPDIFRX_TIMEOUT_VALUE * (SystemCoreClock / 24U / 1000U);
  929. const HAL_SPDIFRX_StateTypeDef tempState = hspdif->State;
  930. if((pData == NULL) || (Size == 0U))
  931. {
  932. return HAL_ERROR;
  933. }
  934. if((tempState == HAL_SPDIFRX_STATE_READY) || (tempState == HAL_SPDIFRX_STATE_BUSY_RX))
  935. {
  936. hspdif->pCsBuffPtr = pData;
  937. hspdif->CsXferSize = Size;
  938. hspdif->CsXferCount = Size;
  939. /* Process Locked */
  940. __HAL_LOCK(hspdif);
  941. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_NONE;
  942. hspdif->State = HAL_SPDIFRX_STATE_BUSY_CX;
  943. /* Set the SPDIFRX Rx DMA Half transfer complete callback */
  944. hspdif->hdmaCsRx->XferHalfCpltCallback = SPDIFRX_DMACxHalfCplt;
  945. /* Set the SPDIFRX Rx DMA transfer complete callback */
  946. hspdif->hdmaCsRx->XferCpltCallback = SPDIFRX_DMACxCplt;
  947. /* Set the DMA error callback */
  948. hspdif->hdmaCsRx->XferErrorCallback = SPDIFRX_DMAError;
  949. /* Enable the DMA request */
  950. if(HAL_DMA_Start_IT(hspdif->hdmaCsRx, (uint32_t)&hspdif->Instance->CSR, (uint32_t)hspdif->pCsBuffPtr, Size) != HAL_OK)
  951. {
  952. /* Set SPDIFRX error */
  953. hspdif->ErrorCode = HAL_SPDIFRX_ERROR_DMA;
  954. /* Set SPDIFRX state */
  955. hspdif->State = HAL_SPDIFRX_STATE_ERROR;
  956. /* Process Unlocked */
  957. __HAL_UNLOCK(hspdif);
  958. return HAL_ERROR;
  959. }
  960. /* Enable CBDMAEN bit in SPDIFRX CR register for control flow reception*/
  961. hspdif->Instance->CR |= SPDIFRX_CR_CBDMAEN;
  962. if((SPDIFRX->CR & SPDIFRX_CR_SPDIFEN) != SPDIFRX_STATE_RCV)
  963. {
  964. /* Start synchronization */
  965. __HAL_SPDIFRX_SYNC(hspdif);
  966. /* Wait until SYNCD flag is set */
  967. do
  968. {
  969. if (count == 0U)
  970. {
  971. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  972. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  973. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  974. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  975. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  976. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  977. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  978. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  979. hspdif->State= HAL_SPDIFRX_STATE_READY;
  980. /* Process Unlocked */
  981. __HAL_UNLOCK(hspdif);
  982. return HAL_TIMEOUT;
  983. }
  984. count--;
  985. } while (__HAL_SPDIFRX_GET_FLAG(hspdif, SPDIFRX_FLAG_SYNCD) == RESET);
  986. /* Start reception */
  987. __HAL_SPDIFRX_RCV(hspdif);
  988. }
  989. /* Process Unlocked */
  990. __HAL_UNLOCK(hspdif);
  991. return HAL_OK;
  992. }
  993. else
  994. {
  995. return HAL_BUSY;
  996. }
  997. }
  998. /**
  999. * @brief stop the audio stream receive from the Media.
  1000. * @param hspdif SPDIFRX handle
  1001. * @retval None
  1002. */
  1003. HAL_StatusTypeDef HAL_SPDIFRX_DMAStop(SPDIFRX_HandleTypeDef *hspdif)
  1004. {
  1005. /* Process Locked */
  1006. __HAL_LOCK(hspdif);
  1007. /* Disable the SPDIFRX DMA requests */
  1008. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_RXDMAEN);
  1009. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_CBDMAEN);
  1010. /* Disable the SPDIFRX DMA channel */
  1011. __HAL_DMA_DISABLE(hspdif->hdmaDrRx);
  1012. __HAL_DMA_DISABLE(hspdif->hdmaCsRx);
  1013. /* Disable SPDIFRX peripheral */
  1014. __HAL_SPDIFRX_IDLE(hspdif);
  1015. hspdif->State = HAL_SPDIFRX_STATE_READY;
  1016. /* Process Unlocked */
  1017. __HAL_UNLOCK(hspdif);
  1018. return HAL_OK;
  1019. }
  1020. /**
  1021. * @brief This function handles SPDIFRX interrupt request.
  1022. * @param hspdif SPDIFRX handle
  1023. * @retval HAL status
  1024. */
  1025. void HAL_SPDIFRX_IRQHandler(SPDIFRX_HandleTypeDef *hspdif)
  1026. {
  1027. uint32_t itFlag = hspdif->Instance->SR;
  1028. uint32_t itSource = hspdif->Instance->IMR;
  1029. /* SPDIFRX in mode Data Flow Reception */
  1030. if(((itFlag & SPDIFRX_FLAG_RXNE) == SPDIFRX_FLAG_RXNE) && ((itSource & SPDIFRX_IT_RXNE) == SPDIFRX_IT_RXNE))
  1031. {
  1032. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_RXNE);
  1033. SPDIFRX_ReceiveDataFlow_IT(hspdif);
  1034. }
  1035. /* SPDIFRX in mode Control Flow Reception */
  1036. if(((itFlag & SPDIFRX_FLAG_CSRNE) == SPDIFRX_FLAG_CSRNE) && ((itSource & SPDIFRX_IT_CSRNE) == SPDIFRX_IT_CSRNE))
  1037. {
  1038. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_CSRNE);
  1039. SPDIFRX_ReceiveControlFlow_IT(hspdif);
  1040. }
  1041. /* SPDIFRX Overrun error interrupt occurred */
  1042. if(((itFlag & SPDIFRX_FLAG_OVR) == SPDIFRX_FLAG_OVR) && ((itSource & SPDIFRX_IT_OVRIE) == SPDIFRX_IT_OVRIE))
  1043. {
  1044. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_OVRIE);
  1045. /* Change the SPDIFRX error code */
  1046. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_OVR;
  1047. /* the transfer is not stopped */
  1048. HAL_SPDIFRX_ErrorCallback(hspdif);
  1049. }
  1050. /* SPDIFRX Parity error interrupt occurred */
  1051. if(((itFlag & SPDIFRX_FLAG_PERR) == SPDIFRX_FLAG_PERR) && ((itSource & SPDIFRX_IT_PERRIE) == SPDIFRX_IT_PERRIE))
  1052. {
  1053. __HAL_SPDIFRX_CLEAR_IT(hspdif, SPDIFRX_IT_PERRIE);
  1054. /* Change the SPDIFRX error code */
  1055. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_PE;
  1056. /* the transfer is not stopped */
  1057. HAL_SPDIFRX_ErrorCallback(hspdif);
  1058. }
  1059. }
  1060. /**
  1061. * @brief Rx Transfer (Data flow) half completed callbacks
  1062. * @param hspdif SPDIFRX handle
  1063. * @retval None
  1064. */
  1065. __weak void HAL_SPDIFRX_RxHalfCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  1066. {
  1067. /* Prevent unused argument(s) compilation warning */
  1068. UNUSED(hspdif);
  1069. /* NOTE : This function Should not be modified, when the callback is needed,
  1070. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  1071. */
  1072. }
  1073. /**
  1074. * @brief Rx Transfer (Data flow) completed callbacks
  1075. * @param hspdif SPDIFRX handle
  1076. * @retval None
  1077. */
  1078. __weak void HAL_SPDIFRX_RxCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  1079. {
  1080. /* Prevent unused argument(s) compilation warning */
  1081. UNUSED(hspdif);
  1082. /* NOTE : This function Should not be modified, when the callback is needed,
  1083. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  1084. */
  1085. }
  1086. /**
  1087. * @brief Rx (Control flow) Transfer half completed callbacks
  1088. * @param hspdif SPDIFRX handle
  1089. * @retval None
  1090. */
  1091. __weak void HAL_SPDIFRX_CxHalfCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  1092. {
  1093. /* Prevent unused argument(s) compilation warning */
  1094. UNUSED(hspdif);
  1095. /* NOTE : This function Should not be modified, when the callback is needed,
  1096. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  1097. */
  1098. }
  1099. /**
  1100. * @brief Rx Transfer (Control flow) completed callbacks
  1101. * @param hspdif SPDIFRX handle
  1102. * @retval None
  1103. */
  1104. __weak void HAL_SPDIFRX_CxCpltCallback(SPDIFRX_HandleTypeDef *hspdif)
  1105. {
  1106. /* Prevent unused argument(s) compilation warning */
  1107. UNUSED(hspdif);
  1108. /* NOTE : This function Should not be modified, when the callback is needed,
  1109. the HAL_SPDIFRX_RxCpltCallback could be implemented in the user file
  1110. */
  1111. }
  1112. /**
  1113. * @brief SPDIFRX error callbacks
  1114. * @param hspdif SPDIFRX handle
  1115. * @retval None
  1116. */
  1117. __weak void HAL_SPDIFRX_ErrorCallback(SPDIFRX_HandleTypeDef *hspdif)
  1118. {
  1119. /* Prevent unused argument(s) compilation warning */
  1120. UNUSED(hspdif);
  1121. /* NOTE : This function Should not be modified, when the callback is needed,
  1122. the HAL_SPDIFRX_ErrorCallback could be implemented in the user file
  1123. */
  1124. }
  1125. /**
  1126. * @}
  1127. */
  1128. /** @defgroup SPDIFRX_Exported_Functions_Group3 Peripheral State and Errors functions
  1129. * @brief Peripheral State functions
  1130. *
  1131. @verbatim
  1132. ===============================================================================
  1133. ##### Peripheral State and Errors functions #####
  1134. ===============================================================================
  1135. [..]
  1136. This subsection permit to get in run-time the status of the peripheral
  1137. and the data flow.
  1138. @endverbatim
  1139. * @{
  1140. */
  1141. /**
  1142. * @brief Return the SPDIFRX state
  1143. * @param hspdif SPDIFRX handle
  1144. * @retval HAL state
  1145. */
  1146. HAL_SPDIFRX_StateTypeDef HAL_SPDIFRX_GetState(SPDIFRX_HandleTypeDef const * const hspdif)
  1147. {
  1148. return hspdif->State;
  1149. }
  1150. /**
  1151. * @brief Return the SPDIFRX error code
  1152. * @param hspdif SPDIFRX handle
  1153. * @retval SPDIFRX Error Code
  1154. */
  1155. uint32_t HAL_SPDIFRX_GetError(SPDIFRX_HandleTypeDef const * const hspdif)
  1156. {
  1157. return hspdif->ErrorCode;
  1158. }
  1159. /**
  1160. * @}
  1161. */
  1162. /**
  1163. * @brief DMA SPDIFRX receive process (Data flow) complete callback
  1164. * @param hdma DMA handle
  1165. * @retval None
  1166. */
  1167. static void SPDIFRX_DMARxCplt(DMA_HandleTypeDef *hdma)
  1168. {
  1169. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1170. /* Disable Rx DMA Request */
  1171. if(hdma->Init.Mode != DMA_CIRCULAR)
  1172. {
  1173. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_RXDMAEN);
  1174. hspdif->RxXferCount = 0;
  1175. hspdif->State = HAL_SPDIFRX_STATE_READY;
  1176. }
  1177. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1178. hspdif->RxCpltCallback(hspdif);
  1179. #else
  1180. HAL_SPDIFRX_RxCpltCallback(hspdif);
  1181. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1182. }
  1183. /**
  1184. * @brief DMA SPDIFRX receive process (Data flow) half complete callback
  1185. * @param hdma DMA handle
  1186. * @retval None
  1187. */
  1188. static void SPDIFRX_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1189. {
  1190. SPDIFRX_HandleTypeDef* hspdif = (SPDIFRX_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1191. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1192. hspdif->RxHalfCpltCallback(hspdif);
  1193. #else
  1194. HAL_SPDIFRX_RxHalfCpltCallback(hspdif);
  1195. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1196. }
  1197. /**
  1198. * @brief DMA SPDIFRX receive process (Control flow) complete callback
  1199. * @param hdma DMA handle
  1200. * @retval None
  1201. */
  1202. static void SPDIFRX_DMACxCplt(DMA_HandleTypeDef *hdma)
  1203. {
  1204. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1205. /* Disable Cb DMA Request */
  1206. hspdif->Instance->CR &= (uint16_t)(~SPDIFRX_CR_CBDMAEN);
  1207. hspdif->CsXferCount = 0;
  1208. hspdif->State = HAL_SPDIFRX_STATE_READY;
  1209. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1210. hspdif->CxCpltCallback(hspdif);
  1211. #else
  1212. HAL_SPDIFRX_CxCpltCallback(hspdif);
  1213. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1214. }
  1215. /**
  1216. * @brief DMA SPDIFRX receive process (Control flow) half complete callback
  1217. * @param hdma DMA handle
  1218. * @retval None
  1219. */
  1220. static void SPDIFRX_DMACxHalfCplt(DMA_HandleTypeDef *hdma)
  1221. {
  1222. SPDIFRX_HandleTypeDef* hspdif = (SPDIFRX_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1223. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1224. hspdif->CxHalfCpltCallback(hspdif);
  1225. #else
  1226. HAL_SPDIFRX_CxHalfCpltCallback(hspdif);
  1227. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1228. }
  1229. /**
  1230. * @brief DMA SPDIFRX communication error callback
  1231. * @param hdma DMA handle
  1232. * @retval None
  1233. */
  1234. static void SPDIFRX_DMAError(DMA_HandleTypeDef *hdma)
  1235. {
  1236. SPDIFRX_HandleTypeDef* hspdif = ( SPDIFRX_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1237. /* Disable Rx and Cb DMA Request */
  1238. hspdif->Instance->CR &= (uint16_t)(~(SPDIFRX_CR_RXDMAEN | SPDIFRX_CR_CBDMAEN));
  1239. hspdif->RxXferCount = 0;
  1240. hspdif->State= HAL_SPDIFRX_STATE_READY;
  1241. /* Set the error code and execute error callback*/
  1242. hspdif->ErrorCode |= HAL_SPDIFRX_ERROR_DMA;
  1243. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1244. /* The transfer is not stopped */
  1245. hspdif->ErrorCallback(hspdif);
  1246. #else
  1247. /* The transfer is not stopped */
  1248. HAL_SPDIFRX_ErrorCallback(hspdif);
  1249. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1250. }
  1251. /**
  1252. * @brief Receive an amount of data (Data Flow) with Interrupt
  1253. * @param hspdif SPDIFRX handle
  1254. * @retval None
  1255. */
  1256. static void SPDIFRX_ReceiveDataFlow_IT(SPDIFRX_HandleTypeDef *hspdif)
  1257. {
  1258. /* Receive data */
  1259. (*hspdif->pRxBuffPtr) = hspdif->Instance->DR;
  1260. hspdif->pRxBuffPtr++;
  1261. hspdif->RxXferCount--;
  1262. if(hspdif->RxXferCount == 0U)
  1263. {
  1264. /* Disable RXNE/PE and OVR interrupts */
  1265. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE | SPDIFRX_IT_PERRIE | SPDIFRX_IT_RXNE);
  1266. hspdif->State = HAL_SPDIFRX_STATE_READY;
  1267. /* Process Unlocked */
  1268. __HAL_UNLOCK(hspdif);
  1269. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1270. hspdif->RxCpltCallback(hspdif);
  1271. #else
  1272. HAL_SPDIFRX_RxCpltCallback(hspdif);
  1273. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1274. }
  1275. }
  1276. /**
  1277. * @brief Receive an amount of data (Control Flow) with Interrupt
  1278. * @param hspdif SPDIFRX handle
  1279. * @retval None
  1280. */
  1281. static void SPDIFRX_ReceiveControlFlow_IT(SPDIFRX_HandleTypeDef *hspdif)
  1282. {
  1283. /* Receive data */
  1284. (*hspdif->pCsBuffPtr) = hspdif->Instance->CSR;
  1285. hspdif->pCsBuffPtr++;
  1286. hspdif->CsXferCount--;
  1287. if(hspdif->CsXferCount == 0U)
  1288. {
  1289. /* Disable CSRNE interrupt */
  1290. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  1291. hspdif->State = HAL_SPDIFRX_STATE_READY;
  1292. /* Process Unlocked */
  1293. __HAL_UNLOCK(hspdif);
  1294. #if (USE_HAL_SPDIFRX_REGISTER_CALLBACKS == 1)
  1295. hspdif->CxCpltCallback(hspdif);
  1296. #else
  1297. HAL_SPDIFRX_CxCpltCallback(hspdif);
  1298. #endif /* USE_HAL_SPDIFRX_REGISTER_CALLBACKS */
  1299. }
  1300. }
  1301. /**
  1302. * @brief This function handles SPDIFRX Communication Timeout.
  1303. * @param hspdif SPDIFRX handle
  1304. * @param Flag Flag checked
  1305. * @param Status Value of the flag expected
  1306. * @param Timeout Duration of the timeout
  1307. * @param tickstart Tick start value
  1308. * @retval HAL status
  1309. */
  1310. static HAL_StatusTypeDef SPDIFRX_WaitOnFlagUntilTimeout(SPDIFRX_HandleTypeDef *hspdif, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t tickstart)
  1311. {
  1312. /* Wait until flag is set */
  1313. while(__HAL_SPDIFRX_GET_FLAG(hspdif, Flag) == Status)
  1314. {
  1315. /* Check for the Timeout */
  1316. if(Timeout != HAL_MAX_DELAY)
  1317. {
  1318. if(((HAL_GetTick() - tickstart ) > Timeout) || (Timeout == 0U))
  1319. {
  1320. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1321. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_RXNE);
  1322. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_CSRNE);
  1323. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_PERRIE);
  1324. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_OVRIE);
  1325. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SBLKIE);
  1326. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_SYNCDIE);
  1327. __HAL_SPDIFRX_DISABLE_IT(hspdif, SPDIFRX_IT_IFEIE);
  1328. hspdif->State= HAL_SPDIFRX_STATE_READY;
  1329. /* Process Unlocked */
  1330. __HAL_UNLOCK(hspdif);
  1331. return HAL_TIMEOUT;
  1332. }
  1333. }
  1334. }
  1335. return HAL_OK;
  1336. }
  1337. /**
  1338. * @}
  1339. */
  1340. #endif /* STM32F446xx */
  1341. #endif /* SPDIFRX */
  1342. #endif /* HAL_SPDIFRX_MODULE_ENABLED */
  1343. /**
  1344. * @}
  1345. */
  1346. /**
  1347. * @}
  1348. */
  1349. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/