stm32l4xx_hal_sai.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The SAI HAL driver can be used as follows:
  30. (#) Declare a SAI_HandleTypeDef handle structure (eg. SAI_HandleTypeDef hsai).
  31. (#) Initialize the SAI low level resources by implementing the HAL_SAI_MspInit() API:
  32. (##) Enable the SAI interface clock.
  33. (##) SAI pins configuration:
  34. (+++) Enable the clock for the SAI GPIOs.
  35. (+++) Configure these SAI pins as alternate function pull-up.
  36. (##) NVIC configuration if you need to use interrupt process (HAL_SAI_Transmit_IT()
  37. and HAL_SAI_Receive_IT() APIs):
  38. (+++) Configure the SAI interrupt priority.
  39. (+++) Enable the NVIC SAI IRQ handle.
  40. (##) DMA Configuration if you need to use DMA process (HAL_SAI_Transmit_DMA()
  41. and HAL_SAI_Receive_DMA() APIs):
  42. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  43. (+++) Enable the DMAx interface clock.
  44. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  45. (+++) Configure the DMA Tx/Rx Stream.
  46. (+++) Associate the initialized DMA handle to the SAI DMA Tx/Rx handle.
  47. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
  48. DMA Tx/Rx Stream.
  49. (#) The initialization can be done by two ways
  50. (##) Expert mode : Initialize the structures Init, FrameInit and SlotInit and call HAL_SAI_Init().
  51. (##) Simplified mode : Initialize the high part of Init Structure and call HAL_SAI_InitProtocol().
  52. [..]
  53. (@) The specific SAI interrupts (FIFO request and Overrun underrun interrupt)
  54. will be managed using the macros __HAL_SAI_ENABLE_IT() and __HAL_SAI_DISABLE_IT()
  55. inside the transmit and receive process.
  56. [..]
  57. (@) Make sure that either:
  58. (+@) PLLSAI1CLK output is configured or
  59. (+@) PLLSAI2CLK output is configured or
  60. (+@) PLLSAI3CLK output is configured or
  61. (+@) External clock source is configured after setting correctly
  62. the define constant EXTERNAL_SAI1_CLOCK_VALUE or EXTERNAL_SAI2_CLOCK_VALUE in the stm32l4xx_hal_conf.h file.
  63. [..]
  64. (@) In master Tx mode: enabling the audio block immediately generates the bit clock
  65. for the external slaves even if there is no data in the FIFO, However FS signal
  66. generation is conditioned by the presence of data in the FIFO.
  67. [..]
  68. (@) In master Rx mode: enabling the audio block immediately generates the bit clock
  69. and FS signal for the external slaves.
  70. [..]
  71. (@) It is mandatory to respect the following conditions in order to avoid bad SAI behavior:
  72. (+@) First bit Offset <= (SLOT size - Data size)
  73. (+@) Data size <= SLOT size
  74. (+@) Number of SLOT x SLOT size = Frame length
  75. (+@) The number of slots should be even when SAI_FS_CHANNEL_IDENTIFICATION is selected.
  76. [..]
  77. (@) For STM32L4Rx/STM32L4Sx devices, PDM interface can be activated through HAL_SAI_Init function.
  78. Please note that PDM interface is only available for SAI1 sub-block A.
  79. PDM microphone delays can be tuned with HAL_SAIEx_ConfigPdmMicDelay function.
  80. [..]
  81. Three operation modes are available within this driver :
  82. *** Polling mode IO operation ***
  83. =================================
  84. [..]
  85. (+) Send an amount of data in blocking mode using HAL_SAI_Transmit()
  86. (+) Receive an amount of data in blocking mode using HAL_SAI_Receive()
  87. *** Interrupt mode IO operation ***
  88. ===================================
  89. [..]
  90. (+) Send an amount of data in non-blocking mode using HAL_SAI_Transmit_IT()
  91. (+) At transmission end of transfer HAL_SAI_TxCpltCallback() is executed and user can
  92. add his own code by customization of function pointer HAL_SAI_TxCpltCallback()
  93. (+) Receive an amount of data in non-blocking mode using HAL_SAI_Receive_IT()
  94. (+) At reception end of transfer HAL_SAI_RxCpltCallback() is executed and user can
  95. add his own code by customization of function pointer HAL_SAI_RxCpltCallback()
  96. (+) In case of flag error, HAL_SAI_ErrorCallback() function is executed and user can
  97. add his own code by customization of function pointer HAL_SAI_ErrorCallback()
  98. *** DMA mode IO operation ***
  99. =============================
  100. [..]
  101. (+) Send an amount of data in non-blocking mode (DMA) using HAL_SAI_Transmit_DMA()
  102. (+) At transmission end of transfer HAL_SAI_TxCpltCallback() is executed and user can
  103. add his own code by customization of function pointer HAL_SAI_TxCpltCallback()
  104. (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SAI_Receive_DMA()
  105. (+) At reception end of transfer HAL_SAI_RxCpltCallback() is executed and user can
  106. add his own code by customization of function pointer HAL_SAI_RxCpltCallback()
  107. (+) In case of flag error, HAL_SAI_ErrorCallback() function is executed and user can
  108. add his own code by customization of function pointer HAL_SAI_ErrorCallback()
  109. (+) Pause the DMA Transfer using HAL_SAI_DMAPause()
  110. (+) Resume the DMA Transfer using HAL_SAI_DMAResume()
  111. (+) Stop the DMA Transfer using HAL_SAI_DMAStop()
  112. *** SAI HAL driver additional function list ***
  113. ===============================================
  114. [..]
  115. Below the list the others API available SAI HAL driver :
  116. (+) HAL_SAI_EnableTxMuteMode(): Enable the mute in tx mode
  117. (+) HAL_SAI_DisableTxMuteMode(): Disable the mute in tx mode
  118. (+) HAL_SAI_EnableRxMuteMode(): Enable the mute in Rx mode
  119. (+) HAL_SAI_DisableRxMuteMode(): Disable the mute in Rx mode
  120. (+) HAL_SAI_FlushRxFifo(): Flush the rx fifo.
  121. (+) HAL_SAI_Abort(): Abort the current transfer
  122. *** SAI HAL driver macros list ***
  123. ==================================
  124. [..]
  125. Below the list of most used macros in SAI HAL driver :
  126. (+) __HAL_SAI_ENABLE(): Enable the SAI peripheral
  127. (+) __HAL_SAI_DISABLE(): Disable the SAI peripheral
  128. (+) __HAL_SAI_ENABLE_IT(): Enable the specified SAI interrupts
  129. (+) __HAL_SAI_DISABLE_IT(): Disable the specified SAI interrupts
  130. (+) __HAL_SAI_GET_IT_SOURCE(): Check if the specified SAI interrupt source is
  131. enabled or disabled
  132. (+) __HAL_SAI_GET_FLAG(): Check whether the specified SAI flag is set or not
  133. *** Callback registration ***
  134. =============================
  135. [..]
  136. The compilation define USE_HAL_SAI_REGISTER_CALLBACKS when set to 1
  137. allows the user to configure dynamically the driver callbacks.
  138. Use functions HAL_SAI_RegisterCallback() to register a user callback.
  139. [..]
  140. Function HAL_SAI_RegisterCallback() allows to register following callbacks:
  141. (+) RxCpltCallback : SAI receive complete.
  142. (+) RxHalfCpltCallback : SAI receive half complete.
  143. (+) TxCpltCallback : SAI transmit complete.
  144. (+) TxHalfCpltCallback : SAI transmit half complete.
  145. (+) ErrorCallback : SAI error.
  146. (+) MspInitCallback : SAI MspInit.
  147. (+) MspDeInitCallback : SAI MspDeInit.
  148. [..]
  149. This function takes as parameters the HAL peripheral handle, the callback ID
  150. and a pointer to the user callback function.
  151. [..]
  152. Use function HAL_SAI_UnRegisterCallback() to reset a callback to the default
  153. weak (surcharged) function.
  154. HAL_SAI_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  155. and the callback ID.
  156. [..]
  157. This function allows to reset following callbacks:
  158. (+) RxCpltCallback : SAI receive complete.
  159. (+) RxHalfCpltCallback : SAI receive half complete.
  160. (+) TxCpltCallback : SAI transmit complete.
  161. (+) TxHalfCpltCallback : SAI transmit half complete.
  162. (+) ErrorCallback : SAI error.
  163. (+) MspInitCallback : SAI MspInit.
  164. (+) MspDeInitCallback : SAI MspDeInit.
  165. [..]
  166. By default, after the HAL_SAI_Init and if the state is HAL_SAI_STATE_RESET
  167. all callbacks are reset to the corresponding legacy weak (surcharged) functions:
  168. examples HAL_SAI_RxCpltCallback(), HAL_SAI_ErrorCallback().
  169. Exception done for MspInit and MspDeInit callbacks that are respectively
  170. reset to the legacy weak (surcharged) functions in the HAL_SAI_Init
  171. and HAL_SAI_DeInit only when these callbacks are null (not registered beforehand).
  172. If not, MspInit or MspDeInit are not null, the HAL_SAI_Init and HAL_SAI_DeInit
  173. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  174. [..]
  175. Callbacks can be registered/unregistered in READY state only.
  176. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  177. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  178. during the Init/DeInit.
  179. In that case first register the MspInit/MspDeInit user callbacks
  180. using HAL_SAI_RegisterCallback before calling HAL_SAI_DeInit
  181. or HAL_SAI_Init function.
  182. [..]
  183. When the compilation define USE_HAL_SAI_REGISTER_CALLBACKS is set to 0 or
  184. not defined, the callback registering feature is not available
  185. and weak (surcharged) callbacks are used.
  186. @endverbatim
  187. */
  188. /* Includes ------------------------------------------------------------------*/
  189. #include "stm32l4xx_hal.h"
  190. /** @addtogroup STM32L4xx_HAL_Driver
  191. * @{
  192. */
  193. #ifdef HAL_SAI_MODULE_ENABLED
  194. #if !defined(STM32L412xx) && !defined(STM32L422xx)
  195. /** @defgroup SAI SAI
  196. * @brief SAI HAL module driver
  197. * @{
  198. */
  199. /* Private typedef -----------------------------------------------------------*/
  200. /** @defgroup SAI_Private_Typedefs SAI Private Typedefs
  201. * @{
  202. */
  203. typedef enum
  204. {
  205. SAI_MODE_DMA,
  206. SAI_MODE_IT
  207. } SAI_ModeTypedef;
  208. /**
  209. * @}
  210. */
  211. /* Private define ------------------------------------------------------------*/
  212. /** @defgroup SAI_Private_Constants SAI Private Constants
  213. * @{
  214. */
  215. #define SAI_DEFAULT_TIMEOUT 4U
  216. #define SAI_LONG_TIMEOUT 1000U
  217. /**
  218. * @}
  219. */
  220. /* Private macro -------------------------------------------------------------*/
  221. /* Private variables ---------------------------------------------------------*/
  222. /* Private function prototypes -----------------------------------------------*/
  223. /** @defgroup SAI_Private_Functions SAI Private Functions
  224. * @{
  225. */
  226. static void SAI_FillFifo(SAI_HandleTypeDef *hsai);
  227. static uint32_t SAI_InterruptFlag(const SAI_HandleTypeDef *hsai, SAI_ModeTypedef mode);
  228. static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
  229. static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
  230. static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai);
  231. static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai);
  232. static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai);
  233. static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai);
  234. static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai);
  235. static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai);
  236. static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai);
  237. static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma);
  238. static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  239. static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma);
  240. static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  241. static void SAI_DMAError(DMA_HandleTypeDef *hdma);
  242. static void SAI_DMAAbort(DMA_HandleTypeDef *hdma);
  243. /**
  244. * @}
  245. */
  246. /* Exported functions ---------------------------------------------------------*/
  247. /** @defgroup SAI_Exported_Functions SAI Exported Functions
  248. * @{
  249. */
  250. /** @defgroup SAI_Exported_Functions_Group1 Initialization and de-initialization functions
  251. * @brief Initialization and Configuration functions
  252. *
  253. @verbatim
  254. ===============================================================================
  255. ##### Initialization and de-initialization functions #####
  256. ===============================================================================
  257. [..] This subsection provides a set of functions allowing to initialize and
  258. de-initialize the SAIx peripheral:
  259. (+) User must implement HAL_SAI_MspInit() function in which he configures
  260. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  261. (+) Call the function HAL_SAI_Init() to configure the selected device with
  262. the selected configuration:
  263. (++) Mode (Master/slave TX/RX)
  264. (++) Protocol
  265. (++) Data Size
  266. (++) MCLK Output
  267. (++) Audio frequency
  268. (++) FIFO Threshold
  269. (++) Frame Config
  270. (++) Slot Config
  271. (++) PDM Config (only for STM32L4Rx/STM32L4Sx devices)
  272. (+) Call the function HAL_SAI_DeInit() to restore the default configuration
  273. of the selected SAI peripheral.
  274. @endverbatim
  275. * @{
  276. */
  277. /**
  278. * @brief Initialize the structure FrameInit, SlotInit and the low part of
  279. * Init according to the specified parameters and call the function
  280. * HAL_SAI_Init to initialize the SAI block.
  281. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  282. * the configuration information for SAI module.
  283. * @param protocol one of the supported protocol @ref SAI_Protocol
  284. * @param datasize one of the supported datasize @ref SAI_Protocol_DataSize
  285. * the configuration information for SAI module.
  286. * @param nbslot Number of slot.
  287. * @retval HAL status
  288. */
  289. HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  290. {
  291. HAL_StatusTypeDef status;
  292. /* Check the parameters */
  293. assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
  294. assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
  295. switch (protocol)
  296. {
  297. case SAI_I2S_STANDARD :
  298. case SAI_I2S_MSBJUSTIFIED :
  299. case SAI_I2S_LSBJUSTIFIED :
  300. status = SAI_InitI2S(hsai, protocol, datasize, nbslot);
  301. break;
  302. case SAI_PCM_LONG :
  303. case SAI_PCM_SHORT :
  304. status = SAI_InitPCM(hsai, protocol, datasize, nbslot);
  305. break;
  306. default :
  307. status = HAL_ERROR;
  308. break;
  309. }
  310. if (status == HAL_OK)
  311. {
  312. status = HAL_SAI_Init(hsai);
  313. }
  314. return status;
  315. }
  316. /**
  317. * @brief Initialize the SAI according to the specified parameters.
  318. * in the SAI_InitTypeDef structure and initialize the associated handle.
  319. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  320. * the configuration information for SAI module.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai)
  324. {
  325. #if defined(SAI2)
  326. uint32_t tmpregisterGCR;
  327. #endif /* SAI2 */
  328. uint32_t ckstr_bits;
  329. uint32_t syncen_bits;
  330. /* Check the SAI handle allocation */
  331. if (hsai == NULL)
  332. {
  333. return HAL_ERROR;
  334. }
  335. /* check the instance */
  336. assert_param(IS_SAI_ALL_INSTANCE(hsai->Instance));
  337. /* Check the SAI Block parameters */
  338. assert_param(IS_SAI_AUDIO_FREQUENCY(hsai->Init.AudioFrequency));
  339. assert_param(IS_SAI_BLOCK_PROTOCOL(hsai->Init.Protocol));
  340. assert_param(IS_SAI_BLOCK_MODE(hsai->Init.AudioMode));
  341. assert_param(IS_SAI_BLOCK_DATASIZE(hsai->Init.DataSize));
  342. assert_param(IS_SAI_BLOCK_FIRST_BIT(hsai->Init.FirstBit));
  343. assert_param(IS_SAI_BLOCK_CLOCK_STROBING(hsai->Init.ClockStrobing));
  344. assert_param(IS_SAI_BLOCK_SYNCHRO(hsai->Init.Synchro));
  345. assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(hsai->Init.OutputDrive));
  346. assert_param(IS_SAI_BLOCK_NODIVIDER(hsai->Init.NoDivider));
  347. assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(hsai->Init.FIFOThreshold));
  348. assert_param(IS_SAI_MONO_STEREO_MODE(hsai->Init.MonoStereoMode));
  349. assert_param(IS_SAI_BLOCK_COMPANDING_MODE(hsai->Init.CompandingMode));
  350. assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(hsai->Init.TriState));
  351. assert_param(IS_SAI_BLOCK_SYNCEXT(hsai->Init.SynchroExt));
  352. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  353. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  354. assert_param(IS_SAI_BLOCK_MCK_OVERSAMPLING(hsai->Init.MckOverSampling));
  355. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  356. /* STM32L4P5xx || STM32L4Q5xx */
  357. /* Check the SAI Block Frame parameters */
  358. assert_param(IS_SAI_BLOCK_FRAME_LENGTH(hsai->FrameInit.FrameLength));
  359. assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(hsai->FrameInit.ActiveFrameLength));
  360. assert_param(IS_SAI_BLOCK_FS_DEFINITION(hsai->FrameInit.FSDefinition));
  361. assert_param(IS_SAI_BLOCK_FS_POLARITY(hsai->FrameInit.FSPolarity));
  362. assert_param(IS_SAI_BLOCK_FS_OFFSET(hsai->FrameInit.FSOffset));
  363. /* Check the SAI Block Slot parameters */
  364. assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(hsai->SlotInit.FirstBitOffset));
  365. assert_param(IS_SAI_BLOCK_SLOT_SIZE(hsai->SlotInit.SlotSize));
  366. assert_param(IS_SAI_BLOCK_SLOT_NUMBER(hsai->SlotInit.SlotNumber));
  367. assert_param(IS_SAI_SLOT_ACTIVE(hsai->SlotInit.SlotActive));
  368. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  369. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  370. /* Check the SAI PDM parameters */
  371. assert_param(IS_FUNCTIONAL_STATE(hsai->Init.PdmInit.Activation));
  372. if (hsai->Init.PdmInit.Activation == ENABLE)
  373. {
  374. assert_param(IS_SAI_PDM_MIC_PAIRS_NUMBER(hsai->Init.PdmInit.MicPairsNbr));
  375. assert_param(IS_SAI_PDM_CLOCK_ENABLE(hsai->Init.PdmInit.ClockEnable));
  376. /* Check that SAI sub-block is SAI1 sub-block A, in master RX mode with free protocol */
  377. if ((hsai->Instance != SAI1_Block_A) ||
  378. (hsai->Init.AudioMode != SAI_MODEMASTER_RX) ||
  379. (hsai->Init.Protocol != SAI_FREE_PROTOCOL))
  380. {
  381. return HAL_ERROR;
  382. }
  383. }
  384. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  385. /* STM32L4P5xx || STM32L4Q5xx */
  386. if (hsai->State == HAL_SAI_STATE_RESET)
  387. {
  388. /* Allocate lock resource and initialize it */
  389. hsai->Lock = HAL_UNLOCKED;
  390. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  391. /* Reset callback pointers to the weak predefined callbacks */
  392. hsai->RxCpltCallback = HAL_SAI_RxCpltCallback;
  393. hsai->RxHalfCpltCallback = HAL_SAI_RxHalfCpltCallback;
  394. hsai->TxCpltCallback = HAL_SAI_TxCpltCallback;
  395. hsai->TxHalfCpltCallback = HAL_SAI_TxHalfCpltCallback;
  396. hsai->ErrorCallback = HAL_SAI_ErrorCallback;
  397. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  398. if (hsai->MspInitCallback == NULL)
  399. {
  400. hsai->MspInitCallback = HAL_SAI_MspInit;
  401. }
  402. hsai->MspInitCallback(hsai);
  403. #else
  404. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  405. HAL_SAI_MspInit(hsai);
  406. #endif
  407. }
  408. /* Disable the selected SAI peripheral */
  409. if (SAI_Disable(hsai) != HAL_OK)
  410. {
  411. return HAL_ERROR;
  412. }
  413. hsai->State = HAL_SAI_STATE_BUSY;
  414. /* SAI Block Synchro Configuration -----------------------------------------*/
  415. /* This setting must be done with both audio block (A & B) disabled */
  416. #if defined(SAI2)
  417. switch (hsai->Init.SynchroExt)
  418. {
  419. case SAI_SYNCEXT_DISABLE :
  420. tmpregisterGCR = 0;
  421. break;
  422. case SAI_SYNCEXT_OUTBLOCKA_ENABLE :
  423. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  424. break;
  425. case SAI_SYNCEXT_OUTBLOCKB_ENABLE :
  426. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  427. break;
  428. default :
  429. tmpregisterGCR = 0;
  430. break;
  431. }
  432. #endif /* SAI2 */
  433. switch (hsai->Init.Synchro)
  434. {
  435. case SAI_ASYNCHRONOUS :
  436. syncen_bits = 0;
  437. break;
  438. case SAI_SYNCHRONOUS :
  439. syncen_bits = SAI_xCR1_SYNCEN_0;
  440. break;
  441. #if defined(SAI2)
  442. case SAI_SYNCHRONOUS_EXT_SAI1 :
  443. syncen_bits = SAI_xCR1_SYNCEN_1;
  444. break;
  445. case SAI_SYNCHRONOUS_EXT_SAI2 :
  446. syncen_bits = SAI_xCR1_SYNCEN_1;
  447. tmpregisterGCR |= SAI_GCR_SYNCIN_0;
  448. break;
  449. #endif /* SAI2 */
  450. default :
  451. syncen_bits = 0;
  452. break;
  453. }
  454. #if defined(SAI2)
  455. if ((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  456. {
  457. SAI1->GCR = tmpregisterGCR;
  458. }
  459. else
  460. {
  461. SAI2->GCR = tmpregisterGCR;
  462. }
  463. #else
  464. SAI1->GCR = 0;
  465. #endif /* SAI2 */
  466. if (hsai->Init.AudioFrequency != SAI_AUDIO_FREQUENCY_MCKDIV)
  467. {
  468. uint32_t freq;
  469. uint32_t tmpval;
  470. /* In this case, the MCKDIV value is calculated to get AudioFrequency */
  471. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  472. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  473. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  474. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  475. if ((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  476. {
  477. freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI1);
  478. }
  479. else
  480. {
  481. /* SAI2_Block_A or SAI2_Block_B */
  482. freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI2);
  483. }
  484. #else
  485. freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI1);
  486. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  487. /* STM32L496xx || STM32L4A6xx || */
  488. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  489. /* STM32L4P5xx || STM32L4Q5xx */
  490. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  491. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  492. /* Configure Master Clock Divider using the following formula :
  493. - If NOMCK = 1 :
  494. MCKDIV[5:0] = SAI_CK_x / (FS * (FRL + 1))
  495. - If NOMCK = 0 :
  496. MCKDIV[5:0] = SAI_CK_x / (FS * (OSR + 1) * 256) */
  497. if (hsai->Init.NoDivider == SAI_MASTERDIVIDER_DISABLE)
  498. {
  499. /* NOMCK = 1 */
  500. uint32_t tmpframelength;
  501. if (hsai->Init.Protocol == SAI_SPDIF_PROTOCOL)
  502. {
  503. /* For SPDIF protocol, frame length is set by hardware to 64 */
  504. tmpframelength = 64U;
  505. }
  506. else if (hsai->Init.Protocol == SAI_AC97_PROTOCOL)
  507. {
  508. /* For AC97 protocol, frame length is set by hardware to 256 */
  509. tmpframelength = 256U;
  510. }
  511. else
  512. {
  513. /* For free protocol, frame length is set by user */
  514. tmpframelength = hsai->FrameInit.FrameLength;
  515. }
  516. /* (freq x 10) to keep Significant digits */
  517. tmpval = (freq * 10U) / (hsai->Init.AudioFrequency * tmpframelength);
  518. }
  519. else
  520. {
  521. /* NOMCK = 0 */
  522. uint32_t tmposr;
  523. tmposr = (hsai->Init.MckOverSampling == SAI_MCK_OVERSAMPLING_ENABLE) ? 2U : 1U;
  524. /* (freq x 10) to keep Significant digits */
  525. tmpval = (freq * 10U) / (hsai->Init.AudioFrequency * tmposr * 256U);
  526. }
  527. hsai->Init.Mckdiv = tmpval / 10U;
  528. /* Round result to the nearest integer */
  529. if ((tmpval % 10U) > 8U)
  530. {
  531. hsai->Init.Mckdiv += 1U;
  532. }
  533. #else
  534. /* Configure Master Clock using the following formula :
  535. MCLK_x = SAI_CK_x / (MCKDIV[3:0] * 2) with MCLK_x = 256 * FS
  536. FS = SAI_CK_x / (MCKDIV[3:0] * 2) * 256
  537. MCKDIV[3:0] = SAI_CK_x / FS * 512 */
  538. /* (freq x 10) to keep Significant digits */
  539. tmpval = (freq * 10U) / (hsai->Init.AudioFrequency * 2U * 256U);
  540. hsai->Init.Mckdiv = tmpval / 10U;
  541. /* Round result to the nearest integer */
  542. if ((tmpval % 10U) > 8U)
  543. {
  544. hsai->Init.Mckdiv += 1U;
  545. }
  546. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  547. /* STM32L4P5xx || STM32L4Q5xx */
  548. /* For SPDIF protocol, SAI shall provide a bit clock twice faster the symbol-rate */
  549. if (hsai->Init.Protocol == SAI_SPDIF_PROTOCOL)
  550. {
  551. hsai->Init.Mckdiv = hsai->Init.Mckdiv >> 1;
  552. }
  553. }
  554. /* Check the SAI Block master clock divider parameter */
  555. assert_param(IS_SAI_BLOCK_MASTER_DIVIDER(hsai->Init.Mckdiv));
  556. /* Compute CKSTR bits of SAI CR1 according ClockStrobing and AudioMode */
  557. if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
  558. {
  559. /* Transmit */
  560. ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? 0U : SAI_xCR1_CKSTR;
  561. }
  562. else
  563. {
  564. /* Receive */
  565. ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? SAI_xCR1_CKSTR : 0U;
  566. }
  567. /* SAI Block Configuration -------------------------------------------------*/
  568. /* SAI CR1 Configuration */
  569. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  570. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  571. hsai->Instance->CR1 &= ~(SAI_xCR1_MODE | SAI_xCR1_PRTCFG | SAI_xCR1_DS | \
  572. SAI_xCR1_LSBFIRST | SAI_xCR1_CKSTR | SAI_xCR1_SYNCEN | \
  573. SAI_xCR1_MONO | SAI_xCR1_OUTDRIV | SAI_xCR1_DMAEN | \
  574. SAI_xCR1_NOMCK | SAI_xCR1_MCKDIV | SAI_xCR1_OSR);
  575. hsai->Instance->CR1 |= (hsai->Init.AudioMode | hsai->Init.Protocol | \
  576. hsai->Init.DataSize | hsai->Init.FirstBit | \
  577. ckstr_bits | syncen_bits | \
  578. hsai->Init.MonoStereoMode | hsai->Init.OutputDrive | \
  579. hsai->Init.NoDivider | (hsai->Init.Mckdiv << 20) | \
  580. hsai->Init.MckOverSampling);
  581. #else
  582. hsai->Instance->CR1 &= ~(SAI_xCR1_MODE | SAI_xCR1_PRTCFG | SAI_xCR1_DS | \
  583. SAI_xCR1_LSBFIRST | SAI_xCR1_CKSTR | SAI_xCR1_SYNCEN | \
  584. SAI_xCR1_MONO | SAI_xCR1_OUTDRIV | SAI_xCR1_DMAEN | \
  585. SAI_xCR1_NODIV | SAI_xCR1_MCKDIV);
  586. hsai->Instance->CR1 |= (hsai->Init.AudioMode | hsai->Init.Protocol | \
  587. hsai->Init.DataSize | hsai->Init.FirstBit | \
  588. ckstr_bits | syncen_bits | \
  589. hsai->Init.MonoStereoMode | hsai->Init.OutputDrive | \
  590. hsai->Init.NoDivider | (hsai->Init.Mckdiv << 20));
  591. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  592. /* STM32L4P5xx || STM32L4Q5xx */
  593. /* SAI CR2 Configuration */
  594. hsai->Instance->CR2 &= ~(SAI_xCR2_FTH | SAI_xCR2_FFLUSH | SAI_xCR2_COMP | SAI_xCR2_CPL);
  595. hsai->Instance->CR2 |= (hsai->Init.FIFOThreshold | hsai->Init.CompandingMode | hsai->Init.TriState);
  596. /* SAI Frame Configuration -----------------------------------------*/
  597. hsai->Instance->FRCR &= (~(SAI_xFRCR_FRL | SAI_xFRCR_FSALL | SAI_xFRCR_FSDEF | \
  598. SAI_xFRCR_FSPOL | SAI_xFRCR_FSOFF));
  599. hsai->Instance->FRCR |= ((hsai->FrameInit.FrameLength - 1U) |
  600. hsai->FrameInit.FSOffset |
  601. hsai->FrameInit.FSDefinition |
  602. hsai->FrameInit.FSPolarity |
  603. ((hsai->FrameInit.ActiveFrameLength - 1U) << 8));
  604. /* SAI Block_x SLOT Configuration ------------------------------------------*/
  605. /* This register has no meaning in AC 97 and SPDIF audio protocol */
  606. hsai->Instance->SLOTR &= (~(SAI_xSLOTR_FBOFF | SAI_xSLOTR_SLOTSZ | \
  607. SAI_xSLOTR_NBSLOT | SAI_xSLOTR_SLOTEN));
  608. hsai->Instance->SLOTR |= hsai->SlotInit.FirstBitOffset | hsai->SlotInit.SlotSize | \
  609. (hsai->SlotInit.SlotActive << 16) | ((hsai->SlotInit.SlotNumber - 1U) << 8);
  610. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  611. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  612. /* SAI PDM Configuration ---------------------------------------------------*/
  613. if (hsai->Instance == SAI1_Block_A)
  614. {
  615. /* Disable PDM interface */
  616. SAI1->PDMCR &= ~(SAI_PDMCR_PDMEN);
  617. if (hsai->Init.PdmInit.Activation == ENABLE)
  618. {
  619. /* Configure and enable PDM interface */
  620. SAI1->PDMCR = (hsai->Init.PdmInit.ClockEnable |
  621. ((hsai->Init.PdmInit.MicPairsNbr - 1U) << SAI_PDMCR_MICNBR_Pos));
  622. SAI1->PDMCR |= SAI_PDMCR_PDMEN;
  623. }
  624. }
  625. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  626. /* STM32L4P5xx || STM32L4Q5xx */
  627. /* Initialize the error code */
  628. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  629. /* Initialize the SAI state */
  630. hsai->State = HAL_SAI_STATE_READY;
  631. /* Release Lock */
  632. __HAL_UNLOCK(hsai);
  633. return HAL_OK;
  634. }
  635. /**
  636. * @brief DeInitialize the SAI peripheral.
  637. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  638. * the configuration information for SAI module.
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_SAI_DeInit(SAI_HandleTypeDef *hsai)
  642. {
  643. /* Check the SAI handle allocation */
  644. if (hsai == NULL)
  645. {
  646. return HAL_ERROR;
  647. }
  648. hsai->State = HAL_SAI_STATE_BUSY;
  649. /* Disabled All interrupt and clear all the flag */
  650. hsai->Instance->IMR = 0;
  651. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  652. /* Disable the SAI */
  653. if (SAI_Disable(hsai) != HAL_OK)
  654. {
  655. /* Reset SAI state to ready */
  656. hsai->State = HAL_SAI_STATE_READY;
  657. /* Release Lock */
  658. __HAL_UNLOCK(hsai);
  659. return HAL_ERROR;
  660. }
  661. /* Flush the fifo */
  662. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  663. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  664. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  665. /* Disable SAI PDM interface */
  666. if (hsai->Instance == SAI1_Block_A)
  667. {
  668. /* Reset PDM delays */
  669. SAI1->PDMDLY = 0U;
  670. /* Disable PDM interface */
  671. SAI1->PDMCR &= ~(SAI_PDMCR_PDMEN);
  672. }
  673. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || */
  674. /* STM32L4P5xx || STM32L4Q5xx */
  675. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  676. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  677. if (hsai->MspDeInitCallback == NULL)
  678. {
  679. hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
  680. }
  681. hsai->MspDeInitCallback(hsai);
  682. #else
  683. HAL_SAI_MspDeInit(hsai);
  684. #endif
  685. /* Initialize the error code */
  686. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  687. /* Initialize the SAI state */
  688. hsai->State = HAL_SAI_STATE_RESET;
  689. /* Release Lock */
  690. __HAL_UNLOCK(hsai);
  691. return HAL_OK;
  692. }
  693. /**
  694. * @brief Initialize the SAI MSP.
  695. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  696. * the configuration information for SAI module.
  697. * @retval None
  698. */
  699. __weak void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
  700. {
  701. /* Prevent unused argument(s) compilation warning */
  702. UNUSED(hsai);
  703. /* NOTE : This function should not be modified, when the callback is needed,
  704. the HAL_SAI_MspInit could be implemented in the user file
  705. */
  706. }
  707. /**
  708. * @brief DeInitialize the SAI MSP.
  709. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  710. * the configuration information for SAI module.
  711. * @retval None
  712. */
  713. __weak void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai)
  714. {
  715. /* Prevent unused argument(s) compilation warning */
  716. UNUSED(hsai);
  717. /* NOTE : This function should not be modified, when the callback is needed,
  718. the HAL_SAI_MspDeInit could be implemented in the user file
  719. */
  720. }
  721. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  722. /**
  723. * @brief Register a user SAI callback
  724. * to be used instead of the weak predefined callback.
  725. * @param hsai SAI handle.
  726. * @param CallbackID ID of the callback to be registered.
  727. * This parameter can be one of the following values:
  728. * @arg @ref HAL_SAI_RX_COMPLETE_CB_ID receive complete callback ID.
  729. * @arg @ref HAL_SAI_RX_HALFCOMPLETE_CB_ID receive half complete callback ID.
  730. * @arg @ref HAL_SAI_TX_COMPLETE_CB_ID transmit complete callback ID.
  731. * @arg @ref HAL_SAI_TX_HALFCOMPLETE_CB_ID transmit half complete callback ID.
  732. * @arg @ref HAL_SAI_ERROR_CB_ID error callback ID.
  733. * @arg @ref HAL_SAI_MSPINIT_CB_ID MSP init callback ID.
  734. * @arg @ref HAL_SAI_MSPDEINIT_CB_ID MSP de-init callback ID.
  735. * @param pCallback pointer to the callback function.
  736. * @retval HAL status.
  737. */
  738. HAL_StatusTypeDef HAL_SAI_RegisterCallback(SAI_HandleTypeDef *hsai,
  739. HAL_SAI_CallbackIDTypeDef CallbackID,
  740. pSAI_CallbackTypeDef pCallback)
  741. {
  742. HAL_StatusTypeDef status = HAL_OK;
  743. if (pCallback == NULL)
  744. {
  745. /* update the error code */
  746. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  747. /* update return status */
  748. status = HAL_ERROR;
  749. }
  750. else
  751. {
  752. if (HAL_SAI_STATE_READY == hsai->State)
  753. {
  754. switch (CallbackID)
  755. {
  756. case HAL_SAI_RX_COMPLETE_CB_ID :
  757. hsai->RxCpltCallback = pCallback;
  758. break;
  759. case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
  760. hsai->RxHalfCpltCallback = pCallback;
  761. break;
  762. case HAL_SAI_TX_COMPLETE_CB_ID :
  763. hsai->TxCpltCallback = pCallback;
  764. break;
  765. case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
  766. hsai->TxHalfCpltCallback = pCallback;
  767. break;
  768. case HAL_SAI_ERROR_CB_ID :
  769. hsai->ErrorCallback = pCallback;
  770. break;
  771. case HAL_SAI_MSPINIT_CB_ID :
  772. hsai->MspInitCallback = pCallback;
  773. break;
  774. case HAL_SAI_MSPDEINIT_CB_ID :
  775. hsai->MspDeInitCallback = pCallback;
  776. break;
  777. default :
  778. /* update the error code */
  779. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  780. /* update return status */
  781. status = HAL_ERROR;
  782. break;
  783. }
  784. }
  785. else if (HAL_SAI_STATE_RESET == hsai->State)
  786. {
  787. switch (CallbackID)
  788. {
  789. case HAL_SAI_MSPINIT_CB_ID :
  790. hsai->MspInitCallback = pCallback;
  791. break;
  792. case HAL_SAI_MSPDEINIT_CB_ID :
  793. hsai->MspDeInitCallback = pCallback;
  794. break;
  795. default :
  796. /* update the error code */
  797. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  798. /* update return status */
  799. status = HAL_ERROR;
  800. break;
  801. }
  802. }
  803. else
  804. {
  805. /* update the error code */
  806. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  807. /* update return status */
  808. status = HAL_ERROR;
  809. }
  810. }
  811. return status;
  812. }
  813. /**
  814. * @brief Unregister a user SAI callback.
  815. * SAI callback is redirected to the weak predefined callback.
  816. * @param hsai SAI handle.
  817. * @param CallbackID ID of the callback to be unregistered.
  818. * This parameter can be one of the following values:
  819. * @arg @ref HAL_SAI_RX_COMPLETE_CB_ID receive complete callback ID.
  820. * @arg @ref HAL_SAI_RX_HALFCOMPLETE_CB_ID receive half complete callback ID.
  821. * @arg @ref HAL_SAI_TX_COMPLETE_CB_ID transmit complete callback ID.
  822. * @arg @ref HAL_SAI_TX_HALFCOMPLETE_CB_ID transmit half complete callback ID.
  823. * @arg @ref HAL_SAI_ERROR_CB_ID error callback ID.
  824. * @arg @ref HAL_SAI_MSPINIT_CB_ID MSP init callback ID.
  825. * @arg @ref HAL_SAI_MSPDEINIT_CB_ID MSP de-init callback ID.
  826. * @retval HAL status.
  827. */
  828. HAL_StatusTypeDef HAL_SAI_UnRegisterCallback(SAI_HandleTypeDef *hsai,
  829. HAL_SAI_CallbackIDTypeDef CallbackID)
  830. {
  831. HAL_StatusTypeDef status = HAL_OK;
  832. if (HAL_SAI_STATE_READY == hsai->State)
  833. {
  834. switch (CallbackID)
  835. {
  836. case HAL_SAI_RX_COMPLETE_CB_ID :
  837. hsai->RxCpltCallback = HAL_SAI_RxCpltCallback;
  838. break;
  839. case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
  840. hsai->RxHalfCpltCallback = HAL_SAI_RxHalfCpltCallback;
  841. break;
  842. case HAL_SAI_TX_COMPLETE_CB_ID :
  843. hsai->TxCpltCallback = HAL_SAI_TxCpltCallback;
  844. break;
  845. case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
  846. hsai->TxHalfCpltCallback = HAL_SAI_TxHalfCpltCallback;
  847. break;
  848. case HAL_SAI_ERROR_CB_ID :
  849. hsai->ErrorCallback = HAL_SAI_ErrorCallback;
  850. break;
  851. case HAL_SAI_MSPINIT_CB_ID :
  852. hsai->MspInitCallback = HAL_SAI_MspInit;
  853. break;
  854. case HAL_SAI_MSPDEINIT_CB_ID :
  855. hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
  856. break;
  857. default :
  858. /* update the error code */
  859. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  860. /* update return status */
  861. status = HAL_ERROR;
  862. break;
  863. }
  864. }
  865. else if (HAL_SAI_STATE_RESET == hsai->State)
  866. {
  867. switch (CallbackID)
  868. {
  869. case HAL_SAI_MSPINIT_CB_ID :
  870. hsai->MspInitCallback = HAL_SAI_MspInit;
  871. break;
  872. case HAL_SAI_MSPDEINIT_CB_ID :
  873. hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
  874. break;
  875. default :
  876. /* update the error code */
  877. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  878. /* update return status */
  879. status = HAL_ERROR;
  880. break;
  881. }
  882. }
  883. else
  884. {
  885. /* update the error code */
  886. hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
  887. /* update return status */
  888. status = HAL_ERROR;
  889. }
  890. return status;
  891. }
  892. #endif /* USE_HAL_SAI_REGISTER_CALLBACKS */
  893. /**
  894. * @}
  895. */
  896. /** @defgroup SAI_Exported_Functions_Group2 IO operation functions
  897. * @brief Data transfers functions
  898. *
  899. @verbatim
  900. ==============================================================================
  901. ##### IO operation functions #####
  902. ==============================================================================
  903. [..]
  904. This subsection provides a set of functions allowing to manage the SAI data
  905. transfers.
  906. (+) There are two modes of transfer:
  907. (++) Blocking mode : The communication is performed in the polling mode.
  908. The status of all data processing is returned by the same function
  909. after finishing transfer.
  910. (++) No-Blocking mode : The communication is performed using Interrupts
  911. or DMA. These functions return the status of the transfer startup.
  912. The end of the data processing will be indicated through the
  913. dedicated SAI IRQ when using Interrupt mode or the DMA IRQ when
  914. using DMA mode.
  915. (+) Blocking mode functions are :
  916. (++) HAL_SAI_Transmit()
  917. (++) HAL_SAI_Receive()
  918. (+) Non Blocking mode functions with Interrupt are :
  919. (++) HAL_SAI_Transmit_IT()
  920. (++) HAL_SAI_Receive_IT()
  921. (+) Non Blocking mode functions with DMA are :
  922. (++) HAL_SAI_Transmit_DMA()
  923. (++) HAL_SAI_Receive_DMA()
  924. (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  925. (++) HAL_SAI_TxCpltCallback()
  926. (++) HAL_SAI_RxCpltCallback()
  927. (++) HAL_SAI_ErrorCallback()
  928. @endverbatim
  929. * @{
  930. */
  931. /**
  932. * @brief Transmit an amount of data in blocking mode.
  933. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  934. * the configuration information for SAI module.
  935. * @param pData Pointer to data buffer
  936. * @param Size Amount of data to be sent
  937. * @param Timeout Timeout duration
  938. * @retval HAL status
  939. */
  940. HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  941. {
  942. uint32_t tickstart = HAL_GetTick();
  943. uint32_t temp;
  944. if ((pData == NULL) || (Size == 0U))
  945. {
  946. return HAL_ERROR;
  947. }
  948. if (hsai->State == HAL_SAI_STATE_READY)
  949. {
  950. /* Process Locked */
  951. __HAL_LOCK(hsai);
  952. hsai->XferSize = Size;
  953. hsai->XferCount = Size;
  954. hsai->pBuffPtr = pData;
  955. hsai->State = HAL_SAI_STATE_BUSY_TX;
  956. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  957. /* Check if the SAI is already enabled */
  958. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  959. {
  960. /* fill the fifo with data before to enabled the SAI */
  961. SAI_FillFifo(hsai);
  962. /* Enable SAI peripheral */
  963. __HAL_SAI_ENABLE(hsai);
  964. }
  965. while (hsai->XferCount > 0U)
  966. {
  967. /* Write data if the FIFO is not full */
  968. if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL)
  969. {
  970. if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  971. {
  972. hsai->Instance->DR = *hsai->pBuffPtr;
  973. hsai->pBuffPtr++;
  974. }
  975. else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
  976. {
  977. temp = (uint32_t)(*hsai->pBuffPtr);
  978. hsai->pBuffPtr++;
  979. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  980. hsai->pBuffPtr++;
  981. hsai->Instance->DR = temp;
  982. }
  983. else
  984. {
  985. temp = (uint32_t)(*hsai->pBuffPtr);
  986. hsai->pBuffPtr++;
  987. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  988. hsai->pBuffPtr++;
  989. temp |= ((uint32_t)(*hsai->pBuffPtr) << 16);
  990. hsai->pBuffPtr++;
  991. temp |= ((uint32_t)(*hsai->pBuffPtr) << 24);
  992. hsai->pBuffPtr++;
  993. hsai->Instance->DR = temp;
  994. }
  995. hsai->XferCount--;
  996. }
  997. else
  998. {
  999. /* Check for the Timeout */
  1000. if ((((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) && (Timeout != HAL_MAX_DELAY))
  1001. {
  1002. /* Update error code */
  1003. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  1004. /* Clear all the flags */
  1005. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  1006. /* Disable SAI peripheral */
  1007. /* No need to check return value because state update, unlock and error return will be performed later */
  1008. (void) SAI_Disable(hsai);
  1009. /* Flush the fifo */
  1010. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  1011. /* Change the SAI state */
  1012. hsai->State = HAL_SAI_STATE_READY;
  1013. /* Process Unlocked */
  1014. __HAL_UNLOCK(hsai);
  1015. return HAL_ERROR;
  1016. }
  1017. }
  1018. }
  1019. hsai->State = HAL_SAI_STATE_READY;
  1020. /* Process Unlocked */
  1021. __HAL_UNLOCK(hsai);
  1022. return HAL_OK;
  1023. }
  1024. else
  1025. {
  1026. return HAL_BUSY;
  1027. }
  1028. }
  1029. /**
  1030. * @brief Receive an amount of data in blocking mode.
  1031. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1032. * the configuration information for SAI module.
  1033. * @param pData Pointer to data buffer
  1034. * @param Size Amount of data to be received
  1035. * @param Timeout Timeout duration
  1036. * @retval HAL status
  1037. */
  1038. HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1039. {
  1040. uint32_t tickstart = HAL_GetTick();
  1041. uint32_t temp;
  1042. if ((pData == NULL) || (Size == 0U))
  1043. {
  1044. return HAL_ERROR;
  1045. }
  1046. if (hsai->State == HAL_SAI_STATE_READY)
  1047. {
  1048. /* Process Locked */
  1049. __HAL_LOCK(hsai);
  1050. hsai->pBuffPtr = pData;
  1051. hsai->XferSize = Size;
  1052. hsai->XferCount = Size;
  1053. hsai->State = HAL_SAI_STATE_BUSY_RX;
  1054. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  1055. /* Check if the SAI is already enabled */
  1056. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1057. {
  1058. /* Enable SAI peripheral */
  1059. __HAL_SAI_ENABLE(hsai);
  1060. }
  1061. /* Receive data */
  1062. while (hsai->XferCount > 0U)
  1063. {
  1064. if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_EMPTY)
  1065. {
  1066. if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  1067. {
  1068. *hsai->pBuffPtr = (uint8_t)hsai->Instance->DR;
  1069. hsai->pBuffPtr++;
  1070. }
  1071. else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
  1072. {
  1073. temp = hsai->Instance->DR;
  1074. *hsai->pBuffPtr = (uint8_t)temp;
  1075. hsai->pBuffPtr++;
  1076. *hsai->pBuffPtr = (uint8_t)(temp >> 8);
  1077. hsai->pBuffPtr++;
  1078. }
  1079. else
  1080. {
  1081. temp = hsai->Instance->DR;
  1082. *hsai->pBuffPtr = (uint8_t)temp;
  1083. hsai->pBuffPtr++;
  1084. *hsai->pBuffPtr = (uint8_t)(temp >> 8);
  1085. hsai->pBuffPtr++;
  1086. *hsai->pBuffPtr = (uint8_t)(temp >> 16);
  1087. hsai->pBuffPtr++;
  1088. *hsai->pBuffPtr = (uint8_t)(temp >> 24);
  1089. hsai->pBuffPtr++;
  1090. }
  1091. hsai->XferCount--;
  1092. }
  1093. else
  1094. {
  1095. /* Check for the Timeout */
  1096. if ((((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) && (Timeout != HAL_MAX_DELAY))
  1097. {
  1098. /* Update error code */
  1099. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  1100. /* Clear all the flags */
  1101. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  1102. /* Disable SAI peripheral */
  1103. /* No need to check return value because state update, unlock and error return will be performed later */
  1104. (void) SAI_Disable(hsai);
  1105. /* Flush the fifo */
  1106. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  1107. /* Change the SAI state */
  1108. hsai->State = HAL_SAI_STATE_READY;
  1109. /* Process Unlocked */
  1110. __HAL_UNLOCK(hsai);
  1111. return HAL_ERROR;
  1112. }
  1113. }
  1114. }
  1115. hsai->State = HAL_SAI_STATE_READY;
  1116. /* Process Unlocked */
  1117. __HAL_UNLOCK(hsai);
  1118. return HAL_OK;
  1119. }
  1120. else
  1121. {
  1122. return HAL_BUSY;
  1123. }
  1124. }
  1125. /**
  1126. * @brief Transmit an amount of data in non-blocking mode with Interrupt.
  1127. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1128. * the configuration information for SAI module.
  1129. * @param pData Pointer to data buffer
  1130. * @param Size Amount of data to be sent
  1131. * @retval HAL status
  1132. */
  1133. HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  1134. {
  1135. if ((pData == NULL) || (Size == 0U))
  1136. {
  1137. return HAL_ERROR;
  1138. }
  1139. if (hsai->State == HAL_SAI_STATE_READY)
  1140. {
  1141. /* Process Locked */
  1142. __HAL_LOCK(hsai);
  1143. hsai->pBuffPtr = pData;
  1144. hsai->XferSize = Size;
  1145. hsai->XferCount = Size;
  1146. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  1147. hsai->State = HAL_SAI_STATE_BUSY_TX;
  1148. if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  1149. {
  1150. hsai->InterruptServiceRoutine = SAI_Transmit_IT8Bit;
  1151. }
  1152. else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
  1153. {
  1154. hsai->InterruptServiceRoutine = SAI_Transmit_IT16Bit;
  1155. }
  1156. else
  1157. {
  1158. hsai->InterruptServiceRoutine = SAI_Transmit_IT32Bit;
  1159. }
  1160. /* Fill the fifo before starting the communication */
  1161. SAI_FillFifo(hsai);
  1162. /* Enable FRQ and OVRUDR interrupts */
  1163. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1164. /* Check if the SAI is already enabled */
  1165. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1166. {
  1167. /* Enable SAI peripheral */
  1168. __HAL_SAI_ENABLE(hsai);
  1169. }
  1170. /* Process Unlocked */
  1171. __HAL_UNLOCK(hsai);
  1172. return HAL_OK;
  1173. }
  1174. else
  1175. {
  1176. return HAL_BUSY;
  1177. }
  1178. }
  1179. /**
  1180. * @brief Receive an amount of data in non-blocking mode with Interrupt.
  1181. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1182. * the configuration information for SAI module.
  1183. * @param pData Pointer to data buffer
  1184. * @param Size Amount of data to be received
  1185. * @retval HAL status
  1186. */
  1187. HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  1188. {
  1189. if ((pData == NULL) || (Size == 0U))
  1190. {
  1191. return HAL_ERROR;
  1192. }
  1193. if (hsai->State == HAL_SAI_STATE_READY)
  1194. {
  1195. /* Process Locked */
  1196. __HAL_LOCK(hsai);
  1197. hsai->pBuffPtr = pData;
  1198. hsai->XferSize = Size;
  1199. hsai->XferCount = Size;
  1200. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  1201. hsai->State = HAL_SAI_STATE_BUSY_RX;
  1202. if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  1203. {
  1204. hsai->InterruptServiceRoutine = SAI_Receive_IT8Bit;
  1205. }
  1206. else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
  1207. {
  1208. hsai->InterruptServiceRoutine = SAI_Receive_IT16Bit;
  1209. }
  1210. else
  1211. {
  1212. hsai->InterruptServiceRoutine = SAI_Receive_IT32Bit;
  1213. }
  1214. /* Enable TXE and OVRUDR interrupts */
  1215. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  1216. /* Check if the SAI is already enabled */
  1217. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1218. {
  1219. /* Enable SAI peripheral */
  1220. __HAL_SAI_ENABLE(hsai);
  1221. }
  1222. /* Process Unlocked */
  1223. __HAL_UNLOCK(hsai);
  1224. return HAL_OK;
  1225. }
  1226. else
  1227. {
  1228. return HAL_BUSY;
  1229. }
  1230. }
  1231. /**
  1232. * @brief Pause the audio stream playing from the Media.
  1233. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1234. * the configuration information for SAI module.
  1235. * @retval HAL status
  1236. */
  1237. HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai)
  1238. {
  1239. /* Process Locked */
  1240. __HAL_LOCK(hsai);
  1241. /* Pause the audio file playing by disabling the SAI DMA requests */
  1242. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  1243. /* Process Unlocked */
  1244. __HAL_UNLOCK(hsai);
  1245. return HAL_OK;
  1246. }
  1247. /**
  1248. * @brief Resume the audio stream playing from the Media.
  1249. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1250. * the configuration information for SAI module.
  1251. * @retval HAL status
  1252. */
  1253. HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai)
  1254. {
  1255. /* Process Locked */
  1256. __HAL_LOCK(hsai);
  1257. /* Enable the SAI DMA requests */
  1258. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  1259. /* If the SAI peripheral is still not enabled, enable it */
  1260. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1261. {
  1262. /* Enable SAI peripheral */
  1263. __HAL_SAI_ENABLE(hsai);
  1264. }
  1265. /* Process Unlocked */
  1266. __HAL_UNLOCK(hsai);
  1267. return HAL_OK;
  1268. }
  1269. /**
  1270. * @brief Stop the audio stream playing from the Media.
  1271. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1272. * the configuration information for SAI module.
  1273. * @retval HAL status
  1274. */
  1275. HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai)
  1276. {
  1277. HAL_StatusTypeDef status = HAL_OK;
  1278. /* Process Locked */
  1279. __HAL_LOCK(hsai);
  1280. /* Disable the SAI DMA request */
  1281. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  1282. /* Abort the SAI Tx DMA Stream */
  1283. if ((hsai->State == HAL_SAI_STATE_BUSY_TX) && (hsai->hdmatx != NULL))
  1284. {
  1285. /* No need to check the returned value of HAL_DMA_Abort. */
  1286. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for SAI. */
  1287. (void) HAL_DMA_Abort(hsai->hdmatx);
  1288. }
  1289. /* Abort the SAI Rx DMA Stream */
  1290. if ((hsai->State == HAL_SAI_STATE_BUSY_RX) && (hsai->hdmarx != NULL))
  1291. {
  1292. /* No need to check the returned value of HAL_DMA_Abort. */
  1293. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for SAI. */
  1294. (void) HAL_DMA_Abort(hsai->hdmarx);
  1295. }
  1296. /* Disable SAI peripheral */
  1297. if (SAI_Disable(hsai) != HAL_OK)
  1298. {
  1299. status = HAL_ERROR;
  1300. }
  1301. /* Flush the fifo */
  1302. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  1303. /* Set hsai state to ready */
  1304. hsai->State = HAL_SAI_STATE_READY;
  1305. /* Process Unlocked */
  1306. __HAL_UNLOCK(hsai);
  1307. return status;
  1308. }
  1309. /**
  1310. * @brief Abort the current transfer and disable the SAI.
  1311. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1312. * the configuration information for SAI module.
  1313. * @retval HAL status
  1314. */
  1315. HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai)
  1316. {
  1317. HAL_StatusTypeDef status = HAL_OK;
  1318. /* Process Locked */
  1319. __HAL_LOCK(hsai);
  1320. /* Check SAI DMA is enabled or not */
  1321. if ((hsai->Instance->CR1 & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
  1322. {
  1323. /* Disable the SAI DMA request */
  1324. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  1325. /* Abort the SAI Tx DMA Stream */
  1326. if ((hsai->State == HAL_SAI_STATE_BUSY_TX) && (hsai->hdmatx != NULL))
  1327. {
  1328. /* No need to check the returned value of HAL_DMA_Abort. */
  1329. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for SAI. */
  1330. (void) HAL_DMA_Abort(hsai->hdmatx);
  1331. }
  1332. /* Abort the SAI Rx DMA Stream */
  1333. if ((hsai->State == HAL_SAI_STATE_BUSY_RX) && (hsai->hdmarx != NULL))
  1334. {
  1335. /* No need to check the returned value of HAL_DMA_Abort. */
  1336. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for SAI. */
  1337. (void) HAL_DMA_Abort(hsai->hdmarx);
  1338. }
  1339. }
  1340. /* Disabled All interrupt and clear all the flag */
  1341. hsai->Instance->IMR = 0;
  1342. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  1343. /* Disable SAI peripheral */
  1344. if (SAI_Disable(hsai) != HAL_OK)
  1345. {
  1346. status = HAL_ERROR;
  1347. }
  1348. /* Flush the fifo */
  1349. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  1350. /* Set hsai state to ready */
  1351. hsai->State = HAL_SAI_STATE_READY;
  1352. /* Process Unlocked */
  1353. __HAL_UNLOCK(hsai);
  1354. return status;
  1355. }
  1356. /**
  1357. * @brief Transmit an amount of data in non-blocking mode with DMA.
  1358. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1359. * the configuration information for SAI module.
  1360. * @param pData Pointer to data buffer
  1361. * @param Size Amount of data to be sent
  1362. * @retval HAL status
  1363. */
  1364. HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  1365. {
  1366. uint32_t tickstart = HAL_GetTick();
  1367. if ((pData == NULL) || (Size == 0U))
  1368. {
  1369. return HAL_ERROR;
  1370. }
  1371. if (hsai->State == HAL_SAI_STATE_READY)
  1372. {
  1373. /* Process Locked */
  1374. __HAL_LOCK(hsai);
  1375. hsai->pBuffPtr = pData;
  1376. hsai->XferSize = Size;
  1377. hsai->XferCount = Size;
  1378. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  1379. hsai->State = HAL_SAI_STATE_BUSY_TX;
  1380. /* Set the SAI Tx DMA Half transfer complete callback */
  1381. hsai->hdmatx->XferHalfCpltCallback = SAI_DMATxHalfCplt;
  1382. /* Set the SAI TxDMA transfer complete callback */
  1383. hsai->hdmatx->XferCpltCallback = SAI_DMATxCplt;
  1384. /* Set the DMA error callback */
  1385. hsai->hdmatx->XferErrorCallback = SAI_DMAError;
  1386. /* Set the DMA Tx abort callback */
  1387. hsai->hdmatx->XferAbortCallback = NULL;
  1388. /* Enable the Tx DMA Stream */
  1389. if (HAL_DMA_Start_IT(hsai->hdmatx, (uint32_t)hsai->pBuffPtr, (uint32_t)&hsai->Instance->DR, hsai->XferSize) != HAL_OK)
  1390. {
  1391. __HAL_UNLOCK(hsai);
  1392. return HAL_ERROR;
  1393. }
  1394. /* Enable the interrupts for error handling */
  1395. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  1396. /* Enable SAI Tx DMA Request */
  1397. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  1398. /* Wait until FIFO is not empty */
  1399. while ((hsai->Instance->SR & SAI_xSR_FLVL) == SAI_FIFOSTATUS_EMPTY)
  1400. {
  1401. /* Check for the Timeout */
  1402. if ((HAL_GetTick() - tickstart) > SAI_LONG_TIMEOUT)
  1403. {
  1404. /* Update error code */
  1405. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  1406. /* Process Unlocked */
  1407. __HAL_UNLOCK(hsai);
  1408. return HAL_TIMEOUT;
  1409. }
  1410. }
  1411. /* Check if the SAI is already enabled */
  1412. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1413. {
  1414. /* Enable SAI peripheral */
  1415. __HAL_SAI_ENABLE(hsai);
  1416. }
  1417. /* Process Unlocked */
  1418. __HAL_UNLOCK(hsai);
  1419. return HAL_OK;
  1420. }
  1421. else
  1422. {
  1423. return HAL_BUSY;
  1424. }
  1425. }
  1426. /**
  1427. * @brief Receive an amount of data in non-blocking mode with DMA.
  1428. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1429. * the configuration information for SAI module.
  1430. * @param pData Pointer to data buffer
  1431. * @param Size Amount of data to be received
  1432. * @retval HAL status
  1433. */
  1434. HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
  1435. {
  1436. if ((pData == NULL) || (Size == 0U))
  1437. {
  1438. return HAL_ERROR;
  1439. }
  1440. if (hsai->State == HAL_SAI_STATE_READY)
  1441. {
  1442. /* Process Locked */
  1443. __HAL_LOCK(hsai);
  1444. hsai->pBuffPtr = pData;
  1445. hsai->XferSize = Size;
  1446. hsai->XferCount = Size;
  1447. hsai->ErrorCode = HAL_SAI_ERROR_NONE;
  1448. hsai->State = HAL_SAI_STATE_BUSY_RX;
  1449. /* Set the SAI Rx DMA Half transfer complete callback */
  1450. hsai->hdmarx->XferHalfCpltCallback = SAI_DMARxHalfCplt;
  1451. /* Set the SAI Rx DMA transfer complete callback */
  1452. hsai->hdmarx->XferCpltCallback = SAI_DMARxCplt;
  1453. /* Set the DMA error callback */
  1454. hsai->hdmarx->XferErrorCallback = SAI_DMAError;
  1455. /* Set the DMA Rx abort callback */
  1456. hsai->hdmarx->XferAbortCallback = NULL;
  1457. /* Enable the Rx DMA Stream */
  1458. if (HAL_DMA_Start_IT(hsai->hdmarx, (uint32_t)&hsai->Instance->DR, (uint32_t)hsai->pBuffPtr, hsai->XferSize) != HAL_OK)
  1459. {
  1460. __HAL_UNLOCK(hsai);
  1461. return HAL_ERROR;
  1462. }
  1463. /* Enable the interrupts for error handling */
  1464. __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  1465. /* Enable SAI Rx DMA Request */
  1466. hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
  1467. /* Check if the SAI is already enabled */
  1468. if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
  1469. {
  1470. /* Enable SAI peripheral */
  1471. __HAL_SAI_ENABLE(hsai);
  1472. }
  1473. /* Process Unlocked */
  1474. __HAL_UNLOCK(hsai);
  1475. return HAL_OK;
  1476. }
  1477. else
  1478. {
  1479. return HAL_BUSY;
  1480. }
  1481. }
  1482. /**
  1483. * @brief Enable the Tx mute mode.
  1484. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1485. * the configuration information for SAI module.
  1486. * @param val value sent during the mute @ref SAI_Block_Mute_Value
  1487. * @retval HAL status
  1488. */
  1489. HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val)
  1490. {
  1491. assert_param(IS_SAI_BLOCK_MUTE_VALUE(val));
  1492. if (hsai->State != HAL_SAI_STATE_RESET)
  1493. {
  1494. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
  1495. SET_BIT(hsai->Instance->CR2, SAI_xCR2_MUTE | (uint32_t)val);
  1496. return HAL_OK;
  1497. }
  1498. return HAL_ERROR;
  1499. }
  1500. /**
  1501. * @brief Disable the Tx mute mode.
  1502. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1503. * the configuration information for SAI module.
  1504. * @retval HAL status
  1505. */
  1506. HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai)
  1507. {
  1508. if (hsai->State != HAL_SAI_STATE_RESET)
  1509. {
  1510. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
  1511. return HAL_OK;
  1512. }
  1513. return HAL_ERROR;
  1514. }
  1515. /**
  1516. * @brief Enable the Rx mute detection.
  1517. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1518. * the configuration information for SAI module.
  1519. * @param callback function called when the mute is detected.
  1520. * @param counter number a data before mute detection max 63.
  1521. * @retval HAL status
  1522. */
  1523. HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter)
  1524. {
  1525. assert_param(IS_SAI_BLOCK_MUTE_COUNTER(counter));
  1526. if (hsai->State != HAL_SAI_STATE_RESET)
  1527. {
  1528. /* set the mute counter */
  1529. CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTECNT);
  1530. SET_BIT(hsai->Instance->CR2, (uint32_t)((uint32_t)counter << SAI_xCR2_MUTECNT_Pos));
  1531. hsai->mutecallback = callback;
  1532. /* enable the IT interrupt */
  1533. __HAL_SAI_ENABLE_IT(hsai, SAI_IT_MUTEDET);
  1534. return HAL_OK;
  1535. }
  1536. return HAL_ERROR;
  1537. }
  1538. /**
  1539. * @brief Disable the Rx mute detection.
  1540. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1541. * the configuration information for SAI module.
  1542. * @retval HAL status
  1543. */
  1544. HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai)
  1545. {
  1546. if (hsai->State != HAL_SAI_STATE_RESET)
  1547. {
  1548. /* set the mutecallback to NULL */
  1549. hsai->mutecallback = NULL;
  1550. /* enable the IT interrupt */
  1551. __HAL_SAI_DISABLE_IT(hsai, SAI_IT_MUTEDET);
  1552. return HAL_OK;
  1553. }
  1554. return HAL_ERROR;
  1555. }
  1556. /**
  1557. * @brief Handle SAI interrupt request.
  1558. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1559. * the configuration information for SAI module.
  1560. * @retval None
  1561. */
  1562. void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai)
  1563. {
  1564. if (hsai->State != HAL_SAI_STATE_RESET)
  1565. {
  1566. uint32_t itflags = hsai->Instance->SR;
  1567. uint32_t itsources = hsai->Instance->IMR;
  1568. uint32_t cr1config = hsai->Instance->CR1;
  1569. uint32_t tmperror;
  1570. /* SAI Fifo request interrupt occurred -----------------------------------*/
  1571. if (((itflags & SAI_xSR_FREQ) == SAI_xSR_FREQ) && ((itsources & SAI_IT_FREQ) == SAI_IT_FREQ))
  1572. {
  1573. hsai->InterruptServiceRoutine(hsai);
  1574. }
  1575. /* SAI Overrun error interrupt occurred ----------------------------------*/
  1576. else if (((itflags & SAI_FLAG_OVRUDR) == SAI_FLAG_OVRUDR) && ((itsources & SAI_IT_OVRUDR) == SAI_IT_OVRUDR))
  1577. {
  1578. /* Clear the SAI Overrun flag */
  1579. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  1580. /* Get the SAI error code */
  1581. tmperror = ((hsai->State == HAL_SAI_STATE_BUSY_RX) ? HAL_SAI_ERROR_OVR : HAL_SAI_ERROR_UDR);
  1582. /* Change the SAI error code */
  1583. hsai->ErrorCode |= tmperror;
  1584. /* the transfer is not stopped, we will forward the information to the user and we let the user decide what needs to be done */
  1585. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1586. hsai->ErrorCallback(hsai);
  1587. #else
  1588. HAL_SAI_ErrorCallback(hsai);
  1589. #endif
  1590. }
  1591. /* SAI mutedet interrupt occurred ----------------------------------*/
  1592. else if (((itflags & SAI_FLAG_MUTEDET) == SAI_FLAG_MUTEDET) && ((itsources & SAI_IT_MUTEDET) == SAI_IT_MUTEDET))
  1593. {
  1594. /* Clear the SAI mutedet flag */
  1595. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_MUTEDET);
  1596. /* call the call back function */
  1597. if (hsai->mutecallback != NULL)
  1598. {
  1599. /* inform the user that an RX mute event has been detected */
  1600. hsai->mutecallback();
  1601. }
  1602. }
  1603. /* SAI AFSDET interrupt occurred ----------------------------------*/
  1604. else if (((itflags & SAI_FLAG_AFSDET) == SAI_FLAG_AFSDET) && ((itsources & SAI_IT_AFSDET) == SAI_IT_AFSDET))
  1605. {
  1606. /* Clear the SAI AFSDET flag */
  1607. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_AFSDET);
  1608. /* Change the SAI error code */
  1609. hsai->ErrorCode |= HAL_SAI_ERROR_AFSDET;
  1610. /* Check SAI DMA is enabled or not */
  1611. if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
  1612. {
  1613. /* Abort the SAI DMA Streams */
  1614. if (hsai->hdmatx != NULL)
  1615. {
  1616. /* Set the DMA Tx abort callback */
  1617. hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
  1618. /* Abort DMA in IT mode */
  1619. if (HAL_DMA_Abort_IT(hsai->hdmatx) != HAL_OK)
  1620. {
  1621. /* Update SAI error code */
  1622. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1623. /* Call SAI error callback */
  1624. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1625. hsai->ErrorCallback(hsai);
  1626. #else
  1627. HAL_SAI_ErrorCallback(hsai);
  1628. #endif
  1629. }
  1630. }
  1631. if (hsai->hdmarx != NULL)
  1632. {
  1633. /* Set the DMA Rx abort callback */
  1634. hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
  1635. /* Abort DMA in IT mode */
  1636. if (HAL_DMA_Abort_IT(hsai->hdmarx) != HAL_OK)
  1637. {
  1638. /* Update SAI error code */
  1639. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1640. /* Call SAI error callback */
  1641. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1642. hsai->ErrorCallback(hsai);
  1643. #else
  1644. HAL_SAI_ErrorCallback(hsai);
  1645. #endif
  1646. }
  1647. }
  1648. }
  1649. else
  1650. {
  1651. /* Abort SAI */
  1652. /* No need to check return value because HAL_SAI_ErrorCallback will be called later */
  1653. (void) HAL_SAI_Abort(hsai);
  1654. /* Set error callback */
  1655. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1656. hsai->ErrorCallback(hsai);
  1657. #else
  1658. HAL_SAI_ErrorCallback(hsai);
  1659. #endif
  1660. }
  1661. }
  1662. /* SAI LFSDET interrupt occurred ----------------------------------*/
  1663. else if (((itflags & SAI_FLAG_LFSDET) == SAI_FLAG_LFSDET) && ((itsources & SAI_IT_LFSDET) == SAI_IT_LFSDET))
  1664. {
  1665. /* Clear the SAI LFSDET flag */
  1666. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_LFSDET);
  1667. /* Change the SAI error code */
  1668. hsai->ErrorCode |= HAL_SAI_ERROR_LFSDET;
  1669. /* Check SAI DMA is enabled or not */
  1670. if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
  1671. {
  1672. /* Abort the SAI DMA Streams */
  1673. if (hsai->hdmatx != NULL)
  1674. {
  1675. /* Set the DMA Tx abort callback */
  1676. hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
  1677. /* Abort DMA in IT mode */
  1678. if (HAL_DMA_Abort_IT(hsai->hdmatx) != HAL_OK)
  1679. {
  1680. /* Update SAI error code */
  1681. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1682. /* Call SAI error callback */
  1683. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1684. hsai->ErrorCallback(hsai);
  1685. #else
  1686. HAL_SAI_ErrorCallback(hsai);
  1687. #endif
  1688. }
  1689. }
  1690. if (hsai->hdmarx != NULL)
  1691. {
  1692. /* Set the DMA Rx abort callback */
  1693. hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
  1694. /* Abort DMA in IT mode */
  1695. if (HAL_DMA_Abort_IT(hsai->hdmarx) != HAL_OK)
  1696. {
  1697. /* Update SAI error code */
  1698. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1699. /* Call SAI error callback */
  1700. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1701. hsai->ErrorCallback(hsai);
  1702. #else
  1703. HAL_SAI_ErrorCallback(hsai);
  1704. #endif
  1705. }
  1706. }
  1707. }
  1708. else
  1709. {
  1710. /* Abort SAI */
  1711. /* No need to check return value because HAL_SAI_ErrorCallback will be called later */
  1712. (void) HAL_SAI_Abort(hsai);
  1713. /* Set error callback */
  1714. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1715. hsai->ErrorCallback(hsai);
  1716. #else
  1717. HAL_SAI_ErrorCallback(hsai);
  1718. #endif
  1719. }
  1720. }
  1721. /* SAI WCKCFG interrupt occurred ----------------------------------*/
  1722. else if (((itflags & SAI_FLAG_WCKCFG) == SAI_FLAG_WCKCFG) && ((itsources & SAI_IT_WCKCFG) == SAI_IT_WCKCFG))
  1723. {
  1724. /* Clear the SAI WCKCFG flag */
  1725. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_WCKCFG);
  1726. /* Change the SAI error code */
  1727. hsai->ErrorCode |= HAL_SAI_ERROR_WCKCFG;
  1728. /* Check SAI DMA is enabled or not */
  1729. if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
  1730. {
  1731. /* Abort the SAI DMA Streams */
  1732. if (hsai->hdmatx != NULL)
  1733. {
  1734. /* Set the DMA Tx abort callback */
  1735. hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
  1736. /* Abort DMA in IT mode */
  1737. if (HAL_DMA_Abort_IT(hsai->hdmatx) != HAL_OK)
  1738. {
  1739. /* Update SAI error code */
  1740. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1741. /* Call SAI error callback */
  1742. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1743. hsai->ErrorCallback(hsai);
  1744. #else
  1745. HAL_SAI_ErrorCallback(hsai);
  1746. #endif
  1747. }
  1748. }
  1749. if (hsai->hdmarx != NULL)
  1750. {
  1751. /* Set the DMA Rx abort callback */
  1752. hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
  1753. /* Abort DMA in IT mode */
  1754. if (HAL_DMA_Abort_IT(hsai->hdmarx) != HAL_OK)
  1755. {
  1756. /* Update SAI error code */
  1757. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  1758. /* Call SAI error callback */
  1759. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1760. hsai->ErrorCallback(hsai);
  1761. #else
  1762. HAL_SAI_ErrorCallback(hsai);
  1763. #endif
  1764. }
  1765. }
  1766. }
  1767. else
  1768. {
  1769. /* If WCKCFG occurs, SAI audio block is automatically disabled */
  1770. /* Disable all interrupts and clear all flags */
  1771. hsai->Instance->IMR = 0U;
  1772. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  1773. /* Set the SAI state to ready to be able to start again the process */
  1774. hsai->State = HAL_SAI_STATE_READY;
  1775. /* Initialize XferCount */
  1776. hsai->XferCount = 0U;
  1777. /* SAI error Callback */
  1778. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1779. hsai->ErrorCallback(hsai);
  1780. #else
  1781. HAL_SAI_ErrorCallback(hsai);
  1782. #endif
  1783. }
  1784. }
  1785. /* SAI CNRDY interrupt occurred ----------------------------------*/
  1786. else if (((itflags & SAI_FLAG_CNRDY) == SAI_FLAG_CNRDY) && ((itsources & SAI_IT_CNRDY) == SAI_IT_CNRDY))
  1787. {
  1788. /* Clear the SAI CNRDY flag */
  1789. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_CNRDY);
  1790. /* Change the SAI error code */
  1791. hsai->ErrorCode |= HAL_SAI_ERROR_CNREADY;
  1792. /* the transfer is not stopped, we will forward the information to the user and we let the user decide what needs to be done */
  1793. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  1794. hsai->ErrorCallback(hsai);
  1795. #else
  1796. HAL_SAI_ErrorCallback(hsai);
  1797. #endif
  1798. }
  1799. else
  1800. {
  1801. /* Nothing to do */
  1802. }
  1803. }
  1804. }
  1805. /**
  1806. * @brief Tx Transfer completed callback.
  1807. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1808. * the configuration information for SAI module.
  1809. * @retval None
  1810. */
  1811. __weak void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
  1812. {
  1813. /* Prevent unused argument(s) compilation warning */
  1814. UNUSED(hsai);
  1815. /* NOTE : This function should not be modified, when the callback is needed,
  1816. the HAL_SAI_TxCpltCallback could be implemented in the user file
  1817. */
  1818. }
  1819. /**
  1820. * @brief Tx Transfer Half completed callback.
  1821. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1822. * the configuration information for SAI module.
  1823. * @retval None
  1824. */
  1825. __weak void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
  1826. {
  1827. /* Prevent unused argument(s) compilation warning */
  1828. UNUSED(hsai);
  1829. /* NOTE : This function should not be modified, when the callback is needed,
  1830. the HAL_SAI_TxHalfCpltCallback could be implemented in the user file
  1831. */
  1832. }
  1833. /**
  1834. * @brief Rx Transfer completed callback.
  1835. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1836. * the configuration information for SAI module.
  1837. * @retval None
  1838. */
  1839. __weak void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai)
  1840. {
  1841. /* Prevent unused argument(s) compilation warning */
  1842. UNUSED(hsai);
  1843. /* NOTE : This function should not be modified, when the callback is needed,
  1844. the HAL_SAI_RxCpltCallback could be implemented in the user file
  1845. */
  1846. }
  1847. /**
  1848. * @brief Rx Transfer half completed callback.
  1849. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1850. * the configuration information for SAI module.
  1851. * @retval None
  1852. */
  1853. __weak void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai)
  1854. {
  1855. /* Prevent unused argument(s) compilation warning */
  1856. UNUSED(hsai);
  1857. /* NOTE : This function should not be modified, when the callback is needed,
  1858. the HAL_SAI_RxHalfCpltCallback could be implemented in the user file
  1859. */
  1860. }
  1861. /**
  1862. * @brief SAI error callback.
  1863. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1864. * the configuration information for SAI module.
  1865. * @retval None
  1866. */
  1867. __weak void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
  1868. {
  1869. /* Prevent unused argument(s) compilation warning */
  1870. UNUSED(hsai);
  1871. /* NOTE : This function should not be modified, when the callback is needed,
  1872. the HAL_SAI_ErrorCallback could be implemented in the user file
  1873. */
  1874. }
  1875. /**
  1876. * @}
  1877. */
  1878. /** @defgroup SAI_Exported_Functions_Group3 Peripheral State functions
  1879. * @brief Peripheral State functions
  1880. *
  1881. @verbatim
  1882. ===============================================================================
  1883. ##### Peripheral State and Errors functions #####
  1884. ===============================================================================
  1885. [..]
  1886. This subsection permits to get in run-time the status of the peripheral
  1887. and the data flow.
  1888. @endverbatim
  1889. * @{
  1890. */
  1891. /**
  1892. * @brief Return the SAI handle state.
  1893. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1894. * the configuration information for SAI module.
  1895. * @retval HAL state
  1896. */
  1897. HAL_SAI_StateTypeDef HAL_SAI_GetState(const SAI_HandleTypeDef *hsai)
  1898. {
  1899. return hsai->State;
  1900. }
  1901. /**
  1902. * @brief Return the SAI error code.
  1903. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1904. * the configuration information for the specified SAI Block.
  1905. * @retval SAI Error Code
  1906. */
  1907. uint32_t HAL_SAI_GetError(const SAI_HandleTypeDef *hsai)
  1908. {
  1909. return hsai->ErrorCode;
  1910. }
  1911. /**
  1912. * @}
  1913. */
  1914. /**
  1915. * @}
  1916. */
  1917. /** @addtogroup SAI_Private_Functions
  1918. * @brief Private functions
  1919. * @{
  1920. */
  1921. /**
  1922. * @brief Initialize the SAI I2S protocol according to the specified parameters
  1923. * in the SAI_InitTypeDef and create the associated handle.
  1924. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  1925. * the configuration information for SAI module.
  1926. * @param protocol one of the supported protocol.
  1927. * @param datasize one of the supported datasize @ref SAI_Protocol_DataSize.
  1928. * @param nbslot number of slot minimum value is 2 and max is 16.
  1929. * the value must be a multiple of 2.
  1930. * @retval HAL status
  1931. */
  1932. static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  1933. {
  1934. HAL_StatusTypeDef status = HAL_OK;
  1935. hsai->Init.Protocol = SAI_FREE_PROTOCOL;
  1936. hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
  1937. /* Compute ClockStrobing according AudioMode */
  1938. if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
  1939. {
  1940. /* Transmit */
  1941. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  1942. }
  1943. else
  1944. {
  1945. /* Receive */
  1946. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
  1947. }
  1948. hsai->FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
  1949. hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
  1950. hsai->SlotInit.FirstBitOffset = 0;
  1951. hsai->SlotInit.SlotNumber = nbslot;
  1952. /* in IS2 the number of slot must be even */
  1953. if ((nbslot & 0x1U) != 0U)
  1954. {
  1955. return HAL_ERROR;
  1956. }
  1957. if (protocol == SAI_I2S_STANDARD)
  1958. {
  1959. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
  1960. hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
  1961. }
  1962. else
  1963. {
  1964. /* SAI_I2S_MSBJUSTIFIED or SAI_I2S_LSBJUSTIFIED */
  1965. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
  1966. hsai->FrameInit.FSOffset = SAI_FS_FIRSTBIT;
  1967. }
  1968. /* Frame definition */
  1969. switch (datasize)
  1970. {
  1971. case SAI_PROTOCOL_DATASIZE_16BIT:
  1972. hsai->Init.DataSize = SAI_DATASIZE_16;
  1973. hsai->FrameInit.FrameLength = 32U * (nbslot / 2U);
  1974. hsai->FrameInit.ActiveFrameLength = 16U * (nbslot / 2U);
  1975. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
  1976. break;
  1977. case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
  1978. hsai->Init.DataSize = SAI_DATASIZE_16;
  1979. hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
  1980. hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
  1981. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1982. break;
  1983. case SAI_PROTOCOL_DATASIZE_24BIT:
  1984. hsai->Init.DataSize = SAI_DATASIZE_24;
  1985. hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
  1986. hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
  1987. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1988. break;
  1989. case SAI_PROTOCOL_DATASIZE_32BIT:
  1990. hsai->Init.DataSize = SAI_DATASIZE_32;
  1991. hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
  1992. hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
  1993. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  1994. break;
  1995. default :
  1996. status = HAL_ERROR;
  1997. break;
  1998. }
  1999. if (protocol == SAI_I2S_LSBJUSTIFIED)
  2000. {
  2001. if (datasize == SAI_PROTOCOL_DATASIZE_16BITEXTENDED)
  2002. {
  2003. hsai->SlotInit.FirstBitOffset = 16;
  2004. }
  2005. if (datasize == SAI_PROTOCOL_DATASIZE_24BIT)
  2006. {
  2007. hsai->SlotInit.FirstBitOffset = 8;
  2008. }
  2009. }
  2010. return status;
  2011. }
  2012. /**
  2013. * @brief Initialize the SAI PCM protocol according to the specified parameters
  2014. * in the SAI_InitTypeDef and create the associated handle.
  2015. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2016. * the configuration information for SAI module.
  2017. * @param protocol one of the supported protocol
  2018. * @param datasize one of the supported datasize @ref SAI_Protocol_DataSize
  2019. * @param nbslot number of slot minimum value is 1 and the max is 16.
  2020. * @retval HAL status
  2021. */
  2022. static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
  2023. {
  2024. HAL_StatusTypeDef status = HAL_OK;
  2025. hsai->Init.Protocol = SAI_FREE_PROTOCOL;
  2026. hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
  2027. /* Compute ClockStrobing according AudioMode */
  2028. if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
  2029. {
  2030. /* Transmit */
  2031. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
  2032. }
  2033. else
  2034. {
  2035. /* Receive */
  2036. hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  2037. }
  2038. hsai->FrameInit.FSDefinition = SAI_FS_STARTFRAME;
  2039. hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
  2040. hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
  2041. hsai->SlotInit.FirstBitOffset = 0;
  2042. hsai->SlotInit.SlotNumber = nbslot;
  2043. hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
  2044. if (protocol == SAI_PCM_SHORT)
  2045. {
  2046. hsai->FrameInit.ActiveFrameLength = 1;
  2047. }
  2048. else
  2049. {
  2050. /* SAI_PCM_LONG */
  2051. hsai->FrameInit.ActiveFrameLength = 13;
  2052. }
  2053. switch (datasize)
  2054. {
  2055. case SAI_PROTOCOL_DATASIZE_16BIT:
  2056. hsai->Init.DataSize = SAI_DATASIZE_16;
  2057. hsai->FrameInit.FrameLength = 16U * nbslot;
  2058. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
  2059. break;
  2060. case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
  2061. hsai->Init.DataSize = SAI_DATASIZE_16;
  2062. hsai->FrameInit.FrameLength = 32U * nbslot;
  2063. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  2064. break;
  2065. case SAI_PROTOCOL_DATASIZE_24BIT :
  2066. hsai->Init.DataSize = SAI_DATASIZE_24;
  2067. hsai->FrameInit.FrameLength = 32U * nbslot;
  2068. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  2069. break;
  2070. case SAI_PROTOCOL_DATASIZE_32BIT:
  2071. hsai->Init.DataSize = SAI_DATASIZE_32;
  2072. hsai->FrameInit.FrameLength = 32U * nbslot;
  2073. hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
  2074. break;
  2075. default :
  2076. status = HAL_ERROR;
  2077. break;
  2078. }
  2079. return status;
  2080. }
  2081. /**
  2082. * @brief Fill the fifo.
  2083. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2084. * the configuration information for SAI module.
  2085. * @retval None
  2086. */
  2087. static void SAI_FillFifo(SAI_HandleTypeDef *hsai)
  2088. {
  2089. uint32_t temp;
  2090. /* fill the fifo with data before to enabled the SAI */
  2091. while (((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL) && (hsai->XferCount > 0U))
  2092. {
  2093. if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
  2094. {
  2095. hsai->Instance->DR = *hsai->pBuffPtr;
  2096. hsai->pBuffPtr++;
  2097. }
  2098. else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
  2099. {
  2100. temp = (uint32_t)(*hsai->pBuffPtr);
  2101. hsai->pBuffPtr++;
  2102. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  2103. hsai->pBuffPtr++;
  2104. hsai->Instance->DR = temp;
  2105. }
  2106. else
  2107. {
  2108. temp = (uint32_t)(*hsai->pBuffPtr);
  2109. hsai->pBuffPtr++;
  2110. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  2111. hsai->pBuffPtr++;
  2112. temp |= ((uint32_t)(*hsai->pBuffPtr) << 16);
  2113. hsai->pBuffPtr++;
  2114. temp |= ((uint32_t)(*hsai->pBuffPtr) << 24);
  2115. hsai->pBuffPtr++;
  2116. hsai->Instance->DR = temp;
  2117. }
  2118. hsai->XferCount--;
  2119. }
  2120. }
  2121. /**
  2122. * @brief Return the interrupt flag to set according the SAI setup.
  2123. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2124. * the configuration information for SAI module.
  2125. * @param mode SAI_MODE_DMA or SAI_MODE_IT
  2126. * @retval the list of the IT flag to enable
  2127. */
  2128. static uint32_t SAI_InterruptFlag(const SAI_HandleTypeDef *hsai, SAI_ModeTypedef mode)
  2129. {
  2130. uint32_t tmpIT = SAI_IT_OVRUDR;
  2131. if (mode == SAI_MODE_IT)
  2132. {
  2133. tmpIT |= SAI_IT_FREQ;
  2134. }
  2135. if ((hsai->Init.Protocol == SAI_AC97_PROTOCOL) &&
  2136. ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODEMASTER_RX)))
  2137. {
  2138. tmpIT |= SAI_IT_CNRDY;
  2139. }
  2140. if ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
  2141. {
  2142. tmpIT |= SAI_IT_AFSDET | SAI_IT_LFSDET;
  2143. }
  2144. else
  2145. {
  2146. /* hsai has been configured in master mode */
  2147. tmpIT |= SAI_IT_WCKCFG;
  2148. }
  2149. return tmpIT;
  2150. }
  2151. /**
  2152. * @brief Disable the SAI and wait for the disabling.
  2153. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2154. * the configuration information for SAI module.
  2155. * @retval None
  2156. */
  2157. static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai)
  2158. {
  2159. uint32_t count = SAI_DEFAULT_TIMEOUT * (SystemCoreClock / 7U / 1000U);
  2160. HAL_StatusTypeDef status = HAL_OK;
  2161. /* Disable the SAI instance */
  2162. __HAL_SAI_DISABLE(hsai);
  2163. do
  2164. {
  2165. /* Check for the Timeout */
  2166. if (count == 0U)
  2167. {
  2168. /* Update error code */
  2169. hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
  2170. status = HAL_TIMEOUT;
  2171. break;
  2172. }
  2173. count--;
  2174. }
  2175. while ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != 0U);
  2176. return status;
  2177. }
  2178. /**
  2179. * @brief Tx Handler for Transmit in Interrupt mode 8-Bit transfer.
  2180. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2181. * the configuration information for SAI module.
  2182. * @retval None
  2183. */
  2184. static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai)
  2185. {
  2186. if (hsai->XferCount == 0U)
  2187. {
  2188. /* Handle the end of the transmission */
  2189. /* Disable FREQ and OVRUDR interrupts */
  2190. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2191. hsai->State = HAL_SAI_STATE_READY;
  2192. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2193. hsai->TxCpltCallback(hsai);
  2194. #else
  2195. HAL_SAI_TxCpltCallback(hsai);
  2196. #endif
  2197. }
  2198. else
  2199. {
  2200. /* Write data on DR register */
  2201. hsai->Instance->DR = *hsai->pBuffPtr;
  2202. hsai->pBuffPtr++;
  2203. hsai->XferCount--;
  2204. }
  2205. }
  2206. /**
  2207. * @brief Tx Handler for Transmit in Interrupt mode for 16-Bit transfer.
  2208. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2209. * the configuration information for SAI module.
  2210. * @retval None
  2211. */
  2212. static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai)
  2213. {
  2214. if (hsai->XferCount == 0U)
  2215. {
  2216. /* Handle the end of the transmission */
  2217. /* Disable FREQ and OVRUDR interrupts */
  2218. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2219. hsai->State = HAL_SAI_STATE_READY;
  2220. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2221. hsai->TxCpltCallback(hsai);
  2222. #else
  2223. HAL_SAI_TxCpltCallback(hsai);
  2224. #endif
  2225. }
  2226. else
  2227. {
  2228. /* Write data on DR register */
  2229. uint32_t temp;
  2230. temp = (uint32_t)(*hsai->pBuffPtr);
  2231. hsai->pBuffPtr++;
  2232. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  2233. hsai->pBuffPtr++;
  2234. hsai->Instance->DR = temp;
  2235. hsai->XferCount--;
  2236. }
  2237. }
  2238. /**
  2239. * @brief Tx Handler for Transmit in Interrupt mode for 32-Bit transfer.
  2240. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2241. * the configuration information for SAI module.
  2242. * @retval None
  2243. */
  2244. static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai)
  2245. {
  2246. if (hsai->XferCount == 0U)
  2247. {
  2248. /* Handle the end of the transmission */
  2249. /* Disable FREQ and OVRUDR interrupts */
  2250. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2251. hsai->State = HAL_SAI_STATE_READY;
  2252. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2253. hsai->TxCpltCallback(hsai);
  2254. #else
  2255. HAL_SAI_TxCpltCallback(hsai);
  2256. #endif
  2257. }
  2258. else
  2259. {
  2260. /* Write data on DR register */
  2261. uint32_t temp;
  2262. temp = (uint32_t)(*hsai->pBuffPtr);
  2263. hsai->pBuffPtr++;
  2264. temp |= ((uint32_t)(*hsai->pBuffPtr) << 8);
  2265. hsai->pBuffPtr++;
  2266. temp |= ((uint32_t)(*hsai->pBuffPtr) << 16);
  2267. hsai->pBuffPtr++;
  2268. temp |= ((uint32_t)(*hsai->pBuffPtr) << 24);
  2269. hsai->pBuffPtr++;
  2270. hsai->Instance->DR = temp;
  2271. hsai->XferCount--;
  2272. }
  2273. }
  2274. /**
  2275. * @brief Rx Handler for Receive in Interrupt mode 8-Bit transfer.
  2276. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2277. * the configuration information for SAI module.
  2278. * @retval None
  2279. */
  2280. static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai)
  2281. {
  2282. /* Receive data */
  2283. *hsai->pBuffPtr = (uint8_t)hsai->Instance->DR;
  2284. hsai->pBuffPtr++;
  2285. hsai->XferCount--;
  2286. /* Check end of the transfer */
  2287. if (hsai->XferCount == 0U)
  2288. {
  2289. /* Disable TXE and OVRUDR interrupts */
  2290. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2291. /* Clear the SAI Overrun flag */
  2292. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  2293. hsai->State = HAL_SAI_STATE_READY;
  2294. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2295. hsai->RxCpltCallback(hsai);
  2296. #else
  2297. HAL_SAI_RxCpltCallback(hsai);
  2298. #endif
  2299. }
  2300. }
  2301. /**
  2302. * @brief Rx Handler for Receive in Interrupt mode for 16-Bit transfer.
  2303. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2304. * the configuration information for SAI module.
  2305. * @retval None
  2306. */
  2307. static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai)
  2308. {
  2309. uint32_t temp;
  2310. /* Receive data */
  2311. temp = hsai->Instance->DR;
  2312. *hsai->pBuffPtr = (uint8_t)temp;
  2313. hsai->pBuffPtr++;
  2314. *hsai->pBuffPtr = (uint8_t)(temp >> 8);
  2315. hsai->pBuffPtr++;
  2316. hsai->XferCount--;
  2317. /* Check end of the transfer */
  2318. if (hsai->XferCount == 0U)
  2319. {
  2320. /* Disable TXE and OVRUDR interrupts */
  2321. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2322. /* Clear the SAI Overrun flag */
  2323. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  2324. hsai->State = HAL_SAI_STATE_READY;
  2325. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2326. hsai->RxCpltCallback(hsai);
  2327. #else
  2328. HAL_SAI_RxCpltCallback(hsai);
  2329. #endif
  2330. }
  2331. }
  2332. /**
  2333. * @brief Rx Handler for Receive in Interrupt mode for 32-Bit transfer.
  2334. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  2335. * the configuration information for SAI module.
  2336. * @retval None
  2337. */
  2338. static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai)
  2339. {
  2340. uint32_t temp;
  2341. /* Receive data */
  2342. temp = hsai->Instance->DR;
  2343. *hsai->pBuffPtr = (uint8_t)temp;
  2344. hsai->pBuffPtr++;
  2345. *hsai->pBuffPtr = (uint8_t)(temp >> 8);
  2346. hsai->pBuffPtr++;
  2347. *hsai->pBuffPtr = (uint8_t)(temp >> 16);
  2348. hsai->pBuffPtr++;
  2349. *hsai->pBuffPtr = (uint8_t)(temp >> 24);
  2350. hsai->pBuffPtr++;
  2351. hsai->XferCount--;
  2352. /* Check end of the transfer */
  2353. if (hsai->XferCount == 0U)
  2354. {
  2355. /* Disable TXE and OVRUDR interrupts */
  2356. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
  2357. /* Clear the SAI Overrun flag */
  2358. __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
  2359. hsai->State = HAL_SAI_STATE_READY;
  2360. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2361. hsai->RxCpltCallback(hsai);
  2362. #else
  2363. HAL_SAI_RxCpltCallback(hsai);
  2364. #endif
  2365. }
  2366. }
  2367. /**
  2368. * @brief DMA SAI transmit process complete callback.
  2369. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2370. * the configuration information for the specified DMA module.
  2371. * @retval None
  2372. */
  2373. static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma)
  2374. {
  2375. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2376. if (hdma->Init.Mode != DMA_CIRCULAR)
  2377. {
  2378. hsai->XferCount = 0;
  2379. /* Disable SAI Tx DMA Request */
  2380. hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
  2381. /* Stop the interrupts error handling */
  2382. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  2383. hsai->State = HAL_SAI_STATE_READY;
  2384. }
  2385. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2386. hsai->TxCpltCallback(hsai);
  2387. #else
  2388. HAL_SAI_TxCpltCallback(hsai);
  2389. #endif
  2390. }
  2391. /**
  2392. * @brief DMA SAI transmit process half complete callback.
  2393. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2394. * the configuration information for the specified DMA module.
  2395. * @retval None
  2396. */
  2397. static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2398. {
  2399. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2400. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2401. hsai->TxHalfCpltCallback(hsai);
  2402. #else
  2403. HAL_SAI_TxHalfCpltCallback(hsai);
  2404. #endif
  2405. }
  2406. /**
  2407. * @brief DMA SAI receive process complete callback.
  2408. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2409. * the configuration information for the specified DMA module.
  2410. * @retval None
  2411. */
  2412. static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma)
  2413. {
  2414. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2415. if (hdma->Init.Mode != DMA_CIRCULAR)
  2416. {
  2417. /* Disable Rx DMA Request */
  2418. hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
  2419. hsai->XferCount = 0;
  2420. /* Stop the interrupts error handling */
  2421. __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
  2422. hsai->State = HAL_SAI_STATE_READY;
  2423. }
  2424. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2425. hsai->RxCpltCallback(hsai);
  2426. #else
  2427. HAL_SAI_RxCpltCallback(hsai);
  2428. #endif
  2429. }
  2430. /**
  2431. * @brief DMA SAI receive process half complete callback
  2432. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2433. * the configuration information for the specified DMA module.
  2434. * @retval None
  2435. */
  2436. static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2437. {
  2438. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2439. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2440. hsai->RxHalfCpltCallback(hsai);
  2441. #else
  2442. HAL_SAI_RxHalfCpltCallback(hsai);
  2443. #endif
  2444. }
  2445. /**
  2446. * @brief DMA SAI communication error callback.
  2447. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2448. * the configuration information for the specified DMA module.
  2449. * @retval None
  2450. */
  2451. static void SAI_DMAError(DMA_HandleTypeDef *hdma)
  2452. {
  2453. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2454. /* Set SAI error code */
  2455. hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
  2456. /* Disable the SAI DMA request */
  2457. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  2458. /* Disable SAI peripheral */
  2459. /* No need to check return value because state will be updated and HAL_SAI_ErrorCallback will be called later */
  2460. (void) SAI_Disable(hsai);
  2461. /* Set the SAI state ready to be able to start again the process */
  2462. hsai->State = HAL_SAI_STATE_READY;
  2463. /* Initialize XferCount */
  2464. hsai->XferCount = 0U;
  2465. /* SAI error Callback */
  2466. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2467. hsai->ErrorCallback(hsai);
  2468. #else
  2469. HAL_SAI_ErrorCallback(hsai);
  2470. #endif
  2471. }
  2472. /**
  2473. * @brief DMA SAI Abort callback.
  2474. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2475. * the configuration information for the specified DMA module.
  2476. * @retval None
  2477. */
  2478. static void SAI_DMAAbort(DMA_HandleTypeDef *hdma)
  2479. {
  2480. SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2481. /* Disable DMA request */
  2482. hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
  2483. /* Disable all interrupts and clear all flags */
  2484. hsai->Instance->IMR = 0U;
  2485. hsai->Instance->CLRFR = 0xFFFFFFFFU;
  2486. if (hsai->ErrorCode != HAL_SAI_ERROR_WCKCFG)
  2487. {
  2488. /* Disable SAI peripheral */
  2489. /* No need to check return value because state will be updated and HAL_SAI_ErrorCallback will be called later */
  2490. (void) SAI_Disable(hsai);
  2491. /* Flush the fifo */
  2492. SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
  2493. }
  2494. /* Set the SAI state to ready to be able to start again the process */
  2495. hsai->State = HAL_SAI_STATE_READY;
  2496. /* Initialize XferCount */
  2497. hsai->XferCount = 0U;
  2498. /* SAI error Callback */
  2499. #if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
  2500. hsai->ErrorCallback(hsai);
  2501. #else
  2502. HAL_SAI_ErrorCallback(hsai);
  2503. #endif
  2504. }
  2505. /**
  2506. * @}
  2507. */
  2508. /**
  2509. * @}
  2510. */
  2511. #endif /* !STM32L412xx && !STM32L422xx */
  2512. #endif /* HAL_SAI_MODULE_ENABLED */
  2513. /**
  2514. * @}
  2515. */