stm32l1xx_hal_i2s.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_i2s.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  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.
  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 initilalized 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 HSE_VALUE in the stm32l1xx_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 USART 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) 2014 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 "stm32l1xx_hal.h"
  128. /** @addtogroup STM32L1xx_HAL_Driver
  129. * @{
  130. */
  131. /** @defgroup I2S I2S
  132. * @brief I2S HAL module driver
  133. * @{
  134. */
  135. #ifdef HAL_I2S_MODULE_ENABLED
  136. #if defined(STM32L100xC) || \
  137. defined(STM32L151xC) || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xE) || \
  138. defined(STM32L152xC) || defined(STM32L152xCA) || defined(STM32L152xD) || defined(STM32L152xE) || \
  139. defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
  140. /* Private typedef -----------------------------------------------------------*/
  141. /* Private define ------------------------------------------------------------*/
  142. /* Private macro -------------------------------------------------------------*/
  143. /* Private variables ---------------------------------------------------------*/
  144. /* Private function prototypes -----------------------------------------------*/
  145. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
  146. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  147. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
  148. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  149. static void I2S_DMAError(DMA_HandleTypeDef *hdma);
  150. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
  151. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
  152. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t Status, uint32_t Timeout);
  153. /* Private functions ---------------------------------------------------------*/
  154. /** @defgroup I2S_Exported_Functions I2S Exported Functions
  155. * @{
  156. */
  157. /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
  158. * @brief Initialization and Configuration functions
  159. *
  160. @verbatim
  161. ===============================================================================
  162. ##### Initialization and de-initialization functions #####
  163. ===============================================================================
  164. [..] This subsection provides a set of functions allowing to initialize and
  165. de-initialiaze the I2Sx peripheral in simplex mode:
  166. (+) User must Implement HAL_I2S_MspInit() function in which he configures
  167. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  168. (+) Call the function HAL_I2S_Init() to configure the selected device with
  169. the selected configuration:
  170. (++) Mode
  171. (++) Standard
  172. (++) Data Format
  173. (++) MCLK Output
  174. (++) Audio frequency
  175. (++) Polarity
  176. (+) Call the function HAL_I2S_DeInit() to restore the default configuration
  177. of the selected I2Sx periperal.
  178. @endverbatim
  179. * @{
  180. */
  181. /**
  182. * @brief Initializes the I2S according to the specified parameters
  183. * in the I2S_InitTypeDef and create the associated handle.
  184. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  185. * the configuration information for I2S module
  186. * @retval HAL status
  187. */
  188. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  189. {
  190. uint32_t i2sdiv = 2, i2sodd = 0, packetlength = 1;
  191. uint32_t tmp = 0, i2sclk = 0;
  192. /* Check the I2S handle allocation */
  193. if(hi2s == NULL)
  194. {
  195. return HAL_ERROR;
  196. }
  197. /* Check the I2S parameters */
  198. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  199. assert_param(IS_I2S_MODE(hi2s->Init.Mode));
  200. assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
  201. assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
  202. assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
  203. assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
  204. assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
  205. if(hi2s->State == HAL_I2S_STATE_RESET)
  206. {
  207. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  208. HAL_I2S_MspInit(hi2s);
  209. }
  210. hi2s->State = HAL_I2S_STATE_BUSY;
  211. /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
  212. if(hi2s->Init.AudioFreq == I2S_AUDIOFREQ_DEFAULT)
  213. {
  214. i2sodd = (uint32_t)0;
  215. i2sdiv = (uint32_t)2;
  216. }
  217. /* If the requested audio frequency is not the default, compute the prescaler */
  218. else
  219. {
  220. /* Check the frame length (For the Prescaler computing) *******************/
  221. if(hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)
  222. {
  223. /* Packet length is 16 bits */
  224. packetlength = 1;
  225. }
  226. else
  227. {
  228. /* Packet length is 32 bits */
  229. packetlength = 2;
  230. }
  231. /* Get the source clock value: based on System Clock value */
  232. i2sclk = HAL_RCC_GetSysClockFreq();
  233. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  234. if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
  235. {
  236. /* MCLK output is enabled */
  237. tmp = (uint32_t)(((((i2sclk / 256) * 10) / hi2s->Init.AudioFreq)) + 5);
  238. }
  239. else
  240. {
  241. /* MCLK output is disabled */
  242. tmp = (uint32_t)(((((i2sclk / (32 * packetlength)) *10 ) / hi2s->Init.AudioFreq)) + 5);
  243. }
  244. /* Remove the flatting point */
  245. tmp = tmp / 10;
  246. /* Check the parity of the divider */
  247. i2sodd = (uint32_t)(tmp & (uint32_t)1);
  248. /* Compute the i2sdiv prescaler */
  249. i2sdiv = (uint32_t)((tmp - i2sodd) / 2);
  250. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  251. i2sodd = (uint32_t) (i2sodd << 8);
  252. }
  253. /* Test if the divider is 1 or 0 or greater than 0xFF */
  254. if((i2sdiv < 2) || (i2sdiv > 0xFF))
  255. {
  256. /* Set the default values */
  257. i2sdiv = 2;
  258. i2sodd = 0;
  259. }
  260. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
  261. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  262. /* And configure the I2S with the I2S_InitStruct values */
  263. MODIFY_REG( hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN |\
  264. SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD |\
  265. SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG |\
  266. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD),\
  267. (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode |\
  268. hi2s->Init.Standard | hi2s->Init.DataFormat |\
  269. hi2s->Init.CPOL));
  270. /* Write to SPIx I2SPR register the computed value */
  271. hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
  272. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  273. hi2s->State= HAL_I2S_STATE_READY;
  274. return HAL_OK;
  275. }
  276. /**
  277. * @brief DeInitializes the I2S peripheral
  278. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  279. * the configuration information for I2S module
  280. * @retval HAL status
  281. */
  282. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  283. {
  284. /* Check the I2S handle allocation */
  285. if(hi2s == NULL)
  286. {
  287. return HAL_ERROR;
  288. }
  289. hi2s->State = HAL_I2S_STATE_BUSY;
  290. /* Disable the I2S Peripheral Clock */
  291. __HAL_I2S_DISABLE(hi2s);
  292. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  293. HAL_I2S_MspDeInit(hi2s);
  294. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  295. hi2s->State = HAL_I2S_STATE_RESET;
  296. /* Release Lock */
  297. __HAL_UNLOCK(hi2s);
  298. return HAL_OK;
  299. }
  300. /**
  301. * @brief I2S MSP Init
  302. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  303. * the configuration information for I2S module
  304. * @retval None
  305. */
  306. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  307. {
  308. /* NOTE : This function Should not be modified, when the callback is needed,
  309. the HAL_I2S_MspInit could be implemented in the user file
  310. */
  311. }
  312. /**
  313. * @brief I2S MSP DeInit
  314. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  315. * the configuration information for I2S module
  316. * @retval None
  317. */
  318. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  319. {
  320. /* NOTE : This function Should not be modified, when the callback is needed,
  321. the HAL_I2S_MspDeInit could be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @}
  326. */
  327. /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
  328. * @brief Data transfers functions
  329. *
  330. @verbatim
  331. ===============================================================================
  332. ##### IO operation functions #####
  333. ===============================================================================
  334. [..]
  335. This subsection provides a set of functions allowing to manage the I2S data
  336. transfers.
  337. (#) There are two modes of transfer:
  338. (++) Blocking mode : The communication is performed in the polling mode.
  339. The status of all data processing is returned by the same function
  340. after finishing transfer.
  341. (++) No-Blocking mode : The communication is performed using Interrupts
  342. or DMA. These functions return the status of the transfer startup.
  343. The end of the data processing will be indicated through the
  344. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  345. using DMA mode.
  346. (#) Blocking mode functions are :
  347. (++) HAL_I2S_Transmit()
  348. (++) HAL_I2S_Receive()
  349. (#) No-Blocking mode functions with Interrupt are :
  350. (++) HAL_I2S_Transmit_IT()
  351. (++) HAL_I2S_Receive_IT()
  352. (#) No-Blocking mode functions with DMA are :
  353. (++) HAL_I2S_Transmit_DMA()
  354. (++) HAL_I2S_Receive_DMA()
  355. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  356. (++) HAL_I2S_TxCpltCallback()
  357. (++) HAL_I2S_RxCpltCallback()
  358. (++) HAL_I2S_ErrorCallback()
  359. @endverbatim
  360. * @{
  361. */
  362. /**
  363. * @brief Transmit an amount of data in blocking mode
  364. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  365. * the configuration information for I2S module
  366. * @param pData: a 16-bit pointer to data buffer.
  367. * @param Size: number of data sample to be sent:
  368. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  369. * configuration phase, the Size parameter means the number of 16-bit data length
  370. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  371. * the Size parameter means the number of 16-bit data length.
  372. * @param Timeout: Timeout duration
  373. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  374. * between Master and Slave(example: audio streaming).
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  378. {
  379. if((pData == NULL ) || (Size == 0))
  380. {
  381. return HAL_ERROR;
  382. }
  383. /* Process Locked */
  384. __HAL_LOCK(hi2s);
  385. if(hi2s->State == HAL_I2S_STATE_READY)
  386. {
  387. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  388. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  389. {
  390. hi2s->TxXferSize = (Size << 1);
  391. hi2s->TxXferCount = (Size << 1);
  392. }
  393. else
  394. {
  395. hi2s->TxXferSize = Size;
  396. hi2s->TxXferCount = Size;
  397. }
  398. /* Set state and reset error code */
  399. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  400. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  401. hi2s->pTxBuffPtr = pData;
  402. /* Check if the I2S is already enabled */
  403. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  404. {
  405. /* Enable I2S peripheral */
  406. __HAL_I2S_ENABLE(hi2s);
  407. }
  408. while(hi2s->TxXferCount > 0)
  409. {
  410. /* Wait until TXE flag is set */
  411. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, RESET, Timeout) != HAL_OK)
  412. {
  413. return HAL_TIMEOUT;
  414. }
  415. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  416. hi2s->TxXferCount--;
  417. }
  418. /* Wait until TXE flag is set, to confirm the end of the transcation */
  419. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, RESET, Timeout) != HAL_OK)
  420. {
  421. return HAL_TIMEOUT;
  422. }
  423. /* Wait until Busy flag is reset */
  424. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, SET, Timeout) != HAL_OK)
  425. {
  426. return HAL_TIMEOUT;
  427. }
  428. hi2s->State = HAL_I2S_STATE_READY;
  429. /* Process Unlocked */
  430. __HAL_UNLOCK(hi2s);
  431. return HAL_OK;
  432. }
  433. else
  434. {
  435. /* Process Unlocked */
  436. __HAL_UNLOCK(hi2s);
  437. return HAL_BUSY;
  438. }
  439. }
  440. /**
  441. * @brief Receive an amount of data in blocking mode
  442. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  443. * the configuration information for I2S module
  444. * @param pData: a 16-bit pointer to data buffer.
  445. * @param Size: number of data sample to be sent:
  446. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  447. * configuration phase, the Size parameter means the number of 16-bit data length
  448. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  449. * the Size parameter means the number of 16-bit data length.
  450. * @param Timeout: Timeout duration
  451. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  452. * between Master and Slave(example: audio streaming).
  453. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  454. * in continouse way and as the I2S is not disabled at the end of the I2S transaction.
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  458. {
  459. if((pData == NULL ) || (Size == 0))
  460. {
  461. return HAL_ERROR;
  462. }
  463. /* Process Locked */
  464. __HAL_LOCK(hi2s);
  465. if(hi2s->State == HAL_I2S_STATE_READY)
  466. {
  467. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  468. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  469. {
  470. hi2s->RxXferSize = (Size << 1);
  471. hi2s->RxXferCount = (Size << 1);
  472. }
  473. else
  474. {
  475. hi2s->RxXferSize = Size;
  476. hi2s->RxXferCount = Size;
  477. }
  478. /* Set state and reset error code */
  479. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  480. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  481. hi2s->pRxBuffPtr = pData;
  482. /* Check if the I2S is already enabled */
  483. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  484. {
  485. /* Enable I2S peripheral */
  486. __HAL_I2S_ENABLE(hi2s);
  487. }
  488. /* Receive data */
  489. while(hi2s->RxXferCount > 0)
  490. {
  491. /* Wait until RXNE flag is set */
  492. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  493. {
  494. return HAL_TIMEOUT;
  495. }
  496. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  497. hi2s->RxXferCount--;
  498. }
  499. hi2s->State = HAL_I2S_STATE_READY;
  500. /* Process Unlocked */
  501. __HAL_UNLOCK(hi2s);
  502. return HAL_OK;
  503. }
  504. else
  505. {
  506. /* Process Unlocked */
  507. __HAL_UNLOCK(hi2s);
  508. return HAL_BUSY;
  509. }
  510. }
  511. /**
  512. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  513. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  514. * the configuration information for I2S module
  515. * @param pData: a 16-bit pointer to data buffer.
  516. * @param Size: number of data sample to be sent:
  517. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  518. * configuration phase, the Size parameter means the number of 16-bit data length
  519. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  520. * the Size parameter means the number of 16-bit data length.
  521. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  522. * between Master and Slave(example: audio streaming).
  523. * @retval HAL status
  524. */
  525. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  526. {
  527. if((pData == NULL) || (Size == 0))
  528. {
  529. return HAL_ERROR;
  530. }
  531. /* Process Locked */
  532. __HAL_LOCK(hi2s);
  533. if(hi2s->State == HAL_I2S_STATE_READY)
  534. {
  535. hi2s->pTxBuffPtr = pData;
  536. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  537. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  538. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  539. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  540. {
  541. hi2s->TxXferSize = (Size << 1);
  542. hi2s->TxXferCount = (Size << 1);
  543. }
  544. else
  545. {
  546. hi2s->TxXferSize = Size;
  547. hi2s->TxXferCount = Size;
  548. }
  549. /* Enable TXE and ERR interrupt */
  550. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  551. /* Check if the I2S is already enabled */
  552. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  553. {
  554. /* Enable I2S peripheral */
  555. __HAL_I2S_ENABLE(hi2s);
  556. }
  557. /* Process Unlocked */
  558. __HAL_UNLOCK(hi2s);
  559. return HAL_OK;
  560. }
  561. else
  562. {
  563. /* Process Unlocked */
  564. __HAL_UNLOCK(hi2s);
  565. return HAL_BUSY;
  566. }
  567. }
  568. /**
  569. * @brief Receive an amount of data in non-blocking mode with Interrupt
  570. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  571. * the configuration information for I2S module
  572. * @param pData: a 16-bit pointer to the Receive data buffer.
  573. * @param Size: number of data sample to be sent:
  574. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  575. * configuration phase, the Size parameter means the number of 16-bit data length
  576. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  577. * the Size parameter means the number of 16-bit data length.
  578. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  579. * between Master and Slave(example: audio streaming).
  580. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
  581. * between Master and Slave otherwise the I2S interrupt should be optimized.
  582. * @retval HAL status
  583. */
  584. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  585. {
  586. if((pData == NULL) || (Size == 0))
  587. {
  588. return HAL_ERROR;
  589. }
  590. /* Process Locked */
  591. __HAL_LOCK(hi2s);
  592. if(hi2s->State == HAL_I2S_STATE_READY)
  593. {
  594. hi2s->pRxBuffPtr = pData;
  595. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  596. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  597. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  598. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  599. {
  600. hi2s->RxXferSize = (Size << 1);
  601. hi2s->RxXferCount = (Size << 1);
  602. }
  603. else
  604. {
  605. hi2s->RxXferSize = Size;
  606. hi2s->RxXferCount = Size;
  607. }
  608. /* Enable TXE and ERR interrupt */
  609. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  610. /* Check if the I2S is already enabled */
  611. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  612. {
  613. /* Enable I2S peripheral */
  614. __HAL_I2S_ENABLE(hi2s);
  615. }
  616. /* Process Unlocked */
  617. __HAL_UNLOCK(hi2s);
  618. return HAL_OK;
  619. }
  620. else
  621. {
  622. /* Process Unlocked */
  623. __HAL_UNLOCK(hi2s);
  624. return HAL_BUSY;
  625. }
  626. }
  627. /**
  628. * @brief Transmit an amount of data in non-blocking mode with DMA
  629. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  630. * the configuration information for I2S module
  631. * @param pData: a 16-bit pointer to the Transmit data buffer.
  632. * @param Size: number of data sample to be sent:
  633. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  634. * configuration phase, the Size parameter means the number of 16-bit data length
  635. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  636. * the Size parameter means the number of 16-bit data length.
  637. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  638. * between Master and Slave(example: audio streaming).
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  642. {
  643. if((pData == NULL) || (Size == 0))
  644. {
  645. return HAL_ERROR;
  646. }
  647. /* Process Locked */
  648. __HAL_LOCK(hi2s);
  649. if(hi2s->State == HAL_I2S_STATE_READY)
  650. {
  651. hi2s->pTxBuffPtr = pData;
  652. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  653. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  654. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  655. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  656. {
  657. hi2s->TxXferSize = (Size << 1);
  658. hi2s->TxXferCount = (Size << 1);
  659. }
  660. else
  661. {
  662. hi2s->TxXferSize = Size;
  663. hi2s->TxXferCount = Size;
  664. }
  665. /* Set the I2S Tx DMA Half transfert complete callback */
  666. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  667. /* Set the I2S Tx DMA transfert complete callback */
  668. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  669. /* Set the DMA error callback */
  670. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  671. /* Enable the Tx DMA Channel */
  672. HAL_DMA_Start_IT(hi2s->hdmatx, (uint32_t)hi2s->pTxBuffPtr, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
  673. /* Check if the I2S is already enabled */
  674. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  675. {
  676. /* Enable I2S peripheral */
  677. __HAL_I2S_ENABLE(hi2s);
  678. }
  679. /* Check if the I2S Tx request is already enabled */
  680. if((hi2s->Instance->CR2 & SPI_CR2_TXDMAEN) != SPI_CR2_TXDMAEN)
  681. {
  682. /* Enable Tx DMA Request */
  683. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  684. }
  685. /* Process Unlocked */
  686. __HAL_UNLOCK(hi2s);
  687. return HAL_OK;
  688. }
  689. else
  690. {
  691. /* Process Unlocked */
  692. __HAL_UNLOCK(hi2s);
  693. return HAL_BUSY;
  694. }
  695. }
  696. /**
  697. * @brief Receive an amount of data in non-blocking mode with DMA
  698. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  699. * the configuration information for I2S module
  700. * @param pData: a 16-bit pointer to the Receive data buffer.
  701. * @param Size: number of data sample to be sent:
  702. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  703. * configuration phase, the Size parameter means the number of 16-bit data length
  704. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  705. * the Size parameter means the number of 16-bit data length.
  706. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  707. * between Master and Slave(example: audio streaming).
  708. * @retval HAL status
  709. */
  710. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  711. {
  712. if((pData == NULL) || (Size == 0))
  713. {
  714. return HAL_ERROR;
  715. }
  716. /* Process Locked */
  717. __HAL_LOCK(hi2s);
  718. if(hi2s->State == HAL_I2S_STATE_READY)
  719. {
  720. hi2s->pRxBuffPtr = pData;
  721. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  722. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  723. if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
  724. ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
  725. {
  726. hi2s->RxXferSize = (Size << 1);
  727. hi2s->RxXferCount = (Size << 1);
  728. }
  729. else
  730. {
  731. hi2s->RxXferSize = Size;
  732. hi2s->RxXferCount = Size;
  733. }
  734. /* Set the I2S Rx DMA Half transfert complete callback */
  735. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  736. /* Set the I2S Rx DMA transfert complete callback */
  737. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  738. /* Set the DMA error callback */
  739. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  740. /* Check if Master Receiver mode is selected */
  741. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  742. {
  743. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  744. access to the SPI_SR register. */
  745. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  746. }
  747. /* Enable the Rx DMA Channel */
  748. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr, hi2s->RxXferSize);
  749. /* Check if the I2S is already enabled */
  750. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  751. {
  752. /* Enable I2S peripheral */
  753. __HAL_I2S_ENABLE(hi2s);
  754. }
  755. /* Check if the I2S Rx request is already enabled */
  756. if((hi2s->Instance->CR2 &SPI_CR2_RXDMAEN) != SPI_CR2_RXDMAEN)
  757. {
  758. /* Enable Rx DMA Request */
  759. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  760. }
  761. /* Process Unlocked */
  762. __HAL_UNLOCK(hi2s);
  763. return HAL_OK;
  764. }
  765. else
  766. {
  767. /* Process Unlocked */
  768. __HAL_UNLOCK(hi2s);
  769. return HAL_BUSY;
  770. }
  771. }
  772. /**
  773. * @brief Pauses the audio stream playing from the Media.
  774. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  775. * the configuration information for I2S module
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  779. {
  780. /* Process Locked */
  781. __HAL_LOCK(hi2s);
  782. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  783. {
  784. /* Disable the I2S DMA Tx request */
  785. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  786. }
  787. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  788. {
  789. /* Disable the I2S DMA Rx request */
  790. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  791. }
  792. /* Process Unlocked */
  793. __HAL_UNLOCK(hi2s);
  794. return HAL_OK;
  795. }
  796. /**
  797. * @brief Resumes the audio stream playing from the Media.
  798. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  799. * the configuration information for I2S module
  800. * @retval HAL status
  801. */
  802. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  803. {
  804. /* Process Locked */
  805. __HAL_LOCK(hi2s);
  806. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  807. {
  808. /* Enable the I2S DMA Tx request */
  809. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  810. }
  811. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  812. {
  813. /* Enable the I2S DMA Rx request */
  814. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  815. }
  816. /* If the I2S peripheral is still not enabled, enable it */
  817. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) == 0)
  818. {
  819. /* Enable I2S peripheral */
  820. __HAL_I2S_ENABLE(hi2s);
  821. }
  822. /* Process Unlocked */
  823. __HAL_UNLOCK(hi2s);
  824. return HAL_OK;
  825. }
  826. /**
  827. * @brief Resumes the audio stream playing from the Media.
  828. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  829. * the configuration information for I2S module
  830. * @retval HAL status
  831. */
  832. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  833. {
  834. /* Process Locked */
  835. __HAL_LOCK(hi2s);
  836. /* Disable the I2S Tx/Rx DMA requests */
  837. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  838. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  839. /* Abort the I2S DMA Channel tx */
  840. if(hi2s->hdmatx != NULL)
  841. {
  842. /* Disable the I2S DMA channel */
  843. __HAL_DMA_DISABLE(hi2s->hdmatx);
  844. HAL_DMA_Abort(hi2s->hdmatx);
  845. }
  846. /* Abort the I2S DMA Channel rx */
  847. if(hi2s->hdmarx != NULL)
  848. {
  849. /* Disable the I2S DMA channel */
  850. __HAL_DMA_DISABLE(hi2s->hdmarx);
  851. HAL_DMA_Abort(hi2s->hdmarx);
  852. }
  853. /* Disable I2S peripheral */
  854. __HAL_I2S_DISABLE(hi2s);
  855. hi2s->State = HAL_I2S_STATE_READY;
  856. /* Process Unlocked */
  857. __HAL_UNLOCK(hi2s);
  858. return HAL_OK;
  859. }
  860. /**
  861. * @brief This function handles I2S interrupt request.
  862. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  863. * the configuration information for I2S module
  864. * @retval None
  865. */
  866. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  867. {
  868. uint32_t i2ssr = hi2s->Instance->SR;
  869. /* I2S in mode Receiver ------------------------------------------------*/
  870. if(((i2ssr & I2S_FLAG_OVR) != I2S_FLAG_OVR) &&
  871. ((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
  872. {
  873. I2S_Receive_IT(hi2s);
  874. return;
  875. }
  876. /* I2S in mode Tramitter -----------------------------------------------*/
  877. if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
  878. {
  879. I2S_Transmit_IT(hi2s);
  880. return;
  881. }
  882. /* I2S interrupt error -------------------------------------------------*/
  883. if(__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET)
  884. {
  885. /* I2S Overrun error interrupt occured ---------------------------------*/
  886. if((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR)
  887. {
  888. /* Disable RXNE and ERR interrupt */
  889. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  890. /* Set the error code and execute error callback*/
  891. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  892. }
  893. /* I2S Underrun error interrupt occured --------------------------------*/
  894. if((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR)
  895. {
  896. /* Disable TXE and ERR interrupt */
  897. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  898. /* Set the error code and execute error callback*/
  899. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  900. }
  901. /* I2S Frame format error interrupt occured --------------------------*/
  902. if((i2ssr & I2S_FLAG_FRE) == I2S_FLAG_FRE)
  903. {
  904. /* Disable TXE and ERR interrupt */
  905. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_RXNE | I2S_IT_ERR));
  906. /* Set the error code and execute error callback*/
  907. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_FRE);
  908. }
  909. /* Set the I2S State ready */
  910. hi2s->State = HAL_I2S_STATE_READY;
  911. /* Call the Error Callback */
  912. HAL_I2S_ErrorCallback(hi2s);
  913. }
  914. }
  915. /**
  916. * @brief Tx Transfer Half completed callbacks
  917. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  918. * the configuration information for I2S module
  919. * @retval None
  920. */
  921. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  922. {
  923. /* NOTE : This function Should not be modified, when the callback is needed,
  924. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  925. */
  926. }
  927. /**
  928. * @brief Tx Transfer completed callbacks
  929. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  930. * the configuration information for I2S module
  931. * @retval None
  932. */
  933. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  934. {
  935. /* NOTE : This function Should not be modified, when the callback is needed,
  936. the HAL_I2S_TxCpltCallback could be implemented in the user file
  937. */
  938. }
  939. /**
  940. * @brief Rx Transfer half completed callbacks
  941. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  942. * the configuration information for I2S module
  943. * @retval None
  944. */
  945. __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  946. {
  947. /* NOTE : This function Should not be modified, when the callback is needed,
  948. the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
  949. */
  950. }
  951. /**
  952. * @brief Rx Transfer completed callbacks
  953. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  954. * the configuration information for I2S module
  955. * @retval None
  956. */
  957. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  958. {
  959. /* NOTE : This function Should not be modified, when the callback is needed,
  960. the HAL_I2S_RxCpltCallback could be implemented in the user file
  961. */
  962. }
  963. /**
  964. * @brief I2S error callbacks
  965. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  966. * the configuration information for I2S module
  967. * @retval None
  968. */
  969. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  970. {
  971. /* NOTE : This function Should not be modified, when the callback is needed,
  972. the HAL_I2S_ErrorCallback could be implemented in the user file
  973. */
  974. }
  975. /**
  976. * @}
  977. */
  978. /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
  979. * @brief Peripheral State functions
  980. *
  981. @verbatim
  982. ===============================================================================
  983. ##### Peripheral State and Errors functions #####
  984. ===============================================================================
  985. [..]
  986. This subsection permits to get in run-time the status of the peripheral
  987. and the data flow.
  988. @endverbatim
  989. * @{
  990. */
  991. /**
  992. * @brief Return the I2S state
  993. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  994. * the configuration information for I2S module
  995. * @retval HAL state
  996. */
  997. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  998. {
  999. return hi2s->State;
  1000. }
  1001. /**
  1002. * @brief Return the I2S error code
  1003. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1004. * the configuration information for I2S module
  1005. * @retval I2S Error Code
  1006. */
  1007. HAL_I2S_ErrorTypeDef HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1008. {
  1009. return hi2s->ErrorCode;
  1010. }
  1011. /**
  1012. * @}
  1013. */
  1014. /**
  1015. * @}
  1016. */
  1017. /** @defgroup I2S_Private_Functions I2S Private Functions
  1018. * @{
  1019. */
  1020. /**
  1021. * @brief DMA I2S transmit process complete callback
  1022. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1023. * the configuration information for the specified DMA module.
  1024. * @retval None
  1025. */
  1026. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1027. {
  1028. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1029. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
  1030. {
  1031. /* Disable Tx DMA Request */
  1032. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1033. hi2s->TxXferCount = 0;
  1034. hi2s->State = HAL_I2S_STATE_READY;
  1035. }
  1036. HAL_I2S_TxCpltCallback(hi2s);
  1037. }
  1038. /**
  1039. * @brief DMA I2S transmit process half complete callback
  1040. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1041. * the configuration information for the specified DMA module.
  1042. * @retval None
  1043. */
  1044. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1045. {
  1046. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1047. HAL_I2S_TxHalfCpltCallback(hi2s);
  1048. }
  1049. /**
  1050. * @brief DMA I2S receive process complete callback
  1051. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1052. * the configuration information for the specified DMA module.
  1053. * @retval None
  1054. */
  1055. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1056. {
  1057. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1058. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
  1059. {
  1060. /* Disable Rx DMA Request */
  1061. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1062. hi2s->RxXferCount = 0;
  1063. hi2s->State = HAL_I2S_STATE_READY;
  1064. }
  1065. HAL_I2S_RxCpltCallback(hi2s);
  1066. }
  1067. /**
  1068. * @brief DMA I2S receive process half complete callback
  1069. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1070. * the configuration information for the specified DMA module.
  1071. * @retval None
  1072. */
  1073. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1074. {
  1075. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1076. HAL_I2S_RxHalfCpltCallback(hi2s);
  1077. }
  1078. /**
  1079. * @brief DMA I2S communication error callback
  1080. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1081. * the configuration information for the specified DMA module.
  1082. * @retval None
  1083. */
  1084. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1085. {
  1086. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1087. /* Disable Rx and Tx DMA Request */
  1088. CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1089. hi2s->TxXferCount = 0;
  1090. hi2s->RxXferCount = 0;
  1091. hi2s->State= HAL_I2S_STATE_READY;
  1092. /* Set the error code and execute error callback*/
  1093. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1094. HAL_I2S_ErrorCallback(hi2s);
  1095. }
  1096. /**
  1097. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1098. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1099. * the configuration information for I2S module
  1100. * @retval None
  1101. */
  1102. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1103. {
  1104. /* Transmit data */
  1105. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  1106. hi2s->TxXferCount--;
  1107. if(hi2s->TxXferCount == 0)
  1108. {
  1109. /* Disable TXE and ERR interrupt */
  1110. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1111. hi2s->State = HAL_I2S_STATE_READY;
  1112. HAL_I2S_TxCpltCallback(hi2s);
  1113. }
  1114. }
  1115. /**
  1116. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1117. * @param hi2s: I2S handle
  1118. * @retval None
  1119. */
  1120. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1121. {
  1122. /* Receive data */
  1123. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  1124. hi2s->RxXferCount--;
  1125. if(hi2s->RxXferCount == 0)
  1126. {
  1127. /* Disable RXNE and ERR interrupt */
  1128. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1129. hi2s->State = HAL_I2S_STATE_READY;
  1130. HAL_I2S_RxCpltCallback(hi2s);
  1131. }
  1132. }
  1133. /**
  1134. * @brief This function handles I2S Communication Timeout.
  1135. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1136. * the configuration information for I2S module
  1137. * @param Flag: Flag checked
  1138. * @param Status: Value of the flag expected
  1139. * @param Timeout: Duration of the timeout
  1140. * @retval HAL status
  1141. */
  1142. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t Status, uint32_t Timeout)
  1143. {
  1144. uint32_t tickstart = 0;
  1145. /* Get tick */
  1146. tickstart = HAL_GetTick();
  1147. /* Wait until flag is set */
  1148. if(Status == RESET)
  1149. {
  1150. while(__HAL_I2S_GET_FLAG(hi2s, Flag) == RESET)
  1151. {
  1152. if(Timeout != HAL_MAX_DELAY)
  1153. {
  1154. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1155. {
  1156. /* Set the I2S State ready */
  1157. hi2s->State= HAL_I2S_STATE_READY;
  1158. /* Process Unlocked */
  1159. __HAL_UNLOCK(hi2s);
  1160. return HAL_TIMEOUT;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. else
  1166. {
  1167. while(__HAL_I2S_GET_FLAG(hi2s, Flag) != RESET)
  1168. {
  1169. if(Timeout != HAL_MAX_DELAY)
  1170. {
  1171. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1172. {
  1173. /* Set the I2S State ready */
  1174. hi2s->State= HAL_I2S_STATE_READY;
  1175. /* Process Unlocked */
  1176. __HAL_UNLOCK(hi2s);
  1177. return HAL_TIMEOUT;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. return HAL_OK;
  1183. }
  1184. /**
  1185. * @}
  1186. */
  1187. #endif /* STM32L100xC ||
  1188. STM32L151xC || STM32L151xCA || STM32L151xD || STM32L151xE ||\\
  1189. STM32L152xC || STM32L152xCA || STM32L152xD || STM32L152xE ||\\
  1190. STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE */
  1191. #endif /* HAL_I2S_MODULE_ENABLED */
  1192. /**
  1193. * @}
  1194. */
  1195. /**
  1196. * @}
  1197. */
  1198. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/