stm32f4xx_hal_sai.c 80 KB

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