stm32f4xx_hal_i2s_ex.c 41 KB

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