stm32g4xx_hal_sai.c 86 KB

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