stm32l4xx_hal_sai.c 90 KB

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