stm32f1xx_hal_i2s.c 48 KB

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