1
0

stm32f4xx_hal_spdifrx.c 40 KB

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