stm32f4xx_hal_i2s.c 52 KB

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