stm32h7xx_hal_i2s.c 58 KB

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