stm32f1xx_hal_i2s.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_i2s.c
  4. * @author MCD Application Team
  5. * @brief I2S HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Integrated Interchip Sound (I2S) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and Errors functions
  11. @verbatim
  12. ===============================================================================
  13. ##### How to use this driver #####
  14. ===============================================================================
  15. [..]
  16. The I2S HAL driver can be used as follow:
  17. (#) Declare a I2S_HandleTypeDef handle structure.
  18. (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:
  19. (##) Enable the SPIx interface clock.
  20. (##) I2S pins configuration:
  21. (+++) Enable the clock for the I2S GPIOs.
  22. (+++) Configure these I2S pins as alternate function.
  23. (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()
  24. and HAL_I2S_Receive_IT() APIs).
  25. (+++) Configure the I2Sx interrupt priority.
  26. (+++) Enable the NVIC I2S IRQ handle.
  27. (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()
  28. and HAL_I2S_Receive_DMA() APIs:
  29. (+++) Declare a DMA handle structure for the Tx/Rx Channel.
  30. (+++) Enable the DMAx interface clock.
  31. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  32. (+++) Configure the DMA Tx/Rx Channel.
  33. (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle.
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  35. DMA Tx/Rx Channel.
  36. (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
  37. using HAL_I2S_Init() function.
  38. -@- The specific I2S interrupts (Transmission complete interrupt,
  39. RXNE interrupt and Error Interrupts) will be managed using the macros
  40. __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
  41. -@- The I2SxCLK source is the system clock (provided by the HSI, the HSE or the PLL, and sourcing the AHB clock).
  42. For connectivity line devices, the I2SxCLK source can be either SYSCLK or the PLL3 VCO (2 x PLL3CLK) clock
  43. in order to achieve the maximum accuracy.
  44. -@- Make sure that either:
  45. (+@) External clock source is configured after setting correctly
  46. the define constant HSE_VALUE in the stm32f1xx_hal_conf.h file.
  47. (#) Three operation modes are available within this driver :
  48. *** Polling mode IO operation ***
  49. =================================
  50. [..]
  51. (+) Send an amount of data in blocking mode using HAL_I2S_Transmit()
  52. (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
  53. *** Interrupt mode IO operation ***
  54. ===================================
  55. [..]
  56. (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT()
  57. (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
  58. add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
  59. (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_I2S_TxCpltCallback
  61. (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT()
  62. (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
  63. add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
  64. (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
  65. add his own code by customization of function pointer HAL_I2S_RxCpltCallback
  66. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  67. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  68. *** DMA mode IO operation ***
  69. ==============================
  70. [..]
  71. (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA()
  72. (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
  73. add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
  74. (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
  75. add his own code by customization of function pointer HAL_I2S_TxCpltCallback
  76. (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA()
  77. (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
  78. add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
  79. (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
  80. add his own code by customization of function pointer HAL_I2S_RxCpltCallback
  81. (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
  82. add his own code by customization of function pointer HAL_I2S_ErrorCallback
  83. (+) Pause the DMA Transfer using HAL_I2S_DMAPause()
  84. (+) Resume the DMA Transfer using HAL_I2S_DMAResume()
  85. (+) Stop the DMA Transfer using HAL_I2S_DMAStop()
  86. *** I2S HAL driver macros list ***
  87. =============================================
  88. [..]
  89. Below the list of most used macros in I2S HAL driver.
  90. (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode)
  91. (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)
  92. (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
  93. (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
  94. (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
  95. [..]
  96. (@) You can refer to the I2S HAL driver header file for more useful macros
  97. *** I2C Workarounds linked to Silicon Limitation ***
  98. ====================================================
  99. [..]
  100. (@) Only the 16-bit mode with no data extension can be used when the I2S
  101. is in Master and used the PCM long synchronization mode.
  102. @endverbatim
  103. ******************************************************************************
  104. * @attention
  105. *
  106. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  107. *
  108. * Redistribution and use in source and binary forms, with or without modification,
  109. * are permitted provided that the following conditions are met:
  110. * 1. Redistributions of source code must retain the above copyright notice,
  111. * this list of conditions and the following disclaimer.
  112. * 2. Redistributions in binary form must reproduce the above copyright notice,
  113. * this list of conditions and the following disclaimer in the documentation
  114. * and/or other materials provided with the distribution.
  115. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  116. * may be used to endorse or promote products derived from this software
  117. * without specific prior written permission.
  118. *
  119. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  120. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  121. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  122. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  123. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  124. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  125. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  126. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  127. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  128. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  129. *
  130. ******************************************************************************
  131. */
  132. /* Includes ------------------------------------------------------------------*/
  133. #include "stm32f1xx_hal.h"
  134. /** @addtogroup STM32F1xx_HAL_Driver
  135. * @{
  136. */
  137. #ifdef HAL_I2S_MODULE_ENABLED
  138. #if defined(STM32F103xE) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
  139. /** @defgroup I2S I2S
  140. * @brief I2S HAL module driver
  141. * @{
  142. */
  143. /* Private typedef -----------------------------------------------------------*/
  144. /* Private define ------------------------------------------------------------*/
  145. /* Private macro -------------------------------------------------------------*/
  146. /* Private variables ---------------------------------------------------------*/
  147. /* Private function prototypes -----------------------------------------------*/
  148. /** @addtogroup I2S_Private_Functions I2S Private Functions
  149. * @{
  150. */
  151. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
  152. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  153. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
  154. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  155. static void I2S_DMAError(DMA_HandleTypeDef *hdma);
  156. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
  157. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
  158. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s);
  159. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State,
  160. uint32_t Timeout);
  161. /**
  162. * @}
  163. */
  164. /* Exported functions ---------------------------------------------------------*/
  165. /** @defgroup I2S_Exported_Functions I2S Exported Functions
  166. * @{
  167. */
  168. /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
  169. * @brief Initialization and Configuration functions
  170. *
  171. @verbatim
  172. ===============================================================================
  173. ##### Initialization and de-initialization functions #####
  174. ===============================================================================
  175. [..] This subsection provides a set of functions allowing to initialize and
  176. de-initialize the I2Sx peripheral in simplex mode:
  177. (+) User must Implement HAL_I2S_MspInit() function in which he configures
  178. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  179. (+) Call the function HAL_I2S_Init() to configure the selected device with
  180. the selected configuration:
  181. (++) Mode
  182. (++) Standard
  183. (++) Data Format
  184. (++) MCLK Output
  185. (++) Audio frequency
  186. (++) Polarity
  187. (+) Call the function HAL_I2S_DeInit() to restore the default configuration
  188. of the selected I2Sx peripheral.
  189. @endverbatim
  190. * @{
  191. */
  192. /**
  193. * @brief Initializes the I2S according to the specified parameters
  194. * in the I2S_InitTypeDef and create the associated handle.
  195. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  196. * the configuration information for I2S module
  197. * @retval HAL status
  198. */
  199. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  200. {
  201. uint32_t tmpreg = 0U, i2sdiv = 2U, i2sodd = 0U, packetlength = 16U;
  202. uint32_t tmp = 0U, i2sclk = 0U;
  203. /* Check the I2S handle allocation */
  204. if(hi2s == NULL)
  205. {
  206. return HAL_ERROR;
  207. }
  208. /* Check the I2S parameters */
  209. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  210. assert_param(IS_I2S_MODE(hi2s->Init.Mode));
  211. assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
  212. assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
  213. assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
  214. assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
  215. assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
  216. hi2s->State = HAL_I2S_STATE_BUSY;
  217. /* Initialize Default I2S IrqHandler ISR */
  218. hi2s->IrqHandlerISR = I2S_IRQHandler;
  219. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  220. HAL_I2S_MspInit(hi2s);
  221. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ---------------*/
  222. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  223. CLEAR_BIT(hi2s->Instance->I2SCFGR,(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  224. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  225. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD));
  226. hi2s->Instance->I2SPR = 0x0002U;
  227. /* Get the I2SCFGR register value */
  228. tmpreg = hi2s->Instance->I2SCFGR;
  229. /* If the default frequency value has to be written, reinitialize i2sdiv and i2sodd */
  230. /* If the requested audio frequency is not the default, compute the prescaler */
  231. if(hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT)
  232. {
  233. /* Check the frame length (For the Prescaler computing) *******************/
  234. /* Set I2S Packet Length value*/
  235. if(hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  236. {
  237. /* Packet length is 32 bits */
  238. packetlength = 32U;
  239. }
  240. else
  241. {
  242. /* Packet length is 16 bits */
  243. packetlength = 16U;
  244. }
  245. /* I2S standard */
  246. if(hi2s->Init.Standard <= I2S_STANDARD_LSB)
  247. {
  248. /* In I2S standard packet lenght is multiplied by 2 */
  249. packetlength = packetlength * 2U;
  250. }
  251. if(hi2s->Instance == SPI2)
  252. {
  253. /* Get the source clock value: based on SPI2 Instance */
  254. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S2);
  255. }
  256. else if(hi2s->Instance == SPI3)
  257. {
  258. /* Get the source clock value: based on SPI3 Instance */
  259. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S3);
  260. }
  261. else
  262. {
  263. /* Get the source clock value: based on System Clock value */
  264. i2sclk = HAL_RCC_GetSysClockFreq();
  265. }
  266. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  267. if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
  268. {
  269. /* MCLK output is enabled */
  270. if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  271. {
  272. tmp = (uint32_t)(((((i2sclk / (packetlength*4)) * 10) / hi2s->Init.AudioFreq)) + 5);
  273. }
  274. else
  275. {
  276. tmp = (uint32_t)(((((i2sclk / (packetlength*8)) * 10) / hi2s->Init.AudioFreq)) + 5);
  277. }
  278. }
  279. else
  280. {
  281. /* MCLK output is disabled */
  282. tmp = (uint32_t)(((((i2sclk / packetlength) *10 ) / hi2s->Init.AudioFreq)) + 5);
  283. }
  284. /* Remove the flatting point */
  285. tmp = tmp / 10U;
  286. /* Check the parity of the divider */
  287. i2sodd = (uint16_t)(tmp & (uint16_t)1U);
  288. /* Compute the i2sdiv prescaler */
  289. i2sdiv = (uint16_t)((tmp - i2sodd) / 2U);
  290. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  291. i2sodd = (uint32_t) (i2sodd << 8U);
  292. }
  293. /* Test if the divider is 1 or 0 or greater than 0xFF */
  294. if((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  295. {
  296. /* Set the default values */
  297. i2sdiv = 2U;
  298. i2sodd = 0U;
  299. /* Set the error code and execute error callback*/
  300. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER);
  301. HAL_I2S_ErrorCallback(hi2s);
  302. return HAL_ERROR;
  303. }
  304. /* Write to SPIx I2SPR register the computed value */
  305. hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
  306. /* Configure the I2S with the I2S_InitStruct values */
  307. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \
  308. (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
  309. (uint16_t)hi2s->Init.CPOL))));
  310. /* Write to SPIx I2SCFGR */
  311. WRITE_REG(hi2s->Instance->I2SCFGR,tmpreg);
  312. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  313. hi2s->State = HAL_I2S_STATE_READY;
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief DeInitializes the I2S peripheral
  318. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  319. * the configuration information for I2S module
  320. * @retval HAL status
  321. */
  322. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  323. {
  324. /* Check the I2S handle allocation */
  325. if(hi2s == NULL)
  326. {
  327. return HAL_ERROR;
  328. }
  329. hi2s->State = HAL_I2S_STATE_BUSY;
  330. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  331. HAL_I2S_MspDeInit(hi2s);
  332. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  333. hi2s->State = HAL_I2S_STATE_RESET;
  334. /* Release Lock */
  335. __HAL_UNLOCK(hi2s);
  336. return HAL_OK;
  337. }
  338. /**
  339. * @brief I2S MSP Init
  340. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  341. * the configuration information for I2S module
  342. * @retval None
  343. */
  344. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  345. {
  346. /* Prevent unused argument(s) compilation warning */
  347. UNUSED(hi2s);
  348. /* NOTE : This function Should not be modified, when the callback is needed,
  349. the HAL_I2S_MspInit could be implemented in the user file
  350. */
  351. }
  352. /**
  353. * @brief I2S MSP DeInit
  354. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  355. * the configuration information for I2S module
  356. * @retval None
  357. */
  358. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  359. {
  360. /* Prevent unused argument(s) compilation warning */
  361. UNUSED(hi2s);
  362. /* NOTE : This function Should not be modified, when the callback is needed,
  363. the HAL_I2S_MspDeInit could be implemented in the user file
  364. */
  365. }
  366. /**
  367. * @}
  368. */
  369. /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
  370. * @brief Data transfers functions
  371. *
  372. @verbatim
  373. ===============================================================================
  374. ##### IO operation functions #####
  375. ===============================================================================
  376. [..]
  377. This subsection provides a set of functions allowing to manage the I2S data
  378. transfers.
  379. (#) There are two modes of transfer:
  380. (++) Blocking mode : The communication is performed in the polling mode.
  381. The status of all data processing is returned by the same function
  382. after finishing transfer.
  383. (++) No-Blocking mode : The communication is performed using Interrupts
  384. or DMA. These functions return the status of the transfer startup.
  385. The end of the data processing will be indicated through the
  386. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  387. using DMA mode.
  388. (#) Blocking mode functions are :
  389. (++) HAL_I2S_Transmit()
  390. (++) HAL_I2S_Receive()
  391. (#) No-Blocking mode functions with Interrupt are :
  392. (++) HAL_I2S_Transmit_IT()
  393. (++) HAL_I2S_Receive_IT()
  394. (#) No-Blocking mode functions with DMA are :
  395. (++) HAL_I2S_Transmit_DMA()
  396. (++) HAL_I2S_Receive_DMA()
  397. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  398. (++) HAL_I2S_TxCpltCallback()
  399. (++) HAL_I2S_RxCpltCallback()
  400. (++) HAL_I2S_ErrorCallback()
  401. @endverbatim
  402. * @{
  403. */
  404. /**
  405. * @brief Transmit an amount of data in blocking mode
  406. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  407. * the configuration information for I2S module
  408. * @param pData: a 16-bit pointer to data buffer.
  409. * @param Size: number of data sample to be sent:
  410. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  411. * configuration phase, the Size parameter means the number of 16-bit data length
  412. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  413. * the Size parameter means the number of 16-bit data length.
  414. * @param Timeout: Timeout duration
  415. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  416. * between Master and Slave(example: audio streaming).
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  420. {
  421. uint32_t tmp1 = 0U;
  422. if((pData == NULL ) || (Size == 0U))
  423. {
  424. return HAL_ERROR;
  425. }
  426. if(hi2s->State == HAL_I2S_STATE_READY)
  427. {
  428. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  429. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  430. {
  431. hi2s->TxXferSize = (Size << 1U);
  432. hi2s->TxXferCount = (Size << 1U);
  433. }
  434. else
  435. {
  436. hi2s->TxXferSize = Size;
  437. hi2s->TxXferCount = Size;
  438. }
  439. /* Process Locked */
  440. __HAL_LOCK(hi2s);
  441. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  442. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  443. /* Check if the I2S is already enabled */
  444. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  445. {
  446. /* Enable I2S peripheral */
  447. __HAL_I2S_ENABLE(hi2s);
  448. }
  449. while(hi2s->TxXferCount > 0U)
  450. {
  451. hi2s->Instance->DR = (*pData++);
  452. hi2s->TxXferCount--;
  453. /* Wait until TXE flag is set */
  454. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  455. {
  456. /* Set the error code and execute error callback*/
  457. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  458. HAL_I2S_ErrorCallback(hi2s);
  459. return HAL_TIMEOUT;
  460. }
  461. /* Check if an underrun occurs */
  462. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
  463. {
  464. /* Clear underrun flag */
  465. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  466. /* Set the I2S State ready */
  467. hi2s->State = HAL_I2S_STATE_READY;
  468. /* Process Unlocked */
  469. __HAL_UNLOCK(hi2s);
  470. /* Set the error code and execute error callback*/
  471. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  472. HAL_I2S_ErrorCallback(hi2s);
  473. return HAL_ERROR;
  474. }
  475. }
  476. hi2s->State = HAL_I2S_STATE_READY;
  477. /* Process Unlocked */
  478. __HAL_UNLOCK(hi2s);
  479. return HAL_OK;
  480. }
  481. else
  482. {
  483. return HAL_BUSY;
  484. }
  485. }
  486. /**
  487. * @brief Receive an amount of data in blocking mode
  488. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  489. * the configuration information for I2S module
  490. * @param pData: a 16-bit pointer to data buffer
  491. * @param Size: number of data sample to be sent:
  492. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  493. * configuration phase, the Size parameter means the number of 16-bit data length
  494. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  495. * the Size parameter means the number of 16-bit data length.
  496. * @param Timeout: Timeout duration
  497. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  498. * between Master and Slave(example: audio streaming)
  499. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  500. * in continuous way and as the I2S is not disabled at the end of the I2S transaction
  501. * @retval HAL status
  502. */
  503. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  504. {
  505. uint32_t tmp1 = 0U;
  506. if((pData == NULL ) || (Size == 0U))
  507. {
  508. return HAL_ERROR;
  509. }
  510. if(hi2s->State == HAL_I2S_STATE_READY)
  511. {
  512. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  513. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  514. {
  515. hi2s->RxXferSize = (Size << 1U);
  516. hi2s->RxXferCount = (Size << 1U);
  517. }
  518. else
  519. {
  520. hi2s->RxXferSize = Size;
  521. hi2s->RxXferCount = Size;
  522. }
  523. /* Process Locked */
  524. __HAL_LOCK(hi2s);
  525. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  526. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  527. /* Check if the I2S is already enabled */
  528. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  529. {
  530. /* Enable I2S peripheral */
  531. __HAL_I2S_ENABLE(hi2s);
  532. }
  533. /* Check if Master Receiver mode is selected */
  534. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  535. {
  536. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  537. access to the SPI_SR register. */
  538. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  539. }
  540. /* Receive data */
  541. while(hi2s->RxXferCount > 0U)
  542. {
  543. /* Wait until RXNE flag is set */
  544. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
  545. {
  546. /* Set the error code and execute error callback*/
  547. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_TIMEOUT);
  548. HAL_I2S_ErrorCallback(hi2s);
  549. return HAL_TIMEOUT;
  550. }
  551. /* Check if an overrun occurs */
  552. if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  553. {
  554. /* Clear overrun flag */
  555. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  556. /* Set the I2S State ready */
  557. hi2s->State = HAL_I2S_STATE_READY;
  558. /* Process Unlocked */
  559. __HAL_UNLOCK(hi2s);
  560. /* Set the error code and execute error callback*/
  561. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  562. HAL_I2S_ErrorCallback(hi2s);
  563. return HAL_ERROR;
  564. }
  565. (*pData++) = hi2s->Instance->DR;
  566. hi2s->RxXferCount--;
  567. }
  568. hi2s->State = HAL_I2S_STATE_READY;
  569. /* Process Unlocked */
  570. __HAL_UNLOCK(hi2s);
  571. return HAL_OK;
  572. }
  573. else
  574. {
  575. return HAL_BUSY;
  576. }
  577. }
  578. /**
  579. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  580. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  581. * the configuration information for I2S module
  582. * @param pData: a 16-bit pointer to data buffer.
  583. * @param Size: number of data sample to be sent:
  584. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  585. * configuration phase, the Size parameter means the number of 16-bit data length
  586. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  587. * the Size parameter means the number of 16-bit data length.
  588. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  589. * between Master and Slave(example: audio streaming).
  590. * @retval HAL status
  591. */
  592. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  593. {
  594. uint32_t tmp1 = 0U;
  595. if(hi2s->State == HAL_I2S_STATE_READY)
  596. {
  597. if((pData == NULL) || (Size == 0U))
  598. {
  599. return HAL_ERROR;
  600. }
  601. hi2s->pTxBuffPtr = pData;
  602. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  603. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  604. {
  605. hi2s->TxXferSize = (Size << 1U);
  606. hi2s->TxXferCount = (Size << 1U);
  607. }
  608. else
  609. {
  610. hi2s->TxXferSize = Size;
  611. hi2s->TxXferCount = Size;
  612. }
  613. /* Process Locked */
  614. __HAL_LOCK(hi2s);
  615. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  616. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  617. /* Enable TXE and ERR interrupt */
  618. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  619. /* Check if the I2S is already enabled */
  620. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  621. {
  622. /* Enable I2S peripheral */
  623. __HAL_I2S_ENABLE(hi2s);
  624. }
  625. /* Process Unlocked */
  626. __HAL_UNLOCK(hi2s);
  627. return HAL_OK;
  628. }
  629. else
  630. {
  631. return HAL_BUSY;
  632. }
  633. }
  634. /**
  635. * @brief Receive an amount of data in non-blocking mode with Interrupt
  636. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  637. * the configuration information for I2S module
  638. * @param pData: a 16-bit pointer to the Receive data buffer.
  639. * @param Size: number of data sample to be sent:
  640. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  641. * configuration phase, the Size parameter means the number of 16-bit data length
  642. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  643. * the Size parameter means the number of 16-bit data length.
  644. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  645. * between Master and Slave(example: audio streaming).
  646. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
  647. * between Master and Slave otherwise the I2S interrupt should be optimized.
  648. * @retval HAL status
  649. */
  650. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  651. {
  652. uint32_t tmp1 = 0U;
  653. if(hi2s->State == HAL_I2S_STATE_READY)
  654. {
  655. if((pData == NULL) || (Size == 0U))
  656. {
  657. return HAL_ERROR;
  658. }
  659. hi2s->pRxBuffPtr = pData;
  660. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  661. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  662. {
  663. hi2s->RxXferSize = (Size << 1U);
  664. hi2s->RxXferCount = (Size << 1U);
  665. }
  666. else
  667. {
  668. hi2s->RxXferSize = Size;
  669. hi2s->RxXferCount = Size;
  670. }
  671. /* Process Locked */
  672. __HAL_LOCK(hi2s);
  673. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  674. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  675. /* Enable TXE and ERR interrupt */
  676. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  677. /* Check if the I2S is already enabled */
  678. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  679. {
  680. /* Enable I2S peripheral */
  681. __HAL_I2S_ENABLE(hi2s);
  682. }
  683. /* Process Unlocked */
  684. __HAL_UNLOCK(hi2s);
  685. return HAL_OK;
  686. }
  687. else
  688. {
  689. return HAL_BUSY;
  690. }
  691. }
  692. /**
  693. * @brief Transmit an amount of data in non-blocking mode with DMA
  694. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  695. * the configuration information for I2S module
  696. * @param pData: a 16-bit pointer to the Transmit data buffer.
  697. * @param Size: number of data sample to be sent:
  698. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  699. * configuration phase, the Size parameter means the number of 16-bit data length
  700. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  701. * the Size parameter means the number of 16-bit data length.
  702. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  703. * between Master and Slave(example: audio streaming).
  704. * @retval HAL status
  705. */
  706. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  707. {
  708. uint32_t *tmp = NULL;
  709. uint32_t tmp1 = 0U;
  710. if((pData == NULL) || (Size == 0U))
  711. {
  712. return HAL_ERROR;
  713. }
  714. if(hi2s->State == HAL_I2S_STATE_READY)
  715. {
  716. hi2s->pTxBuffPtr = pData;
  717. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  718. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  719. {
  720. hi2s->TxXferSize = (Size << 1U);
  721. hi2s->TxXferCount = (Size << 1U);
  722. }
  723. else
  724. {
  725. hi2s->TxXferSize = Size;
  726. hi2s->TxXferCount = Size;
  727. }
  728. /* Process Locked */
  729. __HAL_LOCK(hi2s);
  730. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  731. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  732. /* Set the I2S Tx DMA Half transfer complete callback */
  733. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  734. /* Set the I2S Tx DMA transfer complete callback */
  735. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  736. /* Set the DMA error callback */
  737. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  738. /* Enable the Tx DMA Stream */
  739. tmp = (uint32_t*)&pData;
  740. HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
  741. /* Check if the I2S is already enabled */
  742. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  743. {
  744. /* Enable I2S peripheral */
  745. __HAL_I2S_ENABLE(hi2s);
  746. }
  747. /* Check if the I2S Tx request is already enabled */
  748. if((hi2s->Instance->CR2 & SPI_CR2_TXDMAEN) != SPI_CR2_TXDMAEN)
  749. {
  750. /* Enable Tx DMA Request */
  751. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  752. }
  753. /* Process Unlocked */
  754. __HAL_UNLOCK(hi2s);
  755. return HAL_OK;
  756. }
  757. else
  758. {
  759. return HAL_BUSY;
  760. }
  761. }
  762. /**
  763. * @brief Receive an amount of data in non-blocking mode with DMA
  764. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  765. * the configuration information for I2S module
  766. * @param pData: a 16-bit pointer to the Receive data buffer.
  767. * @param Size: number of data sample to be sent:
  768. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  769. * configuration phase, the Size parameter means the number of 16-bit data length
  770. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  771. * the Size parameter means the number of 16-bit data length.
  772. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  773. * between Master and Slave(example: audio streaming).
  774. * @retval HAL status
  775. */
  776. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  777. {
  778. uint32_t *tmp = NULL;
  779. uint32_t tmp1 = 0U;
  780. if((pData == NULL) || (Size == 0U))
  781. {
  782. return HAL_ERROR;
  783. }
  784. if(hi2s->State == HAL_I2S_STATE_READY)
  785. {
  786. hi2s->pRxBuffPtr = pData;
  787. tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  788. if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
  789. {
  790. hi2s->RxXferSize = (Size << 1U);
  791. hi2s->RxXferCount = (Size << 1U);
  792. }
  793. else
  794. {
  795. hi2s->RxXferSize = Size;
  796. hi2s->RxXferCount = Size;
  797. }
  798. /* Process Locked */
  799. __HAL_LOCK(hi2s);
  800. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  801. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  802. /* Set the I2S Rx DMA Half transfer complete callback */
  803. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  804. /* Set the I2S Rx DMA transfer complete callback */
  805. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  806. /* Set the DMA error callback */
  807. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  808. /* Check if Master Receiver mode is selected */
  809. if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  810. {
  811. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  812. access to the SPI_SR register. */
  813. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  814. }
  815. /* Enable the Rx DMA Stream */
  816. tmp = (uint32_t*)&pData;
  817. HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);
  818. /* Check if the I2S is already enabled */
  819. if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  820. {
  821. /* Enable I2S peripheral */
  822. __HAL_I2S_ENABLE(hi2s);
  823. }
  824. /* Check if the I2S Rx request is already enabled */
  825. if((hi2s->Instance->CR2 &SPI_CR2_RXDMAEN) != SPI_CR2_RXDMAEN)
  826. {
  827. /* Enable Rx DMA Request */
  828. SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  829. }
  830. /* Process Unlocked */
  831. __HAL_UNLOCK(hi2s);
  832. return HAL_OK;
  833. }
  834. else
  835. {
  836. return HAL_BUSY;
  837. }
  838. }
  839. /**
  840. * @brief Pauses the audio channel playing from the Media.
  841. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  842. * the configuration information for I2S module
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  846. {
  847. /* Process Locked */
  848. __HAL_LOCK(hi2s);
  849. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  850. {
  851. /* Disable the I2S DMA Tx request */
  852. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  853. }
  854. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  855. {
  856. /* Disable the I2S DMA Rx request */
  857. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  858. }
  859. /* Process Unlocked */
  860. __HAL_UNLOCK(hi2s);
  861. return HAL_OK;
  862. }
  863. /**
  864. * @brief Resumes the audio channel playing from the Media.
  865. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  866. * the configuration information for I2S module
  867. * @retval HAL status
  868. */
  869. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  870. {
  871. /* Process Locked */
  872. __HAL_LOCK(hi2s);
  873. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  874. {
  875. /* Enable the I2S DMA Tx request */
  876. SET_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  877. }
  878. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  879. {
  880. /* Enable the I2S DMA Rx request */
  881. SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  882. }
  883. /* If the I2S peripheral is still not enabled, enable it */
  884. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) == 0U)
  885. {
  886. /* Enable I2S peripheral */
  887. __HAL_I2S_ENABLE(hi2s);
  888. }
  889. /* Process Unlocked */
  890. __HAL_UNLOCK(hi2s);
  891. return HAL_OK;
  892. }
  893. /**
  894. * @brief Resumes the audio channel playing from the Media.
  895. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  896. * the configuration information for I2S module
  897. * @retval HAL status
  898. */
  899. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  900. {
  901. /* Process Locked */
  902. __HAL_LOCK(hi2s);
  903. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  904. {
  905. /* Disable the I2S DMA requests */
  906. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  907. /* Disable the I2S DMA Channel */
  908. HAL_DMA_Abort(hi2s->hdmatx);
  909. }
  910. else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  911. {
  912. /* Disable the I2S DMA requests */
  913. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  914. /* Disable the I2S DMA Channel */
  915. HAL_DMA_Abort(hi2s->hdmarx);
  916. }
  917. /* Disable I2S peripheral */
  918. __HAL_I2S_DISABLE(hi2s);
  919. hi2s->State = HAL_I2S_STATE_READY;
  920. /* Process Unlocked */
  921. __HAL_UNLOCK(hi2s);
  922. return HAL_OK;
  923. }
  924. /**
  925. * @brief This function handles I2S interrupt request.
  926. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  927. * the configuration information for I2S module
  928. * @retval None
  929. */
  930. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  931. {
  932. /* Call the IrqHandler ISR set during HAL_I2S_INIT */
  933. hi2s->IrqHandlerISR(hi2s);
  934. }
  935. /**
  936. * @brief Tx Transfer Half completed callbacks
  937. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  938. * the configuration information for I2S module
  939. * @retval None
  940. */
  941. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  942. {
  943. /* Prevent unused argument(s) compilation warning */
  944. UNUSED(hi2s);
  945. /* NOTE : This function Should not be modified, when the callback is needed,
  946. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  947. */
  948. }
  949. /**
  950. * @brief Tx Transfer completed callbacks
  951. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  952. * the configuration information for I2S module
  953. * @retval None
  954. */
  955. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  956. {
  957. /* Prevent unused argument(s) compilation warning */
  958. UNUSED(hi2s);
  959. /* NOTE : This function Should not be modified, when the callback is needed,
  960. the HAL_I2S_TxCpltCallback could be implemented in the user file
  961. */
  962. }
  963. /**
  964. * @brief Rx Transfer half completed 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_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  970. {
  971. /* Prevent unused argument(s) compilation warning */
  972. UNUSED(hi2s);
  973. /* NOTE : This function Should not be modified, when the callback is needed,
  974. the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
  975. */
  976. }
  977. /**
  978. * @brief Rx Transfer completed callbacks
  979. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  980. * the configuration information for I2S module
  981. * @retval None
  982. */
  983. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  984. {
  985. /* Prevent unused argument(s) compilation warning */
  986. UNUSED(hi2s);
  987. /* NOTE : This function Should not be modified, when the callback is needed,
  988. the HAL_I2S_RxCpltCallback could be implemented in the user file
  989. */
  990. }
  991. /**
  992. * @brief I2S error callbacks
  993. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  994. * the configuration information for I2S module
  995. * @retval None
  996. */
  997. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  998. {
  999. /* Prevent unused argument(s) compilation warning */
  1000. UNUSED(hi2s);
  1001. /* NOTE : This function Should not be modified, when the callback is needed,
  1002. the HAL_I2S_ErrorCallback could be implemented in the user file
  1003. */
  1004. }
  1005. /**
  1006. * @}
  1007. */
  1008. /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
  1009. * @brief Peripheral State functions
  1010. *
  1011. @verbatim
  1012. ===============================================================================
  1013. ##### Peripheral State and Errors functions #####
  1014. ===============================================================================
  1015. [..]
  1016. This subsection permits to get in run-time the status of the peripheral
  1017. and the data flow.
  1018. @endverbatim
  1019. * @{
  1020. */
  1021. /**
  1022. * @brief Return the I2S state
  1023. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1024. * the configuration information for I2S module
  1025. * @retval HAL state
  1026. */
  1027. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  1028. {
  1029. return hi2s->State;
  1030. }
  1031. /**
  1032. * @brief Return the I2S error code
  1033. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1034. * the configuration information for I2S module
  1035. * @retval I2S Error Code
  1036. */
  1037. uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1038. {
  1039. return hi2s->ErrorCode;
  1040. }
  1041. /**
  1042. * @}
  1043. */
  1044. /**
  1045. * @}
  1046. */
  1047. /* Private functions ---------------------------------------------------------*/
  1048. /** @addtogroup I2S_Private_Functions I2S Private Functions
  1049. * @{
  1050. */
  1051. /**
  1052. * @brief DMA I2S transmit process complete callback
  1053. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1054. * the configuration information for the specified DMA module.
  1055. * @retval None
  1056. */
  1057. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1058. {
  1059. I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1060. if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  1061. {
  1062. /* Disable Tx DMA Request */
  1063. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN);
  1064. hi2s->TxXferCount = 0U;
  1065. hi2s->State = HAL_I2S_STATE_READY;
  1066. }
  1067. HAL_I2S_TxCpltCallback(hi2s);
  1068. }
  1069. /**
  1070. * @brief DMA I2S transmit process half complete callback
  1071. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1072. * the configuration information for the specified DMA module.
  1073. * @retval None
  1074. */
  1075. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1076. {
  1077. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1078. HAL_I2S_TxHalfCpltCallback(hi2s);
  1079. }
  1080. /**
  1081. * @brief DMA I2S receive process complete callback
  1082. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1083. * the configuration information for the specified DMA module.
  1084. * @retval None
  1085. */
  1086. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1087. {
  1088. I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1089. if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  1090. {
  1091. /* Disable Rx DMA Request */
  1092. CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN);
  1093. hi2s->RxXferCount = 0U;
  1094. hi2s->State = HAL_I2S_STATE_READY;
  1095. }
  1096. HAL_I2S_RxCpltCallback(hi2s);
  1097. }
  1098. /**
  1099. * @brief DMA I2S receive process half complete callback
  1100. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1101. * the configuration information for the specified DMA module.
  1102. * @retval None
  1103. */
  1104. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1105. {
  1106. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1107. HAL_I2S_RxHalfCpltCallback(hi2s);
  1108. }
  1109. /**
  1110. * @brief DMA I2S communication error callback
  1111. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1112. * the configuration information for the specified DMA module.
  1113. * @retval None
  1114. */
  1115. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1116. {
  1117. I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1118. /* Disable Rx and Tx DMA Request */
  1119. CLEAR_BIT(hi2s->Instance->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1120. hi2s->TxXferCount = 0U;
  1121. hi2s->RxXferCount = 0U;
  1122. hi2s->State= HAL_I2S_STATE_READY;
  1123. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_DMA);
  1124. HAL_I2S_ErrorCallback(hi2s);
  1125. }
  1126. /**
  1127. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1128. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1129. * the configuration information for I2S module
  1130. * @retval HAL status
  1131. */
  1132. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1133. {
  1134. /* Transmit data */
  1135. hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
  1136. hi2s->TxXferCount--;
  1137. if(hi2s->TxXferCount == 0U)
  1138. {
  1139. /* Disable TXE and ERR interrupt */
  1140. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1141. hi2s->State = HAL_I2S_STATE_READY;
  1142. HAL_I2S_TxCpltCallback(hi2s);
  1143. }
  1144. }
  1145. /**
  1146. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1147. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1148. * the configuration information for I2S module
  1149. * @retval HAL status
  1150. */
  1151. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1152. {
  1153. /* Receive data */
  1154. (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
  1155. hi2s->RxXferCount--;
  1156. if(hi2s->RxXferCount == 0U)
  1157. {
  1158. /* Disable RXNE and ERR interrupt */
  1159. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1160. hi2s->State = HAL_I2S_STATE_READY;
  1161. HAL_I2S_RxCpltCallback(hi2s);
  1162. }
  1163. }
  1164. /**
  1165. * @brief This function handles I2S interrupt request.
  1166. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1167. * the configuration information for I2S module
  1168. * @retval None
  1169. */
  1170. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1171. {
  1172. __IO uint32_t i2ssr = hi2s->Instance->SR;
  1173. if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1174. {
  1175. /* I2S in mode Receiver ------------------------------------------------*/
  1176. if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
  1177. {
  1178. I2S_Receive_IT(hi2s);
  1179. }
  1180. /* I2S Overrun error interrupt occured -------------------------------------*/
  1181. if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1182. {
  1183. /* Disable RXNE and ERR interrupt */
  1184. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1185. /* Clear Overrun flag */
  1186. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1187. /* Set the I2S State ready */
  1188. hi2s->State = HAL_I2S_STATE_READY;
  1189. /* Set the error code and execute error callback*/
  1190. SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_OVR);
  1191. HAL_I2S_ErrorCallback(hi2s);
  1192. }
  1193. }
  1194. if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1195. {
  1196. /* I2S in mode Transmitter -----------------------------------------------*/
  1197. if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
  1198. {
  1199. I2S_Transmit_IT(hi2s);
  1200. }
  1201. /* I2S Underrun error interrupt occurred --------------------------------*/
  1202. if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1203. {
  1204. /* Disable TXE and ERR interrupt */
  1205. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1206. /* Clear Underrun flag */
  1207. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  1208. /* Set the I2S State ready */
  1209. hi2s->State = HAL_I2S_STATE_READY;
  1210. /* Set the error code and execute error callback*/
  1211. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  1212. HAL_I2S_ErrorCallback(hi2s);
  1213. }
  1214. }
  1215. }
  1216. /**
  1217. * @brief This function handles I2S Communication Timeout.
  1218. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1219. * the configuration information for I2S module
  1220. * @param Flag: Flag checked
  1221. * @param State: Value of the flag expected
  1222. * @param Timeout: Duration of the timeout
  1223. * @retval HAL status
  1224. */
  1225. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State,
  1226. uint32_t Timeout)
  1227. {
  1228. uint32_t tickstart = HAL_GetTick();
  1229. /* Wait until flag is set to status*/
  1230. while(((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  1231. {
  1232. if(Timeout != HAL_MAX_DELAY)
  1233. {
  1234. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1235. {
  1236. /* Set the I2S State ready */
  1237. hi2s->State = HAL_I2S_STATE_READY;
  1238. /* Process Unlocked */
  1239. __HAL_UNLOCK(hi2s);
  1240. return HAL_TIMEOUT;
  1241. }
  1242. }
  1243. }
  1244. return HAL_OK;
  1245. }
  1246. /**
  1247. * @}
  1248. */
  1249. /**
  1250. * @}
  1251. */
  1252. #endif /* STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  1253. #endif /* HAL_I2S_MODULE_ENABLED */
  1254. /**
  1255. * @}
  1256. */
  1257. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/