stm32f4xx_hal_sai.c 67 KB

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