stm32f7xx_hal_sai.c 68 KB

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