mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Nov 03 10:45:07 2014 +0000
Revision:
382:ee426a420dbb
Parent:
targets/cmsis/TARGET_STM/TARGET_DISCO_L053C8/stm32l0xx_hal_i2s.c@376:cb4d9db17537
Child:
387:643a59b3dbac
Synchronized with git revision d54467eb07f62efd9ccdf44f1ede7fe1c1b0cf83

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

Who changed what in which revision?

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