Mbed for VNG board

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Nov 07 15:45:07 2014 +0000
Revision:
394:83f921546702
Parent:
targets/cmsis/TARGET_STM/TARGET_NUCLEO_L152RE/stm32l1xx_hal_i2s.c@354:e67efb2aab0e
Synchronized with git revision aab52cb7ec5a665869e507dd988bbfd55b7e087e

Full URL: https://github.com/mbedmicro/mbed/commit/aab52cb7ec5a665869e507dd988bbfd55b7e087e/

Tests: Fix cpputest testrunner

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 354:e67efb2aab0e 1 /**
mbed_official 354:e67efb2aab0e 2 ******************************************************************************
mbed_official 354:e67efb2aab0e 3 * @file stm32l1xx_hal_i2s.c
mbed_official 354:e67efb2aab0e 4 * @author MCD Application Team
mbed_official 354:e67efb2aab0e 5 * @version V1.0.0
mbed_official 354:e67efb2aab0e 6 * @date 5-September-2014
mbed_official 354:e67efb2aab0e 7 * @brief I2S HAL module driver.
mbed_official 354:e67efb2aab0e 8 * This file provides firmware functions to manage the following
mbed_official 354:e67efb2aab0e 9 * functionalities of the Integrated Interchip Sound (I2S) peripheral:
mbed_official 354:e67efb2aab0e 10 * + Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 11 * + IO operation functions
mbed_official 354:e67efb2aab0e 12 * + Peripheral State and Errors functions
mbed_official 354:e67efb2aab0e 13 @verbatim
mbed_official 354:e67efb2aab0e 14 ===============================================================================
mbed_official 354:e67efb2aab0e 15 ##### How to use this driver #####
mbed_official 354:e67efb2aab0e 16 ===============================================================================
mbed_official 354:e67efb2aab0e 17 [..]
mbed_official 354:e67efb2aab0e 18 The I2S HAL driver can be used as follow:
mbed_official 354:e67efb2aab0e 19
mbed_official 354:e67efb2aab0e 20 (#) Declare a I2S_HandleTypeDef handle structure.
mbed_official 354:e67efb2aab0e 21 (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:
mbed_official 354:e67efb2aab0e 22 (##) Enable the SPIx interface clock.
mbed_official 354:e67efb2aab0e 23 (##) I2S pins configuration:
mbed_official 354:e67efb2aab0e 24 (+++) Enable the clock for the I2S GPIOs.
mbed_official 354:e67efb2aab0e 25 (+++) Configure these I2S pins as alternate function.
mbed_official 354:e67efb2aab0e 26 (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()
mbed_official 354:e67efb2aab0e 27 and HAL_I2S_Receive_IT() APIs).
mbed_official 354:e67efb2aab0e 28 (+++) Configure the I2Sx interrupt priority.
mbed_official 354:e67efb2aab0e 29 (+++) Enable the NVIC I2S IRQ handle.
mbed_official 354:e67efb2aab0e 30 (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()
mbed_official 354:e67efb2aab0e 31 and HAL_I2S_Receive_DMA() APIs:
mbed_official 354:e67efb2aab0e 32 (+++) Declare a DMA handle structure for the Tx/Rx Channel.
mbed_official 354:e67efb2aab0e 33 (+++) Enable the DMAx interface clock.
mbed_official 354:e67efb2aab0e 34 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
mbed_official 354:e67efb2aab0e 35 (+++) Configure the DMA Tx/Rx Channel.
mbed_official 354:e67efb2aab0e 36 (+++) Associate the initilalized DMA handle to the I2S DMA Tx/Rx handle.
mbed_official 354:e67efb2aab0e 37 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
mbed_official 354:e67efb2aab0e 38 DMA Tx/Rx Channel.
mbed_official 354:e67efb2aab0e 39
mbed_official 354:e67efb2aab0e 40 (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
mbed_official 354:e67efb2aab0e 41 using HAL_I2S_Init() function.
mbed_official 354:e67efb2aab0e 42
mbed_official 354:e67efb2aab0e 43 -@- The specific I2S interrupts (Transmission complete interrupt,
mbed_official 354:e67efb2aab0e 44 RXNE interrupt and Error Interrupts) will be managed using the macros
mbed_official 354:e67efb2aab0e 45 __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
mbed_official 354:e67efb2aab0e 46 -@- Make sure that either:
mbed_official 354:e67efb2aab0e 47 (+@) External clock source is configured after setting correctly
mbed_official 354:e67efb2aab0e 48 the define constant HSE_VALUE in the stm32l1xx_hal_conf.h file.
mbed_official 354:e67efb2aab0e 49
mbed_official 354:e67efb2aab0e 50 (#) Three mode of operations are available within this driver :
mbed_official 354:e67efb2aab0e 51
mbed_official 354:e67efb2aab0e 52 *** Polling mode IO operation ***
mbed_official 354:e67efb2aab0e 53 =================================
mbed_official 354:e67efb2aab0e 54 [..]
mbed_official 354:e67efb2aab0e 55 (+) Send an amount of data in blocking mode using HAL_I2S_Transmit()
mbed_official 354:e67efb2aab0e 56 (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
mbed_official 354:e67efb2aab0e 57
mbed_official 354:e67efb2aab0e 58 *** Interrupt mode IO operation ***
mbed_official 354:e67efb2aab0e 59 ===================================
mbed_official 354:e67efb2aab0e 60 [..]
mbed_official 354:e67efb2aab0e 61 (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT()
mbed_official 354:e67efb2aab0e 62 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 63 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
mbed_official 354:e67efb2aab0e 64 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 65 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
mbed_official 354:e67efb2aab0e 66 (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT()
mbed_official 354:e67efb2aab0e 67 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 68 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
mbed_official 354:e67efb2aab0e 69 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 70 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
mbed_official 354:e67efb2aab0e 71 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
mbed_official 354:e67efb2aab0e 72 add his own code by customization of function pointer HAL_I2S_ErrorCallback
mbed_official 354:e67efb2aab0e 73
mbed_official 354:e67efb2aab0e 74 *** DMA mode IO operation ***
mbed_official 354:e67efb2aab0e 75 ==============================
mbed_official 354:e67efb2aab0e 76 [..]
mbed_official 354:e67efb2aab0e 77 (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA()
mbed_official 354:e67efb2aab0e 78 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 79 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
mbed_official 354:e67efb2aab0e 80 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 81 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
mbed_official 354:e67efb2aab0e 82 (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA()
mbed_official 354:e67efb2aab0e 83 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 84 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
mbed_official 354:e67efb2aab0e 85 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
mbed_official 354:e67efb2aab0e 86 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
mbed_official 354:e67efb2aab0e 87 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
mbed_official 354:e67efb2aab0e 88 add his own code by customization of function pointer HAL_I2S_ErrorCallback
mbed_official 354:e67efb2aab0e 89 (+) Pause the DMA Transfer using HAL_I2S_DMAPause()
mbed_official 354:e67efb2aab0e 90 (+) Resume the DMA Transfer using HAL_I2S_DMAResume()
mbed_official 354:e67efb2aab0e 91 (+) Stop the DMA Transfer using HAL_I2S_DMAStop()
mbed_official 354:e67efb2aab0e 92
mbed_official 354:e67efb2aab0e 93 *** I2S HAL driver macros list ***
mbed_official 354:e67efb2aab0e 94 =============================================
mbed_official 354:e67efb2aab0e 95 [..]
mbed_official 354:e67efb2aab0e 96 Below the list of most used macros in USART HAL driver.
mbed_official 354:e67efb2aab0e 97
mbed_official 354:e67efb2aab0e 98 (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode)
mbed_official 354:e67efb2aab0e 99 (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)
mbed_official 354:e67efb2aab0e 100 (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
mbed_official 354:e67efb2aab0e 101 (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
mbed_official 354:e67efb2aab0e 102 (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
mbed_official 354:e67efb2aab0e 103
mbed_official 354:e67efb2aab0e 104 [..]
mbed_official 354:e67efb2aab0e 105 (@) You can refer to the I2S HAL driver header file for more useful macros
mbed_official 354:e67efb2aab0e 106
mbed_official 354:e67efb2aab0e 107 @endverbatim
mbed_official 354:e67efb2aab0e 108 ******************************************************************************
mbed_official 354:e67efb2aab0e 109 * @attention
mbed_official 354:e67efb2aab0e 110 *
mbed_official 354:e67efb2aab0e 111 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 354:e67efb2aab0e 112 *
mbed_official 354:e67efb2aab0e 113 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 354:e67efb2aab0e 114 * are permitted provided that the following conditions are met:
mbed_official 354:e67efb2aab0e 115 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 354:e67efb2aab0e 116 * this list of conditions and the following disclaimer.
mbed_official 354:e67efb2aab0e 117 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 354:e67efb2aab0e 118 * this list of conditions and the following disclaimer in the documentation
mbed_official 354:e67efb2aab0e 119 * and/or other materials provided with the distribution.
mbed_official 354:e67efb2aab0e 120 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 354:e67efb2aab0e 121 * may be used to endorse or promote products derived from this software
mbed_official 354:e67efb2aab0e 122 * without specific prior written permission.
mbed_official 354:e67efb2aab0e 123 *
mbed_official 354:e67efb2aab0e 124 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 354:e67efb2aab0e 125 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 354:e67efb2aab0e 126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 354:e67efb2aab0e 127 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 354:e67efb2aab0e 128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 354:e67efb2aab0e 129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 354:e67efb2aab0e 130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 354:e67efb2aab0e 131 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 354:e67efb2aab0e 132 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 354:e67efb2aab0e 133 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 354:e67efb2aab0e 134 *
mbed_official 354:e67efb2aab0e 135 ******************************************************************************
mbed_official 354:e67efb2aab0e 136 */
mbed_official 354:e67efb2aab0e 137
mbed_official 354:e67efb2aab0e 138 /* Includes ------------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 139 #include "stm32l1xx_hal.h"
mbed_official 354:e67efb2aab0e 140
mbed_official 354:e67efb2aab0e 141 /** @addtogroup STM32L1xx_HAL_Driver
mbed_official 354:e67efb2aab0e 142 * @{
mbed_official 354:e67efb2aab0e 143 */
mbed_official 354:e67efb2aab0e 144
mbed_official 354:e67efb2aab0e 145 /** @defgroup I2S I2S
mbed_official 354:e67efb2aab0e 146 * @brief I2S HAL module driver
mbed_official 354:e67efb2aab0e 147 * @{
mbed_official 354:e67efb2aab0e 148 */
mbed_official 354:e67efb2aab0e 149
mbed_official 354:e67efb2aab0e 150 #ifdef HAL_I2S_MODULE_ENABLED
mbed_official 354:e67efb2aab0e 151 #if defined(STM32L100xC) || \
mbed_official 354:e67efb2aab0e 152 defined(STM32L151xC) || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xE) || \
mbed_official 354:e67efb2aab0e 153 defined(STM32L152xC) || defined(STM32L152xCA) || defined(STM32L152xD) || defined(STM32L152xE) || \
mbed_official 354:e67efb2aab0e 154 defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
mbed_official 354:e67efb2aab0e 155
mbed_official 354:e67efb2aab0e 156 /* Private typedef -----------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 157 /* Private define ------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 158 /* Private macro -------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 159 /* Private variables ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 160 /* Private function prototypes -----------------------------------------------*/
mbed_official 354:e67efb2aab0e 161 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 162 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 163 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 164 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 165 static void I2S_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 166 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
mbed_official 354:e67efb2aab0e 167 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
mbed_official 354:e67efb2aab0e 168 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t Status, uint32_t Timeout);
mbed_official 354:e67efb2aab0e 169
mbed_official 354:e67efb2aab0e 170 /* Private functions ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 171
mbed_official 354:e67efb2aab0e 172 /** @defgroup I2S_Exported_Functions I2S Exported Functions
mbed_official 354:e67efb2aab0e 173 * @{
mbed_official 354:e67efb2aab0e 174 */
mbed_official 354:e67efb2aab0e 175
mbed_official 354:e67efb2aab0e 176 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 177 * @brief Initialization and Configuration functions
mbed_official 354:e67efb2aab0e 178 *
mbed_official 354:e67efb2aab0e 179 @verbatim
mbed_official 354:e67efb2aab0e 180 ===============================================================================
mbed_official 354:e67efb2aab0e 181 ##### Initialization and de-initialization functions #####
mbed_official 354:e67efb2aab0e 182 ===============================================================================
mbed_official 354:e67efb2aab0e 183 [..] This subsection provides a set of functions allowing to initialize and
mbed_official 354:e67efb2aab0e 184 de-initialiaze the I2Sx peripheral in simplex mode:
mbed_official 354:e67efb2aab0e 185
mbed_official 354:e67efb2aab0e 186 (+) User must Implement HAL_I2S_MspInit() function in which he configures
mbed_official 354:e67efb2aab0e 187 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
mbed_official 354:e67efb2aab0e 188
mbed_official 354:e67efb2aab0e 189 (+) Call the function HAL_I2S_Init() to configure the selected device with
mbed_official 354:e67efb2aab0e 190 the selected configuration:
mbed_official 354:e67efb2aab0e 191 (++) Mode
mbed_official 354:e67efb2aab0e 192 (++) Standard
mbed_official 354:e67efb2aab0e 193 (++) Data Format
mbed_official 354:e67efb2aab0e 194 (++) MCLK Output
mbed_official 354:e67efb2aab0e 195 (++) Audio frequency
mbed_official 354:e67efb2aab0e 196 (++) Polarity
mbed_official 354:e67efb2aab0e 197
mbed_official 354:e67efb2aab0e 198 (+) Call the function HAL_I2S_DeInit() to restore the default configuration
mbed_official 354:e67efb2aab0e 199 of the selected I2Sx periperal.
mbed_official 354:e67efb2aab0e 200 @endverbatim
mbed_official 354:e67efb2aab0e 201 * @{
mbed_official 354:e67efb2aab0e 202 */
mbed_official 354:e67efb2aab0e 203
mbed_official 354:e67efb2aab0e 204 /**
mbed_official 354:e67efb2aab0e 205 * @brief Initializes the I2S according to the specified parameters
mbed_official 354:e67efb2aab0e 206 * in the I2S_InitTypeDef and create the associated handle.
mbed_official 354:e67efb2aab0e 207 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 208 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 209 * @retval HAL status
mbed_official 354:e67efb2aab0e 210 */
mbed_official 354:e67efb2aab0e 211 HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 212 {
mbed_official 354:e67efb2aab0e 213 uint32_t i2sdiv = 2, i2sodd = 0, packetlength = 1;
mbed_official 354:e67efb2aab0e 214 uint32_t tmp = 0, i2sclk = 0;
mbed_official 354:e67efb2aab0e 215
mbed_official 354:e67efb2aab0e 216 /* Check the I2S handle allocation */
mbed_official 354:e67efb2aab0e 217 if(hi2s == HAL_NULL)
mbed_official 354:e67efb2aab0e 218 {
mbed_official 354:e67efb2aab0e 219 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 220 }
mbed_official 354:e67efb2aab0e 221
mbed_official 354:e67efb2aab0e 222 /* Check the I2S parameters */
mbed_official 354:e67efb2aab0e 223 assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
mbed_official 354:e67efb2aab0e 224 assert_param(IS_I2S_MODE(hi2s->Init.Mode));
mbed_official 354:e67efb2aab0e 225 assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));
mbed_official 354:e67efb2aab0e 226 assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));
mbed_official 354:e67efb2aab0e 227 assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));
mbed_official 354:e67efb2aab0e 228 assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));
mbed_official 354:e67efb2aab0e 229 assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));
mbed_official 354:e67efb2aab0e 230
mbed_official 354:e67efb2aab0e 231 if(hi2s->State == HAL_I2S_STATE_RESET)
mbed_official 354:e67efb2aab0e 232 {
mbed_official 354:e67efb2aab0e 233 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
mbed_official 354:e67efb2aab0e 234 HAL_I2S_MspInit(hi2s);
mbed_official 354:e67efb2aab0e 235 }
mbed_official 354:e67efb2aab0e 236
mbed_official 354:e67efb2aab0e 237 hi2s->State = HAL_I2S_STATE_BUSY;
mbed_official 354:e67efb2aab0e 238
mbed_official 354:e67efb2aab0e 239 /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
mbed_official 354:e67efb2aab0e 240 if(hi2s->Init.AudioFreq == I2S_AUDIOFREQ_DEFAULT)
mbed_official 354:e67efb2aab0e 241 {
mbed_official 354:e67efb2aab0e 242 i2sodd = (uint32_t)0;
mbed_official 354:e67efb2aab0e 243 i2sdiv = (uint32_t)2;
mbed_official 354:e67efb2aab0e 244 }
mbed_official 354:e67efb2aab0e 245 /* If the requested audio frequency is not the default, compute the prescaler */
mbed_official 354:e67efb2aab0e 246 else
mbed_official 354:e67efb2aab0e 247 {
mbed_official 354:e67efb2aab0e 248 /* Check the frame length (For the Prescaler computing) *******************/
mbed_official 354:e67efb2aab0e 249 if(hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)
mbed_official 354:e67efb2aab0e 250 {
mbed_official 354:e67efb2aab0e 251 /* Packet length is 16 bits */
mbed_official 354:e67efb2aab0e 252 packetlength = 1;
mbed_official 354:e67efb2aab0e 253 }
mbed_official 354:e67efb2aab0e 254 else
mbed_official 354:e67efb2aab0e 255 {
mbed_official 354:e67efb2aab0e 256 /* Packet length is 32 bits */
mbed_official 354:e67efb2aab0e 257 packetlength = 2;
mbed_official 354:e67efb2aab0e 258 }
mbed_official 354:e67efb2aab0e 259
mbed_official 354:e67efb2aab0e 260 /* Get the source clock value: based on System Clock value */
mbed_official 354:e67efb2aab0e 261 i2sclk = HAL_RCC_GetSysClockFreq();
mbed_official 354:e67efb2aab0e 262
mbed_official 354:e67efb2aab0e 263 /* Compute the Real divider depending on the MCLK output state, with a floating point */
mbed_official 354:e67efb2aab0e 264 if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)
mbed_official 354:e67efb2aab0e 265 {
mbed_official 354:e67efb2aab0e 266 /* MCLK output is enabled */
mbed_official 354:e67efb2aab0e 267 tmp = (uint32_t)(((((i2sclk / 256) * 10) / hi2s->Init.AudioFreq)) + 5);
mbed_official 354:e67efb2aab0e 268 }
mbed_official 354:e67efb2aab0e 269 else
mbed_official 354:e67efb2aab0e 270 {
mbed_official 354:e67efb2aab0e 271 /* MCLK output is disabled */
mbed_official 354:e67efb2aab0e 272 tmp = (uint32_t)(((((i2sclk / (32 * packetlength)) *10 ) / hi2s->Init.AudioFreq)) + 5);
mbed_official 354:e67efb2aab0e 273 }
mbed_official 354:e67efb2aab0e 274
mbed_official 354:e67efb2aab0e 275 /* Remove the flatting point */
mbed_official 354:e67efb2aab0e 276 tmp = tmp / 10;
mbed_official 354:e67efb2aab0e 277
mbed_official 354:e67efb2aab0e 278 /* Check the parity of the divider */
mbed_official 354:e67efb2aab0e 279 i2sodd = (uint32_t)(tmp & (uint32_t)1);
mbed_official 354:e67efb2aab0e 280
mbed_official 354:e67efb2aab0e 281 /* Compute the i2sdiv prescaler */
mbed_official 354:e67efb2aab0e 282 i2sdiv = (uint32_t)((tmp - i2sodd) / 2);
mbed_official 354:e67efb2aab0e 283
mbed_official 354:e67efb2aab0e 284 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
mbed_official 354:e67efb2aab0e 285 i2sodd = (uint32_t) (i2sodd << 8);
mbed_official 354:e67efb2aab0e 286 }
mbed_official 354:e67efb2aab0e 287
mbed_official 354:e67efb2aab0e 288 /* Test if the divider is 1 or 0 or greater than 0xFF */
mbed_official 354:e67efb2aab0e 289 if((i2sdiv < 2) || (i2sdiv > 0xFF))
mbed_official 354:e67efb2aab0e 290 {
mbed_official 354:e67efb2aab0e 291 /* Set the default values */
mbed_official 354:e67efb2aab0e 292 i2sdiv = 2;
mbed_official 354:e67efb2aab0e 293 i2sodd = 0;
mbed_official 354:e67efb2aab0e 294 }
mbed_official 354:e67efb2aab0e 295
mbed_official 354:e67efb2aab0e 296 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
mbed_official 354:e67efb2aab0e 297 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
mbed_official 354:e67efb2aab0e 298 /* And configure the I2S with the I2S_InitStruct values */
mbed_official 354:e67efb2aab0e 299 MODIFY_REG( hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN |\
mbed_official 354:e67efb2aab0e 300 SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD |\
mbed_official 354:e67efb2aab0e 301 SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG |\
mbed_official 354:e67efb2aab0e 302 SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD),\
mbed_official 354:e67efb2aab0e 303 (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode |\
mbed_official 354:e67efb2aab0e 304 hi2s->Init.Standard | hi2s->Init.DataFormat |\
mbed_official 354:e67efb2aab0e 305 hi2s->Init.CPOL));
mbed_official 354:e67efb2aab0e 306
mbed_official 354:e67efb2aab0e 307 /* Write to SPIx I2SPR register the computed value */
mbed_official 354:e67efb2aab0e 308 hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput));
mbed_official 354:e67efb2aab0e 309
mbed_official 354:e67efb2aab0e 310 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 311 hi2s->State= HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 312
mbed_official 354:e67efb2aab0e 313 return HAL_OK;
mbed_official 354:e67efb2aab0e 314 }
mbed_official 354:e67efb2aab0e 315
mbed_official 354:e67efb2aab0e 316 /**
mbed_official 354:e67efb2aab0e 317 * @brief DeInitializes the I2S peripheral
mbed_official 354:e67efb2aab0e 318 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 319 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 320 * @retval HAL status
mbed_official 354:e67efb2aab0e 321 */
mbed_official 354:e67efb2aab0e 322 HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 323 {
mbed_official 354:e67efb2aab0e 324 /* Check the I2S handle allocation */
mbed_official 354:e67efb2aab0e 325 if(hi2s == HAL_NULL)
mbed_official 354:e67efb2aab0e 326 {
mbed_official 354:e67efb2aab0e 327 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 328 }
mbed_official 354:e67efb2aab0e 329
mbed_official 354:e67efb2aab0e 330 hi2s->State = HAL_I2S_STATE_BUSY;
mbed_official 354:e67efb2aab0e 331
mbed_official 354:e67efb2aab0e 332 /* Disable the I2S Peripheral Clock */
mbed_official 354:e67efb2aab0e 333 __HAL_I2S_DISABLE(hi2s);
mbed_official 354:e67efb2aab0e 334
mbed_official 354:e67efb2aab0e 335 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
mbed_official 354:e67efb2aab0e 336 HAL_I2S_MspDeInit(hi2s);
mbed_official 354:e67efb2aab0e 337
mbed_official 354:e67efb2aab0e 338 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 339 hi2s->State = HAL_I2S_STATE_RESET;
mbed_official 354:e67efb2aab0e 340
mbed_official 354:e67efb2aab0e 341 /* Release Lock */
mbed_official 354:e67efb2aab0e 342 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 343
mbed_official 354:e67efb2aab0e 344 return HAL_OK;
mbed_official 354:e67efb2aab0e 345 }
mbed_official 354:e67efb2aab0e 346
mbed_official 354:e67efb2aab0e 347 /**
mbed_official 354:e67efb2aab0e 348 * @brief I2S MSP Init
mbed_official 354:e67efb2aab0e 349 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 350 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 351 * @retval None
mbed_official 354:e67efb2aab0e 352 */
mbed_official 354:e67efb2aab0e 353 __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 354 {
mbed_official 354:e67efb2aab0e 355 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 356 the HAL_I2S_MspInit could be implemented in the user file
mbed_official 354:e67efb2aab0e 357 */
mbed_official 354:e67efb2aab0e 358 }
mbed_official 354:e67efb2aab0e 359
mbed_official 354:e67efb2aab0e 360 /**
mbed_official 354:e67efb2aab0e 361 * @brief I2S MSP DeInit
mbed_official 354:e67efb2aab0e 362 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 363 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 364 * @retval None
mbed_official 354:e67efb2aab0e 365 */
mbed_official 354:e67efb2aab0e 366 __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 367 {
mbed_official 354:e67efb2aab0e 368 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 369 the HAL_I2S_MspDeInit could be implemented in the user file
mbed_official 354:e67efb2aab0e 370 */
mbed_official 354:e67efb2aab0e 371 }
mbed_official 354:e67efb2aab0e 372
mbed_official 354:e67efb2aab0e 373 /**
mbed_official 354:e67efb2aab0e 374 * @}
mbed_official 354:e67efb2aab0e 375 */
mbed_official 354:e67efb2aab0e 376
mbed_official 354:e67efb2aab0e 377 /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
mbed_official 354:e67efb2aab0e 378 * @brief Data transfers functions
mbed_official 354:e67efb2aab0e 379 *
mbed_official 354:e67efb2aab0e 380 @verbatim
mbed_official 354:e67efb2aab0e 381 ===============================================================================
mbed_official 354:e67efb2aab0e 382 ##### IO operation functions #####
mbed_official 354:e67efb2aab0e 383 ===============================================================================
mbed_official 354:e67efb2aab0e 384 [..]
mbed_official 354:e67efb2aab0e 385 This subsection provides a set of functions allowing to manage the I2S data
mbed_official 354:e67efb2aab0e 386 transfers.
mbed_official 354:e67efb2aab0e 387
mbed_official 354:e67efb2aab0e 388 (#) There are two modes of transfer:
mbed_official 354:e67efb2aab0e 389 (++) Blocking mode : The communication is performed in the polling mode.
mbed_official 354:e67efb2aab0e 390 The status of all data processing is returned by the same function
mbed_official 354:e67efb2aab0e 391 after finishing transfer.
mbed_official 354:e67efb2aab0e 392 (++) No-Blocking mode : The communication is performed using Interrupts
mbed_official 354:e67efb2aab0e 393 or DMA. These functions return the status of the transfer startup.
mbed_official 354:e67efb2aab0e 394 The end of the data processing will be indicated through the
mbed_official 354:e67efb2aab0e 395 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
mbed_official 354:e67efb2aab0e 396 using DMA mode.
mbed_official 354:e67efb2aab0e 397
mbed_official 354:e67efb2aab0e 398 (#) Blocking mode functions are :
mbed_official 354:e67efb2aab0e 399 (++) HAL_I2S_Transmit()
mbed_official 354:e67efb2aab0e 400 (++) HAL_I2S_Receive()
mbed_official 354:e67efb2aab0e 401
mbed_official 354:e67efb2aab0e 402 (#) No-Blocking mode functions with Interrupt are :
mbed_official 354:e67efb2aab0e 403 (++) HAL_I2S_Transmit_IT()
mbed_official 354:e67efb2aab0e 404 (++) HAL_I2S_Receive_IT()
mbed_official 354:e67efb2aab0e 405
mbed_official 354:e67efb2aab0e 406 (#) No-Blocking mode functions with DMA are :
mbed_official 354:e67efb2aab0e 407 (++) HAL_I2S_Transmit_DMA()
mbed_official 354:e67efb2aab0e 408 (++) HAL_I2S_Receive_DMA()
mbed_official 354:e67efb2aab0e 409
mbed_official 354:e67efb2aab0e 410 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
mbed_official 354:e67efb2aab0e 411 (++) HAL_I2S_TxCpltCallback()
mbed_official 354:e67efb2aab0e 412 (++) HAL_I2S_RxCpltCallback()
mbed_official 354:e67efb2aab0e 413 (++) HAL_I2S_ErrorCallback()
mbed_official 354:e67efb2aab0e 414
mbed_official 354:e67efb2aab0e 415 @endverbatim
mbed_official 354:e67efb2aab0e 416 * @{
mbed_official 354:e67efb2aab0e 417 */
mbed_official 354:e67efb2aab0e 418
mbed_official 354:e67efb2aab0e 419 /**
mbed_official 354:e67efb2aab0e 420 * @brief Transmit an amount of data in blocking mode
mbed_official 354:e67efb2aab0e 421 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 422 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 423 * @param pData: a 16-bit pointer to data buffer.
mbed_official 354:e67efb2aab0e 424 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 425 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 426 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 427 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 428 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 429 * @param Timeout: Timeout duration
mbed_official 354:e67efb2aab0e 430 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 431 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 432 * @retval HAL status
mbed_official 354:e67efb2aab0e 433 */
mbed_official 354:e67efb2aab0e 434 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 435 {
mbed_official 354:e67efb2aab0e 436 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 354:e67efb2aab0e 437 {
mbed_official 354:e67efb2aab0e 438 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 439 }
mbed_official 354:e67efb2aab0e 440
mbed_official 354:e67efb2aab0e 441 /* Process Locked */
mbed_official 354:e67efb2aab0e 442 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 443
mbed_official 354:e67efb2aab0e 444 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 445 {
mbed_official 354:e67efb2aab0e 446 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 447 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 448 {
mbed_official 354:e67efb2aab0e 449 hi2s->TxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 450 hi2s->TxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 451 }
mbed_official 354:e67efb2aab0e 452 else
mbed_official 354:e67efb2aab0e 453 {
mbed_official 354:e67efb2aab0e 454 hi2s->TxXferSize = Size;
mbed_official 354:e67efb2aab0e 455 hi2s->TxXferCount = Size;
mbed_official 354:e67efb2aab0e 456 }
mbed_official 354:e67efb2aab0e 457
mbed_official 354:e67efb2aab0e 458 /* Set state and reset error code */
mbed_official 354:e67efb2aab0e 459 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 460 hi2s->State = HAL_I2S_STATE_BUSY_TX;
mbed_official 354:e67efb2aab0e 461 hi2s->pTxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 462
mbed_official 354:e67efb2aab0e 463 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 464 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 465 {
mbed_official 354:e67efb2aab0e 466 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 467 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 468 }
mbed_official 354:e67efb2aab0e 469
mbed_official 354:e67efb2aab0e 470 while(hi2s->TxXferCount > 0)
mbed_official 354:e67efb2aab0e 471 {
mbed_official 354:e67efb2aab0e 472 /* Wait until TXE flag is set */
mbed_official 354:e67efb2aab0e 473 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, RESET, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 474 {
mbed_official 354:e67efb2aab0e 475 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 476 }
mbed_official 354:e67efb2aab0e 477 hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
mbed_official 354:e67efb2aab0e 478 hi2s->TxXferCount--;
mbed_official 354:e67efb2aab0e 479 }
mbed_official 354:e67efb2aab0e 480
mbed_official 354:e67efb2aab0e 481 /* Wait until TXE flag is set, to confirm the end of the transcation */
mbed_official 354:e67efb2aab0e 482 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, RESET, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 483 {
mbed_official 354:e67efb2aab0e 484 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 485 }
mbed_official 354:e67efb2aab0e 486 /* Wait until Busy flag is reset */
mbed_official 354:e67efb2aab0e 487 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, SET, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 488 {
mbed_official 354:e67efb2aab0e 489 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 490 }
mbed_official 354:e67efb2aab0e 491
mbed_official 354:e67efb2aab0e 492 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 493
mbed_official 354:e67efb2aab0e 494 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 495 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 496
mbed_official 354:e67efb2aab0e 497 return HAL_OK;
mbed_official 354:e67efb2aab0e 498 }
mbed_official 354:e67efb2aab0e 499 else
mbed_official 354:e67efb2aab0e 500 {
mbed_official 354:e67efb2aab0e 501 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 502 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 503 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 504 }
mbed_official 354:e67efb2aab0e 505 }
mbed_official 354:e67efb2aab0e 506
mbed_official 354:e67efb2aab0e 507 /**
mbed_official 354:e67efb2aab0e 508 * @brief Receive an amount of data in blocking mode
mbed_official 354:e67efb2aab0e 509 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 510 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 511 * @param pData: a 16-bit pointer to data buffer.
mbed_official 354:e67efb2aab0e 512 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 513 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 514 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 515 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 516 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 517 * @param Timeout: Timeout duration
mbed_official 354:e67efb2aab0e 518 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 519 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 520 * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
mbed_official 354:e67efb2aab0e 521 * in continouse way and as the I2S is not disabled at the end of the I2S transaction.
mbed_official 354:e67efb2aab0e 522 * @retval HAL status
mbed_official 354:e67efb2aab0e 523 */
mbed_official 354:e67efb2aab0e 524 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 525 {
mbed_official 354:e67efb2aab0e 526 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 354:e67efb2aab0e 527 {
mbed_official 354:e67efb2aab0e 528 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 529 }
mbed_official 354:e67efb2aab0e 530
mbed_official 354:e67efb2aab0e 531 /* Process Locked */
mbed_official 354:e67efb2aab0e 532 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 533
mbed_official 354:e67efb2aab0e 534 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 535 {
mbed_official 354:e67efb2aab0e 536 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 537 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 538 {
mbed_official 354:e67efb2aab0e 539 hi2s->RxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 540 hi2s->RxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 541 }
mbed_official 354:e67efb2aab0e 542 else
mbed_official 354:e67efb2aab0e 543 {
mbed_official 354:e67efb2aab0e 544 hi2s->RxXferSize = Size;
mbed_official 354:e67efb2aab0e 545 hi2s->RxXferCount = Size;
mbed_official 354:e67efb2aab0e 546 }
mbed_official 354:e67efb2aab0e 547
mbed_official 354:e67efb2aab0e 548 /* Set state and reset error code */
mbed_official 354:e67efb2aab0e 549 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 550 hi2s->State = HAL_I2S_STATE_BUSY_RX;
mbed_official 354:e67efb2aab0e 551 hi2s->pRxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 552
mbed_official 354:e67efb2aab0e 553 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 554 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 555 {
mbed_official 354:e67efb2aab0e 556 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 557 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 558 }
mbed_official 354:e67efb2aab0e 559
mbed_official 354:e67efb2aab0e 560 /* Receive data */
mbed_official 354:e67efb2aab0e 561 while(hi2s->RxXferCount > 0)
mbed_official 354:e67efb2aab0e 562 {
mbed_official 354:e67efb2aab0e 563 /* Wait until RXNE flag is set */
mbed_official 354:e67efb2aab0e 564 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, RESET, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 565 {
mbed_official 354:e67efb2aab0e 566 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 567 }
mbed_official 354:e67efb2aab0e 568
mbed_official 354:e67efb2aab0e 569 (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
mbed_official 354:e67efb2aab0e 570 hi2s->RxXferCount--;
mbed_official 354:e67efb2aab0e 571 }
mbed_official 354:e67efb2aab0e 572
mbed_official 354:e67efb2aab0e 573 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 574
mbed_official 354:e67efb2aab0e 575 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 576 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 577
mbed_official 354:e67efb2aab0e 578 return HAL_OK;
mbed_official 354:e67efb2aab0e 579 }
mbed_official 354:e67efb2aab0e 580 else
mbed_official 354:e67efb2aab0e 581 {
mbed_official 354:e67efb2aab0e 582 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 583 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 584 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 585 }
mbed_official 354:e67efb2aab0e 586 }
mbed_official 354:e67efb2aab0e 587
mbed_official 354:e67efb2aab0e 588 /**
mbed_official 354:e67efb2aab0e 589 * @brief Transmit an amount of data in non-blocking mode with Interrupt
mbed_official 354:e67efb2aab0e 590 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 591 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 592 * @param pData: a 16-bit pointer to data buffer.
mbed_official 354:e67efb2aab0e 593 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 594 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 595 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 596 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 597 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 598 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 599 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 600 * @retval HAL status
mbed_official 354:e67efb2aab0e 601 */
mbed_official 354:e67efb2aab0e 602 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
mbed_official 354:e67efb2aab0e 603 {
mbed_official 354:e67efb2aab0e 604 if((pData == HAL_NULL) || (Size == 0))
mbed_official 354:e67efb2aab0e 605 {
mbed_official 354:e67efb2aab0e 606 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 607 }
mbed_official 354:e67efb2aab0e 608
mbed_official 354:e67efb2aab0e 609 /* Process Locked */
mbed_official 354:e67efb2aab0e 610 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 611
mbed_official 354:e67efb2aab0e 612 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 613 {
mbed_official 354:e67efb2aab0e 614 hi2s->pTxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 615 hi2s->State = HAL_I2S_STATE_BUSY_TX;
mbed_official 354:e67efb2aab0e 616 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 617
mbed_official 354:e67efb2aab0e 618 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 619 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 620 {
mbed_official 354:e67efb2aab0e 621 hi2s->TxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 622 hi2s->TxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 623 }
mbed_official 354:e67efb2aab0e 624 else
mbed_official 354:e67efb2aab0e 625 {
mbed_official 354:e67efb2aab0e 626 hi2s->TxXferSize = Size;
mbed_official 354:e67efb2aab0e 627 hi2s->TxXferCount = Size;
mbed_official 354:e67efb2aab0e 628 }
mbed_official 354:e67efb2aab0e 629
mbed_official 354:e67efb2aab0e 630 /* Enable TXE and ERR interrupt */
mbed_official 354:e67efb2aab0e 631 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 632
mbed_official 354:e67efb2aab0e 633 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 634 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 635 {
mbed_official 354:e67efb2aab0e 636 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 637 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 638 }
mbed_official 354:e67efb2aab0e 639
mbed_official 354:e67efb2aab0e 640 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 641 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 642
mbed_official 354:e67efb2aab0e 643 return HAL_OK;
mbed_official 354:e67efb2aab0e 644 }
mbed_official 354:e67efb2aab0e 645 else
mbed_official 354:e67efb2aab0e 646 {
mbed_official 354:e67efb2aab0e 647 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 648 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 649 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 650 }
mbed_official 354:e67efb2aab0e 651 }
mbed_official 354:e67efb2aab0e 652
mbed_official 354:e67efb2aab0e 653 /**
mbed_official 354:e67efb2aab0e 654 * @brief Receive an amount of data in non-blocking mode with Interrupt
mbed_official 354:e67efb2aab0e 655 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 656 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 657 * @param pData: a 16-bit pointer to the Receive data buffer.
mbed_official 354:e67efb2aab0e 658 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 659 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 660 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 661 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 662 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 663 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 664 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 665 * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
mbed_official 354:e67efb2aab0e 666 * between Master and Slave otherwise the I2S interrupt should be optimized.
mbed_official 354:e67efb2aab0e 667 * @retval HAL status
mbed_official 354:e67efb2aab0e 668 */
mbed_official 354:e67efb2aab0e 669 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
mbed_official 354:e67efb2aab0e 670 {
mbed_official 354:e67efb2aab0e 671 if((pData == HAL_NULL) || (Size == 0))
mbed_official 354:e67efb2aab0e 672 {
mbed_official 354:e67efb2aab0e 673 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 674 }
mbed_official 354:e67efb2aab0e 675
mbed_official 354:e67efb2aab0e 676 /* Process Locked */
mbed_official 354:e67efb2aab0e 677 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 678
mbed_official 354:e67efb2aab0e 679 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 680 {
mbed_official 354:e67efb2aab0e 681 hi2s->pRxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 682 hi2s->State = HAL_I2S_STATE_BUSY_RX;
mbed_official 354:e67efb2aab0e 683 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 684
mbed_official 354:e67efb2aab0e 685 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 686 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 687 {
mbed_official 354:e67efb2aab0e 688 hi2s->RxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 689 hi2s->RxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 690 }
mbed_official 354:e67efb2aab0e 691 else
mbed_official 354:e67efb2aab0e 692 {
mbed_official 354:e67efb2aab0e 693 hi2s->RxXferSize = Size;
mbed_official 354:e67efb2aab0e 694 hi2s->RxXferCount = Size;
mbed_official 354:e67efb2aab0e 695 }
mbed_official 354:e67efb2aab0e 696
mbed_official 354:e67efb2aab0e 697 /* Enable TXE and ERR interrupt */
mbed_official 354:e67efb2aab0e 698 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 699
mbed_official 354:e67efb2aab0e 700 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 701 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 702 {
mbed_official 354:e67efb2aab0e 703 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 704 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 705 }
mbed_official 354:e67efb2aab0e 706
mbed_official 354:e67efb2aab0e 707 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 708 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 709
mbed_official 354:e67efb2aab0e 710 return HAL_OK;
mbed_official 354:e67efb2aab0e 711 }
mbed_official 354:e67efb2aab0e 712 else
mbed_official 354:e67efb2aab0e 713 {
mbed_official 354:e67efb2aab0e 714 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 715 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 716 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 717 }
mbed_official 354:e67efb2aab0e 718 }
mbed_official 354:e67efb2aab0e 719
mbed_official 354:e67efb2aab0e 720 /**
mbed_official 354:e67efb2aab0e 721 * @brief Transmit an amount of data in non-blocking mode with DMA
mbed_official 354:e67efb2aab0e 722 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 723 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 724 * @param pData: a 16-bit pointer to the Transmit data buffer.
mbed_official 354:e67efb2aab0e 725 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 726 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 727 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 728 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 729 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 730 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 731 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 732 * @retval HAL status
mbed_official 354:e67efb2aab0e 733 */
mbed_official 354:e67efb2aab0e 734 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
mbed_official 354:e67efb2aab0e 735 {
mbed_official 354:e67efb2aab0e 736 if((pData == HAL_NULL) || (Size == 0))
mbed_official 354:e67efb2aab0e 737 {
mbed_official 354:e67efb2aab0e 738 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 739 }
mbed_official 354:e67efb2aab0e 740
mbed_official 354:e67efb2aab0e 741 /* Process Locked */
mbed_official 354:e67efb2aab0e 742 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 743
mbed_official 354:e67efb2aab0e 744 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 745 {
mbed_official 354:e67efb2aab0e 746 hi2s->pTxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 747 hi2s->State = HAL_I2S_STATE_BUSY_TX;
mbed_official 354:e67efb2aab0e 748 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 749
mbed_official 354:e67efb2aab0e 750 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 751 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 752 {
mbed_official 354:e67efb2aab0e 753 hi2s->TxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 754 hi2s->TxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 755 }
mbed_official 354:e67efb2aab0e 756 else
mbed_official 354:e67efb2aab0e 757 {
mbed_official 354:e67efb2aab0e 758 hi2s->TxXferSize = Size;
mbed_official 354:e67efb2aab0e 759 hi2s->TxXferCount = Size;
mbed_official 354:e67efb2aab0e 760 }
mbed_official 354:e67efb2aab0e 761
mbed_official 354:e67efb2aab0e 762 /* Set the I2S Tx DMA Half transfert complete callback */
mbed_official 354:e67efb2aab0e 763 hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
mbed_official 354:e67efb2aab0e 764
mbed_official 354:e67efb2aab0e 765 /* Set the I2S Tx DMA transfert complete callback */
mbed_official 354:e67efb2aab0e 766 hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
mbed_official 354:e67efb2aab0e 767
mbed_official 354:e67efb2aab0e 768 /* Set the DMA error callback */
mbed_official 354:e67efb2aab0e 769 hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
mbed_official 354:e67efb2aab0e 770
mbed_official 354:e67efb2aab0e 771 /* Enable the Tx DMA Channel */
mbed_official 354:e67efb2aab0e 772 HAL_DMA_Start_IT(hi2s->hdmatx, (uint32_t)hi2s->pTxBuffPtr, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
mbed_official 354:e67efb2aab0e 773
mbed_official 354:e67efb2aab0e 774 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 775 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 776 {
mbed_official 354:e67efb2aab0e 777 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 778 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 779 }
mbed_official 354:e67efb2aab0e 780
mbed_official 354:e67efb2aab0e 781 /* Check if the I2S Tx request is already enabled */
mbed_official 354:e67efb2aab0e 782 if((hi2s->Instance->CR2 & SPI_CR2_TXDMAEN) != SPI_CR2_TXDMAEN)
mbed_official 354:e67efb2aab0e 783 {
mbed_official 354:e67efb2aab0e 784 /* Enable Tx DMA Request */
mbed_official 354:e67efb2aab0e 785 SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
mbed_official 354:e67efb2aab0e 786 }
mbed_official 354:e67efb2aab0e 787
mbed_official 354:e67efb2aab0e 788 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 789 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 790
mbed_official 354:e67efb2aab0e 791 return HAL_OK;
mbed_official 354:e67efb2aab0e 792 }
mbed_official 354:e67efb2aab0e 793 else
mbed_official 354:e67efb2aab0e 794 {
mbed_official 354:e67efb2aab0e 795 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 796 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 797 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 798 }
mbed_official 354:e67efb2aab0e 799 }
mbed_official 354:e67efb2aab0e 800
mbed_official 354:e67efb2aab0e 801 /**
mbed_official 354:e67efb2aab0e 802 * @brief Receive an amount of data in non-blocking mode with DMA
mbed_official 354:e67efb2aab0e 803 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 804 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 805 * @param pData: a 16-bit pointer to the Receive data buffer.
mbed_official 354:e67efb2aab0e 806 * @param Size: number of data sample to be sent:
mbed_official 354:e67efb2aab0e 807 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
mbed_official 354:e67efb2aab0e 808 * configuration phase, the Size parameter means the number of 16-bit data length
mbed_official 354:e67efb2aab0e 809 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
mbed_official 354:e67efb2aab0e 810 * the Size parameter means the number of 16-bit data length.
mbed_official 354:e67efb2aab0e 811 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
mbed_official 354:e67efb2aab0e 812 * between Master and Slave(example: audio streaming).
mbed_official 354:e67efb2aab0e 813 * @retval HAL status
mbed_official 354:e67efb2aab0e 814 */
mbed_official 354:e67efb2aab0e 815 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
mbed_official 354:e67efb2aab0e 816 {
mbed_official 354:e67efb2aab0e 817 if((pData == HAL_NULL) || (Size == 0))
mbed_official 354:e67efb2aab0e 818 {
mbed_official 354:e67efb2aab0e 819 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 820 }
mbed_official 354:e67efb2aab0e 821
mbed_official 354:e67efb2aab0e 822 /* Process Locked */
mbed_official 354:e67efb2aab0e 823 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 824
mbed_official 354:e67efb2aab0e 825 if(hi2s->State == HAL_I2S_STATE_READY)
mbed_official 354:e67efb2aab0e 826 {
mbed_official 354:e67efb2aab0e 827 hi2s->pRxBuffPtr = pData;
mbed_official 354:e67efb2aab0e 828 hi2s->State = HAL_I2S_STATE_BUSY_RX;
mbed_official 354:e67efb2aab0e 829 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
mbed_official 354:e67efb2aab0e 830
mbed_official 354:e67efb2aab0e 831 if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
mbed_official 354:e67efb2aab0e 832 ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
mbed_official 354:e67efb2aab0e 833 {
mbed_official 354:e67efb2aab0e 834 hi2s->RxXferSize = (Size << 1);
mbed_official 354:e67efb2aab0e 835 hi2s->RxXferCount = (Size << 1);
mbed_official 354:e67efb2aab0e 836 }
mbed_official 354:e67efb2aab0e 837 else
mbed_official 354:e67efb2aab0e 838 {
mbed_official 354:e67efb2aab0e 839 hi2s->RxXferSize = Size;
mbed_official 354:e67efb2aab0e 840 hi2s->RxXferCount = Size;
mbed_official 354:e67efb2aab0e 841 }
mbed_official 354:e67efb2aab0e 842
mbed_official 354:e67efb2aab0e 843
mbed_official 354:e67efb2aab0e 844 /* Set the I2S Rx DMA Half transfert complete callback */
mbed_official 354:e67efb2aab0e 845 hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
mbed_official 354:e67efb2aab0e 846
mbed_official 354:e67efb2aab0e 847 /* Set the I2S Rx DMA transfert complete callback */
mbed_official 354:e67efb2aab0e 848 hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
mbed_official 354:e67efb2aab0e 849
mbed_official 354:e67efb2aab0e 850 /* Set the DMA error callback */
mbed_official 354:e67efb2aab0e 851 hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
mbed_official 354:e67efb2aab0e 852
mbed_official 354:e67efb2aab0e 853 /* Check if Master Receiver mode is selected */
mbed_official 354:e67efb2aab0e 854 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
mbed_official 354:e67efb2aab0e 855 {
mbed_official 354:e67efb2aab0e 856 /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
mbed_official 354:e67efb2aab0e 857 access to the SPI_SR register. */
mbed_official 354:e67efb2aab0e 858 __HAL_I2S_CLEAR_OVRFLAG(hi2s);
mbed_official 354:e67efb2aab0e 859 }
mbed_official 354:e67efb2aab0e 860
mbed_official 354:e67efb2aab0e 861 /* Enable the Rx DMA Channel */
mbed_official 354:e67efb2aab0e 862 HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr, hi2s->RxXferSize);
mbed_official 354:e67efb2aab0e 863
mbed_official 354:e67efb2aab0e 864 /* Check if the I2S is already enabled */
mbed_official 354:e67efb2aab0e 865 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
mbed_official 354:e67efb2aab0e 866 {
mbed_official 354:e67efb2aab0e 867 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 868 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 869 }
mbed_official 354:e67efb2aab0e 870
mbed_official 354:e67efb2aab0e 871 /* Check if the I2S Rx request is already enabled */
mbed_official 354:e67efb2aab0e 872 if((hi2s->Instance->CR2 &SPI_CR2_RXDMAEN) != SPI_CR2_RXDMAEN)
mbed_official 354:e67efb2aab0e 873 {
mbed_official 354:e67efb2aab0e 874 /* Enable Rx DMA Request */
mbed_official 354:e67efb2aab0e 875 SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
mbed_official 354:e67efb2aab0e 876 }
mbed_official 354:e67efb2aab0e 877
mbed_official 354:e67efb2aab0e 878 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 879 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 880
mbed_official 354:e67efb2aab0e 881 return HAL_OK;
mbed_official 354:e67efb2aab0e 882 }
mbed_official 354:e67efb2aab0e 883 else
mbed_official 354:e67efb2aab0e 884 {
mbed_official 354:e67efb2aab0e 885 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 886 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 887 return HAL_BUSY;
mbed_official 354:e67efb2aab0e 888 }
mbed_official 354:e67efb2aab0e 889 }
mbed_official 354:e67efb2aab0e 890
mbed_official 354:e67efb2aab0e 891 /**
mbed_official 354:e67efb2aab0e 892 * @brief Pauses the audio stream playing from the Media.
mbed_official 354:e67efb2aab0e 893 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 894 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 895 * @retval HAL status
mbed_official 354:e67efb2aab0e 896 */
mbed_official 354:e67efb2aab0e 897 HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 898 {
mbed_official 354:e67efb2aab0e 899 /* Process Locked */
mbed_official 354:e67efb2aab0e 900 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 901
mbed_official 354:e67efb2aab0e 902 if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
mbed_official 354:e67efb2aab0e 903 {
mbed_official 354:e67efb2aab0e 904 /* Disable the I2S DMA Tx request */
mbed_official 354:e67efb2aab0e 905 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
mbed_official 354:e67efb2aab0e 906 }
mbed_official 354:e67efb2aab0e 907 else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
mbed_official 354:e67efb2aab0e 908 {
mbed_official 354:e67efb2aab0e 909 /* Disable the I2S DMA Rx request */
mbed_official 354:e67efb2aab0e 910 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
mbed_official 354:e67efb2aab0e 911 }
mbed_official 354:e67efb2aab0e 912
mbed_official 354:e67efb2aab0e 913 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 914 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 915
mbed_official 354:e67efb2aab0e 916 return HAL_OK;
mbed_official 354:e67efb2aab0e 917 }
mbed_official 354:e67efb2aab0e 918
mbed_official 354:e67efb2aab0e 919 /**
mbed_official 354:e67efb2aab0e 920 * @brief Resumes the audio stream playing from the Media.
mbed_official 354:e67efb2aab0e 921 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 922 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 923 * @retval HAL status
mbed_official 354:e67efb2aab0e 924 */
mbed_official 354:e67efb2aab0e 925 HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 926 {
mbed_official 354:e67efb2aab0e 927 /* Process Locked */
mbed_official 354:e67efb2aab0e 928 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 929
mbed_official 354:e67efb2aab0e 930 if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
mbed_official 354:e67efb2aab0e 931 {
mbed_official 354:e67efb2aab0e 932 /* Enable the I2S DMA Tx request */
mbed_official 354:e67efb2aab0e 933 SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
mbed_official 354:e67efb2aab0e 934 }
mbed_official 354:e67efb2aab0e 935 else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
mbed_official 354:e67efb2aab0e 936 {
mbed_official 354:e67efb2aab0e 937 /* Enable the I2S DMA Rx request */
mbed_official 354:e67efb2aab0e 938 SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
mbed_official 354:e67efb2aab0e 939 }
mbed_official 354:e67efb2aab0e 940
mbed_official 354:e67efb2aab0e 941 /* If the I2S peripheral is still not enabled, enable it */
mbed_official 354:e67efb2aab0e 942 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) == 0)
mbed_official 354:e67efb2aab0e 943 {
mbed_official 354:e67efb2aab0e 944 /* Enable I2S peripheral */
mbed_official 354:e67efb2aab0e 945 __HAL_I2S_ENABLE(hi2s);
mbed_official 354:e67efb2aab0e 946 }
mbed_official 354:e67efb2aab0e 947
mbed_official 354:e67efb2aab0e 948 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 949 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 950
mbed_official 354:e67efb2aab0e 951 return HAL_OK;
mbed_official 354:e67efb2aab0e 952 }
mbed_official 354:e67efb2aab0e 953
mbed_official 354:e67efb2aab0e 954 /**
mbed_official 354:e67efb2aab0e 955 * @brief Resumes the audio stream playing from the Media.
mbed_official 354:e67efb2aab0e 956 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 957 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 958 * @retval HAL status
mbed_official 354:e67efb2aab0e 959 */
mbed_official 354:e67efb2aab0e 960 HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 961 {
mbed_official 354:e67efb2aab0e 962 /* Process Locked */
mbed_official 354:e67efb2aab0e 963 __HAL_LOCK(hi2s);
mbed_official 354:e67efb2aab0e 964
mbed_official 354:e67efb2aab0e 965 /* Disable the I2S Tx/Rx DMA requests */
mbed_official 354:e67efb2aab0e 966 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
mbed_official 354:e67efb2aab0e 967 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
mbed_official 354:e67efb2aab0e 968
mbed_official 354:e67efb2aab0e 969 /* Abort the I2S DMA Channel tx */
mbed_official 354:e67efb2aab0e 970 if(hi2s->hdmatx != HAL_NULL)
mbed_official 354:e67efb2aab0e 971 {
mbed_official 354:e67efb2aab0e 972 /* Disable the I2S DMA channel */
mbed_official 354:e67efb2aab0e 973 __HAL_DMA_DISABLE(hi2s->hdmatx);
mbed_official 354:e67efb2aab0e 974 HAL_DMA_Abort(hi2s->hdmatx);
mbed_official 354:e67efb2aab0e 975 }
mbed_official 354:e67efb2aab0e 976 /* Abort the I2S DMA Channel rx */
mbed_official 354:e67efb2aab0e 977 if(hi2s->hdmarx != HAL_NULL)
mbed_official 354:e67efb2aab0e 978 {
mbed_official 354:e67efb2aab0e 979 /* Disable the I2S DMA channel */
mbed_official 354:e67efb2aab0e 980 __HAL_DMA_DISABLE(hi2s->hdmarx);
mbed_official 354:e67efb2aab0e 981 HAL_DMA_Abort(hi2s->hdmarx);
mbed_official 354:e67efb2aab0e 982 }
mbed_official 354:e67efb2aab0e 983
mbed_official 354:e67efb2aab0e 984 /* Disable I2S peripheral */
mbed_official 354:e67efb2aab0e 985 __HAL_I2S_DISABLE(hi2s);
mbed_official 354:e67efb2aab0e 986
mbed_official 354:e67efb2aab0e 987 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 988
mbed_official 354:e67efb2aab0e 989 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 990 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 991
mbed_official 354:e67efb2aab0e 992 return HAL_OK;
mbed_official 354:e67efb2aab0e 993 }
mbed_official 354:e67efb2aab0e 994
mbed_official 354:e67efb2aab0e 995 /**
mbed_official 354:e67efb2aab0e 996 * @brief This function handles I2S interrupt request.
mbed_official 354:e67efb2aab0e 997 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 998 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 999 * @retval None
mbed_official 354:e67efb2aab0e 1000 */
mbed_official 354:e67efb2aab0e 1001 void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1002 {
mbed_official 354:e67efb2aab0e 1003 uint32_t i2ssr = hi2s->Instance->SR;
mbed_official 354:e67efb2aab0e 1004
mbed_official 354:e67efb2aab0e 1005 /* I2S in mode Receiver ------------------------------------------------*/
mbed_official 354:e67efb2aab0e 1006 if(((i2ssr & I2S_FLAG_OVR) != I2S_FLAG_OVR) &&
mbed_official 354:e67efb2aab0e 1007 ((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
mbed_official 354:e67efb2aab0e 1008 {
mbed_official 354:e67efb2aab0e 1009 I2S_Receive_IT(hi2s);
mbed_official 354:e67efb2aab0e 1010 return;
mbed_official 354:e67efb2aab0e 1011 }
mbed_official 354:e67efb2aab0e 1012
mbed_official 354:e67efb2aab0e 1013 /* I2S in mode Tramitter -----------------------------------------------*/
mbed_official 354:e67efb2aab0e 1014 if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
mbed_official 354:e67efb2aab0e 1015 {
mbed_official 354:e67efb2aab0e 1016 I2S_Transmit_IT(hi2s);
mbed_official 354:e67efb2aab0e 1017 return;
mbed_official 354:e67efb2aab0e 1018 }
mbed_official 354:e67efb2aab0e 1019
mbed_official 354:e67efb2aab0e 1020 /* I2S interrupt error -------------------------------------------------*/
mbed_official 354:e67efb2aab0e 1021 if(__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET)
mbed_official 354:e67efb2aab0e 1022 {
mbed_official 354:e67efb2aab0e 1023 /* I2S Overrun error interrupt occured ---------------------------------*/
mbed_official 354:e67efb2aab0e 1024 if((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR)
mbed_official 354:e67efb2aab0e 1025 {
mbed_official 354:e67efb2aab0e 1026 /* Disable RXNE and ERR interrupt */
mbed_official 354:e67efb2aab0e 1027 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 1028
mbed_official 354:e67efb2aab0e 1029 /* Set the error code and execute error callback*/
mbed_official 354:e67efb2aab0e 1030 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
mbed_official 354:e67efb2aab0e 1031 }
mbed_official 354:e67efb2aab0e 1032
mbed_official 354:e67efb2aab0e 1033 /* I2S Underrun error interrupt occured --------------------------------*/
mbed_official 354:e67efb2aab0e 1034 if((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR)
mbed_official 354:e67efb2aab0e 1035 {
mbed_official 354:e67efb2aab0e 1036 /* Disable TXE and ERR interrupt */
mbed_official 354:e67efb2aab0e 1037 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 1038
mbed_official 354:e67efb2aab0e 1039 /* Set the error code and execute error callback*/
mbed_official 354:e67efb2aab0e 1040 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
mbed_official 354:e67efb2aab0e 1041 }
mbed_official 354:e67efb2aab0e 1042
mbed_official 354:e67efb2aab0e 1043 /* I2S Frame format error interrupt occured --------------------------*/
mbed_official 354:e67efb2aab0e 1044 if((i2ssr & I2S_FLAG_FRE) == I2S_FLAG_FRE)
mbed_official 354:e67efb2aab0e 1045 {
mbed_official 354:e67efb2aab0e 1046 /* Disable TXE and ERR interrupt */
mbed_official 354:e67efb2aab0e 1047 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_RXNE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 1048
mbed_official 354:e67efb2aab0e 1049 /* Set the error code and execute error callback*/
mbed_official 354:e67efb2aab0e 1050 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_FRE);
mbed_official 354:e67efb2aab0e 1051 }
mbed_official 354:e67efb2aab0e 1052
mbed_official 354:e67efb2aab0e 1053 /* Set the I2S State ready */
mbed_official 354:e67efb2aab0e 1054 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1055 /* Call the Error Callback */
mbed_official 354:e67efb2aab0e 1056 HAL_I2S_ErrorCallback(hi2s);
mbed_official 354:e67efb2aab0e 1057 }
mbed_official 354:e67efb2aab0e 1058 }
mbed_official 354:e67efb2aab0e 1059
mbed_official 354:e67efb2aab0e 1060 /**
mbed_official 354:e67efb2aab0e 1061 * @brief Tx Transfer Half completed callbacks
mbed_official 354:e67efb2aab0e 1062 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1063 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1064 * @retval None
mbed_official 354:e67efb2aab0e 1065 */
mbed_official 354:e67efb2aab0e 1066 __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1067 {
mbed_official 354:e67efb2aab0e 1068 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 1069 the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
mbed_official 354:e67efb2aab0e 1070 */
mbed_official 354:e67efb2aab0e 1071 }
mbed_official 354:e67efb2aab0e 1072
mbed_official 354:e67efb2aab0e 1073 /**
mbed_official 354:e67efb2aab0e 1074 * @brief Tx Transfer completed callbacks
mbed_official 354:e67efb2aab0e 1075 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1076 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1077 * @retval None
mbed_official 354:e67efb2aab0e 1078 */
mbed_official 354:e67efb2aab0e 1079 __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1080 {
mbed_official 354:e67efb2aab0e 1081 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 1082 the HAL_I2S_TxCpltCallback could be implemented in the user file
mbed_official 354:e67efb2aab0e 1083 */
mbed_official 354:e67efb2aab0e 1084 }
mbed_official 354:e67efb2aab0e 1085
mbed_official 354:e67efb2aab0e 1086 /**
mbed_official 354:e67efb2aab0e 1087 * @brief Rx Transfer half completed callbacks
mbed_official 354:e67efb2aab0e 1088 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1089 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1090 * @retval None
mbed_official 354:e67efb2aab0e 1091 */
mbed_official 354:e67efb2aab0e 1092 __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1093 {
mbed_official 354:e67efb2aab0e 1094 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 1095 the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
mbed_official 354:e67efb2aab0e 1096 */
mbed_official 354:e67efb2aab0e 1097 }
mbed_official 354:e67efb2aab0e 1098
mbed_official 354:e67efb2aab0e 1099 /**
mbed_official 354:e67efb2aab0e 1100 * @brief Rx Transfer completed callbacks
mbed_official 354:e67efb2aab0e 1101 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1102 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1103 * @retval None
mbed_official 354:e67efb2aab0e 1104 */
mbed_official 354:e67efb2aab0e 1105 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1106 {
mbed_official 354:e67efb2aab0e 1107 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 1108 the HAL_I2S_RxCpltCallback could be implemented in the user file
mbed_official 354:e67efb2aab0e 1109 */
mbed_official 354:e67efb2aab0e 1110 }
mbed_official 354:e67efb2aab0e 1111
mbed_official 354:e67efb2aab0e 1112 /**
mbed_official 354:e67efb2aab0e 1113 * @brief I2S error callbacks
mbed_official 354:e67efb2aab0e 1114 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1115 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1116 * @retval None
mbed_official 354:e67efb2aab0e 1117 */
mbed_official 354:e67efb2aab0e 1118 __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1119 {
mbed_official 354:e67efb2aab0e 1120 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 1121 the HAL_I2S_ErrorCallback could be implemented in the user file
mbed_official 354:e67efb2aab0e 1122 */
mbed_official 354:e67efb2aab0e 1123 }
mbed_official 354:e67efb2aab0e 1124
mbed_official 354:e67efb2aab0e 1125 /**
mbed_official 354:e67efb2aab0e 1126 * @}
mbed_official 354:e67efb2aab0e 1127 */
mbed_official 354:e67efb2aab0e 1128
mbed_official 354:e67efb2aab0e 1129 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
mbed_official 354:e67efb2aab0e 1130 * @brief Peripheral State functions
mbed_official 354:e67efb2aab0e 1131 *
mbed_official 354:e67efb2aab0e 1132 @verbatim
mbed_official 354:e67efb2aab0e 1133 ===============================================================================
mbed_official 354:e67efb2aab0e 1134 ##### Peripheral State and Errors functions #####
mbed_official 354:e67efb2aab0e 1135 ===============================================================================
mbed_official 354:e67efb2aab0e 1136 [..]
mbed_official 354:e67efb2aab0e 1137 This subsection permits to get in run-time the status of the peripheral
mbed_official 354:e67efb2aab0e 1138 and the data flow.
mbed_official 354:e67efb2aab0e 1139
mbed_official 354:e67efb2aab0e 1140 @endverbatim
mbed_official 354:e67efb2aab0e 1141 * @{
mbed_official 354:e67efb2aab0e 1142 */
mbed_official 354:e67efb2aab0e 1143
mbed_official 354:e67efb2aab0e 1144 /**
mbed_official 354:e67efb2aab0e 1145 * @brief Return the I2S state
mbed_official 354:e67efb2aab0e 1146 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1147 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1148 * @retval HAL state
mbed_official 354:e67efb2aab0e 1149 */
mbed_official 354:e67efb2aab0e 1150 HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1151 {
mbed_official 354:e67efb2aab0e 1152 return hi2s->State;
mbed_official 354:e67efb2aab0e 1153 }
mbed_official 354:e67efb2aab0e 1154
mbed_official 354:e67efb2aab0e 1155 /**
mbed_official 354:e67efb2aab0e 1156 * @brief Return the I2S error code
mbed_official 354:e67efb2aab0e 1157 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1158 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1159 * @retval I2S Error Code
mbed_official 354:e67efb2aab0e 1160 */
mbed_official 354:e67efb2aab0e 1161 HAL_I2S_ErrorTypeDef HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1162 {
mbed_official 354:e67efb2aab0e 1163 return hi2s->ErrorCode;
mbed_official 354:e67efb2aab0e 1164 }
mbed_official 354:e67efb2aab0e 1165 /**
mbed_official 354:e67efb2aab0e 1166 * @}
mbed_official 354:e67efb2aab0e 1167 */
mbed_official 354:e67efb2aab0e 1168
mbed_official 354:e67efb2aab0e 1169 /**
mbed_official 354:e67efb2aab0e 1170 * @}
mbed_official 354:e67efb2aab0e 1171 */
mbed_official 354:e67efb2aab0e 1172
mbed_official 354:e67efb2aab0e 1173
mbed_official 354:e67efb2aab0e 1174 /** @defgroup I2S_Private_Functions I2S Private Functions
mbed_official 354:e67efb2aab0e 1175 * @{
mbed_official 354:e67efb2aab0e 1176 */
mbed_official 354:e67efb2aab0e 1177 /**
mbed_official 354:e67efb2aab0e 1178 * @brief DMA I2S transmit process complete callback
mbed_official 354:e67efb2aab0e 1179 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1180 * the configuration information for the specified DMA module.
mbed_official 354:e67efb2aab0e 1181 * @retval None
mbed_official 354:e67efb2aab0e 1182 */
mbed_official 354:e67efb2aab0e 1183 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1184 {
mbed_official 354:e67efb2aab0e 1185 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1186
mbed_official 354:e67efb2aab0e 1187 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
mbed_official 354:e67efb2aab0e 1188 {
mbed_official 354:e67efb2aab0e 1189 /* Disable Tx DMA Request */
mbed_official 354:e67efb2aab0e 1190 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
mbed_official 354:e67efb2aab0e 1191
mbed_official 354:e67efb2aab0e 1192 hi2s->TxXferCount = 0;
mbed_official 354:e67efb2aab0e 1193 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1194 }
mbed_official 354:e67efb2aab0e 1195 HAL_I2S_TxCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1196 }
mbed_official 354:e67efb2aab0e 1197
mbed_official 354:e67efb2aab0e 1198 /**
mbed_official 354:e67efb2aab0e 1199 * @brief DMA I2S transmit process half complete callback
mbed_official 354:e67efb2aab0e 1200 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1201 * the configuration information for the specified DMA module.
mbed_official 354:e67efb2aab0e 1202 * @retval None
mbed_official 354:e67efb2aab0e 1203 */
mbed_official 354:e67efb2aab0e 1204 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1205 {
mbed_official 354:e67efb2aab0e 1206 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1207
mbed_official 354:e67efb2aab0e 1208 HAL_I2S_TxHalfCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1209 }
mbed_official 354:e67efb2aab0e 1210
mbed_official 354:e67efb2aab0e 1211 /**
mbed_official 354:e67efb2aab0e 1212 * @brief DMA I2S receive process complete callback
mbed_official 354:e67efb2aab0e 1213 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1214 * the configuration information for the specified DMA module.
mbed_official 354:e67efb2aab0e 1215 * @retval None
mbed_official 354:e67efb2aab0e 1216 */
mbed_official 354:e67efb2aab0e 1217 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1218 {
mbed_official 354:e67efb2aab0e 1219 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1220
mbed_official 354:e67efb2aab0e 1221 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
mbed_official 354:e67efb2aab0e 1222 {
mbed_official 354:e67efb2aab0e 1223 /* Disable Rx DMA Request */
mbed_official 354:e67efb2aab0e 1224 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
mbed_official 354:e67efb2aab0e 1225 hi2s->RxXferCount = 0;
mbed_official 354:e67efb2aab0e 1226 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1227 }
mbed_official 354:e67efb2aab0e 1228 HAL_I2S_RxCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1229 }
mbed_official 354:e67efb2aab0e 1230
mbed_official 354:e67efb2aab0e 1231 /**
mbed_official 354:e67efb2aab0e 1232 * @brief DMA I2S receive process half complete callback
mbed_official 354:e67efb2aab0e 1233 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1234 * the configuration information for the specified DMA module.
mbed_official 354:e67efb2aab0e 1235 * @retval None
mbed_official 354:e67efb2aab0e 1236 */
mbed_official 354:e67efb2aab0e 1237 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1238 {
mbed_official 354:e67efb2aab0e 1239 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1240
mbed_official 354:e67efb2aab0e 1241 HAL_I2S_RxHalfCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1242 }
mbed_official 354:e67efb2aab0e 1243
mbed_official 354:e67efb2aab0e 1244 /**
mbed_official 354:e67efb2aab0e 1245 * @brief DMA I2S communication error callback
mbed_official 354:e67efb2aab0e 1246 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1247 * the configuration information for the specified DMA module.
mbed_official 354:e67efb2aab0e 1248 * @retval None
mbed_official 354:e67efb2aab0e 1249 */
mbed_official 354:e67efb2aab0e 1250 static void I2S_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1251 {
mbed_official 354:e67efb2aab0e 1252 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1253
mbed_official 354:e67efb2aab0e 1254 /* Disable Rx and Tx DMA Request */
mbed_official 354:e67efb2aab0e 1255 CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
mbed_official 354:e67efb2aab0e 1256 hi2s->TxXferCount = 0;
mbed_official 354:e67efb2aab0e 1257 hi2s->RxXferCount = 0;
mbed_official 354:e67efb2aab0e 1258
mbed_official 354:e67efb2aab0e 1259 hi2s->State= HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1260
mbed_official 354:e67efb2aab0e 1261 /* Set the error code and execute error callback*/
mbed_official 354:e67efb2aab0e 1262 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
mbed_official 354:e67efb2aab0e 1263 HAL_I2S_ErrorCallback(hi2s);
mbed_official 354:e67efb2aab0e 1264 }
mbed_official 354:e67efb2aab0e 1265
mbed_official 354:e67efb2aab0e 1266 /**
mbed_official 354:e67efb2aab0e 1267 * @brief Transmit an amount of data in non-blocking mode with Interrupt
mbed_official 354:e67efb2aab0e 1268 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1269 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1270 * @retval None
mbed_official 354:e67efb2aab0e 1271 */
mbed_official 354:e67efb2aab0e 1272 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1273 {
mbed_official 354:e67efb2aab0e 1274 /* Transmit data */
mbed_official 354:e67efb2aab0e 1275 hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
mbed_official 354:e67efb2aab0e 1276 hi2s->TxXferCount--;
mbed_official 354:e67efb2aab0e 1277
mbed_official 354:e67efb2aab0e 1278 if(hi2s->TxXferCount == 0)
mbed_official 354:e67efb2aab0e 1279 {
mbed_official 354:e67efb2aab0e 1280 /* Disable TXE and ERR interrupt */
mbed_official 354:e67efb2aab0e 1281 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 1282
mbed_official 354:e67efb2aab0e 1283 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1284 HAL_I2S_TxCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1285 }
mbed_official 354:e67efb2aab0e 1286 }
mbed_official 354:e67efb2aab0e 1287
mbed_official 354:e67efb2aab0e 1288 /**
mbed_official 354:e67efb2aab0e 1289 * @brief Receive an amount of data in non-blocking mode with Interrupt
mbed_official 354:e67efb2aab0e 1290 * @param hi2s: I2S handle
mbed_official 354:e67efb2aab0e 1291 * @retval None
mbed_official 354:e67efb2aab0e 1292 */
mbed_official 354:e67efb2aab0e 1293 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
mbed_official 354:e67efb2aab0e 1294 {
mbed_official 354:e67efb2aab0e 1295 /* Receive data */
mbed_official 354:e67efb2aab0e 1296 (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
mbed_official 354:e67efb2aab0e 1297 hi2s->RxXferCount--;
mbed_official 354:e67efb2aab0e 1298
mbed_official 354:e67efb2aab0e 1299 if(hi2s->RxXferCount == 0)
mbed_official 354:e67efb2aab0e 1300 {
mbed_official 354:e67efb2aab0e 1301 /* Disable RXNE and ERR interrupt */
mbed_official 354:e67efb2aab0e 1302 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
mbed_official 354:e67efb2aab0e 1303
mbed_official 354:e67efb2aab0e 1304 hi2s->State = HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1305 HAL_I2S_RxCpltCallback(hi2s);
mbed_official 354:e67efb2aab0e 1306 }
mbed_official 354:e67efb2aab0e 1307 }
mbed_official 354:e67efb2aab0e 1308
mbed_official 354:e67efb2aab0e 1309
mbed_official 354:e67efb2aab0e 1310 /**
mbed_official 354:e67efb2aab0e 1311 * @brief This function handles I2S Communication Timeout.
mbed_official 354:e67efb2aab0e 1312 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1313 * the configuration information for I2S module
mbed_official 354:e67efb2aab0e 1314 * @param Flag: Flag checked
mbed_official 354:e67efb2aab0e 1315 * @param Status: Value of the flag expected
mbed_official 354:e67efb2aab0e 1316 * @param Timeout: Duration of the timeout
mbed_official 354:e67efb2aab0e 1317 * @retval HAL status
mbed_official 354:e67efb2aab0e 1318 */
mbed_official 354:e67efb2aab0e 1319 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t Status, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 1320 {
mbed_official 354:e67efb2aab0e 1321 uint32_t tickstart = 0;
mbed_official 354:e67efb2aab0e 1322
mbed_official 354:e67efb2aab0e 1323 /* Get tick */
mbed_official 354:e67efb2aab0e 1324 tickstart = HAL_GetTick();
mbed_official 354:e67efb2aab0e 1325
mbed_official 354:e67efb2aab0e 1326 /* Wait until flag is set */
mbed_official 354:e67efb2aab0e 1327 if(Status == RESET)
mbed_official 354:e67efb2aab0e 1328 {
mbed_official 354:e67efb2aab0e 1329 while(__HAL_I2S_GET_FLAG(hi2s, Flag) == RESET)
mbed_official 354:e67efb2aab0e 1330 {
mbed_official 354:e67efb2aab0e 1331 if(Timeout != HAL_MAX_DELAY)
mbed_official 354:e67efb2aab0e 1332 {
mbed_official 354:e67efb2aab0e 1333 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
mbed_official 354:e67efb2aab0e 1334 {
mbed_official 354:e67efb2aab0e 1335 /* Set the I2S State ready */
mbed_official 354:e67efb2aab0e 1336 hi2s->State= HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1337
mbed_official 354:e67efb2aab0e 1338 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1339 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 1340
mbed_official 354:e67efb2aab0e 1341 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 1342 }
mbed_official 354:e67efb2aab0e 1343 }
mbed_official 354:e67efb2aab0e 1344 }
mbed_official 354:e67efb2aab0e 1345 }
mbed_official 354:e67efb2aab0e 1346 else
mbed_official 354:e67efb2aab0e 1347 {
mbed_official 354:e67efb2aab0e 1348 while(__HAL_I2S_GET_FLAG(hi2s, Flag) != RESET)
mbed_official 354:e67efb2aab0e 1349 {
mbed_official 354:e67efb2aab0e 1350 if(Timeout != HAL_MAX_DELAY)
mbed_official 354:e67efb2aab0e 1351 {
mbed_official 354:e67efb2aab0e 1352 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
mbed_official 354:e67efb2aab0e 1353 {
mbed_official 354:e67efb2aab0e 1354 /* Set the I2S State ready */
mbed_official 354:e67efb2aab0e 1355 hi2s->State= HAL_I2S_STATE_READY;
mbed_official 354:e67efb2aab0e 1356
mbed_official 354:e67efb2aab0e 1357 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1358 __HAL_UNLOCK(hi2s);
mbed_official 354:e67efb2aab0e 1359
mbed_official 354:e67efb2aab0e 1360 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 1361 }
mbed_official 354:e67efb2aab0e 1362 }
mbed_official 354:e67efb2aab0e 1363 }
mbed_official 354:e67efb2aab0e 1364 }
mbed_official 354:e67efb2aab0e 1365 return HAL_OK;
mbed_official 354:e67efb2aab0e 1366 }
mbed_official 354:e67efb2aab0e 1367
mbed_official 354:e67efb2aab0e 1368 /**
mbed_official 354:e67efb2aab0e 1369 * @}
mbed_official 354:e67efb2aab0e 1370 */
mbed_official 354:e67efb2aab0e 1371 #endif /* STM32L100xC ||
mbed_official 354:e67efb2aab0e 1372 STM32L151xC || STM32L151xCA || STM32L151xD || STM32L151xE ||\\
mbed_official 354:e67efb2aab0e 1373 STM32L152xC || STM32L152xCA || STM32L152xD || STM32L152xE ||\\
mbed_official 354:e67efb2aab0e 1374 STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE */
mbed_official 354:e67efb2aab0e 1375 #endif /* HAL_I2S_MODULE_ENABLED */
mbed_official 354:e67efb2aab0e 1376 /**
mbed_official 354:e67efb2aab0e 1377 * @}
mbed_official 354:e67efb2aab0e 1378 */
mbed_official 354:e67efb2aab0e 1379
mbed_official 354:e67efb2aab0e 1380 /**
mbed_official 354:e67efb2aab0e 1381 * @}
mbed_official 354:e67efb2aab0e 1382 */
mbed_official 354:e67efb2aab0e 1383
mbed_official 354:e67efb2aab0e 1384 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/