stm32f3xx_hal_i2s.c 45 KB

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