stm32f1xx_hal_i2s.c 61 KB

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