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.
 
 
 

2747 lines
85 KiB

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