stm32f1xx_hal_i2s.c 61 KB

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