No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

1153 líneas
37 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_i2s_ex.c
  4. * @author MCD Application Team
  5. * @brief I2S HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of I2S extension peripheral:
  8. * + Extension features Functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### I2S Extension features #####
  13. ==============================================================================
  14. [..]
  15. (#) In I2S full duplex mode, each SPI peripheral is able to manage sending and receiving
  16. data simultaneously using two data lines. Each SPI peripheral has an extended block
  17. called I2Sxext (i.e I2S2ext for SPI2 and I2S3ext for SPI3).
  18. (#) The extension block is not a full SPI IP, it is used only as I2S slave to
  19. implement full duplex mode. The extension block uses the same clock sources
  20. as its master.
  21. (#) Both I2Sx and I2Sx_ext can be configured as transmitters or receivers.
  22. [..]
  23. (@) Only I2Sx can deliver SCK and WS to I2Sx_ext in full duplex mode, where
  24. I2Sx can be I2S2 or I2S3.
  25. ##### How to use this driver #####
  26. ===============================================================================
  27. [..]
  28. Three operation modes are available within this driver :
  29. *** Polling mode IO operation ***
  30. =================================
  31. [..]
  32. (+) Send and receive in the same time an amount of data in blocking mode using HAL_I2SEx_TransmitReceive()
  33. *** Interrupt mode IO operation ***
  34. ===================================
  35. [..]
  36. (+) Send and receive in the same time an amount of data in non blocking mode using HAL_I2SEx_TransmitReceive_IT()
  37. (+) At transmission/reception end of transfer HAL_I2SEx_TxRxCpltCallback is executed and user can
  38. add his own code by customization of function pointer HAL_I2SEx_TxRxCpltCallback
  39. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  40. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  41. *** DMA mode IO operation ***
  42. ==============================
  43. [..]
  44. (+) Send and receive an amount of data in non blocking mode (DMA) using HAL_I2SEx_TransmitReceive_DMA()
  45. (+) At transmission/reception end of transfer HAL_I2SEx_TxRxCpltCallback is executed and user can
  46. add his own code by customization of function pointer HAL_I2S_TxRxCpltCallback
  47. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  48. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  49. @endverbatim
  50. Additional Figure: The Extended block uses the same clock sources as its master.
  51. +-----------------------+
  52. I2Sx_SCK | |
  53. ----------+-->| I2Sx |------------------->I2Sx_SD(in/out)
  54. +--|-->| |
  55. | | +-----------------------+
  56. | |
  57. I2S_WS | |
  58. ------>| |
  59. | | +-----------------------+
  60. | +-->| |
  61. | | I2Sx_ext |------------------->I2Sx_extSD(in/out)
  62. +----->| |
  63. +-----------------------+
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  68. * All rights reserved.</center></h2>
  69. *
  70. * This software component is licensed by ST under BSD 3-Clause license,
  71. * the "License"; You may not use this file except in compliance with the
  72. * License. You may obtain a copy of the License at:
  73. * opensource.org/licenses/BSD-3-Clause
  74. *
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f4xx_hal.h"
  79. /** @addtogroup STM32F4xx_HAL_Driver
  80. * @{
  81. */
  82. #ifdef HAL_I2S_MODULE_ENABLED
  83. /** @defgroup I2SEx I2SEx
  84. * @brief I2S Extended HAL module driver
  85. * @{
  86. */
  87. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  88. /* Private typedef -----------------------------------------------------------*/
  89. /** @defgroup I2SEx_Private_Typedef I2S Extended Private Typedef
  90. * @{
  91. */
  92. typedef enum
  93. {
  94. I2S_USE_I2S = 0x00U, /*!< I2Sx should be used */
  95. I2S_USE_I2SEXT = 0x01U, /*!< I2Sx_ext should be used */
  96. } I2S_UseTypeDef;
  97. /**
  98. * @}
  99. */
  100. /* Private define ------------------------------------------------------------*/
  101. /* Private macro -------------------------------------------------------------*/
  102. /* Private variables ---------------------------------------------------------*/
  103. /* Private function prototypes -----------------------------------------------*/
  104. /** @defgroup I2SEx_Private_Functions I2S Extended Private Functions
  105. * @{
  106. */
  107. static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma);
  108. static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma);
  109. static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma);
  110. static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s);
  111. static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s);
  112. static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s);
  113. static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s);
  114. static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag,
  115. uint32_t State, uint32_t Timeout, I2S_UseTypeDef i2sUsed);
  116. /**
  117. * @}
  118. */
  119. /**
  120. * @}
  121. */
  122. /* Private functions ---------------------------------------------------------*/
  123. /* Exported functions --------------------------------------------------------*/
  124. /** @addtogroup I2SEx I2SEx
  125. * @{
  126. */
  127. /** @addtogroup I2SEx_Exported_Functions I2S Extended Exported Functions
  128. * @{
  129. */
  130. /** @defgroup I2SEx_Exported_Functions_Group1 I2S Extended IO operation functions
  131. * @brief I2SEx IO operation functions
  132. *
  133. @verbatim
  134. ===============================================================================
  135. ##### IO operation functions#####
  136. ===============================================================================
  137. [..]
  138. This subsection provides a set of functions allowing to manage the I2S data
  139. transfers.
  140. (#) There are two modes of transfer:
  141. (++) Blocking mode : The communication is performed in the polling mode.
  142. The status of all data processing is returned by the same function
  143. after finishing transfer.
  144. (++) No-Blocking mode : The communication is performed using Interrupts
  145. or DMA. These functions return the status of the transfer startup.
  146. The end of the data processing will be indicated through the
  147. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  148. using DMA mode.
  149. (#) Blocking mode functions are :
  150. (++) HAL_I2SEx_TransmitReceive()
  151. (#) No-Blocking mode functions with Interrupt are :
  152. (++) HAL_I2SEx_TransmitReceive_IT()
  153. (++) HAL_I2SEx_FullDuplex_IRQHandler()
  154. (#) No-Blocking mode functions with DMA are :
  155. (++) HAL_I2SEx_TransmitReceive_DMA()
  156. (#) A set of Transfer Complete Callback are provided in non Blocking mode:
  157. (++) HAL_I2SEx_TxRxCpltCallback()
  158. @endverbatim
  159. * @{
  160. */
  161. /**
  162. * @brief Full-Duplex Transmit/Receive data in blocking mode.
  163. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  164. * the configuration information for I2S module
  165. * @param pTxData a 16-bit pointer to the Transmit data buffer.
  166. * @param pRxData a 16-bit pointer to the Receive data buffer.
  167. * @param Size number of data sample to be sent:
  168. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  169. * configuration phase, the Size parameter means the number of 16-bit data length
  170. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  171. * the Size parameter means the number of 16-bit data length.
  172. * @param Timeout Timeout duration
  173. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  174. * between Master and Slave(example: audio streaming).
  175. * @retval HAL status
  176. */
  177. HAL_StatusTypeDef HAL_I2SEx_TransmitReceive(I2S_HandleTypeDef *hi2s, uint16_t *pTxData, uint16_t *pRxData,
  178. uint16_t Size, uint32_t Timeout)
  179. {
  180. uint32_t tmp1 = 0U;
  181. HAL_StatusTypeDef errorcode = HAL_OK;
  182. if (hi2s->State != HAL_I2S_STATE_READY)
  183. {
  184. errorcode = HAL_BUSY;
  185. goto error;
  186. }
  187. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  188. {
  189. return HAL_ERROR;
  190. }
  191. /* Process Locked */
  192. __HAL_LOCK(hi2s);
  193. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  194. /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
  195. is selected during the I2S configuration phase, the Size parameter means the number
  196. of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
  197. frame is selected the Size parameter means the number of 16-bit data length. */
  198. if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  199. {
  200. hi2s->TxXferSize = (Size << 1U);
  201. hi2s->TxXferCount = (Size << 1U);
  202. hi2s->RxXferSize = (Size << 1U);
  203. hi2s->RxXferCount = (Size << 1U);
  204. }
  205. else
  206. {
  207. hi2s->TxXferSize = Size;
  208. hi2s->TxXferCount = Size;
  209. hi2s->RxXferSize = Size;
  210. hi2s->RxXferCount = Size;
  211. }
  212. /* Set state and reset error code */
  213. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  214. hi2s->State = HAL_I2S_STATE_BUSY_TX_RX;
  215. tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
  216. /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
  217. if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
  218. {
  219. /* Prepare the First Data before enabling the I2S */
  220. hi2s->Instance->DR = (*pTxData++);
  221. hi2s->TxXferCount--;
  222. /* Enable I2Sext(receiver) before enabling I2Sx peripheral */
  223. __HAL_I2SEXT_ENABLE(hi2s);
  224. /* Enable I2Sx peripheral */
  225. __HAL_I2S_ENABLE(hi2s);
  226. /* Check if Master Receiver mode is selected */
  227. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX)
  228. {
  229. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  230. access to the SPI_SR register. */
  231. __HAL_I2SEXT_CLEAR_OVRFLAG(hi2s);
  232. }
  233. while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
  234. {
  235. if (hi2s->TxXferCount > 0U)
  236. {
  237. /* Wait until TXE flag is set */
  238. if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
  239. {
  240. /* Set the error code */
  241. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  242. errorcode = HAL_ERROR;
  243. goto error;
  244. }
  245. /* Write Data on DR register */
  246. hi2s->Instance->DR = (*pTxData++);
  247. hi2s->TxXferCount--;
  248. /* Check if an underrun occurs */
  249. if ((__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_TX))
  250. {
  251. /* Clear Underrun flag */
  252. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  253. /* Set the error code */
  254. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  255. }
  256. }
  257. if (hi2s->RxXferCount > 0U)
  258. {
  259. /* Wait until RXNE flag is set */
  260. if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
  261. {
  262. /* Set the error code */
  263. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  264. errorcode = HAL_ERROR;
  265. goto error;
  266. }
  267. /* Read Data from DR register */
  268. (*pRxData++) = I2SxEXT(hi2s->Instance)->DR;
  269. hi2s->RxXferCount--;
  270. /* Check if an overrun occurs */
  271. if (__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  272. {
  273. /* Clear Overrun flag */
  274. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  275. /* Set the error code */
  276. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  277. }
  278. }
  279. }
  280. }
  281. /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
  282. else
  283. {
  284. /* Prepare the First Data before enabling the I2S */
  285. I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
  286. hi2s->TxXferCount--;
  287. /* Enable I2Sext(transmitter) after enabling I2Sx peripheral */
  288. __HAL_I2SEXT_ENABLE(hi2s);
  289. /* Enable I2S peripheral before the I2Sext*/
  290. __HAL_I2S_ENABLE(hi2s);
  291. /* Check if Master Receiver mode is selected */
  292. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  293. {
  294. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  295. access to the SPI_SR register. */
  296. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  297. }
  298. while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
  299. {
  300. if (hi2s->TxXferCount > 0U)
  301. {
  302. /* Wait until TXE flag is set */
  303. if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
  304. {
  305. /* Set the error code */
  306. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  307. errorcode = HAL_ERROR;
  308. goto error;
  309. }
  310. /* Write Data on DR register */
  311. I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
  312. hi2s->TxXferCount--;
  313. /* Check if an underrun occurs */
  314. if ((__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_RX))
  315. {
  316. /* Clear Underrun flag */
  317. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  318. /* Set the error code */
  319. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  320. }
  321. }
  322. if (hi2s->RxXferCount > 0U)
  323. {
  324. /* Wait until RXNE flag is set */
  325. if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
  326. {
  327. /* Set the error code */
  328. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  329. errorcode = HAL_ERROR;
  330. goto error;
  331. }
  332. /* Read Data from DR register */
  333. (*pRxData++) = hi2s->Instance->DR;
  334. hi2s->RxXferCount--;
  335. /* Check if an overrun occurs */
  336. if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  337. {
  338. /* Clear Overrun flag */
  339. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  340. /* Set the error code */
  341. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  342. }
  343. }
  344. }
  345. }
  346. if (hi2s->ErrorCode != HAL_I2S_ERROR_NONE)
  347. {
  348. errorcode = HAL_ERROR;
  349. }
  350. error :
  351. hi2s->State = HAL_I2S_STATE_READY;
  352. __HAL_UNLOCK(hi2s);
  353. return errorcode;
  354. }
  355. /**
  356. * @brief Full-Duplex Transmit/Receive data in non-blocking mode using Interrupt
  357. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  358. * the configuration information for I2S module
  359. * @param pTxData a 16-bit pointer to the Transmit data buffer.
  360. * @param pRxData a 16-bit pointer to the Receive data buffer.
  361. * @param Size number of data sample to be sent:
  362. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  363. * configuration phase, the Size parameter means the number of 16-bit data length
  364. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  365. * the Size parameter means the number of 16-bit data length.
  366. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  367. * between Master and Slave(example: audio streaming).
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pTxData, uint16_t *pRxData,
  371. uint16_t Size)
  372. {
  373. uint32_t tmp1 = 0U;
  374. HAL_StatusTypeDef errorcode = HAL_OK;
  375. if (hi2s->State != HAL_I2S_STATE_READY)
  376. {
  377. errorcode = HAL_BUSY;
  378. goto error;
  379. }
  380. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  381. {
  382. return HAL_ERROR;
  383. }
  384. /* Process Locked */
  385. __HAL_LOCK(hi2s);
  386. hi2s->pTxBuffPtr = pTxData;
  387. hi2s->pRxBuffPtr = pRxData;
  388. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  389. /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
  390. is selected during the I2S configuration phase, the Size parameter means the number
  391. of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
  392. frame is selected the Size parameter means the number of 16-bit data length. */
  393. if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  394. {
  395. hi2s->TxXferSize = (Size << 1U);
  396. hi2s->TxXferCount = (Size << 1U);
  397. hi2s->RxXferSize = (Size << 1U);
  398. hi2s->RxXferCount = (Size << 1U);
  399. }
  400. else
  401. {
  402. hi2s->TxXferSize = Size;
  403. hi2s->TxXferCount = Size;
  404. hi2s->RxXferSize = Size;
  405. hi2s->RxXferCount = Size;
  406. }
  407. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  408. hi2s->State = HAL_I2S_STATE_BUSY_TX_RX;
  409. /* Set the function for IT treatment */
  410. if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
  411. {
  412. /* Enable I2Sext RXNE and ERR interrupts */
  413. __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  414. /* Enable I2Sx TXE and ERR interrupts */
  415. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  416. /* Transmit First data */
  417. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  418. hi2s->TxXferCount--;
  419. if (hi2s->TxXferCount == 0U)
  420. {
  421. /* Disable TXE and ERR interrupt */
  422. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  423. }
  424. }
  425. else /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
  426. {
  427. /* Enable I2Sext TXE and ERR interrupts */
  428. __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  429. /* Enable I2Sext RXNE and ERR interrupts */
  430. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  431. /* Transmit First data */
  432. I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
  433. hi2s->TxXferCount--;
  434. if (hi2s->TxXferCount == 0U)
  435. {
  436. /* Disable I2Sext TXE and ERR interrupt */
  437. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  438. }
  439. }
  440. /* Enable I2Sext peripheral */
  441. __HAL_I2SEXT_ENABLE(hi2s);
  442. /* Enable I2S peripheral */
  443. __HAL_I2S_ENABLE(hi2s);
  444. error :
  445. __HAL_UNLOCK(hi2s);
  446. return errorcode;
  447. }
  448. /**
  449. * @brief Full-Duplex Transmit/Receive data in non-blocking mode using DMA
  450. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  451. * the configuration information for I2S module
  452. * @param pTxData a 16-bit pointer to the Transmit data buffer.
  453. * @param pRxData a 16-bit pointer to the Receive data buffer.
  454. * @param Size number of data sample to be sent:
  455. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  456. * configuration phase, the Size parameter means the number of 16-bit data length
  457. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  458. * the Size parameter means the number of 16-bit data length.
  459. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  460. * between Master and Slave(example: audio streaming).
  461. * @retval HAL status
  462. */
  463. HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pTxData, uint16_t *pRxData,
  464. uint16_t Size)
  465. {
  466. uint32_t *tmp = NULL;
  467. uint32_t tmp1 = 0U;
  468. HAL_StatusTypeDef errorcode = HAL_OK;
  469. if (hi2s->State != HAL_I2S_STATE_READY)
  470. {
  471. errorcode = HAL_BUSY;
  472. goto error;
  473. }
  474. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  475. {
  476. return HAL_ERROR;
  477. }
  478. /* Process Locked */
  479. __HAL_LOCK(hi2s);
  480. hi2s->pTxBuffPtr = pTxData;
  481. hi2s->pRxBuffPtr = pRxData;
  482. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  483. /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
  484. is selected during the I2S configuration phase, the Size parameter means the number
  485. of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
  486. frame is selected the Size parameter means the number of 16-bit data length. */
  487. if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  488. {
  489. hi2s->TxXferSize = (Size << 1U);
  490. hi2s->TxXferCount = (Size << 1U);
  491. hi2s->RxXferSize = (Size << 1U);
  492. hi2s->RxXferCount = (Size << 1U);
  493. }
  494. else
  495. {
  496. hi2s->TxXferSize = Size;
  497. hi2s->TxXferCount = Size;
  498. hi2s->RxXferSize = Size;
  499. hi2s->RxXferCount = Size;
  500. }
  501. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  502. hi2s->State = HAL_I2S_STATE_BUSY_TX_RX;
  503. /* Set the I2S Rx DMA Half transfer complete callback */
  504. hi2s->hdmarx->XferHalfCpltCallback = I2SEx_TxRxDMAHalfCplt;
  505. /* Set the I2S Rx DMA transfer complete callback */
  506. hi2s->hdmarx->XferCpltCallback = I2SEx_TxRxDMACplt;
  507. /* Set the I2S Rx DMA error callback */
  508. hi2s->hdmarx->XferErrorCallback = I2SEx_TxRxDMAError;
  509. /* Set the I2S Tx DMA Half transfer complete callback */
  510. hi2s->hdmatx->XferHalfCpltCallback = I2SEx_TxRxDMAHalfCplt;
  511. /* Set the I2S Tx DMA transfer complete callback */
  512. hi2s->hdmatx->XferCpltCallback = I2SEx_TxRxDMACplt;
  513. /* Set the I2S Tx DMA error callback */
  514. hi2s->hdmatx->XferErrorCallback = I2SEx_TxRxDMAError;
  515. tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
  516. /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
  517. if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
  518. {
  519. /* Enable the Rx DMA Stream */
  520. tmp = (uint32_t *)&pRxData;
  521. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
  522. /* Enable Rx DMA Request */
  523. SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
  524. /* Enable the Tx DMA Stream */
  525. tmp = (uint32_t *)&pTxData;
  526. HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
  527. /* Enable Tx DMA Request */
  528. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  529. /* Check if the I2S is already enabled */
  530. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  531. {
  532. /* Enable I2Sext(receiver) before enabling I2Sx peripheral */
  533. __HAL_I2SEXT_ENABLE(hi2s);
  534. /* Enable I2S peripheral after the I2Sext */
  535. __HAL_I2S_ENABLE(hi2s);
  536. }
  537. }
  538. else
  539. {
  540. /* Check if Master Receiver mode is selected */
  541. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  542. {
  543. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  544. access to the SPI_SR register. */
  545. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  546. }
  547. /* Enable the Tx DMA Stream */
  548. tmp = (uint32_t *)&pTxData;
  549. HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, hi2s->TxXferSize);
  550. /* Enable Tx DMA Request */
  551. SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
  552. /* Enable the Rx DMA Stream */
  553. tmp = (uint32_t *)&pRxData;
  554. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
  555. /* Enable Rx DMA Request */
  556. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  557. /* Check if the I2S is already enabled */
  558. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  559. {
  560. /* Enable I2Sext(transmitter) before enabling I2Sx peripheral */
  561. __HAL_I2SEXT_ENABLE(hi2s);
  562. /* Enable I2S peripheral before the I2Sext */
  563. __HAL_I2S_ENABLE(hi2s);
  564. }
  565. }
  566. error :
  567. __HAL_UNLOCK(hi2s);
  568. return errorcode;
  569. }
  570. /**
  571. * @brief This function handles I2S/I2Sext interrupt requests in full-duplex mode.
  572. * @param hi2s I2S handle
  573. * @retval HAL status
  574. */
  575. void HAL_I2SEx_FullDuplex_IRQHandler(I2S_HandleTypeDef *hi2s)
  576. {
  577. __IO uint32_t i2ssr = hi2s->Instance->SR;
  578. __IO uint32_t i2sextsr = I2SxEXT(hi2s->Instance)->SR;
  579. __IO uint32_t i2scr2 = hi2s->Instance->CR2;
  580. __IO uint32_t i2sextcr2 = I2SxEXT(hi2s->Instance)->CR2;
  581. /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
  582. if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
  583. {
  584. /* I2S in mode Transmitter -------------------------------------------------*/
  585. if (((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2scr2 & I2S_IT_TXE) != RESET))
  586. {
  587. /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
  588. the I2S TXE interrupt will be generated to manage the full-duplex transmit phase. */
  589. I2SEx_TxISR_I2S(hi2s);
  590. }
  591. /* I2Sext in mode Receiver -----------------------------------------------*/
  592. if (((i2sextsr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2sextcr2 & I2S_IT_RXNE) != RESET))
  593. {
  594. /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
  595. the I2Sext RXNE interrupt will be generated to manage the full-duplex receive phase. */
  596. I2SEx_RxISR_I2SExt(hi2s);
  597. }
  598. /* I2Sext Overrun error interrupt occurred --------------------------------*/
  599. if (((i2sextsr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
  600. {
  601. /* Disable RXNE and ERR interrupt */
  602. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  603. /* Disable TXE and ERR interrupt */
  604. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  605. /* Clear Overrun flag */
  606. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  607. /* Set the I2S State ready */
  608. hi2s->State = HAL_I2S_STATE_READY;
  609. /* Set the error code and execute error callback*/
  610. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  611. /* Call user error callback */
  612. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  613. hi2s->ErrorCallback(hi2s);
  614. #else
  615. HAL_I2S_ErrorCallback(hi2s);
  616. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  617. }
  618. /* I2S Underrun error interrupt occurred ----------------------------------*/
  619. if (((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2scr2 & I2S_IT_ERR) != RESET))
  620. {
  621. /* Disable TXE and ERR interrupt */
  622. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  623. /* Disable RXNE and ERR interrupt */
  624. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  625. /* Clear underrun flag */
  626. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  627. /* Set the I2S State ready */
  628. hi2s->State = HAL_I2S_STATE_READY;
  629. /* Set the error code and execute error callback*/
  630. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  631. /* Call user error callback */
  632. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  633. hi2s->ErrorCallback(hi2s);
  634. #else
  635. HAL_I2S_ErrorCallback(hi2s);
  636. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  637. }
  638. }
  639. /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
  640. else
  641. {
  642. /* I2Sext in mode Transmitter ----------------------------------------------*/
  643. if (((i2sextsr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2sextcr2 & I2S_IT_TXE) != RESET))
  644. {
  645. /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
  646. the I2Sext TXE interrupt will be generated to manage the full-duplex transmit phase. */
  647. I2SEx_TxISR_I2SExt(hi2s);
  648. }
  649. /* I2S in mode Receiver --------------------------------------------------*/
  650. if (((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2scr2 & I2S_IT_RXNE) != RESET))
  651. {
  652. /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
  653. the I2S RXNE interrupt will be generated to manage the full-duplex receive phase. */
  654. I2SEx_RxISR_I2S(hi2s);
  655. }
  656. /* I2S Overrun error interrupt occurred -------------------------------------*/
  657. if (((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2scr2 & I2S_IT_ERR) != RESET))
  658. {
  659. /* Disable RXNE and ERR interrupt */
  660. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  661. /* Disable TXE and ERR interrupt */
  662. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  663. /* Set the I2S State ready */
  664. hi2s->State = HAL_I2S_STATE_READY;
  665. /* Set the error code and execute error callback*/
  666. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  667. /* Call user error callback */
  668. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  669. hi2s->ErrorCallback(hi2s);
  670. #else
  671. HAL_I2S_ErrorCallback(hi2s);
  672. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  673. }
  674. /* I2Sext Underrun error interrupt occurred -------------------------------*/
  675. if (((i2sextsr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
  676. {
  677. /* Disable TXE and ERR interrupt */
  678. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  679. /* Disable RXNE and ERR interrupt */
  680. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  681. /* Set the I2S State ready */
  682. hi2s->State = HAL_I2S_STATE_READY;
  683. /* Set the error code and execute error callback*/
  684. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  685. /* Call user error callback */
  686. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  687. hi2s->ErrorCallback(hi2s);
  688. #else
  689. HAL_I2S_ErrorCallback(hi2s);
  690. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  691. }
  692. }
  693. }
  694. /**
  695. * @brief Tx and Rx Transfer half completed callback
  696. * @param hi2s I2S handle
  697. * @retval None
  698. */
  699. __weak void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  700. {
  701. /* Prevent unused argument(s) compilation warning */
  702. UNUSED(hi2s);
  703. /* NOTE : This function Should not be modified, when the callback is needed,
  704. the HAL_I2SEx_TxRxHalfCpltCallback could be implemented in the user file
  705. */
  706. }
  707. /**
  708. * @brief Tx and Rx Transfer completed callback
  709. * @param hi2s I2S handle
  710. * @retval None
  711. */
  712. __weak void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef *hi2s)
  713. {
  714. /* Prevent unused argument(s) compilation warning */
  715. UNUSED(hi2s);
  716. /* NOTE : This function should not be modified, when the callback is needed,
  717. the HAL_I2SEx_TxRxCpltCallback could be implemented in the user file
  718. */
  719. }
  720. /**
  721. * @}
  722. */
  723. /**
  724. * @}
  725. */
  726. /** @addtogroup I2SEx_Private_Functions I2S Extended Private Functions
  727. * @{
  728. */
  729. /**
  730. * @brief DMA I2S transmit receive process half complete callback
  731. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  732. * the configuration information for the specified DMA module.
  733. * @retval None
  734. */
  735. static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma)
  736. {
  737. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  738. /* Call user TxRx Half complete callback */
  739. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  740. hi2s->TxRxHalfCpltCallback(hi2s);
  741. #else
  742. HAL_I2SEx_TxRxHalfCpltCallback(hi2s);
  743. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  744. }
  745. /**
  746. * @brief DMA I2S transmit receive process complete callback
  747. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  748. * the configuration information for the specified DMA module.
  749. * @retval None
  750. */
  751. static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma)
  752. {
  753. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  754. /* if DMA is configured in DMA_NORMAL mode */
  755. if (hdma->Init.Mode == DMA_NORMAL)
  756. {
  757. if (hi2s->hdmarx == hdma)
  758. {
  759. /* Disable Rx DMA Request */
  760. if (((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX) || \
  761. ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX))
  762. {
  763. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
  764. }
  765. else
  766. {
  767. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  768. }
  769. hi2s->RxXferCount = 0U;
  770. if (hi2s->TxXferCount == 0U)
  771. {
  772. hi2s->State = HAL_I2S_STATE_READY;
  773. /* Call user TxRx complete callback */
  774. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  775. hi2s->TxRxCpltCallback(hi2s);
  776. #else
  777. HAL_I2SEx_TxRxCpltCallback(hi2s);
  778. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  779. }
  780. }
  781. if (hi2s->hdmatx == hdma)
  782. {
  783. /* Disable Tx DMA Request */
  784. if (((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX) || \
  785. ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX))
  786. {
  787. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  788. }
  789. else
  790. {
  791. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
  792. }
  793. hi2s->TxXferCount = 0U;
  794. if (hi2s->RxXferCount == 0U)
  795. {
  796. hi2s->State = HAL_I2S_STATE_READY;
  797. /* Call user TxRx complete callback */
  798. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  799. hi2s->TxRxCpltCallback(hi2s);
  800. #else
  801. HAL_I2SEx_TxRxCpltCallback(hi2s);
  802. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  803. }
  804. }
  805. }
  806. }
  807. /**
  808. * @brief DMA I2S communication error callback
  809. * @param hdma DMA handle
  810. * @retval None
  811. */
  812. static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma)
  813. {
  814. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  815. /* Disable Rx and Tx DMA Request */
  816. CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  817. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  818. hi2s->TxXferCount = 0U;
  819. hi2s->RxXferCount = 0U;
  820. hi2s->State = HAL_I2S_STATE_READY;
  821. /* Set the error code and execute error callback*/
  822. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  823. /* Call user error callback */
  824. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  825. hi2s->ErrorCallback(hi2s);
  826. #else
  827. HAL_I2S_ErrorCallback(hi2s);
  828. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  829. }
  830. /**
  831. * @brief I2S Full-Duplex IT handler transmit function
  832. * @param hi2s I2S handle
  833. * @retval None
  834. */
  835. static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s)
  836. {
  837. /* Write Data on DR register */
  838. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  839. hi2s->TxXferCount--;
  840. if (hi2s->TxXferCount == 0U)
  841. {
  842. /* Disable TXE and ERR interrupt */
  843. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  844. if (hi2s->RxXferCount == 0U)
  845. {
  846. hi2s->State = HAL_I2S_STATE_READY;
  847. /* Call user TxRx complete callback */
  848. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  849. hi2s->TxRxCpltCallback(hi2s);
  850. #else
  851. HAL_I2SEx_TxRxCpltCallback(hi2s);
  852. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  853. }
  854. }
  855. }
  856. /**
  857. * @brief I2SExt Full-Duplex IT handler transmit function
  858. * @param hi2s I2S handle
  859. * @retval None
  860. */
  861. static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s)
  862. {
  863. /* Write Data on DR register */
  864. I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
  865. hi2s->TxXferCount--;
  866. if (hi2s->TxXferCount == 0U)
  867. {
  868. /* Disable I2Sext TXE and ERR interrupt */
  869. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  870. if (hi2s->RxXferCount == 0U)
  871. {
  872. hi2s->State = HAL_I2S_STATE_READY;
  873. /* Call user TxRx complete callback */
  874. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  875. hi2s->TxRxCpltCallback(hi2s);
  876. #else
  877. HAL_I2SEx_TxRxCpltCallback(hi2s);
  878. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  879. }
  880. }
  881. }
  882. /**
  883. * @brief I2S Full-Duplex IT handler receive function
  884. * @param hi2s I2S handle
  885. * @retval None
  886. */
  887. static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s)
  888. {
  889. /* Read Data from DR register */
  890. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  891. hi2s->RxXferCount--;
  892. if (hi2s->RxXferCount == 0U)
  893. {
  894. /* Disable RXNE and ERR interrupt */
  895. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  896. if (hi2s->TxXferCount == 0U)
  897. {
  898. hi2s->State = HAL_I2S_STATE_READY;
  899. /* Call user TxRx complete callback */
  900. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  901. hi2s->TxRxCpltCallback(hi2s);
  902. #else
  903. HAL_I2SEx_TxRxCpltCallback(hi2s);
  904. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  905. }
  906. }
  907. }
  908. /**
  909. * @brief I2SExt Full-Duplex IT handler receive function
  910. * @param hi2s I2S handle
  911. * @retval None
  912. */
  913. static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s)
  914. {
  915. /* Read Data from DR register */
  916. (*hi2s->pRxBuffPtr++) = I2SxEXT(hi2s->Instance)->DR;
  917. hi2s->RxXferCount--;
  918. if (hi2s->RxXferCount == 0U)
  919. {
  920. /* Disable I2Sext RXNE and ERR interrupt */
  921. __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  922. if (hi2s->TxXferCount == 0U)
  923. {
  924. hi2s->State = HAL_I2S_STATE_READY;
  925. /* Call user TxRx complete callback */
  926. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  927. hi2s->TxRxCpltCallback(hi2s);
  928. #else
  929. HAL_I2SEx_TxRxCpltCallback(hi2s);
  930. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  931. }
  932. }
  933. }
  934. /**
  935. * @brief This function handles I2S Communication Timeout.
  936. * @param hi2s I2S handle
  937. * @param Flag Flag checked
  938. * @param State Value of the flag expected
  939. * @param Timeout Duration of the timeout
  940. * @param i2sUsed I2S instance reference
  941. * @retval HAL status
  942. */
  943. static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag,
  944. uint32_t State, uint32_t Timeout, I2S_UseTypeDef i2sUsed)
  945. {
  946. uint32_t tickstart = HAL_GetTick();
  947. if (i2sUsed == I2S_USE_I2S)
  948. {
  949. /* Wait until flag is reset */
  950. while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  951. {
  952. if (Timeout != HAL_MAX_DELAY)
  953. {
  954. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  955. {
  956. /* Set the I2S State ready */
  957. hi2s->State = HAL_I2S_STATE_READY;
  958. /* Process Unlocked */
  959. __HAL_UNLOCK(hi2s);
  960. return HAL_TIMEOUT;
  961. }
  962. }
  963. }
  964. }
  965. else /* i2sUsed == I2S_USE_I2SEXT */
  966. {
  967. /* Wait until flag is reset */
  968. while (((__HAL_I2SEXT_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  969. {
  970. if (Timeout != HAL_MAX_DELAY)
  971. {
  972. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  973. {
  974. /* Set the I2S State ready */
  975. hi2s->State = HAL_I2S_STATE_READY;
  976. /* Process Unlocked */
  977. __HAL_UNLOCK(hi2s);
  978. return HAL_TIMEOUT;
  979. }
  980. }
  981. }
  982. }
  983. return HAL_OK;
  984. }
  985. /**
  986. * @}
  987. */
  988. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  989. /**
  990. * @}
  991. */
  992. #endif /* HAL_I2S_MODULE_ENABLED */
  993. /**
  994. * @}
  995. */
  996. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/