No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

2621 líneas
82 KiB

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