STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_hal_i2s.c 00004 * @author MCD Application Team 00005 * @brief I2S HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Integrated Interchip Sound (I2S) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral State and Errors functions 00011 @verbatim 00012 =============================================================================== 00013 ##### How to use this driver ##### 00014 =============================================================================== 00015 [..] 00016 The I2S HAL driver can be used as follow: 00017 00018 (#) Declare a I2S_HandleTypeDef handle structure. 00019 (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API: 00020 (##) Enable the SPIx interface clock. 00021 (##) I2S pins configuration: 00022 (+++) Enable the clock for the I2S GPIOs. 00023 (+++) Configure these I2S pins as alternate function pull-up. 00024 (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT() 00025 and HAL_I2S_Receive_IT() APIs). 00026 (+++) Configure the I2Sx interrupt priority. 00027 (+++) Enable the NVIC I2S IRQ handle. 00028 (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA() 00029 and HAL_I2S_Receive_DMA() APIs: 00030 (+++) Declare a DMA handle structure for the Tx/Rx Stream/Channel. 00031 (+++) Enable the DMAx interface clock. 00032 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. 00033 (+++) Configure the DMA Tx/Rx Stream/Channel. 00034 (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle. 00035 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the 00036 DMA Tx/Rx Stream/Channel. 00037 00038 (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity 00039 using HAL_I2S_Init() function. 00040 00041 -@- The specific I2S interrupts (Transmission complete interrupt, 00042 RXNE interrupt and Error Interrupts) will be managed using the macros 00043 __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process. 00044 -@- The I2SxCLK source is the system clock (provided by the HSI, the HSE or the PLL, and sourcing the AHB clock). 00045 For connectivity line devices, the I2SxCLK source can be either SYSCLK or the PLL3 VCO (2 x PLL3CLK) clock 00046 in order to achieve the maximum accuracy. 00047 -@- Make sure that either: 00048 (+@) External clock source is configured after setting correctly 00049 the define constant HSE_VALUE in the stm32f1xx_hal_conf.h file. 00050 00051 (#) Three mode of operations are available within this driver : 00052 00053 *** Polling mode IO operation *** 00054 ================================= 00055 [..] 00056 (+) Send an amount of data in blocking mode using HAL_I2S_Transmit() 00057 (+) Receive an amount of data in blocking mode using HAL_I2S_Receive() 00058 00059 *** Interrupt mode IO operation *** 00060 =================================== 00061 [..] 00062 (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT() 00063 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can 00064 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback 00065 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can 00066 add his own code by customization of function pointer HAL_I2S_TxCpltCallback 00067 (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT() 00068 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can 00069 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback 00070 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can 00071 add his own code by customization of function pointer HAL_I2S_RxCpltCallback 00072 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can 00073 add his own code by customization of function pointer HAL_I2S_ErrorCallback 00074 00075 *** DMA mode IO operation *** 00076 ============================== 00077 [..] 00078 (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA() 00079 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can 00080 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback 00081 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can 00082 add his own code by customization of function pointer HAL_I2S_TxCpltCallback 00083 (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA() 00084 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can 00085 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback 00086 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can 00087 add his own code by customization of function pointer HAL_I2S_RxCpltCallback 00088 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can 00089 add his own code by customization of function pointer HAL_I2S_ErrorCallback 00090 (+) Pause the DMA Transfer using HAL_I2S_DMAPause() 00091 (+) Resume the DMA Transfer using HAL_I2S_DMAResume() 00092 (+) Stop the DMA Transfer using HAL_I2S_DMAStop() 00093 In Slave mode, if HAL_I2S_DMAStop is used to stop the communication, an error 00094 HAL_I2S_ERROR_BUSY_LINE_RX is raised as the master continue to transmit data. 00095 In this case __HAL_I2S_FLUSH_RX_DR macro must be used to flush the remaining data 00096 inside DR register and avoid using DeInit/Init process for the next transfer. 00097 00098 *** I2S HAL driver macros list *** 00099 =================================== 00100 [..] 00101 Below the list of most used macros in I2S HAL driver. 00102 00103 (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode) 00104 (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode) 00105 (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts 00106 (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts 00107 (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not 00108 (+) __HAL_I2S_FLUSH_RX_DR: Read DR Register to Flush RX Data 00109 00110 [..] 00111 (@) You can refer to the I2S HAL driver header file for more useful macros 00112 00113 *** I2S HAL driver macros list *** 00114 =================================== 00115 [..] 00116 Callback registration: 00117 00118 (#) The compilation flag USE_HAL_I2S_REGISTER_CALLBACKS when set to 1U 00119 allows the user to configure dynamically the driver callbacks. 00120 Use Functions HAL_I2S_RegisterCallback() to register an interrupt callback. 00121 00122 Function HAL_I2S_RegisterCallback() allows to register following callbacks: 00123 (++) TxCpltCallback : I2S Tx Completed callback 00124 (++) RxCpltCallback : I2S Rx Completed callback 00125 (++) TxHalfCpltCallback : I2S Tx Half Completed callback 00126 (++) RxHalfCpltCallback : I2S Rx Half Completed callback 00127 (++) ErrorCallback : I2S Error callback 00128 (++) MspInitCallback : I2S Msp Init callback 00129 (++) MspDeInitCallback : I2S Msp DeInit callback 00130 This function takes as parameters the HAL peripheral handle, the Callback ID 00131 and a pointer to the user callback function. 00132 00133 00134 (#) Use function HAL_I2S_UnRegisterCallback to reset a callback to the default 00135 weak function. 00136 HAL_I2S_UnRegisterCallback takes as parameters the HAL peripheral handle, 00137 and the Callback ID. 00138 This function allows to reset following callbacks: 00139 (++) TxCpltCallback : I2S Tx Completed callback 00140 (++) RxCpltCallback : I2S Rx Completed callback 00141 (++) TxHalfCpltCallback : I2S Tx Half Completed callback 00142 (++) RxHalfCpltCallback : I2S Rx Half Completed callback 00143 (++) ErrorCallback : I2S Error callback 00144 (++) MspInitCallback : I2S Msp Init callback 00145 (++) MspDeInitCallback : I2S Msp DeInit callback 00146 00147 [..] 00148 By default, after the HAL_I2S_Init() and when the state is HAL_I2S_STATE_RESET 00149 all callbacks are set to the corresponding weak functions: 00150 examples HAL_I2S_MasterTxCpltCallback(), HAL_I2S_MasterRxCpltCallback(). 00151 Exception done for MspInit and MspDeInit functions that are 00152 reset to the legacy weak functions in the HAL_I2S_Init()/ HAL_I2S_DeInit() only when 00153 these callbacks are null (not registered beforehand). 00154 If MspInit or MspDeInit are not null, the HAL_I2S_Init()/ HAL_I2S_DeInit() 00155 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. 00156 00157 [..] 00158 Callbacks can be registered/unregistered in HAL_I2S_STATE_READY state only. 00159 Exception done MspInit/MspDeInit functions that can be registered/unregistered 00160 in HAL_I2S_STATE_READY or HAL_I2S_STATE_RESET state, 00161 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00162 Then, the user first registers the MspInit/MspDeInit user callbacks 00163 using HAL_I2S_RegisterCallback() before calling HAL_I2S_DeInit() 00164 or HAL_I2S_Init() function. 00165 00166 [..] 00167 When the compilation define USE_HAL_I2S_REGISTER_CALLBACKS is set to 0 or 00168 not defined, the callback registering feature is not available 00169 and weak (surcharged) callbacks are used. 00170 00171 *** I2S Workarounds linked to Silicon Limitation *** 00172 ==================================================== 00173 [..] 00174 (@) Only the 16-bit mode with no data extension can be used when the I2S 00175 is in Master and used the PCM long synchronization mode. 00176 00177 @endverbatim 00178 ****************************************************************************** 00179 * @attention 00180 * 00181 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00182 * All rights reserved.</center></h2> 00183 * 00184 * This software component is licensed by ST under BSD 3-Clause license, 00185 * the "License"; You may not use this file except in compliance with the 00186 * License. You may obtain a copy of the License at: 00187 * opensource.org/licenses/BSD-3-Clause 00188 * 00189 ****************************************************************************** 00190 */ 00191 00192 /* Includes ------------------------------------------------------------------*/ 00193 #include "stm32f1xx_hal.h" 00194 00195 #ifdef HAL_I2S_MODULE_ENABLED 00196 00197 #if defined(SPI_I2S_SUPPORT) 00198 /** @addtogroup STM32F1xx_HAL_Driver 00199 * @{ 00200 */ 00201 00202 /** @defgroup I2S I2S 00203 * @brief I2S HAL module driver 00204 * @{ 00205 */ 00206 00207 /* Private typedef -----------------------------------------------------------*/ 00208 /* Private define ------------------------------------------------------------*/ 00209 #define I2S_TIMEOUT_FLAG 100U /*!< Timeout 100 ms */ 00210 /* Private macro -------------------------------------------------------------*/ 00211 /* Private variables ---------------------------------------------------------*/ 00212 /* Private function prototypes -----------------------------------------------*/ 00213 /** @defgroup I2S_Private_Functions I2S Private Functions 00214 * @{ 00215 */ 00216 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma); 00217 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00218 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma); 00219 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00220 static void I2S_DMAError(DMA_HandleTypeDef *hdma); 00221 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s); 00222 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s); 00223 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State, 00224 uint32_t Timeout); 00225 /** 00226 * @} 00227 */ 00228 00229 /* Exported functions ---------------------------------------------------------*/ 00230 00231 /** @defgroup I2S_Exported_Functions I2S Exported Functions 00232 * @{ 00233 */ 00234 00235 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions 00236 * @brief Initialization and Configuration functions 00237 * 00238 @verbatim 00239 =============================================================================== 00240 ##### Initialization and de-initialization functions ##### 00241 =============================================================================== 00242 [..] This subsection provides a set of functions allowing to initialize and 00243 de-initialize the I2Sx peripheral in simplex mode: 00244 00245 (+) User must Implement HAL_I2S_MspInit() function in which he configures 00246 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ). 00247 00248 (+) Call the function HAL_I2S_Init() to configure the selected device with 00249 the selected configuration: 00250 (++) Mode 00251 (++) Standard 00252 (++) Data Format 00253 (++) MCLK Output 00254 (++) Audio frequency 00255 (++) Polarity 00256 00257 (+) Call the function HAL_I2S_DeInit() to restore the default configuration 00258 of the selected I2Sx peripheral. 00259 @endverbatim 00260 * @{ 00261 */ 00262 00263 /** 00264 * @brief Initializes the I2S according to the specified parameters 00265 * in the I2S_InitTypeDef and create the associated handle. 00266 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00267 * the configuration information for I2S module 00268 * @retval HAL status 00269 */ 00270 HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s) 00271 { 00272 uint32_t i2sdiv; 00273 uint32_t i2sodd; 00274 uint32_t packetlength; 00275 uint32_t tmp; 00276 uint32_t i2sclk; 00277 00278 /* Check the I2S handle allocation */ 00279 if (hi2s == NULL) 00280 { 00281 return HAL_ERROR; 00282 } 00283 00284 /* Check the I2S parameters */ 00285 assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance)); 00286 assert_param(IS_I2S_MODE(hi2s->Init.Mode)); 00287 assert_param(IS_I2S_STANDARD(hi2s->Init.Standard)); 00288 assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat)); 00289 assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput)); 00290 assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq)); 00291 assert_param(IS_I2S_CPOL(hi2s->Init.CPOL)); 00292 00293 if (hi2s->State == HAL_I2S_STATE_RESET) 00294 { 00295 /* Allocate lock resource and initialize it */ 00296 hi2s->Lock = HAL_UNLOCKED; 00297 00298 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 00299 /* Init the I2S Callback settings */ 00300 hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00301 hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00302 hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 00303 hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 00304 hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */ 00305 00306 if (hi2s->MspInitCallback == NULL) 00307 { 00308 hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ 00309 } 00310 00311 /* Init the low level hardware : GPIO, CLOCK, NVIC... */ 00312 hi2s->MspInitCallback(hi2s); 00313 #else 00314 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00315 HAL_I2S_MspInit(hi2s); 00316 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 00317 } 00318 00319 hi2s->State = HAL_I2S_STATE_BUSY; 00320 00321 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/ 00322 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */ 00323 CLEAR_BIT(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \ 00324 SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \ 00325 SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD)); 00326 hi2s->Instance->I2SPR = 0x0002U; 00327 00328 /*----------------------- I2SPR: I2SDIV and ODD Calculation -----------------*/ 00329 /* If the requested audio frequency is not the default, compute the prescaler */ 00330 if (hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT) 00331 { 00332 /* Check the frame length (For the Prescaler computing) ********************/ 00333 if (hi2s->Init.DataFormat == I2S_DATAFORMAT_16B) 00334 { 00335 /* Packet length is 16 bits */ 00336 packetlength = 16U; 00337 } 00338 else 00339 { 00340 /* Packet length is 32 bits */ 00341 packetlength = 32U; 00342 } 00343 00344 /* I2S standard */ 00345 if (hi2s->Init.Standard <= I2S_STANDARD_LSB) 00346 { 00347 /* In I2S standard packet length is multiplied by 2 */ 00348 packetlength = packetlength * 2U; 00349 } 00350 00351 /* Get the source clock value **********************************************/ 00352 if (hi2s->Instance == SPI2) 00353 { 00354 /* Get the source clock value: based on SPI2 Instance */ 00355 i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S2); 00356 } 00357 else if (hi2s->Instance == SPI3) 00358 { 00359 /* Get the source clock value: based on SPI3 Instance */ 00360 i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S3); 00361 } 00362 else 00363 { 00364 /* Get the source clock value: based on System Clock value */ 00365 i2sclk = HAL_RCC_GetSysClockFreq(); 00366 } 00367 /* Compute the Real divider depending on the MCLK output state, with a floating point */ 00368 if (hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE) 00369 { 00370 /* MCLK output is enabled */ 00371 if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B) 00372 { 00373 tmp = (uint32_t)(((((i2sclk / (packetlength * 4U)) * 10U) / hi2s->Init.AudioFreq)) + 5U); 00374 } 00375 else 00376 { 00377 tmp = (uint32_t)(((((i2sclk / (packetlength * 8U)) * 10U) / hi2s->Init.AudioFreq)) + 5U); 00378 } 00379 } 00380 else 00381 { 00382 /* MCLK output is disabled */ 00383 tmp = (uint32_t)(((((i2sclk / packetlength) * 10U) / hi2s->Init.AudioFreq)) + 5U); 00384 } 00385 00386 /* Remove the flatting point */ 00387 tmp = tmp / 10U; 00388 00389 /* Check the parity of the divider */ 00390 i2sodd = (uint32_t)(tmp & (uint32_t)1U); 00391 00392 /* Compute the i2sdiv prescaler */ 00393 i2sdiv = (uint32_t)((tmp - i2sodd) / 2U); 00394 00395 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */ 00396 i2sodd = (uint32_t)(i2sodd << 8U); 00397 } 00398 else 00399 { 00400 /* Set the default values */ 00401 i2sdiv = 2U; 00402 i2sodd = 0U; 00403 } 00404 00405 /* Test if the divider is 1 or 0 or greater than 0xFF */ 00406 if ((i2sdiv < 2U) || (i2sdiv > 0xFFU)) 00407 { 00408 /* Set the error code and execute error callback*/ 00409 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER); 00410 return HAL_ERROR; 00411 } 00412 00413 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/ 00414 00415 /* Write to SPIx I2SPR register the computed value */ 00416 hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput)); 00417 00418 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */ 00419 /* And configure the I2S with the I2S_InitStruct values */ 00420 MODIFY_REG(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \ 00421 SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \ 00422 SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \ 00423 SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD), \ 00424 (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode | \ 00425 hi2s->Init.Standard | hi2s->Init.DataFormat | \ 00426 hi2s->Init.CPOL)); 00427 00428 #if defined(SPI_I2SCFGR_ASTRTEN) 00429 if ((hi2s->Init.Standard == I2S_STANDARD_PCM_SHORT) || ((hi2s->Init.Standard == I2S_STANDARD_PCM_LONG))) 00430 { 00431 /* Write to SPIx I2SCFGR */ 00432 SET_BIT(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_ASTRTEN); 00433 } 00434 #endif /* SPI_I2SCFGR_ASTRTEN */ 00435 00436 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 00437 hi2s->State = HAL_I2S_STATE_READY; 00438 00439 return HAL_OK; 00440 } 00441 00442 /** 00443 * @brief DeInitializes the I2S peripheral 00444 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00445 * the configuration information for I2S module 00446 * @retval HAL status 00447 */ 00448 HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s) 00449 { 00450 /* Check the I2S handle allocation */ 00451 if (hi2s == NULL) 00452 { 00453 return HAL_ERROR; 00454 } 00455 00456 /* Check the parameters */ 00457 assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance)); 00458 00459 hi2s->State = HAL_I2S_STATE_BUSY; 00460 00461 /* Disable the I2S Peripheral Clock */ 00462 __HAL_I2S_DISABLE(hi2s); 00463 00464 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 00465 if (hi2s->MspDeInitCallback == NULL) 00466 { 00467 hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ 00468 } 00469 00470 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ 00471 hi2s->MspDeInitCallback(hi2s); 00472 #else 00473 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ 00474 HAL_I2S_MspDeInit(hi2s); 00475 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 00476 00477 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 00478 hi2s->State = HAL_I2S_STATE_RESET; 00479 00480 /* Release Lock */ 00481 __HAL_UNLOCK(hi2s); 00482 00483 return HAL_OK; 00484 } 00485 00486 /** 00487 * @brief I2S MSP Init 00488 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00489 * the configuration information for I2S module 00490 * @retval None 00491 */ 00492 __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s) 00493 { 00494 /* Prevent unused argument(s) compilation warning */ 00495 UNUSED(hi2s); 00496 00497 /* NOTE : This function Should not be modified, when the callback is needed, 00498 the HAL_I2S_MspInit could be implemented in the user file 00499 */ 00500 } 00501 00502 /** 00503 * @brief I2S MSP DeInit 00504 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00505 * the configuration information for I2S module 00506 * @retval None 00507 */ 00508 __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s) 00509 { 00510 /* Prevent unused argument(s) compilation warning */ 00511 UNUSED(hi2s); 00512 00513 /* NOTE : This function Should not be modified, when the callback is needed, 00514 the HAL_I2S_MspDeInit could be implemented in the user file 00515 */ 00516 } 00517 00518 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 00519 /** 00520 * @brief Register a User I2S Callback 00521 * To be used instead of the weak predefined callback 00522 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains 00523 * the configuration information for the specified I2S. 00524 * @param CallbackID ID of the callback to be registered 00525 * @param pCallback pointer to the Callback function 00526 * @retval HAL status 00527 */ 00528 HAL_StatusTypeDef HAL_I2S_RegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID, 00529 pI2S_CallbackTypeDef pCallback) 00530 { 00531 HAL_StatusTypeDef status = HAL_OK; 00532 00533 if (pCallback == NULL) 00534 { 00535 /* Update the error code */ 00536 hi2s->ErrorCode |= HAL_I2S_ERROR_INVALID_CALLBACK; 00537 00538 return HAL_ERROR; 00539 } 00540 /* Process locked */ 00541 __HAL_LOCK(hi2s); 00542 00543 if (HAL_I2S_STATE_READY == hi2s->State) 00544 { 00545 switch (CallbackID) 00546 { 00547 case HAL_I2S_TX_COMPLETE_CB_ID : 00548 hi2s->TxCpltCallback = pCallback; 00549 break; 00550 00551 case HAL_I2S_RX_COMPLETE_CB_ID : 00552 hi2s->RxCpltCallback = pCallback; 00553 break; 00554 00555 case HAL_I2S_TX_HALF_COMPLETE_CB_ID : 00556 hi2s->TxHalfCpltCallback = pCallback; 00557 break; 00558 00559 case HAL_I2S_RX_HALF_COMPLETE_CB_ID : 00560 hi2s->RxHalfCpltCallback = pCallback; 00561 break; 00562 00563 case HAL_I2S_ERROR_CB_ID : 00564 hi2s->ErrorCallback = pCallback; 00565 break; 00566 00567 case HAL_I2S_MSPINIT_CB_ID : 00568 hi2s->MspInitCallback = pCallback; 00569 break; 00570 00571 case HAL_I2S_MSPDEINIT_CB_ID : 00572 hi2s->MspDeInitCallback = pCallback; 00573 break; 00574 00575 default : 00576 /* Update the error code */ 00577 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00578 00579 /* Return error status */ 00580 status = HAL_ERROR; 00581 break; 00582 } 00583 } 00584 else if (HAL_I2S_STATE_RESET == hi2s->State) 00585 { 00586 switch (CallbackID) 00587 { 00588 case HAL_I2S_MSPINIT_CB_ID : 00589 hi2s->MspInitCallback = pCallback; 00590 break; 00591 00592 case HAL_I2S_MSPDEINIT_CB_ID : 00593 hi2s->MspDeInitCallback = pCallback; 00594 break; 00595 00596 default : 00597 /* Update the error code */ 00598 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00599 00600 /* Return error status */ 00601 status = HAL_ERROR; 00602 break; 00603 } 00604 } 00605 else 00606 { 00607 /* Update the error code */ 00608 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00609 00610 /* Return error status */ 00611 status = HAL_ERROR; 00612 } 00613 00614 /* Release Lock */ 00615 __HAL_UNLOCK(hi2s); 00616 return status; 00617 } 00618 00619 /** 00620 * @brief Unregister an I2S Callback 00621 * I2S callback is redirected to the weak predefined callback 00622 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains 00623 * the configuration information for the specified I2S. 00624 * @param CallbackID ID of the callback to be unregistered 00625 * @retval HAL status 00626 */ 00627 HAL_StatusTypeDef HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID) 00628 { 00629 HAL_StatusTypeDef status = HAL_OK; 00630 00631 /* Process locked */ 00632 __HAL_LOCK(hi2s); 00633 00634 if (HAL_I2S_STATE_READY == hi2s->State) 00635 { 00636 switch (CallbackID) 00637 { 00638 case HAL_I2S_TX_COMPLETE_CB_ID : 00639 hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00640 break; 00641 00642 case HAL_I2S_RX_COMPLETE_CB_ID : 00643 hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00644 break; 00645 00646 case HAL_I2S_TX_HALF_COMPLETE_CB_ID : 00647 hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 00648 break; 00649 00650 case HAL_I2S_RX_HALF_COMPLETE_CB_ID : 00651 hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 00652 break; 00653 00654 case HAL_I2S_ERROR_CB_ID : 00655 hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */ 00656 break; 00657 00658 case HAL_I2S_MSPINIT_CB_ID : 00659 hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ 00660 break; 00661 00662 case HAL_I2S_MSPDEINIT_CB_ID : 00663 hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ 00664 break; 00665 00666 default : 00667 /* Update the error code */ 00668 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00669 00670 /* Return error status */ 00671 status = HAL_ERROR; 00672 break; 00673 } 00674 } 00675 else if (HAL_I2S_STATE_RESET == hi2s->State) 00676 { 00677 switch (CallbackID) 00678 { 00679 case HAL_I2S_MSPINIT_CB_ID : 00680 hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ 00681 break; 00682 00683 case HAL_I2S_MSPDEINIT_CB_ID : 00684 hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ 00685 break; 00686 00687 default : 00688 /* Update the error code */ 00689 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00690 00691 /* Return error status */ 00692 status = HAL_ERROR; 00693 break; 00694 } 00695 } 00696 else 00697 { 00698 /* Update the error code */ 00699 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); 00700 00701 /* Return error status */ 00702 status = HAL_ERROR; 00703 } 00704 00705 /* Release Lock */ 00706 __HAL_UNLOCK(hi2s); 00707 return status; 00708 } 00709 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 00710 /** 00711 * @} 00712 */ 00713 00714 /** @defgroup I2S_Exported_Functions_Group2 IO operation functions 00715 * @brief Data transfers functions 00716 * 00717 @verbatim 00718 =============================================================================== 00719 ##### IO operation functions ##### 00720 =============================================================================== 00721 [..] 00722 This subsection provides a set of functions allowing to manage the I2S data 00723 transfers. 00724 00725 (#) There are two modes of transfer: 00726 (++) Blocking mode : The communication is performed in the polling mode. 00727 The status of all data processing is returned by the same function 00728 after finishing transfer. 00729 (++) No-Blocking mode : The communication is performed using Interrupts 00730 or DMA. These functions return the status of the transfer startup. 00731 The end of the data processing will be indicated through the 00732 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when 00733 using DMA mode. 00734 00735 (#) Blocking mode functions are : 00736 (++) HAL_I2S_Transmit() 00737 (++) HAL_I2S_Receive() 00738 00739 (#) No-Blocking mode functions with Interrupt are : 00740 (++) HAL_I2S_Transmit_IT() 00741 (++) HAL_I2S_Receive_IT() 00742 00743 (#) No-Blocking mode functions with DMA are : 00744 (++) HAL_I2S_Transmit_DMA() 00745 (++) HAL_I2S_Receive_DMA() 00746 00747 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: 00748 (++) HAL_I2S_TxCpltCallback() 00749 (++) HAL_I2S_RxCpltCallback() 00750 (++) HAL_I2S_ErrorCallback() 00751 00752 @endverbatim 00753 * @{ 00754 */ 00755 00756 /** 00757 * @brief Transmit an amount of data in blocking mode 00758 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00759 * the configuration information for I2S module 00760 * @param pData a 16-bit pointer to data buffer. 00761 * @param Size number of data sample to be sent: 00762 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 00763 * configuration phase, the Size parameter means the number of 16-bit data length 00764 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 00765 * the Size parameter means the number of 24-bit or 32-bit data length. 00766 * @param Timeout Timeout duration 00767 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 00768 * between Master and Slave(example: audio streaming). 00769 * @retval HAL status 00770 */ 00771 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) 00772 { 00773 uint32_t tmpreg_cfgr; 00774 00775 if ((pData == NULL) || (Size == 0U)) 00776 { 00777 return HAL_ERROR; 00778 } 00779 00780 /* Process Locked */ 00781 __HAL_LOCK(hi2s); 00782 00783 if (hi2s->State != HAL_I2S_STATE_READY) 00784 { 00785 __HAL_UNLOCK(hi2s); 00786 return HAL_BUSY; 00787 } 00788 00789 /* Set state and reset error code */ 00790 hi2s->State = HAL_I2S_STATE_BUSY_TX; 00791 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 00792 hi2s->pTxBuffPtr = pData; 00793 00794 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 00795 00796 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 00797 { 00798 hi2s->TxXferSize = (Size << 1U); 00799 hi2s->TxXferCount = (Size << 1U); 00800 } 00801 else 00802 { 00803 hi2s->TxXferSize = Size; 00804 hi2s->TxXferCount = Size; 00805 } 00806 00807 tmpreg_cfgr = hi2s->Instance->I2SCFGR; 00808 00809 /* Check if the I2S is already enabled */ 00810 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) 00811 { 00812 /* Enable I2S peripheral */ 00813 __HAL_I2S_ENABLE(hi2s); 00814 } 00815 00816 /* Wait until TXE flag is set */ 00817 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK) 00818 { 00819 /* Set the error code */ 00820 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 00821 hi2s->State = HAL_I2S_STATE_READY; 00822 __HAL_UNLOCK(hi2s); 00823 return HAL_ERROR; 00824 } 00825 00826 while (hi2s->TxXferCount > 0U) 00827 { 00828 hi2s->Instance->DR = (*hi2s->pTxBuffPtr); 00829 hi2s->pTxBuffPtr++; 00830 hi2s->TxXferCount--; 00831 00832 /* Wait until TXE flag is set */ 00833 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK) 00834 { 00835 /* Set the error code */ 00836 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 00837 hi2s->State = HAL_I2S_STATE_READY; 00838 __HAL_UNLOCK(hi2s); 00839 return HAL_ERROR; 00840 } 00841 00842 /* Check if an underrun occurs */ 00843 if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) 00844 { 00845 /* Clear underrun flag */ 00846 __HAL_I2S_CLEAR_UDRFLAG(hi2s); 00847 00848 /* Set the error code */ 00849 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); 00850 } 00851 } 00852 00853 /* Check if Slave mode is selected */ 00854 if (((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX) 00855 || ((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_RX)) 00856 { 00857 /* Wait until Busy flag is reset */ 00858 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK) 00859 { 00860 /* Set the error code */ 00861 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 00862 hi2s->State = HAL_I2S_STATE_READY; 00863 __HAL_UNLOCK(hi2s); 00864 return HAL_ERROR; 00865 } 00866 } 00867 00868 hi2s->State = HAL_I2S_STATE_READY; 00869 __HAL_UNLOCK(hi2s); 00870 return HAL_OK; 00871 } 00872 00873 /** 00874 * @brief Receive an amount of data in blocking mode 00875 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00876 * the configuration information for I2S module 00877 * @param pData a 16-bit pointer to data buffer. 00878 * @param Size number of data sample to be sent: 00879 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 00880 * configuration phase, the Size parameter means the number of 16-bit data length 00881 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 00882 * the Size parameter means the number of 24-bit or 32-bit data length. 00883 * @param Timeout Timeout duration 00884 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 00885 * between Master and Slave(example: audio streaming). 00886 * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate 00887 * in continuous way and as the I2S is not disabled at the end of the I2S transaction. 00888 * @retval HAL status 00889 */ 00890 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) 00891 { 00892 uint32_t tmpreg_cfgr; 00893 00894 if ((pData == NULL) || (Size == 0U)) 00895 { 00896 return HAL_ERROR; 00897 } 00898 00899 /* Process Locked */ 00900 __HAL_LOCK(hi2s); 00901 00902 if (hi2s->State != HAL_I2S_STATE_READY) 00903 { 00904 __HAL_UNLOCK(hi2s); 00905 return HAL_BUSY; 00906 } 00907 00908 /* Set state and reset error code */ 00909 hi2s->State = HAL_I2S_STATE_BUSY_RX; 00910 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 00911 hi2s->pRxBuffPtr = pData; 00912 00913 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 00914 00915 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 00916 { 00917 hi2s->RxXferSize = (Size << 1U); 00918 hi2s->RxXferCount = (Size << 1U); 00919 } 00920 else 00921 { 00922 hi2s->RxXferSize = Size; 00923 hi2s->RxXferCount = Size; 00924 } 00925 00926 /* Check if the I2S is already enabled */ 00927 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) 00928 { 00929 /* Enable I2S peripheral */ 00930 __HAL_I2S_ENABLE(hi2s); 00931 } 00932 00933 /* Check if Master Receiver mode is selected */ 00934 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) 00935 { 00936 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read 00937 access to the SPI_SR register. */ 00938 __HAL_I2S_CLEAR_OVRFLAG(hi2s); 00939 } 00940 00941 /* Receive data */ 00942 while (hi2s->RxXferCount > 0U) 00943 { 00944 /* Wait until RXNE flag is set */ 00945 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK) 00946 { 00947 /* Set the error code */ 00948 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 00949 hi2s->State = HAL_I2S_STATE_READY; 00950 __HAL_UNLOCK(hi2s); 00951 return HAL_ERROR; 00952 } 00953 00954 (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR; 00955 hi2s->pRxBuffPtr++; 00956 hi2s->RxXferCount--; 00957 00958 /* Check if an overrun occurs */ 00959 if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET) 00960 { 00961 /* Clear overrun flag */ 00962 __HAL_I2S_CLEAR_OVRFLAG(hi2s); 00963 00964 /* Set the error code */ 00965 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR); 00966 } 00967 } 00968 00969 hi2s->State = HAL_I2S_STATE_READY; 00970 __HAL_UNLOCK(hi2s); 00971 return HAL_OK; 00972 } 00973 00974 /** 00975 * @brief Transmit an amount of data in non-blocking mode with Interrupt 00976 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 00977 * the configuration information for I2S module 00978 * @param pData a 16-bit pointer to data buffer. 00979 * @param Size number of data sample to be sent: 00980 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 00981 * configuration phase, the Size parameter means the number of 16-bit data length 00982 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 00983 * the Size parameter means the number of 24-bit or 32-bit data length. 00984 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 00985 * between Master and Slave(example: audio streaming). 00986 * @retval HAL status 00987 */ 00988 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) 00989 { 00990 uint32_t tmpreg_cfgr; 00991 00992 if ((pData == NULL) || (Size == 0U)) 00993 { 00994 return HAL_ERROR; 00995 } 00996 00997 /* Process Locked */ 00998 __HAL_LOCK(hi2s); 00999 01000 if (hi2s->State != HAL_I2S_STATE_READY) 01001 { 01002 __HAL_UNLOCK(hi2s); 01003 return HAL_BUSY; 01004 } 01005 01006 /* Set state and reset error code */ 01007 hi2s->State = HAL_I2S_STATE_BUSY_TX; 01008 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 01009 hi2s->pTxBuffPtr = pData; 01010 01011 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 01012 01013 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 01014 { 01015 hi2s->TxXferSize = (Size << 1U); 01016 hi2s->TxXferCount = (Size << 1U); 01017 } 01018 else 01019 { 01020 hi2s->TxXferSize = Size; 01021 hi2s->TxXferCount = Size; 01022 } 01023 01024 /* Enable TXE and ERR interrupt */ 01025 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); 01026 01027 /* Check if the I2S is already enabled */ 01028 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) 01029 { 01030 /* Enable I2S peripheral */ 01031 __HAL_I2S_ENABLE(hi2s); 01032 } 01033 01034 __HAL_UNLOCK(hi2s); 01035 return HAL_OK; 01036 } 01037 01038 /** 01039 * @brief Receive an amount of data in non-blocking mode with Interrupt 01040 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01041 * the configuration information for I2S module 01042 * @param pData a 16-bit pointer to the Receive data buffer. 01043 * @param Size number of data sample to be sent: 01044 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 01045 * configuration phase, the Size parameter means the number of 16-bit data length 01046 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 01047 * the Size parameter means the number of 24-bit or 32-bit data length. 01048 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 01049 * between Master and Slave(example: audio streaming). 01050 * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization 01051 * between Master and Slave otherwise the I2S interrupt should be optimized. 01052 * @retval HAL status 01053 */ 01054 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) 01055 { 01056 uint32_t tmpreg_cfgr; 01057 01058 if ((pData == NULL) || (Size == 0U)) 01059 { 01060 return HAL_ERROR; 01061 } 01062 01063 /* Process Locked */ 01064 __HAL_LOCK(hi2s); 01065 01066 if (hi2s->State != HAL_I2S_STATE_READY) 01067 { 01068 __HAL_UNLOCK(hi2s); 01069 return HAL_BUSY; 01070 } 01071 01072 /* Set state and reset error code */ 01073 hi2s->State = HAL_I2S_STATE_BUSY_RX; 01074 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 01075 hi2s->pRxBuffPtr = pData; 01076 01077 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 01078 01079 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 01080 { 01081 hi2s->RxXferSize = (Size << 1U); 01082 hi2s->RxXferCount = (Size << 1U); 01083 } 01084 else 01085 { 01086 hi2s->RxXferSize = Size; 01087 hi2s->RxXferCount = Size; 01088 } 01089 01090 /* Enable RXNE and ERR interrupt */ 01091 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); 01092 01093 /* Check if the I2S is already enabled */ 01094 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) 01095 { 01096 /* Enable I2S peripheral */ 01097 __HAL_I2S_ENABLE(hi2s); 01098 } 01099 01100 __HAL_UNLOCK(hi2s); 01101 return HAL_OK; 01102 } 01103 01104 /** 01105 * @brief Transmit an amount of data in non-blocking mode with DMA 01106 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01107 * the configuration information for I2S module 01108 * @param pData a 16-bit pointer to the Transmit data buffer. 01109 * @param Size number of data sample to be sent: 01110 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 01111 * configuration phase, the Size parameter means the number of 16-bit data length 01112 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 01113 * the Size parameter means the number of 24-bit or 32-bit data length. 01114 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 01115 * between Master and Slave(example: audio streaming). 01116 * @retval HAL status 01117 */ 01118 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) 01119 { 01120 uint32_t tmpreg_cfgr; 01121 01122 if ((pData == NULL) || (Size == 0U)) 01123 { 01124 return HAL_ERROR; 01125 } 01126 01127 /* Process Locked */ 01128 __HAL_LOCK(hi2s); 01129 01130 if (hi2s->State != HAL_I2S_STATE_READY) 01131 { 01132 __HAL_UNLOCK(hi2s); 01133 return HAL_BUSY; 01134 } 01135 01136 /* Set state and reset error code */ 01137 hi2s->State = HAL_I2S_STATE_BUSY_TX; 01138 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 01139 hi2s->pTxBuffPtr = pData; 01140 01141 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 01142 01143 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 01144 { 01145 hi2s->TxXferSize = (Size << 1U); 01146 hi2s->TxXferCount = (Size << 1U); 01147 } 01148 else 01149 { 01150 hi2s->TxXferSize = Size; 01151 hi2s->TxXferCount = Size; 01152 } 01153 01154 /* Set the I2S Tx DMA Half transfer complete callback */ 01155 hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt; 01156 01157 /* Set the I2S Tx DMA transfer complete callback */ 01158 hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt; 01159 01160 /* Set the DMA error callback */ 01161 hi2s->hdmatx->XferErrorCallback = I2S_DMAError; 01162 01163 /* Enable the Tx DMA Stream/Channel */ 01164 if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmatx, 01165 (uint32_t)hi2s->pTxBuffPtr, 01166 (uint32_t)&hi2s->Instance->DR, 01167 hi2s->TxXferSize)) 01168 { 01169 /* Update SPI error code */ 01170 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); 01171 hi2s->State = HAL_I2S_STATE_READY; 01172 01173 __HAL_UNLOCK(hi2s); 01174 return HAL_ERROR; 01175 } 01176 01177 /* Check if the I2S is already enabled */ 01178 if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) 01179 { 01180 /* Enable I2S peripheral */ 01181 __HAL_I2S_ENABLE(hi2s); 01182 } 01183 01184 /* Check if the I2S Tx request is already enabled */ 01185 if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_TXDMAEN)) 01186 { 01187 /* Enable Tx DMA Request */ 01188 SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); 01189 } 01190 01191 __HAL_UNLOCK(hi2s); 01192 return HAL_OK; 01193 } 01194 01195 /** 01196 * @brief Receive an amount of data in non-blocking mode with DMA 01197 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01198 * the configuration information for I2S module 01199 * @param pData a 16-bit pointer to the Receive data buffer. 01200 * @param Size number of data sample to be sent: 01201 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S 01202 * configuration phase, the Size parameter means the number of 16-bit data length 01203 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 01204 * the Size parameter means the number of 24-bit or 32-bit data length. 01205 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 01206 * between Master and Slave(example: audio streaming). 01207 * @retval HAL status 01208 */ 01209 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) 01210 { 01211 uint32_t tmpreg_cfgr; 01212 01213 if ((pData == NULL) || (Size == 0U)) 01214 { 01215 return HAL_ERROR; 01216 } 01217 01218 /* Process Locked */ 01219 __HAL_LOCK(hi2s); 01220 01221 if (hi2s->State != HAL_I2S_STATE_READY) 01222 { 01223 __HAL_UNLOCK(hi2s); 01224 return HAL_BUSY; 01225 } 01226 01227 /* Set state and reset error code */ 01228 hi2s->State = HAL_I2S_STATE_BUSY_RX; 01229 hi2s->ErrorCode = HAL_I2S_ERROR_NONE; 01230 hi2s->pRxBuffPtr = pData; 01231 01232 tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); 01233 01234 if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) 01235 { 01236 hi2s->RxXferSize = (Size << 1U); 01237 hi2s->RxXferCount = (Size << 1U); 01238 } 01239 else 01240 { 01241 hi2s->RxXferSize = Size; 01242 hi2s->RxXferCount = Size; 01243 } 01244 01245 /* Set the I2S Rx DMA Half transfer complete callback */ 01246 hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt; 01247 01248 /* Set the I2S Rx DMA transfer complete callback */ 01249 hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt; 01250 01251 /* Set the DMA error callback */ 01252 hi2s->hdmarx->XferErrorCallback = I2S_DMAError; 01253 01254 /* Check if Master Receiver mode is selected */ 01255 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) 01256 { 01257 /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read 01258 access to the SPI_SR register. */ 01259 __HAL_I2S_CLEAR_OVRFLAG(hi2s); 01260 } 01261 01262 /* Enable the Rx DMA Stream/Channel */ 01263 if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr, 01264 hi2s->RxXferSize)) 01265 { 01266 /* Update SPI error code */ 01267 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); 01268 hi2s->State = HAL_I2S_STATE_READY; 01269 01270 __HAL_UNLOCK(hi2s); 01271 return HAL_ERROR; 01272 } 01273 01274 /* Check if the I2S is already enabled */ 01275 if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) 01276 { 01277 /* Enable I2S peripheral */ 01278 __HAL_I2S_ENABLE(hi2s); 01279 } 01280 01281 /* Check if the I2S Rx request is already enabled */ 01282 if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_RXDMAEN)) 01283 { 01284 /* Enable Rx DMA Request */ 01285 SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); 01286 } 01287 01288 __HAL_UNLOCK(hi2s); 01289 return HAL_OK; 01290 } 01291 01292 /** 01293 * @brief Pauses the audio DMA Stream/Channel playing from the Media. 01294 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01295 * the configuration information for I2S module 01296 * @retval HAL status 01297 */ 01298 HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s) 01299 { 01300 /* Process Locked */ 01301 __HAL_LOCK(hi2s); 01302 01303 if (hi2s->State == HAL_I2S_STATE_BUSY_TX) 01304 { 01305 /* Disable the I2S DMA Tx request */ 01306 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); 01307 } 01308 else if (hi2s->State == HAL_I2S_STATE_BUSY_RX) 01309 { 01310 /* Disable the I2S DMA Rx request */ 01311 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); 01312 } 01313 else 01314 { 01315 /* nothing to do */ 01316 } 01317 01318 /* Process Unlocked */ 01319 __HAL_UNLOCK(hi2s); 01320 01321 return HAL_OK; 01322 } 01323 01324 /** 01325 * @brief Resumes the audio DMA Stream/Channel playing from the Media. 01326 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01327 * the configuration information for I2S module 01328 * @retval HAL status 01329 */ 01330 HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s) 01331 { 01332 /* Process Locked */ 01333 __HAL_LOCK(hi2s); 01334 01335 if (hi2s->State == HAL_I2S_STATE_BUSY_TX) 01336 { 01337 /* Enable the I2S DMA Tx request */ 01338 SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); 01339 } 01340 else if (hi2s->State == HAL_I2S_STATE_BUSY_RX) 01341 { 01342 /* Enable the I2S DMA Rx request */ 01343 SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); 01344 } 01345 else 01346 { 01347 /* nothing to do */ 01348 } 01349 01350 /* If the I2S peripheral is still not enabled, enable it */ 01351 if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) 01352 { 01353 /* Enable I2S peripheral */ 01354 __HAL_I2S_ENABLE(hi2s); 01355 } 01356 01357 /* Process Unlocked */ 01358 __HAL_UNLOCK(hi2s); 01359 01360 return HAL_OK; 01361 } 01362 01363 /** 01364 * @brief Stops the audio DMA Stream/Channel playing from the Media. 01365 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01366 * the configuration information for I2S module 01367 * @retval HAL status 01368 */ 01369 HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s) 01370 { 01371 HAL_StatusTypeDef errorcode = HAL_OK; 01372 /* The Lock is not implemented on this API to allow the user application 01373 to call the HAL SPI API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback() 01374 when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated 01375 and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback() 01376 */ 01377 01378 if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX)) 01379 { 01380 /* Abort the I2S DMA tx Stream/Channel */ 01381 if (hi2s->hdmatx != NULL) 01382 { 01383 /* Disable the I2S DMA tx Stream/Channel */ 01384 if (HAL_OK != HAL_DMA_Abort(hi2s->hdmatx)) 01385 { 01386 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); 01387 errorcode = HAL_ERROR; 01388 } 01389 } 01390 01391 /* Wait until TXE flag is set */ 01392 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, I2S_TIMEOUT_FLAG) != HAL_OK) 01393 { 01394 /* Set the error code */ 01395 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 01396 hi2s->State = HAL_I2S_STATE_READY; 01397 errorcode = HAL_ERROR; 01398 } 01399 01400 /* Wait until BSY flag is Reset */ 01401 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, I2S_TIMEOUT_FLAG) != HAL_OK) 01402 { 01403 /* Set the error code */ 01404 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); 01405 hi2s->State = HAL_I2S_STATE_READY; 01406 errorcode = HAL_ERROR; 01407 } 01408 01409 /* Disable I2S peripheral */ 01410 __HAL_I2S_DISABLE(hi2s); 01411 01412 /* Clear UDR flag */ 01413 __HAL_I2S_CLEAR_UDRFLAG(hi2s); 01414 01415 /* Disable the I2S Tx DMA requests */ 01416 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); 01417 01418 } 01419 01420 else if ((hi2s->Init.Mode == I2S_MODE_MASTER_RX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_RX)) 01421 { 01422 /* Abort the I2S DMA rx Stream/Channel */ 01423 if (hi2s->hdmarx != NULL) 01424 { 01425 /* Disable the I2S DMA rx Stream/Channel */ 01426 if (HAL_OK != HAL_DMA_Abort(hi2s->hdmarx)) 01427 { 01428 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); 01429 errorcode = HAL_ERROR; 01430 } 01431 } 01432 01433 /* Disable I2S peripheral */ 01434 __HAL_I2S_DISABLE(hi2s); 01435 01436 /* Clear OVR flag */ 01437 __HAL_I2S_CLEAR_OVRFLAG(hi2s); 01438 01439 /* Disable the I2S Rx DMA request */ 01440 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); 01441 01442 if (hi2s->Init.Mode == I2S_MODE_SLAVE_RX) 01443 { 01444 /* Set the error code */ 01445 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_BUSY_LINE_RX); 01446 01447 /* Set the I2S State ready */ 01448 hi2s->State = HAL_I2S_STATE_READY; 01449 errorcode = HAL_ERROR; 01450 } 01451 else 01452 { 01453 /* Read DR to Flush RX Data */ 01454 READ_REG((hi2s->Instance)->DR); 01455 } 01456 } 01457 01458 hi2s->State = HAL_I2S_STATE_READY; 01459 01460 return errorcode; 01461 } 01462 01463 /** 01464 * @brief This function handles I2S interrupt request. 01465 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01466 * the configuration information for I2S module 01467 * @retval None 01468 */ 01469 void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s) 01470 { 01471 uint32_t itsource = hi2s->Instance->CR2; 01472 uint32_t itflag = hi2s->Instance->SR; 01473 01474 /* I2S in mode Receiver ------------------------------------------------*/ 01475 if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) == RESET) && 01476 (I2S_CHECK_FLAG(itflag, I2S_FLAG_RXNE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_RXNE) != RESET)) 01477 { 01478 I2S_Receive_IT(hi2s); 01479 return; 01480 } 01481 01482 /* I2S in mode Tramitter -----------------------------------------------*/ 01483 if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_TXE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_TXE) != RESET)) 01484 { 01485 I2S_Transmit_IT(hi2s); 01486 return; 01487 } 01488 01489 /* I2S interrupt error -------------------------------------------------*/ 01490 if (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_ERR) != RESET) 01491 { 01492 /* I2S Overrun error interrupt occurred ---------------------------------*/ 01493 if (I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) != RESET) 01494 { 01495 /* Disable RXNE and ERR interrupt */ 01496 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); 01497 01498 /* Set the error code and execute error callback*/ 01499 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR); 01500 } 01501 01502 /* I2S Underrun error interrupt occurred --------------------------------*/ 01503 if (I2S_CHECK_FLAG(itflag, I2S_FLAG_UDR) != RESET) 01504 { 01505 /* Disable TXE and ERR interrupt */ 01506 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); 01507 01508 /* Set the error code and execute error callback*/ 01509 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); 01510 } 01511 01512 /* Set the I2S State ready */ 01513 hi2s->State = HAL_I2S_STATE_READY; 01514 01515 /* Call user error callback */ 01516 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01517 hi2s->ErrorCallback(hi2s); 01518 #else 01519 HAL_I2S_ErrorCallback(hi2s); 01520 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01521 } 01522 } 01523 01524 /** 01525 * @brief Tx Transfer Half completed callbacks 01526 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01527 * the configuration information for I2S module 01528 * @retval None 01529 */ 01530 __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) 01531 { 01532 /* Prevent unused argument(s) compilation warning */ 01533 UNUSED(hi2s); 01534 01535 /* NOTE : This function Should not be modified, when the callback is needed, 01536 the HAL_I2S_TxHalfCpltCallback could be implemented in the user file 01537 */ 01538 } 01539 01540 /** 01541 * @brief Tx Transfer completed callbacks 01542 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01543 * the configuration information for I2S module 01544 * @retval None 01545 */ 01546 __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) 01547 { 01548 /* Prevent unused argument(s) compilation warning */ 01549 UNUSED(hi2s); 01550 01551 /* NOTE : This function Should not be modified, when the callback is needed, 01552 the HAL_I2S_TxCpltCallback could be implemented in the user file 01553 */ 01554 } 01555 01556 /** 01557 * @brief Rx Transfer half completed callbacks 01558 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01559 * the configuration information for I2S module 01560 * @retval None 01561 */ 01562 __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) 01563 { 01564 /* Prevent unused argument(s) compilation warning */ 01565 UNUSED(hi2s); 01566 01567 /* NOTE : This function Should not be modified, when the callback is needed, 01568 the HAL_I2S_RxHalfCpltCallback could be implemented in the user file 01569 */ 01570 } 01571 01572 /** 01573 * @brief Rx Transfer completed callbacks 01574 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01575 * the configuration information for I2S module 01576 * @retval None 01577 */ 01578 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) 01579 { 01580 /* Prevent unused argument(s) compilation warning */ 01581 UNUSED(hi2s); 01582 01583 /* NOTE : This function Should not be modified, when the callback is needed, 01584 the HAL_I2S_RxCpltCallback could be implemented in the user file 01585 */ 01586 } 01587 01588 /** 01589 * @brief I2S error callbacks 01590 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01591 * the configuration information for I2S module 01592 * @retval None 01593 */ 01594 __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) 01595 { 01596 /* Prevent unused argument(s) compilation warning */ 01597 UNUSED(hi2s); 01598 01599 /* NOTE : This function Should not be modified, when the callback is needed, 01600 the HAL_I2S_ErrorCallback could be implemented in the user file 01601 */ 01602 } 01603 01604 /** 01605 * @} 01606 */ 01607 01608 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions 01609 * @brief Peripheral State functions 01610 * 01611 @verbatim 01612 =============================================================================== 01613 ##### Peripheral State and Errors functions ##### 01614 =============================================================================== 01615 [..] 01616 This subsection permits to get in run-time the status of the peripheral 01617 and the data flow. 01618 01619 @endverbatim 01620 * @{ 01621 */ 01622 01623 /** 01624 * @brief Return the I2S state 01625 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01626 * the configuration information for I2S module 01627 * @retval HAL state 01628 */ 01629 HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s) 01630 { 01631 return hi2s->State; 01632 } 01633 01634 /** 01635 * @brief Return the I2S error code 01636 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01637 * the configuration information for I2S module 01638 * @retval I2S Error Code 01639 */ 01640 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s) 01641 { 01642 return hi2s->ErrorCode; 01643 } 01644 /** 01645 * @} 01646 */ 01647 01648 /** 01649 * @} 01650 */ 01651 01652 /** @addtogroup I2S_Private_Functions I2S Private Functions 01653 * @{ 01654 */ 01655 /** 01656 * @brief DMA I2S transmit process complete callback 01657 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 01658 * the configuration information for the specified DMA module. 01659 * @retval None 01660 */ 01661 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma) 01662 { 01663 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ 01664 01665 /* if DMA is configured in DMA_NORMAL Mode */ 01666 if (hdma->Init.Mode == DMA_NORMAL) 01667 { 01668 /* Disable Tx DMA Request */ 01669 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); 01670 01671 hi2s->TxXferCount = 0U; 01672 hi2s->State = HAL_I2S_STATE_READY; 01673 } 01674 /* Call user Tx complete callback */ 01675 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01676 hi2s->TxCpltCallback(hi2s); 01677 #else 01678 HAL_I2S_TxCpltCallback(hi2s); 01679 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01680 } 01681 01682 /** 01683 * @brief DMA I2S transmit process half complete callback 01684 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 01685 * the configuration information for the specified DMA module. 01686 * @retval None 01687 */ 01688 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 01689 { 01690 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ 01691 01692 /* Call user Tx half complete callback */ 01693 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01694 hi2s->TxHalfCpltCallback(hi2s); 01695 #else 01696 HAL_I2S_TxHalfCpltCallback(hi2s); 01697 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01698 } 01699 01700 /** 01701 * @brief DMA I2S receive process complete callback 01702 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 01703 * the configuration information for the specified DMA module. 01704 * @retval None 01705 */ 01706 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma) 01707 { 01708 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ 01709 01710 /* if DMA is configured in DMA_NORMAL Mode */ 01711 if (hdma->Init.Mode == DMA_NORMAL) 01712 { 01713 /* Disable Rx DMA Request */ 01714 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); 01715 hi2s->RxXferCount = 0U; 01716 hi2s->State = HAL_I2S_STATE_READY; 01717 } 01718 /* Call user Rx complete callback */ 01719 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01720 hi2s->RxCpltCallback(hi2s); 01721 #else 01722 HAL_I2S_RxCpltCallback(hi2s); 01723 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01724 } 01725 01726 /** 01727 * @brief DMA I2S receive process half complete callback 01728 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 01729 * the configuration information for the specified DMA module. 01730 * @retval None 01731 */ 01732 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 01733 { 01734 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ 01735 01736 /* Call user Rx half complete callback */ 01737 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01738 hi2s->RxHalfCpltCallback(hi2s); 01739 #else 01740 HAL_I2S_RxHalfCpltCallback(hi2s); 01741 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01742 } 01743 01744 /** 01745 * @brief DMA I2S communication error callback 01746 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 01747 * the configuration information for the specified DMA module. 01748 * @retval None 01749 */ 01750 static void I2S_DMAError(DMA_HandleTypeDef *hdma) 01751 { 01752 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ 01753 01754 /* Disable Rx and Tx DMA Request */ 01755 CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN)); 01756 hi2s->TxXferCount = 0U; 01757 hi2s->RxXferCount = 0U; 01758 01759 hi2s->State = HAL_I2S_STATE_READY; 01760 01761 /* Set the error code and execute error callback*/ 01762 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); 01763 /* Call user error callback */ 01764 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01765 hi2s->ErrorCallback(hi2s); 01766 #else 01767 HAL_I2S_ErrorCallback(hi2s); 01768 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01769 } 01770 01771 /** 01772 * @brief Transmit an amount of data in non-blocking mode with Interrupt 01773 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01774 * the configuration information for I2S module 01775 * @retval None 01776 */ 01777 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s) 01778 { 01779 /* Transmit data */ 01780 hi2s->Instance->DR = (*hi2s->pTxBuffPtr); 01781 hi2s->pTxBuffPtr++; 01782 hi2s->TxXferCount--; 01783 01784 if (hi2s->TxXferCount == 0U) 01785 { 01786 /* Disable TXE and ERR interrupt */ 01787 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); 01788 01789 hi2s->State = HAL_I2S_STATE_READY; 01790 /* Call user Tx complete callback */ 01791 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01792 hi2s->TxCpltCallback(hi2s); 01793 #else 01794 HAL_I2S_TxCpltCallback(hi2s); 01795 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01796 } 01797 } 01798 01799 /** 01800 * @brief Receive an amount of data in non-blocking mode with Interrupt 01801 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01802 * the configuration information for I2S module 01803 * @retval None 01804 */ 01805 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s) 01806 { 01807 /* Receive data */ 01808 (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR; 01809 hi2s->pRxBuffPtr++; 01810 hi2s->RxXferCount--; 01811 01812 if (hi2s->RxXferCount == 0U) 01813 { 01814 /* Disable RXNE and ERR interrupt */ 01815 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); 01816 01817 hi2s->State = HAL_I2S_STATE_READY; 01818 /* Call user Rx complete callback */ 01819 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) 01820 hi2s->RxCpltCallback(hi2s); 01821 #else 01822 HAL_I2S_RxCpltCallback(hi2s); 01823 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ 01824 } 01825 } 01826 01827 /** 01828 * @brief This function handles I2S Communication Timeout. 01829 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains 01830 * the configuration information for I2S module 01831 * @param Flag Flag checked 01832 * @param State Value of the flag expected 01833 * @param Timeout Duration of the timeout 01834 * @retval HAL status 01835 */ 01836 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State, 01837 uint32_t Timeout) 01838 { 01839 uint32_t tickstart; 01840 01841 /* Get tick */ 01842 tickstart = HAL_GetTick(); 01843 01844 /* Wait until flag is set to status*/ 01845 while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State) 01846 { 01847 if (Timeout != HAL_MAX_DELAY) 01848 { 01849 if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U)) 01850 { 01851 /* Set the I2S State ready */ 01852 hi2s->State = HAL_I2S_STATE_READY; 01853 01854 /* Process Unlocked */ 01855 __HAL_UNLOCK(hi2s); 01856 01857 return HAL_TIMEOUT; 01858 } 01859 } 01860 } 01861 return HAL_OK; 01862 } 01863 01864 /** 01865 * @} 01866 */ 01867 01868 /** 01869 * @} 01870 */ 01871 01872 /** 01873 * @} 01874 */ 01875 #endif /* SPI_I2S_SUPPORT */ 01876 01877 #endif /* HAL_I2S_MODULE_ENABLED */ 01878 01879 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/