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:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
489:119543c9f674
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

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

Initial version of drivers for SAMR21

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