stm32l4xx_hal_sai.c 69 KB

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