stm32f1xx_hal_i2s.c 48 KB

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