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.
 
 
 

1542 lines
50 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_i2s.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 the Integrated Interchip Sound (I2S) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral State and Errors functions
  13. @verbatim
  14. ===============================================================================
  15. ##### How to use this driver #####
  16. ===============================================================================
  17. [..]
  18. The I2S HAL driver can be used as follow:
  19. (#) Declare a I2S_HandleTypeDef handle structure.
  20. (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:
  21. (##) Enable the SPIx interface clock.
  22. (##) I2S pins configuration:
  23. (+++) Enable the clock for the I2S GPIOs.
  24. (+++) Configure these I2S pins as alternate function pull-up.
  25. (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()
  26. and HAL_I2S_Receive_IT() APIs).
  27. (+++) Configure the I2Sx interrupt priority.
  28. (+++) Enable the NVIC I2S IRQ handle.
  29. (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()
  30. and HAL_I2S_Receive_DMA() APIs:
  31. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  32. (+++) Enable the DMAx interface clock.
  33. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  34. (+++) Configure the DMA Tx/Rx Stream.
  35. (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle.
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  37. DMA Tx/Rx Stream.
  38. (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
  39. using HAL_I2S_Init() function.
  40. -@- The specific I2S interrupts (Transmission complete interrupt,
  41. RXNE interrupt and Error Interrupts) will be managed using the macros
  42. __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
  43. -@- Make sure that either:
  44. (+@) I2S PLL is configured or
  45. (+@) External clock source is configured after setting correctly
  46. the define constant EXTERNAL_CLOCK_VALUE in the stm32f4xx_hal_conf.h file.
  47. (#) Three operation modes are available within this driver :
  48. *** Polling mode IO operation ***
  49. =================================
  50. [..]
  51. (+) Send an amount of data in blocking mode using HAL_I2S_Transmit()
  52. (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
  53. *** Interrupt mode IO operation ***
  54. ===================================
  55. [..]
  56. (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT()
  57. (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
  58. add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
  59. (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_I2S_TxCpltCallback
  61. (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT()
  62. (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
  63. add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
  64. (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
  65. add his own code by customization of function pointer HAL_I2S_RxCpltCallback
  66. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  67. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  68. *** DMA mode IO operation ***
  69. ==============================
  70. [..]
  71. (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA()
  72. (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
  73. add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
  74. (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
  75. add his own code by customization of function pointer HAL_I2S_TxCpltCallback
  76. (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA()
  77. (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
  78. add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
  79. (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
  80. add his own code by customization of function pointer HAL_I2S_RxCpltCallback
  81. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  82. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  83. (+) Pause the DMA Transfer using HAL_I2S_DMAPause()
  84. (+) Resume the DMA Transfer using HAL_I2S_DMAResume()
  85. (+) Stop the DMA Transfer using HAL_I2S_DMAStop()
  86. *** I2S HAL driver macros list ***
  87. =============================================
  88. [..]
  89. Below the list of most used macros in I2S HAL driver.
  90. (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode)
  91. (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)
  92. (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
  93. (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
  94. (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
  95. [..]
  96. (@) You can refer to the I2S HAL driver header file for more useful macros
  97. @endverbatim
  98. ******************************************************************************
  99. * @attention
  100. *
  101. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  102. *
  103. * Redistribution and use in source and binary forms, with or without modification,
  104. * are permitted provided that the following conditions are met:
  105. * 1. Redistributions of source code must retain the above copyright notice,
  106. * this list of conditions and the following disclaimer.
  107. * 2. Redistributions in binary form must reproduce the above copyright notice,
  108. * this list of conditions and the following disclaimer in the documentation
  109. * and/or other materials provided with the distribution.
  110. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  111. * may be used to endorse or promote products derived from this software
  112. * without specific prior written permission.
  113. *
  114. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  115. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  116. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  117. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  118. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  119. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  120. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  121. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  122. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  123. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  124. *
  125. ******************************************************************************
  126. */
  127. /* Includes ------------------------------------------------------------------*/
  128. #include "stm32f4xx_hal.h"
  129. /** @addtogroup STM32F4xx_HAL_Driver
  130. * @{
  131. */
  132. #ifdef HAL_I2S_MODULE_ENABLED
  133. /** @defgroup I2S I2S
  134. * @brief I2S HAL module driver
  135. * @{
  136. */
  137. /* Private typedef -----------------------------------------------------------*/
  138. /* Private define ------------------------------------------------------------*/
  139. /* Private macro -------------------------------------------------------------*/
  140. /* Private variables ---------------------------------------------------------*/
  141. /* Private function prototypes -----------------------------------------------*/
  142. /** @addtogroup I2S_Private_Functions
  143. * @{
  144. */
  145. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
  146. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  147. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
  148. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  149. static void I2S_DMAError(DMA_HandleTypeDef *hdma);
  150. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
  151. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
  152. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s);
  153. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State,
  154. uint32_t Timeout);
  155. /**
  156. * @}
  157. */
  158. /* Exported functions --------------------------------------------------------*/
  159. /** @addtogroup I2S_Exported_Functions I2S Exported Functions
  160. * @{
  161. */
  162. /** @addtogroup I2S_Exported_Functions_Group1
  163. * @brief Initialization and Configuration functions
  164. *
  165. @verbatim
  166. ===============================================================================
  167. ##### Initialization and de-initialization functions #####
  168. ===============================================================================
  169. [..] This subsection provides a set of functions allowing to initialize and
  170. de-initialize the I2Sx peripheral in simplex mode:
  171. (+) User must Implement HAL_I2S_MspInit() function in which he configures
  172. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  173. (+) Call the function HAL_I2S_Init() to configure the selected device with
  174. the selected configuration:
  175. (++) Mode
  176. (++) Standard
  177. (++) Data Format
  178. (++) MCLK Output
  179. (++) Audio frequency
  180. (++) Polarity
  181. (++) Full duplex mode
  182. (+) Call the function HAL_I2S_DeInit() to restore the default configuration
  183. of the selected I2Sx peripheral.
  184. @endverbatim
  185. * @{
  186. */
  187. /**
  188. * @brief Initializes the I2S according to the specified parameters
  189. * in the I2S_InitTypeDef and create the associated handle.
  190. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  191. * the configuration information for I2S module
  192. * @retval HAL status
  193. */
  194. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  195. {
  196. uint32_t tmpreg = 0U, i2sdiv = 2U, i2sodd = 0U, packetlength = 16U;
  197. uint32_t tmp = 0U, i2sclk = 0U;
  198. /* Check the I2S handle allocation */
  199. if(hi2s == NULL)
  200. {
  201. return HAL_ERROR;
  202. }
  203. /* Check the I2S parameters */
  204. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  205. assert_param(IS_I2S_MODE(hi2s->Init.Mode));
  206. assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
  207. assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
  208. assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
  209. assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
  210. assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
  211. assert_param(IS_I2S_CLOCKSOURCE(hi2s->Init.ClockSource));
  212. hi2s->State = HAL_I2S_STATE_BUSY;
  213. /* Initialize Default I2S IrqHandler ISR */
  214. hi2s->IrqHandlerISR = I2S_IRQHandler;
  215. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  216. HAL_I2S_MspInit(hi2s);
  217. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ---------------*/
  218. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  219. CLEAR_BIT(hi2s->Instance->I2SCFGR,(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  220. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  221. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD));
  222. hi2s->Instance->I2SPR = 0x0002U;
  223. /* Get the I2SCFGR register value */
  224. tmpreg = hi2s->Instance->I2SCFGR;
  225. /* If the default frequency value has to be written, reinitialize i2sdiv and i2sodd */
  226. /* If the requested audio frequency is not the default, compute the prescaler */
  227. if(hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT)
  228. {
  229. /* Check the frame length (For the Prescaler computing) *******************/
  230. /* Set I2S Packet Length value*/
  231. if(hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  232. {
  233. /* Packet length is 32 bits */
  234. packetlength = 32U;
  235. }
  236. else
  237. {
  238. /* Packet length is 16 bits */
  239. packetlength = 16U;
  240. }
  241. /* I2S standard */
  242. if(hi2s->Init.Standard <= I2S_STANDARD_LSB)
  243. {
  244. /* In I2S standard packet lenght is multiplied by 2 */
  245. packetlength = packetlength * 2U;
  246. }
  247. /* Get I2S source Clock frequency from RCC ********************************/
  248. #if defined(I2S_APB1_APB2_FEATURE)
  249. if(IS_I2S_APB1_INSTANCE(hi2s->Instance))
  250. {
  251. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB1);
  252. }
  253. else
  254. {
  255. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB2);
  256. }
  257. #else
  258. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S);
  259. #endif
  260. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  261. if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
  262. {
  263. /* MCLK output is enabled */
  264. if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  265. {
  266. tmp = (uint32_t)(((((i2sclk / (packetlength*4)) * 10) / hi2s->Init.AudioFreq)) + 5);
  267. }
  268. else
  269. {
  270. tmp = (uint32_t)(((((i2sclk / (packetlength*8)) * 10) / hi2s->Init.AudioFreq)) + 5);
  271. }
  272. }
  273. else
  274. {
  275. /* MCLK output is disabled */
  276. tmp = (uint32_t)(((((i2sclk / packetlength) *10 ) / hi2s->Init.AudioFreq)) + 5);
  277. }
  278. /* Remove the flatting point */
  279. tmp = tmp / 10U;
  280. /* Check the parity of the divider */
  281. i2sodd = (uint16_t)(tmp & (uint16_t)1U);
  282. /* Compute the i2sdiv prescaler */
  283. i2sdiv = (uint16_t)((tmp - i2sodd) / 2U);
  284. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  285. i2sodd = (uint32_t) (i2sodd << 8U);
  286. }
  287. /* Test if the divider is 1 or 0 or greater than 0xFF */
  288. if((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  289. {
  290. /* Set the default values */
  291. i2sdiv = 2U;
  292. i2sodd = 0U;
  293. /* Set the error code and execute error callback*/
  294. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER);
  295. HAL_I2S_ErrorCallback(hi2s);
  296. return HAL_ERROR;
  297. }
  298. /* Write to SPIx I2SPR register the computed value */
  299. hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
  300. /* Configure the I2S with the I2S_InitStruct values */
  301. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \
  302. (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
  303. (uint16_t)hi2s->Init.CPOL))));
  304. #if defined(SPI_I2SCFGR_ASTRTEN)
  305. if ((hi2s->Init.Standard == I2S_STANDARD_PCM_SHORT) ||(hi2s->Init.Standard == I2S_STANDARD_PCM_LONG))
  306. {
  307. /* Write to SPIx I2SCFGR */
  308. WRITE_REG(hi2s->Instance->I2SCFGR,(tmpreg | SPI_I2SCFGR_ASTRTEN));
  309. }
  310. else
  311. {
  312. /* Write to SPIx I2SCFGR */
  313. WRITE_REG(hi2s->Instance->I2SCFGR,tmpreg);
  314. }
  315. #else
  316. /* Write to SPIx I2SCFGR */
  317. WRITE_REG(hi2s->Instance->I2SCFGR, tmpreg);
  318. #endif
  319. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  320. /* Configure the I2S extended if the full duplex mode is enabled */
  321. assert_param(IS_I2S_FULLDUPLEX_MODE(hi2s->Init.FullDuplexMode));
  322. if(hi2s->Init.FullDuplexMode == I2S_FULLDUPLEXMODE_ENABLE)
  323. {
  324. /* Set FullDuplex I2S IrqHandler ISR if FULLDUPLEXMODE is enabled */
  325. hi2s->IrqHandlerISR = HAL_I2SEx_FullDuplex_IRQHandler;
  326. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  327. CLEAR_BIT(I2SxEXT(hi2s->Instance)->I2SCFGR,(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  328. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  329. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD));
  330. I2SxEXT(hi2s->Instance)->I2SPR = 2U;
  331. /* Get the I2SCFGR register value */
  332. tmpreg = I2SxEXT(hi2s->Instance)->I2SCFGR;
  333. /* Get the mode to be configured for the extended I2S */
  334. if((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
  335. {
  336. tmp = I2S_MODE_SLAVE_RX;
  337. }
  338. else /* I2S_MODE_MASTER_RX || I2S_MODE_SLAVE_RX */
  339. {
  340. tmp = I2S_MODE_SLAVE_TX;
  341. }
  342. /* Configure the I2S Slave with the I2S Master parameter values */
  343. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(tmp | \
  344. (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
  345. (uint16_t)hi2s->Init.CPOL))));
  346. /* Write to SPIx I2SCFGR */
  347. WRITE_REG(I2SxEXT(hi2s->Instance)->I2SCFGR,tmpreg);
  348. }
  349. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  350. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  351. hi2s->State = HAL_I2S_STATE_READY;
  352. return HAL_OK;
  353. }
  354. /**
  355. * @brief DeInitializes the I2S peripheral
  356. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  357. * the configuration information for I2S module
  358. * @retval HAL status
  359. */
  360. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  361. {
  362. /* Check the I2S handle allocation */
  363. if(hi2s == NULL)
  364. {
  365. return HAL_ERROR;
  366. }
  367. hi2s->State = HAL_I2S_STATE_BUSY;
  368. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  369. HAL_I2S_MspDeInit(hi2s);
  370. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  371. hi2s->State = HAL_I2S_STATE_RESET;
  372. /* Release Lock */
  373. __HAL_UNLOCK(hi2s);
  374. return HAL_OK;
  375. }
  376. /**
  377. * @brief I2S MSP Init
  378. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  379. * the configuration information for I2S module
  380. * @retval None
  381. */
  382. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  383. {
  384. /* Prevent unused argument(s) compilation warning */
  385. UNUSED(hi2s);
  386. /* NOTE : This function Should not be modified, when the callback is needed,
  387. the HAL_I2S_MspInit could be implemented in the user file
  388. */
  389. }
  390. /**
  391. * @brief I2S MSP DeInit
  392. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  393. * the configuration information for I2S module
  394. * @retval None
  395. */
  396. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  397. {
  398. /* Prevent unused argument(s) compilation warning */
  399. UNUSED(hi2s);
  400. /* NOTE : This function Should not be modified, when the callback is needed,
  401. the HAL_I2S_MspDeInit could be implemented in the user file
  402. */
  403. }
  404. /**
  405. * @}
  406. */
  407. /** @addtogroup I2S_Exported_Functions_Group2
  408. * @brief Data transfers functions
  409. *
  410. @verbatim
  411. ===============================================================================
  412. ##### IO operation functions #####
  413. ===============================================================================
  414. [..]
  415. This subsection provides a set of functions allowing to manage the I2S data
  416. transfers.
  417. (#) There are two modes of transfer:
  418. (++) Blocking mode : The communication is performed in the polling mode.
  419. The status of all data processing is returned by the same function
  420. after finishing transfer.
  421. (++) No-Blocking mode : The communication is performed using Interrupts
  422. or DMA. These functions return the status of the transfer startup.
  423. The end of the data processing will be indicated through the
  424. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  425. using DMA mode.
  426. (#) Blocking mode functions are :
  427. (++) HAL_I2S_Transmit()
  428. (++) HAL_I2S_Receive()
  429. (#) No-Blocking mode functions with Interrupt are :
  430. (++) HAL_I2S_Transmit_IT()
  431. (++) HAL_I2S_Receive_IT()
  432. (#) No-Blocking mode functions with DMA are :
  433. (++) HAL_I2S_Transmit_DMA()
  434. (++) HAL_I2S_Receive_DMA()
  435. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  436. (++) HAL_I2S_TxCpltCallback()
  437. (++) HAL_I2S_RxCpltCallback()
  438. (++) HAL_I2S_ErrorCallback()
  439. @endverbatim
  440. * @{
  441. */
  442. /**
  443. * @brief Transmit an amount of data in blocking mode
  444. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  445. * the configuration information for I2S module
  446. * @param pData: a 16-bit pointer to data buffer.
  447. * @param Size: number of data sample to be sent:
  448. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  449. * configuration phase, the Size parameter means the number of 16-bit data length
  450. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  451. * the Size parameter means the number of 16-bit data length.
  452. * @param Timeout: Timeout duration
  453. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  454. * between Master and Slave(example: audio streaming).
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  458. {
  459. uint32_t tmp1 = 0U;
  460. if((pData == NULL ) || (Size == 0U))
  461. {
  462. return HAL_ERROR;
  463. }
  464. if(hi2s->State == HAL_I2S_STATE_READY)
  465. {
  466. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  467. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  468. {
  469. hi2s->TxXferSize = (Size << 1U);
  470. hi2s->TxXferCount = (Size << 1U);
  471. }
  472. else
  473. {
  474. hi2s->TxXferSize = Size;
  475. hi2s->TxXferCount = Size;
  476. }
  477. /* Process Locked */
  478. __HAL_LOCK(hi2s);
  479. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  480. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  481. /* Check if the I2S is already enabled */
  482. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  483. {
  484. /* Enable I2S peripheral */
  485. __HAL_I2S_ENABLE(hi2s);
  486. }
  487. while(hi2s->TxXferCount > 0U)
  488. {
  489. hi2s->Instance->DR = (*pData++);
  490. hi2s->TxXferCount--;
  491. /* Wait until TXE flag is set */
  492. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  493. {
  494. /* Set the error code and execute error callback*/
  495. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  496. HAL_I2S_ErrorCallback(hi2s);
  497. return HAL_TIMEOUT;
  498. }
  499. /* Check if an underrun occurs */
  500. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
  501. {
  502. /* Clear underrun flag */
  503. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  504. /* Set the I2S State ready */
  505. hi2s->State = HAL_I2S_STATE_READY;
  506. /* Process Unlocked */
  507. __HAL_UNLOCK(hi2s);
  508. /* Set the error code and execute error callback*/
  509. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  510. HAL_I2S_ErrorCallback(hi2s);
  511. return HAL_ERROR;
  512. }
  513. }
  514. hi2s->State = HAL_I2S_STATE_READY;
  515. /* Process Unlocked */
  516. __HAL_UNLOCK(hi2s);
  517. return HAL_OK;
  518. }
  519. else
  520. {
  521. return HAL_BUSY;
  522. }
  523. }
  524. /**
  525. * @brief Receive an amount of data in blocking mode
  526. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  527. * the configuration information for I2S module
  528. * @param pData: a 16-bit pointer to data buffer
  529. * @param Size: number of data sample to be sent:
  530. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  531. * configuration phase, the Size parameter means the number of 16-bit data length
  532. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  533. * the Size parameter means the number of 16-bit data length.
  534. * @param Timeout: Timeout duration
  535. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  536. * between Master and Slave(example: audio streaming)
  537. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  538. * in continuous way and as the I2S is not disabled at the end of the I2S transaction
  539. * @retval HAL status
  540. */
  541. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  542. {
  543. uint32_t tmp1 = 0U;
  544. if((pData == NULL ) || (Size == 0U))
  545. {
  546. return HAL_ERROR;
  547. }
  548. if(hi2s->State == HAL_I2S_STATE_READY)
  549. {
  550. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  551. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  552. {
  553. hi2s->RxXferSize = (Size << 1U);
  554. hi2s->RxXferCount = (Size << 1U);
  555. }
  556. else
  557. {
  558. hi2s->RxXferSize = Size;
  559. hi2s->RxXferCount = Size;
  560. }
  561. /* Process Locked */
  562. __HAL_LOCK(hi2s);
  563. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  564. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  565. /* Check if the I2S is already enabled */
  566. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  567. {
  568. /* Enable I2S peripheral */
  569. __HAL_I2S_ENABLE(hi2s);
  570. }
  571. /* Check if Master Receiver mode is selected */
  572. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  573. {
  574. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  575. access to the SPI_SR register. */
  576. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  577. }
  578. /* Receive data */
  579. while(hi2s->RxXferCount > 0U)
  580. {
  581. /* Wait until RXNE flag is set */
  582. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
  583. {
  584. /* Set the error code and execute error callback*/
  585. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_TIMEOUT);
  586. HAL_I2S_ErrorCallback(hi2s);
  587. return HAL_TIMEOUT;
  588. }
  589. /* Check if an overrun occurs */
  590. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  591. {
  592. /* Clear overrun flag */
  593. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  594. /* Set the I2S State ready */
  595. hi2s->State = HAL_I2S_STATE_READY;
  596. /* Process Unlocked */
  597. __HAL_UNLOCK(hi2s);
  598. /* Set the error code and execute error callback*/
  599. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  600. HAL_I2S_ErrorCallback(hi2s);
  601. return HAL_ERROR;
  602. }
  603. (*pData++) = hi2s->Instance->DR;
  604. hi2s->RxXferCount--;
  605. }
  606. hi2s->State = HAL_I2S_STATE_READY;
  607. /* Process Unlocked */
  608. __HAL_UNLOCK(hi2s);
  609. return HAL_OK;
  610. }
  611. else
  612. {
  613. return HAL_BUSY;
  614. }
  615. }
  616. /**
  617. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  618. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  619. * the configuration information for I2S module
  620. * @param pData: a 16-bit pointer to data buffer.
  621. * @param Size: number of data sample to be sent:
  622. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  623. * configuration phase, the Size parameter means the number of 16-bit data length
  624. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  625. * the Size parameter means the number of 16-bit data length.
  626. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  627. * between Master and Slave(example: audio streaming).
  628. * @retval HAL status
  629. */
  630. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  631. {
  632. uint32_t tmp1 = 0U;
  633. if(hi2s->State == HAL_I2S_STATE_READY)
  634. {
  635. if((pData == NULL) || (Size == 0U))
  636. {
  637. return HAL_ERROR;
  638. }
  639. hi2s->pTxBuffPtr = pData;
  640. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  641. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  642. {
  643. hi2s->TxXferSize = (Size << 1U);
  644. hi2s->TxXferCount = (Size << 1U);
  645. }
  646. else
  647. {
  648. hi2s->TxXferSize = Size;
  649. hi2s->TxXferCount = Size;
  650. }
  651. /* Process Locked */
  652. __HAL_LOCK(hi2s);
  653. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  654. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  655. /* Enable TXE and ERR interrupt */
  656. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  657. /* Check if the I2S is already enabled */
  658. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  659. {
  660. /* Enable I2S peripheral */
  661. __HAL_I2S_ENABLE(hi2s);
  662. }
  663. /* Process Unlocked */
  664. __HAL_UNLOCK(hi2s);
  665. return HAL_OK;
  666. }
  667. else
  668. {
  669. return HAL_BUSY;
  670. }
  671. }
  672. /**
  673. * @brief Receive an amount of data in non-blocking mode with Interrupt
  674. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  675. * the configuration information for I2S module
  676. * @param pData: a 16-bit pointer to the Receive data buffer.
  677. * @param Size: number of data sample to be sent:
  678. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  679. * configuration phase, the Size parameter means the number of 16-bit data length
  680. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  681. * the Size parameter means the number of 16-bit data length.
  682. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  683. * between Master and Slave(example: audio streaming).
  684. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
  685. * between Master and Slave otherwise the I2S interrupt should be optimized.
  686. * @retval HAL status
  687. */
  688. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  689. {
  690. uint32_t tmp1 = 0U;
  691. if(hi2s->State == HAL_I2S_STATE_READY)
  692. {
  693. if((pData == NULL) || (Size == 0U))
  694. {
  695. return HAL_ERROR;
  696. }
  697. hi2s->pRxBuffPtr = pData;
  698. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  699. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  700. {
  701. hi2s->RxXferSize = (Size << 1U);
  702. hi2s->RxXferCount = (Size << 1U);
  703. }
  704. else
  705. {
  706. hi2s->RxXferSize = Size;
  707. hi2s->RxXferCount = Size;
  708. }
  709. /* Process Locked */
  710. __HAL_LOCK(hi2s);
  711. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  712. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  713. /* Enable TXE and ERR interrupt */
  714. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  715. /* Check if the I2S is already enabled */
  716. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  717. {
  718. /* Enable I2S peripheral */
  719. __HAL_I2S_ENABLE(hi2s);
  720. }
  721. /* Process Unlocked */
  722. __HAL_UNLOCK(hi2s);
  723. return HAL_OK;
  724. }
  725. else
  726. {
  727. return HAL_BUSY;
  728. }
  729. }
  730. /**
  731. * @brief Transmit an amount of data in non-blocking mode with DMA
  732. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  733. * the configuration information for I2S module
  734. * @param pData: a 16-bit pointer to the Transmit data buffer.
  735. * @param Size: number of data sample to be sent:
  736. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  737. * configuration phase, the Size parameter means the number of 16-bit data length
  738. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  739. * the Size parameter means the number of 16-bit data length.
  740. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  741. * between Master and Slave(example: audio streaming).
  742. * @retval HAL status
  743. */
  744. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  745. {
  746. uint32_t *tmp = NULL;
  747. uint32_t tmp1 = 0U;
  748. if((pData == NULL) || (Size == 0U))
  749. {
  750. return HAL_ERROR;
  751. }
  752. if(hi2s->State == HAL_I2S_STATE_READY)
  753. {
  754. hi2s->pTxBuffPtr = pData;
  755. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  756. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  757. {
  758. hi2s->TxXferSize = (Size << 1U);
  759. hi2s->TxXferCount = (Size << 1U);
  760. }
  761. else
  762. {
  763. hi2s->TxXferSize = Size;
  764. hi2s->TxXferCount = Size;
  765. }
  766. /* Process Locked */
  767. __HAL_LOCK(hi2s);
  768. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  769. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  770. /* Set the I2S Tx DMA Half transfer complete callback */
  771. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  772. /* Set the I2S Tx DMA transfer complete callback */
  773. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  774. /* Set the DMA error callback */
  775. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  776. /* Enable the Tx DMA Stream */
  777. tmp = (uint32_t*)&pData;
  778. HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
  779. /* Check if the I2S is already enabled */
  780. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  781. {
  782. /* Enable I2S peripheral */
  783. __HAL_I2S_ENABLE(hi2s);
  784. }
  785. /* Check if the I2S Tx request is already enabled */
  786. if((hi2s->Instance->CR2 & SPI_CR2_TXDMAEN) != SPI_CR2_TXDMAEN)
  787. {
  788. /* Enable Tx DMA Request */
  789. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  790. }
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hi2s);
  793. return HAL_OK;
  794. }
  795. else
  796. {
  797. return HAL_BUSY;
  798. }
  799. }
  800. /**
  801. * @brief Receive an amount of data in non-blocking mode with DMA
  802. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  803. * the configuration information for I2S module
  804. * @param pData: a 16-bit pointer to the Receive data buffer.
  805. * @param Size: number of data sample to be sent:
  806. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  807. * configuration phase, the Size parameter means the number of 16-bit data length
  808. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  809. * the Size parameter means the number of 16-bit data length.
  810. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  811. * between Master and Slave(example: audio streaming).
  812. * @retval HAL status
  813. */
  814. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  815. {
  816. uint32_t *tmp = NULL;
  817. uint32_t tmp1 = 0U;
  818. if((pData == NULL) || (Size == 0U))
  819. {
  820. return HAL_ERROR;
  821. }
  822. if(hi2s->State == HAL_I2S_STATE_READY)
  823. {
  824. hi2s->pRxBuffPtr = pData;
  825. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  826. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  827. {
  828. hi2s->RxXferSize = (Size << 1U);
  829. hi2s->RxXferCount = (Size << 1U);
  830. }
  831. else
  832. {
  833. hi2s->RxXferSize = Size;
  834. hi2s->RxXferCount = Size;
  835. }
  836. /* Process Locked */
  837. __HAL_LOCK(hi2s);
  838. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  839. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  840. /* Set the I2S Rx DMA Half transfer complete callback */
  841. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  842. /* Set the I2S Rx DMA transfer complete callback */
  843. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  844. /* Set the DMA error callback */
  845. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  846. /* Check if Master Receiver mode is selected */
  847. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  848. {
  849. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  850. access to the SPI_SR register. */
  851. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  852. }
  853. /* Enable the Rx DMA Stream */
  854. tmp = (uint32_t*)&pData;
  855. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);
  856. /* Check if the I2S is already enabled */
  857. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  858. {
  859. /* Enable I2S peripheral */
  860. __HAL_I2S_ENABLE(hi2s);
  861. }
  862. /* Check if the I2S Rx request is already enabled */
  863. if((hi2s->Instance->CR2 &SPI_CR2_RXDMAEN) != SPI_CR2_RXDMAEN)
  864. {
  865. /* Enable Rx DMA Request */
  866. SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  867. }
  868. /* Process Unlocked */
  869. __HAL_UNLOCK(hi2s);
  870. return HAL_OK;
  871. }
  872. else
  873. {
  874. return HAL_BUSY;
  875. }
  876. }
  877. /**
  878. * @brief Pauses the audio stream playing from the Media.
  879. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  880. * the configuration information for I2S module
  881. * @retval HAL status
  882. */
  883. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  884. {
  885. /* Process Locked */
  886. __HAL_LOCK(hi2s);
  887. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  888. {
  889. /* Disable the I2S DMA Tx request */
  890. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  891. }
  892. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  893. {
  894. /* Disable the I2S DMA Rx request */
  895. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  896. }
  897. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  898. else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  899. {
  900. /* Pause the audio file playing by disabling the I2S DMA request */
  901. CLEAR_BIT(hi2s->Instance->CR2,(SPI_CR2_TXDMAEN|SPI_CR2_RXDMAEN));
  902. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2,(SPI_CR2_TXDMAEN|SPI_CR2_RXDMAEN));
  903. }
  904. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  905. /* Process Unlocked */
  906. __HAL_UNLOCK(hi2s);
  907. return HAL_OK;
  908. }
  909. /**
  910. * @brief Resumes the audio stream playing from the Media.
  911. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  912. * the configuration information for I2S module
  913. * @retval HAL status
  914. */
  915. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  916. {
  917. /* Process Locked */
  918. __HAL_LOCK(hi2s);
  919. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  920. {
  921. /* Enable the I2S DMA Tx request */
  922. SET_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  923. }
  924. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  925. {
  926. /* Enable the I2S DMA Rx request */
  927. SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  928. }
  929. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  930. else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  931. {
  932. /* Pause the audio file playing by disabling the I2S DMA request */
  933. SET_BIT(hi2s->Instance->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  934. SET_BIT(I2SxEXT(hi2s->Instance)->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  935. /* If the I2Sext peripheral is still not enabled, enable it */
  936. if ((I2SxEXT(hi2s->Instance)->I2SCFGR & SPI_I2SCFGR_I2SE) == 0U)
  937. {
  938. /* Enable I2Sext peripheral */
  939. __HAL_I2SEXT_ENABLE(hi2s);
  940. }
  941. }
  942. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  943. /* If the I2S peripheral is still not enabled, enable it */
  944. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) == 0U)
  945. {
  946. /* Enable I2S peripheral */
  947. __HAL_I2S_ENABLE(hi2s);
  948. }
  949. /* Process Unlocked */
  950. __HAL_UNLOCK(hi2s);
  951. return HAL_OK;
  952. }
  953. /**
  954. * @brief Resumes the audio stream playing from the Media.
  955. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  956. * the configuration information for I2S module
  957. * @retval HAL status
  958. */
  959. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  960. {
  961. /* Process Locked */
  962. __HAL_LOCK(hi2s);
  963. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  964. {
  965. /* Disable the I2S DMA requests */
  966. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  967. /* Disable the I2S DMA Channel */
  968. HAL_DMA_Abort(hi2s->hdmatx);
  969. }
  970. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  971. {
  972. /* Disable the I2S DMA requests */
  973. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  974. /* Disable the I2S DMA Channel */
  975. HAL_DMA_Abort(hi2s->hdmarx);
  976. }
  977. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  978. else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  979. {
  980. /* Disable the I2S DMA requests */
  981. CLEAR_BIT(hi2s->Instance->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  982. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  983. /* Disable the I2S DMA Channels */
  984. HAL_DMA_Abort(hi2s->hdmatx);
  985. HAL_DMA_Abort(hi2s->hdmarx);
  986. /* Disable I2Sext peripheral */
  987. __HAL_I2SEXT_DISABLE(hi2s);
  988. }
  989. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  990. /* Disable I2S peripheral */
  991. __HAL_I2S_DISABLE(hi2s);
  992. hi2s->State = HAL_I2S_STATE_READY;
  993. /* Process Unlocked */
  994. __HAL_UNLOCK(hi2s);
  995. return HAL_OK;
  996. }
  997. /**
  998. * @brief This function handles I2S interrupt request.
  999. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1000. * the configuration information for I2S module
  1001. * @retval None
  1002. */
  1003. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1004. {
  1005. /* Call the IrqHandler ISR set during HAL_I2S_INIT */
  1006. hi2s->IrqHandlerISR(hi2s);
  1007. }
  1008. /**
  1009. * @brief Tx Transfer Half completed callbacks
  1010. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1011. * the configuration information for I2S module
  1012. * @retval None
  1013. */
  1014. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1015. {
  1016. /* Prevent unused argument(s) compilation warning */
  1017. UNUSED(hi2s);
  1018. /* NOTE : This function Should not be modified, when the callback is needed,
  1019. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  1020. */
  1021. }
  1022. /**
  1023. * @brief Tx Transfer completed callbacks
  1024. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1025. * the configuration information for I2S module
  1026. * @retval None
  1027. */
  1028. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  1029. {
  1030. /* Prevent unused argument(s) compilation warning */
  1031. UNUSED(hi2s);
  1032. /* NOTE : This function Should not be modified, when the callback is needed,
  1033. the HAL_I2S_TxCpltCallback could be implemented in the user file
  1034. */
  1035. }
  1036. /**
  1037. * @brief Rx Transfer half completed callbacks
  1038. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1039. * the configuration information for I2S module
  1040. * @retval None
  1041. */
  1042. __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1043. {
  1044. /* Prevent unused argument(s) compilation warning */
  1045. UNUSED(hi2s);
  1046. /* NOTE : This function Should not be modified, when the callback is needed,
  1047. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1048. */
  1049. }
  1050. /**
  1051. * @brief Rx Transfer completed callbacks
  1052. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1053. * the configuration information for I2S module
  1054. * @retval None
  1055. */
  1056. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  1057. {
  1058. /* Prevent unused argument(s) compilation warning */
  1059. UNUSED(hi2s);
  1060. /* NOTE : This function Should not be modified, when the callback is needed,
  1061. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1062. */
  1063. }
  1064. /**
  1065. * @brief I2S error callbacks
  1066. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1067. * the configuration information for I2S module
  1068. * @retval None
  1069. */
  1070. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  1071. {
  1072. /* Prevent unused argument(s) compilation warning */
  1073. UNUSED(hi2s);
  1074. /* NOTE : This function Should not be modified, when the callback is needed,
  1075. the HAL_I2S_ErrorCallback could be implemented in the user file
  1076. */
  1077. }
  1078. /**
  1079. * @}
  1080. */
  1081. /** @addtogroup I2S_Exported_Functions_Group3
  1082. * @brief Peripheral State functions
  1083. *
  1084. @verbatim
  1085. ===============================================================================
  1086. ##### Peripheral State and Errors functions #####
  1087. ===============================================================================
  1088. [..]
  1089. This subsection permits to get in run-time the status of the peripheral
  1090. and the data flow.
  1091. @endverbatim
  1092. * @{
  1093. */
  1094. /**
  1095. * @brief Return the I2S state
  1096. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1097. * the configuration information for I2S module
  1098. * @retval HAL state
  1099. */
  1100. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  1101. {
  1102. return hi2s->State;
  1103. }
  1104. /**
  1105. * @brief Return the I2S error code
  1106. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1107. * the configuration information for I2S module
  1108. * @retval I2S Error Code
  1109. */
  1110. uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1111. {
  1112. return hi2s->ErrorCode;
  1113. }
  1114. /**
  1115. * @}
  1116. */
  1117. /**
  1118. * @}
  1119. */
  1120. /** @addtogroup I2S_Private_Functions I2S Private Functions
  1121. * @{
  1122. */
  1123. /**
  1124. * @brief DMA I2S transmit process complete callback
  1125. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1126. * the configuration information for the specified DMA module.
  1127. * @retval None
  1128. */
  1129. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1130. {
  1131. I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1132. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1133. {
  1134. /* Disable Tx DMA Request */
  1135. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  1136. hi2s->TxXferCount = 0U;
  1137. hi2s->State = HAL_I2S_STATE_READY;
  1138. }
  1139. HAL_I2S_TxCpltCallback(hi2s);
  1140. }
  1141. /**
  1142. * @brief DMA I2S transmit process half complete callback
  1143. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1144. * the configuration information for the specified DMA module.
  1145. * @retval None
  1146. */
  1147. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1148. {
  1149. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1150. HAL_I2S_TxHalfCpltCallback(hi2s);
  1151. }
  1152. /**
  1153. * @brief DMA I2S receive process complete callback
  1154. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1155. * the configuration information for the specified DMA module.
  1156. * @retval None
  1157. */
  1158. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1159. {
  1160. I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1161. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1162. {
  1163. /* Disable Rx DMA Request */
  1164. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  1165. hi2s->RxXferCount = 0U;
  1166. hi2s->State = HAL_I2S_STATE_READY;
  1167. }
  1168. HAL_I2S_RxCpltCallback(hi2s);
  1169. }
  1170. /**
  1171. * @brief DMA I2S receive process half complete callback
  1172. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1173. * the configuration information for the specified DMA module.
  1174. * @retval None
  1175. */
  1176. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1177. {
  1178. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1179. HAL_I2S_RxHalfCpltCallback(hi2s);
  1180. }
  1181. /**
  1182. * @brief DMA I2S communication error callback
  1183. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1184. * the configuration information for the specified DMA module.
  1185. * @retval None
  1186. */
  1187. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1188. {
  1189. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1190. /* Disable Rx and Tx DMA Request */
  1191. CLEAR_BIT(hi2s->Instance->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1192. hi2s->TxXferCount = 0U;
  1193. hi2s->RxXferCount = 0U;
  1194. hi2s->State= HAL_I2S_STATE_READY;
  1195. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_DMA);
  1196. HAL_I2S_ErrorCallback(hi2s);
  1197. }
  1198. /**
  1199. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1200. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1201. * the configuration information for I2S module
  1202. * @retval HAL status
  1203. */
  1204. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1205. {
  1206. /* Transmit data */
  1207. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  1208. hi2s->TxXferCount--;
  1209. if(hi2s->TxXferCount == 0U)
  1210. {
  1211. /* Disable TXE and ERR interrupt */
  1212. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1213. hi2s->State = HAL_I2S_STATE_READY;
  1214. HAL_I2S_TxCpltCallback(hi2s);
  1215. }
  1216. }
  1217. /**
  1218. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1219. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1220. * the configuration information for I2S module
  1221. * @retval HAL status
  1222. */
  1223. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1224. {
  1225. /* Receive data */
  1226. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  1227. hi2s->RxXferCount--;
  1228. if(hi2s->RxXferCount == 0U)
  1229. {
  1230. /* Disable RXNE and ERR interrupt */
  1231. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1232. hi2s->State = HAL_I2S_STATE_READY;
  1233. HAL_I2S_RxCpltCallback(hi2s);
  1234. }
  1235. }
  1236. /**
  1237. * @brief This function handles I2S interrupt request.
  1238. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1239. * the configuration information for I2S module
  1240. * @retval None
  1241. */
  1242. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1243. {
  1244. __IO uint32_t i2ssr = hi2s->Instance->SR;
  1245. if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1246. {
  1247. /* I2S in mode Receiver ------------------------------------------------*/
  1248. if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
  1249. {
  1250. I2S_Receive_IT(hi2s);
  1251. }
  1252. /* I2S Overrun error interrupt occured -------------------------------------*/
  1253. if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1254. {
  1255. /* Disable RXNE and ERR interrupt */
  1256. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1257. /* Clear Overrun flag */
  1258. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1259. /* Set the I2S State ready */
  1260. hi2s->State = HAL_I2S_STATE_READY;
  1261. /* Set the error code and execute error callback*/
  1262. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_OVR);
  1263. HAL_I2S_ErrorCallback(hi2s);
  1264. }
  1265. }
  1266. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1267. {
  1268. /* I2S in mode Transmitter -----------------------------------------------*/
  1269. if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
  1270. {
  1271. I2S_Transmit_IT(hi2s);
  1272. }
  1273. /* I2S Underrun error interrupt occurred --------------------------------*/
  1274. if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1275. {
  1276. /* Disable TXE and ERR interrupt */
  1277. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1278. /* Clear Underrun flag */
  1279. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  1280. /* Set the I2S State ready */
  1281. hi2s->State = HAL_I2S_STATE_READY;
  1282. /* Set the error code and execute error callback*/
  1283. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  1284. HAL_I2S_ErrorCallback(hi2s);
  1285. }
  1286. }
  1287. }
  1288. /**
  1289. * @brief This function handles I2S Communication Timeout.
  1290. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1291. * the configuration information for I2S module
  1292. * @param Flag: Flag checked
  1293. * @param State: Value of the flag expected
  1294. * @param Timeout: Duration of the timeout
  1295. * @retval HAL status
  1296. */
  1297. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State,
  1298. uint32_t Timeout)
  1299. {
  1300. uint32_t tickstart = HAL_GetTick();
  1301. /* Wait until flag is set to status*/
  1302. while(((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  1303. {
  1304. if(Timeout != HAL_MAX_DELAY)
  1305. {
  1306. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1307. {
  1308. /* Set the I2S State ready */
  1309. hi2s->State = HAL_I2S_STATE_READY;
  1310. /* Process Unlocked */
  1311. __HAL_UNLOCK(hi2s);
  1312. return HAL_TIMEOUT;
  1313. }
  1314. }
  1315. }
  1316. return HAL_OK;
  1317. }
  1318. /**
  1319. * @}
  1320. */
  1321. /**
  1322. * @}
  1323. */
  1324. #endif /* HAL_I2S_MODULE_ENABLED */
  1325. /**
  1326. * @}
  1327. */
  1328. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/