stm32f7xx_hal_sai.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_sai.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief SAI HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Serial Audio Interface (SAI) peripheral:
  10. * + Initialization/de-initialization functions
  11. * + I/O operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The SAI HAL driver can be used as follows:
  21. (#) Declare a SAI_HandleTypeDef handle structure.
  22. (#) Initialize the SAI low level resources by implementing the HAL_SAI_MspInit() API:
  23. (##) Enable the SAI interface clock.
  24. (##) SAI pins configuration:
  25. (+++) Enable the clock for the SAI GPIOs.
  26. (+++) Configure these SAI pins as alternate function pull-up.
  27. (##) NVIC configuration if you need to use interrupt process (HAL_SAI_Transmit_IT()
  28. and HAL_SAI_Receive_IT() APIs):
  29. (+++) Configure the SAI interrupt priority.
  30. (+++) Enable the NVIC SAI IRQ handle.
  31. (##) DMA Configuration if you need to use DMA process (HAL_SAI_Transmit_DMA()
  32. and HAL_SAI_Receive_DMA() APIs):
  33. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  34. (+++) Enable the DMAx interface clock.
  35. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  36. (+++) Configure the DMA Tx/Rx Stream.
  37. (+++) Associate the initialized DMA handle to the SAI DMA Tx/Rx handle.
  38. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  39. DMA Tx/Rx Stream.
  40. (#) Program the SAI Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
  41. using HAL_SAI_Init() function.
  42. -@- The specific SAI interrupts (FIFO request and Overrun underrun interrupt)
  43. will be managed using the macros __SAI_ENABLE_IT() and __SAI_DISABLE_IT()
  44. inside the transmit and receive process.
  45. [..]
  46. (@) SAI Clock Source, the configuration is managed through RCCEx_PeriphCLKConfig()
  47. function in the HAL RCC drivers
  48. [..]
  49. (@) Make sure that either:
  50. (+@) I2S PLL is configured or
  51. (+@) SAI PLL is configured or
  52. (+@) External clock source is configured after setting correctly
  53. the define constant EXTERNAL_CLOCK_VALUE in the stm32f7xx_hal_conf.h file.
  54. [..]
  55. (@) In master Tx mode: enabling the audio block immediately generates the bit clock
  56. for the external slaves even if there is no data in the FIFO, However FS signal
  57. generation is conditioned by the presence of data in the FIFO.
  58. [..]
  59. (@) In master Rx mode: enabling the audio block immediately generates the bit clock
  60. and FS signal for the external slaves.
  61. [..]
  62. (@) It is mandatory to respect the following conditions in order to avoid bad SAI behavior:
  63. (+@) First bit Offset <= (SLOT size - Data size)
  64. (+@) Data size <= SLOT size
  65. (+@) Number of SLOT x SLOT size = Frame length
  66. (+@) The number of slots should be even when SAI_FS_CHANNEL_IDENTIFICATION is selected.
  67. [..]
  68. Three operation modes are available within this driver :
  69. *** Polling mode IO operation ***
  70. =================================
  71. [..]
  72. (+) Send an amount of data in blocking mode using HAL_SAI_Transmit()
  73. (+) Receive an amount of data in blocking mode using HAL_SAI_Receive()
  74. *** Interrupt mode IO operation ***
  75. ===================================
  76. [..]
  77. (+) Send an amount of data in non blocking mode using HAL_SAI_Transmit_IT()
  78. (+) At transmission end of transfer HAL_SAI_TxCpltCallback is executed and user can
  79. add his own code by customization of function pointer HAL_SAI_TxCpltCallback
  80. (+) Receive an amount of data in non blocking mode using HAL_SAI_Receive_IT()
  81. (+) At reception end of transfer HAL_SAI_RxCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_SAI_RxCpltCallback
  83. (+) In case of transfer Error, HAL_SAI_ErrorCallback() function is executed and user can
  84. add his own code by customization of function pointer HAL_SAI_ErrorCallback
  85. *** DMA mode IO operation ***
  86. ==============================
  87. [..]
  88. (+) Send an amount of data in non blocking mode (DMA) using HAL_SAI_Transmit_DMA()
  89. (+) At transmission end of transfer HAL_SAI_TxCpltCallback is executed and user can
  90. add his own code by customization of function pointer HAL_SAI_TxCpltCallback
  91. (+) Receive an amount of data in non blocking mode (DMA) using HAL_SAI_Receive_DMA()
  92. (+) At reception end of transfer HAL_SAI_RxCpltCallback is executed and user can
  93. add his own code by customization of function pointer HAL_SAI_RxCpltCallback
  94. (+) In case of transfer Error, HAL_SAI_ErrorCallback() function is executed and user can
  95. add his own code by customization of function pointer HAL_SAI_ErrorCallback
  96. (+) Pause the DMA Transfer using HAL_SAI_DMAPause()
  97. (+) Resume the DMA Transfer using HAL_SAI_DMAResume()
  98. (+) Stop the DMA Transfer using HAL_SAI_DMAStop()
  99. *** SAI HAL driver macros list ***
  100. =============================================
  101. [..]
  102. Below the list of most used macros in USART HAL driver :
  103. (+) __HAL_SAI_ENABLE: Enable the SAI peripheral
  104. (+) __HAL_SAI_DISABLE: Disable the SAI peripheral
  105. (+) __HAL_SAI_ENABLE_IT : Enable the specified SAI interrupts
  106. (+) __HAL_SAI_DISABLE_IT : Disable the specified SAI interrupts
  107. (+) __HAL_SAI_GET_IT_SOURCE: Check if the specified SAI interrupt source is
  108. enabled or disabled
  109. (+) __HAL_SAI_GET_FLAG: Check whether the specified SAI flag is set or not
  110. @endverbatim
  111. ******************************************************************************
  112. * @attention
  113. *
  114. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  115. *
  116. * Redistribution and use in source and binary forms, with or without modification,
  117. * are permitted provided that the following conditions are met:
  118. * 1. Redistributions of source code must retain the above copyright notice,
  119. * this list of conditions and the following disclaimer.
  120. * 2. Redistributions in binary form must reproduce the above copyright notice,
  121. * this list of conditions and the following disclaimer in the documentation
  122. * and/or other materials provided with the distribution.
  123. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  124. * may be used to endorse or promote products derived from this software
  125. * without specific prior written permission.
  126. *
  127. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  128. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  129. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  130. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  131. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  132. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  133. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  134. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  135. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  136. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  137. *
  138. ******************************************************************************
  139. */
  140. /* Includes ------------------------------------------------------------------*/
  141. #include "stm32f7xx_hal.h"
  142. /** @addtogroup STM32F7xx_HAL_Driver
  143. * @{
  144. */
  145. /** @defgroup SAI SAI
  146. * @brief SAI HAL module driver
  147. * @{
  148. */
  149. #ifdef HAL_SAI_MODULE_ENABLED
  150. /* Private typedef -----------------------------------------------------------*/
  151. /** @defgroup SAI_Private_Typedefs SAI Private Typedefs
  152. * @{
  153. */
  154. typedef enum {
  155. SAI_MODE_DMA,
  156. SAI_MODE_IT
  157. }SAI_ModeTypedef;
  158. /**
  159. * @}
  160. */
  161. /* Private define ------------------------------------------------------------*/
  162. /** @defgroup SAI_Private_Constants SAI Private Constants
  163. * @{
  164. */
  165. #define SAI_FIFO_SIZE 8
  166. #define SAI_DEFAULT_TIMEOUT 4
  167. /**
  168. * @}
  169. */
  170. /* SAI registers Masks */
  171. #define CR1_CLEAR_MASK ((uint32_t)0xFF04C010)
  172. #define FRCR_CLEAR_MASK ((uint32_t)0xFFF88000)
  173. #define SLOTR_CLEAR_MASK ((uint32_t)0x0000F020)
  174. #define SAI_TIMEOUT_VALUE 10
  175. /* Private macro -------------------------------------------------------------*/
  176. /* Private variables ---------------------------------------------------------*/
  177. /* Private function prototypes -----------------------------------------------*/
  178. static void SAI_FillFifo(SAI_HandleTypeDef *hsai);
  179. static uint32_t SAI_InterruptFlag(SAI_HandleTypeDef *hsai, uint32_t mode);
  180. static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
  181. static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
  182. static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai);
  183. static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai);
  184. static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai);
  185. static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai);
  186. static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai);
  187. static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai);
  188. static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai);
  189. static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma);
  190. static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  191. static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma);
  192. static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  193. static void SAI_DMAError(DMA_HandleTypeDef *hdma);
  194. /* Exported functions ---------------------------------------------------------*/
  195. /** @defgroup SAI_Exported_Functions SAI Exported Functions
  196. * @{
  197. */
  198. /** @defgroup SAI_Exported_Functions_Group1 Initialization and de-initialization functions
  199. * @brief Initialization and Configuration functions
  200. *
  201. @verbatim
  202. ===============================================================================
  203. ##### Initialization and de-initialization functions #####
  204. ===============================================================================
  205. [..] This subsection provides a set of functions allowing to initialize and
  206. de-initialize the SAIx peripheral:
  207. (+) User must implement HAL_SAI_MspInit() function in which he configures
  208. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  209. (+) Call the function HAL_SAI_Init() to configure the selected device with
  210. the selected configuration:
  211. (++) Mode (Master/slave TX/RX)
  212. (++) Protocol
  213. (++) Data Size
  214. (++) MCLK Output
  215. (++) Audio frequency
  216. (++) FIFO Threshold
  217. (++) Frame Config
  218. (++) Slot Config
  219. (+) Call the function HAL_SAI_DeInit() to restore the default configuration
  220. of the selected SAI peripheral.
  221. @endverbatim
  222. * @{
  223. */
  224. /**
  225. * @brief Initializes the structure FrameInit, SlotInit and the low part of
  226. * Init according to the specified parameters and call the function
  227. * HAL_SAI_Init to initialize the SAI block.
  228. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  229. * the configuration information for SAI module.
  230. * @param protocol : one of the supported protocol @ref SAI_Protocol
  231. * @param datasize : one of the supported datasize @ref SAI_Protocol_DataSize
  232. * the configuration information for SAI module.
  233. * @param nbslot : Number of slot.
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  237. {
  238. HAL_StatusTypeDef status = HAL_OK;
  239. /* Check the parameters */
  240. assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
  241. assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
  242. switch(protocol)
  243. {
  244. case SAI_I2S_STANDARD :
  245. case SAI_I2S_MSBJUSTIFIED :
  246. case SAI_I2S_LSBJUSTIFIED :
  247. status = SAI_InitI2S(hsai, protocol, datasize, nbslot);
  248. break;
  249. case SAI_PCM_LONG :
  250. case SAI_PCM_SHORT :
  251. status = SAI_InitPCM(hsai, protocol, datasize, nbslot);
  252. break;
  253. default :
  254. status = HAL_ERROR;
  255. break;
  256. }
  257. if(status == HAL_OK)
  258. {
  259. status = HAL_SAI_Init(hsai);
  260. }
  261. return status;
  262. }
  263. /**
  264. * @brief Initializes the SAI according to the specified parameters
  265. * in the SAI_InitTypeDef and create the associated handle.
  266. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  267. * the configuration information for SAI module.
  268. * @retval HAL status
  269. */
  270. HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai)
  271. {
  272. uint32_t tmpclock = 0;
  273. /* This variable used to store the SAI_CK_x (value in Hz) */
  274. uint32_t freq = 0;
  275. /* Check the SAI handle allocation */
  276. if(hsai == NULL)
  277. {
  278. return HAL_ERROR;
  279. }
  280. /* Check the SAI Block parameters */
  281. assert_param(IS_SAI_AUDIO_FREQUENCY(hsai->Init.AudioFrequency));
  282. assert_param(IS_SAI_BLOCK_PROTOCOL(hsai->Init.Protocol));
  283. assert_param(IS_SAI_BLOCK_MODE(hsai->Init.AudioMode));
  284. assert_param(IS_SAI_BLOCK_SYNCEXT(hsai->Init.SynchroExt));
  285. assert_param(IS_SAI_BLOCK_DATASIZE(hsai->Init.DataSize));
  286. assert_param(IS_SAI_BLOCK_FIRST_BIT(hsai->Init.FirstBit));
  287. assert_param(IS_SAI_BLOCK_CLOCK_STROBING(hsai->Init.ClockStrobing));
  288. assert_param(IS_SAI_BLOCK_SYNCHRO(hsai->Init.Synchro));
  289. assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(hsai->Init.OutputDrive));
  290. assert_param(IS_SAI_BLOCK_NODIVIDER(hsai->Init.NoDivider));
  291. assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(hsai->Init.FIFOThreshold));
  292. assert_param(IS_SAI_MONO_STEREO_MODE(hsai->Init.MonoStereoMode));
  293. assert_param(IS_SAI_BLOCK_COMPANDING_MODE(hsai->Init.CompandingMode));
  294. assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(hsai->Init.TriState));
  295. /* Check the SAI Block Frame parameters */
  296. assert_param(IS_SAI_BLOCK_FRAME_LENGTH(hsai->FrameInit.FrameLength));
  297. assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(hsai->FrameInit.ActiveFrameLength));
  298. assert_param(IS_SAI_BLOCK_FS_DEFINITION(hsai->FrameInit.FSDefinition));
  299. assert_param(IS_SAI_BLOCK_FS_POLARITY(hsai->FrameInit.FSPolarity));
  300. assert_param(IS_SAI_BLOCK_FS_OFFSET(hsai->FrameInit.FSOffset));
  301. /* Check the SAI Block Slot parameters */
  302. assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(hsai->SlotInit.FirstBitOffset));
  303. assert_param(IS_SAI_BLOCK_SLOT_SIZE(hsai->SlotInit.SlotSize));
  304. assert_param(IS_SAI_BLOCK_SLOT_NUMBER(hsai->SlotInit.SlotNumber));
  305. assert_param(IS_SAI_SLOT_ACTIVE(hsai->SlotInit.SlotActive));
  306. if(hsai->State == HAL_SAI_STATE_RESET)
  307. {
  308. /* Allocate lock resource and initialize it */
  309. hsai->Lock = HAL_UNLOCKED;
  310. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  311. HAL_SAI_MspInit(hsai);
  312. }
  313. hsai->State = HAL_SAI_STATE_BUSY;
  314. /* Disable the selected SAI peripheral */
  315. SAI_Disable(hsai);
  316. /* SAI Block Synchro Configuration -----------------------------------------*/
  317. SAI_BlockSynchroConfig(hsai);
  318. /* Configure Master Clock using the following formula :
  319. MCLK_x = SAI_CK_x / (MCKDIV[3:0] * 2) with MCLK_x = 256 * FS
  320. FS = SAI_CK_x / (MCKDIV[3:0] * 2) * 256
  321. MCKDIV[3:0] = SAI_CK_x / FS * 512 */
  322. if(hsai->Init.AudioFrequency != SAI_AUDIO_FREQUENCY_MCKDIV)
  323. {
  324. /* Get SAI clock source based on Source clock selection from RCC */
  325. freq = SAI_GetInputClock(hsai);
  326. /* (saiclocksource x 10) to keep Significant digits */
  327. tmpclock = (((freq * 10) / ((hsai->Init.AudioFrequency) * 512)));
  328. hsai->Init.Mckdiv = tmpclock / 10;
  329. /* Round result to the nearest integer */
  330. if((tmpclock % 10) > 8)
  331. {
  332. hsai->Init.Mckdiv+= 1;
  333. }
  334. }
  335. /* SAI Block Configuration ------------------------------------------------------------*/
  336. /* SAI CR1 Configuration */
  337. hsai->Instance->CR1&=~(SAI_xCR1_MODE | SAI_xCR1_PRTCFG | SAI_xCR1_DS | \
  338. SAI_xCR1_LSBFIRST | SAI_xCR1_CKSTR | SAI_xCR1_SYNCEN |\
  339. SAI_xCR1_MONO | SAI_xCR1_OUTDRIV | SAI_xCR1_DMAEN | \
  340. SAI_xCR1_NODIV | SAI_xCR1_MCKDIV);
  341. hsai->Instance->CR1|= (hsai->Init.AudioMode | hsai->Init.Protocol | \
  342. hsai->Init.DataSize | hsai->Init.FirstBit | \
  343. hsai->Init.ClockStrobing | hsai->Init.Synchro | \
  344. hsai->Init.MonoStereoMode | hsai->Init.OutputDrive | \
  345. hsai->Init.NoDivider | (hsai->Init.Mckdiv << 20) | hsai->Init.CompandingMode);
  346. /* SAI CR2 Configuration */
  347. hsai->Instance->CR2&= ~(SAI_xCR2_FTH | SAI_xCR2_FFLUSH | SAI_xCR2_COMP);
  348. hsai->Instance->CR2|= (hsai->Init.FIFOThreshold | hsai->Init.CompandingMode | hsai->Init.TriState);
  349. /* SAI Frame Configuration -----------------------------------------*/
  350. hsai->Instance->FRCR&=(~(SAI_xFRCR_FRL | SAI_xFRCR_FSALL | SAI_xFRCR_FSDEF | \
  351. SAI_xFRCR_FSPO | SAI_xFRCR_FSOFF));
  352. hsai->Instance->FRCR|=((hsai->FrameInit.FrameLength - 1) |
  353. hsai->FrameInit.FSOffset |
  354. hsai->FrameInit.FSDefinition |
  355. hsai->FrameInit.FSPolarity |
  356. ((hsai->FrameInit.ActiveFrameLength - 1) << 8));
  357. /* SAI Block_x SLOT Configuration ------------------------------------------*/
  358. /* This register has no meaning in AC 97 and SPDIF audio protocol */
  359. hsai->Instance->SLOTR&= (~(SAI_xSLOTR_FBOFF | SAI_xSLOTR_SLOTSZ | \
  360. SAI_xSLOTR_NBSLOT | SAI_xSLOTR_SLOTEN ));
  361. hsai->Instance->SLOTR|= hsai->SlotInit.FirstBitOffset | hsai->SlotInit.SlotSize
  362. | hsai->SlotInit.SlotActive | ((hsai->SlotInit.SlotNumber - 1) << 8);
  363. /* Initialise the error code */
  364. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  365. /* Initialize the SAI state */
  366. hsai->State= HAL_SAI_STATE_READY;
  367. /* Release Lock */
  368. __HAL_UNLOCK(hsai);
  369. return HAL_OK;
  370. }
  371. /**
  372. * @brief DeInitializes the SAI peripheral.
  373. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  374. * the configuration information for SAI module.
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_SAI_DeInit(SAI_HandleTypeDef *hsai)
  378. {
  379. /* Check the SAI handle allocation */
  380. if(hsai == NULL)
  381. {
  382. return HAL_ERROR;
  383. }
  384. hsai->State = HAL_SAI_STATE_BUSY;
  385. /* Disabled All interrupt and clear all the flag */
  386. hsai->Instance->IMR = 0;
  387. hsai->Instance->CLRFR = 0xFFFFFFFF;
  388. /* Disable the SAI */
  389. SAI_Disable(hsai);
  390. /* Flush the fifo */
  391. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  392. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  393. HAL_SAI_MspDeInit(hsai);
  394. /* Initialize the error code */
  395. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  396. /* Initialize the SAI state */
  397. hsai->State = HAL_SAI_STATE_RESET;
  398. /* Release Lock */
  399. __HAL_UNLOCK(hsai);
  400. return HAL_OK;
  401. }
  402. /**
  403. * @brief SAI MSP Init.
  404. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  405. * the configuration information for SAI module.
  406. * @retval None
  407. */
  408. __weak void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
  409. {
  410. /* NOTE : This function Should not be modified, when the callback is needed,
  411. the HAL_SAI_MspInit could be implemented in the user file
  412. */
  413. }
  414. /**
  415. * @brief SAI MSP DeInit.
  416. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  417. * the configuration information for SAI module.
  418. * @retval None
  419. */
  420. __weak void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai)
  421. {
  422. /* NOTE : This function Should not be modified, when the callback is needed,
  423. the HAL_SAI_MspDeInit could be implemented in the user file
  424. */
  425. }
  426. /**
  427. * @}
  428. */
  429. /** @defgroup SAI_Exported_Functions_Group2 IO operation functions
  430. * @brief Data transfers functions
  431. *
  432. @verbatim
  433. ===============================================================================
  434. ##### IO operation functions #####
  435. ===============================================================================
  436. [..]
  437. This subsection provides a set of functions allowing to manage the SAI data
  438. transfers.
  439. (+) There are two modes of transfer:
  440. (++) Blocking mode : The communication is performed in the polling mode.
  441. The status of all data processing is returned by the same function
  442. after finishing transfer.
  443. (++) No-Blocking mode : The communication is performed using Interrupts
  444. or DMA. These functions return the status of the transfer startup.
  445. The end of the data processing will be indicated through the
  446. dedicated SAI IRQ when using Interrupt mode or the DMA IRQ when
  447. using DMA mode.
  448. (+) Blocking mode functions are :
  449. (++) HAL_SAI_Transmit()
  450. (++) HAL_SAI_Receive()
  451. (++) HAL_SAI_TransmitReceive()
  452. (+) Non Blocking mode functions with Interrupt are :
  453. (++) HAL_SAI_Transmit_IT()
  454. (++) HAL_SAI_Receive_IT()
  455. (++) HAL_SAI_TransmitReceive_IT()
  456. (+) Non Blocking mode functions with DMA are :
  457. (++) HAL_SAI_Transmit_DMA()
  458. (++) HAL_SAI_Receive_DMA()
  459. (++) HAL_SAI_TransmitReceive_DMA()
  460. (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  461. (++) HAL_SAI_TxCpltCallback()
  462. (++) HAL_SAI_RxCpltCallback()
  463. (++) HAL_SAI_ErrorCallback()
  464. @endverbatim
  465. * @{
  466. */
  467. /**
  468. * @brief Transmits an amount of data in blocking mode.
  469. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  470. * the configuration information for SAI module.
  471. * @param pData: Pointer to data buffer
  472. * @param Size: Amount of data to be sent
  473. * @param Timeout: Timeout duration
  474. * @retval HAL status
  475. */
  476. HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t* pData, uint16_t Size, uint32_t Timeout)
  477. {
  478. uint32_t tickstart = 0;
  479. if((pData == NULL ) || (Size == 0))
  480. {
  481. return HAL_ERROR;
  482. }
  483. if(hsai->State == HAL_SAI_STATE_READY)
  484. {
  485. /* Process Locked */
  486. __HAL_LOCK(hsai);
  487. hsai->State = HAL_SAI_STATE_BUSY_TX;
  488. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  489. hsai->XferSize = Size;
  490. hsai->XferCount = Size;
  491. hsai->pBuffPtr = pData;
  492. /* Check if the SAI is already enabled */
  493. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  494. {
  495. /* fill the fifo with data before to enabled the SAI */
  496. SAI_FillFifo(hsai);
  497. /* Enable SAI peripheral */
  498. __HAL_SAI_ENABLE(hsai);
  499. }
  500. while(hsai->XferCount > 0)
  501. {
  502. /* Write data if the FIFO is not full */
  503. if((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL)
  504. {
  505. if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  506. {
  507. hsai->Instance->DR = (*hsai->pBuffPtr++);
  508. }
  509. else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
  510. {
  511. hsai->Instance->DR = *((uint16_t *)hsai->pBuffPtr);
  512. hsai->pBuffPtr+= 2;
  513. }
  514. else
  515. {
  516. hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
  517. hsai->pBuffPtr+= 4;
  518. }
  519. hsai->XferCount--;
  520. }
  521. else
  522. {
  523. /* Get tick */
  524. tickstart = HAL_GetTick();
  525. /* Check for the Timeout */
  526. if(Timeout != HAL_MAX_DELAY)
  527. {
  528. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  529. {
  530. /* Update error code */
  531. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  532. /* Process Unlocked */
  533. __HAL_UNLOCK(hsai);
  534. /* Change the SAI state */
  535. hsai->State = HAL_SAI_STATE_TIMEOUT;
  536. return HAL_TIMEOUT;
  537. }
  538. }
  539. }
  540. }
  541. hsai->State = HAL_SAI_STATE_READY;
  542. /* Process Unlocked */
  543. __HAL_UNLOCK(hsai);
  544. return HAL_OK;
  545. }
  546. else
  547. {
  548. return HAL_BUSY;
  549. }
  550. }
  551. /**
  552. * @brief Receives an amount of data in blocking mode.
  553. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  554. * the configuration information for SAI module.
  555. * @param pData: Pointer to data buffer
  556. * @param Size: Amount of data to be received
  557. * @param Timeout: Timeout duration
  558. * @retval HAL status
  559. */
  560. HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  561. {
  562. uint32_t tickstart = 0;
  563. if((pData == NULL ) || (Size == 0))
  564. {
  565. return HAL_ERROR;
  566. }
  567. if(hsai->State == HAL_SAI_STATE_READY)
  568. {
  569. /* Process Locked */
  570. __HAL_LOCK(hsai);
  571. hsai->State = HAL_SAI_STATE_BUSY_RX;
  572. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  573. hsai->pBuffPtr = pData;
  574. hsai->XferSize = Size;
  575. hsai->XferCount = Size;
  576. /* Check if the SAI is already enabled */
  577. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  578. {
  579. /* Enable SAI peripheral */
  580. __HAL_SAI_ENABLE(hsai);
  581. }
  582. /* Receive data */
  583. while(hsai->XferCount > 0)
  584. {
  585. if((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_EMPTY)
  586. {
  587. if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  588. {
  589. (*hsai->pBuffPtr++) = hsai->Instance->DR;
  590. }
  591. else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
  592. {
  593. *((uint16_t*)hsai->pBuffPtr) = hsai->Instance->DR;
  594. hsai->pBuffPtr+= 2;
  595. }
  596. else
  597. {
  598. *((uint32_t*)hsai->pBuffPtr) = hsai->Instance->DR;
  599. hsai->pBuffPtr+= 4;
  600. }
  601. hsai->XferCount--;
  602. }
  603. else
  604. {
  605. /* Get tick */
  606. tickstart = HAL_GetTick();
  607. /* Check for the Timeout */
  608. if(Timeout != HAL_MAX_DELAY)
  609. {
  610. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  611. {
  612. /* Update error code */
  613. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  614. /* Process Unlocked */
  615. __HAL_UNLOCK(hsai);
  616. /* Change the SAI state */
  617. hsai->State = HAL_SAI_STATE_TIMEOUT;
  618. return HAL_TIMEOUT;
  619. }
  620. }
  621. }
  622. }
  623. hsai->State = HAL_SAI_STATE_READY;
  624. /* Process Unlocked */
  625. __HAL_UNLOCK(hsai);
  626. return HAL_OK;
  627. }
  628. else
  629. {
  630. return HAL_BUSY;
  631. }
  632. }
  633. /**
  634. * @brief Transmits an amount of data in no-blocking mode with Interrupt.
  635. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  636. * the configuration information for SAI module.
  637. * @param pData: Pointer to data buffer
  638. * @param Size: Amount of data to be sent
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  642. {
  643. if(hsai->State == HAL_SAI_STATE_READY)
  644. {
  645. if((pData == NULL) || (Size == 0))
  646. {
  647. return HAL_ERROR;
  648. }
  649. hsai->pBuffPtr = pData;
  650. hsai->XferSize = Size;
  651. hsai->XferCount = Size;
  652. /* Process Locked */
  653. __HAL_LOCK(hsai);
  654. hsai->State = HAL_SAI_STATE_BUSY_TX;
  655. if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  656. {
  657. hsai->InterruptServiceRoutine = SAI_Transmit_IT8Bit;
  658. }
  659. else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
  660. {
  661. hsai->InterruptServiceRoutine = SAI_Transmit_IT16Bit;
  662. }
  663. else
  664. {
  665. hsai->InterruptServiceRoutine = SAI_Transmit_IT32Bit;
  666. }
  667. /* Enable FRQ and OVRUDR interrupts */
  668. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  669. /* Check if the SAI is already enabled */
  670. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  671. {
  672. /* Fill the fifo before starting the communication */
  673. SAI_FillFifo(hsai);
  674. /* Enable SAI peripheral */
  675. __HAL_SAI_ENABLE(hsai);
  676. }
  677. /* Process Unlocked */
  678. __HAL_UNLOCK(hsai);
  679. return HAL_OK;
  680. }
  681. else
  682. {
  683. return HAL_BUSY;
  684. }
  685. }
  686. /**
  687. * @brief Receives an amount of data in no-blocking mode with Interrupt.
  688. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  689. * the configuration information for SAI module.
  690. * @param pData: Pointer to data buffer
  691. * @param Size: Amount of data to be received
  692. * @retval HAL status
  693. */
  694. HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  695. {
  696. if(hsai->State == HAL_SAI_STATE_READY)
  697. {
  698. if((pData == NULL) || (Size == 0))
  699. {
  700. return HAL_ERROR;
  701. }
  702. hsai->pBuffPtr = pData;
  703. hsai->XferSize = Size;
  704. hsai->XferCount = Size;
  705. /* Process Locked */
  706. __HAL_LOCK(hsai);
  707. hsai->State = HAL_SAI_STATE_BUSY_RX;
  708. if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  709. {
  710. hsai->InterruptServiceRoutine = SAI_Receive_IT8Bit;
  711. }
  712. else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
  713. {
  714. hsai->InterruptServiceRoutine = SAI_Receive_IT16Bit;
  715. }
  716. else
  717. {
  718. hsai->InterruptServiceRoutine = SAI_Receive_IT32Bit;
  719. }
  720. /* Enable TXE and OVRUDR interrupts */
  721. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  722. /* Check if the SAI is already enabled */
  723. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  724. {
  725. /* Enable SAI peripheral */
  726. __HAL_SAI_ENABLE(hsai);
  727. }
  728. /* Process Unlocked */
  729. __HAL_UNLOCK(hsai);
  730. return HAL_OK;
  731. }
  732. else
  733. {
  734. return HAL_BUSY;
  735. }
  736. }
  737. /**
  738. * @brief Pauses the audio stream playing from the Media.
  739. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  740. * the configuration information for SAI module.
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai)
  744. {
  745. /* Process Locked */
  746. __HAL_LOCK(hsai);
  747. /* Pause the audio file playing by disabling the SAI DMA requests */
  748. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  749. /* Process Unlocked */
  750. __HAL_UNLOCK(hsai);
  751. return HAL_OK;
  752. }
  753. /**
  754. * @brief Resumes the audio stream playing from the Media.
  755. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  756. * the configuration information for SAI module.
  757. * @retval HAL status
  758. */
  759. HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai)
  760. {
  761. /* Process Locked */
  762. __HAL_LOCK(hsai);
  763. /* Enable the SAI DMA requests */
  764. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  765. /* If the SAI peripheral is still not enabled, enable it */
  766. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
  767. {
  768. /* Enable SAI peripheral */
  769. __HAL_SAI_ENABLE(hsai);
  770. }
  771. /* Process Unlocked */
  772. __HAL_UNLOCK(hsai);
  773. return HAL_OK;
  774. }
  775. /**
  776. * @brief Stops the audio stream playing from the Media.
  777. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  778. * the configuration information for SAI module.
  779. * @retval HAL status
  780. */
  781. HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai)
  782. {
  783. /* Process Locked */
  784. __HAL_LOCK(hsai);
  785. /* Disable the SAI DMA request */
  786. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  787. /* Abort the SAI DMA Tx Stream */
  788. if(hsai->hdmatx != NULL)
  789. {
  790. HAL_DMA_Abort(hsai->hdmatx);
  791. }
  792. /* Abort the SAI DMA Rx Stream */
  793. if(hsai->hdmarx != NULL)
  794. {
  795. HAL_DMA_Abort(hsai->hdmarx);
  796. }
  797. /* Disable SAI peripheral */
  798. SAI_Disable(hsai);
  799. hsai->State = HAL_SAI_STATE_READY;
  800. /* Process Unlocked */
  801. __HAL_UNLOCK(hsai);
  802. return HAL_OK;
  803. }
  804. /**
  805. * @brief Abort the current transfer and disbaled the SAI.
  806. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  807. * the configuration information for SAI module.
  808. * @retval HAL status
  809. */
  810. HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai)
  811. {
  812. /* Disable the SAI DMA request */
  813. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  814. /* Abort the SAI DMA Tx Stream */
  815. if(hsai->hdmatx != NULL)
  816. {
  817. HAL_DMA_Abort(hsai->hdmatx);
  818. }
  819. /* Abort the SAI DMA Rx Stream */
  820. if(hsai->hdmarx != NULL)
  821. {
  822. HAL_DMA_Abort(hsai->hdmarx);
  823. }
  824. /* Disabled All interrupt and clear all the flag */
  825. hsai->Instance->IMR = 0;
  826. hsai->Instance->CLRFR = 0xFFFFFFFF;
  827. /* Disable SAI peripheral */
  828. SAI_Disable(hsai);
  829. /* Flush the fifo */
  830. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  831. hsai->State = HAL_SAI_STATE_READY;
  832. /* Process Unlocked */
  833. __HAL_UNLOCK(hsai);
  834. return HAL_OK;
  835. }
  836. /**
  837. * @brief Transmits an amount of data in no-blocking mode with DMA.
  838. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  839. * the configuration information for SAI module.
  840. * @param pData: Pointer to data buffer
  841. * @param Size: Amount of data to be sent
  842. * @retval HAL status
  843. */
  844. HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  845. {
  846. uint32_t *tmp;
  847. if((pData == NULL) || (Size == 0))
  848. {
  849. return HAL_ERROR;
  850. }
  851. if(hsai->State == HAL_SAI_STATE_READY)
  852. {
  853. hsai->pBuffPtr = pData;
  854. hsai->XferSize = Size;
  855. hsai->XferCount = Size;
  856. /* Process Locked */
  857. __HAL_LOCK(hsai);
  858. hsai->State = HAL_SAI_STATE_BUSY_TX;
  859. /* Set the SAI Tx DMA Half transfer complete callback */
  860. hsai->hdmatx->XferHalfCpltCallback = SAI_DMATxHalfCplt;
  861. /* Set the SAI TxDMA transfer complete callback */
  862. hsai->hdmatx->XferCpltCallback = SAI_DMATxCplt;
  863. /* Set the DMA error callback */
  864. hsai->hdmatx->XferErrorCallback = SAI_DMAError;
  865. /* Enable the Tx DMA Stream */
  866. tmp = (uint32_t*)&pData;
  867. HAL_DMA_Start_IT(hsai->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsai->Instance->DR, hsai->XferSize);
  868. /* Check if the SAI is already enabled */
  869. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  870. {
  871. /* Enable SAI peripheral */
  872. __HAL_SAI_ENABLE(hsai);
  873. }
  874. /* Enable the interrupts for error handling */
  875. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  876. /* Enable SAI Tx DMA Request */
  877. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  878. /* Process Unlocked */
  879. __HAL_UNLOCK(hsai);
  880. return HAL_OK;
  881. }
  882. else
  883. {
  884. return HAL_BUSY;
  885. }
  886. }
  887. /**
  888. * @brief Receives an amount of data in no-blocking mode with DMA.
  889. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  890. * the configuration information for SAI module.
  891. * @param pData: Pointer to data buffer
  892. * @param Size: Amount of data to be received
  893. * @retval HAL status
  894. */
  895. HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  896. {
  897. uint32_t *tmp;
  898. if((pData == NULL) || (Size == 0))
  899. {
  900. return HAL_ERROR;
  901. }
  902. if(hsai->State == HAL_SAI_STATE_READY)
  903. {
  904. hsai->pBuffPtr = pData;
  905. hsai->XferSize = Size;
  906. hsai->XferCount = Size;
  907. /* Process Locked */
  908. __HAL_LOCK(hsai);
  909. hsai->State = HAL_SAI_STATE_BUSY_RX;
  910. /* Set the SAI Rx DMA Half transfer complete callback */
  911. hsai->hdmarx->XferHalfCpltCallback = SAI_DMARxHalfCplt;
  912. /* Set the SAI Rx DMA transfer complete callback */
  913. hsai->hdmarx->XferCpltCallback = SAI_DMARxCplt;
  914. /* Set the DMA error callback */
  915. hsai->hdmarx->XferErrorCallback = SAI_DMAError;
  916. /* Enable the Rx DMA Stream */
  917. tmp = (uint32_t*)&pData;
  918. HAL_DMA_Start_IT(hsai->hdmarx, (uint32_t)&hsai->Instance->DR, *(uint32_t*)tmp, hsai->XferSize);
  919. /* Check if the SAI is already enabled */
  920. if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != SAI_xCR1_SAIEN)
  921. {
  922. /* Enable SAI peripheral */
  923. __HAL_SAI_ENABLE(hsai);
  924. }
  925. /* Enable the interrupts for error handling */
  926. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  927. /* Enable SAI Rx DMA Request */
  928. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  929. /* Process Unlocked */
  930. __HAL_UNLOCK(hsai);
  931. return HAL_OK;
  932. }
  933. else
  934. {
  935. return HAL_BUSY;
  936. }
  937. }
  938. /**
  939. * @brief Enable the tx mute mode.
  940. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  941. * the configuration information for SAI module.
  942. * @param val : value sent during the mute @ref SAI_Block_Mute_Value
  943. * @retval HAL status
  944. */
  945. HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val)
  946. {
  947. assert_param(IS_SAI_BLOCK_MUTE_VALUE(val));
  948. if(hsai->State != HAL_SAI_STATE_RESET)
  949. {
  950. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
  951. SET_BIT(hsai->Instance->CR2, SAI_xCR2_MUTE | val);
  952. return HAL_OK;
  953. }
  954. return HAL_ERROR;
  955. }
  956. /**
  957. * @brief Disable the tx mute mode.
  958. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  959. * the configuration information for SAI module.
  960. * @retval HAL status
  961. */
  962. HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai)
  963. {
  964. if(hsai->State != HAL_SAI_STATE_RESET)
  965. {
  966. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
  967. return HAL_OK;
  968. }
  969. return HAL_ERROR;
  970. }
  971. /**
  972. * @brief Enable the rx mute detection.
  973. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  974. * the configuration information for SAI module.
  975. * @param callback : function called when the mute is detected
  976. * @param counter : number a data before mute detection max 63.
  977. * @retval HAL status
  978. */
  979. HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter)
  980. {
  981. assert_param(IS_SAI_BLOCK_MUTE_COUNTER(counter));
  982. if(hsai->State != HAL_SAI_STATE_RESET)
  983. {
  984. /* set the mute counter */
  985. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTECNT);
  986. SET_BIT(hsai->Instance->CR2, (uint32_t)((uint32_t)counter << 6));
  987. hsai->mutecallback = callback;
  988. /* enable the IT interrupt */
  989. __HAL_SAI_ENABLE_IT(hsai, SAI_IT_MUTEDET);
  990. return HAL_OK;
  991. }
  992. return HAL_ERROR;
  993. }
  994. /**
  995. * @brief Disable the rx mute detection.
  996. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  997. * the configuration information for SAI module.
  998. * @retval HAL status
  999. */
  1000. HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai)
  1001. {
  1002. if(hsai->State != HAL_SAI_STATE_RESET)
  1003. {
  1004. /* set the mutecallback to NULL */
  1005. hsai->mutecallback = (SAIcallback)NULL;
  1006. /* enable the IT interrupt */
  1007. __HAL_SAI_DISABLE_IT(hsai, SAI_IT_MUTEDET);
  1008. return HAL_OK;
  1009. }
  1010. return HAL_ERROR;
  1011. }
  1012. /**
  1013. * @brief This function handles SAI interrupt request.
  1014. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1015. * the configuration information for SAI module.
  1016. * @retval HAL status
  1017. */
  1018. void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai)
  1019. {
  1020. if(hsai->State != HAL_SAI_STATE_RESET)
  1021. {
  1022. uint32_t tmpFlag = hsai->Instance->SR;
  1023. uint32_t tmpItSource = hsai->Instance->IMR;
  1024. if(((tmpFlag & SAI_xSR_FREQ) == SAI_xSR_FREQ) && ((tmpItSource & SAI_IT_FREQ) == SAI_IT_FREQ))
  1025. {
  1026. hsai->InterruptServiceRoutine(hsai);
  1027. }
  1028. /* check the flag only if one of them is set */
  1029. if(tmpFlag != 0x00000000)
  1030. {
  1031. /* SAI Overrun error interrupt occurred ----------------------------------*/
  1032. if(((tmpFlag & SAI_FLAG_OVRUDR) == SAI_FLAG_OVRUDR) && ((tmpItSource & SAI_IT_OVRUDR) == SAI_IT_OVRUDR))
  1033. {
  1034. /* Clear the SAI Overrun flag */
  1035. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  1036. /* Change the SAI error code */
  1037. hsai->ErrorCode = ((hsai->State == HAL_SAI_STATE_BUSY_RX) ? HAL_SAI_ERROR_OVR : HAL_SAI_ERROR_UDR);
  1038. /* the transfer is not stopped, we will forward the information to the user and we let the user decide what needs to be done */
  1039. HAL_SAI_ErrorCallback(hsai);
  1040. }
  1041. /* SAI mutedet interrupt occurred ----------------------------------*/
  1042. if(((tmpFlag & SAI_FLAG_MUTEDET) == SAI_FLAG_MUTEDET) && ((tmpItSource & SAI_IT_MUTEDET) == SAI_IT_MUTEDET))
  1043. {
  1044. /* Clear the SAI mutedet flag */
  1045. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_MUTEDET);
  1046. /* call the call back function */
  1047. if(hsai->mutecallback != (SAIcallback)NULL)
  1048. {
  1049. /* inform the user that an RX mute event has been detected */
  1050. hsai->mutecallback();
  1051. }
  1052. }
  1053. /* SAI AFSDET interrupt occurred ----------------------------------*/
  1054. if(((tmpFlag & SAI_FLAG_AFSDET) == SAI_FLAG_AFSDET) && ((tmpItSource & SAI_IT_AFSDET) == SAI_IT_AFSDET))
  1055. {
  1056. /* Change the SAI error code */
  1057. hsai->ErrorCode = HAL_SAI_ERROR_AFSDET;
  1058. HAL_SAI_Abort(hsai);
  1059. HAL_SAI_ErrorCallback(hsai);
  1060. }
  1061. /* SAI LFSDET interrupt occurred ----------------------------------*/
  1062. if(((tmpFlag & SAI_FLAG_LFSDET) == SAI_FLAG_LFSDET) && ((tmpItSource & SAI_IT_LFSDET) == SAI_IT_LFSDET))
  1063. {
  1064. /* Change the SAI error code */
  1065. hsai->ErrorCode = HAL_SAI_ERROR_LFSDET;
  1066. HAL_SAI_Abort(hsai);
  1067. HAL_SAI_ErrorCallback(hsai);
  1068. }
  1069. /* SAI WCKCFG interrupt occurred ----------------------------------*/
  1070. if(((tmpFlag & SAI_FLAG_WCKCFG) == SAI_FLAG_WCKCFG) && ((tmpItSource & SAI_IT_WCKCFG) == SAI_IT_WCKCFG))
  1071. {
  1072. /* Change the SAI error code */
  1073. hsai->ErrorCode = HAL_SAI_ERROR_WCKCFG;
  1074. HAL_SAI_Abort(hsai);
  1075. HAL_SAI_ErrorCallback(hsai);
  1076. }
  1077. }
  1078. }
  1079. }
  1080. /**
  1081. * @brief Tx Transfer completed callbacks.
  1082. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1083. * the configuration information for SAI module.
  1084. * @retval None
  1085. */
  1086. __weak void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
  1087. {
  1088. /* NOTE : This function Should not be modified, when the callback is needed,
  1089. the HAL_SAI_TxCpltCallback could be implemented in the user file
  1090. */
  1091. }
  1092. /**
  1093. * @brief Tx Transfer Half completed callbacks
  1094. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1095. * the configuration information for SAI module.
  1096. * @retval None
  1097. */
  1098. __weak void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
  1099. {
  1100. /* NOTE : This function Should not be modified, when the callback is needed,
  1101. the HAL_SAI_TxHalfCpltCallback could be implemented in the user file
  1102. */
  1103. }
  1104. /**
  1105. * @brief Rx Transfer completed callbacks.
  1106. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1107. * the configuration information for SAI module.
  1108. * @retval None
  1109. */
  1110. __weak void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai)
  1111. {
  1112. /* NOTE : This function Should not be modified, when the callback is needed,
  1113. the HAL_SAI_RxCpltCallback could be implemented in the user file
  1114. */
  1115. }
  1116. /**
  1117. * @brief Rx Transfer half completed callbacks
  1118. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1119. * the configuration information for SAI module.
  1120. * @retval None
  1121. */
  1122. __weak void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai)
  1123. {
  1124. /* NOTE : This function Should not be modified, when the callback is needed,
  1125. the HAL_SAI_RxCpltCallback could be implemented in the user file
  1126. */
  1127. }
  1128. /**
  1129. * @brief SAI error callbacks.
  1130. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1131. * the configuration information for SAI module.
  1132. * @retval None
  1133. */
  1134. __weak void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
  1135. {
  1136. /* NOTE : This function Should not be modified, when the callback is needed,
  1137. the HAL_SAI_ErrorCallback could be implemented in the user file
  1138. */
  1139. }
  1140. /**
  1141. * @}
  1142. */
  1143. /** @defgroup SAI_Exported_Functions_Group3 Peripheral State functions
  1144. * @brief Peripheral State functions
  1145. *
  1146. @verbatim
  1147. ===============================================================================
  1148. ##### Peripheral State and Errors functions #####
  1149. ===============================================================================
  1150. [..]
  1151. This subsection permits to get in run-time the status of the peripheral
  1152. and the data flow.
  1153. @endverbatim
  1154. * @{
  1155. */
  1156. /**
  1157. * @brief Returns the SAI state.
  1158. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  1159. * the configuration information for SAI module.
  1160. * @retval HAL state
  1161. */
  1162. HAL_SAI_StateTypeDef HAL_SAI_GetState(SAI_HandleTypeDef *hsai)
  1163. {
  1164. return hsai->State;
  1165. }
  1166. /**
  1167. * @brief Return the SAI error code
  1168. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1169. * the configuration information for the specified SAI Block.
  1170. * @retval SAI Error Code
  1171. */
  1172. uint32_t HAL_SAI_GetError(SAI_HandleTypeDef *hsai)
  1173. {
  1174. return hsai->ErrorCode;
  1175. }
  1176. /**
  1177. * @}
  1178. */
  1179. /**
  1180. * @brief Initializes the SAI I2S protocol according to the specified parameters
  1181. * in the SAI_InitTypeDef and create the associated handle.
  1182. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1183. * the configuration information for SAI module.
  1184. * @param protocol : one of the supported protocol
  1185. * @param datasize : one of the supported datasize @ref SAI_Protocol_DataSize
  1186. * the configuration information for SAI module.
  1187. * @param nbslot : number of slot minimum value is 2 and max is 16.
  1188. * the value must be a multiple of 2.
  1189. * @retval HAL status
  1190. */
  1191. static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  1192. {
  1193. /* Check the parameters */
  1194. assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
  1195. assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
  1196. hsai->Init.Protocol = SAI_FREE_PROTOCOL;
  1197. hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
  1198. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  1199. hsai->FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
  1200. hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
  1201. hsai->SlotInit.FirstBitOffset = 0;
  1202. hsai->SlotInit.SlotNumber = nbslot;
  1203. /* in IS2 the number of slot must be even */
  1204. if((nbslot & 0x1) != 0 )
  1205. {
  1206. return HAL_ERROR;
  1207. }
  1208. switch(protocol)
  1209. {
  1210. case SAI_I2S_STANDARD :
  1211. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
  1212. hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
  1213. break;
  1214. case SAI_I2S_MSBJUSTIFIED :
  1215. case SAI_I2S_LSBJUSTIFIED :
  1216. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
  1217. hsai->FrameInit.FSOffset = SAI_FS_FIRSTBIT;
  1218. break;
  1219. default :
  1220. return HAL_ERROR;
  1221. }
  1222. /* Frame definition */
  1223. hsai->Init.DataSize = 0xFFFFFFFF;
  1224. switch(datasize)
  1225. {
  1226. case SAI_PROTOCOL_DATASIZE_16BIT:
  1227. hsai->Init.DataSize = SAI_DATASIZE_16;
  1228. hsai->FrameInit.FrameLength = 32*(nbslot/2);
  1229. hsai->FrameInit.ActiveFrameLength = 16*(nbslot/2);
  1230. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
  1231. break;
  1232. case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
  1233. if(hsai->Init.DataSize == 0xFFFFFFFF)
  1234. {
  1235. hsai->Init.DataSize = SAI_DATASIZE_16;
  1236. }
  1237. break;
  1238. case SAI_PROTOCOL_DATASIZE_24BIT:
  1239. if(hsai->Init.DataSize == 0xFFFFFFFF)
  1240. {
  1241. hsai->Init.DataSize = SAI_DATASIZE_24;
  1242. }
  1243. break;
  1244. case SAI_PROTOCOL_DATASIZE_32BIT:
  1245. if(hsai->Init.DataSize == 0xFFFFFFFF)
  1246. {
  1247. hsai->Init.DataSize = SAI_DATASIZE_32;
  1248. }
  1249. hsai->FrameInit.FrameLength = 64*(nbslot/2);
  1250. hsai->FrameInit.ActiveFrameLength = 32*(nbslot/2);
  1251. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1252. if(protocol == SAI_I2S_LSBJUSTIFIED)
  1253. {
  1254. if (datasize == SAI_PROTOCOL_DATASIZE_16BITEXTENDED)
  1255. {
  1256. hsai->SlotInit.FirstBitOffset = 16;
  1257. }
  1258. if (datasize == SAI_PROTOCOL_DATASIZE_24BIT)
  1259. {
  1260. hsai->SlotInit.FirstBitOffset = 8;
  1261. }
  1262. }
  1263. break;
  1264. default :
  1265. return HAL_ERROR;
  1266. }
  1267. return HAL_OK;
  1268. }
  1269. /**
  1270. * @brief Initializes the SAI PCM protocol according to the specified parameters
  1271. * in the SAI_InitTypeDef and create the associated handle.
  1272. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1273. * the configuration information for SAI module.
  1274. * @param protocol : one of the supported protocol
  1275. * @param datasize : one of the supported datasize @ref SAI_Protocol_DataSize
  1276. * @param nbslot : number of slot minimum value is 1 and the max is 16.
  1277. * @retval HAL status
  1278. */
  1279. static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  1280. {
  1281. /* Check the parameters */
  1282. assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
  1283. assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
  1284. hsai->Init.Protocol = SAI_FREE_PROTOCOL;
  1285. hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
  1286. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  1287. hsai->FrameInit.FSDefinition = SAI_FS_STARTFRAME;
  1288. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
  1289. hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
  1290. hsai->SlotInit.FirstBitOffset = 0;
  1291. hsai->SlotInit.SlotNumber = nbslot;
  1292. hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
  1293. switch(protocol)
  1294. {
  1295. case SAI_PCM_SHORT :
  1296. hsai->FrameInit.ActiveFrameLength = 1;
  1297. break;
  1298. case SAI_PCM_LONG :
  1299. hsai->FrameInit.ActiveFrameLength = 13;
  1300. break;
  1301. default :
  1302. return HAL_ERROR;
  1303. }
  1304. switch(datasize)
  1305. {
  1306. case SAI_PROTOCOL_DATASIZE_16BIT:
  1307. hsai->Init.DataSize = SAI_DATASIZE_16;
  1308. hsai->FrameInit.FrameLength = 16 * nbslot;
  1309. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
  1310. break;
  1311. case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
  1312. hsai->Init.DataSize = SAI_DATASIZE_16;
  1313. hsai->FrameInit.FrameLength = 32 * nbslot;
  1314. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1315. break;
  1316. case SAI_PROTOCOL_DATASIZE_32BIT:
  1317. hsai->Init.DataSize = SAI_DATASIZE_32;
  1318. hsai->FrameInit.FrameLength = 32 * nbslot;
  1319. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1320. break;
  1321. default :
  1322. return HAL_ERROR;
  1323. }
  1324. return HAL_OK;
  1325. }
  1326. /**
  1327. * @brief Fill the fifo
  1328. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1329. * the configuration information for SAI module.
  1330. * @retval None.
  1331. */
  1332. static void SAI_FillFifo(SAI_HandleTypeDef *hsai)
  1333. {
  1334. /* fill the fifo with data before to enabled the SAI */
  1335. while((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL)
  1336. {
  1337. if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  1338. {
  1339. hsai->Instance->DR = (*hsai->pBuffPtr++);
  1340. }
  1341. else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
  1342. {
  1343. hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
  1344. hsai->pBuffPtr+= 2;
  1345. }
  1346. else
  1347. {
  1348. hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
  1349. hsai->pBuffPtr+= 4;
  1350. }
  1351. hsai->XferCount--;
  1352. }
  1353. }
  1354. /**
  1355. * @brief return the interrupt flag to set according the SAI setup
  1356. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1357. * the configuration information for SAI module.
  1358. * @param mode : SAI_MODE_DMA or SAI_MODE_IT
  1359. * @retval the list of the IT flag to enable
  1360. */
  1361. static uint32_t SAI_InterruptFlag(SAI_HandleTypeDef *hsai, uint32_t mode)
  1362. {
  1363. uint32_t tmpIT = SAI_IT_OVRUDR;
  1364. if(mode == SAI_MODE_IT)
  1365. {
  1366. tmpIT|= SAI_IT_FREQ;
  1367. }
  1368. if((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
  1369. {
  1370. tmpIT|= SAI_IT_AFSDET | SAI_IT_LFSDET;
  1371. }
  1372. else
  1373. {
  1374. /* hsai has been configured in master mode */
  1375. tmpIT|= SAI_IT_WCKCFG;
  1376. }
  1377. return tmpIT;
  1378. }
  1379. /**
  1380. * @brief disable the SAI and wait the disabling
  1381. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1382. * the configuration information for SAI module.
  1383. * @retval None.
  1384. */
  1385. static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai)
  1386. {
  1387. uint32_t tickstart = HAL_GetTick();
  1388. HAL_StatusTypeDef status = HAL_OK;
  1389. __HAL_SAI_DISABLE(hsai);
  1390. while((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != RESET)
  1391. {
  1392. /* Check for the Timeout */
  1393. if((HAL_GetTick() - tickstart ) > SAI_TIMEOUT_VALUE)
  1394. {
  1395. /* Update error code */
  1396. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  1397. status = HAL_TIMEOUT;
  1398. /* Change the SAI state */
  1399. HAL_SAI_ErrorCallback(hsai);
  1400. }
  1401. }
  1402. return status;
  1403. }
  1404. /**
  1405. * @brief Tx Handler for Transmit in Interrupt mode 8Bit transfer
  1406. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1407. * the configuration information for SAI module.
  1408. * @retval None.
  1409. */
  1410. static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai)
  1411. {
  1412. /* Write data on DR register */
  1413. hsai->Instance->DR = (*hsai->pBuffPtr++);
  1414. hsai->XferCount--;
  1415. /* Handle the end of the transmission */
  1416. if(hsai->XferCount == 0)
  1417. {
  1418. /* Disable FREQ and OVRUDR interrupts */
  1419. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1420. hsai->State = HAL_SAI_STATE_READY;
  1421. HAL_SAI_TxCpltCallback(hsai);
  1422. }
  1423. }
  1424. /**
  1425. * @brief Tx Handler for Transmit in Interrupt mode for 16Bit transfer
  1426. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1427. * the configuration information for SAI module.
  1428. * @retval None.
  1429. */
  1430. static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai)
  1431. {
  1432. /* Write data on DR register */
  1433. hsai->Instance->DR = *(uint16_t *)hsai->pBuffPtr;
  1434. hsai->pBuffPtr+=2;
  1435. hsai->XferCount--;
  1436. /* Handle the end of the transmission */
  1437. if(hsai->XferCount == 0)
  1438. {
  1439. /* Disable FREQ and OVRUDR interrupts */
  1440. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1441. hsai->State = HAL_SAI_STATE_READY;
  1442. HAL_SAI_TxCpltCallback(hsai);
  1443. }
  1444. }
  1445. /**
  1446. * @brief Tx Handler for Transmit in Interrupt mode for 32Bit transfer
  1447. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1448. * the configuration information for SAI module.
  1449. * @retval None.
  1450. */
  1451. static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai)
  1452. {
  1453. /* Write data on DR register */
  1454. hsai->Instance->DR = *(uint32_t *)hsai->pBuffPtr;
  1455. hsai->pBuffPtr+=4;
  1456. hsai->XferCount--;
  1457. /* Handle the end of the transmission */
  1458. if(hsai->XferCount == 0)
  1459. {
  1460. /* Disable FREQ and OVRUDR interrupts */
  1461. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1462. hsai->State = HAL_SAI_STATE_READY;
  1463. HAL_SAI_TxCpltCallback(hsai);
  1464. }
  1465. }
  1466. /**
  1467. * @brief Rx Handler for Receive in Interrupt mode 8Bit transfer
  1468. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1469. * the configuration information for SAI module.
  1470. * @retval None.
  1471. */
  1472. static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai)
  1473. {
  1474. /* Receive data */
  1475. (*hsai->pBuffPtr++) = hsai->Instance->DR;
  1476. hsai->XferCount--;
  1477. /* Check end of the transfer */
  1478. if(hsai->XferCount == 0)
  1479. {
  1480. /* Disable TXE and OVRUDR interrupts */
  1481. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1482. /* Clear the SAI Overrun flag */
  1483. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  1484. hsai->State = HAL_SAI_STATE_READY;
  1485. HAL_SAI_RxCpltCallback(hsai);
  1486. }
  1487. }
  1488. /**
  1489. * @brief Rx Handler for Receive in Interrupt mode for 16Bit transfer
  1490. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1491. * the configuration information for SAI module.
  1492. * @retval None.
  1493. */
  1494. static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai)
  1495. {
  1496. /* Receive data */
  1497. *(uint16_t*)hsai->pBuffPtr = hsai->Instance->DR;
  1498. hsai->pBuffPtr+=2;
  1499. hsai->XferCount--;
  1500. /* Check end of the transfer */
  1501. if(hsai->XferCount == 0)
  1502. {
  1503. /* Disable TXE and OVRUDR interrupts */
  1504. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1505. /* Clear the SAI Overrun flag */
  1506. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  1507. hsai->State = HAL_SAI_STATE_READY;
  1508. HAL_SAI_RxCpltCallback(hsai);
  1509. }
  1510. }
  1511. /**
  1512. * @brief Rx Handler for Receive in Interrupt mode for 32Bit transfer
  1513. * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
  1514. * the configuration information for SAI module.
  1515. * @retval None.
  1516. */
  1517. static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai)
  1518. {
  1519. /* Receive data */
  1520. *(uint32_t*)hsai->pBuffPtr = hsai->Instance->DR;
  1521. hsai->pBuffPtr+=4;
  1522. hsai->XferCount--;
  1523. /* Check end of the transfer */
  1524. if(hsai->XferCount == 0)
  1525. {
  1526. /* Disable TXE and OVRUDR interrupts */
  1527. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1528. /* Clear the SAI Overrun flag */
  1529. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  1530. hsai->State = HAL_SAI_STATE_READY;
  1531. HAL_SAI_RxCpltCallback(hsai);
  1532. }
  1533. }
  1534. /**
  1535. * @brief DMA SAI transmit process complete callback.
  1536. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1537. * the configuration information for the specified DMA module.
  1538. * @retval None
  1539. */
  1540. static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma)
  1541. {
  1542. uint32_t tickstart = 0;
  1543. SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef* )hdma)->Parent;
  1544. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1545. {
  1546. hsai->XferCount = 0;
  1547. /* Disable SAI Tx DMA Request */
  1548. hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
  1549. /* Get tick */
  1550. tickstart = HAL_GetTick();
  1551. /* Set timeout: 10 is the max delay to send the remaining data in the SAI FIFO */
  1552. /* Wait until FIFO is empty */
  1553. while(__HAL_SAI_GET_FLAG(hsai, SAI_xSR_FLVL) != RESET)
  1554. {
  1555. /* Check for the Timeout */
  1556. if((HAL_GetTick() - tickstart ) > SAI_TIMEOUT_VALUE)
  1557. {
  1558. /* Update error code */
  1559. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  1560. /* Change the SAI state */
  1561. HAL_SAI_ErrorCallback(hsai);
  1562. }
  1563. }
  1564. /* Stop the interrupts error handling */
  1565. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  1566. hsai->State= HAL_SAI_STATE_READY;
  1567. }
  1568. HAL_SAI_TxCpltCallback(hsai);
  1569. }
  1570. /**
  1571. * @brief DMA SAI transmit process half complete callback
  1572. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1573. * the configuration information for the specified DMA module.
  1574. * @retval None
  1575. */
  1576. static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1577. {
  1578. SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1579. HAL_SAI_TxHalfCpltCallback(hsai);
  1580. }
  1581. /**
  1582. * @brief DMA SAI receive process complete callback.
  1583. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1584. * the configuration information for the specified DMA module.
  1585. * @retval None
  1586. */
  1587. static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma)
  1588. {
  1589. SAI_HandleTypeDef* hsai = ( SAI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1590. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1591. {
  1592. /* Disable Rx DMA Request */
  1593. hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
  1594. hsai->XferCount = 0;
  1595. /* Stop the interrupts error handling */
  1596. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  1597. hsai->State = HAL_SAI_STATE_READY;
  1598. }
  1599. HAL_SAI_RxCpltCallback(hsai);
  1600. }
  1601. /**
  1602. * @brief DMA SAI receive process half complete callback
  1603. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1604. * the configuration information for the specified DMA module.
  1605. * @retval None
  1606. */
  1607. static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1608. {
  1609. SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1610. HAL_SAI_RxHalfCpltCallback(hsai);
  1611. }
  1612. /**
  1613. * @brief DMA SAI communication error callback.
  1614. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1615. * the configuration information for the specified DMA module.
  1616. * @retval None
  1617. */
  1618. static void SAI_DMAError(DMA_HandleTypeDef *hdma)
  1619. {
  1620. SAI_HandleTypeDef* hsai = ( SAI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1621. /* Stop the DMA transfer */
  1622. HAL_SAI_DMAStop(hsai);
  1623. /* Set the SAI state ready to be able to start again the process */
  1624. hsai->State= HAL_SAI_STATE_READY;
  1625. HAL_SAI_ErrorCallback(hsai);
  1626. hsai->XferCount = 0;
  1627. }
  1628. /**
  1629. * @}
  1630. */
  1631. #endif /* HAL_SAI_MODULE_ENABLED */
  1632. /**
  1633. * @}
  1634. */
  1635. /**
  1636. * @}
  1637. */
  1638. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/