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.
 
 
 

1536 lines
49 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_i2s.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  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 follows:
  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 channel.
  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 Channel.
  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 Channel.
  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 clock is configured based on SYSCLK or
  45. (+@) External clock source is configured after setting correctly
  46. the define constant EXTERNAL_CLOCK_VALUE in the stm32f3xx_hal_conf.h file.
  47. (#) Three mode of operations 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) 2015 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 "stm32f7xx_hal.h"
  129. /** @addtogroup STM32F7xx_HAL_Driver
  130. * @{
  131. */
  132. /** @defgroup I2S I2S
  133. * @brief I2S HAL module driver
  134. * @{
  135. */
  136. #ifdef HAL_I2S_MODULE_ENABLED
  137. /* Private typedef -----------------------------------------------------------*/
  138. /* Private define ------------------------------------------------------------*/
  139. /* Private macro -------------------------------------------------------------*/
  140. /* Private variables ---------------------------------------------------------*/
  141. /* Private function prototypes -----------------------------------------------*/
  142. /** @defgroup I2S_Private_Functions 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 uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s);
  153. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, uint32_t Timeout);
  154. /**
  155. * @}
  156. */
  157. /* Exported functions ---------------------------------------------------------*/
  158. /** @defgroup I2S_Exported_Functions I2S Exported Functions
  159. * @{
  160. */
  161. /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
  162. * @brief Initialization and Configuration functions
  163. *
  164. @verbatim
  165. ===============================================================================
  166. ##### Initialization and de-initialization functions #####
  167. ===============================================================================
  168. [..] This subsection provides a set of functions allowing to initialize and
  169. de-initialize the I2Sx peripheral in simplex mode:
  170. (+) User must Implement HAL_I2S_MspInit() function in which he configures
  171. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  172. (+) Call the function HAL_I2S_Init() to configure the selected device with
  173. the selected configuration:
  174. (++) Mode
  175. (++) Standard
  176. (++) Data Format
  177. (++) MCLK Output
  178. (++) Audio frequency
  179. (++) Polarity
  180. (++) Full duplex mode
  181. (+) Call the function HAL_I2S_DeInit() to restore the default configuration
  182. of the selected I2Sx peripheral.
  183. @endverbatim
  184. * @{
  185. */
  186. /**
  187. * @brief Initializes the I2S according to the specified parameters
  188. * in the I2S_InitTypeDef and create the associated handle.
  189. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  190. * the configuration information for I2S module
  191. * @retval HAL status
  192. */
  193. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  194. {
  195. uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
  196. uint32_t tmp = 0, i2sclk = 0;
  197. /* Check the I2S handle allocation */
  198. if(hi2s == NULL)
  199. {
  200. return HAL_ERROR;
  201. }
  202. /* Check the parameters */
  203. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  204. assert_param(IS_I2S_MODE(hi2s->Init.Mode));
  205. assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
  206. assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
  207. assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
  208. assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
  209. assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
  210. assert_param(IS_I2S_CLOCKSOURCE(hi2s->Init.ClockSource));
  211. if(hi2s->State == HAL_I2S_STATE_RESET)
  212. {
  213. /* Allocate lock resource and initialize it */
  214. hi2s->Lock = HAL_UNLOCKED;
  215. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  216. HAL_I2S_MspInit(hi2s);
  217. }
  218. hi2s->State = HAL_I2S_STATE_BUSY;
  219. /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
  220. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  221. hi2s->Instance->I2SCFGR &= ~(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  222. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  223. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD);
  224. hi2s->Instance->I2SPR = 0x0002;
  225. /* Get the I2SCFGR register value */
  226. tmpreg = hi2s->Instance->I2SCFGR;
  227. /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
  228. if(hi2s->Init.AudioFreq == I2S_AUDIOFREQ_DEFAULT)
  229. {
  230. i2sodd = (uint16_t)0;
  231. i2sdiv = (uint16_t)2;
  232. }
  233. /* If the requested audio frequency is not the default, compute the prescaler */
  234. else
  235. {
  236. /* Check the frame length (For the Prescaler computing) *******************/
  237. if(hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)
  238. {
  239. /* Packet length is 16 bits */
  240. packetlength = 1;
  241. }
  242. else
  243. {
  244. /* Packet length is 32 bits */
  245. packetlength = 2;
  246. }
  247. /* Get I2S source Clock frequency ****************************************/
  248. /* If an external I2S clock has to be used, the specific define should be set
  249. in the project configuration or in the stm32f3xx_conf.h file */
  250. if(hi2s->Init.ClockSource == I2S_CLOCK_EXTERNAL)
  251. {
  252. /* Set the I2S clock to the external clock value */
  253. i2sclk = EXTERNAL_CLOCK_VALUE;
  254. }
  255. else
  256. {
  257. /* Get the I2S source clock value */
  258. i2sclk = I2S_GetClockFreq(hi2s);
  259. }
  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. tmp = (uint16_t)(((((i2sclk / 256) * 10) / hi2s->Init.AudioFreq)) + 5);
  265. }
  266. else
  267. {
  268. /* MCLK output is disabled */
  269. tmp = (uint16_t)(((((i2sclk / (32 * packetlength)) *10 ) / hi2s->Init.AudioFreq)) + 5);
  270. }
  271. /* Remove the flatting point */
  272. tmp = tmp / 10;
  273. /* Check the parity of the divider */
  274. i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
  275. /* Compute the i2sdiv prescaler */
  276. i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
  277. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  278. i2sodd = (uint16_t) (i2sodd << 8);
  279. }
  280. /* Test if the divider is 1 or 0 or greater than 0xFF */
  281. if((i2sdiv < 2) || (i2sdiv > 0xFF))
  282. {
  283. /* Set the default values */
  284. i2sdiv = 2;
  285. i2sodd = 0;
  286. }
  287. /* Write to SPIx I2SPR register the computed value */
  288. hi2s->Instance->I2SPR = (uint16_t)((uint16_t)i2sdiv | (uint16_t)(i2sodd | (uint16_t)hi2s->Init.MCLKOutput));
  289. /* Configure the I2S with the I2S_InitStruct values */
  290. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \
  291. (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
  292. (uint16_t)hi2s->Init.CPOL))));
  293. /* Write to SPIx I2SCFGR */
  294. hi2s->Instance->I2SCFGR = tmpreg;
  295. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  296. hi2s->State= HAL_I2S_STATE_READY;
  297. return HAL_OK;
  298. }
  299. /**
  300. * @brief DeInitializes the I2S peripheral
  301. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  302. * the configuration information for I2S module
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  306. {
  307. /* Check the I2S handle allocation */
  308. if(hi2s == NULL)
  309. {
  310. return HAL_ERROR;
  311. }
  312. /* Check the parameters */
  313. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  314. hi2s->State = HAL_I2S_STATE_BUSY;
  315. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  316. HAL_I2S_MspDeInit(hi2s);
  317. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  318. hi2s->State = HAL_I2S_STATE_RESET;
  319. /* Release Lock */
  320. __HAL_UNLOCK(hi2s);
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief I2S MSP Init
  325. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  326. * the configuration information for I2S module
  327. * @retval None
  328. */
  329. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  330. {
  331. /* NOTE : This function Should not be modified, when the callback is needed,
  332. the HAL_I2S_MspInit could be implemented in the user file
  333. */
  334. }
  335. /**
  336. * @brief I2S MSP DeInit
  337. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  338. * the configuration information for I2S module
  339. * @retval None
  340. */
  341. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  342. {
  343. /* NOTE : This function Should not be modified, when the callback is needed,
  344. the HAL_I2S_MspDeInit could be implemented in the user file
  345. */
  346. }
  347. /**
  348. * @}
  349. */
  350. /** @defgroup I2S_Exported_Functions_Group2 Input and Output operation functions
  351. * @brief Data transfers functions
  352. *
  353. @verbatim
  354. ===============================================================================
  355. ##### IO operation functions #####
  356. ===============================================================================
  357. [..]
  358. This subsection provides a set of functions allowing to manage the I2S data
  359. transfers.
  360. (#) There are two modes of transfer:
  361. (++) Blocking mode : The communication is performed in the polling mode.
  362. The status of all data processing is returned by the same function
  363. after finishing transfer.
  364. (++) No-Blocking mode : The communication is performed using Interrupts
  365. or DMA. These functions return the status of the transfer startup.
  366. The end of the data processing will be indicated through the
  367. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  368. using DMA mode.
  369. (#) Blocking mode functions are :
  370. (++) HAL_I2S_Transmit()
  371. (++) HAL_I2S_Receive()
  372. (#) No-Blocking mode functions with Interrupt are :
  373. (++) HAL_I2S_Transmit_IT()
  374. (++) HAL_I2S_Receive_IT()
  375. (#) No-Blocking mode functions with DMA are :
  376. (++) HAL_I2S_Transmit_DMA()
  377. (++) HAL_I2S_Receive_DMA()
  378. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  379. (++) HAL_I2S_TxCpltCallback()
  380. (++) HAL_I2S_RxCpltCallback()
  381. (++) HAL_I2S_ErrorCallback()
  382. @endverbatim
  383. * @{
  384. */
  385. /**
  386. * @brief Transmit an amount of data in blocking mode
  387. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  388. * the configuration information for I2S module
  389. * @param pData: a 16-bit pointer to data buffer.
  390. * @param Size: number of data sample to be sent:
  391. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  392. * configuration phase, the Size parameter means the number of 16-bit data length
  393. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  394. * the Size parameter means the number of 16-bit data length.
  395. * @param Timeout: Timeout duration
  396. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  397. * between Master and Slave(example: audio streaming).
  398. * @retval HAL status
  399. */
  400. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  401. {
  402. if((pData == NULL ) || (Size == 0))
  403. {
  404. return HAL_ERROR;
  405. }
  406. if(hi2s->State == HAL_I2S_STATE_READY)
  407. {
  408. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  409. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  410. {
  411. hi2s->TxXferSize = (Size << 1);
  412. hi2s->TxXferCount = (Size << 1);
  413. }
  414. else
  415. {
  416. hi2s->TxXferSize = Size;
  417. hi2s->TxXferCount = Size;
  418. }
  419. /* Process Locked */
  420. __HAL_LOCK(hi2s);
  421. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  422. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  423. /* Check if the I2S is already enabled */
  424. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  425. {
  426. /* Enable I2S peripheral */
  427. __HAL_I2S_ENABLE(hi2s);
  428. }
  429. while(hi2s->TxXferCount > 0)
  430. {
  431. hi2s->Instance->DR = (*pData++);
  432. hi2s->TxXferCount--;
  433. /* Wait until TXE flag is set */
  434. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  435. {
  436. /* Set the error code and execute error callback*/
  437. hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
  438. HAL_I2S_ErrorCallback(hi2s);
  439. return HAL_TIMEOUT;
  440. }
  441. /* Check if an underrun occurs */
  442. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
  443. {
  444. /* Set the I2S State ready */
  445. hi2s->State = HAL_I2S_STATE_READY;
  446. /* Process Unlocked */
  447. __HAL_UNLOCK(hi2s);
  448. /* Set the error code and execute error callback*/
  449. hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
  450. HAL_I2S_ErrorCallback(hi2s);
  451. return HAL_ERROR;
  452. }
  453. }
  454. /* Check if Slave mode is selected */
  455. if(((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX) || ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_RX))
  456. {
  457. /* Wait until Busy flag is reset */
  458. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK)
  459. {
  460. /* Set the error code and execute error callback*/
  461. hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
  462. HAL_I2S_ErrorCallback(hi2s);
  463. return HAL_TIMEOUT;
  464. }
  465. }
  466. hi2s->State = HAL_I2S_STATE_READY;
  467. /* Process Unlocked */
  468. __HAL_UNLOCK(hi2s);
  469. return HAL_OK;
  470. }
  471. else
  472. {
  473. return HAL_BUSY;
  474. }
  475. }
  476. /**
  477. * @brief Receive an amount of data in blocking mode
  478. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  479. * the configuration information for I2S module
  480. * @param pData: a 16-bit pointer to data buffer.
  481. * @param Size: number of data sample to be sent:
  482. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  483. * configuration phase, the Size parameter means the number of 16-bit data length
  484. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  485. * the Size parameter means the number of 16-bit data length.
  486. * @param Timeout: Timeout duration
  487. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  488. * between Master and Slave(example: audio streaming).
  489. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  490. * in continuous way and as the I2S is not disabled at the end of the I2S transaction.
  491. * @retval HAL status
  492. */
  493. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  494. {
  495. if((pData == NULL ) || (Size == 0))
  496. {
  497. return HAL_ERROR;
  498. }
  499. if(hi2s->State == HAL_I2S_STATE_READY)
  500. {
  501. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  502. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  503. {
  504. hi2s->RxXferSize = (Size << 1);
  505. hi2s->RxXferCount = (Size << 1);
  506. }
  507. else
  508. {
  509. hi2s->RxXferSize = Size;
  510. hi2s->RxXferCount = Size;
  511. }
  512. /* Process Locked */
  513. __HAL_LOCK(hi2s);
  514. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  515. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  516. /* Check if the I2S is already enabled */
  517. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  518. {
  519. /* Enable I2S peripheral */
  520. __HAL_I2S_ENABLE(hi2s);
  521. }
  522. /* Check if Master Receiver mode is selected */
  523. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  524. {
  525. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  526. access to the SPI_SR register. */
  527. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  528. }
  529. /* Receive data */
  530. while(hi2s->RxXferCount > 0)
  531. {
  532. /* Wait until RXNE flag is set */
  533. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
  534. {
  535. /* Set the error code and execute error callback*/
  536. hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
  537. HAL_I2S_ErrorCallback(hi2s);
  538. return HAL_TIMEOUT;
  539. }
  540. /* Check if an overrun occurs */
  541. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  542. {
  543. /* Set the I2S State ready */
  544. hi2s->State = HAL_I2S_STATE_READY;
  545. /* Process Unlocked */
  546. __HAL_UNLOCK(hi2s);
  547. /* Set the error code and execute error callback*/
  548. hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
  549. HAL_I2S_ErrorCallback(hi2s);
  550. return HAL_ERROR;
  551. }
  552. (*pData++) = hi2s->Instance->DR;
  553. hi2s->RxXferCount--;
  554. }
  555. hi2s->State = HAL_I2S_STATE_READY;
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(hi2s);
  558. return HAL_OK;
  559. }
  560. else
  561. {
  562. return HAL_BUSY;
  563. }
  564. }
  565. /**
  566. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  567. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  568. * the configuration information for I2S module
  569. * @param pData: a 16-bit pointer to data buffer.
  570. * @param Size: number of data sample to be sent:
  571. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  572. * configuration phase, the Size parameter means the number of 16-bit data length
  573. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  574. * the Size parameter means the number of 16-bit data length.
  575. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  576. * between Master and Slave(example: audio streaming).
  577. * @retval HAL status
  578. */
  579. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  580. {
  581. if(hi2s->State == HAL_I2S_STATE_READY)
  582. {
  583. if((pData == NULL) || (Size == 0))
  584. {
  585. return HAL_ERROR;
  586. }
  587. hi2s->pTxBuffPtr = pData;
  588. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  589. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  590. {
  591. hi2s->TxXferSize = (Size << 1);
  592. hi2s->TxXferCount = (Size << 1);
  593. }
  594. else
  595. {
  596. hi2s->TxXferSize = Size;
  597. hi2s->TxXferCount = Size;
  598. }
  599. /* Process Locked */
  600. __HAL_LOCK(hi2s);
  601. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  602. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  603. /* Enable TXE and ERR interrupt */
  604. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  605. /* Check if the I2S is already enabled */
  606. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  607. {
  608. /* Enable I2S peripheral */
  609. __HAL_I2S_ENABLE(hi2s);
  610. }
  611. /* Process Unlocked */
  612. __HAL_UNLOCK(hi2s);
  613. return HAL_OK;
  614. }
  615. else
  616. {
  617. return HAL_BUSY;
  618. }
  619. }
  620. /**
  621. * @brief Receive an amount of data in non-blocking mode with Interrupt
  622. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  623. * the configuration information for I2S module
  624. * @param pData: a 16-bit pointer to the Receive data buffer.
  625. * @param Size: number of data sample to be sent:
  626. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  627. * configuration phase, the Size parameter means the number of 16-bit data length
  628. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  629. * the Size parameter means the number of 16-bit data length.
  630. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  631. * between Master and Slave(example: audio streaming).
  632. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
  633. * between Master and Slave otherwise the I2S interrupt should be optimized.
  634. * @retval HAL status
  635. */
  636. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  637. {
  638. if(hi2s->State == HAL_I2S_STATE_READY)
  639. {
  640. if((pData == NULL) || (Size == 0))
  641. {
  642. return HAL_ERROR;
  643. }
  644. hi2s->pRxBuffPtr = pData;
  645. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  646. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  647. {
  648. hi2s->RxXferSize = (Size << 1);
  649. hi2s->RxXferCount = (Size << 1);
  650. }
  651. else
  652. {
  653. hi2s->RxXferSize = Size;
  654. hi2s->RxXferCount = Size;
  655. }
  656. /* Process Locked */
  657. __HAL_LOCK(hi2s);
  658. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  659. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  660. /* Enable TXE and ERR interrupt */
  661. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  662. /* Check if the I2S is already enabled */
  663. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  664. {
  665. /* Enable I2S peripheral */
  666. __HAL_I2S_ENABLE(hi2s);
  667. }
  668. /* Process Unlocked */
  669. __HAL_UNLOCK(hi2s);
  670. return HAL_OK;
  671. }
  672. else
  673. {
  674. return HAL_BUSY;
  675. }
  676. }
  677. /**
  678. * @brief Transmit an amount of data in non-blocking mode with DMA
  679. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  680. * the configuration information for I2S module
  681. * @param pData: a 16-bit pointer to the Transmit data buffer.
  682. * @param Size: number of data sample to be sent:
  683. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  684. * configuration phase, the Size parameter means the number of 16-bit data length
  685. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  686. * the Size parameter means the number of 16-bit data length.
  687. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  688. * between Master and Slave(example: audio streaming).
  689. * @retval HAL status
  690. */
  691. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  692. {
  693. uint32_t *tmp;
  694. if((pData == NULL) || (Size == 0))
  695. {
  696. return HAL_ERROR;
  697. }
  698. if(hi2s->State == HAL_I2S_STATE_READY)
  699. {
  700. hi2s->pTxBuffPtr = pData;
  701. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  702. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  703. {
  704. hi2s->TxXferSize = (Size << 1);
  705. hi2s->TxXferCount = (Size << 1);
  706. }
  707. else
  708. {
  709. hi2s->TxXferSize = Size;
  710. hi2s->TxXferCount = Size;
  711. }
  712. /* Process Locked */
  713. __HAL_LOCK(hi2s);
  714. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  715. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  716. /* Set the I2S Tx DMA Half transfer complete callback */
  717. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  718. /* Set the I2S TxDMA transfer complete callback */
  719. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  720. /* Set the DMA error callback */
  721. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  722. /* Enable the Tx DMA Channel */
  723. tmp = (uint32_t*)&pData;
  724. HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
  725. /* Check if the I2S is already enabled */
  726. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  727. {
  728. /* Enable I2S peripheral */
  729. __HAL_I2S_ENABLE(hi2s);
  730. }
  731. /* Enable Tx DMA Request */
  732. hi2s->Instance->CR2 |= SPI_CR2_TXDMAEN;
  733. /* Process Unlocked */
  734. __HAL_UNLOCK(hi2s);
  735. return HAL_OK;
  736. }
  737. else
  738. {
  739. return HAL_BUSY;
  740. }
  741. }
  742. /**
  743. * @brief Receive an amount of data in non-blocking mode with DMA
  744. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  745. * the configuration information for I2S module
  746. * @param pData: a 16-bit pointer to the Receive data buffer.
  747. * @param Size: number of data sample to be sent:
  748. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  749. * configuration phase, the Size parameter means the number of 16-bit data length
  750. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  751. * the Size parameter means the number of 16-bit data length.
  752. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  753. * between Master and Slave(example: audio streaming).
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  757. {
  758. uint32_t *tmp;
  759. if((pData == NULL) || (Size == 0))
  760. {
  761. return HAL_ERROR;
  762. }
  763. if(hi2s->State == HAL_I2S_STATE_READY)
  764. {
  765. hi2s->pRxBuffPtr = pData;
  766. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  767. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  768. {
  769. hi2s->RxXferSize = (Size << 1);
  770. hi2s->RxXferCount = (Size << 1);
  771. }
  772. else
  773. {
  774. hi2s->RxXferSize = Size;
  775. hi2s->RxXferCount = Size;
  776. }
  777. /* Process Locked */
  778. __HAL_LOCK(hi2s);
  779. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  780. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  781. /* Set the I2S Rx DMA Half transfer complete callback */
  782. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  783. /* Set the I2S Rx DMA transfer complete callback */
  784. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  785. /* Set the DMA error callback */
  786. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  787. /* Check if Master Receiver mode is selected */
  788. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  789. {
  790. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  791. access to the SPI_SR register. */
  792. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  793. }
  794. /* Enable the Rx DMA Channel */
  795. tmp = (uint32_t*)&pData;
  796. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);
  797. /* Check if the I2S is already enabled */
  798. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  799. {
  800. /* Enable I2S peripheral */
  801. __HAL_I2S_ENABLE(hi2s);
  802. }
  803. /* Enable Rx DMA Request */
  804. hi2s->Instance->CR2 |= SPI_CR2_RXDMAEN;
  805. /* Process Unlocked */
  806. __HAL_UNLOCK(hi2s);
  807. return HAL_OK;
  808. }
  809. else
  810. {
  811. return HAL_BUSY;
  812. }
  813. }
  814. /**
  815. * @brief Pauses the audio stream playing from the Media.
  816. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  817. * the configuration information for I2S module
  818. * @retval HAL status
  819. */
  820. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  821. {
  822. /* Process Locked */
  823. __HAL_LOCK(hi2s);
  824. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  825. {
  826. /* Disable the I2S DMA Tx request */
  827. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  828. }
  829. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  830. {
  831. /* Disable the I2S DMA Rx request */
  832. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  833. }
  834. else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  835. {
  836. if((hi2s->Init.Mode == I2S_MODE_SLAVE_TX)||(hi2s->Init.Mode == I2S_MODE_MASTER_TX))
  837. {
  838. /* Disable the I2S DMA Tx request */
  839. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  840. }
  841. else
  842. {
  843. /* Disable the I2S DMA Rx request */
  844. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  845. }
  846. }
  847. /* Process Unlocked */
  848. __HAL_UNLOCK(hi2s);
  849. return HAL_OK;
  850. }
  851. /**
  852. * @brief Resumes the audio stream playing from the Media.
  853. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  854. * the configuration information for I2S module
  855. * @retval HAL status
  856. */
  857. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  858. {
  859. /* Process Locked */
  860. __HAL_LOCK(hi2s);
  861. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  862. {
  863. /* Enable the I2S DMA Tx request */
  864. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  865. }
  866. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  867. {
  868. /* Enable the I2S DMA Rx request */
  869. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  870. }
  871. /* If the I2S peripheral is still not enabled, enable it */
  872. if(HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  873. {
  874. /* Enable I2S peripheral */
  875. __HAL_I2S_ENABLE(hi2s);
  876. }
  877. /* Process Unlocked */
  878. __HAL_UNLOCK(hi2s);
  879. return HAL_OK;
  880. }
  881. /**
  882. * @brief Stops the audio stream playing from the Media.
  883. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  884. * the configuration information for I2S module
  885. * @retval HAL status
  886. */
  887. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  888. {
  889. /* Process Locked */
  890. __HAL_LOCK(hi2s);
  891. /* Disable the I2S Tx/Rx DMA requests */
  892. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  893. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  894. /* Abort the I2S DMA Channel tx */
  895. if(hi2s->hdmatx != NULL)
  896. {
  897. /* Disable the I2S DMA channel */
  898. __HAL_DMA_DISABLE(hi2s->hdmatx);
  899. HAL_DMA_Abort(hi2s->hdmatx);
  900. }
  901. /* Abort the I2S DMA Channel rx */
  902. if(hi2s->hdmarx != NULL)
  903. {
  904. /* Disable the I2S DMA channel */
  905. __HAL_DMA_DISABLE(hi2s->hdmarx);
  906. HAL_DMA_Abort(hi2s->hdmarx);
  907. }
  908. /* Disable I2S peripheral */
  909. __HAL_I2S_DISABLE(hi2s);
  910. hi2s->State = HAL_I2S_STATE_READY;
  911. /* Process Unlocked */
  912. __HAL_UNLOCK(hi2s);
  913. return HAL_OK;
  914. }
  915. /**
  916. * @brief This function handles I2S interrupt request.
  917. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  918. * the configuration information for I2S module
  919. * @retval HAL status
  920. */
  921. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  922. {
  923. __IO uint32_t i2ssr = hi2s->Instance->SR;
  924. if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  925. {
  926. /* I2S in mode Receiver ----------------------------------------------------*/
  927. if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
  928. {
  929. I2S_Receive_IT(hi2s);
  930. }
  931. /* I2S Overrun error interrupt occurred -------------------------------------*/
  932. if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  933. {
  934. /* Disable RXNE and ERR interrupt */
  935. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  936. /* Set the I2S State ready */
  937. hi2s->State = HAL_I2S_STATE_READY;
  938. /* Set the error code and execute error callback*/
  939. hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
  940. HAL_I2S_ErrorCallback(hi2s);
  941. }
  942. }
  943. else if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  944. {
  945. /* I2S in mode Transmitter ---------------------------------------------------*/
  946. if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
  947. {
  948. I2S_Transmit_IT(hi2s);
  949. }
  950. /* I2S Underrun error interrupt occurred ------------------------------------*/
  951. if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  952. {
  953. /* Disable TXE and ERR interrupt */
  954. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  955. /* Set the I2S State ready */
  956. hi2s->State = HAL_I2S_STATE_READY;
  957. /* Set the error code and execute error callback*/
  958. hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
  959. HAL_I2S_ErrorCallback(hi2s);
  960. }
  961. }
  962. }
  963. /**
  964. * @}
  965. */
  966. /**
  967. * @}
  968. */
  969. /** @addtogroup I2S_Private_Functions I2S Private Functions
  970. * @{
  971. */
  972. /**
  973. * @brief This function handles I2S Communication Timeout.
  974. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  975. * the configuration information for I2S module
  976. * @param Flag: Flag checked
  977. * @param State: Value of the flag expected
  978. * @param Timeout: Duration of the timeout
  979. * @retval HAL status
  980. */
  981. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag,
  982. uint32_t State, uint32_t Timeout)
  983. {
  984. uint32_t tickstart = 0;
  985. /* Get tick */
  986. tickstart = HAL_GetTick();
  987. /* Wait until flag is set */
  988. if(State == RESET)
  989. {
  990. while(__HAL_I2S_GET_FLAG(hi2s, Flag) == RESET)
  991. {
  992. if(Timeout != HAL_MAX_DELAY)
  993. {
  994. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  995. {
  996. /* Set the I2S State ready */
  997. hi2s->State= HAL_I2S_STATE_READY;
  998. /* Process Unlocked */
  999. __HAL_UNLOCK(hi2s);
  1000. return HAL_TIMEOUT;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. else
  1006. {
  1007. while(__HAL_I2S_GET_FLAG(hi2s, Flag) != RESET)
  1008. {
  1009. if(Timeout != HAL_MAX_DELAY)
  1010. {
  1011. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1012. {
  1013. /* Set the I2S State ready */
  1014. hi2s->State= HAL_I2S_STATE_READY;
  1015. /* Process Unlocked */
  1016. __HAL_UNLOCK(hi2s);
  1017. return HAL_TIMEOUT;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. return HAL_OK;
  1023. }
  1024. /**
  1025. * @}
  1026. */
  1027. /** @addtogroup I2S_Exported_Functions I2S Exported Functions
  1028. * @{
  1029. */
  1030. /** @addtogroup I2S_Exported_Functions_Group2 Input and Output operation functions
  1031. * @{
  1032. */
  1033. /**
  1034. * @brief Tx Transfer Half completed callbacks
  1035. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1036. * the configuration information for I2S module
  1037. * @retval None
  1038. */
  1039. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1040. {
  1041. /* NOTE : This function Should not be modified, when the callback is needed,
  1042. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  1043. */
  1044. }
  1045. /**
  1046. * @brief Tx Transfer completed callbacks
  1047. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1048. * the configuration information for I2S module
  1049. * @retval None
  1050. */
  1051. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  1052. {
  1053. /* NOTE : This function Should not be modified, when the callback is needed,
  1054. the HAL_I2S_TxCpltCallback could be implemented in the user file
  1055. */
  1056. }
  1057. /**
  1058. * @brief Rx Transfer half completed callbacks
  1059. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1060. * the configuration information for I2S module
  1061. * @retval None
  1062. */
  1063. __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1064. {
  1065. /* NOTE : This function Should not be modified, when the callback is needed,
  1066. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1067. */
  1068. }
  1069. /**
  1070. * @brief Rx Transfer completed callbacks
  1071. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1072. * the configuration information for I2S module
  1073. * @retval None
  1074. */
  1075. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  1076. {
  1077. /* NOTE : This function Should not be modified, when the callback is needed,
  1078. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1079. */
  1080. }
  1081. /**
  1082. * @brief I2S error callbacks
  1083. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1084. * the configuration information for I2S module
  1085. * @retval None
  1086. */
  1087. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  1088. {
  1089. /* NOTE : This function Should not be modified, when the callback is needed,
  1090. the HAL_I2S_ErrorCallback could be implemented in the user file
  1091. */
  1092. }
  1093. /**
  1094. * @}
  1095. */
  1096. /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
  1097. * @brief Peripheral State functions
  1098. *
  1099. @verbatim
  1100. ===============================================================================
  1101. ##### Peripheral State and Errors functions #####
  1102. ===============================================================================
  1103. [..]
  1104. This subsection permits to get in run-time the status of the peripheral
  1105. and the data flow.
  1106. @endverbatim
  1107. * @{
  1108. */
  1109. /**
  1110. * @brief Return the I2S state
  1111. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1112. * the configuration information for I2S module
  1113. * @retval HAL state
  1114. */
  1115. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  1116. {
  1117. return hi2s->State;
  1118. }
  1119. /**
  1120. * @brief Return the I2S error code
  1121. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1122. * the configuration information for I2S module
  1123. * @retval I2S Error Code
  1124. */
  1125. uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1126. {
  1127. return hi2s->ErrorCode;
  1128. }
  1129. /**
  1130. * @}
  1131. */
  1132. /**
  1133. * @}
  1134. */
  1135. /**
  1136. * @brief Get I2S Input Clock based on I2S source clock selection
  1137. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1138. * the configuration information for I2S module.
  1139. * @retval I2S Clock Input
  1140. */
  1141. static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s)
  1142. {
  1143. uint32_t tmpreg = 0;
  1144. /* This variable used to store the VCO Input (value in Hz) */
  1145. uint32_t vcoinput = 0;
  1146. /* This variable used to store the I2S_CK_x (value in Hz) */
  1147. uint32_t i2sclocksource = 0;
  1148. /* Configure I2S Clock based on I2S source clock selection */
  1149. /* I2S_CLK_x : I2S Block Clock configuration for different clock sources selected */
  1150. switch(hi2s->Init.ClockSource)
  1151. {
  1152. case I2S_CLOCK_SYSCLK :
  1153. {
  1154. /* Configure the PLLI2S division factor */
  1155. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1156. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  1157. {
  1158. /* In Case the PLL Source is HSI (Internal Clock) */
  1159. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1160. }
  1161. else
  1162. {
  1163. /* In Case the PLL Source is HSE (External Clock) */
  1164. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  1165. }
  1166. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1167. /* I2S_CLK(first level) = PLLI2S_VCO Output/PLLI2SR */
  1168. tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28;
  1169. i2sclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6))/(tmpreg);
  1170. break;
  1171. }
  1172. case I2S_CLOCK_EXTERNAL :
  1173. {
  1174. i2sclocksource = EXTERNAL_CLOCK_VALUE;
  1175. break;
  1176. }
  1177. default :
  1178. {
  1179. break;
  1180. }
  1181. }
  1182. /* the return result is the value of I2S clock */
  1183. return i2sclocksource;
  1184. }
  1185. /** @addtogroup I2S_Private_Functions I2S Private Functions
  1186. * @{
  1187. */
  1188. /**
  1189. * @brief DMA I2S transmit process complete callback
  1190. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1191. * the configuration information for the specified DMA module.
  1192. * @retval None
  1193. */
  1194. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1195. {
  1196. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1197. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1198. {
  1199. hi2s->TxXferCount = 0;
  1200. /* Disable Tx DMA Request */
  1201. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1202. if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  1203. {
  1204. if(hi2s->RxXferCount == 0)
  1205. {
  1206. hi2s->State = HAL_I2S_STATE_READY;
  1207. }
  1208. }
  1209. else
  1210. {
  1211. hi2s->State = HAL_I2S_STATE_READY;
  1212. }
  1213. }
  1214. HAL_I2S_TxCpltCallback(hi2s);
  1215. }
  1216. /**
  1217. * @brief DMA I2S transmit process half complete callback
  1218. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1219. * the configuration information for the specified DMA module.
  1220. * @retval None
  1221. */
  1222. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1223. {
  1224. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1225. HAL_I2S_TxHalfCpltCallback(hi2s);
  1226. }
  1227. /**
  1228. * @brief DMA I2S receive process complete callback
  1229. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1230. * the configuration information for the specified DMA module.
  1231. * @retval None
  1232. */
  1233. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1234. {
  1235. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1236. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1237. {
  1238. /* Disable Rx DMA Request */
  1239. hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1240. hi2s->RxXferCount = 0;
  1241. if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  1242. {
  1243. if(hi2s->TxXferCount == 0)
  1244. {
  1245. hi2s->State = HAL_I2S_STATE_READY;
  1246. }
  1247. }
  1248. else
  1249. {
  1250. hi2s->State = HAL_I2S_STATE_READY;
  1251. }
  1252. }
  1253. HAL_I2S_RxCpltCallback(hi2s);
  1254. }
  1255. /**
  1256. * @brief DMA I2S receive process half complete callback
  1257. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1258. * the configuration information for the specified DMA module.
  1259. * @retval None
  1260. */
  1261. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1262. {
  1263. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1264. HAL_I2S_RxHalfCpltCallback(hi2s);
  1265. }
  1266. /**
  1267. * @brief DMA I2S communication error callback
  1268. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1269. * the configuration information for the specified DMA module.
  1270. * @retval None
  1271. */
  1272. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1273. {
  1274. I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1275. /* Disable Rx and Tx DMA Request */
  1276. hi2s->Instance->CR2 &= (uint32_t)(~(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1277. hi2s->TxXferCount = 0;
  1278. hi2s->RxXferCount = 0;
  1279. hi2s->State= HAL_I2S_STATE_READY;
  1280. /* Set the error code and execute error callback*/
  1281. hi2s->ErrorCode |= HAL_I2S_ERROR_DMA;
  1282. HAL_I2S_ErrorCallback(hi2s);
  1283. }
  1284. /**
  1285. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1286. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1287. * the configuration information for I2S module
  1288. * @retval None
  1289. */
  1290. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1291. {
  1292. /* Transmit data */
  1293. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  1294. hi2s->TxXferCount--;
  1295. if(hi2s->TxXferCount == 0)
  1296. {
  1297. /* Disable TXE and ERR interrupt */
  1298. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1299. hi2s->State = HAL_I2S_STATE_READY;
  1300. HAL_I2S_TxCpltCallback(hi2s);
  1301. }
  1302. }
  1303. /**
  1304. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1305. * @param hi2s: I2S handle
  1306. * @retval None
  1307. */
  1308. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1309. {
  1310. /* Receive data */
  1311. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  1312. hi2s->RxXferCount--;
  1313. if(hi2s->RxXferCount == 0)
  1314. {
  1315. /* Disable RXNE and ERR interrupt */
  1316. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1317. hi2s->State = HAL_I2S_STATE_READY;
  1318. HAL_I2S_RxCpltCallback(hi2s);
  1319. }
  1320. }
  1321. /**
  1322. * @}
  1323. */
  1324. #endif /* HAL_I2S_MODULE_ENABLED */
  1325. /**
  1326. * @}
  1327. */
  1328. /**
  1329. * @}
  1330. */
  1331. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/