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