stm32f4xx_hal_sai.c 61 KB

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