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