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