stm32f4xx_hal_sai.c 71 KB

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