stm32f4xx_hal_i2s.c 68 KB

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