stm32f4xx_hal_sai.c 79 KB

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