训练营PLSR题目
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2555 lines
80 KiB

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