Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

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