stm32l1xx_hal_i2s.c 47 KB

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