stm32g4xx_hal_i2s.c 60 KB

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