You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1152 lines
39 KiB

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