stm32g0xx_hal_i2s.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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) 2018 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. (+@) PLLPCLK output is configured or
  55. (+@) HSI is enabled or
  56. (+@) External clock source is configured after setting correctly
  57. the define constant EXTERNAL_I2S1_CLOCK_VALUE in the stm32g0xx_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 "stm32g0xx_hal.h"
  167. #ifdef HAL_I2S_MODULE_ENABLED
  168. #if defined(SPI_I2S_SUPPORT)
  169. /** @addtogroup STM32G0xx_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. #if defined (RCC_PERIPHCLK_I2S2)
  303. if (hi2s->Instance == SPI1)
  304. {
  305. /* Get the source clock value: based on System Clock value */
  306. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S1);
  307. }
  308. else /* SPI2 case */
  309. {
  310. /* Get the source clock value: based on System Clock value */
  311. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S2);
  312. }
  313. #else
  314. /* Get the source clock value: based on System Clock value */
  315. i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S1);
  316. #endif /* RCC_PERIPHCLK_I2S2 */
  317. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  318. if (hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
  319. {
  320. /* MCLK output is enabled */
  321. if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B)
  322. {
  323. tmp = (uint32_t)(((((i2sclk / (packetlength * 4U)) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  324. }
  325. else
  326. {
  327. tmp = (uint32_t)(((((i2sclk / (packetlength * 8U)) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  328. }
  329. }
  330. else
  331. {
  332. /* MCLK output is disabled */
  333. tmp = (uint32_t)(((((i2sclk / packetlength) * 10U) / hi2s->Init.AudioFreq)) + 5U);
  334. }
  335. /* Remove the flatting point */
  336. tmp = tmp / 10U;
  337. /* Check the parity of the divider */
  338. i2sodd = (uint32_t)(tmp & (uint32_t)1U);
  339. /* Compute the i2sdiv prescaler */
  340. i2sdiv = (uint32_t)((tmp - i2sodd) / 2U);
  341. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  342. i2sodd = (uint32_t)(i2sodd << 8U);
  343. }
  344. else
  345. {
  346. /* Set the default values */
  347. i2sdiv = 2U;
  348. i2sodd = 0U;
  349. }
  350. /* Test if the divider is 1 or 0 or greater than 0xFF */
  351. if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  352. {
  353. /* Set the error code and execute error callback*/
  354. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER);
  355. return HAL_ERROR;
  356. }
  357. /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
  358. /* Write to SPIx I2SPR register the computed value */
  359. hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
  360. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  361. /* And configure the I2S with the I2S_InitStruct values */
  362. MODIFY_REG(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \
  363. SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \
  364. SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
  365. SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD), \
  366. (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode | \
  367. hi2s->Init.Standard | hi2s->Init.DataFormat | \
  368. hi2s->Init.CPOL));
  369. #if defined(SPI_I2SCFGR_ASTRTEN)
  370. if ((hi2s->Init.Standard == I2S_STANDARD_PCM_SHORT) || ((hi2s->Init.Standard == I2S_STANDARD_PCM_LONG)))
  371. {
  372. /* Write to SPIx I2SCFGR */
  373. SET_BIT(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_ASTRTEN);
  374. }
  375. #endif /* SPI_I2SCFGR_ASTRTEN */
  376. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  377. hi2s->State = HAL_I2S_STATE_READY;
  378. return HAL_OK;
  379. }
  380. /**
  381. * @brief DeInitializes the I2S peripheral
  382. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  383. * the configuration information for I2S module
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  387. {
  388. /* Check the I2S handle allocation */
  389. if (hi2s == NULL)
  390. {
  391. return HAL_ERROR;
  392. }
  393. /* Check the parameters */
  394. assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
  395. hi2s->State = HAL_I2S_STATE_BUSY;
  396. /* Disable the I2S Peripheral Clock */
  397. __HAL_I2S_DISABLE(hi2s);
  398. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  399. if (hi2s->MspDeInitCallback == NULL)
  400. {
  401. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  402. }
  403. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  404. hi2s->MspDeInitCallback(hi2s);
  405. #else
  406. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  407. HAL_I2S_MspDeInit(hi2s);
  408. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  409. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  410. hi2s->State = HAL_I2S_STATE_RESET;
  411. /* Release Lock */
  412. __HAL_UNLOCK(hi2s);
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief I2S MSP Init
  417. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  418. * the configuration information for I2S module
  419. * @retval None
  420. */
  421. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  422. {
  423. /* Prevent unused argument(s) compilation warning */
  424. UNUSED(hi2s);
  425. /* NOTE : This function Should not be modified, when the callback is needed,
  426. the HAL_I2S_MspInit could be implemented in the user file
  427. */
  428. }
  429. /**
  430. * @brief I2S MSP DeInit
  431. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  432. * the configuration information for I2S module
  433. * @retval None
  434. */
  435. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  436. {
  437. /* Prevent unused argument(s) compilation warning */
  438. UNUSED(hi2s);
  439. /* NOTE : This function Should not be modified, when the callback is needed,
  440. the HAL_I2S_MspDeInit could be implemented in the user file
  441. */
  442. }
  443. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  444. /**
  445. * @brief Register a User I2S Callback
  446. * To be used instead of the weak predefined callback
  447. * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
  448. * the configuration information for the specified I2S.
  449. * @param CallbackID ID of the callback to be registered
  450. * @param pCallback pointer to the Callback function
  451. * @retval HAL status
  452. */
  453. HAL_StatusTypeDef HAL_I2S_RegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID,
  454. pI2S_CallbackTypeDef pCallback)
  455. {
  456. HAL_StatusTypeDef status = HAL_OK;
  457. if (pCallback == NULL)
  458. {
  459. /* Update the error code */
  460. hi2s->ErrorCode |= HAL_I2S_ERROR_INVALID_CALLBACK;
  461. return HAL_ERROR;
  462. }
  463. /* Process locked */
  464. __HAL_LOCK(hi2s);
  465. if (HAL_I2S_STATE_READY == hi2s->State)
  466. {
  467. switch (CallbackID)
  468. {
  469. case HAL_I2S_TX_COMPLETE_CB_ID :
  470. hi2s->TxCpltCallback = pCallback;
  471. break;
  472. case HAL_I2S_RX_COMPLETE_CB_ID :
  473. hi2s->RxCpltCallback = pCallback;
  474. break;
  475. case HAL_I2S_TX_HALF_COMPLETE_CB_ID :
  476. hi2s->TxHalfCpltCallback = pCallback;
  477. break;
  478. case HAL_I2S_RX_HALF_COMPLETE_CB_ID :
  479. hi2s->RxHalfCpltCallback = pCallback;
  480. break;
  481. case HAL_I2S_ERROR_CB_ID :
  482. hi2s->ErrorCallback = pCallback;
  483. break;
  484. case HAL_I2S_MSPINIT_CB_ID :
  485. hi2s->MspInitCallback = pCallback;
  486. break;
  487. case HAL_I2S_MSPDEINIT_CB_ID :
  488. hi2s->MspDeInitCallback = pCallback;
  489. break;
  490. default :
  491. /* Update the error code */
  492. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  493. /* Return error status */
  494. status = HAL_ERROR;
  495. break;
  496. }
  497. }
  498. else if (HAL_I2S_STATE_RESET == hi2s->State)
  499. {
  500. switch (CallbackID)
  501. {
  502. case HAL_I2S_MSPINIT_CB_ID :
  503. hi2s->MspInitCallback = pCallback;
  504. break;
  505. case HAL_I2S_MSPDEINIT_CB_ID :
  506. hi2s->MspDeInitCallback = pCallback;
  507. break;
  508. default :
  509. /* Update the error code */
  510. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  511. /* Return error status */
  512. status = HAL_ERROR;
  513. break;
  514. }
  515. }
  516. else
  517. {
  518. /* Update the error code */
  519. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  520. /* Return error status */
  521. status = HAL_ERROR;
  522. }
  523. /* Release Lock */
  524. __HAL_UNLOCK(hi2s);
  525. return status;
  526. }
  527. /**
  528. * @brief Unregister an I2S Callback
  529. * I2S callback is redirected to the weak predefined callback
  530. * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
  531. * the configuration information for the specified I2S.
  532. * @param CallbackID ID of the callback to be unregistered
  533. * @retval HAL status
  534. */
  535. HAL_StatusTypeDef HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID)
  536. {
  537. HAL_StatusTypeDef status = HAL_OK;
  538. /* Process locked */
  539. __HAL_LOCK(hi2s);
  540. if (HAL_I2S_STATE_READY == hi2s->State)
  541. {
  542. switch (CallbackID)
  543. {
  544. case HAL_I2S_TX_COMPLETE_CB_ID :
  545. hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */
  546. break;
  547. case HAL_I2S_RX_COMPLETE_CB_ID :
  548. hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */
  549. break;
  550. case HAL_I2S_TX_HALF_COMPLETE_CB_ID :
  551. hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  552. break;
  553. case HAL_I2S_RX_HALF_COMPLETE_CB_ID :
  554. hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  555. break;
  556. case HAL_I2S_ERROR_CB_ID :
  557. hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */
  558. break;
  559. case HAL_I2S_MSPINIT_CB_ID :
  560. hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */
  561. break;
  562. case HAL_I2S_MSPDEINIT_CB_ID :
  563. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  564. break;
  565. default :
  566. /* Update the error code */
  567. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  568. /* Return error status */
  569. status = HAL_ERROR;
  570. break;
  571. }
  572. }
  573. else if (HAL_I2S_STATE_RESET == hi2s->State)
  574. {
  575. switch (CallbackID)
  576. {
  577. case HAL_I2S_MSPINIT_CB_ID :
  578. hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */
  579. break;
  580. case HAL_I2S_MSPDEINIT_CB_ID :
  581. hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */
  582. break;
  583. default :
  584. /* Update the error code */
  585. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  586. /* Return error status */
  587. status = HAL_ERROR;
  588. break;
  589. }
  590. }
  591. else
  592. {
  593. /* Update the error code */
  594. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK);
  595. /* Return error status */
  596. status = HAL_ERROR;
  597. }
  598. /* Release Lock */
  599. __HAL_UNLOCK(hi2s);
  600. return status;
  601. }
  602. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  603. /**
  604. * @}
  605. */
  606. /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
  607. * @brief Data transfers functions
  608. *
  609. @verbatim
  610. ===============================================================================
  611. ##### IO operation functions #####
  612. ===============================================================================
  613. [..]
  614. This subsection provides a set of functions allowing to manage the I2S data
  615. transfers.
  616. (#) There are two modes of transfer:
  617. (++) Blocking mode : The communication is performed in the polling mode.
  618. The status of all data processing is returned by the same function
  619. after finishing transfer.
  620. (++) No-Blocking mode : The communication is performed using Interrupts
  621. or DMA. These functions return the status of the transfer startup.
  622. The end of the data processing will be indicated through the
  623. dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
  624. using DMA mode.
  625. (#) Blocking mode functions are :
  626. (++) HAL_I2S_Transmit()
  627. (++) HAL_I2S_Receive()
  628. (#) No-Blocking mode functions with Interrupt are :
  629. (++) HAL_I2S_Transmit_IT()
  630. (++) HAL_I2S_Receive_IT()
  631. (#) No-Blocking mode functions with DMA are :
  632. (++) HAL_I2S_Transmit_DMA()
  633. (++) HAL_I2S_Receive_DMA()
  634. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  635. (++) HAL_I2S_TxCpltCallback()
  636. (++) HAL_I2S_RxCpltCallback()
  637. (++) HAL_I2S_ErrorCallback()
  638. @endverbatim
  639. * @{
  640. */
  641. /**
  642. * @brief Transmit an amount of data in blocking mode
  643. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  644. * the configuration information for I2S module
  645. * @param pData a 16-bit pointer to data buffer.
  646. * @param Size number of data sample to be sent:
  647. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  648. * configuration phase, the Size parameter means the number of 16-bit data length
  649. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  650. * the Size parameter means the number of 24-bit or 32-bit data length.
  651. * @param Timeout Timeout duration
  652. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  653. * between Master and Slave(example: audio streaming).
  654. * @retval HAL status
  655. */
  656. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  657. {
  658. uint32_t tmpreg_cfgr;
  659. if ((pData == NULL) || (Size == 0U))
  660. {
  661. return HAL_ERROR;
  662. }
  663. /* Process Locked */
  664. __HAL_LOCK(hi2s);
  665. if (hi2s->State != HAL_I2S_STATE_READY)
  666. {
  667. __HAL_UNLOCK(hi2s);
  668. return HAL_BUSY;
  669. }
  670. /* Set state and reset error code */
  671. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  672. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  673. hi2s->pTxBuffPtr = pData;
  674. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  675. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  676. {
  677. hi2s->TxXferSize = (Size << 1U);
  678. hi2s->TxXferCount = (Size << 1U);
  679. }
  680. else
  681. {
  682. hi2s->TxXferSize = Size;
  683. hi2s->TxXferCount = Size;
  684. }
  685. tmpreg_cfgr = hi2s->Instance->I2SCFGR;
  686. /* Check if the I2S is already enabled */
  687. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  688. {
  689. /* Enable I2S peripheral */
  690. __HAL_I2S_ENABLE(hi2s);
  691. }
  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. while (hi2s->TxXferCount > 0U)
  702. {
  703. hi2s->Instance->DR = (*hi2s->pTxBuffPtr);
  704. hi2s->pTxBuffPtr++;
  705. hi2s->TxXferCount--;
  706. /* Wait until TXE flag is set */
  707. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
  708. {
  709. /* Set the error code */
  710. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  711. hi2s->State = HAL_I2S_STATE_READY;
  712. __HAL_UNLOCK(hi2s);
  713. return HAL_ERROR;
  714. }
  715. /* Check if an underrun occurs */
  716. if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
  717. {
  718. /* Clear underrun flag */
  719. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  720. /* Set the error code */
  721. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  722. }
  723. }
  724. /* Check if Slave mode is selected */
  725. if (((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX)
  726. || ((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_RX))
  727. {
  728. /* Wait until Busy flag is reset */
  729. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK)
  730. {
  731. /* Set the error code */
  732. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  733. hi2s->State = HAL_I2S_STATE_READY;
  734. __HAL_UNLOCK(hi2s);
  735. return HAL_ERROR;
  736. }
  737. }
  738. hi2s->State = HAL_I2S_STATE_READY;
  739. __HAL_UNLOCK(hi2s);
  740. return HAL_OK;
  741. }
  742. /**
  743. * @brief Receive an amount of data in blocking mode
  744. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  745. * the configuration information for I2S module
  746. * @param pData a 16-bit pointer to data buffer.
  747. * @param Size number of data sample to be sent:
  748. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  749. * configuration phase, the Size parameter means the number of 16-bit data length
  750. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  751. * the Size parameter means the number of 24-bit or 32-bit data length.
  752. * @param Timeout Timeout duration
  753. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  754. * between Master and Slave(example: audio streaming).
  755. * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
  756. * in continuous way and as the I2S is not disabled at the end of the I2S transaction.
  757. * @retval HAL status
  758. */
  759. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
  760. {
  761. uint32_t tmpreg_cfgr;
  762. if ((pData == NULL) || (Size == 0U))
  763. {
  764. return HAL_ERROR;
  765. }
  766. /* Process Locked */
  767. __HAL_LOCK(hi2s);
  768. if (hi2s->State != HAL_I2S_STATE_READY)
  769. {
  770. __HAL_UNLOCK(hi2s);
  771. return HAL_BUSY;
  772. }
  773. /* Set state and reset error code */
  774. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  775. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  776. hi2s->pRxBuffPtr = pData;
  777. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  778. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  779. {
  780. hi2s->RxXferSize = (Size << 1U);
  781. hi2s->RxXferCount = (Size << 1U);
  782. }
  783. else
  784. {
  785. hi2s->RxXferSize = Size;
  786. hi2s->RxXferCount = Size;
  787. }
  788. /* Check if the I2S is already enabled */
  789. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  790. {
  791. /* Enable I2S peripheral */
  792. __HAL_I2S_ENABLE(hi2s);
  793. }
  794. /* Check if Master Receiver mode is selected */
  795. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  796. {
  797. /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
  798. access to the SPI_SR register. */
  799. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  800. }
  801. /* Receive data */
  802. while (hi2s->RxXferCount > 0U)
  803. {
  804. /* Wait until RXNE flag is set */
  805. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
  806. {
  807. /* Set the error code */
  808. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  809. hi2s->State = HAL_I2S_STATE_READY;
  810. __HAL_UNLOCK(hi2s);
  811. return HAL_ERROR;
  812. }
  813. (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR;
  814. hi2s->pRxBuffPtr++;
  815. hi2s->RxXferCount--;
  816. /* Check if an overrun occurs */
  817. if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
  818. {
  819. /* Clear overrun flag */
  820. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  821. /* Set the error code */
  822. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  823. }
  824. }
  825. hi2s->State = HAL_I2S_STATE_READY;
  826. __HAL_UNLOCK(hi2s);
  827. return HAL_OK;
  828. }
  829. /**
  830. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  831. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  832. * the configuration information for I2S module
  833. * @param pData a 16-bit pointer to data buffer.
  834. * @param Size number of data sample to be sent:
  835. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  836. * configuration phase, the Size parameter means the number of 16-bit data length
  837. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  838. * the Size parameter means the number of 24-bit or 32-bit data length.
  839. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  840. * between Master and Slave(example: audio streaming).
  841. * @retval HAL status
  842. */
  843. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  844. {
  845. uint32_t tmpreg_cfgr;
  846. if ((pData == NULL) || (Size == 0U))
  847. {
  848. return HAL_ERROR;
  849. }
  850. /* Process Locked */
  851. __HAL_LOCK(hi2s);
  852. if (hi2s->State != HAL_I2S_STATE_READY)
  853. {
  854. __HAL_UNLOCK(hi2s);
  855. return HAL_BUSY;
  856. }
  857. /* Set state and reset error code */
  858. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  859. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  860. hi2s->pTxBuffPtr = pData;
  861. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  862. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  863. {
  864. hi2s->TxXferSize = (Size << 1U);
  865. hi2s->TxXferCount = (Size << 1U);
  866. }
  867. else
  868. {
  869. hi2s->TxXferSize = Size;
  870. hi2s->TxXferCount = Size;
  871. }
  872. /* Enable TXE and ERR interrupt */
  873. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  874. /* Check if the I2S is already enabled */
  875. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  876. {
  877. /* Enable I2S peripheral */
  878. __HAL_I2S_ENABLE(hi2s);
  879. }
  880. __HAL_UNLOCK(hi2s);
  881. return HAL_OK;
  882. }
  883. /**
  884. * @brief Receive an amount of data in non-blocking mode with Interrupt
  885. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  886. * the configuration information for I2S module
  887. * @param pData a 16-bit pointer to the Receive data buffer.
  888. * @param Size number of data sample to be sent:
  889. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  890. * configuration phase, the Size parameter means the number of 16-bit data length
  891. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  892. * the Size parameter means the number of 24-bit or 32-bit data length.
  893. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  894. * between Master and Slave(example: audio streaming).
  895. * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization
  896. * between Master and Slave otherwise the I2S interrupt should be optimized.
  897. * @retval HAL status
  898. */
  899. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  900. {
  901. uint32_t tmpreg_cfgr;
  902. if ((pData == NULL) || (Size == 0U))
  903. {
  904. return HAL_ERROR;
  905. }
  906. /* Process Locked */
  907. __HAL_LOCK(hi2s);
  908. if (hi2s->State != HAL_I2S_STATE_READY)
  909. {
  910. __HAL_UNLOCK(hi2s);
  911. return HAL_BUSY;
  912. }
  913. /* Set state and reset error code */
  914. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  915. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  916. hi2s->pRxBuffPtr = pData;
  917. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  918. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  919. {
  920. hi2s->RxXferSize = (Size << 1U);
  921. hi2s->RxXferCount = (Size << 1U);
  922. }
  923. else
  924. {
  925. hi2s->RxXferSize = Size;
  926. hi2s->RxXferCount = Size;
  927. }
  928. /* Enable RXNE and ERR interrupt */
  929. __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  930. /* Check if the I2S is already enabled */
  931. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
  932. {
  933. /* Enable I2S peripheral */
  934. __HAL_I2S_ENABLE(hi2s);
  935. }
  936. __HAL_UNLOCK(hi2s);
  937. return HAL_OK;
  938. }
  939. /**
  940. * @brief Transmit an amount of data in non-blocking mode with DMA
  941. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  942. * the configuration information for I2S module
  943. * @param pData a 16-bit pointer to the Transmit data buffer.
  944. * @param Size number of data sample to be sent:
  945. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  946. * configuration phase, the Size parameter means the number of 16-bit data length
  947. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  948. * the Size parameter means the number of 24-bit or 32-bit data length.
  949. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  950. * between Master and Slave(example: audio streaming).
  951. * @retval HAL status
  952. */
  953. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  954. {
  955. uint32_t tmpreg_cfgr;
  956. if ((pData == NULL) || (Size == 0U))
  957. {
  958. return HAL_ERROR;
  959. }
  960. /* Process Locked */
  961. __HAL_LOCK(hi2s);
  962. if (hi2s->State != HAL_I2S_STATE_READY)
  963. {
  964. __HAL_UNLOCK(hi2s);
  965. return HAL_BUSY;
  966. }
  967. /* Set state and reset error code */
  968. hi2s->State = HAL_I2S_STATE_BUSY_TX;
  969. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  970. hi2s->pTxBuffPtr = pData;
  971. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  972. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  973. {
  974. hi2s->TxXferSize = (Size << 1U);
  975. hi2s->TxXferCount = (Size << 1U);
  976. }
  977. else
  978. {
  979. hi2s->TxXferSize = Size;
  980. hi2s->TxXferCount = Size;
  981. }
  982. /* Set the I2S Tx DMA Half transfer complete callback */
  983. hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
  984. /* Set the I2S Tx DMA transfer complete callback */
  985. hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
  986. /* Set the DMA error callback */
  987. hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
  988. /* Enable the Tx DMA Stream/Channel */
  989. if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmatx,
  990. (uint32_t)hi2s->pTxBuffPtr,
  991. (uint32_t)&hi2s->Instance->DR,
  992. hi2s->TxXferSize))
  993. {
  994. /* Update SPI error code */
  995. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  996. hi2s->State = HAL_I2S_STATE_READY;
  997. __HAL_UNLOCK(hi2s);
  998. return HAL_ERROR;
  999. }
  1000. /* Check if the I2S is already enabled */
  1001. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1002. {
  1003. /* Enable I2S peripheral */
  1004. __HAL_I2S_ENABLE(hi2s);
  1005. }
  1006. /* Check if the I2S Tx request is already enabled */
  1007. if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_TXDMAEN))
  1008. {
  1009. /* Enable Tx DMA Request */
  1010. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1011. }
  1012. __HAL_UNLOCK(hi2s);
  1013. return HAL_OK;
  1014. }
  1015. /**
  1016. * @brief Receive an amount of data in non-blocking mode with DMA
  1017. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1018. * the configuration information for I2S module
  1019. * @param pData a 16-bit pointer to the Receive data buffer.
  1020. * @param Size number of data sample to be sent:
  1021. * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
  1022. * configuration phase, the Size parameter means the number of 16-bit data length
  1023. * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
  1024. * the Size parameter means the number of 24-bit or 32-bit data length.
  1025. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  1026. * between Master and Slave(example: audio streaming).
  1027. * @retval HAL status
  1028. */
  1029. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
  1030. {
  1031. uint32_t tmpreg_cfgr;
  1032. if ((pData == NULL) || (Size == 0U))
  1033. {
  1034. return HAL_ERROR;
  1035. }
  1036. /* Process Locked */
  1037. __HAL_LOCK(hi2s);
  1038. if (hi2s->State != HAL_I2S_STATE_READY)
  1039. {
  1040. __HAL_UNLOCK(hi2s);
  1041. return HAL_BUSY;
  1042. }
  1043. /* Set state and reset error code */
  1044. hi2s->State = HAL_I2S_STATE_BUSY_RX;
  1045. hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
  1046. hi2s->pRxBuffPtr = pData;
  1047. tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
  1048. if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B))
  1049. {
  1050. hi2s->RxXferSize = (Size << 1U);
  1051. hi2s->RxXferCount = (Size << 1U);
  1052. }
  1053. else
  1054. {
  1055. hi2s->RxXferSize = Size;
  1056. hi2s->RxXferCount = Size;
  1057. }
  1058. /* Set the I2S Rx DMA Half transfer complete callback */
  1059. hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
  1060. /* Set the I2S Rx DMA transfer complete callback */
  1061. hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
  1062. /* Set the DMA error callback */
  1063. hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
  1064. /* Check if Master Receiver mode is selected */
  1065. if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
  1066. {
  1067. /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
  1068. access to the SPI_SR register. */
  1069. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1070. }
  1071. /* Enable the Rx DMA Stream/Channel */
  1072. if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr,
  1073. hi2s->RxXferSize))
  1074. {
  1075. /* Update SPI error code */
  1076. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1077. hi2s->State = HAL_I2S_STATE_READY;
  1078. __HAL_UNLOCK(hi2s);
  1079. return HAL_ERROR;
  1080. }
  1081. /* Check if the I2S is already enabled */
  1082. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1083. {
  1084. /* Enable I2S peripheral */
  1085. __HAL_I2S_ENABLE(hi2s);
  1086. }
  1087. /* Check if the I2S Rx request is already enabled */
  1088. if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_RXDMAEN))
  1089. {
  1090. /* Enable Rx DMA Request */
  1091. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1092. }
  1093. __HAL_UNLOCK(hi2s);
  1094. return HAL_OK;
  1095. }
  1096. /**
  1097. * @brief Pauses the audio DMA Stream/Channel playing from the Media.
  1098. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1099. * the configuration information for I2S module
  1100. * @retval HAL status
  1101. */
  1102. HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
  1103. {
  1104. /* Process Locked */
  1105. __HAL_LOCK(hi2s);
  1106. if (hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1107. {
  1108. /* Disable the I2S DMA Tx request */
  1109. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1110. }
  1111. else if (hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1112. {
  1113. /* Disable the I2S DMA Rx request */
  1114. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1115. }
  1116. else
  1117. {
  1118. /* nothing to do */
  1119. }
  1120. /* Process Unlocked */
  1121. __HAL_UNLOCK(hi2s);
  1122. return HAL_OK;
  1123. }
  1124. /**
  1125. * @brief Resumes the audio DMA Stream/Channel playing from the Media.
  1126. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1127. * the configuration information for I2S module
  1128. * @retval HAL status
  1129. */
  1130. HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
  1131. {
  1132. /* Process Locked */
  1133. __HAL_LOCK(hi2s);
  1134. if (hi2s->State == HAL_I2S_STATE_BUSY_TX)
  1135. {
  1136. /* Enable the I2S DMA Tx request */
  1137. SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1138. }
  1139. else if (hi2s->State == HAL_I2S_STATE_BUSY_RX)
  1140. {
  1141. /* Enable the I2S DMA Rx request */
  1142. SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1143. }
  1144. else
  1145. {
  1146. /* nothing to do */
  1147. }
  1148. /* If the I2S peripheral is still not enabled, enable it */
  1149. if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))
  1150. {
  1151. /* Enable I2S peripheral */
  1152. __HAL_I2S_ENABLE(hi2s);
  1153. }
  1154. /* Process Unlocked */
  1155. __HAL_UNLOCK(hi2s);
  1156. return HAL_OK;
  1157. }
  1158. /**
  1159. * @brief Stops the audio DMA Stream/Channel playing from the Media.
  1160. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1161. * the configuration information for I2S module
  1162. * @retval HAL status
  1163. */
  1164. HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
  1165. {
  1166. HAL_StatusTypeDef errorcode = HAL_OK;
  1167. /* The Lock is not implemented on this API to allow the user application
  1168. to call the HAL SPI API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
  1169. when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated
  1170. and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
  1171. */
  1172. if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
  1173. {
  1174. /* Abort the I2S DMA tx Stream/Channel */
  1175. if (hi2s->hdmatx != NULL)
  1176. {
  1177. /* Disable the I2S DMA tx Stream/Channel */
  1178. if (HAL_OK != HAL_DMA_Abort(hi2s->hdmatx))
  1179. {
  1180. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1181. errorcode = HAL_ERROR;
  1182. }
  1183. }
  1184. /* Wait until TXE flag is set */
  1185. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, I2S_TIMEOUT_FLAG) != HAL_OK)
  1186. {
  1187. /* Set the error code */
  1188. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  1189. hi2s->State = HAL_I2S_STATE_READY;
  1190. errorcode = HAL_ERROR;
  1191. }
  1192. /* Wait until BSY flag is Reset */
  1193. if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, I2S_TIMEOUT_FLAG) != HAL_OK)
  1194. {
  1195. /* Set the error code */
  1196. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
  1197. hi2s->State = HAL_I2S_STATE_READY;
  1198. errorcode = HAL_ERROR;
  1199. }
  1200. /* Disable I2S peripheral */
  1201. __HAL_I2S_DISABLE(hi2s);
  1202. /* Clear UDR flag */
  1203. __HAL_I2S_CLEAR_UDRFLAG(hi2s);
  1204. /* Disable the I2S Tx DMA requests */
  1205. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1206. }
  1207. else if ((hi2s->Init.Mode == I2S_MODE_MASTER_RX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_RX))
  1208. {
  1209. /* Abort the I2S DMA rx Stream/Channel */
  1210. if (hi2s->hdmarx != NULL)
  1211. {
  1212. /* Disable the I2S DMA rx Stream/Channel */
  1213. if (HAL_OK != HAL_DMA_Abort(hi2s->hdmarx))
  1214. {
  1215. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1216. errorcode = HAL_ERROR;
  1217. }
  1218. }
  1219. /* Disable I2S peripheral */
  1220. __HAL_I2S_DISABLE(hi2s);
  1221. /* Clear OVR flag */
  1222. __HAL_I2S_CLEAR_OVRFLAG(hi2s);
  1223. /* Disable the I2S Rx DMA request */
  1224. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1225. if (hi2s->Init.Mode == I2S_MODE_SLAVE_RX)
  1226. {
  1227. /* Set the error code */
  1228. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_BUSY_LINE_RX);
  1229. /* Set the I2S State ready */
  1230. hi2s->State = HAL_I2S_STATE_READY;
  1231. errorcode = HAL_ERROR;
  1232. }
  1233. else
  1234. {
  1235. /* Read DR to Flush RX Data */
  1236. READ_REG((hi2s->Instance)->DR);
  1237. }
  1238. }
  1239. hi2s->State = HAL_I2S_STATE_READY;
  1240. return errorcode;
  1241. }
  1242. /**
  1243. * @brief This function handles I2S interrupt request.
  1244. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1245. * the configuration information for I2S module
  1246. * @retval None
  1247. */
  1248. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  1249. {
  1250. uint32_t itsource = hi2s->Instance->CR2;
  1251. uint32_t itflag = hi2s->Instance->SR;
  1252. /* I2S in mode Receiver ------------------------------------------------*/
  1253. if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) == RESET) &&
  1254. (I2S_CHECK_FLAG(itflag, I2S_FLAG_RXNE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_RXNE) != RESET))
  1255. {
  1256. I2S_Receive_IT(hi2s);
  1257. return;
  1258. }
  1259. /* I2S in mode Tramitter -----------------------------------------------*/
  1260. if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_TXE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_TXE) != RESET))
  1261. {
  1262. I2S_Transmit_IT(hi2s);
  1263. return;
  1264. }
  1265. /* I2S interrupt error -------------------------------------------------*/
  1266. if (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_ERR) != RESET)
  1267. {
  1268. /* I2S Overrun error interrupt occurred ---------------------------------*/
  1269. if (I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) != RESET)
  1270. {
  1271. /* Disable RXNE and ERR interrupt */
  1272. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1273. /* Set the error code and execute error callback*/
  1274. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
  1275. }
  1276. /* I2S Underrun error interrupt occurred --------------------------------*/
  1277. if (I2S_CHECK_FLAG(itflag, I2S_FLAG_UDR) != RESET)
  1278. {
  1279. /* Disable TXE and ERR interrupt */
  1280. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1281. /* Set the error code and execute error callback*/
  1282. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
  1283. }
  1284. /* Set the I2S State ready */
  1285. hi2s->State = HAL_I2S_STATE_READY;
  1286. /* Call user error callback */
  1287. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1288. hi2s->ErrorCallback(hi2s);
  1289. #else
  1290. HAL_I2S_ErrorCallback(hi2s);
  1291. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1292. }
  1293. }
  1294. /**
  1295. * @brief Tx Transfer Half completed callbacks
  1296. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1297. * the configuration information for I2S module
  1298. * @retval None
  1299. */
  1300. __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1301. {
  1302. /* Prevent unused argument(s) compilation warning */
  1303. UNUSED(hi2s);
  1304. /* NOTE : This function Should not be modified, when the callback is needed,
  1305. the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
  1306. */
  1307. }
  1308. /**
  1309. * @brief Tx Transfer completed callbacks
  1310. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1311. * the configuration information for I2S module
  1312. * @retval None
  1313. */
  1314. __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  1315. {
  1316. /* Prevent unused argument(s) compilation warning */
  1317. UNUSED(hi2s);
  1318. /* NOTE : This function Should not be modified, when the callback is needed,
  1319. the HAL_I2S_TxCpltCallback could be implemented in the user file
  1320. */
  1321. }
  1322. /**
  1323. * @brief Rx Transfer half completed callbacks
  1324. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1325. * the configuration information for I2S module
  1326. * @retval None
  1327. */
  1328. __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  1329. {
  1330. /* Prevent unused argument(s) compilation warning */
  1331. UNUSED(hi2s);
  1332. /* NOTE : This function Should not be modified, when the callback is needed,
  1333. the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
  1334. */
  1335. }
  1336. /**
  1337. * @brief Rx Transfer completed callbacks
  1338. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1339. * the configuration information for I2S module
  1340. * @retval None
  1341. */
  1342. __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  1343. {
  1344. /* Prevent unused argument(s) compilation warning */
  1345. UNUSED(hi2s);
  1346. /* NOTE : This function Should not be modified, when the callback is needed,
  1347. the HAL_I2S_RxCpltCallback could be implemented in the user file
  1348. */
  1349. }
  1350. /**
  1351. * @brief I2S error callbacks
  1352. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1353. * the configuration information for I2S module
  1354. * @retval None
  1355. */
  1356. __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  1357. {
  1358. /* Prevent unused argument(s) compilation warning */
  1359. UNUSED(hi2s);
  1360. /* NOTE : This function Should not be modified, when the callback is needed,
  1361. the HAL_I2S_ErrorCallback could be implemented in the user file
  1362. */
  1363. }
  1364. /**
  1365. * @}
  1366. */
  1367. /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
  1368. * @brief Peripheral State functions
  1369. *
  1370. @verbatim
  1371. ===============================================================================
  1372. ##### Peripheral State and Errors functions #####
  1373. ===============================================================================
  1374. [..]
  1375. This subsection permits to get in run-time the status of the peripheral
  1376. and the data flow.
  1377. @endverbatim
  1378. * @{
  1379. */
  1380. /**
  1381. * @brief Return the I2S state
  1382. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1383. * the configuration information for I2S module
  1384. * @retval HAL state
  1385. */
  1386. HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
  1387. {
  1388. return hi2s->State;
  1389. }
  1390. /**
  1391. * @brief Return the I2S error code
  1392. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1393. * the configuration information for I2S module
  1394. * @retval I2S Error Code
  1395. */
  1396. uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
  1397. {
  1398. return hi2s->ErrorCode;
  1399. }
  1400. /**
  1401. * @}
  1402. */
  1403. /**
  1404. * @}
  1405. */
  1406. /** @addtogroup I2S_Private_Functions I2S Private Functions
  1407. * @{
  1408. */
  1409. /**
  1410. * @brief DMA I2S transmit process complete callback
  1411. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1412. * the configuration information for the specified DMA module.
  1413. * @retval None
  1414. */
  1415. static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
  1416. {
  1417. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1418. /* if DMA is configured in DMA_NORMAL Mode */
  1419. if (hdma->Init.Mode == DMA_NORMAL)
  1420. {
  1421. /* Disable Tx DMA Request */
  1422. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
  1423. hi2s->TxXferCount = 0U;
  1424. hi2s->State = HAL_I2S_STATE_READY;
  1425. }
  1426. /* Call user Tx complete callback */
  1427. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1428. hi2s->TxCpltCallback(hi2s);
  1429. #else
  1430. HAL_I2S_TxCpltCallback(hi2s);
  1431. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1432. }
  1433. /**
  1434. * @brief DMA I2S transmit process half complete callback
  1435. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1436. * the configuration information for the specified DMA module.
  1437. * @retval None
  1438. */
  1439. static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1440. {
  1441. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1442. /* Call user Tx half complete callback */
  1443. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1444. hi2s->TxHalfCpltCallback(hi2s);
  1445. #else
  1446. HAL_I2S_TxHalfCpltCallback(hi2s);
  1447. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1448. }
  1449. /**
  1450. * @brief DMA I2S receive process complete callback
  1451. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1452. * the configuration information for the specified DMA module.
  1453. * @retval None
  1454. */
  1455. static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
  1456. {
  1457. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1458. /* if DMA is configured in DMA_NORMAL Mode */
  1459. if (hdma->Init.Mode == DMA_NORMAL)
  1460. {
  1461. /* Disable Rx DMA Request */
  1462. CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
  1463. hi2s->RxXferCount = 0U;
  1464. hi2s->State = HAL_I2S_STATE_READY;
  1465. }
  1466. /* Call user Rx complete callback */
  1467. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1468. hi2s->RxCpltCallback(hi2s);
  1469. #else
  1470. HAL_I2S_RxCpltCallback(hi2s);
  1471. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1472. }
  1473. /**
  1474. * @brief DMA I2S receive process half complete callback
  1475. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1476. * the configuration information for the specified DMA module.
  1477. * @retval None
  1478. */
  1479. static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1480. {
  1481. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1482. /* Call user Rx half complete callback */
  1483. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1484. hi2s->RxHalfCpltCallback(hi2s);
  1485. #else
  1486. HAL_I2S_RxHalfCpltCallback(hi2s);
  1487. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1488. }
  1489. /**
  1490. * @brief DMA I2S communication error callback
  1491. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1492. * the configuration information for the specified DMA module.
  1493. * @retval None
  1494. */
  1495. static void I2S_DMAError(DMA_HandleTypeDef *hdma)
  1496. {
  1497. I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  1498. /* Disable Rx and Tx DMA Request */
  1499. CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
  1500. hi2s->TxXferCount = 0U;
  1501. hi2s->RxXferCount = 0U;
  1502. hi2s->State = HAL_I2S_STATE_READY;
  1503. /* Set the error code and execute error callback*/
  1504. SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
  1505. /* Call user error callback */
  1506. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1507. hi2s->ErrorCallback(hi2s);
  1508. #else
  1509. HAL_I2S_ErrorCallback(hi2s);
  1510. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1511. }
  1512. /**
  1513. * @brief Transmit an amount of data in non-blocking mode with Interrupt
  1514. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1515. * the configuration information for I2S module
  1516. * @retval None
  1517. */
  1518. static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
  1519. {
  1520. /* Transmit data */
  1521. hi2s->Instance->DR = (*hi2s->pTxBuffPtr);
  1522. hi2s->pTxBuffPtr++;
  1523. hi2s->TxXferCount--;
  1524. if (hi2s->TxXferCount == 0U)
  1525. {
  1526. /* Disable TXE and ERR interrupt */
  1527. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
  1528. hi2s->State = HAL_I2S_STATE_READY;
  1529. /* Call user Tx complete callback */
  1530. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1531. hi2s->TxCpltCallback(hi2s);
  1532. #else
  1533. HAL_I2S_TxCpltCallback(hi2s);
  1534. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1535. }
  1536. }
  1537. /**
  1538. * @brief Receive an amount of data in non-blocking mode with Interrupt
  1539. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1540. * the configuration information for I2S module
  1541. * @retval None
  1542. */
  1543. static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
  1544. {
  1545. /* Receive data */
  1546. (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR;
  1547. hi2s->pRxBuffPtr++;
  1548. hi2s->RxXferCount--;
  1549. if (hi2s->RxXferCount == 0U)
  1550. {
  1551. /* Disable RXNE and ERR interrupt */
  1552. __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
  1553. hi2s->State = HAL_I2S_STATE_READY;
  1554. /* Call user Rx complete callback */
  1555. #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
  1556. hi2s->RxCpltCallback(hi2s);
  1557. #else
  1558. HAL_I2S_RxCpltCallback(hi2s);
  1559. #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
  1560. }
  1561. }
  1562. /**
  1563. * @brief This function handles I2S Communication Timeout.
  1564. * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
  1565. * the configuration information for I2S module
  1566. * @param Flag Flag checked
  1567. * @param State Value of the flag expected
  1568. * @param Timeout Duration of the timeout
  1569. * @retval HAL status
  1570. */
  1571. static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State,
  1572. uint32_t Timeout)
  1573. {
  1574. uint32_t tickstart;
  1575. /* Get tick */
  1576. tickstart = HAL_GetTick();
  1577. /* Wait until flag is set to status*/
  1578. while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
  1579. {
  1580. if (Timeout != HAL_MAX_DELAY)
  1581. {
  1582. if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U))
  1583. {
  1584. /* Set the I2S State ready */
  1585. hi2s->State = HAL_I2S_STATE_READY;
  1586. /* Process Unlocked */
  1587. __HAL_UNLOCK(hi2s);
  1588. return HAL_TIMEOUT;
  1589. }
  1590. }
  1591. }
  1592. return HAL_OK;
  1593. }
  1594. /**
  1595. * @}
  1596. */
  1597. /**
  1598. * @}
  1599. */
  1600. /**
  1601. * @}
  1602. */
  1603. #endif /* SPI_I2S_SUPPORT */
  1604. #endif /* HAL_I2S_MODULE_ENABLED */