stm32l4xx_hal_sai.c 76 KB

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