Patched for Audio example - Add status check when DFSDM's filter and channel de-init.

Dependents:   DISCO_F413ZH-AUDIO-demo

The base repository is https://os.mbed.com/teams/ST/code/BSP_DISCO_F413ZH/. I've just added workaround patch for Audio-in demo on DISCO_F413ZH board(Microphone U16, U17)

Committer:
Daniel_Lee
Date:
Fri Jan 31 07:17:05 2020 +0000
Revision:
4:c051317d4051
Parent:
3:42b354f5069c
Patched for Audio example;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arostm 0:4af3ca173992 1 /**
arostm 0:4af3ca173992 2 ******************************************************************************
arostm 0:4af3ca173992 3 * @file STM32f413h_discovery_audio.c
arostm 0:4af3ca173992 4 * @author MCD Application Team
arostm 0:4af3ca173992 5 * @brief This file provides the Audio driver for the STM32F413H-DISCOVERY board.
arostm 0:4af3ca173992 6 ******************************************************************************
arostm 0:4af3ca173992 7 * @attention
arostm 0:4af3ca173992 8 *
arostm 0:4af3ca173992 9 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
arostm 0:4af3ca173992 10 *
arostm 0:4af3ca173992 11 * Redistribution and use in source and binary forms, with or without modification,
arostm 0:4af3ca173992 12 * are permitted provided that the following conditions are met:
arostm 0:4af3ca173992 13 * 1. Redistributions of source code must retain the above copyright notice,
arostm 0:4af3ca173992 14 * this list of conditions and the following disclaimer.
arostm 0:4af3ca173992 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
arostm 0:4af3ca173992 16 * this list of conditions and the following disclaimer in the documentation
arostm 0:4af3ca173992 17 * and/or other materials provided with the distribution.
arostm 0:4af3ca173992 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
arostm 0:4af3ca173992 19 * may be used to endorse or promote products derived from this software
arostm 0:4af3ca173992 20 * without specific prior written permission.
arostm 0:4af3ca173992 21 *
arostm 0:4af3ca173992 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
arostm 0:4af3ca173992 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
arostm 0:4af3ca173992 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
arostm 0:4af3ca173992 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
arostm 0:4af3ca173992 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
arostm 0:4af3ca173992 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
arostm 0:4af3ca173992 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
arostm 0:4af3ca173992 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
arostm 0:4af3ca173992 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
arostm 0:4af3ca173992 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
arostm 0:4af3ca173992 32 *
arostm 0:4af3ca173992 33 ******************************************************************************
arostm 0:4af3ca173992 34 */
arostm 0:4af3ca173992 35
arostm 0:4af3ca173992 36 /*==============================================================================
arostm 0:4af3ca173992 37 User NOTES
arostm 0:4af3ca173992 38
arostm 0:4af3ca173992 39 How To use this driver:
arostm 0:4af3ca173992 40 -----------------------
arostm 0:4af3ca173992 41 + This driver supports STM32F4xx devices on STM32F413H-DISCOVERY boards.
arostm 0:4af3ca173992 42 + Call the function BSP_AUDIO_OUT_Init(
arostm 0:4af3ca173992 43 OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER,
arostm 0:4af3ca173992 44 OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH)
arostm 0:4af3ca173992 45 Volume : Initial volume to be set (0 is min (mute), 100 is max (100%)
arostm 0:4af3ca173992 46 AudioFreq : Audio frequency in Hz (8000, 16000, 22500, 32000...)
arostm 0:4af3ca173992 47 this parameter is relative to the audio file/stream type.
arostm 0:4af3ca173992 48 )
arostm 0:4af3ca173992 49 This function configures all the hardware required for the audio application (codec, I2C, I2S,
arostm 0:4af3ca173992 50 GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if configuration is OK.
arostm 0:4af3ca173992 51 If the returned value is different from AUDIO_OK or the function is stuck then the communication with
arostm 0:4af3ca173992 52 the codec has failed (try to un-plug the power or reset device in this case).
arostm 0:4af3ca173992 53 - OUTPUT_DEVICE_SPEAKER : only speaker will be set as output for the audio stream.
arostm 0:4af3ca173992 54 - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream.
arostm 0:4af3ca173992 55 - OUTPUT_DEVICE_BOTH : both Speaker and Headphone are used as outputs for the audio stream
arostm 0:4af3ca173992 56 at the same time.
arostm 0:4af3ca173992 57 + Call the function BSP_AUDIO_OUT_Play(
arostm 0:4af3ca173992 58 pBuffer: pointer to the audio data file address
arostm 0:4af3ca173992 59 Size : size of the buffer to be sent in Bytes
arostm 0:4af3ca173992 60 )
arostm 0:4af3ca173992 61 to start playing (for the first time) from the audio file/stream.
arostm 0:4af3ca173992 62 + Call the function BSP_AUDIO_OUT_Pause() to pause playing
arostm 0:4af3ca173992 63 + Call the function BSP_AUDIO_OUT_Resume() to resume playing.
arostm 0:4af3ca173992 64 Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called
arostm 0:4af3ca173992 65 for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case).
arostm 0:4af3ca173992 66 Note. This function should be called only when the audio file is played or paused (not stopped).
arostm 0:4af3ca173992 67 + For each mode, you may need to implement the relative callback functions into your code.
arostm 0:4af3ca173992 68 The Callback functions are named AUDIO_OUT_XXX_CallBack() and only their prototypes are declared in
arostm 0:4af3ca173992 69 the STM32F413H_discovery_audio.h file. (refer to the example for more details on the callbacks implementations)
arostm 0:4af3ca173992 70 + To Stop playing, to modify the volume level, the frequency, use the functions: BSP_AUDIO_OUT_SetVolume(),
arostm 0:4af3ca173992 71 AUDIO_OUT_SetFrequency(), BSP_AUDIO_OUT_SetOutputMode(), BSP_AUDIO_OUT_SetMute() and BSP_AUDIO_OUT_Stop().
arostm 0:4af3ca173992 72 + The driver API and the callback functions are at the end of the STM32F413H_discovery_audio.h file.
arostm 0:4af3ca173992 73
arostm 0:4af3ca173992 74
arostm 0:4af3ca173992 75 Driver architecture:
arostm 0:4af3ca173992 76 --------------------
arostm 0:4af3ca173992 77 + This driver provides the High Audio Layer: consists of the function API exported in the stm32f413h_discovery_audio.h file
arostm 0:4af3ca173992 78 (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...)
arostm 0:4af3ca173992 79 + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/
arostm 0:4af3ca173992 80 providing the audio file/stream. These functions are also included as local functions into
arostm 0:4af3ca173992 81 the stm32f413h_discovery_audio_codec.c file (I2Sx_Out_Init(), I2Sx_Out_DeInit(), I2Sx_In_Init() and I2Sx_In_DeInit())
arostm 0:4af3ca173992 82
arostm 0:4af3ca173992 83 Known Limitations:
arostm 0:4af3ca173992 84 ------------------
arostm 0:4af3ca173992 85 1- If the TDM Format used to play in parallel 2 audio Stream (the first Stream is configured in codec SLOT0 and second
arostm 0:4af3ca173992 86 Stream in SLOT1) the Pause/Resume, volume and mute feature will control the both streams.
arostm 0:4af3ca173992 87 2- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size,
arostm 0:4af3ca173992 88 File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file.
arostm 0:4af3ca173992 89 3- Supports only Stereo audio streaming.
arostm 0:4af3ca173992 90 4- Supports only 16-bits audio data size.
arostm 0:4af3ca173992 91 ==============================================================================*/
arostm 0:4af3ca173992 92
arostm 0:4af3ca173992 93 /* Includes ------------------------------------------------------------------*/
arostm 0:4af3ca173992 94 #include "stm32f413h_discovery_audio.h"
arostm 0:4af3ca173992 95
arostm 0:4af3ca173992 96 /** @addtogroup BSP
arostm 0:4af3ca173992 97 * @{
arostm 0:4af3ca173992 98 */
arostm 0:4af3ca173992 99
arostm 0:4af3ca173992 100 /** @addtogroup STM32F413H_DISCOVERY
arostm 0:4af3ca173992 101 * @{
arostm 0:4af3ca173992 102 */
arostm 0:4af3ca173992 103
arostm 0:4af3ca173992 104 /** @defgroup STM32F413H_DISCOVERY_AUDIO STM32F413H_DISCOVERY AUDIO
arostm 0:4af3ca173992 105 * @brief This file includes the low layer driver for wm8994 Audio Codec
arostm 0:4af3ca173992 106 * available on STM32F413H-DISCOVERY board(MB1209).
arostm 0:4af3ca173992 107 * @{
arostm 0:4af3ca173992 108 */
arostm 0:4af3ca173992 109
arostm 0:4af3ca173992 110 /** @defgroup STM32F413H_DISCOVERY_AUDIO_Private_Macros STM32F413H DISCOVERY Audio Private macros
arostm 0:4af3ca173992 111 * @{
arostm 0:4af3ca173992 112 */
arostm 0:4af3ca173992 113
arostm 0:4af3ca173992 114 #define DFSDM_OVER_SAMPLING(__FREQUENCY__) \
arostm 0:4af3ca173992 115 (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 256 \
arostm 0:4af3ca173992 116 : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 256 \
arostm 0:4af3ca173992 117 : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 128 \
arostm 0:4af3ca173992 118 : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 128 \
arostm 0:4af3ca173992 119 : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 64 \
arostm 0:4af3ca173992 120 : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 64 \
arostm 0:4af3ca173992 121 : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 25 \
arostm 0:4af3ca173992 122
arostm 0:4af3ca173992 123 #define DFSDM_CLOCK_DIVIDER(__FREQUENCY__) \
arostm 0:4af3ca173992 124 (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 24 \
arostm 0:4af3ca173992 125 : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 48 \
arostm 0:4af3ca173992 126 : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 24 \
arostm 0:4af3ca173992 127 : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 48 \
arostm 0:4af3ca173992 128 : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 24 \
arostm 0:4af3ca173992 129 : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 48 \
arostm 0:4af3ca173992 130 : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 72 \
arostm 0:4af3ca173992 131
arostm 0:4af3ca173992 132 #define DFSDM_FILTER_ORDER(__FREQUENCY__) \
arostm 0:4af3ca173992 133 (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? DFSDM_FILTER_SINC3_ORDER \
arostm 0:4af3ca173992 134 : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? DFSDM_FILTER_SINC3_ORDER \
arostm 0:4af3ca173992 135 : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? DFSDM_FILTER_SINC3_ORDER \
arostm 0:4af3ca173992 136 : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? DFSDM_FILTER_SINC3_ORDER \
arostm 0:4af3ca173992 137 : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? DFSDM_FILTER_SINC4_ORDER \
arostm 0:4af3ca173992 138 : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? DFSDM_FILTER_SINC4_ORDER \
arostm 0:4af3ca173992 139 : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? DFSDM_FILTER_SINC4_ORDER : DFSDM_FILTER_SINC4_ORDER \
arostm 0:4af3ca173992 140
arostm 0:4af3ca173992 141 #define DFSDM_MIC_BIT_SHIFT(__FREQUENCY__) \
arostm 0:4af3ca173992 142 (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 5 \
arostm 0:4af3ca173992 143 : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 4 \
arostm 0:4af3ca173992 144 : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 2 \
arostm 0:4af3ca173992 145 : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 2 \
arostm 0:4af3ca173992 146 : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 5 \
arostm 0:4af3ca173992 147 : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 6 \
arostm 0:4af3ca173992 148 : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 2 : 0 \
arostm 0:4af3ca173992 149
arostm 0:4af3ca173992 150 /* Saturate the record PCM sample */
arostm 0:4af3ca173992 151 #define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N)))
arostm 0:4af3ca173992 152 /**
arostm 0:4af3ca173992 153 * @}
arostm 0:4af3ca173992 154 */
arostm 0:4af3ca173992 155
arostm 0:4af3ca173992 156 /** @defgroup STM32F413H_DISCOVERY_AUDIO_Private_Variables STM32F413H DISCOVERY Audio Private Variables
arostm 0:4af3ca173992 157 * @{
arostm 0:4af3ca173992 158 */
arostm 0:4af3ca173992 159
arostm 0:4af3ca173992 160 AUDIO_DrvTypeDef *audio_drv;
arostm 0:4af3ca173992 161 I2S_HandleTypeDef haudio_i2s; /* for Audio_OUT and Audio_IN_analog mic */
arostm 0:4af3ca173992 162 I2S_HandleTypeDef haudio_in_i2sext; /* for Analog mic with full duplex mode */
arostm 0:4af3ca173992 163 AUDIOIN_ContextTypeDef hAudioIn;
arostm 0:4af3ca173992 164
arostm 0:4af3ca173992 165 DFSDM_Channel_HandleTypeDef hAudioInDfsdmChannel[DFSDM_MIC_NUMBER]; /* 5 DFSDM channel handle used for all microphones */
arostm 0:4af3ca173992 166 DFSDM_Filter_HandleTypeDef hAudioInDfsdmFilter[DFSDM_MIC_NUMBER]; /* 5 DFSDM filter handle */
arostm 0:4af3ca173992 167 DMA_HandleTypeDef hDmaDfsdm[DFSDM_MIC_NUMBER]; /* 5 DMA handle used for DFSDM regular conversions */
arostm 0:4af3ca173992 168
arostm 0:4af3ca173992 169 /* Buffers for right and left samples */
arostm 0:4af3ca173992 170 int32_t *pScratchBuff[DEFAULT_AUDIO_IN_CHANNEL_NBR];
arostm 2:0f07a9ac06f7 171 uint32_t ScratchSize;
arostm 0:4af3ca173992 172
arostm 0:4af3ca173992 173 uint32_t DmaRecHalfBuffCplt[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 174 uint32_t DmaRecBuffCplt[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 175
arostm 0:4af3ca173992 176 /* Application Buffer Trigger */
arostm 0:4af3ca173992 177 __IO uint32_t AppBuffTrigger = 0;
arostm 0:4af3ca173992 178 __IO uint32_t AppBuffHalf = 0;
arostm 0:4af3ca173992 179 __IO uint32_t MicBuff[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 180 __IO uint16_t AudioInVolume = DEFAULT_AUDIO_IN_VOLUME;
arostm 0:4af3ca173992 181
arostm 0:4af3ca173992 182 /**
arostm 0:4af3ca173992 183 * @}
arostm 0:4af3ca173992 184 */
arostm 0:4af3ca173992 185
arostm 0:4af3ca173992 186 /** @defgroup STM32F413H_DISCOVERY_AUDIO_Private_Function_Prototypes STM32F413H DISCOVERY Audio Private Prototypes
arostm 0:4af3ca173992 187 * @{
arostm 0:4af3ca173992 188 */
arostm 0:4af3ca173992 189 static void I2Sx_In_Init(uint32_t AudioFreq);
arostm 0:4af3ca173992 190 static void I2Sx_In_DeInit(void);
arostm 0:4af3ca173992 191 static void I2Sx_In_MspInit(void);
arostm 0:4af3ca173992 192 static void I2Sx_In_MspDeInit(void);
arostm 0:4af3ca173992 193
arostm 0:4af3ca173992 194 static void I2Sx_Out_Init(uint32_t AudioFreq);
arostm 0:4af3ca173992 195 static void I2Sx_Out_DeInit(void);
arostm 0:4af3ca173992 196
arostm 0:4af3ca173992 197 static uint8_t DFSDMx_DeInit(void);
arostm 0:4af3ca173992 198 static void DFSDMx_ChannelMspInit(void);
arostm 0:4af3ca173992 199 static void DFSDMx_ChannelMspDeInit(void);
arostm 0:4af3ca173992 200 static void DFSDMx_FilterMspInit(void);
arostm 0:4af3ca173992 201 static void DFSDMx_FilterMspDeInit(void);
arostm 0:4af3ca173992 202
arostm 0:4af3ca173992 203 /**
arostm 0:4af3ca173992 204 * @}
arostm 0:4af3ca173992 205 */
arostm 0:4af3ca173992 206
arostm 0:4af3ca173992 207 /** @defgroup STM32F413H_DISCOVERY_AUDIO_out_Private_Functions STM32F413H DISCOVERY AUDIO OUT Private Functions
arostm 0:4af3ca173992 208 * @{
arostm 0:4af3ca173992 209 */
arostm 0:4af3ca173992 210
arostm 0:4af3ca173992 211 /**
arostm 0:4af3ca173992 212 * @brief Configures the audio peripherals.
arostm 0:4af3ca173992 213 * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
arostm 0:4af3ca173992 214 * or OUTPUT_DEVICE_BOTH.
arostm 0:4af3ca173992 215 * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
arostm 0:4af3ca173992 216 * @param AudioFreq: Audio frequency used to play the audio stream.
arostm 0:4af3ca173992 217 * @note The I2S PLL input clock must be done in the user application.
arostm 0:4af3ca173992 218 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 219 */
arostm 0:4af3ca173992 220 uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
arostm 0:4af3ca173992 221 {
arostm 0:4af3ca173992 222 uint8_t ret = AUDIO_ERROR;
arostm 0:4af3ca173992 223 uint32_t deviceid = 0x00;
arostm 0:4af3ca173992 224 uint16_t buffer_fake[16] = {0x00};
arostm 0:4af3ca173992 225
arostm 0:4af3ca173992 226 I2Sx_Out_DeInit();
arostm 0:4af3ca173992 227 AUDIO_IO_DeInit();
arostm 0:4af3ca173992 228
arostm 0:4af3ca173992 229 /* PLL clock is set depending on the AudioFreq (44.1 kHz vs 48kHz groups) */
arostm 0:4af3ca173992 230 BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
arostm 0:4af3ca173992 231
arostm 0:4af3ca173992 232 /* Configure the I2S peripheral */
arostm 0:4af3ca173992 233 haudio_i2s.Instance = AUDIO_OUT_I2Sx;
arostm 0:4af3ca173992 234 if(HAL_I2S_GetState(&haudio_i2s) == HAL_I2S_STATE_RESET)
arostm 0:4af3ca173992 235 {
arostm 0:4af3ca173992 236 /* Initialize the I2S Msp: this __weak function can be rewritten by the application */
arostm 0:4af3ca173992 237 BSP_AUDIO_OUT_MspInit(&haudio_i2s, NULL);
arostm 0:4af3ca173992 238 }
arostm 0:4af3ca173992 239 I2Sx_Out_Init(AudioFreq);
arostm 0:4af3ca173992 240
arostm 0:4af3ca173992 241 AUDIO_IO_Init();
arostm 0:4af3ca173992 242
arostm 0:4af3ca173992 243 /* wm8994 codec initialization */
arostm 0:4af3ca173992 244 deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS);
arostm 0:4af3ca173992 245
arostm 0:4af3ca173992 246 if(deviceid == WM8994_ID)
arostm 0:4af3ca173992 247 {
arostm 0:4af3ca173992 248 /* Reset the Codec Registers */
arostm 0:4af3ca173992 249 wm8994_drv.Reset(AUDIO_I2C_ADDRESS);
arostm 0:4af3ca173992 250 /* Initialize the audio driver structure */
arostm 0:4af3ca173992 251 audio_drv = &wm8994_drv;
arostm 0:4af3ca173992 252 ret = AUDIO_OK;
arostm 0:4af3ca173992 253 }
arostm 0:4af3ca173992 254 else
arostm 0:4af3ca173992 255 {
arostm 0:4af3ca173992 256 ret = AUDIO_ERROR;
arostm 0:4af3ca173992 257 }
arostm 0:4af3ca173992 258
arostm 0:4af3ca173992 259 if(ret == AUDIO_OK)
arostm 0:4af3ca173992 260 {
arostm 0:4af3ca173992 261 /* Send fake I2S data in order to generate MCLK needed by WM8994 to set its registers
arostm 0:4af3ca173992 262 * MCLK is generated only when a data stream is sent on I2S */
arostm 0:4af3ca173992 263 HAL_I2S_Transmit_DMA(&haudio_i2s, buffer_fake, 16);
arostm 0:4af3ca173992 264 /* Initialize the codec internal registers */
arostm 0:4af3ca173992 265 audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq);
arostm 0:4af3ca173992 266 /* Stop sending fake I2S data */
arostm 0:4af3ca173992 267 HAL_I2S_DMAStop(&haudio_i2s);
arostm 0:4af3ca173992 268 }
arostm 0:4af3ca173992 269
arostm 0:4af3ca173992 270 return ret;
arostm 0:4af3ca173992 271 }
arostm 0:4af3ca173992 272
arostm 0:4af3ca173992 273 /**
arostm 0:4af3ca173992 274 * @brief Starts playing audio stream from a data buffer for a determined size.
arostm 0:4af3ca173992 275 * @param pBuffer: Pointer to the buffer
arostm 0:4af3ca173992 276 * @param Size: Number of audio data BYTES.
arostm 0:4af3ca173992 277 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 278 */
arostm 0:4af3ca173992 279 uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size)
arostm 0:4af3ca173992 280 {
arostm 0:4af3ca173992 281 /* Call the audio Codec Play function */
arostm 0:4af3ca173992 282 if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
arostm 0:4af3ca173992 283 {
arostm 0:4af3ca173992 284 return AUDIO_ERROR;
arostm 0:4af3ca173992 285 }
arostm 0:4af3ca173992 286 else
arostm 0:4af3ca173992 287 {
arostm 0:4af3ca173992 288 /* Update the Media layer and enable it for play */
arostm 0:4af3ca173992 289 HAL_I2S_Transmit_DMA(&haudio_i2s, pBuffer, DMA_MAX(Size / AUDIODATA_SIZE));
arostm 0:4af3ca173992 290
arostm 0:4af3ca173992 291 return AUDIO_OK;
arostm 0:4af3ca173992 292 }
arostm 0:4af3ca173992 293 }
arostm 0:4af3ca173992 294
arostm 0:4af3ca173992 295 /**
arostm 0:4af3ca173992 296 * @brief Sends n-Bytes on the I2S interface.
arostm 0:4af3ca173992 297 * @param pData: pointer on data address
arostm 0:4af3ca173992 298 * @param Size: number of data to be written
arostm 0:4af3ca173992 299 */
arostm 0:4af3ca173992 300 void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
arostm 0:4af3ca173992 301 {
arostm 0:4af3ca173992 302 HAL_I2S_Transmit_DMA(&haudio_i2s, pData, Size);
arostm 0:4af3ca173992 303 }
arostm 0:4af3ca173992 304
arostm 0:4af3ca173992 305 /**
arostm 0:4af3ca173992 306 * @brief This function Pauses the audio file stream. In case
arostm 0:4af3ca173992 307 * of using DMA, the DMA Pause feature is used.
arostm 0:4af3ca173992 308 * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only
arostm 0:4af3ca173992 309 * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
arostm 0:4af3ca173992 310 * function for resume could lead to unexpected behavior).
arostm 0:4af3ca173992 311 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 312 */
arostm 0:4af3ca173992 313 uint8_t BSP_AUDIO_OUT_Pause(void)
arostm 0:4af3ca173992 314 {
arostm 0:4af3ca173992 315 /* Call the Audio Codec Pause/Resume function */
arostm 0:4af3ca173992 316 if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0)
arostm 0:4af3ca173992 317 {
arostm 0:4af3ca173992 318 return AUDIO_ERROR;
arostm 0:4af3ca173992 319 }
arostm 0:4af3ca173992 320 else
arostm 0:4af3ca173992 321 {
arostm 0:4af3ca173992 322 /* Call the Media layer pause function */
arostm 0:4af3ca173992 323 HAL_I2S_DMAPause(&haudio_i2s);
arostm 0:4af3ca173992 324
arostm 0:4af3ca173992 325 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 326 return AUDIO_OK;
arostm 0:4af3ca173992 327 }
arostm 0:4af3ca173992 328 }
arostm 0:4af3ca173992 329
arostm 0:4af3ca173992 330 /**
arostm 0:4af3ca173992 331 * @brief This function Resumes the audio file stream.
arostm 0:4af3ca173992 332 * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only
arostm 0:4af3ca173992 333 * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
arostm 0:4af3ca173992 334 * function for resume could lead to unexpected behavior).
arostm 0:4af3ca173992 335 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 336 */
arostm 0:4af3ca173992 337 uint8_t BSP_AUDIO_OUT_Resume(void)
arostm 0:4af3ca173992 338 {
arostm 0:4af3ca173992 339 /* Call the Media layer pause/resume function */
arostm 0:4af3ca173992 340 /* DMA stream resumed before accessing WM8994 register as WM8994 needs the MCLK to be generated to access its registers
arostm 0:4af3ca173992 341 * MCLK is generated only when a data stream is sent on I2S */
arostm 0:4af3ca173992 342 HAL_I2S_DMAResume(&haudio_i2s);
arostm 0:4af3ca173992 343
arostm 0:4af3ca173992 344 /* Call the Audio Codec Pause/Resume function */
arostm 0:4af3ca173992 345 if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0)
arostm 0:4af3ca173992 346 {
arostm 0:4af3ca173992 347 return AUDIO_ERROR;
arostm 0:4af3ca173992 348 }
arostm 0:4af3ca173992 349 else
arostm 0:4af3ca173992 350 {
arostm 0:4af3ca173992 351 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 352 return AUDIO_OK;
arostm 0:4af3ca173992 353 }
arostm 0:4af3ca173992 354 }
arostm 0:4af3ca173992 355
arostm 0:4af3ca173992 356 /**
arostm 0:4af3ca173992 357 * @brief Stops audio playing and Power down the Audio Codec.
arostm 0:4af3ca173992 358 * @param Option: could be one of the following parameters
arostm 0:4af3ca173992 359 * - CODEC_PDWN_SW: for software power off (by writing registers).
arostm 0:4af3ca173992 360 * Then no need to reconfigure the Codec after power on.
arostm 0:4af3ca173992 361 * - CODEC_PDWN_HW: completely shut down the codec (physically).
arostm 0:4af3ca173992 362 * Then need to reconfigure the Codec after power on.
arostm 0:4af3ca173992 363 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 364 */
arostm 0:4af3ca173992 365 uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
arostm 0:4af3ca173992 366 {
arostm 0:4af3ca173992 367 /* Call the Media layer stop function */
arostm 0:4af3ca173992 368 HAL_I2S_DMAStop(&haudio_i2s);
arostm 0:4af3ca173992 369
arostm 0:4af3ca173992 370 /* Call Audio Codec Stop function */
arostm 0:4af3ca173992 371 if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
arostm 0:4af3ca173992 372 {
arostm 0:4af3ca173992 373 return AUDIO_ERROR;
arostm 0:4af3ca173992 374 }
arostm 0:4af3ca173992 375 else
arostm 0:4af3ca173992 376 {
arostm 0:4af3ca173992 377 if(Option == CODEC_PDWN_HW)
arostm 0:4af3ca173992 378 {
arostm 0:4af3ca173992 379 /* Wait at least 100us */
arostm 0:4af3ca173992 380 HAL_Delay(1);
arostm 0:4af3ca173992 381 }
arostm 0:4af3ca173992 382 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 383 return AUDIO_OK;
arostm 0:4af3ca173992 384 }
arostm 0:4af3ca173992 385 }
arostm 0:4af3ca173992 386
arostm 0:4af3ca173992 387 /**
arostm 0:4af3ca173992 388 * @brief Controls the current audio volume level.
arostm 0:4af3ca173992 389 * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
arostm 0:4af3ca173992 390 * Mute and 100 for Max volume level).
arostm 0:4af3ca173992 391 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 392 */
arostm 0:4af3ca173992 393 uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
arostm 0:4af3ca173992 394 {
arostm 0:4af3ca173992 395 /* Call the codec volume control function with converted volume value */
arostm 0:4af3ca173992 396 if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
arostm 0:4af3ca173992 397 {
arostm 0:4af3ca173992 398 return AUDIO_ERROR;
arostm 0:4af3ca173992 399 }
arostm 0:4af3ca173992 400 else
arostm 0:4af3ca173992 401 {
arostm 0:4af3ca173992 402 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 403 return AUDIO_OK;
arostm 0:4af3ca173992 404 }
arostm 0:4af3ca173992 405 }
arostm 0:4af3ca173992 406
arostm 0:4af3ca173992 407 /**
arostm 0:4af3ca173992 408 * @brief Enables or disables the MUTE mode by software
arostm 0:4af3ca173992 409 * @param Cmd: Could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to
arostm 0:4af3ca173992 410 * unmute the codec and restore previous volume level.
arostm 0:4af3ca173992 411 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 412 */
arostm 0:4af3ca173992 413 uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
arostm 0:4af3ca173992 414 {
arostm 0:4af3ca173992 415 /* Call the Codec Mute function */
arostm 0:4af3ca173992 416 if(audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
arostm 0:4af3ca173992 417 {
arostm 0:4af3ca173992 418 return AUDIO_ERROR;
arostm 0:4af3ca173992 419 }
arostm 0:4af3ca173992 420 else
arostm 0:4af3ca173992 421 {
arostm 0:4af3ca173992 422 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 423 return AUDIO_OK;
arostm 0:4af3ca173992 424 }
arostm 0:4af3ca173992 425 }
arostm 0:4af3ca173992 426
arostm 0:4af3ca173992 427 /**
arostm 0:4af3ca173992 428 * @brief Switch dynamically (while audio file is played) the output target
arostm 0:4af3ca173992 429 * (speaker or headphone).
arostm 0:4af3ca173992 430 * @param Output: The audio output target: OUTPUT_DEVICE_SPEAKER,
arostm 0:4af3ca173992 431 * OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH
arostm 0:4af3ca173992 432 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 433 */
arostm 0:4af3ca173992 434 uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
arostm 0:4af3ca173992 435 {
arostm 0:4af3ca173992 436 /* Call the Codec output device function */
arostm 0:4af3ca173992 437 if(audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
arostm 0:4af3ca173992 438 {
arostm 0:4af3ca173992 439 return AUDIO_ERROR;
arostm 0:4af3ca173992 440 }
arostm 0:4af3ca173992 441 else
arostm 0:4af3ca173992 442 {
arostm 0:4af3ca173992 443 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 444 return AUDIO_OK;
arostm 0:4af3ca173992 445 }
arostm 0:4af3ca173992 446 }
arostm 0:4af3ca173992 447
arostm 0:4af3ca173992 448 /**
arostm 0:4af3ca173992 449 * @brief Updates the audio frequency.
arostm 0:4af3ca173992 450 * @param AudioFreq: Audio frequency used to play the audio stream.
arostm 0:4af3ca173992 451 * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the
arostm 0:4af3ca173992 452 * audio frequency.
arostm 0:4af3ca173992 453 * @retval None
arostm 0:4af3ca173992 454 */
arostm 0:4af3ca173992 455 void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
arostm 0:4af3ca173992 456 {
arostm 0:4af3ca173992 457 /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
arostm 0:4af3ca173992 458 BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
arostm 0:4af3ca173992 459
arostm 0:4af3ca173992 460 /* Disable I2S peripheral to allow access to I2S internal registers */
arostm 0:4af3ca173992 461 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 462
arostm 0:4af3ca173992 463 /* Update the I2S audio frequency configuration */
arostm 0:4af3ca173992 464 haudio_i2s.Init.AudioFreq = AudioFreq;
arostm 0:4af3ca173992 465 HAL_I2S_Init(&haudio_i2s);
arostm 0:4af3ca173992 466
arostm 0:4af3ca173992 467 /* Enable I2S peripheral to generate MCLK */
arostm 0:4af3ca173992 468 __HAL_I2S_ENABLE(&haudio_i2s);
arostm 0:4af3ca173992 469 }
arostm 0:4af3ca173992 470
arostm 0:4af3ca173992 471 /**
arostm 0:4af3ca173992 472 * @brief Deinit the audio peripherals.
arostm 0:4af3ca173992 473 */
arostm 0:4af3ca173992 474 void BSP_AUDIO_OUT_DeInit(void)
arostm 0:4af3ca173992 475 {
arostm 0:4af3ca173992 476 I2Sx_Out_DeInit();
arostm 0:4af3ca173992 477 /* DeInit the I2S MSP : this __weak function can be rewritten by the application */
arostm 0:4af3ca173992 478 BSP_AUDIO_OUT_MspDeInit(&haudio_i2s, NULL);
arostm 0:4af3ca173992 479 }
arostm 0:4af3ca173992 480
arostm 0:4af3ca173992 481 /**
arostm 0:4af3ca173992 482 * @brief Tx Transfer completed callbacks.
arostm 0:4af3ca173992 483 * @param hi2s: I2S handle
arostm 0:4af3ca173992 484 */
arostm 0:4af3ca173992 485 void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
arostm 0:4af3ca173992 486 {
arostm 0:4af3ca173992 487 /* Manage the remaining file size and new address offset: This function
arostm 0:4af3ca173992 488 should be coded by user (its prototype is already declared in STM32F413H_discovery_audio.h) */
arostm 0:4af3ca173992 489 BSP_AUDIO_OUT_TransferComplete_CallBack();
arostm 0:4af3ca173992 490 }
arostm 0:4af3ca173992 491
arostm 0:4af3ca173992 492 /**
arostm 0:4af3ca173992 493 * @brief Tx Half Transfer completed callbacks.
arostm 0:4af3ca173992 494 * @param hi2s: I2S handle
arostm 0:4af3ca173992 495 */
arostm 0:4af3ca173992 496 void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
arostm 0:4af3ca173992 497 {
arostm 0:4af3ca173992 498 /* Manage the remaining file size and new address offset: This function
arostm 0:4af3ca173992 499 should be coded by user (its prototype is already declared in STM32F413H_discovery_audio.h) */
arostm 0:4af3ca173992 500 BSP_AUDIO_OUT_HalfTransfer_CallBack();
arostm 0:4af3ca173992 501 }
arostm 0:4af3ca173992 502
arostm 0:4af3ca173992 503 /**
arostm 0:4af3ca173992 504 * @brief I2S error callbacks.
arostm 0:4af3ca173992 505 * @param hi2s: I2S handle
arostm 0:4af3ca173992 506 */
arostm 0:4af3ca173992 507 void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
arostm 0:4af3ca173992 508 {
arostm 0:4af3ca173992 509 BSP_AUDIO_OUT_Error_CallBack();
arostm 0:4af3ca173992 510 }
arostm 0:4af3ca173992 511
arostm 0:4af3ca173992 512 /**
arostm 0:4af3ca173992 513 * @brief Manages the DMA full Transfer complete event.
arostm 0:4af3ca173992 514 */
arostm 0:4af3ca173992 515 __weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
arostm 0:4af3ca173992 516 {
arostm 0:4af3ca173992 517 }
arostm 0:4af3ca173992 518
arostm 0:4af3ca173992 519 /**
arostm 0:4af3ca173992 520 * @brief Manages the DMA Half Transfer complete event.
arostm 0:4af3ca173992 521 */
arostm 0:4af3ca173992 522 __weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
arostm 0:4af3ca173992 523 {
arostm 0:4af3ca173992 524 }
arostm 0:4af3ca173992 525
arostm 0:4af3ca173992 526 /**
arostm 0:4af3ca173992 527 * @brief Manages the DMA FIFO error event.
arostm 0:4af3ca173992 528 */
arostm 0:4af3ca173992 529 __weak void BSP_AUDIO_OUT_Error_CallBack(void)
arostm 0:4af3ca173992 530 {
arostm 0:4af3ca173992 531 }
arostm 0:4af3ca173992 532
arostm 0:4af3ca173992 533 /**
arostm 0:4af3ca173992 534 * @brief Initializes BSP_AUDIO_OUT MSP.
arostm 0:4af3ca173992 535 * @param hi2s: I2S handle
arostm 0:4af3ca173992 536 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 537 */
arostm 0:4af3ca173992 538 __weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
arostm 0:4af3ca173992 539 {
arostm 0:4af3ca173992 540 static DMA_HandleTypeDef hdma_i2s_tx;
arostm 0:4af3ca173992 541 GPIO_InitTypeDef gpio_init_structure;
arostm 0:4af3ca173992 542
arostm 0:4af3ca173992 543 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 544 UNUSED(Params);
arostm 0:4af3ca173992 545
arostm 0:4af3ca173992 546 /* Enable I2S clock */
arostm 0:4af3ca173992 547 AUDIO_OUT_I2Sx_CLK_ENABLE();
arostm 0:4af3ca173992 548
arostm 0:4af3ca173992 549 /* Enable MCK, SCK, WS, SD and CODEC_INT GPIO clock */
arostm 0:4af3ca173992 550 AUDIO_OUT_I2Sx_MCK_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 551 AUDIO_OUT_I2Sx_SCK_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 552 AUDIO_OUT_I2Sx_SD_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 553 AUDIO_OUT_I2Sx_WS_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 554
arostm 0:4af3ca173992 555 /* CODEC_I2S pins configuration: MCK, SCK, WS and SD pins */
arostm 0:4af3ca173992 556 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_MCK_PIN;
arostm 0:4af3ca173992 557 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
arostm 0:4af3ca173992 558 gpio_init_structure.Pull = GPIO_NOPULL;
arostm 0:4af3ca173992 559 gpio_init_structure.Speed = GPIO_SPEED_FAST;
arostm 0:4af3ca173992 560 gpio_init_structure.Alternate = AUDIO_OUT_I2Sx_MCK_AF;
arostm 0:4af3ca173992 561 HAL_GPIO_Init(AUDIO_OUT_I2Sx_MCK_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 562
arostm 0:4af3ca173992 563 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_SCK_PIN;
arostm 0:4af3ca173992 564 gpio_init_structure.Alternate = AUDIO_OUT_I2Sx_SCK_AF;
arostm 0:4af3ca173992 565 HAL_GPIO_Init(AUDIO_OUT_I2Sx_SCK_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 566
arostm 0:4af3ca173992 567 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_WS_PIN;
arostm 0:4af3ca173992 568 gpio_init_structure.Alternate = AUDIO_OUT_I2Sx_WS_AF;
arostm 0:4af3ca173992 569 HAL_GPIO_Init(AUDIO_OUT_I2Sx_WS_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 570
arostm 0:4af3ca173992 571 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_SD_PIN;
arostm 0:4af3ca173992 572 gpio_init_structure.Alternate = AUDIO_OUT_I2Sx_SD_AF;
arostm 0:4af3ca173992 573 HAL_GPIO_Init(AUDIO_OUT_I2Sx_SD_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 574
arostm 0:4af3ca173992 575 /* Enable the DMA clock */
arostm 0:4af3ca173992 576 AUDIO_OUT_I2Sx_DMAx_CLK_ENABLE();
arostm 0:4af3ca173992 577
arostm 0:4af3ca173992 578 if(hi2s->Instance == AUDIO_OUT_I2Sx)
arostm 0:4af3ca173992 579 {
arostm 0:4af3ca173992 580 /* Configure the hdma_i2s_rx handle parameters */
arostm 0:4af3ca173992 581 hdma_i2s_tx.Init.Channel = AUDIO_OUT_I2Sx_DMAx_CHANNEL;
arostm 0:4af3ca173992 582 hdma_i2s_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
arostm 0:4af3ca173992 583 hdma_i2s_tx.Init.PeriphInc = DMA_PINC_DISABLE;
arostm 0:4af3ca173992 584 hdma_i2s_tx.Init.MemInc = DMA_MINC_ENABLE;
arostm 0:4af3ca173992 585 hdma_i2s_tx.Init.PeriphDataAlignment = AUDIO_OUT_I2Sx_DMAx_PERIPH_DATA_SIZE;
arostm 0:4af3ca173992 586 hdma_i2s_tx.Init.MemDataAlignment = AUDIO_OUT_I2Sx_DMAx_MEM_DATA_SIZE;
arostm 0:4af3ca173992 587 hdma_i2s_tx.Init.Mode = DMA_CIRCULAR;
arostm 0:4af3ca173992 588 hdma_i2s_tx.Init.Priority = DMA_PRIORITY_HIGH;
arostm 0:4af3ca173992 589 hdma_i2s_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
arostm 0:4af3ca173992 590 hdma_i2s_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
arostm 0:4af3ca173992 591 hdma_i2s_tx.Init.MemBurst = DMA_MBURST_SINGLE;
arostm 0:4af3ca173992 592 hdma_i2s_tx.Init.PeriphBurst = DMA_MBURST_SINGLE;
arostm 0:4af3ca173992 593
arostm 0:4af3ca173992 594 hdma_i2s_tx.Instance = AUDIO_OUT_I2Sx_DMAx_STREAM;
arostm 0:4af3ca173992 595
arostm 0:4af3ca173992 596 /* Associate the DMA handle */
arostm 0:4af3ca173992 597 __HAL_LINKDMA(hi2s, hdmatx, hdma_i2s_tx);
arostm 0:4af3ca173992 598
arostm 0:4af3ca173992 599 /* Deinitialize the Stream for new transfer */
arostm 0:4af3ca173992 600 HAL_DMA_DeInit(&hdma_i2s_tx);
arostm 0:4af3ca173992 601
arostm 0:4af3ca173992 602 /* Configure the DMA Stream */
arostm 0:4af3ca173992 603 HAL_DMA_Init(&hdma_i2s_tx);
arostm 0:4af3ca173992 604 }
arostm 0:4af3ca173992 605
arostm 0:4af3ca173992 606 /* Enable and set I2Sx Interrupt to a lower priority */
arostm 0:4af3ca173992 607 HAL_NVIC_SetPriority(SPI3_IRQn, 0x0F, 0x00);
arostm 0:4af3ca173992 608 HAL_NVIC_EnableIRQ(SPI3_IRQn);
arostm 0:4af3ca173992 609
arostm 0:4af3ca173992 610 /* I2S DMA IRQ Channel configuration */
arostm 0:4af3ca173992 611 HAL_NVIC_SetPriority(AUDIO_OUT_I2Sx_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0);
arostm 0:4af3ca173992 612 HAL_NVIC_EnableIRQ(AUDIO_OUT_I2Sx_DMAx_IRQ);
arostm 0:4af3ca173992 613 }
arostm 0:4af3ca173992 614
arostm 0:4af3ca173992 615 /**
arostm 0:4af3ca173992 616 * @brief Deinitializes I2S MSP.
arostm 0:4af3ca173992 617 * @param hi2s: I2S handle
arostm 0:4af3ca173992 618 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 619 */
arostm 0:4af3ca173992 620 __weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
arostm 0:4af3ca173992 621 {
arostm 0:4af3ca173992 622 GPIO_InitTypeDef gpio_init_structure;
arostm 0:4af3ca173992 623
arostm 0:4af3ca173992 624 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 625 UNUSED(Params);
arostm 0:4af3ca173992 626
arostm 0:4af3ca173992 627 /* I2S DMA IRQ Channel deactivation */
arostm 0:4af3ca173992 628 HAL_NVIC_DisableIRQ(AUDIO_OUT_I2Sx_DMAx_IRQ);
arostm 0:4af3ca173992 629
arostm 0:4af3ca173992 630 if(hi2s->Instance == AUDIO_OUT_I2Sx)
arostm 0:4af3ca173992 631 {
arostm 0:4af3ca173992 632 /* Deinitialize the DMA stream */
arostm 0:4af3ca173992 633 HAL_DMA_DeInit(hi2s->hdmatx);
arostm 0:4af3ca173992 634 }
arostm 0:4af3ca173992 635
arostm 0:4af3ca173992 636 /* Disable I2S peripheral */
arostm 0:4af3ca173992 637 __HAL_I2S_DISABLE(hi2s);
arostm 0:4af3ca173992 638
arostm 0:4af3ca173992 639 /* Deactives CODEC_I2S pins MCK, SCK, WS and SD by putting them in input mode */
arostm 0:4af3ca173992 640 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_MCK_PIN;
arostm 0:4af3ca173992 641 HAL_GPIO_DeInit(AUDIO_OUT_I2Sx_MCK_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 642
arostm 0:4af3ca173992 643 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_SCK_PIN;
arostm 0:4af3ca173992 644 HAL_GPIO_DeInit(AUDIO_OUT_I2Sx_SCK_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 645
arostm 0:4af3ca173992 646 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_WS_PIN;
arostm 0:4af3ca173992 647 HAL_GPIO_DeInit(AUDIO_OUT_I2Sx_WS_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 648
arostm 0:4af3ca173992 649 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_SD_PIN;
arostm 0:4af3ca173992 650 HAL_GPIO_DeInit(AUDIO_OUT_I2Sx_SD_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 651
arostm 0:4af3ca173992 652 /* Disable I2S clock */
arostm 0:4af3ca173992 653 AUDIO_OUT_I2Sx_CLK_DISABLE();
arostm 0:4af3ca173992 654
arostm 0:4af3ca173992 655 /* GPIO pins clock and DMA clock can be shut down in the application
arostm 0:4af3ca173992 656 by surcharging this __weak function */
arostm 0:4af3ca173992 657 }
arostm 0:4af3ca173992 658
arostm 0:4af3ca173992 659 /**
arostm 0:4af3ca173992 660 * @brief Clock Config.
arostm 0:4af3ca173992 661 * @param hi2s: might be required to set audio peripheral predivider if any.
arostm 0:4af3ca173992 662 * @param AudioFreq: Audio frequency used to play the audio stream.
arostm 0:4af3ca173992 663 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 664 * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency()
arostm 0:4af3ca173992 665 * Being __weak it can be overwritten by the application
arostm 0:4af3ca173992 666 */
arostm 0:4af3ca173992 667 __weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
arostm 0:4af3ca173992 668 {
arostm 0:4af3ca173992 669 RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
arostm 0:4af3ca173992 670
arostm 0:4af3ca173992 671 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 672 UNUSED(Params);
arostm 0:4af3ca173992 673
arostm 0:4af3ca173992 674 HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 675
arostm 0:4af3ca173992 676 /* Set the PLL configuration according to the audio frequency */
arostm 0:4af3ca173992 677 if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K))
arostm 0:4af3ca173992 678 {
arostm 0:4af3ca173992 679 /* Configure PLLI2S prescalers */
arostm 0:4af3ca173992 680 rcc_ex_clk_init_struct.PeriphClockSelection = (RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_PLLI2S);
arostm 0:4af3ca173992 681 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 682 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 683 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 684 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 271;
arostm 0:4af3ca173992 685 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 2;
arostm 0:4af3ca173992 686
arostm 0:4af3ca173992 687 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 688 }
arostm 0:4af3ca173992 689 else if(AudioFreq == AUDIO_FREQUENCY_96K) /* AUDIO_FREQUENCY_96K */
arostm 0:4af3ca173992 690 {
arostm 0:4af3ca173992 691 /* I2S clock config */
arostm 0:4af3ca173992 692 rcc_ex_clk_init_struct.PeriphClockSelection = (RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_PLLI2S);
arostm 0:4af3ca173992 693 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 694 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 695 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 696 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344;
arostm 0:4af3ca173992 697 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 2;
arostm 0:4af3ca173992 698
arostm 0:4af3ca173992 699 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 700 }
arostm 0:4af3ca173992 701 else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_48K */
arostm 0:4af3ca173992 702 {
arostm 0:4af3ca173992 703 /* I2S clock config
arostm 0:4af3ca173992 704 PLLI2S_VCO: VCO_344M
arostm 0:4af3ca173992 705 I2S_CLK(first level) = PLLI2S_VCO/PLLI2SR = 344/7 = 49.142 Mhz
arostm 0:4af3ca173992 706 I2S_CLK_x = I2S_CLK(first level)/PLLI2SDIVR = 49.142/1 = 49.142 Mhz */
arostm 0:4af3ca173992 707 rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_PLLI2S;
arostm 0:4af3ca173992 708 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 709 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 710 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 711 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344;
arostm 0:4af3ca173992 712 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 7;
arostm 0:4af3ca173992 713
arostm 0:4af3ca173992 714 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 715 }
arostm 0:4af3ca173992 716 }
arostm 0:4af3ca173992 717
arostm 0:4af3ca173992 718 /*******************************************************************************
arostm 0:4af3ca173992 719 Static Functions
arostm 0:4af3ca173992 720 *******************************************************************************/
arostm 0:4af3ca173992 721
arostm 0:4af3ca173992 722 /**
arostm 0:4af3ca173992 723 * @brief Initializes the Audio Codec audio interface (I2S)
arostm 0:4af3ca173992 724 * @note This function assumes that the I2S input clock
arostm 0:4af3ca173992 725 * is already configured and ready to be used.
arostm 0:4af3ca173992 726 * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
arostm 0:4af3ca173992 727 */
arostm 0:4af3ca173992 728 static void I2Sx_Out_Init(uint32_t AudioFreq)
arostm 0:4af3ca173992 729 {
arostm 0:4af3ca173992 730 /* Initialize the hAudioInI2s Instance parameter */
arostm 0:4af3ca173992 731 haudio_i2s.Instance = AUDIO_OUT_I2Sx;
arostm 0:4af3ca173992 732
arostm 0:4af3ca173992 733 /* Disable I2S block */
arostm 0:4af3ca173992 734 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 735
arostm 0:4af3ca173992 736 /* I2S peripheral configuration */
arostm 0:4af3ca173992 737 haudio_i2s.Init.AudioFreq = AudioFreq;
arostm 0:4af3ca173992 738 haudio_i2s.Init.ClockSource = I2S_CLOCK_PLL;
arostm 0:4af3ca173992 739 haudio_i2s.Init.CPOL = I2S_CPOL_LOW;
arostm 0:4af3ca173992 740 haudio_i2s.Init.DataFormat = I2S_DATAFORMAT_16B;
arostm 0:4af3ca173992 741 haudio_i2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
arostm 0:4af3ca173992 742 haudio_i2s.Init.Mode = I2S_MODE_MASTER_TX;
arostm 0:4af3ca173992 743 haudio_i2s.Init.Standard = I2S_STANDARD_PHILIPS;
arostm 0:4af3ca173992 744 haudio_i2s.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
arostm 0:4af3ca173992 745
arostm 0:4af3ca173992 746 /* Init the I2S */
arostm 0:4af3ca173992 747 HAL_I2S_Init(&haudio_i2s);
arostm 0:4af3ca173992 748
arostm 0:4af3ca173992 749 /* Enable I2S block */
arostm 0:4af3ca173992 750 __HAL_I2S_ENABLE(&haudio_i2s);
arostm 0:4af3ca173992 751 }
arostm 0:4af3ca173992 752
arostm 0:4af3ca173992 753 /**
arostm 0:4af3ca173992 754 * @brief Deinitializes the Audio Codec audio interface (I2S).
arostm 0:4af3ca173992 755 */
arostm 0:4af3ca173992 756 static void I2Sx_Out_DeInit(void)
arostm 0:4af3ca173992 757 {
arostm 0:4af3ca173992 758 /* Initialize the hAudioInI2s Instance parameter */
arostm 0:4af3ca173992 759 haudio_i2s.Instance = AUDIO_OUT_I2Sx;
arostm 0:4af3ca173992 760
arostm 0:4af3ca173992 761 /* Disable I2S block */
arostm 0:4af3ca173992 762 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 763
arostm 0:4af3ca173992 764 /* DeInit the I2S */
arostm 0:4af3ca173992 765 HAL_I2S_DeInit(&haudio_i2s);
arostm 0:4af3ca173992 766 }
arostm 0:4af3ca173992 767
arostm 0:4af3ca173992 768 /**
arostm 0:4af3ca173992 769 * @}
arostm 0:4af3ca173992 770 */
arostm 0:4af3ca173992 771
arostm 0:4af3ca173992 772 /** @defgroup STM32F413H_DISCOVERY_AUDIO_IN_Private_Functions STM32F413H DISCOVERY AUDIO IN Private functions
arostm 0:4af3ca173992 773 * @{
arostm 0:4af3ca173992 774 */
arostm 0:4af3ca173992 775
arostm 0:4af3ca173992 776 /**
arostm 0:4af3ca173992 777 * @brief Initializes wave recording.
arostm 0:4af3ca173992 778 * @param AudioFreq: Audio frequency to be configured for the audio in peripheral.
arostm 0:4af3ca173992 779 * @param BitRes: Audio bit resolution.
arostm 0:4af3ca173992 780 * @param ChnlNbr: Audio channel number.
arostm 0:4af3ca173992 781 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 782 */
arostm 0:4af3ca173992 783 uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
arostm 0:4af3ca173992 784 {
arostm 0:4af3ca173992 785 return BSP_AUDIO_IN_InitEx(INPUT_DEVICE_DIGITAL_MIC, AudioFreq, BitRes, ChnlNbr);
arostm 0:4af3ca173992 786 }
arostm 0:4af3ca173992 787
arostm 0:4af3ca173992 788 /**
arostm 0:4af3ca173992 789 * @brief Initializes wave recording.
arostm 0:4af3ca173992 790 * @param InputDevice: INPUT_DEVICE_DIGITAL_MICx or INPUT_DEVICE_ANALOG_MIC.
arostm 0:4af3ca173992 791 * @param AudioFreq: Audio frequency to be configured for the audio in peripheral.
arostm 0:4af3ca173992 792 * @param BitRes: Audio bit resolution.
arostm 0:4af3ca173992 793 * @param ChnlNbr: Audio channel number.
arostm 0:4af3ca173992 794 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 795 */
arostm 0:4af3ca173992 796 uint8_t BSP_AUDIO_IN_InitEx(uint32_t InputDevice, uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
arostm 0:4af3ca173992 797 {
arostm 0:4af3ca173992 798 uint32_t ret = AUDIO_ERROR;
arostm 0:4af3ca173992 799 uint32_t deviceid =0;
arostm 0:4af3ca173992 800 uint32_t mic_enabled =0;
arostm 0:4af3ca173992 801 uint16_t buffer_fake[16] = {0x00};
arostm 0:4af3ca173992 802 uint32_t i = 0;
arostm 0:4af3ca173992 803
arostm 0:4af3ca173992 804 /* Store the audio record context */
arostm 0:4af3ca173992 805 hAudioIn.Frequency = AudioFreq;
arostm 0:4af3ca173992 806 hAudioIn.BitResolution = BitRes;
arostm 0:4af3ca173992 807 hAudioIn.InputDevice = InputDevice;
arostm 0:4af3ca173992 808 hAudioIn.ChannelNbr = ChnlNbr;
arostm 0:4af3ca173992 809
arostm 0:4af3ca173992 810 /* Store the total number of microphones enabled */
arostm 0:4af3ca173992 811 for(i = 0; i < DFSDM_MIC_NUMBER; i ++)
arostm 0:4af3ca173992 812 {
arostm 0:4af3ca173992 813 if(((hAudioIn.InputDevice >> i) & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1)
arostm 0:4af3ca173992 814 {
arostm 0:4af3ca173992 815 mic_enabled++;
arostm 0:4af3ca173992 816 }
arostm 0:4af3ca173992 817 }
arostm 0:4af3ca173992 818
arostm 0:4af3ca173992 819 if (InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 820 {
arostm 0:4af3ca173992 821 InputDevice = INPUT_DEVICE_INPUT_LINE_1;
arostm 0:4af3ca173992 822 /* INPUT_DEVICE_ANALOG_MIC */
arostm 0:4af3ca173992 823 /* Disable I2S */
arostm 0:4af3ca173992 824 I2Sx_In_DeInit();
arostm 0:4af3ca173992 825
arostm 0:4af3ca173992 826 /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */
arostm 0:4af3ca173992 827 BSP_AUDIO_IN_ClockConfig(AudioFreq, NULL); /* Clock config is shared between AUDIO IN and OUT for analog mic */
arostm 0:4af3ca173992 828
arostm 0:4af3ca173992 829 /* I2S data transfer preparation:
arostm 0:4af3ca173992 830 Prepare the Media to be used for the audio transfer from I2S peripheral to memory */
arostm 0:4af3ca173992 831 haudio_i2s.Instance = AUDIO_IN_I2Sx;
arostm 0:4af3ca173992 832 if(HAL_I2S_GetState(&haudio_i2s) == HAL_I2S_STATE_RESET)
arostm 0:4af3ca173992 833 {
arostm 0:4af3ca173992 834 BSP_AUDIO_OUT_MspInit(&haudio_i2s, NULL); /* Initialize GPIOs for SPI3 Master signals */
arostm 0:4af3ca173992 835 /* Init the I2S MSP: this __weak function can be redefined by the application*/
arostm 0:4af3ca173992 836 BSP_AUDIO_IN_MspInit(NULL);
arostm 0:4af3ca173992 837 }
arostm 0:4af3ca173992 838
arostm 0:4af3ca173992 839 /* Configure I2S */
arostm 0:4af3ca173992 840 I2Sx_In_Init(AudioFreq);
arostm 0:4af3ca173992 841
arostm 0:4af3ca173992 842 AUDIO_IO_Init();
arostm 0:4af3ca173992 843
arostm 0:4af3ca173992 844 /* wm8994 codec initialization */
arostm 0:4af3ca173992 845 deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS);
arostm 0:4af3ca173992 846
arostm 0:4af3ca173992 847 if((deviceid) == WM8994_ID)
arostm 0:4af3ca173992 848 {
arostm 0:4af3ca173992 849 /* Reset the Codec Registers */
arostm 0:4af3ca173992 850 wm8994_drv.Reset(AUDIO_I2C_ADDRESS);
arostm 0:4af3ca173992 851 /* Initialize the audio driver structure */
arostm 0:4af3ca173992 852 audio_drv = &wm8994_drv;
arostm 0:4af3ca173992 853 ret = AUDIO_OK;
arostm 0:4af3ca173992 854 }
arostm 0:4af3ca173992 855 else
arostm 0:4af3ca173992 856 {
arostm 0:4af3ca173992 857 ret = AUDIO_ERROR;
arostm 0:4af3ca173992 858 }
arostm 0:4af3ca173992 859
arostm 0:4af3ca173992 860 if(ret == AUDIO_OK)
arostm 0:4af3ca173992 861 {
arostm 0:4af3ca173992 862 /* Receive fake I2S data in order to generate MCLK needed by WM8994 to set its registers */
arostm 0:4af3ca173992 863 HAL_I2S_Receive_DMA(&haudio_i2s, buffer_fake, 16);
arostm 0:4af3ca173992 864 /* Initialize the codec internal registers */
arostm 0:4af3ca173992 865 audio_drv->Init(AUDIO_I2C_ADDRESS, (OUTPUT_DEVICE_HEADPHONE|InputDevice), 100, AudioFreq);
arostm 0:4af3ca173992 866 /* Stop receiving fake I2S data */
arostm 0:4af3ca173992 867 HAL_I2S_DMAStop(&haudio_i2s);
arostm 0:4af3ca173992 868 }
arostm 0:4af3ca173992 869 }
arostm 0:4af3ca173992 870 else
arostm 0:4af3ca173992 871 {
arostm 0:4af3ca173992 872 if(hAudioIn.ChannelNbr != mic_enabled)
arostm 0:4af3ca173992 873 {
arostm 0:4af3ca173992 874 return AUDIO_ERROR;
arostm 0:4af3ca173992 875 }
arostm 0:4af3ca173992 876 else
arostm 0:4af3ca173992 877 {
arostm 0:4af3ca173992 878 /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */
arostm 0:4af3ca173992 879 BSP_AUDIO_IN_ClockConfig(AudioFreq, NULL); /* Clock config is shared between AUDIO IN and OUT for analog mic */
arostm 0:4af3ca173992 880
arostm 0:4af3ca173992 881 /* Init the DFSDM MSP: this __weak function can be redefined by the application*/
arostm 0:4af3ca173992 882 BSP_AUDIO_IN_MspInit(NULL);
arostm 0:4af3ca173992 883
arostm 0:4af3ca173992 884 /* Default configuration of DFSDM filters and channels */
arostm 0:4af3ca173992 885 ret = BSP_AUDIO_IN_ConfigDigitalMic(hAudioIn.InputDevice, NULL);
arostm 0:4af3ca173992 886 }
arostm 0:4af3ca173992 887 }
arostm 0:4af3ca173992 888
arostm 0:4af3ca173992 889 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 890 return ret;
arostm 0:4af3ca173992 891 }
arostm 0:4af3ca173992 892
arostm 0:4af3ca173992 893 /**
arostm 0:4af3ca173992 894 * @brief DeInitializes the audio peripheral.
arostm 0:4af3ca173992 895 */
arostm 0:4af3ca173992 896 void BSP_AUDIO_IN_DeInit(void)
arostm 0:4af3ca173992 897 {
arostm 0:4af3ca173992 898 if(hAudioIn.InputDevice != INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 899 {
arostm 0:4af3ca173992 900 /* MSP filters/channels initialization */
arostm 0:4af3ca173992 901 BSP_AUDIO_IN_MspDeInit(NULL);
arostm 0:4af3ca173992 902
arostm 0:4af3ca173992 903 DFSDMx_DeInit();
arostm 0:4af3ca173992 904 }
arostm 0:4af3ca173992 905 else
arostm 0:4af3ca173992 906 {
arostm 0:4af3ca173992 907 I2Sx_In_DeInit();
arostm 0:4af3ca173992 908 }
arostm 0:4af3ca173992 909 }
arostm 0:4af3ca173992 910
arostm 0:4af3ca173992 911 /**
arostm 0:4af3ca173992 912 * @brief Initializes default configuration of the Digital Filter for Sigma-Delta Modulators interface (DFSDM).
arostm 0:4af3ca173992 913 * @param InputDevice: The microphone to be configured. Can be INPUT_DEVICE_DIGITAL_MIC1..INPUT_DEVICE_DIGITAL_MIC5
arostm 0:4af3ca173992 914 * @note Channel output Clock Divider and Filter Oversampling are calculated as follow:
arostm 0:4af3ca173992 915 * - Clock_Divider = CLK(input DFSDM)/CLK(micro) with
arostm 0:4af3ca173992 916 * 1MHZ < CLK(micro) < 3.2MHZ (TYP 2.4MHZ for MP34DT01TR)
arostm 0:4af3ca173992 917 * - Oversampling = CLK(input DFSDM)/(Clock_Divider * AudioFreq)
arostm 0:4af3ca173992 918 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 919 */
arostm 0:4af3ca173992 920 uint8_t BSP_AUDIO_IN_ConfigMicDefault(uint32_t InputDevice)
arostm 0:4af3ca173992 921 {
arostm 0:4af3ca173992 922 uint32_t i = 0, mic_init[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 923 uint32_t filter_ch = 0, mic_num = 0;
arostm 0:4af3ca173992 924
arostm 0:4af3ca173992 925 DFSDM_Filter_TypeDef* FilterInstnace[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_MIC1_FILTER, AUDIO_DFSDMx_MIC2_FILTER, AUDIO_DFSDMx_MIC3_FILTER, AUDIO_DFSDMx_MIC4_FILTER, AUDIO_DFSDMx_MIC5_FILTER};
arostm 0:4af3ca173992 926 DFSDM_Channel_TypeDef* ChannelInstnace[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_MIC1_CHANNEL, AUDIO_DFSDMx_MIC2_CHANNEL, AUDIO_DFSDMx_MIC3_CHANNEL, AUDIO_DFSDMx_MIC4_CHANNEL, AUDIO_DFSDMx_MIC5_CHANNEL};
arostm 0:4af3ca173992 927 uint32_t DigitalMicPins[DFSDM_MIC_NUMBER] = {DFSDM_CHANNEL_SAME_CHANNEL_PINS, DFSDM_CHANNEL_SAME_CHANNEL_PINS, DFSDM_CHANNEL_FOLLOWING_CHANNEL_PINS, DFSDM_CHANNEL_SAME_CHANNEL_PINS, DFSDM_CHANNEL_FOLLOWING_CHANNEL_PINS};
arostm 0:4af3ca173992 928 uint32_t DigitalMicType[DFSDM_MIC_NUMBER] = {DFSDM_CHANNEL_SPI_RISING, DFSDM_CHANNEL_SPI_RISING, DFSDM_CHANNEL_SPI_FALLING, DFSDM_CHANNEL_SPI_RISING, DFSDM_CHANNEL_SPI_FALLING};
arostm 0:4af3ca173992 929 uint32_t Channel4Filter[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_MIC1_CHANNEL_FOR_FILTER, AUDIO_DFSDMx_MIC2_CHANNEL_FOR_FILTER, AUDIO_DFSDMx_MIC3_CHANNEL_FOR_FILTER, AUDIO_DFSDMx_MIC4_CHANNEL_FOR_FILTER, AUDIO_DFSDMx_MIC5_CHANNEL_FOR_FILTER};
arostm 0:4af3ca173992 930
arostm 0:4af3ca173992 931 for(i = 0; i < hAudioIn.ChannelNbr; i++)
arostm 0:4af3ca173992 932 {
arostm 0:4af3ca173992 933 if(((InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] != 1))
arostm 0:4af3ca173992 934 {
arostm 0:4af3ca173992 935 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC1);
arostm 0:4af3ca173992 936 }
arostm 0:4af3ca173992 937 else if(((InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] != 1))
arostm 0:4af3ca173992 938 {
arostm 0:4af3ca173992 939 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC2);
arostm 0:4af3ca173992 940 }
arostm 0:4af3ca173992 941 else if(((InputDevice & INPUT_DEVICE_DIGITAL_MIC3) == INPUT_DEVICE_DIGITAL_MIC3) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)] != 1))
arostm 0:4af3ca173992 942 {
arostm 0:4af3ca173992 943 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC3);
arostm 0:4af3ca173992 944 }
arostm 0:4af3ca173992 945 else if(((InputDevice & INPUT_DEVICE_DIGITAL_MIC4) == INPUT_DEVICE_DIGITAL_MIC4) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)] != 1))
arostm 0:4af3ca173992 946 {
arostm 0:4af3ca173992 947 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC4);
arostm 0:4af3ca173992 948 }
arostm 0:4af3ca173992 949 else if(((InputDevice & INPUT_DEVICE_DIGITAL_MIC5) == INPUT_DEVICE_DIGITAL_MIC5) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)] != 1))
arostm 0:4af3ca173992 950 {
arostm 0:4af3ca173992 951 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC5);
arostm 0:4af3ca173992 952 }
arostm 0:4af3ca173992 953
arostm 0:4af3ca173992 954 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 955
Daniel_Lee 4:c051317d4051 956 /* Filter **********************************************************************************************************/
Daniel_Lee 4:c051317d4051 957 if(hAudioInDfsdmFilter[mic_num].Instance != NULL)
Daniel_Lee 4:c051317d4051 958 {
Daniel_Lee 4:c051317d4051 959 if(HAL_OK != HAL_DFSDM_FilterDeInit(&hAudioInDfsdmFilter[mic_num]))
Daniel_Lee 4:c051317d4051 960 {
Daniel_Lee 4:c051317d4051 961 return AUDIO_ERROR;
Daniel_Lee 4:c051317d4051 962 }
Daniel_Lee 4:c051317d4051 963 /* MIC filters initialization */
Daniel_Lee 4:c051317d4051 964 __HAL_DFSDM_FILTER_RESET_HANDLE_STATE(&hAudioInDfsdmFilter[mic_num]);
Daniel_Lee 4:c051317d4051 965 }
Daniel_Lee 4:c051317d4051 966
arostm 0:4af3ca173992 967 /* MIC filters initialization */
arostm 0:4af3ca173992 968 __HAL_DFSDM_FILTER_RESET_HANDLE_STATE(&hAudioInDfsdmFilter[mic_num]);
arostm 0:4af3ca173992 969 hAudioInDfsdmFilter[mic_num].Instance = FilterInstnace[mic_num];
arostm 0:4af3ca173992 970 hAudioInDfsdmFilter[mic_num].Init.RegularParam.Trigger = DFSDM_FILTER_SW_TRIGGER;
arostm 0:4af3ca173992 971 hAudioInDfsdmFilter[mic_num].Init.RegularParam.FastMode = ENABLE;
arostm 0:4af3ca173992 972 hAudioInDfsdmFilter[mic_num].Init.RegularParam.DmaMode = ENABLE;
arostm 0:4af3ca173992 973 hAudioInDfsdmFilter[mic_num].Init.InjectedParam.Trigger = DFSDM_FILTER_SW_TRIGGER;
arostm 0:4af3ca173992 974 hAudioInDfsdmFilter[mic_num].Init.InjectedParam.ScanMode = DISABLE;
arostm 0:4af3ca173992 975 hAudioInDfsdmFilter[mic_num].Init.InjectedParam.DmaMode = DISABLE;
arostm 0:4af3ca173992 976 hAudioInDfsdmFilter[mic_num].Init.InjectedParam.ExtTrigger = DFSDM_FILTER_EXT_TRIG_TIM8_TRGO;
arostm 0:4af3ca173992 977 hAudioInDfsdmFilter[mic_num].Init.InjectedParam.ExtTriggerEdge = DFSDM_FILTER_EXT_TRIG_BOTH_EDGES;
arostm 0:4af3ca173992 978 hAudioInDfsdmFilter[mic_num].Init.FilterParam.SincOrder = DFSDM_FILTER_ORDER(hAudioIn.Frequency);
arostm 0:4af3ca173992 979 hAudioInDfsdmFilter[mic_num].Init.FilterParam.Oversampling = DFSDM_OVER_SAMPLING(hAudioIn.Frequency);
arostm 0:4af3ca173992 980 hAudioInDfsdmFilter[mic_num].Init.FilterParam.IntOversampling = 1;
arostm 0:4af3ca173992 981
arostm 0:4af3ca173992 982 if(HAL_OK != HAL_DFSDM_FilterInit(&hAudioInDfsdmFilter[mic_num]))
arostm 0:4af3ca173992 983 {
arostm 0:4af3ca173992 984 return AUDIO_ERROR;
arostm 0:4af3ca173992 985 }
arostm 0:4af3ca173992 986
Daniel_Lee 4:c051317d4051 987 /* Channel **********************************************************************************************************/
Daniel_Lee 4:c051317d4051 988 if(hAudioInDfsdmChannel[mic_num].Instance != NULL)
Daniel_Lee 4:c051317d4051 989 {
Daniel_Lee 4:c051317d4051 990 if(HAL_OK != HAL_DFSDM_ChannelDeInit(&hAudioInDfsdmChannel[mic_num]))
Daniel_Lee 4:c051317d4051 991 {
Daniel_Lee 4:c051317d4051 992 return AUDIO_ERROR;
Daniel_Lee 4:c051317d4051 993 }
Daniel_Lee 4:c051317d4051 994
Daniel_Lee 4:c051317d4051 995 /* MIC channels initialization */
Daniel_Lee 4:c051317d4051 996 __HAL_DFSDM_CHANNEL_RESET_HANDLE_STATE(&hAudioInDfsdmChannel[mic_num]);
Daniel_Lee 4:c051317d4051 997 }
Daniel_Lee 4:c051317d4051 998
arostm 0:4af3ca173992 999 /* MIC channels initialization */
arostm 0:4af3ca173992 1000 __HAL_DFSDM_CHANNEL_RESET_HANDLE_STATE(&hAudioInDfsdmChannel[mic_num]);
arostm 0:4af3ca173992 1001 hAudioInDfsdmChannel[mic_num].Init.OutputClock.Activation = ENABLE;
arostm 0:4af3ca173992 1002 hAudioInDfsdmChannel[mic_num].Init.OutputClock.Selection = DFSDM_CHANNEL_OUTPUT_CLOCK_AUDIO;
arostm 0:4af3ca173992 1003 hAudioInDfsdmChannel[mic_num].Init.OutputClock.Divider = DFSDM_CLOCK_DIVIDER(hAudioIn.Frequency);
arostm 0:4af3ca173992 1004 hAudioInDfsdmChannel[mic_num].Init.Input.Multiplexer = DFSDM_CHANNEL_EXTERNAL_INPUTS;
arostm 0:4af3ca173992 1005 hAudioInDfsdmChannel[mic_num].Init.Input.DataPacking = DFSDM_CHANNEL_STANDARD_MODE;
arostm 0:4af3ca173992 1006 hAudioInDfsdmChannel[mic_num].Init.SerialInterface.SpiClock = DFSDM_CHANNEL_SPI_CLOCK_INTERNAL;
arostm 0:4af3ca173992 1007 hAudioInDfsdmChannel[mic_num].Init.Awd.FilterOrder = DFSDM_CHANNEL_SINC1_ORDER;
arostm 0:4af3ca173992 1008 hAudioInDfsdmChannel[mic_num].Init.Awd.Oversampling = 10;
arostm 0:4af3ca173992 1009 hAudioInDfsdmChannel[mic_num].Init.Offset = 0;
arostm 0:4af3ca173992 1010 hAudioInDfsdmChannel[mic_num].Init.RightBitShift = DFSDM_MIC_BIT_SHIFT(hAudioIn.Frequency);
arostm 0:4af3ca173992 1011 hAudioInDfsdmChannel[mic_num].Instance = ChannelInstnace[mic_num];
arostm 0:4af3ca173992 1012 hAudioInDfsdmChannel[mic_num].Init.Input.Pins = DigitalMicPins[mic_num];
arostm 0:4af3ca173992 1013 hAudioInDfsdmChannel[mic_num].Init.SerialInterface.Type = DigitalMicType[mic_num];
arostm 0:4af3ca173992 1014
arostm 0:4af3ca173992 1015 if(HAL_OK != HAL_DFSDM_ChannelInit(&hAudioInDfsdmChannel[mic_num]))
arostm 0:4af3ca173992 1016 {
arostm 0:4af3ca173992 1017 return AUDIO_ERROR;
arostm 0:4af3ca173992 1018 }
arostm 0:4af3ca173992 1019
arostm 0:4af3ca173992 1020 filter_ch = Channel4Filter[mic_num];
arostm 0:4af3ca173992 1021 /* Configure injected channel */
arostm 0:4af3ca173992 1022 if(HAL_OK != HAL_DFSDM_FilterConfigRegChannel(&hAudioInDfsdmFilter[mic_num], filter_ch, DFSDM_CONTINUOUS_CONV_ON))
arostm 0:4af3ca173992 1023 {
arostm 0:4af3ca173992 1024 return AUDIO_ERROR;
arostm 0:4af3ca173992 1025 }
arostm 0:4af3ca173992 1026 }
arostm 0:4af3ca173992 1027 return AUDIO_OK;
arostm 0:4af3ca173992 1028 }
arostm 0:4af3ca173992 1029
arostm 0:4af3ca173992 1030 /**
arostm 0:4af3ca173992 1031 * @brief Initializes the Digital Filter for Sigma-Delta Modulators interface (DFSDM).
arostm 0:4af3ca173992 1032 * @param InputDevice: The microphone to be configured. Can be INPUT_DEVICE_DIGITAL_MIC1..INPUT_DEVICE_DIGITAL_MIC5
arostm 0:4af3ca173992 1033 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 1034 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1035 */
arostm 0:4af3ca173992 1036 __weak uint8_t BSP_AUDIO_IN_ConfigDigitalMic(uint32_t InputDevice, void *Params)
arostm 0:4af3ca173992 1037 {
arostm 0:4af3ca173992 1038 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 1039 UNUSED(Params);
arostm 0:4af3ca173992 1040
arostm 0:4af3ca173992 1041 /* Default configuration of DFSDM filters and channels */
arostm 0:4af3ca173992 1042 return(BSP_AUDIO_IN_ConfigMicDefault(InputDevice));
arostm 0:4af3ca173992 1043 /* Note: This function can be called at application level and default configuration
arostm 0:4af3ca173992 1044 can be ovewritten to fit user's need */
arostm 0:4af3ca173992 1045 }
arostm 0:4af3ca173992 1046
arostm 0:4af3ca173992 1047 /**
arostm 0:4af3ca173992 1048 * @brief Allocate channel buffer scratch
arostm 0:4af3ca173992 1049 * @param pScratch : pointer to scratch tables.
arostm 0:4af3ca173992 1050 * @param size: size of scratch buffer
arostm 0:4af3ca173992 1051 */
arostm 0:4af3ca173992 1052 uint8_t BSP_AUDIO_IN_AllocScratch (int32_t *pScratch, uint32_t size)
arostm 0:4af3ca173992 1053 {
arostm 0:4af3ca173992 1054 uint32_t idx;
arostm 0:4af3ca173992 1055
arostm 0:4af3ca173992 1056 ScratchSize = size / DEFAULT_AUDIO_IN_CHANNEL_NBR;
arostm 0:4af3ca173992 1057
arostm 0:4af3ca173992 1058 /* copy scratch pointers */
arostm 0:4af3ca173992 1059 for (idx = 0; idx < DEFAULT_AUDIO_IN_CHANNEL_NBR ; idx++)
arostm 0:4af3ca173992 1060 {
arostm 0:4af3ca173992 1061 pScratchBuff[idx] = (int32_t *)(pScratch + idx * ScratchSize);
arostm 0:4af3ca173992 1062 }
arostm 0:4af3ca173992 1063 /* Return AUDIO_OK */
arostm 0:4af3ca173992 1064 return AUDIO_OK;
arostm 0:4af3ca173992 1065 }
arostm 0:4af3ca173992 1066
arostm 0:4af3ca173992 1067 /**
arostm 0:4af3ca173992 1068 * @brief Starts audio recording.
arostm 0:4af3ca173992 1069 * @param pBuf: Main buffer pointer for the recorded data storing
arostm 0:4af3ca173992 1070 * @param size: Current size of the recorded buffer
arostm 0:4af3ca173992 1071 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1072 */
arostm 0:4af3ca173992 1073 uint8_t BSP_AUDIO_IN_Record(uint16_t *pBuf, uint32_t size)
arostm 0:4af3ca173992 1074 {
arostm 0:4af3ca173992 1075 hAudioIn.pRecBuf = pBuf;
arostm 0:4af3ca173992 1076 hAudioIn.RecSize = size;
arostm 0:4af3ca173992 1077 /* Reset Application Buffer Trigger */
arostm 0:4af3ca173992 1078 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1079 AppBuffHalf = 0;
arostm 0:4af3ca173992 1080
arostm 0:4af3ca173992 1081 if (hAudioIn.InputDevice == INPUT_DEVICE_DIGITAL_MIC)
arostm 0:4af3ca173992 1082 {
arostm 0:4af3ca173992 1083 /* Call the Media layer start function for MIC1 channel */
arostm 0:4af3ca173992 1084 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)], pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)], ScratchSize))
arostm 0:4af3ca173992 1085 {
arostm 0:4af3ca173992 1086 return AUDIO_ERROR;
arostm 0:4af3ca173992 1087 }
arostm 0:4af3ca173992 1088
arostm 0:4af3ca173992 1089 /* Call the Media layer start function for MIC2 channel */
arostm 0:4af3ca173992 1090 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)], pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)], ScratchSize))
arostm 0:4af3ca173992 1091 {
arostm 0:4af3ca173992 1092 return AUDIO_ERROR;
arostm 0:4af3ca173992 1093 }
arostm 0:4af3ca173992 1094 }
arostm 0:4af3ca173992 1095 else
arostm 0:4af3ca173992 1096 {
arostm 0:4af3ca173992 1097 /* Start the process to receive the DMA */
arostm 0:4af3ca173992 1098 if (HAL_OK != HAL_I2SEx_TransmitReceive_DMA(&haudio_i2s, pBuf, pBuf, size))
arostm 0:4af3ca173992 1099 {
arostm 0:4af3ca173992 1100 return AUDIO_ERROR;
arostm 0:4af3ca173992 1101 }
arostm 0:4af3ca173992 1102 }
arostm 0:4af3ca173992 1103 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1104 return AUDIO_OK;
arostm 0:4af3ca173992 1105 }
arostm 0:4af3ca173992 1106
arostm 0:4af3ca173992 1107 /**
arostm 0:4af3ca173992 1108 * @brief Starts audio recording.
arostm 0:4af3ca173992 1109 * @param pBuf: Main buffer pointer for the recorded data storing
arostm 0:4af3ca173992 1110 * @param size: Current size of the recorded buffer
arostm 0:4af3ca173992 1111 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1112 */
arostm 0:4af3ca173992 1113 uint8_t BSP_AUDIO_IN_RecordEx(uint32_t *pBuf, uint32_t size)
arostm 0:4af3ca173992 1114 {
arostm 0:4af3ca173992 1115 uint8_t ret = AUDIO_ERROR;
arostm 0:4af3ca173992 1116 hAudioIn.RecSize = size;
arostm 0:4af3ca173992 1117 uint32_t i = 0;
arostm 0:4af3ca173992 1118 uint32_t mic_init[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 1119 if (hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1120 {
arostm 0:4af3ca173992 1121 return ret;
arostm 0:4af3ca173992 1122 }
arostm 0:4af3ca173992 1123 else
arostm 0:4af3ca173992 1124 {
arostm 0:4af3ca173992 1125 hAudioIn.MultiBuffMode = 1;
arostm 0:4af3ca173992 1126 for(i = 0; i < hAudioIn.ChannelNbr; i++)
arostm 0:4af3ca173992 1127 {
arostm 0:4af3ca173992 1128 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] != 1))
arostm 0:4af3ca173992 1129 {
arostm 0:4af3ca173992 1130 /* Call the Media layer start function for MIC1 channel 1 */
arostm 0:4af3ca173992 1131 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)], (int32_t*)pBuf[i], size))
arostm 0:4af3ca173992 1132 {
arostm 0:4af3ca173992 1133 return AUDIO_ERROR;
arostm 0:4af3ca173992 1134 }
arostm 0:4af3ca173992 1135 MicBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = i;
arostm 0:4af3ca173992 1136 mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = 1;
arostm 0:4af3ca173992 1137 }
arostm 0:4af3ca173992 1138 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] != 1))
arostm 0:4af3ca173992 1139 {
arostm 0:4af3ca173992 1140 /* Call the Media layer start function for MIC2 channel 1 */
arostm 0:4af3ca173992 1141 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)], (int32_t*)pBuf[i], size))
arostm 0:4af3ca173992 1142 {
arostm 0:4af3ca173992 1143 return AUDIO_ERROR;
arostm 0:4af3ca173992 1144 }
arostm 0:4af3ca173992 1145 MicBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = i;
arostm 0:4af3ca173992 1146 mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = 1;
arostm 0:4af3ca173992 1147 }
arostm 0:4af3ca173992 1148 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC3) == INPUT_DEVICE_DIGITAL_MIC3) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)] != 1))
arostm 0:4af3ca173992 1149 {
arostm 0:4af3ca173992 1150 /* Call the Media layer start function for MIC3 channel 0 */
arostm 0:4af3ca173992 1151 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)], (int32_t*)pBuf[i], size))
arostm 0:4af3ca173992 1152 {
arostm 0:4af3ca173992 1153 return AUDIO_ERROR;
arostm 0:4af3ca173992 1154 }
arostm 0:4af3ca173992 1155 MicBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)] = i;
arostm 0:4af3ca173992 1156 mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)] = 1;
arostm 0:4af3ca173992 1157 }
arostm 0:4af3ca173992 1158 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC4) == INPUT_DEVICE_DIGITAL_MIC4) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)] != 1))
arostm 0:4af3ca173992 1159 {
arostm 0:4af3ca173992 1160 /* Call the Media layer start function for MIC4 channel 7 */
arostm 0:4af3ca173992 1161 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)], (int32_t*)pBuf[i], size))
arostm 0:4af3ca173992 1162 {
arostm 0:4af3ca173992 1163 return AUDIO_ERROR;
arostm 0:4af3ca173992 1164 }
arostm 0:4af3ca173992 1165 MicBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)] = i;
arostm 0:4af3ca173992 1166 mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)] = 1;
arostm 0:4af3ca173992 1167 }
arostm 0:4af3ca173992 1168 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC5) == INPUT_DEVICE_DIGITAL_MIC5) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)] != 1))
arostm 0:4af3ca173992 1169 {
arostm 0:4af3ca173992 1170 /* Call the Media layer start function for MIC5 channel 6 */
arostm 0:4af3ca173992 1171 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)], (int32_t*)pBuf[i], size))
arostm 0:4af3ca173992 1172 {
arostm 0:4af3ca173992 1173 return AUDIO_ERROR;
arostm 0:4af3ca173992 1174 }
arostm 0:4af3ca173992 1175 MicBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)] = i;
arostm 0:4af3ca173992 1176 mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)] = 1;
arostm 0:4af3ca173992 1177 }
arostm 0:4af3ca173992 1178 }
arostm 0:4af3ca173992 1179 }
arostm 0:4af3ca173992 1180 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1181 return AUDIO_OK;
arostm 0:4af3ca173992 1182 }
arostm 0:4af3ca173992 1183
arostm 0:4af3ca173992 1184 /**
arostm 0:4af3ca173992 1185 * @brief Initializes the I2S MSP.
arostm 0:4af3ca173992 1186 */
arostm 0:4af3ca173992 1187 static void I2Sx_In_MspInit(void)
arostm 0:4af3ca173992 1188 {
arostm 0:4af3ca173992 1189 static DMA_HandleTypeDef hdma_i2s_rx;
arostm 0:4af3ca173992 1190 GPIO_InitTypeDef gpio_init_structure;
arostm 0:4af3ca173992 1191
arostm 0:4af3ca173992 1192 /* Enable I2S clock */
arostm 0:4af3ca173992 1193 AUDIO_IN_I2Sx_CLK_ENABLE();
arostm 0:4af3ca173992 1194
arostm 0:4af3ca173992 1195 /* Enable MCK GPIO clock, needed by the codec */
arostm 0:4af3ca173992 1196 AUDIO_OUT_I2Sx_MCK_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1197
arostm 0:4af3ca173992 1198 /* CODEC_I2S pins configuration: MCK pins */
arostm 0:4af3ca173992 1199 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_MCK_PIN;
arostm 0:4af3ca173992 1200 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
arostm 0:4af3ca173992 1201 gpio_init_structure.Pull = GPIO_NOPULL;
arostm 0:4af3ca173992 1202 gpio_init_structure.Speed = GPIO_SPEED_FAST;
arostm 0:4af3ca173992 1203 gpio_init_structure.Alternate = AUDIO_OUT_I2Sx_MCK_AF;
arostm 0:4af3ca173992 1204 HAL_GPIO_Init(AUDIO_OUT_I2Sx_MCK_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 1205
arostm 0:4af3ca173992 1206 /* Enable SD GPIO clock */
arostm 0:4af3ca173992 1207 AUDIO_IN_I2Sx_EXT_SD_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1208 /* CODEC_I2S pin configuration: SD pin */
arostm 0:4af3ca173992 1209 gpio_init_structure.Pin = AUDIO_IN_I2Sx_EXT_SD_PIN;
arostm 0:4af3ca173992 1210 gpio_init_structure.Alternate = AUDIO_IN_I2Sx_EXT_SD_AF;
arostm 0:4af3ca173992 1211 HAL_GPIO_Init(AUDIO_IN_I2Sx_EXT_SD_GPIO_PORT, &gpio_init_structure);
arostm 0:4af3ca173992 1212
arostm 0:4af3ca173992 1213 /* Enable the DMA clock */
arostm 0:4af3ca173992 1214 AUDIO_IN_I2Sx_DMAx_CLK_ENABLE();
arostm 0:4af3ca173992 1215
arostm 0:4af3ca173992 1216 if(haudio_i2s.Instance == AUDIO_IN_I2Sx)
arostm 0:4af3ca173992 1217 {
arostm 0:4af3ca173992 1218 /* Configure the hdma_i2s_rx handle parameters */
arostm 0:4af3ca173992 1219 hdma_i2s_rx.Init.Channel = AUDIO_IN_I2Sx_DMAx_CHANNEL;
arostm 0:4af3ca173992 1220 hdma_i2s_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
arostm 0:4af3ca173992 1221 hdma_i2s_rx.Init.PeriphInc = DMA_PINC_DISABLE;
arostm 0:4af3ca173992 1222 hdma_i2s_rx.Init.MemInc = DMA_MINC_ENABLE;
arostm 0:4af3ca173992 1223 hdma_i2s_rx.Init.PeriphDataAlignment = AUDIO_IN_I2Sx_DMAx_PERIPH_DATA_SIZE;
arostm 0:4af3ca173992 1224 hdma_i2s_rx.Init.MemDataAlignment = AUDIO_IN_I2Sx_DMAx_MEM_DATA_SIZE;
arostm 0:4af3ca173992 1225 hdma_i2s_rx.Init.Mode = DMA_CIRCULAR;
arostm 0:4af3ca173992 1226 hdma_i2s_rx.Init.Priority = DMA_PRIORITY_HIGH;
arostm 0:4af3ca173992 1227 hdma_i2s_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
arostm 0:4af3ca173992 1228 hdma_i2s_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
arostm 0:4af3ca173992 1229 hdma_i2s_rx.Init.MemBurst = DMA_MBURST_SINGLE;
arostm 0:4af3ca173992 1230 hdma_i2s_rx.Init.PeriphBurst = DMA_MBURST_SINGLE;
arostm 0:4af3ca173992 1231
arostm 0:4af3ca173992 1232 hdma_i2s_rx.Instance = AUDIO_IN_I2Sx_DMAx_STREAM;
arostm 0:4af3ca173992 1233
arostm 0:4af3ca173992 1234 /* Associate the DMA handle */
arostm 0:4af3ca173992 1235 __HAL_LINKDMA(&haudio_i2s, hdmarx, hdma_i2s_rx);
arostm 0:4af3ca173992 1236
arostm 0:4af3ca173992 1237 /* Deinitialize the Stream for new transfer */
arostm 0:4af3ca173992 1238 HAL_DMA_DeInit(&hdma_i2s_rx);
arostm 0:4af3ca173992 1239
arostm 0:4af3ca173992 1240 /* Configure the DMA Stream */
arostm 0:4af3ca173992 1241 HAL_DMA_Init(&hdma_i2s_rx);
arostm 0:4af3ca173992 1242 }
arostm 0:4af3ca173992 1243
arostm 0:4af3ca173992 1244 /* I2S DMA IRQ Channel configuration */
arostm 0:4af3ca173992 1245 HAL_NVIC_SetPriority(AUDIO_IN_I2Sx_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0);
arostm 0:4af3ca173992 1246 HAL_NVIC_EnableIRQ(AUDIO_IN_I2Sx_DMAx_IRQ);
arostm 0:4af3ca173992 1247 }
arostm 0:4af3ca173992 1248
arostm 0:4af3ca173992 1249 /**
arostm 0:4af3ca173992 1250 * @brief De-Initializes the I2S MSP.
arostm 0:4af3ca173992 1251 */
arostm 0:4af3ca173992 1252 static void I2Sx_In_MspDeInit(void)
arostm 0:4af3ca173992 1253 {
arostm 0:4af3ca173992 1254 GPIO_InitTypeDef gpio_init_structure;
arostm 0:4af3ca173992 1255
arostm 0:4af3ca173992 1256 /* I2S DMA IRQ Channel deactivation */
arostm 0:4af3ca173992 1257 HAL_NVIC_DisableIRQ(AUDIO_IN_I2Sx_DMAx_IRQ);
arostm 0:4af3ca173992 1258
arostm 0:4af3ca173992 1259 if(haudio_i2s.Instance == AUDIO_IN_I2Sx)
arostm 0:4af3ca173992 1260 {
arostm 0:4af3ca173992 1261 /* Deinitialize the DMA stream */
arostm 0:4af3ca173992 1262 HAL_DMA_DeInit(haudio_i2s.hdmarx);
arostm 0:4af3ca173992 1263 }
arostm 0:4af3ca173992 1264
arostm 0:4af3ca173992 1265 /* Disable I2S peripheral */
arostm 0:4af3ca173992 1266 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 1267
arostm 0:4af3ca173992 1268 /* Deactives CODEC_I2S pins MCK by putting them in input mode */
arostm 0:4af3ca173992 1269 gpio_init_structure.Pin = AUDIO_OUT_I2Sx_MCK_PIN;
arostm 0:4af3ca173992 1270 HAL_GPIO_DeInit(AUDIO_OUT_I2Sx_MCK_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 1271
arostm 0:4af3ca173992 1272 gpio_init_structure.Pin = AUDIO_IN_I2Sx_EXT_SD_PIN;
arostm 0:4af3ca173992 1273 HAL_GPIO_DeInit(AUDIO_IN_I2Sx_EXT_SD_GPIO_PORT, gpio_init_structure.Pin);
arostm 0:4af3ca173992 1274
arostm 0:4af3ca173992 1275 /* Disable I2S clock */
arostm 0:4af3ca173992 1276 AUDIO_IN_I2Sx_CLK_DISABLE();
arostm 0:4af3ca173992 1277 }
arostm 0:4af3ca173992 1278
arostm 0:4af3ca173992 1279 /**
arostm 0:4af3ca173992 1280 * @brief Initializes BSP_AUDIO_IN MSP.
arostm 0:4af3ca173992 1281 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 1282 */
arostm 0:4af3ca173992 1283 __weak void BSP_AUDIO_IN_MspInit(void *Params)
arostm 0:4af3ca173992 1284 {
arostm 0:4af3ca173992 1285 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 1286 UNUSED(Params);
arostm 0:4af3ca173992 1287
arostm 0:4af3ca173992 1288 if(hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1289 {
arostm 0:4af3ca173992 1290 I2Sx_In_MspInit();
arostm 0:4af3ca173992 1291 }
arostm 0:4af3ca173992 1292 else
arostm 0:4af3ca173992 1293 {
arostm 0:4af3ca173992 1294 /* MSP channels initialization */
arostm 0:4af3ca173992 1295 DFSDMx_ChannelMspInit();
arostm 0:4af3ca173992 1296
arostm 0:4af3ca173992 1297 /* MSP filters initialization */
arostm 0:4af3ca173992 1298 DFSDMx_FilterMspInit();
arostm 0:4af3ca173992 1299 }
arostm 0:4af3ca173992 1300 }
arostm 0:4af3ca173992 1301
arostm 0:4af3ca173992 1302 /**
arostm 0:4af3ca173992 1303 * @brief De-Initializes BSP_AUDIO_IN MSP.
arostm 0:4af3ca173992 1304 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 1305 */
arostm 0:4af3ca173992 1306 __weak void BSP_AUDIO_IN_MspDeInit(void *Params)
arostm 0:4af3ca173992 1307 {
arostm 0:4af3ca173992 1308 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 1309 UNUSED(Params);
arostm 0:4af3ca173992 1310
arostm 0:4af3ca173992 1311 if(hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1312 {
arostm 0:4af3ca173992 1313 I2Sx_In_MspDeInit();
arostm 0:4af3ca173992 1314 }
arostm 0:4af3ca173992 1315 else
arostm 0:4af3ca173992 1316 {
arostm 0:4af3ca173992 1317 /* MSP channels initialization */
arostm 0:4af3ca173992 1318 DFSDMx_ChannelMspDeInit();
arostm 0:4af3ca173992 1319
arostm 0:4af3ca173992 1320 /* MSP filters initialization */
arostm 0:4af3ca173992 1321 DFSDMx_FilterMspDeInit();
arostm 0:4af3ca173992 1322 }
arostm 0:4af3ca173992 1323 }
arostm 0:4af3ca173992 1324
arostm 0:4af3ca173992 1325 /**
arostm 0:4af3ca173992 1326 * @brief Clock Config.
arostm 0:4af3ca173992 1327 * @param AudioFreq: Audio frequency used to play the audio stream.
arostm 0:4af3ca173992 1328 * @param Params : pointer on additional configuration parameters, can be NULL.
arostm 0:4af3ca173992 1329 * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency()
arostm 0:4af3ca173992 1330 * Being __weak it can be overwritten by the application
arostm 0:4af3ca173992 1331 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1332 */
arostm 0:4af3ca173992 1333 __weak uint8_t BSP_AUDIO_IN_ClockConfig(uint32_t AudioFreq, void *Params)
arostm 0:4af3ca173992 1334 {
arostm 0:4af3ca173992 1335 RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
arostm 0:4af3ca173992 1336
arostm 0:4af3ca173992 1337 /* Prevent unused argument(s) compilation warning */
arostm 0:4af3ca173992 1338 UNUSED(Params);
arostm 0:4af3ca173992 1339
arostm 0:4af3ca173992 1340 HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 1341
arostm 0:4af3ca173992 1342 /* Set the PLL configuration according to the audio frequency */
arostm 0:4af3ca173992 1343 if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K))
arostm 0:4af3ca173992 1344 {
arostm 0:4af3ca173992 1345 /* Configure PLLI2S prescalers */
arostm 0:4af3ca173992 1346 rcc_ex_clk_init_struct.PeriphClockSelection = (RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_DFSDM | RCC_PERIPHCLK_DFSDM2);
arostm 0:4af3ca173992 1347 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 1348 rcc_ex_clk_init_struct.Dfsdm1ClockSelection = RCC_DFSDM1CLKSOURCE_APB2;
arostm 0:4af3ca173992 1349 rcc_ex_clk_init_struct.Dfsdm2ClockSelection = RCC_DFSDM2CLKSOURCE_APB2;
arostm 0:4af3ca173992 1350 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 1351 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 1352 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 271;
arostm 0:4af3ca173992 1353 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 2;
arostm 0:4af3ca173992 1354
arostm 0:4af3ca173992 1355 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 1356 }
arostm 0:4af3ca173992 1357 else if(AudioFreq == AUDIO_FREQUENCY_96K)
arostm 0:4af3ca173992 1358 {
arostm 0:4af3ca173992 1359 /* I2S clock config */
arostm 0:4af3ca173992 1360 rcc_ex_clk_init_struct.PeriphClockSelection = (RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_DFSDM | RCC_PERIPHCLK_DFSDM2);
arostm 0:4af3ca173992 1361 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 1362 rcc_ex_clk_init_struct.Dfsdm1ClockSelection = RCC_DFSDM1CLKSOURCE_APB2;
arostm 0:4af3ca173992 1363 rcc_ex_clk_init_struct.Dfsdm2ClockSelection = RCC_DFSDM2CLKSOURCE_APB2;
arostm 0:4af3ca173992 1364 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 1365 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 1366 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344;
arostm 0:4af3ca173992 1367 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 2;
arostm 0:4af3ca173992 1368
arostm 0:4af3ca173992 1369 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 1370 }
arostm 0:4af3ca173992 1371 else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_32K, AUDIO_FREQUENCY_48K */
arostm 0:4af3ca173992 1372 {
arostm 0:4af3ca173992 1373 /* I2S clock config
arostm 0:4af3ca173992 1374 PLLI2S_VCO: VCO_344M
arostm 0:4af3ca173992 1375 I2S_CLK(first level) = PLLI2S_VCO/PLLI2SR = 344/7 = 49.142 Mhz
arostm 0:4af3ca173992 1376 I2S_CLK_x = I2S_CLK(first level)/PLLI2SDIVR = 49.142/1 = 49.142 Mhz */
arostm 0:4af3ca173992 1377 rcc_ex_clk_init_struct.PeriphClockSelection = (RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_DFSDM | RCC_PERIPHCLK_DFSDM2);
arostm 0:4af3ca173992 1378 rcc_ex_clk_init_struct.I2sApb1ClockSelection = RCC_I2SAPB1CLKSOURCE_PLLI2S;
arostm 0:4af3ca173992 1379 rcc_ex_clk_init_struct.DfsdmClockSelection = RCC_DFSDM1CLKSOURCE_APB2|RCC_DFSDM2CLKSOURCE_APB2;
arostm 0:4af3ca173992 1380 rcc_ex_clk_init_struct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
arostm 0:4af3ca173992 1381 rcc_ex_clk_init_struct.PLLI2S.PLLI2SM = 8;
arostm 0:4af3ca173992 1382 rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344;
arostm 0:4af3ca173992 1383 rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 7;
arostm 0:4af3ca173992 1384
arostm 0:4af3ca173992 1385 HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
arostm 0:4af3ca173992 1386 }
arostm 0:4af3ca173992 1387
arostm 0:4af3ca173992 1388 if(hAudioIn.InputDevice != INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1389 {
arostm 0:4af3ca173992 1390 /* I2S_APB1 selected as DFSDM audio clock source */
arostm 0:4af3ca173992 1391 __HAL_RCC_DFSDM1AUDIO_CONFIG(RCC_DFSDM1AUDIOCLKSOURCE_I2SAPB1);
arostm 0:4af3ca173992 1392 /* I2S_APB1 selected as DFSDM audio clock source */
arostm 0:4af3ca173992 1393 __HAL_RCC_DFSDM2AUDIO_CONFIG(RCC_DFSDM2AUDIOCLKSOURCE_I2SAPB1);
arostm 0:4af3ca173992 1394 }
arostm 0:4af3ca173992 1395
arostm 0:4af3ca173992 1396 return AUDIO_OK;
arostm 0:4af3ca173992 1397 }
arostm 0:4af3ca173992 1398
arostm 0:4af3ca173992 1399 /**
arostm 0:4af3ca173992 1400 * @brief Regular conversion complete callback.
arostm 0:4af3ca173992 1401 * @note In interrupt mode, user has to read conversion value in this function
arostm 0:4af3ca173992 1402 using HAL_DFSDM_FilterGetRegularValue.
arostm 0:4af3ca173992 1403 * @param hdfsdm_filter : DFSDM filter handle.
arostm 0:4af3ca173992 1404 */
arostm 0:4af3ca173992 1405 void HAL_DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
arostm 0:4af3ca173992 1406 {
arostm 0:4af3ca173992 1407 uint32_t index, input_device = 0;
arostm 0:4af3ca173992 1408
arostm 0:4af3ca173992 1409 if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC1_FILTER)
arostm 0:4af3ca173992 1410 {
arostm 0:4af3ca173992 1411 DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = 1;
arostm 0:4af3ca173992 1412 input_device = INPUT_DEVICE_DIGITAL_MIC1;
arostm 0:4af3ca173992 1413 }
arostm 0:4af3ca173992 1414 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC2_FILTER)
arostm 0:4af3ca173992 1415 {
arostm 0:4af3ca173992 1416 DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = 1;
arostm 0:4af3ca173992 1417 input_device = INPUT_DEVICE_DIGITAL_MIC2;
arostm 0:4af3ca173992 1418 }
arostm 0:4af3ca173992 1419 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC3_FILTER)
arostm 0:4af3ca173992 1420 {
arostm 0:4af3ca173992 1421 input_device = INPUT_DEVICE_DIGITAL_MIC3;
arostm 0:4af3ca173992 1422 }
arostm 0:4af3ca173992 1423 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC4_FILTER)
arostm 0:4af3ca173992 1424 {
arostm 0:4af3ca173992 1425 input_device = INPUT_DEVICE_DIGITAL_MIC4;
arostm 0:4af3ca173992 1426 }
arostm 0:4af3ca173992 1427 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC5_FILTER)
arostm 0:4af3ca173992 1428 {
arostm 0:4af3ca173992 1429 input_device = INPUT_DEVICE_DIGITAL_MIC5;
arostm 0:4af3ca173992 1430 }
arostm 0:4af3ca173992 1431
arostm 0:4af3ca173992 1432 if(hAudioIn.MultiBuffMode == 1)
arostm 0:4af3ca173992 1433 {
arostm 0:4af3ca173992 1434 BSP_AUDIO_IN_TransferComplete_CallBackEx(input_device);
arostm 0:4af3ca173992 1435 }
arostm 0:4af3ca173992 1436 else
arostm 0:4af3ca173992 1437 {
arostm 0:4af3ca173992 1438 if((DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] == 1) && (DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] == 1))
arostm 0:4af3ca173992 1439 {
arostm 0:4af3ca173992 1440 if(AppBuffTrigger >= hAudioIn.RecSize)
arostm 0:4af3ca173992 1441 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1442
arostm 0:4af3ca173992 1443 for(index = (ScratchSize/2) ; index < ScratchSize; index++)
arostm 0:4af3ca173992 1444 {
arostm 0:4af3ca173992 1445 hAudioIn.pRecBuf[AppBuffTrigger] = (uint16_t)(SaturaLH((pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)][index] >> 8), -32760, 32760));
arostm 0:4af3ca173992 1446 hAudioIn.pRecBuf[AppBuffTrigger + 1] = (uint16_t)(SaturaLH((pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)][index] >> 8), -32760, 32760));
arostm 0:4af3ca173992 1447 AppBuffTrigger += 2;
arostm 0:4af3ca173992 1448 }
arostm 0:4af3ca173992 1449 DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = DmaRecBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = 0;
arostm 0:4af3ca173992 1450 }
arostm 0:4af3ca173992 1451
arostm 0:4af3ca173992 1452 /* Update Trigger with Remaining Byte before callback if necessary */
arostm 0:4af3ca173992 1453 if(AppBuffTrigger >= hAudioIn.RecSize)
arostm 0:4af3ca173992 1454 {
arostm 0:4af3ca173992 1455 /* Reset Application Buffer Trigger */
arostm 0:4af3ca173992 1456 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1457 AppBuffHalf = 0;
arostm 0:4af3ca173992 1458
arostm 0:4af3ca173992 1459 /* Call the record update function to get the next buffer to fill and its size (size is ignored) */
arostm 0:4af3ca173992 1460 BSP_AUDIO_IN_TransferComplete_CallBack();
arostm 0:4af3ca173992 1461 }
arostm 0:4af3ca173992 1462 else if((AppBuffTrigger >= hAudioIn.RecSize/2))
arostm 0:4af3ca173992 1463 {
arostm 0:4af3ca173992 1464 if(AppBuffHalf == 0)
arostm 0:4af3ca173992 1465 {
arostm 0:4af3ca173992 1466 AppBuffHalf = 1;
arostm 0:4af3ca173992 1467 /* Manage the remaining file size and new address offset: This function
arostm 0:4af3ca173992 1468 should be coded by user (its prototype is already declared in stm32l476g_eval_audio.h) */
arostm 0:4af3ca173992 1469 BSP_AUDIO_IN_HalfTransfer_CallBack();
arostm 0:4af3ca173992 1470 }
arostm 0:4af3ca173992 1471 }
arostm 0:4af3ca173992 1472 }
arostm 0:4af3ca173992 1473 }
arostm 0:4af3ca173992 1474
arostm 0:4af3ca173992 1475 /**
arostm 0:4af3ca173992 1476 * @brief Half regular conversion complete callback.
arostm 0:4af3ca173992 1477 * @param hdfsdm_filter : DFSDM filter handle.
arostm 0:4af3ca173992 1478 */
arostm 0:4af3ca173992 1479 void HAL_DFSDM_FilterRegConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
arostm 0:4af3ca173992 1480 {
arostm 0:4af3ca173992 1481 uint32_t index, input_device = 0;
arostm 0:4af3ca173992 1482
arostm 0:4af3ca173992 1483 if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC1_FILTER)
arostm 0:4af3ca173992 1484 {
arostm 0:4af3ca173992 1485 DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = 1;
arostm 0:4af3ca173992 1486 input_device = INPUT_DEVICE_DIGITAL_MIC1;
arostm 0:4af3ca173992 1487 }
arostm 0:4af3ca173992 1488 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC2_FILTER)
arostm 0:4af3ca173992 1489 {
arostm 0:4af3ca173992 1490 DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = 1;
arostm 0:4af3ca173992 1491 input_device = INPUT_DEVICE_DIGITAL_MIC2;
arostm 0:4af3ca173992 1492 }
arostm 0:4af3ca173992 1493 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC3_FILTER)
arostm 0:4af3ca173992 1494 {
arostm 0:4af3ca173992 1495 input_device = INPUT_DEVICE_DIGITAL_MIC3;
arostm 0:4af3ca173992 1496 }
arostm 0:4af3ca173992 1497 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC4_FILTER)
arostm 0:4af3ca173992 1498 {
arostm 0:4af3ca173992 1499 input_device = INPUT_DEVICE_DIGITAL_MIC4;
arostm 0:4af3ca173992 1500 }
arostm 0:4af3ca173992 1501 else if(hdfsdm_filter->Instance == AUDIO_DFSDMx_MIC5_FILTER)
arostm 0:4af3ca173992 1502 {
arostm 0:4af3ca173992 1503 input_device = INPUT_DEVICE_DIGITAL_MIC5;
arostm 0:4af3ca173992 1504 }
arostm 0:4af3ca173992 1505
arostm 0:4af3ca173992 1506 if(hAudioIn.MultiBuffMode == 1)
arostm 0:4af3ca173992 1507 {
arostm 0:4af3ca173992 1508 BSP_AUDIO_IN_HalfTransfer_CallBackEx(input_device);
arostm 0:4af3ca173992 1509 }
arostm 0:4af3ca173992 1510 else
arostm 0:4af3ca173992 1511 {
arostm 0:4af3ca173992 1512 if((DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] == 1) && (DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] == 1))
arostm 0:4af3ca173992 1513 {
arostm 0:4af3ca173992 1514 if(AppBuffTrigger >= hAudioIn.RecSize)
arostm 0:4af3ca173992 1515 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1516
arostm 0:4af3ca173992 1517 for(index = 0; index < ScratchSize/2; index++)
arostm 0:4af3ca173992 1518 {
arostm 0:4af3ca173992 1519 hAudioIn.pRecBuf[AppBuffTrigger] = (int16_t)(SaturaLH((pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)][index] >> 8), -32760, 32760));
arostm 0:4af3ca173992 1520 hAudioIn.pRecBuf[AppBuffTrigger + 1] = (int16_t)(SaturaLH((pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)][index] >> 8), -32760, 32760));
arostm 0:4af3ca173992 1521 AppBuffTrigger += 2;
arostm 0:4af3ca173992 1522 }
arostm 0:4af3ca173992 1523 DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] = DmaRecHalfBuffCplt[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] = 0;
arostm 0:4af3ca173992 1524 }
arostm 0:4af3ca173992 1525
arostm 0:4af3ca173992 1526
arostm 0:4af3ca173992 1527 /* Update Trigger with Remaining Byte before callback if necessary */
arostm 0:4af3ca173992 1528 if(AppBuffTrigger >= hAudioIn.RecSize)
arostm 0:4af3ca173992 1529 {
arostm 0:4af3ca173992 1530 /* Reset Application Buffer Trigger */
arostm 0:4af3ca173992 1531 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1532 AppBuffHalf = 0;
arostm 0:4af3ca173992 1533
arostm 0:4af3ca173992 1534 /* Call the record update function to get the next buffer to fill and its size (size is ignored) */
arostm 0:4af3ca173992 1535 BSP_AUDIO_IN_TransferComplete_CallBack();
arostm 0:4af3ca173992 1536 }
arostm 0:4af3ca173992 1537 else if((AppBuffTrigger >= hAudioIn.RecSize/2))
arostm 0:4af3ca173992 1538 {
arostm 0:4af3ca173992 1539 if(AppBuffHalf == 0)
arostm 0:4af3ca173992 1540 {
arostm 0:4af3ca173992 1541 AppBuffHalf = 1;
arostm 0:4af3ca173992 1542 /* Manage the remaining file size and new address offset: This function
arostm 0:4af3ca173992 1543 should be coded by user */
arostm 0:4af3ca173992 1544 BSP_AUDIO_IN_HalfTransfer_CallBack();
arostm 0:4af3ca173992 1545 }
arostm 0:4af3ca173992 1546 }
arostm 0:4af3ca173992 1547 }
arostm 0:4af3ca173992 1548 }
arostm 0:4af3ca173992 1549
arostm 0:4af3ca173992 1550 /**
arostm 0:4af3ca173992 1551 * @brief Half reception complete callback.
arostm 0:4af3ca173992 1552 * @param hi2s : I2S handle.
arostm 0:4af3ca173992 1553 */
arostm 0:4af3ca173992 1554 void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
arostm 0:4af3ca173992 1555 {
arostm 0:4af3ca173992 1556 /* Manage the remaining file size and new address offset: This function
arostm 0:4af3ca173992 1557 should be coded by user (its prototype is already declared in stm32746g_discovery_audio.h) */
arostm 0:4af3ca173992 1558 BSP_AUDIO_IN_HalfTransfer_CallBack();
arostm 0:4af3ca173992 1559 }
arostm 0:4af3ca173992 1560
arostm 0:4af3ca173992 1561 /**
arostm 0:4af3ca173992 1562 * @brief Reception complete callback.
arostm 0:4af3ca173992 1563 * @param hi2s : I2S handle.
arostm 0:4af3ca173992 1564 */
arostm 0:4af3ca173992 1565 void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
arostm 0:4af3ca173992 1566 {
arostm 0:4af3ca173992 1567 /* Call the record update function to get the next buffer to fill and its size (size is ignored) */
arostm 0:4af3ca173992 1568 BSP_AUDIO_IN_TransferComplete_CallBack();
arostm 0:4af3ca173992 1569 }
arostm 0:4af3ca173992 1570
arostm 0:4af3ca173992 1571 /**
arostm 0:4af3ca173992 1572 * @brief Stops audio recording.
arostm 0:4af3ca173992 1573 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1574 */
arostm 0:4af3ca173992 1575 uint8_t BSP_AUDIO_IN_Stop(void)
arostm 0:4af3ca173992 1576 {
arostm 0:4af3ca173992 1577 AppBuffTrigger = 0;
arostm 0:4af3ca173992 1578 AppBuffHalf = 0;
arostm 0:4af3ca173992 1579
arostm 0:4af3ca173992 1580 if (hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1581 {
arostm 0:4af3ca173992 1582 /* Call the Media layer stop function */
arostm 0:4af3ca173992 1583 if(HAL_OK != HAL_I2S_DMAStop(&haudio_i2s))
arostm 0:4af3ca173992 1584 {
arostm 0:4af3ca173992 1585 return AUDIO_ERROR;
arostm 0:4af3ca173992 1586 }
arostm 0:4af3ca173992 1587 /* Call Audio Codec Stop function */
arostm 0:4af3ca173992 1588 if(audio_drv->Stop(AUDIO_I2C_ADDRESS, CODEC_PDWN_HW) != 0)
arostm 0:4af3ca173992 1589 {
arostm 0:4af3ca173992 1590 return AUDIO_ERROR;
arostm 0:4af3ca173992 1591 }
arostm 0:4af3ca173992 1592 /* Wait at least 100us */
arostm 0:4af3ca173992 1593 HAL_Delay(1);
arostm 0:4af3ca173992 1594 }
arostm 0:4af3ca173992 1595 else /* InputDevice = Digital Mic */
arostm 0:4af3ca173992 1596 {
arostm 0:4af3ca173992 1597 /* Call the Media layer stop function for MIC1 channel */
arostm 0:4af3ca173992 1598 if(AUDIO_OK != BSP_AUDIO_IN_PauseEx(INPUT_DEVICE_DIGITAL_MIC1))
arostm 0:4af3ca173992 1599 {
arostm 0:4af3ca173992 1600 return AUDIO_ERROR;
arostm 0:4af3ca173992 1601 }
arostm 0:4af3ca173992 1602
arostm 0:4af3ca173992 1603 /* Call the Media layer stop function for MIC2 channel */
arostm 0:4af3ca173992 1604 if(AUDIO_OK != BSP_AUDIO_IN_PauseEx(INPUT_DEVICE_DIGITAL_MIC2))
arostm 0:4af3ca173992 1605 {
arostm 0:4af3ca173992 1606 return AUDIO_ERROR;
arostm 0:4af3ca173992 1607 }
arostm 0:4af3ca173992 1608 }
arostm 0:4af3ca173992 1609
arostm 0:4af3ca173992 1610 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1611 return AUDIO_OK;
arostm 0:4af3ca173992 1612 }
arostm 0:4af3ca173992 1613
arostm 0:4af3ca173992 1614 /**
arostm 0:4af3ca173992 1615 * @brief Stops audio recording.
arostm 0:4af3ca173992 1616 * @param InputDevice: Microphone to be stopped. Can be INPUT_DEVICE_DIGITAL_MIC1 .. INPUT_DEVICE_DIGITAL_MIC5.
arostm 0:4af3ca173992 1617 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1618 */
arostm 0:4af3ca173992 1619 uint8_t BSP_AUDIO_IN_StopEx(uint32_t InputDevice)
arostm 0:4af3ca173992 1620 {
arostm 0:4af3ca173992 1621 if((InputDevice < INPUT_DEVICE_DIGITAL_MIC1) || (InputDevice > INPUT_DEVICE_DIGITAL_MIC5))
arostm 0:4af3ca173992 1622 {
arostm 0:4af3ca173992 1623 return AUDIO_ERROR;
arostm 0:4af3ca173992 1624 }
arostm 0:4af3ca173992 1625 else
arostm 0:4af3ca173992 1626 {
arostm 0:4af3ca173992 1627 BSP_AUDIO_IN_PauseEx(InputDevice);
arostm 0:4af3ca173992 1628 }
arostm 0:4af3ca173992 1629
arostm 0:4af3ca173992 1630 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1631 return AUDIO_OK;
arostm 0:4af3ca173992 1632 }
arostm 0:4af3ca173992 1633
arostm 0:4af3ca173992 1634 /**
arostm 0:4af3ca173992 1635 * @brief Pauses the audio file stream.
arostm 0:4af3ca173992 1636 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1637 */
arostm 0:4af3ca173992 1638 uint8_t BSP_AUDIO_IN_Pause(void)
arostm 0:4af3ca173992 1639 {
arostm 0:4af3ca173992 1640 if (hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1641 {
arostm 0:4af3ca173992 1642 return AUDIO_ERROR;
arostm 0:4af3ca173992 1643 }
arostm 0:4af3ca173992 1644 else
arostm 0:4af3ca173992 1645 {
arostm 0:4af3ca173992 1646 /* Call the Media layer stop function */
arostm 0:4af3ca173992 1647 if(HAL_OK != HAL_DFSDM_FilterRegularStop_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)]))
arostm 0:4af3ca173992 1648 {
arostm 0:4af3ca173992 1649 return AUDIO_ERROR;
arostm 0:4af3ca173992 1650 }
arostm 0:4af3ca173992 1651
arostm 0:4af3ca173992 1652 /* Call the Media layer stop function */
arostm 0:4af3ca173992 1653 if(HAL_OK != HAL_DFSDM_FilterRegularStop_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)]))
arostm 0:4af3ca173992 1654 {
arostm 0:4af3ca173992 1655 return AUDIO_ERROR;
arostm 0:4af3ca173992 1656 }
arostm 0:4af3ca173992 1657 }
arostm 0:4af3ca173992 1658 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1659 return AUDIO_OK;
arostm 0:4af3ca173992 1660 }
arostm 0:4af3ca173992 1661
arostm 0:4af3ca173992 1662 /**
arostm 0:4af3ca173992 1663 * @brief Pauses the audio file stream.
arostm 0:4af3ca173992 1664 * @param InputDevice: Microphone to be paused. Can be INPUT_DEVICE_DIGITAL_MIC1 .. INPUT_DEVICE_DIGITAL_MIC5.
arostm 0:4af3ca173992 1665 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1666 */
arostm 0:4af3ca173992 1667 uint8_t BSP_AUDIO_IN_PauseEx(uint32_t InputDevice)
arostm 0:4af3ca173992 1668 {
arostm 0:4af3ca173992 1669 if((InputDevice < INPUT_DEVICE_DIGITAL_MIC1) || (InputDevice > INPUT_DEVICE_DIGITAL_MIC5))
arostm 0:4af3ca173992 1670 {
arostm 0:4af3ca173992 1671 return AUDIO_ERROR;
arostm 0:4af3ca173992 1672 }
arostm 0:4af3ca173992 1673 else
arostm 0:4af3ca173992 1674 {
arostm 0:4af3ca173992 1675 /* Call the Media layer stop function */
arostm 0:4af3ca173992 1676 if(HAL_OK != HAL_DFSDM_FilterRegularStop_DMA(&hAudioInDfsdmFilter[POS_VAL(InputDevice)]))
arostm 0:4af3ca173992 1677 {
arostm 0:4af3ca173992 1678 return AUDIO_ERROR;
arostm 0:4af3ca173992 1679 }
arostm 0:4af3ca173992 1680 }
arostm 0:4af3ca173992 1681 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1682 return AUDIO_OK;
arostm 0:4af3ca173992 1683 }
arostm 0:4af3ca173992 1684
arostm 0:4af3ca173992 1685 /**
arostm 0:4af3ca173992 1686 * @brief Resumes the audio file stream.
arostm 0:4af3ca173992 1687 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1688 */
arostm 0:4af3ca173992 1689 uint8_t BSP_AUDIO_IN_Resume(void)
arostm 0:4af3ca173992 1690 {
arostm 0:4af3ca173992 1691 if (hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
arostm 0:4af3ca173992 1692 {
arostm 0:4af3ca173992 1693 return AUDIO_ERROR;
arostm 0:4af3ca173992 1694 }
arostm 0:4af3ca173992 1695 else
arostm 0:4af3ca173992 1696 {
arostm 0:4af3ca173992 1697 /* Call the Media layer start function for MIC2 channel */
arostm 0:4af3ca173992 1698 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)], pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)], ScratchSize))
arostm 0:4af3ca173992 1699 {
arostm 0:4af3ca173992 1700 return AUDIO_ERROR;
arostm 0:4af3ca173992 1701 }
arostm 0:4af3ca173992 1702
arostm 0:4af3ca173992 1703 /* Call the Media layer start function for MIC1 channel */
arostm 0:4af3ca173992 1704 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)], pScratchBuff[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)], ScratchSize))
arostm 0:4af3ca173992 1705 {
arostm 0:4af3ca173992 1706 return AUDIO_ERROR;
arostm 0:4af3ca173992 1707 }
arostm 0:4af3ca173992 1708 }
arostm 0:4af3ca173992 1709 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1710 return AUDIO_OK;
arostm 0:4af3ca173992 1711 }
arostm 0:4af3ca173992 1712
arostm 0:4af3ca173992 1713 /**
arostm 0:4af3ca173992 1714 * @brief Resumes the audio file stream.
arostm 0:4af3ca173992 1715 * @param pBuf: Main buffer pointer for the recorded data storing
arostm 0:4af3ca173992 1716 * @param InputDevice: Microphone to be paused. Can be INPUT_DEVICE_DIGITAL_MIC1 .. INPUT_DEVICE_DIGITAL_MIC5.
arostm 0:4af3ca173992 1717 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1718 */
arostm 0:4af3ca173992 1719 uint8_t BSP_AUDIO_IN_ResumeEx(uint32_t *pBuf, uint32_t InputDevice)
arostm 0:4af3ca173992 1720 {
arostm 0:4af3ca173992 1721 if((InputDevice < INPUT_DEVICE_DIGITAL_MIC1) || (InputDevice > INPUT_DEVICE_DIGITAL_MIC5))
arostm 0:4af3ca173992 1722 {
arostm 0:4af3ca173992 1723 return AUDIO_ERROR;
arostm 0:4af3ca173992 1724 }
arostm 0:4af3ca173992 1725 else
arostm 0:4af3ca173992 1726 {
arostm 0:4af3ca173992 1727 /* Call the Media layer stop function */
arostm 0:4af3ca173992 1728 if(HAL_OK != HAL_DFSDM_FilterRegularStart_DMA(&hAudioInDfsdmFilter[POS_VAL(InputDevice)], (int32_t*)pBuf[MicBuff[POS_VAL(InputDevice)]], hAudioIn.RecSize))
arostm 0:4af3ca173992 1729 {
arostm 0:4af3ca173992 1730 return AUDIO_ERROR;
arostm 0:4af3ca173992 1731 }
arostm 0:4af3ca173992 1732 }
arostm 0:4af3ca173992 1733 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1734 return AUDIO_OK;
arostm 0:4af3ca173992 1735 }
arostm 0:4af3ca173992 1736
arostm 0:4af3ca173992 1737 /**
arostm 0:4af3ca173992 1738 * @brief Controls the audio in volume level.
arostm 0:4af3ca173992 1739 * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
arostm 0:4af3ca173992 1740 * Mute and 100 for Max volume level).
arostm 0:4af3ca173992 1741 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1742 */
arostm 0:4af3ca173992 1743 uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume)
arostm 0:4af3ca173992 1744 {
arostm 0:4af3ca173992 1745 /* Set the Global variable AudioInVolume */
arostm 0:4af3ca173992 1746 AudioInVolume = Volume;
arostm 0:4af3ca173992 1747
arostm 0:4af3ca173992 1748 /* Return AUDIO_OK when all operations are correctly done */
arostm 0:4af3ca173992 1749 return AUDIO_OK;
arostm 0:4af3ca173992 1750 }
arostm 0:4af3ca173992 1751
arostm 0:4af3ca173992 1752 /**
arostm 0:4af3ca173992 1753 * @brief User callback when record buffer is filled.
arostm 0:4af3ca173992 1754 */
arostm 0:4af3ca173992 1755 __weak void BSP_AUDIO_IN_TransferComplete_CallBack(void)
arostm 0:4af3ca173992 1756 {
arostm 0:4af3ca173992 1757 /* This function should be implemented by the user application.
arostm 0:4af3ca173992 1758 It is called into this driver when the current buffer is filled
arostm 0:4af3ca173992 1759 to prepare the next buffer pointer and its size. */
arostm 0:4af3ca173992 1760 }
arostm 0:4af3ca173992 1761
arostm 0:4af3ca173992 1762 /**
arostm 0:4af3ca173992 1763 * @brief Manages the DMA Half Transfer complete event.
arostm 0:4af3ca173992 1764 */
arostm 0:4af3ca173992 1765 __weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
arostm 0:4af3ca173992 1766 {
arostm 0:4af3ca173992 1767 /* This function should be implemented by the user application.
arostm 0:4af3ca173992 1768 It is called into this driver when the current buffer is filled
arostm 0:4af3ca173992 1769 to prepare the next buffer pointer and its size. */
arostm 0:4af3ca173992 1770 }
arostm 0:4af3ca173992 1771
arostm 0:4af3ca173992 1772 /**
arostm 0:4af3ca173992 1773 * @brief User callback when record buffer is filled.
arostm 0:4af3ca173992 1774 * @param InputDevice: INPUT_DEVICE_DIGITAL_MIC1 .. INPUT_DEVICE_DIGITAL_MIC5.
arostm 0:4af3ca173992 1775 */
arostm 0:4af3ca173992 1776 __weak void BSP_AUDIO_IN_TransferComplete_CallBackEx(uint32_t InputDevice)
arostm 0:4af3ca173992 1777 {
arostm 0:4af3ca173992 1778 /* This function should be implemented by the user application.
arostm 0:4af3ca173992 1779 It is called into this driver when the current buffer is filled
arostm 0:4af3ca173992 1780 to prepare the next buffer pointer and its size. */
arostm 0:4af3ca173992 1781 }
arostm 0:4af3ca173992 1782
arostm 0:4af3ca173992 1783 /**
arostm 0:4af3ca173992 1784 * @brief User callback when record buffer is filled.
arostm 0:4af3ca173992 1785 * @param InputDevice: INPUT_DEVICE_DIGITAL_MIC1 .. INPUT_DEVICE_DIGITAL_MIC5.
arostm 0:4af3ca173992 1786 */
arostm 0:4af3ca173992 1787 __weak void BSP_AUDIO_IN_HalfTransfer_CallBackEx(uint32_t InputDevice)
arostm 0:4af3ca173992 1788 {
arostm 0:4af3ca173992 1789 /* This function should be implemented by the user application.
arostm 0:4af3ca173992 1790 It is called into this driver when the current buffer is filled
arostm 0:4af3ca173992 1791 to prepare the next buffer pointer and its size. */
arostm 0:4af3ca173992 1792 }
arostm 0:4af3ca173992 1793
arostm 0:4af3ca173992 1794 /**
arostm 0:4af3ca173992 1795 * @brief Audio IN Error callback function.
arostm 0:4af3ca173992 1796 */
arostm 0:4af3ca173992 1797 __weak void BSP_AUDIO_IN_Error_Callback(void)
arostm 0:4af3ca173992 1798 {
arostm 0:4af3ca173992 1799 /* This function is called when an Interrupt due to transfer error on or peripheral
arostm 0:4af3ca173992 1800 error occurs. */
arostm 0:4af3ca173992 1801 }
arostm 0:4af3ca173992 1802
arostm 0:4af3ca173992 1803 /**
arostm 0:4af3ca173992 1804 * @}
arostm 0:4af3ca173992 1805 */
arostm 0:4af3ca173992 1806
arostm 0:4af3ca173992 1807 /*******************************************************************************
arostm 0:4af3ca173992 1808 Static Functions
arostm 0:4af3ca173992 1809 *******************************************************************************/
arostm 0:4af3ca173992 1810
arostm 0:4af3ca173992 1811 /**
arostm 0:4af3ca173992 1812 * @brief De-initializes the Digital Filter for Sigma-Delta Modulators interface (DFSDM).
arostm 0:4af3ca173992 1813 * @retval AUDIO_OK if correct communication, else wrong communication
arostm 0:4af3ca173992 1814 */
arostm 0:4af3ca173992 1815 static uint8_t DFSDMx_DeInit(void)
arostm 0:4af3ca173992 1816 {
arostm 0:4af3ca173992 1817 for(uint32_t i = 0; i < DFSDM_MIC_NUMBER; i++)
arostm 0:4af3ca173992 1818 {
arostm 0:4af3ca173992 1819 if(hAudioInDfsdmFilter[i].Instance != NULL)
arostm 0:4af3ca173992 1820 {
arostm 0:4af3ca173992 1821 if(HAL_OK != HAL_DFSDM_FilterDeInit(&hAudioInDfsdmFilter[i]))
arostm 0:4af3ca173992 1822 {
arostm 0:4af3ca173992 1823 return AUDIO_ERROR;
arostm 0:4af3ca173992 1824 }
arostm 0:4af3ca173992 1825 hAudioInDfsdmFilter[i].Instance = NULL;
arostm 0:4af3ca173992 1826 }
arostm 0:4af3ca173992 1827 if(hAudioInDfsdmChannel[i].Instance != NULL)
arostm 0:4af3ca173992 1828 {
arostm 0:4af3ca173992 1829 if(HAL_OK != HAL_DFSDM_ChannelDeInit(&hAudioInDfsdmChannel[i]))
arostm 0:4af3ca173992 1830 {
arostm 0:4af3ca173992 1831 return AUDIO_ERROR;
arostm 0:4af3ca173992 1832 }
arostm 0:4af3ca173992 1833 hAudioInDfsdmChannel[i].Instance = NULL;
arostm 0:4af3ca173992 1834 }
arostm 0:4af3ca173992 1835 }
arostm 0:4af3ca173992 1836 return AUDIO_OK;
arostm 0:4af3ca173992 1837 }
arostm 0:4af3ca173992 1838
arostm 0:4af3ca173992 1839 /**
arostm 0:4af3ca173992 1840 * @brief Initializes the DFSDM channel MSP.
arostm 0:4af3ca173992 1841 */
arostm 0:4af3ca173992 1842 static void DFSDMx_ChannelMspInit(void)
arostm 0:4af3ca173992 1843 {
arostm 0:4af3ca173992 1844 GPIO_InitTypeDef GPIO_InitStruct;
arostm 0:4af3ca173992 1845
arostm 0:4af3ca173992 1846 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
arostm 0:4af3ca173992 1847 GPIO_InitStruct.Pull = GPIO_NOPULL;
arostm 0:4af3ca173992 1848 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
arostm 0:4af3ca173992 1849
arostm 0:4af3ca173992 1850 if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1)
arostm 0:4af3ca173992 1851 {
arostm 0:4af3ca173992 1852 /* Enable DFSDM clock */
arostm 0:4af3ca173992 1853 AUDIO_DFSDMx_MIC1_CLK_ENABLE();
arostm 0:4af3ca173992 1854 /* Enable GPIO clock */
arostm 0:4af3ca173992 1855 AUDIO_DFSDMx_MIC1_CKOUT_DMIC_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1856
arostm 0:4af3ca173992 1857 /* DFSDM MIC1 pins configuration: DFSDM_CKOUT, DMIC_DATIN pins -------------*/
arostm 0:4af3ca173992 1858 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC1_CKOUT_PIN;
arostm 0:4af3ca173992 1859 GPIO_InitStruct.Alternate = AUDIO_DFSDMx_MIC1_CKOUT_DMIC_AF;
arostm 0:4af3ca173992 1860 HAL_GPIO_Init(AUDIO_DFSDMx_MIC1_CKOUT_DMIC_GPIO_PORT, &GPIO_InitStruct);
arostm 0:4af3ca173992 1861
arostm 0:4af3ca173992 1862 AUDIO_DFSDMx_MIC1_DMIC_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1863 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC1_DMIC_PIN;
arostm 0:4af3ca173992 1864 GPIO_InitStruct.Alternate = AUDIO_DFSDMx_MIC1_DMIC_AF;
arostm 0:4af3ca173992 1865 HAL_GPIO_Init(AUDIO_DFSDMx_MIC1_DMIC_GPIO_PORT, &GPIO_InitStruct);
arostm 0:4af3ca173992 1866 }
arostm 0:4af3ca173992 1867
arostm 0:4af3ca173992 1868 if(hAudioIn.InputDevice > INPUT_DEVICE_DIGITAL_MIC1)
arostm 0:4af3ca173992 1869 {
arostm 0:4af3ca173992 1870 /* Enable DFSDM clock */
arostm 0:4af3ca173992 1871 AUDIO_DFSDMx_MIC2_5_CLK_ENABLE();
arostm 0:4af3ca173992 1872 /* Enable GPIO clock */
arostm 0:4af3ca173992 1873 AUDIO_DFSDMx_MIC2_5_CKOUT_DMIC_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1874
arostm 0:4af3ca173992 1875 /* DFSDM MIC2 pins configuration: DFSDM_CKOUT, DMIC_DATIN pins -------------*/
arostm 0:4af3ca173992 1876 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC2_5_CKOUT_PIN;
arostm 0:4af3ca173992 1877 GPIO_InitStruct.Alternate = AUDIO_DFSDMx_MIC2_5_CKOUT_DMIC_AF;
arostm 0:4af3ca173992 1878 HAL_GPIO_Init(AUDIO_DFSDMx_MIC2_5_CKOUT_DMIC_GPIO_PORT, &GPIO_InitStruct);
arostm 0:4af3ca173992 1879
arostm 0:4af3ca173992 1880 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2) ||\
arostm 0:4af3ca173992 1881 ((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC3) == INPUT_DEVICE_DIGITAL_MIC3))
arostm 0:4af3ca173992 1882 {
arostm 0:4af3ca173992 1883 AUDIO_DFSDMx_MIC23_DMIC_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1884 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC23_DMIC_PIN;
arostm 0:4af3ca173992 1885 GPIO_InitStruct.Alternate = AUDIO_DFSDMx_MIC23_DMIC_AF;
arostm 0:4af3ca173992 1886 HAL_GPIO_Init(AUDIO_DFSDMx_MIC23_DMIC_GPIO_PORT, &GPIO_InitStruct);
arostm 0:4af3ca173992 1887 }
arostm 0:4af3ca173992 1888
arostm 0:4af3ca173992 1889 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC4) == INPUT_DEVICE_DIGITAL_MIC4) ||\
arostm 0:4af3ca173992 1890 ((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC5) == INPUT_DEVICE_DIGITAL_MIC5))
arostm 0:4af3ca173992 1891 {
arostm 0:4af3ca173992 1892
arostm 0:4af3ca173992 1893 AUDIO_DFSDMx_MIC45_DMIC_GPIO_CLK_ENABLE();
arostm 0:4af3ca173992 1894 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC45_DMIC_PIN;
arostm 0:4af3ca173992 1895 GPIO_InitStruct.Alternate = AUDIO_DFSDMx_MIC45_DMIC_AF;
arostm 0:4af3ca173992 1896 HAL_GPIO_Init(AUDIO_DFSDMx_MIC45_DMIC_GPIO_PORT, &GPIO_InitStruct);
arostm 0:4af3ca173992 1897 }
arostm 0:4af3ca173992 1898 }
arostm 0:4af3ca173992 1899 }
arostm 0:4af3ca173992 1900
arostm 0:4af3ca173992 1901 /**
arostm 0:4af3ca173992 1902 * @brief DeInitializes the DFSDM channel MSP.
arostm 0:4af3ca173992 1903 */
arostm 0:4af3ca173992 1904 static void DFSDMx_ChannelMspDeInit(void)
arostm 0:4af3ca173992 1905 {
arostm 0:4af3ca173992 1906 GPIO_InitTypeDef GPIO_InitStruct;
arostm 0:4af3ca173992 1907
arostm 0:4af3ca173992 1908 if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1)
arostm 0:4af3ca173992 1909 {
arostm 0:4af3ca173992 1910 /* DFSDM MIC1 pins configuration: DFSDM_CKOUT, DMIC_DATIN pins -------------*/
arostm 0:4af3ca173992 1911 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC1_CKOUT_PIN;
arostm 0:4af3ca173992 1912 HAL_GPIO_DeInit(AUDIO_DFSDMx_MIC1_CKOUT_DMIC_GPIO_PORT, GPIO_InitStruct.Pin);
arostm 0:4af3ca173992 1913
arostm 0:4af3ca173992 1914 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC1_DMIC_PIN;
arostm 0:4af3ca173992 1915 HAL_GPIO_DeInit(AUDIO_DFSDMx_MIC1_DMIC_GPIO_PORT, GPIO_InitStruct.Pin);
arostm 0:4af3ca173992 1916 }
arostm 0:4af3ca173992 1917
arostm 0:4af3ca173992 1918 if(hAudioIn.InputDevice > INPUT_DEVICE_DIGITAL_MIC1)
arostm 0:4af3ca173992 1919 {
arostm 0:4af3ca173992 1920 /* DFSDM MIC2, MIC3, MIC4 and MIC5 pins configuration: DFSDM_CKOUT pin -----*/
arostm 0:4af3ca173992 1921 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC2_5_CKOUT_PIN;
arostm 0:4af3ca173992 1922 HAL_GPIO_DeInit(AUDIO_DFSDMx_MIC2_5_CKOUT_DMIC_GPIO_PORT, GPIO_InitStruct.Pin);
arostm 0:4af3ca173992 1923
arostm 0:4af3ca173992 1924 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2) ||\
arostm 0:4af3ca173992 1925 ((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC3) == INPUT_DEVICE_DIGITAL_MIC3))
arostm 0:4af3ca173992 1926 {
arostm 0:4af3ca173992 1927 /* DFSDM MIC2, MIC3 pins configuration: DMIC_DATIN pin -----*/
arostm 0:4af3ca173992 1928 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC23_DMIC_PIN;
arostm 0:4af3ca173992 1929 HAL_GPIO_DeInit(AUDIO_DFSDMx_MIC23_DMIC_GPIO_PORT, GPIO_InitStruct.Pin);
arostm 0:4af3ca173992 1930 }
arostm 0:4af3ca173992 1931
arostm 0:4af3ca173992 1932 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC4) == INPUT_DEVICE_DIGITAL_MIC4) ||\
arostm 0:4af3ca173992 1933 ((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC5) == INPUT_DEVICE_DIGITAL_MIC5))
arostm 0:4af3ca173992 1934 {
arostm 0:4af3ca173992 1935 /* DFSDM MIC4, MIC5 pins configuration: DMIC_DATIN pin -----*/
arostm 0:4af3ca173992 1936 GPIO_InitStruct.Pin = AUDIO_DFSDMx_MIC45_DMIC_PIN;
arostm 0:4af3ca173992 1937 HAL_GPIO_DeInit(AUDIO_DFSDMx_MIC45_DMIC_GPIO_PORT, GPIO_InitStruct.Pin);
arostm 0:4af3ca173992 1938 }
arostm 0:4af3ca173992 1939 }
arostm 0:4af3ca173992 1940 }
arostm 0:4af3ca173992 1941
arostm 0:4af3ca173992 1942 /**
arostm 0:4af3ca173992 1943 * @brief Initializes the DFSDM filter MSP.
arostm 0:4af3ca173992 1944 */
arostm 0:4af3ca173992 1945 static void DFSDMx_FilterMspInit(void)
arostm 0:4af3ca173992 1946 {
arostm 0:4af3ca173992 1947 uint32_t i = 0, mic_num = 0, mic_init[DFSDM_MIC_NUMBER] = {0};
arostm 0:4af3ca173992 1948 IRQn_Type AUDIO_DFSDM_DMAx_MIC_IRQHandler[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_DMAx_MIC1_IRQ, AUDIO_DFSDMx_DMAx_MIC2_IRQ, AUDIO_DFSDMx_DMAx_MIC3_IRQ, AUDIO_DFSDMx_DMAx_MIC4_IRQ, AUDIO_DFSDMx_DMAx_MIC5_IRQ};
arostm 0:4af3ca173992 1949 DMA_Stream_TypeDef* AUDIO_DFSDMx_DMAx_MIC_STREAM[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_DMAx_MIC1_STREAM, AUDIO_DFSDMx_DMAx_MIC2_STREAM, AUDIO_DFSDMx_DMAx_MIC3_STREAM, AUDIO_DFSDMx_DMAx_MIC4_STREAM, AUDIO_DFSDMx_DMAx_MIC5_STREAM};
arostm 0:4af3ca173992 1950 uint32_t AUDIO_DFSDMx_DMAx_MIC_CHANNEL[DFSDM_MIC_NUMBER] = {AUDIO_DFSDMx_DMAx_MIC1_CHANNEL, AUDIO_DFSDMx_DMAx_MIC2_CHANNEL, AUDIO_DFSDMx_DMAx_MIC3_CHANNEL, AUDIO_DFSDMx_DMAx_MIC4_CHANNEL, AUDIO_DFSDMx_DMAx_MIC5_CHANNEL};
arostm 0:4af3ca173992 1951
arostm 0:4af3ca173992 1952 /* Enable the DMA clock */
arostm 0:4af3ca173992 1953 AUDIO_DFSDMx_DMAx_CLK_ENABLE();
arostm 0:4af3ca173992 1954
arostm 0:4af3ca173992 1955 for(i = 0; i < hAudioIn.ChannelNbr; i++)
arostm 0:4af3ca173992 1956 {
arostm 0:4af3ca173992 1957 if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)] != 1))
arostm 0:4af3ca173992 1958 {
arostm 0:4af3ca173992 1959 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC1);
arostm 0:4af3ca173992 1960 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 1961 }
arostm 0:4af3ca173992 1962 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)] != 1))
arostm 0:4af3ca173992 1963 {
arostm 0:4af3ca173992 1964 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC2);
arostm 0:4af3ca173992 1965 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 1966 }
arostm 0:4af3ca173992 1967 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC3) == INPUT_DEVICE_DIGITAL_MIC3) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC3)] != 1))
arostm 0:4af3ca173992 1968 {
arostm 0:4af3ca173992 1969 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC3);
arostm 0:4af3ca173992 1970 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 1971 }
arostm 0:4af3ca173992 1972 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC4) == INPUT_DEVICE_DIGITAL_MIC4) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC4)] != 1))
arostm 0:4af3ca173992 1973 {
arostm 0:4af3ca173992 1974 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC4);
arostm 0:4af3ca173992 1975 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 1976 }
arostm 0:4af3ca173992 1977 else if(((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC5) == INPUT_DEVICE_DIGITAL_MIC5) && (mic_init[POS_VAL(INPUT_DEVICE_DIGITAL_MIC5)] != 1))
arostm 0:4af3ca173992 1978 {
arostm 0:4af3ca173992 1979 mic_num = POS_VAL(INPUT_DEVICE_DIGITAL_MIC5);
arostm 0:4af3ca173992 1980 mic_init[mic_num] = 1;
arostm 0:4af3ca173992 1981 }
arostm 0:4af3ca173992 1982
arostm 0:4af3ca173992 1983 /* Configure the hDmaDfsdm[i] handle parameters */
arostm 0:4af3ca173992 1984 hDmaDfsdm[mic_num].Init.Channel = AUDIO_DFSDMx_DMAx_MIC_CHANNEL[mic_num];
arostm 0:4af3ca173992 1985 hDmaDfsdm[mic_num].Instance = AUDIO_DFSDMx_DMAx_MIC_STREAM[mic_num];
arostm 0:4af3ca173992 1986 hDmaDfsdm[mic_num].Init.Direction = DMA_PERIPH_TO_MEMORY;
arostm 0:4af3ca173992 1987 hDmaDfsdm[mic_num].Init.PeriphInc = DMA_PINC_DISABLE;
arostm 0:4af3ca173992 1988 hDmaDfsdm[mic_num].Init.MemInc = DMA_MINC_ENABLE;
arostm 0:4af3ca173992 1989 hDmaDfsdm[mic_num].Init.PeriphDataAlignment = AUDIO_DFSDMx_DMAx_PERIPH_DATA_SIZE;
arostm 0:4af3ca173992 1990 hDmaDfsdm[mic_num].Init.MemDataAlignment = AUDIO_DFSDMx_DMAx_MEM_DATA_SIZE;
arostm 0:4af3ca173992 1991 hDmaDfsdm[mic_num].Init.Mode = DMA_CIRCULAR;
arostm 0:4af3ca173992 1992 hDmaDfsdm[mic_num].Init.Priority = DMA_PRIORITY_HIGH;
arostm 0:4af3ca173992 1993 hDmaDfsdm[mic_num].Init.FIFOMode = DMA_FIFOMODE_DISABLE;
arostm 0:4af3ca173992 1994 hDmaDfsdm[mic_num].Init.MemBurst = DMA_MBURST_SINGLE;
arostm 0:4af3ca173992 1995 hDmaDfsdm[mic_num].Init.PeriphBurst = DMA_PBURST_SINGLE;
arostm 0:4af3ca173992 1996 hDmaDfsdm[mic_num].State = HAL_DMA_STATE_RESET;
arostm 0:4af3ca173992 1997
arostm 0:4af3ca173992 1998 /* Associate the DMA handle */
arostm 0:4af3ca173992 1999 __HAL_LINKDMA(&hAudioInDfsdmFilter[mic_num], hdmaReg, hDmaDfsdm[mic_num]);
arostm 0:4af3ca173992 2000
arostm 0:4af3ca173992 2001 /* Reset DMA handle state */
arostm 0:4af3ca173992 2002 __HAL_DMA_RESET_HANDLE_STATE(&hDmaDfsdm[mic_num]);
arostm 0:4af3ca173992 2003
arostm 0:4af3ca173992 2004 /* Configure the DMA Channel */
arostm 0:4af3ca173992 2005 HAL_DMA_Init(&hDmaDfsdm[mic_num]);
arostm 0:4af3ca173992 2006
arostm 0:4af3ca173992 2007 /* DMA IRQ Channel configuration */
arostm 0:4af3ca173992 2008 HAL_NVIC_SetPriority(AUDIO_DFSDM_DMAx_MIC_IRQHandler[mic_num], AUDIO_IN_IRQ_PREPRIO, 0);
arostm 0:4af3ca173992 2009 HAL_NVIC_EnableIRQ(AUDIO_DFSDM_DMAx_MIC_IRQHandler[mic_num]);
arostm 0:4af3ca173992 2010 }
arostm 0:4af3ca173992 2011 }
arostm 0:4af3ca173992 2012
arostm 0:4af3ca173992 2013 /**
arostm 0:4af3ca173992 2014 * @brief DeInitializes the DFSDM filter MSP.
arostm 0:4af3ca173992 2015 */
arostm 0:4af3ca173992 2016 static void DFSDMx_FilterMspDeInit(void)
arostm 0:4af3ca173992 2017 {
arostm 0:4af3ca173992 2018 /* Configure the DMA Channel */
arostm 0:4af3ca173992 2019 for(uint32_t i = 0; i < DFSDM_MIC_NUMBER; i++)
arostm 0:4af3ca173992 2020 {
arostm 0:4af3ca173992 2021 if(hDmaDfsdm[i].Instance != NULL)
arostm 0:4af3ca173992 2022 {
arostm 0:4af3ca173992 2023 HAL_DMA_DeInit(&hDmaDfsdm[i]);
arostm 0:4af3ca173992 2024 }
arostm 0:4af3ca173992 2025 }
arostm 0:4af3ca173992 2026 }
arostm 0:4af3ca173992 2027
arostm 0:4af3ca173992 2028 /**
arostm 0:4af3ca173992 2029 * @brief Initializes the Audio Codec audio interface (I2S)
arostm 0:4af3ca173992 2030 * @note This function assumes that the I2S input clock
arostm 0:4af3ca173992 2031 * is already configured and ready to be used.
arostm 0:4af3ca173992 2032 * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
arostm 0:4af3ca173992 2033 */
arostm 0:4af3ca173992 2034 static void I2Sx_In_Init(uint32_t AudioFreq)
arostm 0:4af3ca173992 2035 {
arostm 0:4af3ca173992 2036 /* Initialize the hAudioInI2s and haudio_in_i2sext Instance parameters */
arostm 0:4af3ca173992 2037 haudio_i2s.Instance = AUDIO_IN_I2Sx;
arostm 0:4af3ca173992 2038 haudio_in_i2sext.Instance = I2S3ext;
arostm 0:4af3ca173992 2039
arostm 0:4af3ca173992 2040 /* Disable I2S block */
arostm 0:4af3ca173992 2041 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 2042 __HAL_I2S_DISABLE(&haudio_in_i2sext);
arostm 0:4af3ca173992 2043
arostm 0:4af3ca173992 2044 /* I2S peripheral configuration */
arostm 0:4af3ca173992 2045 haudio_i2s.Init.AudioFreq = AudioFreq;
arostm 0:4af3ca173992 2046 haudio_i2s.Init.ClockSource = I2S_CLOCK_PLL;
arostm 0:4af3ca173992 2047 haudio_i2s.Init.CPOL = I2S_CPOL_LOW;
arostm 0:4af3ca173992 2048 haudio_i2s.Init.DataFormat = I2S_DATAFORMAT_16B;
arostm 0:4af3ca173992 2049 haudio_i2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
arostm 0:4af3ca173992 2050 haudio_i2s.Init.Mode = I2S_MODE_MASTER_TX;
arostm 0:4af3ca173992 2051 haudio_i2s.Init.Standard = I2S_STANDARD_PHILIPS;
arostm 0:4af3ca173992 2052 haudio_i2s.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
arostm 0:4af3ca173992 2053 /* Init the I2S */
arostm 0:4af3ca173992 2054 HAL_I2S_Init(&haudio_i2s);
arostm 0:4af3ca173992 2055
arostm 0:4af3ca173992 2056 /* I2Sext peripheral configuration */
arostm 0:4af3ca173992 2057 haudio_in_i2sext.Init.AudioFreq = AudioFreq;
arostm 0:4af3ca173992 2058 haudio_in_i2sext.Init.ClockSource = I2S_CLOCK_PLL;
arostm 0:4af3ca173992 2059 haudio_in_i2sext.Init.CPOL = I2S_CPOL_HIGH;
arostm 0:4af3ca173992 2060 haudio_in_i2sext.Init.DataFormat = I2S_DATAFORMAT_16B;
arostm 0:4af3ca173992 2061 haudio_in_i2sext.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
arostm 0:4af3ca173992 2062 haudio_in_i2sext.Init.Mode = I2S_MODE_SLAVE_RX;
arostm 0:4af3ca173992 2063 haudio_in_i2sext.Init.Standard = I2S_STANDARD_PHILIPS;
arostm 0:4af3ca173992 2064
arostm 0:4af3ca173992 2065 /* Init the I2Sext */
arostm 0:4af3ca173992 2066 HAL_I2S_Init(&haudio_in_i2sext);
arostm 0:4af3ca173992 2067
arostm 0:4af3ca173992 2068 /* Enable I2S block */
arostm 0:4af3ca173992 2069 __HAL_I2S_ENABLE(&haudio_i2s);
arostm 0:4af3ca173992 2070 __HAL_I2S_ENABLE(&haudio_in_i2sext);
arostm 0:4af3ca173992 2071 }
arostm 0:4af3ca173992 2072
arostm 0:4af3ca173992 2073 /**
arostm 0:4af3ca173992 2074 * @brief Deinitializes the Audio Codec audio interface (I2S).
arostm 0:4af3ca173992 2075 */
arostm 0:4af3ca173992 2076 static void I2Sx_In_DeInit(void)
arostm 0:4af3ca173992 2077 {
arostm 0:4af3ca173992 2078 /* Initialize the hAudioInI2s Instance parameter */
arostm 0:4af3ca173992 2079 haudio_i2s.Instance = AUDIO_IN_I2Sx;
arostm 0:4af3ca173992 2080
arostm 0:4af3ca173992 2081 /* Disable I2S block */
arostm 0:4af3ca173992 2082 __HAL_I2S_DISABLE(&haudio_i2s);
arostm 0:4af3ca173992 2083
arostm 0:4af3ca173992 2084 /* DeInit the I2S */
arostm 0:4af3ca173992 2085 HAL_I2S_DeInit(&haudio_i2s);
arostm 0:4af3ca173992 2086
arostm 0:4af3ca173992 2087 /* Initialize the hAudioInI2s Instance parameter */
arostm 0:4af3ca173992 2088 haudio_in_i2sext.Instance = I2S3ext;
arostm 0:4af3ca173992 2089
arostm 0:4af3ca173992 2090 /* Disable I2S block */
arostm 0:4af3ca173992 2091 __HAL_I2S_DISABLE(&haudio_in_i2sext);
arostm 0:4af3ca173992 2092
arostm 0:4af3ca173992 2093 /* DeInit the I2S */
arostm 0:4af3ca173992 2094 HAL_I2S_DeInit(&haudio_in_i2sext);
arostm 0:4af3ca173992 2095 }
arostm 0:4af3ca173992 2096
arostm 0:4af3ca173992 2097 /**
arostm 2:0f07a9ac06f7 2098 * @brief This function handles DFSDM MIC1 DMA interrupt request.
arostm 2:0f07a9ac06f7 2099 * @param None
arostm 2:0f07a9ac06f7 2100 * @retval None
arostm 2:0f07a9ac06f7 2101 */
arostm 2:0f07a9ac06f7 2102 void AUDIO_DFSDM_DMAx_MIC1_IRQHandler(void)
arostm 2:0f07a9ac06f7 2103 {
arostm 2:0f07a9ac06f7 2104 HAL_DMA_IRQHandler(hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC1)].hdmaReg);
arostm 2:0f07a9ac06f7 2105
arostm 2:0f07a9ac06f7 2106 }
arostm 2:0f07a9ac06f7 2107
arostm 2:0f07a9ac06f7 2108 /**
arostm 2:0f07a9ac06f7 2109 * @brief This function handles DFSDM MIC2 DMA interrupt request.
arostm 2:0f07a9ac06f7 2110 * @param None
arostm 2:0f07a9ac06f7 2111 * @retval None
arostm 2:0f07a9ac06f7 2112 */
arostm 2:0f07a9ac06f7 2113 void AUDIO_DFSDM_DMAx_MIC2_IRQHandler(void)
arostm 2:0f07a9ac06f7 2114 {
arostm 2:0f07a9ac06f7 2115 HAL_DMA_IRQHandler(hAudioInDfsdmFilter[POS_VAL(INPUT_DEVICE_DIGITAL_MIC2)].hdmaReg);
arostm 2:0f07a9ac06f7 2116 }
arostm 2:0f07a9ac06f7 2117
arostm 2:0f07a9ac06f7 2118 /**
arostm 2:0f07a9ac06f7 2119 * @brief This function handles I2S DMA interrupt request.
arostm 2:0f07a9ac06f7 2120 * @param None
arostm 2:0f07a9ac06f7 2121 * @retval None
arostm 2:0f07a9ac06f7 2122 */
arostm 2:0f07a9ac06f7 2123 void AUDIO_OUT_I2Sx_DMAx_IRQHandler(void)
arostm 2:0f07a9ac06f7 2124 {
arostm 2:0f07a9ac06f7 2125 HAL_DMA_IRQHandler(haudio_i2s.hdmatx);
arostm 2:0f07a9ac06f7 2126 }
arostm 2:0f07a9ac06f7 2127
arostm 2:0f07a9ac06f7 2128 /**
arostm 0:4af3ca173992 2129 * @}
arostm 0:4af3ca173992 2130 */
arostm 0:4af3ca173992 2131
arostm 0:4af3ca173992 2132 /**
arostm 0:4af3ca173992 2133 * @}
arostm 0:4af3ca173992 2134 */
arostm 0:4af3ca173992 2135
arostm 0:4af3ca173992 2136 /**
arostm 0:4af3ca173992 2137 * @}
arostm 0:4af3ca173992 2138 */
arostm 0:4af3ca173992 2139
arostm 0:4af3ca173992 2140 /**
arostm 0:4af3ca173992 2141 * @}
arostm 0:4af3ca173992 2142 */
arostm 0:4af3ca173992 2143
arostm 0:4af3ca173992 2144 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/