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.
 
 
 

1557 lines
50 KiB

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