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.
 
 
 

1772 lines
57 KiB

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