stm32f4xx_hal_i2s.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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/Channel.
  30. (+++) Enable the DMAx interface clock.
  31. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  32. (+++) Configure the DMA Tx/Rx Stream/Channel.
  33. (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle.
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  35. DMA Tx/Rx Stream/Channel.
  36. (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
  37. using HAL_I2S_Init() function.
  38. -@- The specific I2S interrupts (Transmission complete interrupt,
  39. RXNE interrupt and Error Interrupts) will be managed using the macros
  40. __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
  41. -@- Make sure that either:
  42. (+@) I2S PLL clock 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 mode of operations 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. *** I2S HAL driver macros list ***
  96. ===================================
  97. [..]
  98. Callback registration:
  99. (#) The compilation flag USE_HAL_I2S_REGISTER_CALLBACKS when set to 1U
  100. allows the user to configure dynamically the driver callbacks.
  101. Use Functions HAL_I2S_RegisterCallback() to register an interrupt callback.
  102. Function HAL_I2S_RegisterCallback() allows to register following callbacks:
  103. (++) TxCpltCallback : I2S Tx Completed callback
  104. (++) RxCpltCallback : I2S Rx Completed callback
  105. (++) TxRxCpltCallback : I2S TxRx Completed callback
  106. (++) TxHalfCpltCallback : I2S Tx Half Completed callback
  107. (++) RxHalfCpltCallback : I2S Rx Half Completed callback
  108. (++) ErrorCallback : I2S Error callback
  109. (++) MspInitCallback : I2S Msp Init callback
  110. (++) MspDeInitCallback : I2S Msp DeInit callback
  111. This function takes as parameters the HAL peripheral handle, the Callback ID
  112. and a pointer to the user callback function.
  113. (#) Use function HAL_I2S_UnRegisterCallback to reset a callback to the default
  114. weak function.
  115. HAL_I2S_UnRegisterCallback takes as parameters the HAL peripheral handle,
  116. and the Callback ID.
  117. This function allows to reset following callbacks:
  118. (++) TxCpltCallback : I2S Tx Completed callback
  119. (++) RxCpltCallback : I2S Rx Completed callback
  120. (++) TxRxCpltCallback : I2S TxRx Completed callback
  121. (++) TxHalfCpltCallback : I2S Tx Half Completed callback
  122. (++) RxHalfCpltCallback : I2S Rx Half Completed callback
  123. (++) ErrorCallback : I2S Error callback
  124. (++) MspInitCallback : I2S Msp Init callback
  125. (++) MspDeInitCallback : I2S Msp DeInit callback
  126. [..]
  127. By default, after the HAL_I2S_Init() and when the state is HAL_I2S_STATE_RESET
  128. all callbacks are set to the corresponding weak functions:
  129. examples HAL_I2S_MasterTxCpltCallback(), HAL_I2S_MasterRxCpltCallback().
  130. Exception done for MspInit and MspDeInit functions that are
  131. reset to the legacy weak functions in the HAL_I2S_Init()/ HAL_I2S_DeInit() only when
  132. these callbacks are null (not registered beforehand).
  133. If MspInit or MspDeInit are not null, the HAL_I2S_Init()/ HAL_I2S_DeInit()
  134. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  135. [..]
  136. Callbacks can be registered/unregistered in HAL_I2S_STATE_READY state only.
  137. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  138. in HAL_I2S_STATE_READY or HAL_I2S_STATE_RESET state,
  139. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  140. Then, the user first registers the MspInit/MspDeInit user callbacks
  141. using HAL_I2S_RegisterCallback() before calling HAL_I2S_DeInit()
  142. or HAL_I2S_Init() function.
  143. [..]
  144. When the compilation define USE_HAL_I2S_REGISTER_CALLBACKS is set to 0 or
  145. not defined, the callback registering feature is not available
  146. and weak (surcharged) callbacks are used.
  147. @endverbatim
  148. ******************************************************************************
  149. * @attention
  150. *
  151. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  152. * All rights reserved.</center></h2>
  153. *
  154. * This software component is licensed by ST under BSD 3-Clause license,
  155. * the "License"; You may not use this file except in compliance with the
  156. * License. You may obtain a copy of the License at:
  157. * opensource.org/licenses/BSD-3-Clause
  158. *
  159. ******************************************************************************
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "stm32f4xx_hal.h"
  163. #ifdef HAL_I2S_MODULE_ENABLED
  164. /** @addtogroup STM32F4xx_HAL_Driver
  165. * @{
  166. */
  167. /** @defgroup I2S I2S
  168. * @brief I2S HAL module driver
  169. * @{
  170. */
  171. /* Private typedef -----------------------------------------------------------*/
  172. /* Private define ------------------------------------------------------------*/
  173. /* Private macro -------------------------------------------------------------*/
  174. /* Private variables ---------------------------------------------------------*/
  175. /* Private function prototypes -----------------------------------------------*/
  176. /** @defgroup I2S_Private_Functions I2S Private Functions
  177. * @{
  178. */
  179. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
  180. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  181. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
  182. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  183. static void I2S_DMAError(DMA_HandleTypeDef *hdma);
  184. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
  185. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
  186. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s);
  187. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State,
  188. uint32_t Timeout);
  189. /**
  190. * @}
  191. */
  192. /* Exported functions ---------------------------------------------------------*/
  193. /** @defgroup I2S_Exported_Functions I2S Exported Functions
  194. * @{
  195. */
  196. /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
  197. * @brief Initialization and Configuration functions
  198. *
  199. @verbatim
  200. ===============================================================================
  201. ##### Initialization and de-initialization functions #####
  202. ===============================================================================
  203. [..] This subsection provides a set of functions allowing to initialize and
  204. de-initialize the I2Sx peripheral in simplex mode:
  205. (+) User must Implement HAL_I2S_MspInit() function in which he configures
  206. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  207. (+) Call the function HAL_I2S_Init() to configure the selected device with
  208. the selected configuration:
  209. (++) Mode
  210. (++) Standard
  211. (++) Data Format
  212. (++) MCLK Output
  213. (++) Audio frequency
  214. (++) Polarity
  215. (++) Full duplex mode
  216. (+) Call the function HAL_I2S_DeInit() to restore the default configuration
  217. of the selected I2Sx peripheral.
  218. @endverbatim
  219. * @{
  220. */
  221. /**
  222. * @brief Initializes the I2S according to the specified parameters
  223. * in the I2S_InitTypeDef and create the associated handle.
  224. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  225. * the configuration information for I2S module
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  229. {
  230. uint32_t i2sdiv;
  231. uint32_t i2sodd;
  232. uint32_t packetlength;
  233. uint32_t tmp;
  234. uint32_t i2sclk;
  235. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  236. uint16_t tmpreg;
  237. #endif
  238. /* Check the I2S handle allocation */
  239. if (hi2s == NULL)
  240. {
  241. return HAL_ERROR;
  242. }
  243. /* Check the I2S parameters */
  244. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  245. assert_param(IS_I2S_MODE(hi2s->Init.Mode));
  246. assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
  247. assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
  248. assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
  249. assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
  250. assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
  251. assert_param(IS_I2S_CLOCKSOURCE(hi2s->Init.ClockSource));
  252. if (hi2s->State == HAL_I2S_STATE_RESET)
  253. {
  254. /* Allocate lock resource and initialize it */
  255. hi2s->Lock = HAL_UNLOCKED;
  256. /* Initialize Default I2S IrqHandler ISR */
  257. hi2s->IrqHandlerISR = I2S_IRQHandler;
  258. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  259. /* Init the I2S Callback settings */
  260. hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */
  261. hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */
  262. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  263. hi2s->TxRxCpltCallback = HAL_I2SEx_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  264. #endif
  265. hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  266. hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  267. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  268. hi2s->TxRxHalfCpltCallback = HAL_I2SEx_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
  269. #endif
  270. hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */
  271. if (hi2s->MspInitCallback == NULL)
  272. {
  273. hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */
  274. }
  275. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  276. hi2s->MspInitCallback(hi2s);
  277. #else
  278. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  279. HAL_I2S_MspInit(hi2s);
  280. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  281. }
  282. hi2s->State = HAL_I2S_STATE_BUSY;
  283. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
  284. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  285. CLEAR_BIT(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  286. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  287. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD));
  288. hi2s->Instance->I2SPR = 0x0002U;
  289. /*----------------------- I2SPR: I2SDIV and ODD Calculation -----------------*/
  290. /* If the requested audio frequency is not the default, compute the prescaler */
  291. if (hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT)
  292. {
  293. /* Check the frame length (For the Prescaler computing) ********************/
  294. if (hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)
  295. {
  296. /* Packet length is 16 bits */
  297. packetlength = 16U;
  298. }
  299. else
  300. {
  301. /* Packet length is 32 bits */
  302. packetlength = 32U;
  303. }
  304. /* I2S standard */
  305. if (hi2s->Init.Standard <= I2S_STANDARD_LSB)
  306. {
  307. /* In I2S standard packet lenght is multiplied by 2 */
  308. packetlength = packetlength * 2U;
  309. }
  310. /* Get the source clock value **********************************************/
  311. #if defined(I2S_APB1_APB2_FEATURE)
  312. if (IS_I2S_APB1_INSTANCE(hi2s->Instance))
  313. {
  314. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB1);
  315. }
  316. else
  317. {
  318. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB2);
  319. }
  320. #else
  321. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S);
  322. #endif
  323. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  324. if (hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
  325. {
  326. /* MCLK output is enabled */
  327. if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  328. {
  329. tmp = (uint32_t)(((((i2sclk / (packetlength * 4U)) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  330. }
  331. else
  332. {
  333. tmp = (uint32_t)(((((i2sclk / (packetlength * 8U)) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  334. }
  335. }
  336. else
  337. {
  338. /* MCLK output is disabled */
  339. tmp = (uint32_t)(((((i2sclk / packetlength) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  340. }
  341. /* Remove the flatting point */
  342. tmp = tmp / 10U;
  343. /* Check the parity of the divider */
  344. i2sodd = (uint32_t)(tmp & (uint32_t)1U);
  345. /* Compute the i2sdiv prescaler */
  346. i2sdiv = (uint32_t)((tmp - i2sodd) / 2U);
  347. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  348. i2sodd = (uint32_t)(i2sodd << 8U);
  349. }
  350. else
  351. {
  352. /* Set the default values */
  353. i2sdiv = 2U;
  354. i2sodd = 0U;
  355. }
  356. /* Test if the divider is 1 or 0 or greater than 0xFF */
  357. if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  358. {
  359. /* Set the error code and execute error callback*/
  360. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER);
  361. return HAL_ERROR;
  362. }
  363. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
  364. /* Write to SPIx I2SPR register the computed value */
  365. hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
  366. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  367. /* And configure the I2S with the I2S_InitStruct values */
  368. MODIFY_REG(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \
  369. SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \
  370. SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  371. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD), \
  372. (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode | \
  373. hi2s->Init.Standard | hi2s->Init.DataFormat | \
  374. hi2s->Init.CPOL));
  375. #if defined(SPI_I2SCFGR_ASTRTEN)
  376. if ((hi2s->Init.Standard == I2S_STANDARD_PCM_SHORT) || ((hi2s->Init.Standard == I2S_STANDARD_PCM_LONG)))
  377. {
  378. /* Write to SPIx I2SCFGR */
  379. SET_BIT(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_ASTRTEN);
  380. }
  381. #endif /* SPI_I2SCFGR_ASTRTEN */
  382. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  383. /* Configure the I2S extended if the full duplex mode is enabled */
  384. assert_param(IS_I2S_FULLDUPLEX_MODE(hi2s->Init.FullDuplexMode));
  385. if (hi2s->Init.FullDuplexMode == I2S_FULLDUPLEXMODE_ENABLE)
  386. {
  387. /* Set FullDuplex I2S IrqHandler ISR if FULLDUPLEXMODE is enabled */
  388. hi2s->IrqHandlerISR = HAL_I2SEx_FullDuplex_IRQHandler;
  389. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  390. CLEAR_BIT(I2SxEXT(hi2s->Instance)->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \
  391. SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  392. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD));
  393. I2SxEXT(hi2s->Instance)->I2SPR = 2U;
  394. /* Get the I2SCFGR register value */
  395. tmpreg = I2SxEXT(hi2s->Instance)->I2SCFGR;
  396. /* Get the mode to be configured for the extended I2S */
  397. if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
  398. {
  399. tmp = I2S_MODE_SLAVE_RX;
  400. }
  401. else /* I2S_MODE_MASTER_RX || I2S_MODE_SLAVE_RX */
  402. {
  403. tmp = I2S_MODE_SLAVE_TX;
  404. }
  405. /* Configure the I2S Slave with the I2S Master parameter values */
  406. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(tmp | \
  407. (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
  408. (uint16_t)hi2s->Init.CPOL))));
  409. /* Write to SPIx I2SCFGR */
  410. WRITE_REG(I2SxEXT(hi2s->Instance)->I2SCFGR, tmpreg);
  411. }
  412. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  413. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  414. hi2s->State = HAL_I2S_STATE_READY;
  415. return HAL_OK;
  416. }
  417. /**
  418. * @brief DeInitializes the I2S peripheral
  419. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  420. * the configuration information for I2S module
  421. * @retval HAL status
  422. */
  423. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  424. {
  425. /* Check the I2S handle allocation */
  426. if (hi2s == NULL)
  427. {
  428. return HAL_ERROR;
  429. }
  430. /* Check the parameters */
  431. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  432. hi2s->State = HAL_I2S_STATE_BUSY;
  433. /* Disable the I2S Peripheral Clock */
  434. __HAL_I2S_DISABLE(hi2s);
  435. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  436. if (hi2s->MspDeInitCallback == NULL)
  437. {
  438. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  439. }
  440. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  441. hi2s->MspDeInitCallback(hi2s);
  442. #else
  443. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  444. HAL_I2S_MspDeInit(hi2s);
  445. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  446. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  447. hi2s->State = HAL_I2S_STATE_RESET;
  448. /* Release Lock */
  449. __HAL_UNLOCK(hi2s);
  450. return HAL_OK;
  451. }
  452. /**
  453. * @brief I2S MSP Init
  454. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  455. * the configuration information for I2S module
  456. * @retval None
  457. */
  458. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  459. {
  460. /* Prevent unused argument(s) compilation warning */
  461. UNUSED(hi2s);
  462. /* NOTE : This function Should not be modified, when the callback is needed,
  463. the HAL_I2S_MspInit could be implemented in the user file
  464. */
  465. }
  466. /**
  467. * @brief I2S MSP DeInit
  468. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  469. * the configuration information for I2S module
  470. * @retval None
  471. */
  472. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  473. {
  474. /* Prevent unused argument(s) compilation warning */
  475. UNUSED(hi2s);
  476. /* NOTE : This function Should not be modified, when the callback is needed,
  477. the HAL_I2S_MspDeInit could be implemented in the user file
  478. */
  479. }
  480. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  481. /**
  482. * @brief Register a User I2S Callback
  483. * To be used instead of the weak predefined callback
  484. * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
  485. * the configuration information for the specified I2S.
  486. * @param CallbackID ID of the callback to be registered
  487. * @param pCallback pointer to the Callback function
  488. * @retval HAL status
  489. */
  490. HAL_StatusTypeDef HAL_I2S_RegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID,
  491. pI2S_CallbackTypeDef pCallback)
  492. {
  493. HAL_StatusTypeDef status = HAL_OK;
  494. if (pCallback == NULL)
  495. {
  496. /* Update the error code */
  497. hi2s->ErrorCode |= HAL_I2S_ERROR_INVALID_CALLBACK;
  498. return HAL_ERROR;
  499. }
  500. /* Process locked */
  501. __HAL_LOCK(hi2s);
  502. if (HAL_I2S_STATE_READY == hi2s->State)
  503. {
  504. switch (CallbackID)
  505. {
  506. case HAL_I2S_TX_COMPLETE_CB_ID :
  507. hi2s->TxCpltCallback = pCallback;
  508. break;
  509. case HAL_I2S_RX_COMPLETE_CB_ID :
  510. hi2s->RxCpltCallback = pCallback;
  511. break;
  512. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  513. case HAL_I2S_TX_RX_COMPLETE_CB_ID :
  514. hi2s->TxRxCpltCallback = pCallback;
  515. break;
  516. #endif
  517. case HAL_I2S_TX_HALF_COMPLETE_CB_ID :
  518. hi2s->TxHalfCpltCallback = pCallback;
  519. break;
  520. case HAL_I2S_RX_HALF_COMPLETE_CB_ID :
  521. hi2s->RxHalfCpltCallback = pCallback;
  522. break;
  523. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  524. case HAL_I2S_TX_RX_HALF_COMPLETE_CB_ID :
  525. hi2s->TxRxHalfCpltCallback = pCallback;
  526. break;
  527. #endif
  528. case HAL_I2S_ERROR_CB_ID :
  529. hi2s->ErrorCallback = pCallback;
  530. break;
  531. case HAL_I2S_MSPINIT_CB_ID :
  532. hi2s->MspInitCallback = pCallback;
  533. break;
  534. case HAL_I2S_MSPDEINIT_CB_ID :
  535. hi2s->MspDeInitCallback = pCallback;
  536. break;
  537. default :
  538. /* Update the error code */
  539. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  540. /* Return error status */
  541. status = HAL_ERROR;
  542. break;
  543. }
  544. }
  545. else if (HAL_I2S_STATE_RESET == hi2s->State)
  546. {
  547. switch (CallbackID)
  548. {
  549. case HAL_I2S_MSPINIT_CB_ID :
  550. hi2s->MspInitCallback = pCallback;
  551. break;
  552. case HAL_I2S_MSPDEINIT_CB_ID :
  553. hi2s->MspDeInitCallback = pCallback;
  554. break;
  555. default :
  556. /* Update the error code */
  557. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  558. /* Return error status */
  559. status = HAL_ERROR;
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. /* Update the error code */
  566. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  567. /* Return error status */
  568. status = HAL_ERROR;
  569. }
  570. /* Release Lock */
  571. __HAL_UNLOCK(hi2s);
  572. return status;
  573. }
  574. /**
  575. * @brief Unregister an I2S Callback
  576. * I2S callback is redirected to the weak predefined callback
  577. * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
  578. * the configuration information for the specified I2S.
  579. * @param CallbackID ID of the callback to be unregistered
  580. * @retval HAL status
  581. */
  582. HAL_StatusTypeDef HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID)
  583. {
  584. HAL_StatusTypeDef status = HAL_OK;
  585. /* Process locked */
  586. __HAL_LOCK(hi2s);
  587. if (HAL_I2S_STATE_READY == hi2s->State)
  588. {
  589. switch (CallbackID)
  590. {
  591. case HAL_I2S_TX_COMPLETE_CB_ID :
  592. hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */
  593. break;
  594. case HAL_I2S_RX_COMPLETE_CB_ID :
  595. hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */
  596. break;
  597. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  598. case HAL_I2S_TX_RX_COMPLETE_CB_ID :
  599. hi2s->TxRxCpltCallback = HAL_I2SEx_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  600. break;
  601. #endif
  602. case HAL_I2S_TX_HALF_COMPLETE_CB_ID :
  603. hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  604. break;
  605. case HAL_I2S_RX_HALF_COMPLETE_CB_ID :
  606. hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  607. break;
  608. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  609. case HAL_I2S_TX_RX_HALF_COMPLETE_CB_ID :
  610. hi2s->TxRxHalfCpltCallback = HAL_I2SEx_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
  611. break;
  612. #endif
  613. case HAL_I2S_ERROR_CB_ID :
  614. hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */
  615. break;
  616. case HAL_I2S_MSPINIT_CB_ID :
  617. hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */
  618. break;
  619. case HAL_I2S_MSPDEINIT_CB_ID :
  620. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  621. break;
  622. default :
  623. /* Update the error code */
  624. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  625. /* Return error status */
  626. status = HAL_ERROR;
  627. break;
  628. }
  629. }
  630. else if (HAL_I2S_STATE_RESET == hi2s->State)
  631. {
  632. switch (CallbackID)
  633. {
  634. case HAL_I2S_MSPINIT_CB_ID :
  635. hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */
  636. break;
  637. case HAL_I2S_MSPDEINIT_CB_ID :
  638. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  639. break;
  640. default :
  641. /* Update the error code */
  642. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  643. /* Return error status */
  644. status = HAL_ERROR;
  645. break;
  646. }
  647. }
  648. else
  649. {
  650. /* Update the error code */
  651. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  652. /* Return error status */
  653. status = HAL_ERROR;
  654. }
  655. /* Release Lock */
  656. __HAL_UNLOCK(hi2s);
  657. return status;
  658. }
  659. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  660. /**
  661. * @}
  662. */
  663. /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
  664. * @brief Data transfers functions
  665. *
  666. @verbatim
  667. ===============================================================================
  668. ##### IO operation functions #####
  669. ===============================================================================
  670. [..]
  671. This subsection provides a set of functions allowing to manage the I2S data
  672. transfers.
  673. (#) There are two modes of transfer:
  674. (++) Blocking mode : The communication is performed in the polling mode.
  675. The status of all data processing is returned by the same function
  676. after finishing transfer.
  677. (++) No-Blocking mode : The communication is performed using Interrupts
  678. or DMA. These functions return the status of the transfer startup.
  679. The end of the data processing will be indicated through the
  680. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  681. using DMA mode.
  682. (#) Blocking mode functions are :
  683. (++) HAL_I2S_Transmit()
  684. (++) HAL_I2S_Receive()
  685. (#) No-Blocking mode functions with Interrupt are :
  686. (++) HAL_I2S_Transmit_IT()
  687. (++) HAL_I2S_Receive_IT()
  688. (#) No-Blocking mode functions with DMA are :
  689. (++) HAL_I2S_Transmit_DMA()
  690. (++) HAL_I2S_Receive_DMA()
  691. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  692. (++) HAL_I2S_TxCpltCallback()
  693. (++) HAL_I2S_RxCpltCallback()
  694. (++) HAL_I2S_ErrorCallback()
  695. @endverbatim
  696. * @{
  697. */
  698. /**
  699. * @brief Transmit an amount of data in blocking mode
  700. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  701. * the configuration information for I2S module
  702. * @param pData a 16-bit pointer to data buffer.
  703. * @param Size number of data sample to be sent:
  704. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  705. * configuration phase, the Size parameter means the number of 16-bit data length
  706. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  707. * the Size parameter means the number of 16-bit data length.
  708. * @param Timeout Timeout duration
  709. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  710. * between Master and Slave(example: audio streaming).
  711. * @retval HAL status
  712. */
  713. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  714. {
  715. uint32_t tmpreg_cfgr;
  716. if ((pData == NULL) || (Size == 0U))
  717. {
  718. return HAL_ERROR;
  719. }
  720. /* Process Locked */
  721. __HAL_LOCK(hi2s);
  722. if (hi2s->State != HAL_I2S_STATE_READY)
  723. {
  724. __HAL_UNLOCK(hi2s);
  725. return HAL_BUSY;
  726. }
  727. /* Set state and reset error code */
  728. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  729. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  730. hi2s->pTxBuffPtr = pData;
  731. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  732. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  733. {
  734. hi2s->TxXferSize = (Size << 1U);
  735. hi2s->TxXferCount = (Size << 1U);
  736. }
  737. else
  738. {
  739. hi2s->TxXferSize = Size;
  740. hi2s->TxXferCount = Size;
  741. }
  742. tmpreg_cfgr = hi2s->Instance->I2SCFGR;
  743. /* Check if the I2S is already enabled */
  744. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  745. {
  746. /* Enable I2S peripheral */
  747. __HAL_I2S_ENABLE(hi2s);
  748. }
  749. /* Wait until TXE flag is set */
  750. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  751. {
  752. /* Set the error code */
  753. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  754. hi2s->State = HAL_I2S_STATE_READY;
  755. __HAL_UNLOCK(hi2s);
  756. return HAL_ERROR;
  757. }
  758. while (hi2s->TxXferCount > 0U)
  759. {
  760. hi2s->Instance->DR = (*hi2s->pTxBuffPtr);
  761. hi2s->pTxBuffPtr++;
  762. hi2s->TxXferCount--;
  763. /* Wait until TXE flag is set */
  764. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  765. {
  766. /* Set the error code */
  767. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  768. hi2s->State = HAL_I2S_STATE_READY;
  769. __HAL_UNLOCK(hi2s);
  770. return HAL_ERROR;
  771. }
  772. /* Check if an underrun occurs */
  773. if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
  774. {
  775. /* Clear underrun flag */
  776. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  777. /* Set the error code */
  778. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  779. }
  780. }
  781. /* Check if Slave mode is selected */
  782. if (((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX)
  783. || ((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_RX))
  784. {
  785. /* Wait until Busy flag is reset */
  786. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK)
  787. {
  788. /* Set the error code */
  789. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  790. hi2s->State = HAL_I2S_STATE_READY;
  791. __HAL_UNLOCK(hi2s);
  792. return HAL_ERROR;
  793. }
  794. }
  795. hi2s->State = HAL_I2S_STATE_READY;
  796. __HAL_UNLOCK(hi2s);
  797. return HAL_OK;
  798. }
  799. /**
  800. * @brief Receive an amount of data in blocking mode
  801. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  802. * the configuration information for I2S module
  803. * @param pData a 16-bit pointer to data buffer.
  804. * @param Size number of data sample to be sent:
  805. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  806. * configuration phase, the Size parameter means the number of 16-bit data length
  807. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  808. * the Size parameter means the number of 16-bit data length.
  809. * @param Timeout Timeout duration
  810. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  811. * between Master and Slave(example: audio streaming).
  812. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  813. * in continuous way and as the I2S is not disabled at the end of the I2S transaction.
  814. * @retval HAL status
  815. */
  816. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  817. {
  818. uint32_t tmpreg_cfgr;
  819. if ((pData == NULL) || (Size == 0U))
  820. {
  821. return HAL_ERROR;
  822. }
  823. /* Process Locked */
  824. __HAL_LOCK(hi2s);
  825. if (hi2s->State != HAL_I2S_STATE_READY)
  826. {
  827. __HAL_UNLOCK(hi2s);
  828. return HAL_BUSY;
  829. }
  830. /* Set state and reset error code */
  831. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  832. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  833. hi2s->pRxBuffPtr = pData;
  834. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  835. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  836. {
  837. hi2s->RxXferSize = (Size << 1U);
  838. hi2s->RxXferCount = (Size << 1U);
  839. }
  840. else
  841. {
  842. hi2s->RxXferSize = Size;
  843. hi2s->RxXferCount = Size;
  844. }
  845. /* Check if the I2S is already enabled */
  846. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  847. {
  848. /* Enable I2S peripheral */
  849. __HAL_I2S_ENABLE(hi2s);
  850. }
  851. /* Check if Master Receiver mode is selected */
  852. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  853. {
  854. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  855. access to the SPI_SR register. */
  856. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  857. }
  858. /* Receive data */
  859. while (hi2s->RxXferCount > 0U)
  860. {
  861. /* Wait until RXNE flag is set */
  862. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
  863. {
  864. /* Set the error code */
  865. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  866. hi2s->State = HAL_I2S_STATE_READY;
  867. __HAL_UNLOCK(hi2s);
  868. return HAL_ERROR;
  869. }
  870. (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR;
  871. hi2s->pRxBuffPtr++;
  872. hi2s->RxXferCount--;
  873. /* Check if an overrun occurs */
  874. if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  875. {
  876. /* Clear overrun flag */
  877. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  878. /* Set the error code */
  879. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  880. }
  881. }
  882. hi2s->State = HAL_I2S_STATE_READY;
  883. __HAL_UNLOCK(hi2s);
  884. return HAL_OK;
  885. }
  886. /**
  887. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  888. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  889. * the configuration information for I2S module
  890. * @param pData a 16-bit pointer to data buffer.
  891. * @param Size number of data sample to be sent:
  892. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  893. * configuration phase, the Size parameter means the number of 16-bit data length
  894. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  895. * the Size parameter means the number of 16-bit data length.
  896. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  897. * between Master and Slave(example: audio streaming).
  898. * @retval HAL status
  899. */
  900. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  901. {
  902. uint32_t tmpreg_cfgr;
  903. if ((pData == NULL) || (Size == 0U))
  904. {
  905. return HAL_ERROR;
  906. }
  907. /* Process Locked */
  908. __HAL_LOCK(hi2s);
  909. if (hi2s->State != HAL_I2S_STATE_READY)
  910. {
  911. __HAL_UNLOCK(hi2s);
  912. return HAL_BUSY;
  913. }
  914. /* Set state and reset error code */
  915. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  916. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  917. hi2s->pTxBuffPtr = pData;
  918. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  919. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  920. {
  921. hi2s->TxXferSize = (Size << 1U);
  922. hi2s->TxXferCount = (Size << 1U);
  923. }
  924. else
  925. {
  926. hi2s->TxXferSize = Size;
  927. hi2s->TxXferCount = Size;
  928. }
  929. /* Enable TXE and ERR interrupt */
  930. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  931. /* Check if the I2S is already enabled */
  932. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  933. {
  934. /* Enable I2S peripheral */
  935. __HAL_I2S_ENABLE(hi2s);
  936. }
  937. __HAL_UNLOCK(hi2s);
  938. return HAL_OK;
  939. }
  940. /**
  941. * @brief Receive an amount of data in non-blocking mode with Interrupt
  942. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  943. * the configuration information for I2S module
  944. * @param pData a 16-bit pointer to the Receive data buffer.
  945. * @param Size number of data sample to be sent:
  946. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  947. * configuration phase, the Size parameter means the number of 16-bit data length
  948. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  949. * the Size parameter means the number of 16-bit data length.
  950. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  951. * between Master and Slave(example: audio streaming).
  952. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization
  953. * between Master and Slave otherwise the I2S interrupt should be optimized.
  954. * @retval HAL status
  955. */
  956. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  957. {
  958. uint32_t tmpreg_cfgr;
  959. if ((pData == NULL) || (Size == 0U))
  960. {
  961. return HAL_ERROR;
  962. }
  963. /* Process Locked */
  964. __HAL_LOCK(hi2s);
  965. if (hi2s->State != HAL_I2S_STATE_READY)
  966. {
  967. __HAL_UNLOCK(hi2s);
  968. return HAL_BUSY;
  969. }
  970. /* Set state and reset error code */
  971. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  972. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  973. hi2s->pRxBuffPtr = pData;
  974. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  975. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  976. {
  977. hi2s->RxXferSize = (Size << 1U);
  978. hi2s->RxXferCount = (Size << 1U);
  979. }
  980. else
  981. {
  982. hi2s->RxXferSize = Size;
  983. hi2s->RxXferCount = Size;
  984. }
  985. /* Enable RXNE and ERR interrupt */
  986. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  987. /* Check if the I2S is already enabled */
  988. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  989. {
  990. /* Enable I2S peripheral */
  991. __HAL_I2S_ENABLE(hi2s);
  992. }
  993. __HAL_UNLOCK(hi2s);
  994. return HAL_OK;
  995. }
  996. /**
  997. * @brief Transmit an amount of data in non-blocking mode with DMA
  998. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  999. * the configuration information for I2S module
  1000. * @param pData a 16-bit pointer to the Transmit data buffer.
  1001. * @param Size number of data sample to be sent:
  1002. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  1003. * configuration phase, the Size parameter means the number of 16-bit data length
  1004. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  1005. * the Size parameter means the number of 16-bit data length.
  1006. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  1007. * between Master and Slave(example: audio streaming).
  1008. * @retval HAL status
  1009. */
  1010. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  1011. {
  1012. uint32_t tmpreg_cfgr;
  1013. if ((pData == NULL) || (Size == 0U))
  1014. {
  1015. return HAL_ERROR;
  1016. }
  1017. /* Process Locked */
  1018. __HAL_LOCK(hi2s);
  1019. if (hi2s->State != HAL_I2S_STATE_READY)
  1020. {
  1021. __HAL_UNLOCK(hi2s);
  1022. return HAL_BUSY;
  1023. }
  1024. /* Set state and reset error code */
  1025. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  1026. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  1027. hi2s->pTxBuffPtr = pData;
  1028. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  1029. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  1030. {
  1031. hi2s->TxXferSize = (Size << 1U);
  1032. hi2s->TxXferCount = (Size << 1U);
  1033. }
  1034. else
  1035. {
  1036. hi2s->TxXferSize = Size;
  1037. hi2s->TxXferCount = Size;
  1038. }
  1039. /* Set the I2S Tx DMA Half transfer complete callback */
  1040. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  1041. /* Set the I2S Tx DMA transfer complete callback */
  1042. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  1043. /* Set the DMA error callback */
  1044. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  1045. /* Enable the Tx DMA Stream/Channel */
  1046. if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmatx,
  1047. (uint32_t)hi2s->pTxBuffPtr,
  1048. (uint32_t)&hi2s->Instance->DR,
  1049. hi2s->TxXferSize))
  1050. {
  1051. /* Update SPI error code */
  1052. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1053. hi2s->State = HAL_I2S_STATE_READY;
  1054. __HAL_UNLOCK(hi2s);
  1055. return HAL_ERROR;
  1056. }
  1057. /* Check if the I2S is already enabled */
  1058. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1059. {
  1060. /* Enable I2S peripheral */
  1061. __HAL_I2S_ENABLE(hi2s);
  1062. }
  1063. /* Check if the I2S Tx request is already enabled */
  1064. if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_TXDMAEN))
  1065. {
  1066. /* Enable Tx DMA Request */
  1067. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1068. }
  1069. __HAL_UNLOCK(hi2s);
  1070. return HAL_OK;
  1071. }
  1072. /**
  1073. * @brief Receive an amount of data in non-blocking mode with DMA
  1074. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1075. * the configuration information for I2S module
  1076. * @param pData a 16-bit pointer to the Receive data buffer.
  1077. * @param Size number of data sample to be sent:
  1078. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  1079. * configuration phase, the Size parameter means the number of 16-bit data length
  1080. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  1081. * the Size parameter means the number of 16-bit data length.
  1082. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  1083. * between Master and Slave(example: audio streaming).
  1084. * @retval HAL status
  1085. */
  1086. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  1087. {
  1088. uint32_t tmpreg_cfgr;
  1089. if ((pData == NULL) || (Size == 0U))
  1090. {
  1091. return HAL_ERROR;
  1092. }
  1093. /* Process Locked */
  1094. __HAL_LOCK(hi2s);
  1095. if (hi2s->State != HAL_I2S_STATE_READY)
  1096. {
  1097. __HAL_UNLOCK(hi2s);
  1098. return HAL_BUSY;
  1099. }
  1100. /* Set state and reset error code */
  1101. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  1102. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  1103. hi2s->pRxBuffPtr = pData;
  1104. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  1105. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  1106. {
  1107. hi2s->RxXferSize = (Size << 1U);
  1108. hi2s->RxXferCount = (Size << 1U);
  1109. }
  1110. else
  1111. {
  1112. hi2s->RxXferSize = Size;
  1113. hi2s->RxXferCount = Size;
  1114. }
  1115. /* Set the I2S Rx DMA Half transfer complete callback */
  1116. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  1117. /* Set the I2S Rx DMA transfer complete callback */
  1118. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  1119. /* Set the DMA error callback */
  1120. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  1121. /* Check if Master Receiver mode is selected */
  1122. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  1123. {
  1124. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  1125. access to the SPI_SR register. */
  1126. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1127. }
  1128. /* Enable the Rx DMA Stream/Channel */
  1129. if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr,
  1130. hi2s->RxXferSize))
  1131. {
  1132. /* Update SPI error code */
  1133. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1134. hi2s->State = HAL_I2S_STATE_READY;
  1135. __HAL_UNLOCK(hi2s);
  1136. return HAL_ERROR;
  1137. }
  1138. /* Check if the I2S is already enabled */
  1139. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1140. {
  1141. /* Enable I2S peripheral */
  1142. __HAL_I2S_ENABLE(hi2s);
  1143. }
  1144. /* Check if the I2S Rx request is already enabled */
  1145. if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_RXDMAEN))
  1146. {
  1147. /* Enable Rx DMA Request */
  1148. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1149. }
  1150. __HAL_UNLOCK(hi2s);
  1151. return HAL_OK;
  1152. }
  1153. /**
  1154. * @brief Pauses the audio DMA Stream/Channel playing from the Media.
  1155. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1156. * the configuration information for I2S module
  1157. * @retval HAL status
  1158. */
  1159. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  1160. {
  1161. /* Process Locked */
  1162. __HAL_LOCK(hi2s);
  1163. if (hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1164. {
  1165. /* Disable the I2S DMA Tx request */
  1166. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1167. }
  1168. else if (hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1169. {
  1170. /* Disable the I2S DMA Rx request */
  1171. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1172. }
  1173. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  1174. else if (hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  1175. {
  1176. /* Pause the audio file playing by disabling the I2S DMA request */
  1177. CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
  1178. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
  1179. }
  1180. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  1181. else
  1182. {
  1183. /* nothing to do */
  1184. }
  1185. /* Process Unlocked */
  1186. __HAL_UNLOCK(hi2s);
  1187. return HAL_OK;
  1188. }
  1189. /**
  1190. * @brief Resumes the audio DMA Stream/Channel playing from the Media.
  1191. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1192. * the configuration information for I2S module
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  1196. {
  1197. /* Process Locked */
  1198. __HAL_LOCK(hi2s);
  1199. if (hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1200. {
  1201. /* Enable the I2S DMA Tx request */
  1202. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1203. }
  1204. else if (hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1205. {
  1206. /* Enable the I2S DMA Rx request */
  1207. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1208. }
  1209. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  1210. else if (hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  1211. {
  1212. /* Pause the audio file playing by disabling the I2S DMA request */
  1213. SET_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1214. SET_BIT(I2SxEXT(hi2s->Instance)->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1215. /* If the I2Sext peripheral is still not enabled, enable it */
  1216. if ((I2SxEXT(hi2s->Instance)->I2SCFGR & SPI_I2SCFGR_I2SE) == 0U)
  1217. {
  1218. /* Enable I2Sext peripheral */
  1219. __HAL_I2SEXT_ENABLE(hi2s);
  1220. }
  1221. }
  1222. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  1223. else
  1224. {
  1225. /* nothing to do */
  1226. }
  1227. /* If the I2S peripheral is still not enabled, enable it */
  1228. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1229. {
  1230. /* Enable I2S peripheral */
  1231. __HAL_I2S_ENABLE(hi2s);
  1232. }
  1233. /* Process Unlocked */
  1234. __HAL_UNLOCK(hi2s);
  1235. return HAL_OK;
  1236. }
  1237. /**
  1238. * @brief Stops the audio DMA Stream/Channel playing from the Media.
  1239. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1240. * the configuration information for I2S module
  1241. * @retval HAL status
  1242. */
  1243. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  1244. {
  1245. HAL_StatusTypeDef errorcode = HAL_OK;
  1246. /* The Lock is not implemented on this API to allow the user application
  1247. to call the HAL SPI API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
  1248. when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated
  1249. and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
  1250. */
  1251. /* Disable the I2S Tx/Rx DMA requests */
  1252. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1253. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1254. /* Abort the I2S DMA tx Stream/Channel */
  1255. if (hi2s->hdmatx != NULL)
  1256. {
  1257. /* Disable the I2S DMA tx Stream/Channel */
  1258. if (HAL_OK != HAL_DMA_Abort(hi2s->hdmatx))
  1259. {
  1260. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1261. errorcode = HAL_ERROR;
  1262. }
  1263. }
  1264. /* Abort the I2S DMA rx Stream/Channel */
  1265. if (hi2s->hdmarx != NULL)
  1266. {
  1267. /* Disable the I2S DMA rx Stream/Channel */
  1268. if (HAL_OK != HAL_DMA_Abort(hi2s->hdmarx))
  1269. {
  1270. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1271. errorcode = HAL_ERROR;
  1272. }
  1273. }
  1274. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  1275. /* In case of Full-Duplex, disable the I2SxEXT Tx/Rx DMA requests*/
  1276. if (hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
  1277. {
  1278. /* Disable the I2SxEXT DMA requests */
  1279. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
  1280. CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
  1281. /* Disable I2Sext peripheral */
  1282. __HAL_I2SEXT_DISABLE(hi2s);
  1283. }
  1284. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  1285. /* Disable I2S peripheral */
  1286. __HAL_I2S_DISABLE(hi2s);
  1287. hi2s->State = HAL_I2S_STATE_READY;
  1288. return errorcode;
  1289. }
  1290. /**
  1291. * @brief This function handles I2S interrupt request.
  1292. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1293. * the configuration information for I2S module
  1294. * @retval None
  1295. */
  1296. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1297. {
  1298. /* Call the IrqHandler ISR set during HAL_I2S_INIT */
  1299. hi2s->IrqHandlerISR(hi2s);
  1300. }
  1301. /**
  1302. * @brief Tx Transfer Half completed callbacks
  1303. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1304. * the configuration information for I2S module
  1305. * @retval None
  1306. */
  1307. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1308. {
  1309. /* Prevent unused argument(s) compilation warning */
  1310. UNUSED(hi2s);
  1311. /* NOTE : This function Should not be modified, when the callback is needed,
  1312. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  1313. */
  1314. }
  1315. /**
  1316. * @brief Tx Transfer completed callbacks
  1317. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1318. * the configuration information for I2S module
  1319. * @retval None
  1320. */
  1321. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  1322. {
  1323. /* Prevent unused argument(s) compilation warning */
  1324. UNUSED(hi2s);
  1325. /* NOTE : This function Should not be modified, when the callback is needed,
  1326. the HAL_I2S_TxCpltCallback could be implemented in the user file
  1327. */
  1328. }
  1329. /**
  1330. * @brief Rx Transfer half completed callbacks
  1331. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1332. * the configuration information for I2S module
  1333. * @retval None
  1334. */
  1335. __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1336. {
  1337. /* Prevent unused argument(s) compilation warning */
  1338. UNUSED(hi2s);
  1339. /* NOTE : This function Should not be modified, when the callback is needed,
  1340. the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
  1341. */
  1342. }
  1343. /**
  1344. * @brief Rx Transfer completed callbacks
  1345. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1346. * the configuration information for I2S module
  1347. * @retval None
  1348. */
  1349. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  1350. {
  1351. /* Prevent unused argument(s) compilation warning */
  1352. UNUSED(hi2s);
  1353. /* NOTE : This function Should not be modified, when the callback is needed,
  1354. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1355. */
  1356. }
  1357. /**
  1358. * @brief I2S error callbacks
  1359. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1360. * the configuration information for I2S module
  1361. * @retval None
  1362. */
  1363. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  1364. {
  1365. /* Prevent unused argument(s) compilation warning */
  1366. UNUSED(hi2s);
  1367. /* NOTE : This function Should not be modified, when the callback is needed,
  1368. the HAL_I2S_ErrorCallback could be implemented in the user file
  1369. */
  1370. }
  1371. /**
  1372. * @}
  1373. */
  1374. /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
  1375. * @brief Peripheral State functions
  1376. *
  1377. @verbatim
  1378. ===============================================================================
  1379. ##### Peripheral State and Errors functions #####
  1380. ===============================================================================
  1381. [..]
  1382. This subsection permits to get in run-time the status of the peripheral
  1383. and the data flow.
  1384. @endverbatim
  1385. * @{
  1386. */
  1387. /**
  1388. * @brief Return the I2S state
  1389. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1390. * the configuration information for I2S module
  1391. * @retval HAL state
  1392. */
  1393. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  1394. {
  1395. return hi2s->State;
  1396. }
  1397. /**
  1398. * @brief Return the I2S error code
  1399. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1400. * the configuration information for I2S module
  1401. * @retval I2S Error Code
  1402. */
  1403. uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1404. {
  1405. return hi2s->ErrorCode;
  1406. }
  1407. /**
  1408. * @}
  1409. */
  1410. /**
  1411. * @}
  1412. */
  1413. /** @addtogroup I2S_Private_Functions I2S Private Functions
  1414. * @{
  1415. */
  1416. /**
  1417. * @brief DMA I2S transmit process complete callback
  1418. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1419. * the configuration information for the specified DMA module.
  1420. * @retval None
  1421. */
  1422. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1423. {
  1424. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1425. /* if DMA is configured in DMA_NORMAL Mode */
  1426. if (hdma->Init.Mode == DMA_NORMAL)
  1427. {
  1428. /* Disable Tx DMA Request */
  1429. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1430. hi2s->TxXferCount = 0U;
  1431. hi2s->State = HAL_I2S_STATE_READY;
  1432. }
  1433. /* Call user Tx complete callback */
  1434. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1435. hi2s->TxCpltCallback(hi2s);
  1436. #else
  1437. HAL_I2S_TxCpltCallback(hi2s);
  1438. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1439. }
  1440. /**
  1441. * @brief DMA I2S transmit process half complete callback
  1442. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1443. * the configuration information for the specified DMA module.
  1444. * @retval None
  1445. */
  1446. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1447. {
  1448. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1449. /* Call user Tx half complete callback */
  1450. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1451. hi2s->TxHalfCpltCallback(hi2s);
  1452. #else
  1453. HAL_I2S_TxHalfCpltCallback(hi2s);
  1454. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1455. }
  1456. /**
  1457. * @brief DMA I2S receive process complete callback
  1458. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1459. * the configuration information for the specified DMA module.
  1460. * @retval None
  1461. */
  1462. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1463. {
  1464. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1465. /* if DMA is configured in DMA_NORMAL Mode */
  1466. if (hdma->Init.Mode == DMA_NORMAL)
  1467. {
  1468. /* Disable Rx DMA Request */
  1469. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1470. hi2s->RxXferCount = 0U;
  1471. hi2s->State = HAL_I2S_STATE_READY;
  1472. }
  1473. /* Call user Rx complete callback */
  1474. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1475. hi2s->RxCpltCallback(hi2s);
  1476. #else
  1477. HAL_I2S_RxCpltCallback(hi2s);
  1478. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1479. }
  1480. /**
  1481. * @brief DMA I2S receive process half complete callback
  1482. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1483. * the configuration information for the specified DMA module.
  1484. * @retval None
  1485. */
  1486. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1487. {
  1488. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1489. /* Call user Rx half complete callback */
  1490. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1491. hi2s->RxHalfCpltCallback(hi2s);
  1492. #else
  1493. HAL_I2S_RxHalfCpltCallback(hi2s);
  1494. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1495. }
  1496. /**
  1497. * @brief DMA I2S communication error callback
  1498. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1499. * the configuration information for the specified DMA module.
  1500. * @retval None
  1501. */
  1502. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1503. {
  1504. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1505. /* Disable Rx and Tx DMA Request */
  1506. CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1507. hi2s->TxXferCount = 0U;
  1508. hi2s->RxXferCount = 0U;
  1509. hi2s->State = HAL_I2S_STATE_READY;
  1510. /* Set the error code and execute error callback*/
  1511. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1512. /* Call user error callback */
  1513. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1514. hi2s->ErrorCallback(hi2s);
  1515. #else
  1516. HAL_I2S_ErrorCallback(hi2s);
  1517. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1518. }
  1519. /**
  1520. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1521. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1522. * the configuration information for I2S module
  1523. * @retval None
  1524. */
  1525. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1526. {
  1527. /* Transmit data */
  1528. hi2s->Instance->DR = (*hi2s->pTxBuffPtr);
  1529. hi2s->pTxBuffPtr++;
  1530. hi2s->TxXferCount--;
  1531. if (hi2s->TxXferCount == 0U)
  1532. {
  1533. /* Disable TXE and ERR interrupt */
  1534. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1535. hi2s->State = HAL_I2S_STATE_READY;
  1536. /* Call user Tx complete callback */
  1537. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1538. hi2s->TxCpltCallback(hi2s);
  1539. #else
  1540. HAL_I2S_TxCpltCallback(hi2s);
  1541. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1542. }
  1543. }
  1544. /**
  1545. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1546. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1547. * the configuration information for I2S module
  1548. * @retval None
  1549. */
  1550. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1551. {
  1552. /* Receive data */
  1553. (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR;
  1554. hi2s->pRxBuffPtr++;
  1555. hi2s->RxXferCount--;
  1556. if (hi2s->RxXferCount == 0U)
  1557. {
  1558. /* Disable RXNE and ERR interrupt */
  1559. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1560. hi2s->State = HAL_I2S_STATE_READY;
  1561. /* Call user Rx complete callback */
  1562. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1563. hi2s->RxCpltCallback(hi2s);
  1564. #else
  1565. HAL_I2S_RxCpltCallback(hi2s);
  1566. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1567. }
  1568. }
  1569. /**
  1570. * @brief This function handles I2S interrupt request.
  1571. * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
  1572. * the configuration information for I2S module
  1573. * @retval None
  1574. */
  1575. static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1576. {
  1577. __IO uint32_t i2ssr = hi2s->Instance->SR;
  1578. if (hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1579. {
  1580. /* I2S in mode Receiver ------------------------------------------------*/
  1581. if (((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
  1582. {
  1583. I2S_Receive_IT(hi2s);
  1584. }
  1585. /* I2S Overrun error interrupt occurred -------------------------------------*/
  1586. if (((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1587. {
  1588. /* Disable RXNE and ERR interrupt */
  1589. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1590. /* Clear Overrun flag */
  1591. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1592. /* Set the I2S State ready */
  1593. hi2s->State = HAL_I2S_STATE_READY;
  1594. /* Set the error code and execute error callback*/
  1595. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  1596. /* Call user error callback */
  1597. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1598. hi2s->ErrorCallback(hi2s);
  1599. #else
  1600. HAL_I2S_ErrorCallback(hi2s);
  1601. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1602. }
  1603. }
  1604. if (hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1605. {
  1606. /* I2S in mode Transmitter -----------------------------------------------*/
  1607. if (((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
  1608. {
  1609. I2S_Transmit_IT(hi2s);
  1610. }
  1611. /* I2S Underrun error interrupt occurred --------------------------------*/
  1612. if (((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
  1613. {
  1614. /* Disable TXE and ERR interrupt */
  1615. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1616. /* Clear Underrun flag */
  1617. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  1618. /* Set the I2S State ready */
  1619. hi2s->State = HAL_I2S_STATE_READY;
  1620. /* Set the error code and execute error callback*/
  1621. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  1622. /* Call user error callback */
  1623. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1624. hi2s->ErrorCallback(hi2s);
  1625. #else
  1626. HAL_I2S_ErrorCallback(hi2s);
  1627. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1628. }
  1629. }
  1630. }
  1631. /**
  1632. * @brief This function handles I2S Communication Timeout.
  1633. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1634. * the configuration information for I2S module
  1635. * @param Flag Flag checked
  1636. * @param State Value of the flag expected
  1637. * @param Timeout Duration of the timeout
  1638. * @retval HAL status
  1639. */
  1640. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State,
  1641. uint32_t Timeout)
  1642. {
  1643. uint32_t tickstart;
  1644. /* Get tick */
  1645. tickstart = HAL_GetTick();
  1646. /* Wait until flag is set to status*/
  1647. while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  1648. {
  1649. if (Timeout != HAL_MAX_DELAY)
  1650. {
  1651. if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U))
  1652. {
  1653. /* Set the I2S State ready */
  1654. hi2s->State = HAL_I2S_STATE_READY;
  1655. /* Process Unlocked */
  1656. __HAL_UNLOCK(hi2s);
  1657. return HAL_TIMEOUT;
  1658. }
  1659. }
  1660. }
  1661. return HAL_OK;
  1662. }
  1663. /**
  1664. * @}
  1665. */
  1666. /**
  1667. * @}
  1668. */
  1669. /**
  1670. * @}
  1671. */
  1672. #endif /* HAL_I2S_MODULE_ENABLED */
  1673. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/