You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2921 lines
90 KiB

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