Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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