stm32f1xx_hal_i2s.c 59 KB

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