AudioRecord and FFT/MSE comparison. Call AudioRecord_demo for control record and AudioSample for subsequent recordings.
Dependencies: CMSIS_DSP_401 STM32L4xx_HAL_Driver
Fork of OneHopeOnePrayer by
stm32l4xx_nucleo_audio.c@5:f6afbd3fc47a, 2015-12-05 (annotated)
- Committer:
- EricLew
- Date:
- Sat Dec 05 16:17:25 2015 +0000
- Revision:
- 5:f6afbd3fc47a
Ported to Nucleo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricLew | 5:f6afbd3fc47a | 1 | /** |
EricLew | 5:f6afbd3fc47a | 2 | ****************************************************************************** |
EricLew | 5:f6afbd3fc47a | 3 | * @file stm32l4xx_nucleo_audio.c |
EricLew | 5:f6afbd3fc47a | 4 | * @author MCD Application Team |
EricLew | 5:f6afbd3fc47a | 5 | * @version V1.0.1 |
EricLew | 5:f6afbd3fc47a | 6 | * @date 16-September-2015 |
EricLew | 5:f6afbd3fc47a | 7 | * @brief This file provides a set of functions needed to manage the |
EricLew | 5:f6afbd3fc47a | 8 | * Audio driver for the STM32L476G-Nucleo board. |
EricLew | 5:f6afbd3fc47a | 9 | ****************************************************************************** |
EricLew | 5:f6afbd3fc47a | 10 | * @attention |
EricLew | 5:f6afbd3fc47a | 11 | * |
EricLew | 5:f6afbd3fc47a | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
EricLew | 5:f6afbd3fc47a | 13 | * |
EricLew | 5:f6afbd3fc47a | 14 | * Redistribution and use in source and binary forms, with or without modification, |
EricLew | 5:f6afbd3fc47a | 15 | * are permitted provided that the following conditions are met: |
EricLew | 5:f6afbd3fc47a | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
EricLew | 5:f6afbd3fc47a | 17 | * this list of conditions and the following disclaimer. |
EricLew | 5:f6afbd3fc47a | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
EricLew | 5:f6afbd3fc47a | 19 | * this list of conditions and the following disclaimer in the documentation |
EricLew | 5:f6afbd3fc47a | 20 | * and/or other materials provided with the distribution. |
EricLew | 5:f6afbd3fc47a | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
EricLew | 5:f6afbd3fc47a | 22 | * may be used to endorse or promote products derived from this software |
EricLew | 5:f6afbd3fc47a | 23 | * without specific prior written permission. |
EricLew | 5:f6afbd3fc47a | 24 | * |
EricLew | 5:f6afbd3fc47a | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
EricLew | 5:f6afbd3fc47a | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
EricLew | 5:f6afbd3fc47a | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
EricLew | 5:f6afbd3fc47a | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
EricLew | 5:f6afbd3fc47a | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
EricLew | 5:f6afbd3fc47a | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
EricLew | 5:f6afbd3fc47a | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
EricLew | 5:f6afbd3fc47a | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
EricLew | 5:f6afbd3fc47a | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
EricLew | 5:f6afbd3fc47a | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
EricLew | 5:f6afbd3fc47a | 35 | * |
EricLew | 5:f6afbd3fc47a | 36 | ****************************************************************************** |
EricLew | 5:f6afbd3fc47a | 37 | */ |
EricLew | 5:f6afbd3fc47a | 38 | |
EricLew | 5:f6afbd3fc47a | 39 | /*============================================================================== |
EricLew | 5:f6afbd3fc47a | 40 | User NOTES |
EricLew | 5:f6afbd3fc47a | 41 | |
EricLew | 5:f6afbd3fc47a | 42 | 1. How To use this driver: |
EricLew | 5:f6afbd3fc47a | 43 | -------------------------- |
EricLew | 5:f6afbd3fc47a | 44 | + This driver supports STM32L4xx devices on STM32L476G-Nucleo (MB1184) Nucleo boards. |
EricLew | 5:f6afbd3fc47a | 45 | a) to play an audio file (all functions names start by BSP_AUDIO_OUT_xxx) |
EricLew | 5:f6afbd3fc47a | 46 | b) to record an audio file through MP34DT01TR, ST MEMS (all functions names start by BSP_AUDIO_IN_xxx) |
EricLew | 5:f6afbd3fc47a | 47 | |
EricLew | 5:f6afbd3fc47a | 48 | a) PLAY A FILE: |
EricLew | 5:f6afbd3fc47a | 49 | ============== |
EricLew | 5:f6afbd3fc47a | 50 | + Call the function BSP_AUDIO_OUT_Init( |
EricLew | 5:f6afbd3fc47a | 51 | OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER, |
EricLew | 5:f6afbd3fc47a | 52 | OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH) |
EricLew | 5:f6afbd3fc47a | 53 | Volume : Initial volume to be set (0 is min (mute), 100 is max (100%) |
EricLew | 5:f6afbd3fc47a | 54 | AudioFreq : Audio frequency in Hz (8000, 16000, 22500, 32000...) |
EricLew | 5:f6afbd3fc47a | 55 | this parameter is relative to the audio file/stream type. |
EricLew | 5:f6afbd3fc47a | 56 | ) |
EricLew | 5:f6afbd3fc47a | 57 | This function configures all the hardware required for the audio application (codec, I2C, SAI, |
EricLew | 5:f6afbd3fc47a | 58 | GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if configuration is OK. |
EricLew | 5:f6afbd3fc47a | 59 | If the returned value is different from AUDIO_OK or the function is stuck then the communication with |
EricLew | 5:f6afbd3fc47a | 60 | the audio codec has failed. |
EricLew | 5:f6afbd3fc47a | 61 | - OUTPUT_DEVICE_SPEAKER : only speaker will be set as output for the audio stream. |
EricLew | 5:f6afbd3fc47a | 62 | - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream. |
EricLew | 5:f6afbd3fc47a | 63 | - OUTPUT_DEVICE_BOTH : both Speaker and Headphone are used as outputs for the audio stream |
EricLew | 5:f6afbd3fc47a | 64 | at the same time. |
EricLew | 5:f6afbd3fc47a | 65 | |
EricLew | 5:f6afbd3fc47a | 66 | + Call the function BSP_AUDIO_OUT_RegisterCallbacks to register user callbacks |
EricLew | 5:f6afbd3fc47a | 67 | required to manage audio data streaming towards the audio codec (ErrorCallback(), |
EricLew | 5:f6afbd3fc47a | 68 | HalfTransfer_CallBack() and TransferComplete_CallBack()). |
EricLew | 5:f6afbd3fc47a | 69 | |
EricLew | 5:f6afbd3fc47a | 70 | + Call the function BSP_AUDIO_OUT_Play() to start audio playback (for the first time). |
EricLew | 5:f6afbd3fc47a | 71 | + Call the function BSP_AUDIO_OUT_Pause() to pause audio playabck |
EricLew | 5:f6afbd3fc47a | 72 | + Call the function BSP_AUDIO_OUT_Resume() to resume audio playback. |
EricLew | 5:f6afbd3fc47a | 73 | Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called |
EricLew | 5:f6afbd3fc47a | 74 | for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case). |
EricLew | 5:f6afbd3fc47a | 75 | Note. This function should be called only when the audio file is played or paused (not stopped). |
EricLew | 5:f6afbd3fc47a | 76 | + Call the function BSP_AUDIO_OUT_Stop() to stop audio playback. |
EricLew | 5:f6afbd3fc47a | 77 | + To modify the volume level, the sampling frequency, the device output mode, |
EricLew | 5:f6afbd3fc47a | 78 | the mute status or the audio configuration or the stop, use the functions: BSP_AUDIO_OUT_SetVolume(), |
EricLew | 5:f6afbd3fc47a | 79 | AUDIO_OUT_SetFrequency(), BSP_AUDIO_OUT_SetOutputMode(), BSP_AUDIO_OUT_SetMute()and |
EricLew | 5:f6afbd3fc47a | 80 | BSP_AUDIO_OUT_ChangeAudioConfig(). |
EricLew | 5:f6afbd3fc47a | 81 | |
EricLew | 5:f6afbd3fc47a | 82 | Driver architecture: |
EricLew | 5:f6afbd3fc47a | 83 | -------------------- |
EricLew | 5:f6afbd3fc47a | 84 | + This driver provides the audio layer high level API: it consists in functions |
EricLew | 5:f6afbd3fc47a | 85 | exported in the stm32l4xx_nucleo_audio.h file (e.g. BSP_AUDIO_OUT_Init(), |
EricLew | 5:f6afbd3fc47a | 86 | BSP_AUDIO_OUT_Play(), ...). |
EricLew | 5:f6afbd3fc47a | 87 | + This driver also includes the Media Access Layer (MAL): it consists in |
EricLew | 5:f6afbd3fc47a | 88 | functions allowing to access setup the audio devices. These functions |
EricLew | 5:f6afbd3fc47a | 89 | are included as local functions into the stm32l4xx_nucleo_audio.c file |
EricLew | 5:f6afbd3fc47a | 90 | (e.g. AUDIO_SAIx_Init()). |
EricLew | 5:f6afbd3fc47a | 91 | |
EricLew | 5:f6afbd3fc47a | 92 | Known Limitations: |
EricLew | 5:f6afbd3fc47a | 93 | ------------------ |
EricLew | 5:f6afbd3fc47a | 94 | 1- Communication with the audio codec (through I2C) may be corrupted if it is interrupted by some |
EricLew | 5:f6afbd3fc47a | 95 | user interrupt routines (in this case, interrupts could be disabled just before the start of |
EricLew | 5:f6afbd3fc47a | 96 | communication then re-enabled when it is over). Note that this communication is only done at |
EricLew | 5:f6afbd3fc47a | 97 | the configuration phase (BSP_AUDIO_OUT_Init() or BSP_AUDIO_OUT_Stop()) and when Volume control modification is |
EricLew | 5:f6afbd3fc47a | 98 | performed (BSP_AUDIO_OUT_SetVolume() or BSP_AUDIO_OUT_SetMute()or BSP_AUDIO_OUT_SetOutputMode()). |
EricLew | 5:f6afbd3fc47a | 99 | When the audio data is played, no communication is required with the audio codec. |
EricLew | 5:f6afbd3fc47a | 100 | 2- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size, |
EricLew | 5:f6afbd3fc47a | 101 | File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file. |
EricLew | 5:f6afbd3fc47a | 102 | 3- Supports only 16-bits audio data size. |
EricLew | 5:f6afbd3fc47a | 103 | |
EricLew | 5:f6afbd3fc47a | 104 | b) RECORD A FILE: |
EricLew | 5:f6afbd3fc47a | 105 | ================ |
EricLew | 5:f6afbd3fc47a | 106 | + Call the function BSP_AUDIO_IN_Init( |
EricLew | 5:f6afbd3fc47a | 107 | AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...) |
EricLew | 5:f6afbd3fc47a | 108 | ) |
EricLew | 5:f6afbd3fc47a | 109 | This function configures all the hardware required for the audio application (DFSDM, |
EricLew | 5:f6afbd3fc47a | 110 | GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if the |
EricLew | 5:f6afbd3fc47a | 111 | configuration completes successfully. |
EricLew | 5:f6afbd3fc47a | 112 | |
EricLew | 5:f6afbd3fc47a | 113 | + Call the function BSP_AUDIO_IN_RegisterCallbacks to register user callbacks |
EricLew | 5:f6afbd3fc47a | 114 | used to stream audio data toward the record buffer (ErrorCallback(), |
EricLew | 5:f6afbd3fc47a | 115 | HalfTransfer_CallBack() and TransferComplete_CallBack()). |
EricLew | 5:f6afbd3fc47a | 116 | |
EricLew | 5:f6afbd3fc47a | 117 | + Call the function BSP_AUDIO_IN_Record( |
EricLew | 5:f6afbd3fc47a | 118 | pbuf Main buffer pointer for the recorded data storing |
EricLew | 5:f6afbd3fc47a | 119 | size Current size of the recorded buffer |
EricLew | 5:f6afbd3fc47a | 120 | ) |
EricLew | 5:f6afbd3fc47a | 121 | to start recording from the microphone. |
EricLew | 5:f6afbd3fc47a | 122 | |
EricLew | 5:f6afbd3fc47a | 123 | + Call the function AUDIO_IN_STOP() to stop recording |
EricLew | 5:f6afbd3fc47a | 124 | ==============================================================================*/ |
EricLew | 5:f6afbd3fc47a | 125 | |
EricLew | 5:f6afbd3fc47a | 126 | /* Includes ------------------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 127 | #include <string.h> |
EricLew | 5:f6afbd3fc47a | 128 | #include "stm32l4xx_nucleo_audio.h" |
EricLew | 5:f6afbd3fc47a | 129 | |
EricLew | 5:f6afbd3fc47a | 130 | /** @addtogroup BSP |
EricLew | 5:f6afbd3fc47a | 131 | * @{ |
EricLew | 5:f6afbd3fc47a | 132 | */ |
EricLew | 5:f6afbd3fc47a | 133 | |
EricLew | 5:f6afbd3fc47a | 134 | /** @addtogroup STM32L476G_NUCLEO |
EricLew | 5:f6afbd3fc47a | 135 | * @{ |
EricLew | 5:f6afbd3fc47a | 136 | */ |
EricLew | 5:f6afbd3fc47a | 137 | |
EricLew | 5:f6afbd3fc47a | 138 | /** @defgroup STM32L476G_NUCLEO_AUDIO STM32L476G-NUCLEO AUDIO |
EricLew | 5:f6afbd3fc47a | 139 | * @brief This file includes the low layer driver for cs43l22 Audio Codec |
EricLew | 5:f6afbd3fc47a | 140 | * available on STM32L476G-Nucleo board(MB1184). |
EricLew | 5:f6afbd3fc47a | 141 | * @{ |
EricLew | 5:f6afbd3fc47a | 142 | */ |
EricLew | 5:f6afbd3fc47a | 143 | |
EricLew | 5:f6afbd3fc47a | 144 | /* Private typedef -----------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 145 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Private_Types Private Types |
EricLew | 5:f6afbd3fc47a | 146 | * @{ |
EricLew | 5:f6afbd3fc47a | 147 | */ |
EricLew | 5:f6afbd3fc47a | 148 | //typedef struct |
EricLew | 5:f6afbd3fc47a | 149 | //{ |
EricLew | 5:f6afbd3fc47a | 150 | // AUDIO_DrvTypeDef * AudioDrv; /* Audio codec driver */ |
EricLew | 5:f6afbd3fc47a | 151 | // Audio_CallbackTypeDef CbError; /* pointer to the callback function invoked when ... */ |
EricLew | 5:f6afbd3fc47a | 152 | // Audio_CallbackTypeDef CbHalfTransfer; /* pointer to the callback function invoked when ... */ |
EricLew | 5:f6afbd3fc47a | 153 | // Audio_CallbackTypeDef CbTransferComplete; /* pointer to the callback function invoked when ... */ |
EricLew | 5:f6afbd3fc47a | 154 | //} AUDIO_OUT_TypeDef; |
EricLew | 5:f6afbd3fc47a | 155 | |
EricLew | 5:f6afbd3fc47a | 156 | typedef struct |
EricLew | 5:f6afbd3fc47a | 157 | { |
EricLew | 5:f6afbd3fc47a | 158 | DFSDM_Channel_HandleTypeDef hDfsdmLeftChannel; /* DFSDM channel handle used for left channel */ |
EricLew | 5:f6afbd3fc47a | 159 | DMA_HandleTypeDef hDmaDfsdmLeft; /* DMA handle used for DFSDM regular conversions on left channel */ |
EricLew | 5:f6afbd3fc47a | 160 | int32_t * LeftRecBuff; /* Buffers for left samples */ |
EricLew | 5:f6afbd3fc47a | 161 | uint32_t Frequency; /* Record Frequency */ |
EricLew | 5:f6afbd3fc47a | 162 | uint32_t BitResolution; /* Record bit resolution */ |
EricLew | 5:f6afbd3fc47a | 163 | uint32_t ChannelNbr; /* Record Channel Number */ |
EricLew | 5:f6afbd3fc47a | 164 | uint16_t * pRecBuf; /* Pointer to record user buffer */ |
EricLew | 5:f6afbd3fc47a | 165 | uint32_t RecSize; /* Size to record in mono, double size to record in stereo */ |
EricLew | 5:f6afbd3fc47a | 166 | Audio_CallbackTypeDef CbError; /* pointer to the callback function invoked when a DMA transfer fails */ |
EricLew | 5:f6afbd3fc47a | 167 | Audio_CallbackTypeDef CbHalfTransfer; /* pointer to the callback function invoked when half of the DMA transfer is completed */ |
EricLew | 5:f6afbd3fc47a | 168 | Audio_CallbackTypeDef CbTransferComplete; /* pointer to the callback function invoked when the DMA transfer is completed */ |
EricLew | 5:f6afbd3fc47a | 169 | } AUDIO_IN_TypeDef; |
EricLew | 5:f6afbd3fc47a | 170 | |
EricLew | 5:f6afbd3fc47a | 171 | /** |
EricLew | 5:f6afbd3fc47a | 172 | * @} |
EricLew | 5:f6afbd3fc47a | 173 | */ |
EricLew | 5:f6afbd3fc47a | 174 | |
EricLew | 5:f6afbd3fc47a | 175 | /* Private defines ------------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 176 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Private_Constants Private Constants |
EricLew | 5:f6afbd3fc47a | 177 | * @{ |
EricLew | 5:f6afbd3fc47a | 178 | */ |
EricLew | 5:f6afbd3fc47a | 179 | /** |
EricLew | 5:f6afbd3fc47a | 180 | * @} |
EricLew | 5:f6afbd3fc47a | 181 | */ |
EricLew | 5:f6afbd3fc47a | 182 | |
EricLew | 5:f6afbd3fc47a | 183 | /* Private macros ------------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 184 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Private_Macros Private Macros |
EricLew | 5:f6afbd3fc47a | 185 | * @{ |
EricLew | 5:f6afbd3fc47a | 186 | */ |
EricLew | 5:f6afbd3fc47a | 187 | /*### PLAY ###*/ |
EricLew | 5:f6afbd3fc47a | 188 | /* SCK(kHz) = SAI_CK_x/(SAIClockDivider*2*256) */ |
EricLew | 5:f6afbd3fc47a | 189 | #define SAIClockDivider(__FREQUENCY__) \ |
EricLew | 5:f6afbd3fc47a | 190 | (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 12 \ |
EricLew | 5:f6afbd3fc47a | 191 | : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 2 \ |
EricLew | 5:f6afbd3fc47a | 192 | : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 6 \ |
EricLew | 5:f6afbd3fc47a | 193 | : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 1 \ |
EricLew | 5:f6afbd3fc47a | 194 | : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 3 \ |
EricLew | 5:f6afbd3fc47a | 195 | : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 0 \ |
EricLew | 5:f6afbd3fc47a | 196 | : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 2 : 1 \ |
EricLew | 5:f6afbd3fc47a | 197 | |
EricLew | 5:f6afbd3fc47a | 198 | /*### RECORD ###*/ |
EricLew | 5:f6afbd3fc47a | 199 | #define DFSDMOverSampling(__FREQUENCY__) \ |
EricLew | 5:f6afbd3fc47a | 200 | (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 256 \ |
EricLew | 5:f6afbd3fc47a | 201 | : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 256 \ |
EricLew | 5:f6afbd3fc47a | 202 | : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 128 \ |
EricLew | 5:f6afbd3fc47a | 203 | : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 128 \ |
EricLew | 5:f6afbd3fc47a | 204 | : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 64 \ |
EricLew | 5:f6afbd3fc47a | 205 | : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 64 \ |
EricLew | 5:f6afbd3fc47a | 206 | : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 16 \ |
EricLew | 5:f6afbd3fc47a | 207 | |
EricLew | 5:f6afbd3fc47a | 208 | #define DFSDMClockDivider(__FREQUENCY__) \ |
EricLew | 5:f6afbd3fc47a | 209 | (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 24 \ |
EricLew | 5:f6afbd3fc47a | 210 | : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 4 \ |
EricLew | 5:f6afbd3fc47a | 211 | : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 24 \ |
EricLew | 5:f6afbd3fc47a | 212 | : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 4 \ |
EricLew | 5:f6afbd3fc47a | 213 | : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 24 \ |
EricLew | 5:f6afbd3fc47a | 214 | : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 4 \ |
EricLew | 5:f6afbd3fc47a | 215 | : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 32 \ |
EricLew | 5:f6afbd3fc47a | 216 | |
EricLew | 5:f6afbd3fc47a | 217 | #define DFSDMFilterOrder(__FREQUENCY__) \ |
EricLew | 5:f6afbd3fc47a | 218 | (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? DFSDM_FILTER_SINC3_ORDER \ |
EricLew | 5:f6afbd3fc47a | 219 | : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? DFSDM_FILTER_SINC3_ORDER \ |
EricLew | 5:f6afbd3fc47a | 220 | : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? DFSDM_FILTER_SINC3_ORDER \ |
EricLew | 5:f6afbd3fc47a | 221 | : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? DFSDM_FILTER_SINC3_ORDER \ |
EricLew | 5:f6afbd3fc47a | 222 | : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? DFSDM_FILTER_SINC4_ORDER \ |
EricLew | 5:f6afbd3fc47a | 223 | : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? DFSDM_FILTER_SINC4_ORDER \ |
EricLew | 5:f6afbd3fc47a | 224 | : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? DFSDM_FILTER_SINC4_ORDER : DFSDM_FILTER_SINC5_ORDER \ |
EricLew | 5:f6afbd3fc47a | 225 | |
EricLew | 5:f6afbd3fc47a | 226 | #define DFSDMRightBitShift(__FREQUENCY__) \ |
EricLew | 5:f6afbd3fc47a | 227 | (__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 2 \ |
EricLew | 5:f6afbd3fc47a | 228 | : (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 3 \ |
EricLew | 5:f6afbd3fc47a | 229 | : (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 3 \ |
EricLew | 5:f6afbd3fc47a | 230 | : (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 0 \ |
EricLew | 5:f6afbd3fc47a | 231 | : (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 3 \ |
EricLew | 5:f6afbd3fc47a | 232 | : (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 3 \ |
EricLew | 5:f6afbd3fc47a | 233 | : (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 7 : 0 \ |
EricLew | 5:f6afbd3fc47a | 234 | |
EricLew | 5:f6afbd3fc47a | 235 | /* Saturate the record PCM sample */ |
EricLew | 5:f6afbd3fc47a | 236 | #define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N))) |
EricLew | 5:f6afbd3fc47a | 237 | |
EricLew | 5:f6afbd3fc47a | 238 | /** |
EricLew | 5:f6afbd3fc47a | 239 | * @} |
EricLew | 5:f6afbd3fc47a | 240 | */ |
EricLew | 5:f6afbd3fc47a | 241 | |
EricLew | 5:f6afbd3fc47a | 242 | /* Private variables ---------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 243 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Private_Variables Private Variables |
EricLew | 5:f6afbd3fc47a | 244 | * @{ |
EricLew | 5:f6afbd3fc47a | 245 | */ |
EricLew | 5:f6afbd3fc47a | 246 | /* Audio output context information */ |
EricLew | 5:f6afbd3fc47a | 247 | //static AUDIO_OUT_TypeDef hAudioOut; |
EricLew | 5:f6afbd3fc47a | 248 | |
EricLew | 5:f6afbd3fc47a | 249 | /* Audio input context information */ |
EricLew | 5:f6afbd3fc47a | 250 | static AUDIO_IN_TypeDef hAudioIn; |
EricLew | 5:f6afbd3fc47a | 251 | |
EricLew | 5:f6afbd3fc47a | 252 | /* SAI DMA handle */ |
EricLew | 5:f6afbd3fc47a | 253 | static DMA_HandleTypeDef hDmaSai; |
EricLew | 5:f6afbd3fc47a | 254 | /** |
EricLew | 5:f6afbd3fc47a | 255 | * @} |
EricLew | 5:f6afbd3fc47a | 256 | */ |
EricLew | 5:f6afbd3fc47a | 257 | |
EricLew | 5:f6afbd3fc47a | 258 | /* Exported variables ---------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 259 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Exported_Variables Exported Variables |
EricLew | 5:f6afbd3fc47a | 260 | * @{ |
EricLew | 5:f6afbd3fc47a | 261 | */ |
EricLew | 5:f6afbd3fc47a | 262 | /* SAIx handle */ |
EricLew | 5:f6afbd3fc47a | 263 | SAI_HandleTypeDef BSP_AUDIO_hSai; |
EricLew | 5:f6afbd3fc47a | 264 | |
EricLew | 5:f6afbd3fc47a | 265 | /* DFSDM filter handle */ |
EricLew | 5:f6afbd3fc47a | 266 | DFSDM_Filter_HandleTypeDef BSP_AUDIO_hDfsdmLeftFilter; |
EricLew | 5:f6afbd3fc47a | 267 | /** |
EricLew | 5:f6afbd3fc47a | 268 | * @} |
EricLew | 5:f6afbd3fc47a | 269 | */ |
EricLew | 5:f6afbd3fc47a | 270 | |
EricLew | 5:f6afbd3fc47a | 271 | /* Private function prototypes -----------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 272 | /** @defgroup STM32L476G_NUCLEO_AUDIO_Private_Functions Private Functions |
EricLew | 5:f6afbd3fc47a | 273 | * @{ |
EricLew | 5:f6afbd3fc47a | 274 | */ |
EricLew | 5:f6afbd3fc47a | 275 | //static void AUDIO_CODEC_Reset(void); |
EricLew | 5:f6afbd3fc47a | 276 | //static uint8_t AUDIO_SAIx_Init(uint32_t AudioFreq); |
EricLew | 5:f6afbd3fc47a | 277 | //static uint8_t AUDIO_SAIx_DeInit(void); |
EricLew | 5:f6afbd3fc47a | 278 | static uint8_t AUDIO_DFSDMx_Init(uint32_t AudioFreq); |
EricLew | 5:f6afbd3fc47a | 279 | static uint8_t AUDIO_DFSDMx_DeInit(void); |
EricLew | 5:f6afbd3fc47a | 280 | static uint8_t AUDIO_SAIPLLConfig(uint32_t AudioFreq); |
EricLew | 5:f6afbd3fc47a | 281 | /** |
EricLew | 5:f6afbd3fc47a | 282 | * @} |
EricLew | 5:f6afbd3fc47a | 283 | */ |
EricLew | 5:f6afbd3fc47a | 284 | |
EricLew | 5:f6afbd3fc47a | 285 | /* Exported functions --------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 286 | /** @addtogroup STM32L476G_NUCLEO_AUDIO_Exported_Functions |
EricLew | 5:f6afbd3fc47a | 287 | * @{ |
EricLew | 5:f6afbd3fc47a | 288 | */ |
EricLew | 5:f6afbd3fc47a | 289 | |
EricLew | 5:f6afbd3fc47a | 290 | /** |
EricLew | 5:f6afbd3fc47a | 291 | * @brief Configures the audio codec related peripherals. |
EricLew | 5:f6afbd3fc47a | 292 | * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, |
EricLew | 5:f6afbd3fc47a | 293 | * or OUTPUT_DEVICE_BOTH. |
EricLew | 5:f6afbd3fc47a | 294 | * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max)) |
EricLew | 5:f6afbd3fc47a | 295 | * @param AudioFreq: Audio frequency used to play the audio stream.ion. |
EricLew | 5:f6afbd3fc47a | 296 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 297 | * @note The SAI PLL input clock must be configure in the user application. |
EricLew | 5:f6afbd3fc47a | 298 | * The SAI PLL configuration done within this function assumes that |
EricLew | 5:f6afbd3fc47a | 299 | * the SAI PLL input clock runs at 8 MHz. |
EricLew | 5:f6afbd3fc47a | 300 | */ |
EricLew | 5:f6afbd3fc47a | 301 | //uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, |
EricLew | 5:f6afbd3fc47a | 302 | // uint8_t Volume, |
EricLew | 5:f6afbd3fc47a | 303 | // uint32_t AudioFreq) |
EricLew | 5:f6afbd3fc47a | 304 | //{ |
EricLew | 5:f6afbd3fc47a | 305 | // /* Initialize the audio output context */ |
EricLew | 5:f6afbd3fc47a | 306 | // hAudioOut.AudioDrv = &cs43l22_drv; |
EricLew | 5:f6afbd3fc47a | 307 | // hAudioOut.CbError = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 308 | // hAudioOut.CbHalfTransfer = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 309 | // hAudioOut.CbTransferComplete = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 310 | // |
EricLew | 5:f6afbd3fc47a | 311 | // /* Configure the SAI PLL according to the requested audio frequency */ |
EricLew | 5:f6afbd3fc47a | 312 | // if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 313 | // { |
EricLew | 5:f6afbd3fc47a | 314 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 315 | // } |
EricLew | 5:f6afbd3fc47a | 316 | // |
EricLew | 5:f6afbd3fc47a | 317 | // /* SAI data transfer preparation: prepare the Media to be used for the audio |
EricLew | 5:f6afbd3fc47a | 318 | // transfer from memory to SAI peripheral. */ |
EricLew | 5:f6afbd3fc47a | 319 | // if (AUDIO_SAIx_Init(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 320 | // { |
EricLew | 5:f6afbd3fc47a | 321 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 322 | // } |
EricLew | 5:f6afbd3fc47a | 323 | // |
EricLew | 5:f6afbd3fc47a | 324 | // /* Retieve audio codec identifier */ |
EricLew | 5:f6afbd3fc47a | 325 | // if (cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS) != CS43L22_ID) |
EricLew | 5:f6afbd3fc47a | 326 | // { |
EricLew | 5:f6afbd3fc47a | 327 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 328 | // } |
EricLew | 5:f6afbd3fc47a | 329 | // |
EricLew | 5:f6afbd3fc47a | 330 | // /* Reset the audio codec Registers */ |
EricLew | 5:f6afbd3fc47a | 331 | // AUDIO_CODEC_Reset(); |
EricLew | 5:f6afbd3fc47a | 332 | // |
EricLew | 5:f6afbd3fc47a | 333 | // /* Initialize the audio codec internal registers */ |
EricLew | 5:f6afbd3fc47a | 334 | // if (hAudioOut.AudioDrv->Init(AUDIO_I2C_ADDRESS, |
EricLew | 5:f6afbd3fc47a | 335 | // OutputDevice, |
EricLew | 5:f6afbd3fc47a | 336 | // Volume, |
EricLew | 5:f6afbd3fc47a | 337 | // AudioFreq) != 0) |
EricLew | 5:f6afbd3fc47a | 338 | // { |
EricLew | 5:f6afbd3fc47a | 339 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 340 | // } |
EricLew | 5:f6afbd3fc47a | 341 | // |
EricLew | 5:f6afbd3fc47a | 342 | // /* Set the requested volume */ |
EricLew | 5:f6afbd3fc47a | 343 | // BSP_AUDIO_OUT_SetVolume(Volume); |
EricLew | 5:f6afbd3fc47a | 344 | // |
EricLew | 5:f6afbd3fc47a | 345 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 346 | //} |
EricLew | 5:f6afbd3fc47a | 347 | |
EricLew | 5:f6afbd3fc47a | 348 | /** |
EricLew | 5:f6afbd3fc47a | 349 | * @brief De-Initializes audio codec related peripherals |
EricLew | 5:f6afbd3fc47a | 350 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 351 | |
EricLew | 5:f6afbd3fc47a | 352 | */ |
EricLew | 5:f6afbd3fc47a | 353 | //uint8_t BSP_AUDIO_OUT_DeInit(void) |
EricLew | 5:f6afbd3fc47a | 354 | //{ |
EricLew | 5:f6afbd3fc47a | 355 | // /* De-initializes the Audio Codec audio interface */ |
EricLew | 5:f6afbd3fc47a | 356 | // if (AUDIO_SAIx_DeInit() != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 357 | // { |
EricLew | 5:f6afbd3fc47a | 358 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 359 | // } |
EricLew | 5:f6afbd3fc47a | 360 | // |
EricLew | 5:f6afbd3fc47a | 361 | // /* DeInit Audio component interface */ |
EricLew | 5:f6afbd3fc47a | 362 | // hAudioOut.AudioDrv->DeInit(); |
EricLew | 5:f6afbd3fc47a | 363 | // |
EricLew | 5:f6afbd3fc47a | 364 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 365 | //} |
EricLew | 5:f6afbd3fc47a | 366 | |
EricLew | 5:f6afbd3fc47a | 367 | /** |
EricLew | 5:f6afbd3fc47a | 368 | * @brief Starts playing audio stream from a data buffer for a determined size. |
EricLew | 5:f6afbd3fc47a | 369 | * @param pData: pointer on PCM samples buffer |
EricLew | 5:f6afbd3fc47a | 370 | * @param Size: Number of audio data BYTES. |
EricLew | 5:f6afbd3fc47a | 371 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 372 | */ |
EricLew | 5:f6afbd3fc47a | 373 | //uint8_t BSP_AUDIO_OUT_Play(uint16_t* pData, uint32_t Size) |
EricLew | 5:f6afbd3fc47a | 374 | //{ |
EricLew | 5:f6afbd3fc47a | 375 | // /* Initiate a DMA transfer of PCM samples towards the serial audio interface */ |
EricLew | 5:f6afbd3fc47a | 376 | // if (HAL_SAI_Transmit_DMA(&BSP_AUDIO_hSai, (uint8_t *)pData, DMA_MAX(Size))!= HAL_OK) |
EricLew | 5:f6afbd3fc47a | 377 | // { |
EricLew | 5:f6afbd3fc47a | 378 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 379 | // } |
EricLew | 5:f6afbd3fc47a | 380 | // |
EricLew | 5:f6afbd3fc47a | 381 | // /* Call the audio Codec Play function */ |
EricLew | 5:f6afbd3fc47a | 382 | // if (hAudioOut.AudioDrv->Play(AUDIO_I2C_ADDRESS, pData, Size) != 0) |
EricLew | 5:f6afbd3fc47a | 383 | // { |
EricLew | 5:f6afbd3fc47a | 384 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 385 | // } |
EricLew | 5:f6afbd3fc47a | 386 | // |
EricLew | 5:f6afbd3fc47a | 387 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 388 | // } |
EricLew | 5:f6afbd3fc47a | 389 | |
EricLew | 5:f6afbd3fc47a | 390 | /** |
EricLew | 5:f6afbd3fc47a | 391 | * @brief Sends n-Bytes on the SAI interface. |
EricLew | 5:f6afbd3fc47a | 392 | * @param pData: pointer on PCM samples buffer |
EricLew | 5:f6afbd3fc47a | 393 | * @param Size: number of data to be written |
EricLew | 5:f6afbd3fc47a | 394 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 395 | */ |
EricLew | 5:f6afbd3fc47a | 396 | //uint8_t BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size) |
EricLew | 5:f6afbd3fc47a | 397 | //{ |
EricLew | 5:f6afbd3fc47a | 398 | // /* Initiate a DMA transfer of PCM samples towards the serial audio interface */ |
EricLew | 5:f6afbd3fc47a | 399 | // if (HAL_SAI_Transmit_DMA(&BSP_AUDIO_hSai, (uint8_t *)pData, Size)!= HAL_OK) |
EricLew | 5:f6afbd3fc47a | 400 | // { |
EricLew | 5:f6afbd3fc47a | 401 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 402 | // } |
EricLew | 5:f6afbd3fc47a | 403 | // |
EricLew | 5:f6afbd3fc47a | 404 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 405 | //} |
EricLew | 5:f6afbd3fc47a | 406 | |
EricLew | 5:f6afbd3fc47a | 407 | /** |
EricLew | 5:f6afbd3fc47a | 408 | * @brief This function Pauses the audio file stream. In case |
EricLew | 5:f6afbd3fc47a | 409 | * of using DMA, the DMA Pause feature is used. |
EricLew | 5:f6afbd3fc47a | 410 | * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only |
EricLew | 5:f6afbd3fc47a | 411 | * BSP_AUDIO_OUT_Resume() function should be called for resume |
EricLew | 5:f6afbd3fc47a | 412 | * (use of BSP_AUDIO_OUT_Play() function for resume could lead |
EricLew | 5:f6afbd3fc47a | 413 | * to unexpected behavior). |
EricLew | 5:f6afbd3fc47a | 414 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 415 | */ |
EricLew | 5:f6afbd3fc47a | 416 | //uint8_t BSP_AUDIO_OUT_Pause(void) |
EricLew | 5:f6afbd3fc47a | 417 | //{ |
EricLew | 5:f6afbd3fc47a | 418 | // /* Call the Audio Codec Pause function */ |
EricLew | 5:f6afbd3fc47a | 419 | // if (hAudioOut.AudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0) |
EricLew | 5:f6afbd3fc47a | 420 | // { |
EricLew | 5:f6afbd3fc47a | 421 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 422 | // } |
EricLew | 5:f6afbd3fc47a | 423 | // |
EricLew | 5:f6afbd3fc47a | 424 | // /* Pause DMA transfer of PCM samples towards the serial audio interface */ |
EricLew | 5:f6afbd3fc47a | 425 | // if (HAL_SAI_DMAPause(&BSP_AUDIO_hSai)!= HAL_OK) |
EricLew | 5:f6afbd3fc47a | 426 | // { |
EricLew | 5:f6afbd3fc47a | 427 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 428 | // } |
EricLew | 5:f6afbd3fc47a | 429 | // |
EricLew | 5:f6afbd3fc47a | 430 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 431 | //} |
EricLew | 5:f6afbd3fc47a | 432 | |
EricLew | 5:f6afbd3fc47a | 433 | /** |
EricLew | 5:f6afbd3fc47a | 434 | * @brief This function Resumes the audio file stream. |
EricLew | 5:f6afbd3fc47a | 435 | * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only |
EricLew | 5:f6afbd3fc47a | 436 | * BSP_AUDIO_OUT_Resume() function should be called for resume |
EricLew | 5:f6afbd3fc47a | 437 | * (use of BSP_AUDIO_OUT_Play() function for resume could lead to |
EricLew | 5:f6afbd3fc47a | 438 | * unexpected behavior). |
EricLew | 5:f6afbd3fc47a | 439 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 440 | */ |
EricLew | 5:f6afbd3fc47a | 441 | //uint8_t BSP_AUDIO_OUT_Resume(void) |
EricLew | 5:f6afbd3fc47a | 442 | //{ |
EricLew | 5:f6afbd3fc47a | 443 | // /* Call the Audio Codec Resume function */ |
EricLew | 5:f6afbd3fc47a | 444 | // if (hAudioOut.AudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0) |
EricLew | 5:f6afbd3fc47a | 445 | // { |
EricLew | 5:f6afbd3fc47a | 446 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 447 | // } |
EricLew | 5:f6afbd3fc47a | 448 | // |
EricLew | 5:f6afbd3fc47a | 449 | // /* Resume DMA transfer of PCM samples towards the serial audio interface */ |
EricLew | 5:f6afbd3fc47a | 450 | // if (HAL_SAI_DMAResume(&BSP_AUDIO_hSai)!= HAL_OK) |
EricLew | 5:f6afbd3fc47a | 451 | // { |
EricLew | 5:f6afbd3fc47a | 452 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 453 | // } |
EricLew | 5:f6afbd3fc47a | 454 | // |
EricLew | 5:f6afbd3fc47a | 455 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 456 | //} |
EricLew | 5:f6afbd3fc47a | 457 | |
EricLew | 5:f6afbd3fc47a | 458 | /** |
EricLew | 5:f6afbd3fc47a | 459 | * @brief Stops audio playing and Power down the Audio Codec. |
EricLew | 5:f6afbd3fc47a | 460 | * @param Option: could be one of the following parameters |
EricLew | 5:f6afbd3fc47a | 461 | * - CODEC_PDWN_SW: for software power off (by writing registers). |
EricLew | 5:f6afbd3fc47a | 462 | * Then no need to reconfigure the Codec after power on. |
EricLew | 5:f6afbd3fc47a | 463 | * - CODEC_PDWN_HW: completely shut down the codec (physically). |
EricLew | 5:f6afbd3fc47a | 464 | * Then need to reconfigure the Codec after power on. |
EricLew | 5:f6afbd3fc47a | 465 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 466 | */ |
EricLew | 5:f6afbd3fc47a | 467 | //uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option) |
EricLew | 5:f6afbd3fc47a | 468 | //{ |
EricLew | 5:f6afbd3fc47a | 469 | // /* Call Audio Codec Stop function */ |
EricLew | 5:f6afbd3fc47a | 470 | // if (hAudioOut.AudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) |
EricLew | 5:f6afbd3fc47a | 471 | // { |
EricLew | 5:f6afbd3fc47a | 472 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 473 | // } |
EricLew | 5:f6afbd3fc47a | 474 | // |
EricLew | 5:f6afbd3fc47a | 475 | // if(Option == CODEC_PDWN_HW) |
EricLew | 5:f6afbd3fc47a | 476 | // { |
EricLew | 5:f6afbd3fc47a | 477 | // /* Wait at least 100us */ |
EricLew | 5:f6afbd3fc47a | 478 | // HAL_Delay(1); |
EricLew | 5:f6afbd3fc47a | 479 | // } |
EricLew | 5:f6afbd3fc47a | 480 | // |
EricLew | 5:f6afbd3fc47a | 481 | // /* Stop DMA transfer of PCM samples towards the serial audio interface */ |
EricLew | 5:f6afbd3fc47a | 482 | // if (HAL_SAI_DMAStop(&BSP_AUDIO_hSai)!= HAL_OK) |
EricLew | 5:f6afbd3fc47a | 483 | // { |
EricLew | 5:f6afbd3fc47a | 484 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 485 | // } |
EricLew | 5:f6afbd3fc47a | 486 | // |
EricLew | 5:f6afbd3fc47a | 487 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 488 | //} |
EricLew | 5:f6afbd3fc47a | 489 | |
EricLew | 5:f6afbd3fc47a | 490 | /** |
EricLew | 5:f6afbd3fc47a | 491 | * @brief Controls the current audio volume level. |
EricLew | 5:f6afbd3fc47a | 492 | * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for |
EricLew | 5:f6afbd3fc47a | 493 | * Mute and 100 for Max volume level). |
EricLew | 5:f6afbd3fc47a | 494 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 495 | */ |
EricLew | 5:f6afbd3fc47a | 496 | //uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume) |
EricLew | 5:f6afbd3fc47a | 497 | //{ |
EricLew | 5:f6afbd3fc47a | 498 | // /* Call the codec volume control function with converted volume value */ |
EricLew | 5:f6afbd3fc47a | 499 | // if (hAudioOut.AudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) |
EricLew | 5:f6afbd3fc47a | 500 | // { |
EricLew | 5:f6afbd3fc47a | 501 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 502 | // } |
EricLew | 5:f6afbd3fc47a | 503 | // |
EricLew | 5:f6afbd3fc47a | 504 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 505 | //} |
EricLew | 5:f6afbd3fc47a | 506 | |
EricLew | 5:f6afbd3fc47a | 507 | /** |
EricLew | 5:f6afbd3fc47a | 508 | * @brief Enables or disables the MUTE mode by software |
EricLew | 5:f6afbd3fc47a | 509 | * @param Cmd: Could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to |
EricLew | 5:f6afbd3fc47a | 510 | * unmute the codec and restore previous volume level. |
EricLew | 5:f6afbd3fc47a | 511 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 512 | */ |
EricLew | 5:f6afbd3fc47a | 513 | //uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd) |
EricLew | 5:f6afbd3fc47a | 514 | //{ |
EricLew | 5:f6afbd3fc47a | 515 | // /* Call the Codec Mute function */ |
EricLew | 5:f6afbd3fc47a | 516 | // if (hAudioOut.AudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) |
EricLew | 5:f6afbd3fc47a | 517 | // { |
EricLew | 5:f6afbd3fc47a | 518 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 519 | // } |
EricLew | 5:f6afbd3fc47a | 520 | // |
EricLew | 5:f6afbd3fc47a | 521 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 522 | //} |
EricLew | 5:f6afbd3fc47a | 523 | |
EricLew | 5:f6afbd3fc47a | 524 | /** |
EricLew | 5:f6afbd3fc47a | 525 | * @brief Switch dynamically (while audio file is being played) the output |
EricLew | 5:f6afbd3fc47a | 526 | * target (speaker or headphone). |
EricLew | 5:f6afbd3fc47a | 527 | * @param Output: The audio output target: OUTPUT_DEVICE_SPEAKER, |
EricLew | 5:f6afbd3fc47a | 528 | * OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH |
EricLew | 5:f6afbd3fc47a | 529 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 530 | */ |
EricLew | 5:f6afbd3fc47a | 531 | //uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output) |
EricLew | 5:f6afbd3fc47a | 532 | //{ |
EricLew | 5:f6afbd3fc47a | 533 | // /* Call the Codec output device function */ |
EricLew | 5:f6afbd3fc47a | 534 | // if (hAudioOut.AudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) |
EricLew | 5:f6afbd3fc47a | 535 | // { |
EricLew | 5:f6afbd3fc47a | 536 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 537 | // } |
EricLew | 5:f6afbd3fc47a | 538 | // |
EricLew | 5:f6afbd3fc47a | 539 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 540 | //} |
EricLew | 5:f6afbd3fc47a | 541 | |
EricLew | 5:f6afbd3fc47a | 542 | /** |
EricLew | 5:f6afbd3fc47a | 543 | * @brief Updates the audio frequency. |
EricLew | 5:f6afbd3fc47a | 544 | * @param AudioFreq: Audio frequency used to play the audio stream. |
EricLew | 5:f6afbd3fc47a | 545 | * @note The SAI PLL input clock must be configure in the user application. |
EricLew | 5:f6afbd3fc47a | 546 | * The SAI PLL configuration done within this function assumes that |
EricLew | 5:f6afbd3fc47a | 547 | * the SAI PLL input clock runs at 8 MHz. |
EricLew | 5:f6afbd3fc47a | 548 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 549 | */ |
EricLew | 5:f6afbd3fc47a | 550 | //uint8_t BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq) |
EricLew | 5:f6afbd3fc47a | 551 | //{ |
EricLew | 5:f6afbd3fc47a | 552 | // /* Configure the SAI PLL according to the requested audio frequency */ |
EricLew | 5:f6afbd3fc47a | 553 | // if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 554 | // { |
EricLew | 5:f6afbd3fc47a | 555 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 556 | // } |
EricLew | 5:f6afbd3fc47a | 557 | // |
EricLew | 5:f6afbd3fc47a | 558 | // /* Disable SAI peripheral to allow access to SAI internal registers */ |
EricLew | 5:f6afbd3fc47a | 559 | // __HAL_SAI_DISABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 560 | // |
EricLew | 5:f6afbd3fc47a | 561 | // /* Update the SAI audio frequency configuration */ |
EricLew | 5:f6afbd3fc47a | 562 | // BSP_AUDIO_hSai.Init.Mckdiv = SAIClockDivider(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 563 | // HAL_SAI_Init(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 564 | // |
EricLew | 5:f6afbd3fc47a | 565 | // /* Enable SAI peripheral to generate MCLK */ |
EricLew | 5:f6afbd3fc47a | 566 | // __HAL_SAI_ENABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 567 | // |
EricLew | 5:f6afbd3fc47a | 568 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 569 | //} |
EricLew | 5:f6afbd3fc47a | 570 | |
EricLew | 5:f6afbd3fc47a | 571 | /** |
EricLew | 5:f6afbd3fc47a | 572 | * @brief Changes the Audio Out Configuration. |
EricLew | 5:f6afbd3fc47a | 573 | * @param AudioOutOption: specifies the audio out new configuration |
EricLew | 5:f6afbd3fc47a | 574 | * This parameter can be any value of @ref BSP_Audio_Out_Option |
EricLew | 5:f6afbd3fc47a | 575 | * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the |
EricLew | 5:f6afbd3fc47a | 576 | * audio out configuration. |
EricLew | 5:f6afbd3fc47a | 577 | * @retval None |
EricLew | 5:f6afbd3fc47a | 578 | */ |
EricLew | 5:f6afbd3fc47a | 579 | //void BSP_AUDIO_OUT_ChangeAudioConfig(uint32_t AudioOutOption) |
EricLew | 5:f6afbd3fc47a | 580 | //{ |
EricLew | 5:f6afbd3fc47a | 581 | // /********** Playback Buffer circular/normal mode **********/ |
EricLew | 5:f6afbd3fc47a | 582 | // if(AudioOutOption & BSP_AUDIO_OUT_CIRCULARMODE) |
EricLew | 5:f6afbd3fc47a | 583 | // { |
EricLew | 5:f6afbd3fc47a | 584 | // /* Deinitialize the Stream to update DMA mode */ |
EricLew | 5:f6afbd3fc47a | 585 | // HAL_DMA_DeInit(BSP_AUDIO_hSai.hdmatx); |
EricLew | 5:f6afbd3fc47a | 586 | // |
EricLew | 5:f6afbd3fc47a | 587 | // /* Update the SAI audio Transfer DMA mode */ |
EricLew | 5:f6afbd3fc47a | 588 | // BSP_AUDIO_hSai.hdmatx->Init.Mode = DMA_CIRCULAR; |
EricLew | 5:f6afbd3fc47a | 589 | // |
EricLew | 5:f6afbd3fc47a | 590 | // /* Configure the DMA Stream with new Transfer DMA mode */ |
EricLew | 5:f6afbd3fc47a | 591 | // HAL_DMA_Init(BSP_AUDIO_hSai.hdmatx); |
EricLew | 5:f6afbd3fc47a | 592 | // } |
EricLew | 5:f6afbd3fc47a | 593 | // else /* BSP_AUDIO_OUT_NORMALMODE */ |
EricLew | 5:f6afbd3fc47a | 594 | // { |
EricLew | 5:f6afbd3fc47a | 595 | // /* Deinitialize the Stream to update DMA mode */ |
EricLew | 5:f6afbd3fc47a | 596 | // HAL_DMA_DeInit(BSP_AUDIO_hSai.hdmatx); |
EricLew | 5:f6afbd3fc47a | 597 | // |
EricLew | 5:f6afbd3fc47a | 598 | // /* Update the SAI audio Transfer DMA mode */ |
EricLew | 5:f6afbd3fc47a | 599 | // BSP_AUDIO_hSai.hdmatx->Init.Mode = DMA_NORMAL; |
EricLew | 5:f6afbd3fc47a | 600 | // |
EricLew | 5:f6afbd3fc47a | 601 | // /* Configure the DMA Stream with new Transfer DMA mode */ |
EricLew | 5:f6afbd3fc47a | 602 | // HAL_DMA_Init(BSP_AUDIO_hSai.hdmatx); |
EricLew | 5:f6afbd3fc47a | 603 | // } |
EricLew | 5:f6afbd3fc47a | 604 | // |
EricLew | 5:f6afbd3fc47a | 605 | // /********** Playback Buffer stereo/mono mode **********/ |
EricLew | 5:f6afbd3fc47a | 606 | // if(AudioOutOption & BSP_AUDIO_OUT_STEREOMODE) |
EricLew | 5:f6afbd3fc47a | 607 | // { |
EricLew | 5:f6afbd3fc47a | 608 | // /* Disable SAI peripheral to allow access to SAI internal registers */ |
EricLew | 5:f6afbd3fc47a | 609 | // __HAL_SAI_DISABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 610 | // |
EricLew | 5:f6afbd3fc47a | 611 | // /* Update the SAI audio frame slot configuration */ |
EricLew | 5:f6afbd3fc47a | 612 | // BSP_AUDIO_hSai.Init.MonoStereoMode = SAI_STEREOMODE; |
EricLew | 5:f6afbd3fc47a | 613 | // HAL_SAI_Init(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 614 | // |
EricLew | 5:f6afbd3fc47a | 615 | // /* Enable SAI peripheral to generate MCLK */ |
EricLew | 5:f6afbd3fc47a | 616 | // __HAL_SAI_ENABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 617 | // } |
EricLew | 5:f6afbd3fc47a | 618 | // else /* BSP_AUDIO_OUT_MONOMODE */ |
EricLew | 5:f6afbd3fc47a | 619 | // { |
EricLew | 5:f6afbd3fc47a | 620 | // /* Disable SAI peripheral to allow access to SAI internal registers */ |
EricLew | 5:f6afbd3fc47a | 621 | // __HAL_SAI_DISABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 622 | // |
EricLew | 5:f6afbd3fc47a | 623 | // /* Update the SAI audio frame slot configuration */ |
EricLew | 5:f6afbd3fc47a | 624 | // BSP_AUDIO_hSai.Init.MonoStereoMode = SAI_MONOMODE; |
EricLew | 5:f6afbd3fc47a | 625 | // HAL_SAI_Init(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 626 | // |
EricLew | 5:f6afbd3fc47a | 627 | // /* Enable SAI peripheral to generate MCLK */ |
EricLew | 5:f6afbd3fc47a | 628 | // __HAL_SAI_ENABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 629 | // } |
EricLew | 5:f6afbd3fc47a | 630 | //} |
EricLew | 5:f6afbd3fc47a | 631 | |
EricLew | 5:f6afbd3fc47a | 632 | /** |
EricLew | 5:f6afbd3fc47a | 633 | * @brief register user callback functions |
EricLew | 5:f6afbd3fc47a | 634 | * @param ErrorCallback: pointer to the error callback function |
EricLew | 5:f6afbd3fc47a | 635 | * @param HalfTransferCallback: pointer to the half transfer callback function |
EricLew | 5:f6afbd3fc47a | 636 | * @param TransferCompleteCallback: pointer to the transfer complete callback function |
EricLew | 5:f6afbd3fc47a | 637 | * @retval None |
EricLew | 5:f6afbd3fc47a | 638 | */ |
EricLew | 5:f6afbd3fc47a | 639 | //void BSP_AUDIO_OUT_RegisterCallbacks(Audio_CallbackTypeDef ErrorCallback, |
EricLew | 5:f6afbd3fc47a | 640 | // Audio_CallbackTypeDef HalfTransferCallback, |
EricLew | 5:f6afbd3fc47a | 641 | // Audio_CallbackTypeDef TransferCompleteCallback) |
EricLew | 5:f6afbd3fc47a | 642 | //{ |
EricLew | 5:f6afbd3fc47a | 643 | // hAudioOut.CbError = ErrorCallback; |
EricLew | 5:f6afbd3fc47a | 644 | // hAudioOut.CbHalfTransfer = HalfTransferCallback; |
EricLew | 5:f6afbd3fc47a | 645 | // hAudioOut.CbTransferComplete = TransferCompleteCallback; |
EricLew | 5:f6afbd3fc47a | 646 | //} |
EricLew | 5:f6afbd3fc47a | 647 | |
EricLew | 5:f6afbd3fc47a | 648 | /** |
EricLew | 5:f6afbd3fc47a | 649 | * @brief Tx Transfer completed callbacks. |
EricLew | 5:f6afbd3fc47a | 650 | * @param hsai: SAI handle |
EricLew | 5:f6afbd3fc47a | 651 | * @retval None |
EricLew | 5:f6afbd3fc47a | 652 | */ |
EricLew | 5:f6afbd3fc47a | 653 | //void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai) |
EricLew | 5:f6afbd3fc47a | 654 | //{ |
EricLew | 5:f6afbd3fc47a | 655 | // /* Invoke the registered 'TransferComplete' function (if any) */ |
EricLew | 5:f6afbd3fc47a | 656 | // if (hAudioOut.CbTransferComplete != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 657 | // { |
EricLew | 5:f6afbd3fc47a | 658 | // hAudioOut.CbTransferComplete(); |
EricLew | 5:f6afbd3fc47a | 659 | // } |
EricLew | 5:f6afbd3fc47a | 660 | //} |
EricLew | 5:f6afbd3fc47a | 661 | |
EricLew | 5:f6afbd3fc47a | 662 | /** |
EricLew | 5:f6afbd3fc47a | 663 | * @brief Tx Half Transfer completed callbacks. |
EricLew | 5:f6afbd3fc47a | 664 | * @param hsai: SAI handle |
EricLew | 5:f6afbd3fc47a | 665 | * @retval None |
EricLew | 5:f6afbd3fc47a | 666 | */ |
EricLew | 5:f6afbd3fc47a | 667 | //void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai) |
EricLew | 5:f6afbd3fc47a | 668 | //{ |
EricLew | 5:f6afbd3fc47a | 669 | // /* Invoke the registered 'HalfTransfer' callback function (if any) */ |
EricLew | 5:f6afbd3fc47a | 670 | // if (hAudioOut.CbHalfTransfer != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 671 | // { |
EricLew | 5:f6afbd3fc47a | 672 | // hAudioOut.CbHalfTransfer(); |
EricLew | 5:f6afbd3fc47a | 673 | // } |
EricLew | 5:f6afbd3fc47a | 674 | //} |
EricLew | 5:f6afbd3fc47a | 675 | |
EricLew | 5:f6afbd3fc47a | 676 | /** |
EricLew | 5:f6afbd3fc47a | 677 | * @brief SAI error callbacks. |
EricLew | 5:f6afbd3fc47a | 678 | * @param hsai: SAI handle |
EricLew | 5:f6afbd3fc47a | 679 | * @retval None |
EricLew | 5:f6afbd3fc47a | 680 | */ |
EricLew | 5:f6afbd3fc47a | 681 | //void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai) |
EricLew | 5:f6afbd3fc47a | 682 | //{ |
EricLew | 5:f6afbd3fc47a | 683 | // /* Invoke the registered 'ErrorCallback' callback function (if any) */ |
EricLew | 5:f6afbd3fc47a | 684 | // if (hAudioOut.CbError != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 685 | // { |
EricLew | 5:f6afbd3fc47a | 686 | // hAudioOut.CbError(); |
EricLew | 5:f6afbd3fc47a | 687 | // } |
EricLew | 5:f6afbd3fc47a | 688 | //} |
EricLew | 5:f6afbd3fc47a | 689 | |
EricLew | 5:f6afbd3fc47a | 690 | /** |
EricLew | 5:f6afbd3fc47a | 691 | * @} |
EricLew | 5:f6afbd3fc47a | 692 | */ |
EricLew | 5:f6afbd3fc47a | 693 | |
EricLew | 5:f6afbd3fc47a | 694 | /** @addtogroup STM32L476G_EVAL_AUDIO_Exported_Functions |
EricLew | 5:f6afbd3fc47a | 695 | * @{ |
EricLew | 5:f6afbd3fc47a | 696 | */ |
EricLew | 5:f6afbd3fc47a | 697 | |
EricLew | 5:f6afbd3fc47a | 698 | /** |
EricLew | 5:f6afbd3fc47a | 699 | * @brief Initializes micropone related peripherals. |
EricLew | 5:f6afbd3fc47a | 700 | * @note This function assumes that the SAI input clock (through PLL_M) |
EricLew | 5:f6afbd3fc47a | 701 | * is already configured and ready to be used. |
EricLew | 5:f6afbd3fc47a | 702 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
EricLew | 5:f6afbd3fc47a | 703 | * @param BitRes: Audio frequency to be configured for the SAI peripheral. |
EricLew | 5:f6afbd3fc47a | 704 | * @param ChnlNbr: Audio frequency to be configured for the SAI peripheral. |
EricLew | 5:f6afbd3fc47a | 705 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 706 | */ |
EricLew | 5:f6afbd3fc47a | 707 | uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr) |
EricLew | 5:f6afbd3fc47a | 708 | { |
EricLew | 5:f6afbd3fc47a | 709 | /* Update the audio input context */ |
EricLew | 5:f6afbd3fc47a | 710 | hAudioIn.Frequency = AudioFreq; |
EricLew | 5:f6afbd3fc47a | 711 | hAudioIn.BitResolution = BitRes; |
EricLew | 5:f6afbd3fc47a | 712 | hAudioIn.ChannelNbr = ChnlNbr; |
EricLew | 5:f6afbd3fc47a | 713 | hAudioIn.CbError = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 714 | hAudioIn.CbHalfTransfer = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 715 | hAudioIn.CbTransferComplete = (Audio_CallbackTypeDef)NULL; |
EricLew | 5:f6afbd3fc47a | 716 | |
EricLew | 5:f6afbd3fc47a | 717 | /* Configure the SAI PLL according to the requested audio frequency */ |
EricLew | 5:f6afbd3fc47a | 718 | if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 719 | { |
EricLew | 5:f6afbd3fc47a | 720 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 721 | } |
EricLew | 5:f6afbd3fc47a | 722 | |
EricLew | 5:f6afbd3fc47a | 723 | /* Initializes the Digital Filter for Sigma-Delta Modulators interface */ |
EricLew | 5:f6afbd3fc47a | 724 | if(AUDIO_DFSDMx_Init(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 725 | { |
EricLew | 5:f6afbd3fc47a | 726 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 727 | } |
EricLew | 5:f6afbd3fc47a | 728 | |
EricLew | 5:f6afbd3fc47a | 729 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 730 | } |
EricLew | 5:f6afbd3fc47a | 731 | |
EricLew | 5:f6afbd3fc47a | 732 | /** |
EricLew | 5:f6afbd3fc47a | 733 | * @brief De-Initializes microphone related peripherals. |
EricLew | 5:f6afbd3fc47a | 734 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 735 | |
EricLew | 5:f6afbd3fc47a | 736 | */ |
EricLew | 5:f6afbd3fc47a | 737 | uint8_t BSP_AUDIO_IN_DeInit(void) |
EricLew | 5:f6afbd3fc47a | 738 | { |
EricLew | 5:f6afbd3fc47a | 739 | /* De-initializes the Digital Filter for Sigma-Delta Modulators interface */ |
EricLew | 5:f6afbd3fc47a | 740 | if (AUDIO_DFSDMx_DeInit() != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 741 | { |
EricLew | 5:f6afbd3fc47a | 742 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 743 | } |
EricLew | 5:f6afbd3fc47a | 744 | |
EricLew | 5:f6afbd3fc47a | 745 | /* Reset the audio input context */ |
EricLew | 5:f6afbd3fc47a | 746 | memset(&hAudioIn, 0, sizeof(hAudioIn)); |
EricLew | 5:f6afbd3fc47a | 747 | |
EricLew | 5:f6afbd3fc47a | 748 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 749 | } |
EricLew | 5:f6afbd3fc47a | 750 | |
EricLew | 5:f6afbd3fc47a | 751 | /** |
EricLew | 5:f6afbd3fc47a | 752 | * @brief Starts audio recording. |
EricLew | 5:f6afbd3fc47a | 753 | * @param pbuf: Main buffer pointer for the recorded data storing |
EricLew | 5:f6afbd3fc47a | 754 | * @param size: Current size of the recorded buffer |
EricLew | 5:f6afbd3fc47a | 755 | * @note The Right channel is start at first with synchro on start of Left channel |
EricLew | 5:f6afbd3fc47a | 756 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 757 | */ |
EricLew | 5:f6afbd3fc47a | 758 | uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size) |
EricLew | 5:f6afbd3fc47a | 759 | { |
EricLew | 5:f6afbd3fc47a | 760 | hAudioIn.pRecBuf = pbuf; |
EricLew | 5:f6afbd3fc47a | 761 | hAudioIn.RecSize = size; |
EricLew | 5:f6afbd3fc47a | 762 | |
EricLew | 5:f6afbd3fc47a | 763 | /* Allocate hAudioIn.LeftRecBuff buffer */ |
EricLew | 5:f6afbd3fc47a | 764 | #if defined(BSP_AUDIO_USE_RTOS) |
EricLew | 5:f6afbd3fc47a | 765 | hAudioIn.LeftRecBuff = (int32_t *)k_malloc(size * sizeof(int32_t)); |
EricLew | 5:f6afbd3fc47a | 766 | #else |
EricLew | 5:f6afbd3fc47a | 767 | hAudioIn.LeftRecBuff = (int32_t *)malloc(size * sizeof(int32_t)); |
EricLew | 5:f6afbd3fc47a | 768 | #endif |
EricLew | 5:f6afbd3fc47a | 769 | if(hAudioIn.LeftRecBuff == NULL) |
EricLew | 5:f6afbd3fc47a | 770 | { |
EricLew | 5:f6afbd3fc47a | 771 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 772 | } |
EricLew | 5:f6afbd3fc47a | 773 | |
EricLew | 5:f6afbd3fc47a | 774 | /* Call the Media layer start function for left channel */ |
EricLew | 5:f6afbd3fc47a | 775 | if(HAL_DFSDM_FilterRegularStart_DMA(&BSP_AUDIO_hDfsdmLeftFilter, |
EricLew | 5:f6afbd3fc47a | 776 | (int32_t*)hAudioIn.LeftRecBuff, |
EricLew | 5:f6afbd3fc47a | 777 | (hAudioIn.RecSize/DEFAULT_AUDIO_IN_CHANNEL_NBR)) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 778 | { |
EricLew | 5:f6afbd3fc47a | 779 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 780 | } |
EricLew | 5:f6afbd3fc47a | 781 | |
EricLew | 5:f6afbd3fc47a | 782 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 783 | } |
EricLew | 5:f6afbd3fc47a | 784 | |
EricLew | 5:f6afbd3fc47a | 785 | /** |
EricLew | 5:f6afbd3fc47a | 786 | * @brief Updates the audio frequency. |
EricLew | 5:f6afbd3fc47a | 787 | * @param AudioFreq: Audio frequency used to record the audio stream. |
EricLew | 5:f6afbd3fc47a | 788 | * @note This API should be called after the BSP_AUDIO_IN_Init() to adjust the |
EricLew | 5:f6afbd3fc47a | 789 | * audio frequency. |
EricLew | 5:f6afbd3fc47a | 790 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 791 | */ |
EricLew | 5:f6afbd3fc47a | 792 | uint8_t BSP_AUDIO_IN_SetFrequency(uint32_t AudioFreq) |
EricLew | 5:f6afbd3fc47a | 793 | { |
EricLew | 5:f6afbd3fc47a | 794 | /* Configure the SAI PLL according to the requested audio frequency */ |
EricLew | 5:f6afbd3fc47a | 795 | if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 796 | { |
EricLew | 5:f6afbd3fc47a | 797 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 798 | } |
EricLew | 5:f6afbd3fc47a | 799 | |
EricLew | 5:f6afbd3fc47a | 800 | /* De-initializes the Digital Filter for Sigma-Delta Modulators interface */ |
EricLew | 5:f6afbd3fc47a | 801 | if(AUDIO_DFSDMx_DeInit() != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 802 | { |
EricLew | 5:f6afbd3fc47a | 803 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 804 | } |
EricLew | 5:f6afbd3fc47a | 805 | |
EricLew | 5:f6afbd3fc47a | 806 | /* Initializes the Digital Filter for Sigma-Delta Modulators interface */ |
EricLew | 5:f6afbd3fc47a | 807 | if(AUDIO_DFSDMx_Init(AudioFreq) != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 808 | { |
EricLew | 5:f6afbd3fc47a | 809 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 810 | } |
EricLew | 5:f6afbd3fc47a | 811 | |
EricLew | 5:f6afbd3fc47a | 812 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 813 | } |
EricLew | 5:f6afbd3fc47a | 814 | |
EricLew | 5:f6afbd3fc47a | 815 | /** |
EricLew | 5:f6afbd3fc47a | 816 | * @brief Regular conversion complete callback. |
EricLew | 5:f6afbd3fc47a | 817 | * @note In interrupt mode, user has to read conversion value in this function |
EricLew | 5:f6afbd3fc47a | 818 | using HAL_DFSDM_FilterGetRegularValue. |
EricLew | 5:f6afbd3fc47a | 819 | * @param hdfsdm_filter : DFSDM filter handle. |
EricLew | 5:f6afbd3fc47a | 820 | * @retval None |
EricLew | 5:f6afbd3fc47a | 821 | */ |
EricLew | 5:f6afbd3fc47a | 822 | void HAL_DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) |
EricLew | 5:f6afbd3fc47a | 823 | { |
EricLew | 5:f6afbd3fc47a | 824 | uint32_t index; |
EricLew | 5:f6afbd3fc47a | 825 | uint32_t recbufsize = (hAudioIn.RecSize/DEFAULT_AUDIO_IN_CHANNEL_NBR); |
EricLew | 5:f6afbd3fc47a | 826 | |
EricLew | 5:f6afbd3fc47a | 827 | for(index = (recbufsize/2); index < recbufsize; index++) |
EricLew | 5:f6afbd3fc47a | 828 | { |
EricLew | 5:f6afbd3fc47a | 829 | hAudioIn.pRecBuf[index] = (uint16_t)(SaturaLH((hAudioIn.LeftRecBuff[index] >> 8), -32760, 32760)); |
EricLew | 5:f6afbd3fc47a | 830 | } |
EricLew | 5:f6afbd3fc47a | 831 | |
EricLew | 5:f6afbd3fc47a | 832 | /* Invoke the registered 'TransferComplete' function (if any) */ |
EricLew | 5:f6afbd3fc47a | 833 | if (hAudioIn.CbTransferComplete != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 834 | { |
EricLew | 5:f6afbd3fc47a | 835 | hAudioIn.CbTransferComplete(); |
EricLew | 5:f6afbd3fc47a | 836 | } |
EricLew | 5:f6afbd3fc47a | 837 | } |
EricLew | 5:f6afbd3fc47a | 838 | |
EricLew | 5:f6afbd3fc47a | 839 | /** |
EricLew | 5:f6afbd3fc47a | 840 | * @brief Half regular conversion complete callback. |
EricLew | 5:f6afbd3fc47a | 841 | * @param hdfsdm_filter : DFSDM filter handle. |
EricLew | 5:f6afbd3fc47a | 842 | * @retval None |
EricLew | 5:f6afbd3fc47a | 843 | */ |
EricLew | 5:f6afbd3fc47a | 844 | void HAL_DFSDM_FilterRegConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) |
EricLew | 5:f6afbd3fc47a | 845 | { |
EricLew | 5:f6afbd3fc47a | 846 | uint32_t index; |
EricLew | 5:f6afbd3fc47a | 847 | uint32_t recbufsize = (hAudioIn.RecSize/DEFAULT_AUDIO_IN_CHANNEL_NBR); |
EricLew | 5:f6afbd3fc47a | 848 | |
EricLew | 5:f6afbd3fc47a | 849 | |
EricLew | 5:f6afbd3fc47a | 850 | for(index = 0; index < (recbufsize/2); index++) |
EricLew | 5:f6afbd3fc47a | 851 | { |
EricLew | 5:f6afbd3fc47a | 852 | hAudioIn.pRecBuf[index] = (uint16_t)(SaturaLH((hAudioIn.LeftRecBuff[index] >> 8), -32760, 32760)); |
EricLew | 5:f6afbd3fc47a | 853 | } |
EricLew | 5:f6afbd3fc47a | 854 | |
EricLew | 5:f6afbd3fc47a | 855 | /* Invoke the registered 'HalfTransfer' callback function (if any) */ |
EricLew | 5:f6afbd3fc47a | 856 | if (hAudioIn.CbHalfTransfer != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 857 | { |
EricLew | 5:f6afbd3fc47a | 858 | hAudioIn.CbHalfTransfer(); |
EricLew | 5:f6afbd3fc47a | 859 | } |
EricLew | 5:f6afbd3fc47a | 860 | } |
EricLew | 5:f6afbd3fc47a | 861 | |
EricLew | 5:f6afbd3fc47a | 862 | /** |
EricLew | 5:f6afbd3fc47a | 863 | * @brief Error callback. |
EricLew | 5:f6afbd3fc47a | 864 | * @param hdfsdm_filter : DFSDM filter handle. |
EricLew | 5:f6afbd3fc47a | 865 | * @retval None |
EricLew | 5:f6afbd3fc47a | 866 | */ |
EricLew | 5:f6afbd3fc47a | 867 | void HAL_DFSDM_FilterErrorCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) |
EricLew | 5:f6afbd3fc47a | 868 | { |
EricLew | 5:f6afbd3fc47a | 869 | /* Invoke the registered 'ErrorCallback' callback function (if any) */ |
EricLew | 5:f6afbd3fc47a | 870 | if (hAudioIn.CbError != (Audio_CallbackTypeDef)NULL) |
EricLew | 5:f6afbd3fc47a | 871 | { |
EricLew | 5:f6afbd3fc47a | 872 | hAudioIn.CbError(); |
EricLew | 5:f6afbd3fc47a | 873 | } |
EricLew | 5:f6afbd3fc47a | 874 | } |
EricLew | 5:f6afbd3fc47a | 875 | |
EricLew | 5:f6afbd3fc47a | 876 | /** |
EricLew | 5:f6afbd3fc47a | 877 | * @brief Stops audio recording. |
EricLew | 5:f6afbd3fc47a | 878 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 879 | */ |
EricLew | 5:f6afbd3fc47a | 880 | uint8_t BSP_AUDIO_IN_Stop(void) |
EricLew | 5:f6afbd3fc47a | 881 | { |
EricLew | 5:f6afbd3fc47a | 882 | /* Call the Media layer stop function for left channel */ |
EricLew | 5:f6afbd3fc47a | 883 | if(HAL_DFSDM_FilterRegularStop_DMA(&BSP_AUDIO_hDfsdmLeftFilter) != HAL_OK ) |
EricLew | 5:f6afbd3fc47a | 884 | { |
EricLew | 5:f6afbd3fc47a | 885 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 886 | } |
EricLew | 5:f6afbd3fc47a | 887 | |
EricLew | 5:f6afbd3fc47a | 888 | /* Free hAudioIn.LeftRecBuff buffer */ |
EricLew | 5:f6afbd3fc47a | 889 | #if defined(BSP_AUDIO_USE_RTOS) |
EricLew | 5:f6afbd3fc47a | 890 | k_free((void *)hAudioIn.LeftRecBuff); |
EricLew | 5:f6afbd3fc47a | 891 | #else |
EricLew | 5:f6afbd3fc47a | 892 | free((void *)hAudioIn.LeftRecBuff); |
EricLew | 5:f6afbd3fc47a | 893 | #endif |
EricLew | 5:f6afbd3fc47a | 894 | |
EricLew | 5:f6afbd3fc47a | 895 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 896 | } |
EricLew | 5:f6afbd3fc47a | 897 | |
EricLew | 5:f6afbd3fc47a | 898 | /** |
EricLew | 5:f6afbd3fc47a | 899 | * @brief Pauses the audio file stream. |
EricLew | 5:f6afbd3fc47a | 900 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 901 | */ |
EricLew | 5:f6afbd3fc47a | 902 | uint8_t BSP_AUDIO_IN_Pause(void) |
EricLew | 5:f6afbd3fc47a | 903 | { |
EricLew | 5:f6afbd3fc47a | 904 | /* Call the Media layer stop function */ |
EricLew | 5:f6afbd3fc47a | 905 | if(HAL_DFSDM_FilterRegularStop_DMA(&BSP_AUDIO_hDfsdmLeftFilter) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 906 | { |
EricLew | 5:f6afbd3fc47a | 907 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 908 | } |
EricLew | 5:f6afbd3fc47a | 909 | |
EricLew | 5:f6afbd3fc47a | 910 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 911 | } |
EricLew | 5:f6afbd3fc47a | 912 | |
EricLew | 5:f6afbd3fc47a | 913 | /** |
EricLew | 5:f6afbd3fc47a | 914 | * @brief Resumes the audio file stream. |
EricLew | 5:f6afbd3fc47a | 915 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 916 | */ |
EricLew | 5:f6afbd3fc47a | 917 | uint8_t BSP_AUDIO_IN_Resume(void) |
EricLew | 5:f6afbd3fc47a | 918 | { |
EricLew | 5:f6afbd3fc47a | 919 | /* Call the Media layer start function for left channel */ |
EricLew | 5:f6afbd3fc47a | 920 | if(HAL_DFSDM_FilterRegularStart_DMA(&BSP_AUDIO_hDfsdmLeftFilter, |
EricLew | 5:f6afbd3fc47a | 921 | (int32_t*)hAudioIn.LeftRecBuff, |
EricLew | 5:f6afbd3fc47a | 922 | (hAudioIn.RecSize/DEFAULT_AUDIO_IN_CHANNEL_NBR)) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 923 | { |
EricLew | 5:f6afbd3fc47a | 924 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 925 | } |
EricLew | 5:f6afbd3fc47a | 926 | |
EricLew | 5:f6afbd3fc47a | 927 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 928 | } |
EricLew | 5:f6afbd3fc47a | 929 | |
EricLew | 5:f6afbd3fc47a | 930 | /** |
EricLew | 5:f6afbd3fc47a | 931 | * @brief register user callback functions |
EricLew | 5:f6afbd3fc47a | 932 | * @param ErrorCallback: pointer to the error callback function |
EricLew | 5:f6afbd3fc47a | 933 | * @param HalfTransferCallback: pointer to the half transfer callback function |
EricLew | 5:f6afbd3fc47a | 934 | * @param TransferCompleteCallback: pointer to the transfer complete callback function |
EricLew | 5:f6afbd3fc47a | 935 | * @retval None |
EricLew | 5:f6afbd3fc47a | 936 | */ |
EricLew | 5:f6afbd3fc47a | 937 | void BSP_AUDIO_IN_RegisterCallbacks(Audio_CallbackTypeDef ErrorCallback, |
EricLew | 5:f6afbd3fc47a | 938 | Audio_CallbackTypeDef HalfTransferCallback, |
EricLew | 5:f6afbd3fc47a | 939 | Audio_CallbackTypeDef TransferCompleteCallback) |
EricLew | 5:f6afbd3fc47a | 940 | { |
EricLew | 5:f6afbd3fc47a | 941 | hAudioIn.CbError = ErrorCallback; |
EricLew | 5:f6afbd3fc47a | 942 | hAudioIn.CbHalfTransfer = HalfTransferCallback; |
EricLew | 5:f6afbd3fc47a | 943 | hAudioIn.CbTransferComplete = TransferCompleteCallback; |
EricLew | 5:f6afbd3fc47a | 944 | } |
EricLew | 5:f6afbd3fc47a | 945 | /** |
EricLew | 5:f6afbd3fc47a | 946 | * @} |
EricLew | 5:f6afbd3fc47a | 947 | */ |
EricLew | 5:f6afbd3fc47a | 948 | |
EricLew | 5:f6afbd3fc47a | 949 | /* private functions --------------------------------------------------------*/ |
EricLew | 5:f6afbd3fc47a | 950 | /** @addtogroup STM32L476G_NUCLEO_AUDIO_Private_Functions |
EricLew | 5:f6afbd3fc47a | 951 | * @{ |
EricLew | 5:f6afbd3fc47a | 952 | */ |
EricLew | 5:f6afbd3fc47a | 953 | /** |
EricLew | 5:f6afbd3fc47a | 954 | * @brief Initializes the Audio Codec audio interface (SAI). |
EricLew | 5:f6afbd3fc47a | 955 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
EricLew | 5:f6afbd3fc47a | 956 | * @note The default SlotActive configuration is set to CODEC_AUDIOFRAME_SLOT_0123 |
EricLew | 5:f6afbd3fc47a | 957 | * and user can update this configuration using |
EricLew | 5:f6afbd3fc47a | 958 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 959 | */ |
EricLew | 5:f6afbd3fc47a | 960 | //static uint8_t AUDIO_SAIx_Init(uint32_t AudioFreq) |
EricLew | 5:f6afbd3fc47a | 961 | //{ |
EricLew | 5:f6afbd3fc47a | 962 | // /* Disable SAI peripheral to allow access to SAI internal registers */ |
EricLew | 5:f6afbd3fc47a | 963 | // __HAL_SAI_DISABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 964 | // |
EricLew | 5:f6afbd3fc47a | 965 | // /* Initialize the BSP_AUDIO_hSai Instance parameter */ |
EricLew | 5:f6afbd3fc47a | 966 | // BSP_AUDIO_hSai.Instance = AUDIO_SAIx; |
EricLew | 5:f6afbd3fc47a | 967 | // |
EricLew | 5:f6afbd3fc47a | 968 | // /* Configure SAI_Block_x |
EricLew | 5:f6afbd3fc47a | 969 | // LSBFirst: Disabled |
EricLew | 5:f6afbd3fc47a | 970 | // DataSize: 16 */ |
EricLew | 5:f6afbd3fc47a | 971 | // BSP_AUDIO_hSai.Init.AudioMode = SAI_MODEMASTER_TX; |
EricLew | 5:f6afbd3fc47a | 972 | // BSP_AUDIO_hSai.Init.Synchro = SAI_ASYNCHRONOUS; |
EricLew | 5:f6afbd3fc47a | 973 | // BSP_AUDIO_hSai.Init.SynchroExt = SAI_SYNCEXT_DISABLE; |
EricLew | 5:f6afbd3fc47a | 974 | // BSP_AUDIO_hSai.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLE; |
EricLew | 5:f6afbd3fc47a | 975 | // BSP_AUDIO_hSai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE; |
EricLew | 5:f6afbd3fc47a | 976 | // BSP_AUDIO_hSai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF; |
EricLew | 5:f6afbd3fc47a | 977 | // BSP_AUDIO_hSai.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_MCKDIV; |
EricLew | 5:f6afbd3fc47a | 978 | // BSP_AUDIO_hSai.Init.Mckdiv = SAIClockDivider(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 979 | // BSP_AUDIO_hSai.Init.MonoStereoMode = SAI_STEREOMODE; |
EricLew | 5:f6afbd3fc47a | 980 | // BSP_AUDIO_hSai.Init.CompandingMode = SAI_NOCOMPANDING; |
EricLew | 5:f6afbd3fc47a | 981 | // BSP_AUDIO_hSai.Init.TriState = SAI_OUTPUT_NOTRELEASED; |
EricLew | 5:f6afbd3fc47a | 982 | // BSP_AUDIO_hSai.Init.Protocol = SAI_FREE_PROTOCOL; |
EricLew | 5:f6afbd3fc47a | 983 | // BSP_AUDIO_hSai.Init.DataSize = SAI_DATASIZE_16; |
EricLew | 5:f6afbd3fc47a | 984 | // BSP_AUDIO_hSai.Init.FirstBit = SAI_FIRSTBIT_MSB; |
EricLew | 5:f6afbd3fc47a | 985 | // BSP_AUDIO_hSai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE; |
EricLew | 5:f6afbd3fc47a | 986 | // |
EricLew | 5:f6afbd3fc47a | 987 | // /* Configure SAI_Block_x Frame |
EricLew | 5:f6afbd3fc47a | 988 | // Frame Length: 32 |
EricLew | 5:f6afbd3fc47a | 989 | // Frame active Length: 16 |
EricLew | 5:f6afbd3fc47a | 990 | // FS Definition: Start frame + Channel Side identification |
EricLew | 5:f6afbd3fc47a | 991 | // FS Polarity: FS active Low |
EricLew | 5:f6afbd3fc47a | 992 | // FS Offset: FS asserted one bit before the first bit of slot 0 */ |
EricLew | 5:f6afbd3fc47a | 993 | // BSP_AUDIO_hSai.FrameInit.FrameLength = 32; |
EricLew | 5:f6afbd3fc47a | 994 | // BSP_AUDIO_hSai.FrameInit.ActiveFrameLength = 16; |
EricLew | 5:f6afbd3fc47a | 995 | // BSP_AUDIO_hSai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION; |
EricLew | 5:f6afbd3fc47a | 996 | // BSP_AUDIO_hSai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW; |
EricLew | 5:f6afbd3fc47a | 997 | // BSP_AUDIO_hSai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT; |
EricLew | 5:f6afbd3fc47a | 998 | // |
EricLew | 5:f6afbd3fc47a | 999 | // /* Configure SAI Block_x Slot |
EricLew | 5:f6afbd3fc47a | 1000 | // Slot First Bit Offset: 0 |
EricLew | 5:f6afbd3fc47a | 1001 | // Slot Size : 16 |
EricLew | 5:f6afbd3fc47a | 1002 | // Slot Number: 2 |
EricLew | 5:f6afbd3fc47a | 1003 | // Slot Active: Slots 0 and 1 actives */ |
EricLew | 5:f6afbd3fc47a | 1004 | // BSP_AUDIO_hSai.SlotInit.FirstBitOffset = 0; |
EricLew | 5:f6afbd3fc47a | 1005 | // BSP_AUDIO_hSai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE; |
EricLew | 5:f6afbd3fc47a | 1006 | // BSP_AUDIO_hSai.SlotInit.SlotNumber = 2; |
EricLew | 5:f6afbd3fc47a | 1007 | // BSP_AUDIO_hSai.SlotInit.SlotActive = SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1; |
EricLew | 5:f6afbd3fc47a | 1008 | // |
EricLew | 5:f6afbd3fc47a | 1009 | // /* Initializes the SAI peripheral*/ |
EricLew | 5:f6afbd3fc47a | 1010 | // if (HAL_SAI_Init(&BSP_AUDIO_hSai) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1011 | // { |
EricLew | 5:f6afbd3fc47a | 1012 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1013 | // } |
EricLew | 5:f6afbd3fc47a | 1014 | // |
EricLew | 5:f6afbd3fc47a | 1015 | // /* Enable SAI peripheral to generate MCLK */ |
EricLew | 5:f6afbd3fc47a | 1016 | // __HAL_SAI_ENABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 1017 | // |
EricLew | 5:f6afbd3fc47a | 1018 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 1019 | // |
EricLew | 5:f6afbd3fc47a | 1020 | //} |
EricLew | 5:f6afbd3fc47a | 1021 | |
EricLew | 5:f6afbd3fc47a | 1022 | /** |
EricLew | 5:f6afbd3fc47a | 1023 | * @brief De-initializes the Audio Codec audio interface (SAI). |
EricLew | 5:f6afbd3fc47a | 1024 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 1025 | */ |
EricLew | 5:f6afbd3fc47a | 1026 | //static uint8_t AUDIO_SAIx_DeInit(void) |
EricLew | 5:f6afbd3fc47a | 1027 | //{ |
EricLew | 5:f6afbd3fc47a | 1028 | // /* Disable the SAI audio block */ |
EricLew | 5:f6afbd3fc47a | 1029 | // __HAL_SAI_DISABLE(&BSP_AUDIO_hSai); |
EricLew | 5:f6afbd3fc47a | 1030 | // |
EricLew | 5:f6afbd3fc47a | 1031 | // /* De-initializes the SAI peripheral */ |
EricLew | 5:f6afbd3fc47a | 1032 | // if (HAL_SAI_DeInit(&BSP_AUDIO_hSai) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1033 | // { |
EricLew | 5:f6afbd3fc47a | 1034 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1035 | // } |
EricLew | 5:f6afbd3fc47a | 1036 | // |
EricLew | 5:f6afbd3fc47a | 1037 | // /* Disable SAIx PLL */ |
EricLew | 5:f6afbd3fc47a | 1038 | // if (AUDIO_SAIx_PLL_DISABLE() != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 1039 | // { |
EricLew | 5:f6afbd3fc47a | 1040 | // return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1041 | // } |
EricLew | 5:f6afbd3fc47a | 1042 | // |
EricLew | 5:f6afbd3fc47a | 1043 | // return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 1044 | //} |
EricLew | 5:f6afbd3fc47a | 1045 | |
EricLew | 5:f6afbd3fc47a | 1046 | /** |
EricLew | 5:f6afbd3fc47a | 1047 | * @brief SAI MSP Init |
EricLew | 5:f6afbd3fc47a | 1048 | * @param hsai : pointer to a SAI_HandleTypeDef structure |
EricLew | 5:f6afbd3fc47a | 1049 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1050 | */ |
EricLew | 5:f6afbd3fc47a | 1051 | void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai) |
EricLew | 5:f6afbd3fc47a | 1052 | { |
EricLew | 5:f6afbd3fc47a | 1053 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 5:f6afbd3fc47a | 1054 | |
EricLew | 5:f6afbd3fc47a | 1055 | /* Enable SAI clock */ |
EricLew | 5:f6afbd3fc47a | 1056 | AUDIO_SAIx_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1057 | |
EricLew | 5:f6afbd3fc47a | 1058 | /* Enable GPIO clock */ |
EricLew | 5:f6afbd3fc47a | 1059 | AUDIO_SAIx_MCK_SCK_SD_FS_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1060 | |
EricLew | 5:f6afbd3fc47a | 1061 | /* CODEC_SAI pins configuration: FS, SCK, MCK and SD pins ------------------*/ |
EricLew | 5:f6afbd3fc47a | 1062 | GPIO_InitStruct.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN | AUDIO_SAIx_MCK_PIN; |
EricLew | 5:f6afbd3fc47a | 1063 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
EricLew | 5:f6afbd3fc47a | 1064 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 5:f6afbd3fc47a | 1065 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 5:f6afbd3fc47a | 1066 | GPIO_InitStruct.Alternate = AUDIO_SAIx_MCK_SCK_SD_FS_AF; |
EricLew | 5:f6afbd3fc47a | 1067 | HAL_GPIO_Init(AUDIO_SAIx_MCK_SCK_SD_FS_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 5:f6afbd3fc47a | 1068 | |
EricLew | 5:f6afbd3fc47a | 1069 | /* Enable the DMA clock */ |
EricLew | 5:f6afbd3fc47a | 1070 | AUDIO_SAIx_DMAx_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1071 | |
EricLew | 5:f6afbd3fc47a | 1072 | if(hsai->Instance == AUDIO_SAIx) |
EricLew | 5:f6afbd3fc47a | 1073 | { |
EricLew | 5:f6afbd3fc47a | 1074 | /* Configure the hDmaSai handle parameters */ |
EricLew | 5:f6afbd3fc47a | 1075 | hDmaSai.Init.Request = DMA_REQUEST_1; |
EricLew | 5:f6afbd3fc47a | 1076 | hDmaSai.Init.Direction = DMA_MEMORY_TO_PERIPH; |
EricLew | 5:f6afbd3fc47a | 1077 | hDmaSai.Init.PeriphInc = DMA_PINC_DISABLE; |
EricLew | 5:f6afbd3fc47a | 1078 | hDmaSai.Init.MemInc = DMA_MINC_ENABLE; |
EricLew | 5:f6afbd3fc47a | 1079 | hDmaSai.Init.PeriphDataAlignment = AUDIO_SAIx_DMAx_PERIPH_DATA_SIZE; |
EricLew | 5:f6afbd3fc47a | 1080 | hDmaSai.Init.MemDataAlignment = AUDIO_SAIx_DMAx_MEM_DATA_SIZE; |
EricLew | 5:f6afbd3fc47a | 1081 | hDmaSai.Init.Mode = DMA_NORMAL; |
EricLew | 5:f6afbd3fc47a | 1082 | hDmaSai.Init.Priority = DMA_PRIORITY_HIGH; |
EricLew | 5:f6afbd3fc47a | 1083 | |
EricLew | 5:f6afbd3fc47a | 1084 | hDmaSai.Instance = AUDIO_SAIx_DMAx_CHANNEL; |
EricLew | 5:f6afbd3fc47a | 1085 | |
EricLew | 5:f6afbd3fc47a | 1086 | /* Associate the DMA handle */ |
EricLew | 5:f6afbd3fc47a | 1087 | __HAL_LINKDMA(hsai, hdmatx, hDmaSai); |
EricLew | 5:f6afbd3fc47a | 1088 | |
EricLew | 5:f6afbd3fc47a | 1089 | /* Deinitialize the Stream for new transfer */ |
EricLew | 5:f6afbd3fc47a | 1090 | HAL_DMA_DeInit(&hDmaSai); |
EricLew | 5:f6afbd3fc47a | 1091 | |
EricLew | 5:f6afbd3fc47a | 1092 | /* Configure the DMA Stream */ |
EricLew | 5:f6afbd3fc47a | 1093 | HAL_DMA_Init(&hDmaSai); |
EricLew | 5:f6afbd3fc47a | 1094 | } |
EricLew | 5:f6afbd3fc47a | 1095 | |
EricLew | 5:f6afbd3fc47a | 1096 | /* SAI DMA IRQ Channel configuration */ |
EricLew | 5:f6afbd3fc47a | 1097 | HAL_NVIC_SetPriority(AUDIO_SAIx_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0); |
EricLew | 5:f6afbd3fc47a | 1098 | HAL_NVIC_EnableIRQ(AUDIO_SAIx_DMAx_IRQ); |
EricLew | 5:f6afbd3fc47a | 1099 | } |
EricLew | 5:f6afbd3fc47a | 1100 | |
EricLew | 5:f6afbd3fc47a | 1101 | /** |
EricLew | 5:f6afbd3fc47a | 1102 | * @brief SAI MSP De-init |
EricLew | 5:f6afbd3fc47a | 1103 | * @param hsai : pointer to a SAI_HandleTypeDef structure |
EricLew | 5:f6afbd3fc47a | 1104 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1105 | */ |
EricLew | 5:f6afbd3fc47a | 1106 | void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai) |
EricLew | 5:f6afbd3fc47a | 1107 | { |
EricLew | 5:f6afbd3fc47a | 1108 | /* Disable SAI DMA Channel IRQ */ |
EricLew | 5:f6afbd3fc47a | 1109 | HAL_NVIC_DisableIRQ(AUDIO_SAIx_DMAx_IRQ); |
EricLew | 5:f6afbd3fc47a | 1110 | |
EricLew | 5:f6afbd3fc47a | 1111 | /* Reset the DMA Stream configuration*/ |
EricLew | 5:f6afbd3fc47a | 1112 | HAL_DMA_DeInit(&hDmaSai); |
EricLew | 5:f6afbd3fc47a | 1113 | |
EricLew | 5:f6afbd3fc47a | 1114 | /* Disable the DMA clock */ |
EricLew | 5:f6afbd3fc47a | 1115 | AUDIO_SAIx_DMAx_CLK_DISABLE(); |
EricLew | 5:f6afbd3fc47a | 1116 | |
EricLew | 5:f6afbd3fc47a | 1117 | /* De-initialize FS, SCK, MCK and SD pins*/ |
EricLew | 5:f6afbd3fc47a | 1118 | HAL_GPIO_DeInit(AUDIO_SAIx_MCK_SCK_SD_FS_GPIO_PORT, |
EricLew | 5:f6afbd3fc47a | 1119 | AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN | AUDIO_SAIx_MCK_PIN); |
EricLew | 5:f6afbd3fc47a | 1120 | |
EricLew | 5:f6afbd3fc47a | 1121 | /* Disable GPIO clock */ |
EricLew | 5:f6afbd3fc47a | 1122 | AUDIO_SAIx_MCK_SCK_SD_FS_DISABLE(); |
EricLew | 5:f6afbd3fc47a | 1123 | |
EricLew | 5:f6afbd3fc47a | 1124 | /* Disable SAI clock */ |
EricLew | 5:f6afbd3fc47a | 1125 | AUDIO_SAIx_CLK_DISABLE(); |
EricLew | 5:f6afbd3fc47a | 1126 | } |
EricLew | 5:f6afbd3fc47a | 1127 | |
EricLew | 5:f6afbd3fc47a | 1128 | /** |
EricLew | 5:f6afbd3fc47a | 1129 | * @brief Resets the audio codec. It restores the default configuration of the |
EricLew | 5:f6afbd3fc47a | 1130 | * codec (this function shall be called before initializing the codec). |
EricLew | 5:f6afbd3fc47a | 1131 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1132 | */ |
EricLew | 5:f6afbd3fc47a | 1133 | //static void AUDIO_CODEC_Reset(void) |
EricLew | 5:f6afbd3fc47a | 1134 | //{ |
EricLew | 5:f6afbd3fc47a | 1135 | // /* Initialize the audio driver structure */ |
EricLew | 5:f6afbd3fc47a | 1136 | // hAudioOut.AudioDrv = &cs43l22_drv; |
EricLew | 5:f6afbd3fc47a | 1137 | // |
EricLew | 5:f6afbd3fc47a | 1138 | // hAudioOut.AudioDrv->Reset(AUDIO_I2C_ADDRESS); |
EricLew | 5:f6afbd3fc47a | 1139 | //} |
EricLew | 5:f6afbd3fc47a | 1140 | |
EricLew | 5:f6afbd3fc47a | 1141 | /** |
EricLew | 5:f6afbd3fc47a | 1142 | * @} |
EricLew | 5:f6afbd3fc47a | 1143 | */ |
EricLew | 5:f6afbd3fc47a | 1144 | |
EricLew | 5:f6afbd3fc47a | 1145 | /** @addtogroup STM32L476G_NUCLEO_AUDIO_Private_Functions |
EricLew | 5:f6afbd3fc47a | 1146 | * @{ |
EricLew | 5:f6afbd3fc47a | 1147 | */ |
EricLew | 5:f6afbd3fc47a | 1148 | |
EricLew | 5:f6afbd3fc47a | 1149 | /** |
EricLew | 5:f6afbd3fc47a | 1150 | * @brief Initializes the Digital Filter for Sigma-Delta Modulators interface (DFSDM). |
EricLew | 5:f6afbd3fc47a | 1151 | * @param AudioFreq: Audio frequency to be used to set correctly the DFSDM peripheral. |
EricLew | 5:f6afbd3fc47a | 1152 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 1153 | */ |
EricLew | 5:f6afbd3fc47a | 1154 | static uint8_t AUDIO_DFSDMx_Init(uint32_t AudioFreq) |
EricLew | 5:f6afbd3fc47a | 1155 | { |
EricLew | 5:f6afbd3fc47a | 1156 | /*####CHANNEL 2####*/ |
EricLew | 5:f6afbd3fc47a | 1157 | hAudioIn.hDfsdmLeftChannel.Init.OutputClock.Activation = ENABLE; |
EricLew | 5:f6afbd3fc47a | 1158 | hAudioIn.hDfsdmLeftChannel.Init.OutputClock.Selection = DFSDM_CHANNEL_OUTPUT_CLOCK_AUDIO; |
EricLew | 5:f6afbd3fc47a | 1159 | /* Set the DFSDM clock OUT audio frequency configuration */ |
EricLew | 5:f6afbd3fc47a | 1160 | hAudioIn.hDfsdmLeftChannel.Init.OutputClock.Divider = DFSDMClockDivider(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 1161 | hAudioIn.hDfsdmLeftChannel.Init.Input.Multiplexer = DFSDM_CHANNEL_EXTERNAL_INPUTS; |
EricLew | 5:f6afbd3fc47a | 1162 | hAudioIn.hDfsdmLeftChannel.Init.Input.DataPacking = DFSDM_CHANNEL_STANDARD_MODE; |
EricLew | 5:f6afbd3fc47a | 1163 | hAudioIn.hDfsdmLeftChannel.Init.Input.Pins = DFSDM_CHANNEL_SAME_CHANNEL_PINS; |
EricLew | 5:f6afbd3fc47a | 1164 | /* Request to sample stable data for LEFT micro on Rising edge */ |
EricLew | 5:f6afbd3fc47a | 1165 | hAudioIn.hDfsdmLeftChannel.Init.SerialInterface.Type = DFSDM_CHANNEL_SPI_RISING; |
EricLew | 5:f6afbd3fc47a | 1166 | hAudioIn.hDfsdmLeftChannel.Init.SerialInterface.SpiClock = DFSDM_CHANNEL_SPI_CLOCK_INTERNAL; |
EricLew | 5:f6afbd3fc47a | 1167 | hAudioIn.hDfsdmLeftChannel.Init.Awd.FilterOrder = DFSDM_CHANNEL_SINC1_ORDER; |
EricLew | 5:f6afbd3fc47a | 1168 | hAudioIn.hDfsdmLeftChannel.Init.Awd.Oversampling = 10; |
EricLew | 5:f6afbd3fc47a | 1169 | hAudioIn.hDfsdmLeftChannel.Init.Offset = 0; |
EricLew | 5:f6afbd3fc47a | 1170 | hAudioIn.hDfsdmLeftChannel.Init.RightBitShift = DFSDMRightBitShift(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 1171 | |
EricLew | 5:f6afbd3fc47a | 1172 | hAudioIn.hDfsdmLeftChannel.Instance = DFSDM_Channel2; |
EricLew | 5:f6afbd3fc47a | 1173 | |
EricLew | 5:f6afbd3fc47a | 1174 | /* Init the DFSDM Channel */ |
EricLew | 5:f6afbd3fc47a | 1175 | if (HAL_DFSDM_ChannelInit(&hAudioIn.hDfsdmLeftChannel) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1176 | { |
EricLew | 5:f6afbd3fc47a | 1177 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1178 | } |
EricLew | 5:f6afbd3fc47a | 1179 | |
EricLew | 5:f6afbd3fc47a | 1180 | /*####FILTER 0####*/ |
EricLew | 5:f6afbd3fc47a | 1181 | BSP_AUDIO_hDfsdmLeftFilter.Init.RegularParam.Trigger = DFSDM_FILTER_SW_TRIGGER; |
EricLew | 5:f6afbd3fc47a | 1182 | BSP_AUDIO_hDfsdmLeftFilter.Init.RegularParam.FastMode = ENABLE; |
EricLew | 5:f6afbd3fc47a | 1183 | BSP_AUDIO_hDfsdmLeftFilter.Init.RegularParam.DmaMode = ENABLE; |
EricLew | 5:f6afbd3fc47a | 1184 | BSP_AUDIO_hDfsdmLeftFilter.Init.InjectedParam.Trigger = DFSDM_FILTER_SW_TRIGGER; |
EricLew | 5:f6afbd3fc47a | 1185 | BSP_AUDIO_hDfsdmLeftFilter.Init.InjectedParam.ScanMode = DISABLE; |
EricLew | 5:f6afbd3fc47a | 1186 | BSP_AUDIO_hDfsdmLeftFilter.Init.InjectedParam.DmaMode = DISABLE; |
EricLew | 5:f6afbd3fc47a | 1187 | BSP_AUDIO_hDfsdmLeftFilter.Init.InjectedParam.ExtTrigger = DFSDM_FILTER_EXT_TRIG_TIM8_TRGO; |
EricLew | 5:f6afbd3fc47a | 1188 | BSP_AUDIO_hDfsdmLeftFilter.Init.InjectedParam.ExtTriggerEdge = DFSDM_FILTER_EXT_TRIG_BOTH_EDGES; |
EricLew | 5:f6afbd3fc47a | 1189 | BSP_AUDIO_hDfsdmLeftFilter.Init.FilterParam.SincOrder = DFSDMFilterOrder(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 1190 | /* Set the DFSDM Filters Oversampling to have correct sample rate */ |
EricLew | 5:f6afbd3fc47a | 1191 | BSP_AUDIO_hDfsdmLeftFilter.Init.FilterParam.Oversampling = DFSDMOverSampling(AudioFreq); |
EricLew | 5:f6afbd3fc47a | 1192 | BSP_AUDIO_hDfsdmLeftFilter.Init.FilterParam.IntOversampling = 1; |
EricLew | 5:f6afbd3fc47a | 1193 | |
EricLew | 5:f6afbd3fc47a | 1194 | BSP_AUDIO_hDfsdmLeftFilter.Instance = AUDIO_DFSDMx_LEFT_FILTER; |
EricLew | 5:f6afbd3fc47a | 1195 | |
EricLew | 5:f6afbd3fc47a | 1196 | /* Init the DFSDM Filter */ |
EricLew | 5:f6afbd3fc47a | 1197 | if (HAL_DFSDM_FilterInit(&BSP_AUDIO_hDfsdmLeftFilter) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1198 | { |
EricLew | 5:f6afbd3fc47a | 1199 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1200 | } |
EricLew | 5:f6afbd3fc47a | 1201 | |
EricLew | 5:f6afbd3fc47a | 1202 | /* Configure regular channel */ |
EricLew | 5:f6afbd3fc47a | 1203 | if (HAL_DFSDM_FilterConfigRegChannel(&BSP_AUDIO_hDfsdmLeftFilter, |
EricLew | 5:f6afbd3fc47a | 1204 | DFSDM_CHANNEL_2, |
EricLew | 5:f6afbd3fc47a | 1205 | DFSDM_CONTINUOUS_CONV_ON) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1206 | { |
EricLew | 5:f6afbd3fc47a | 1207 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1208 | } |
EricLew | 5:f6afbd3fc47a | 1209 | |
EricLew | 5:f6afbd3fc47a | 1210 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 1211 | } |
EricLew | 5:f6afbd3fc47a | 1212 | |
EricLew | 5:f6afbd3fc47a | 1213 | /** |
EricLew | 5:f6afbd3fc47a | 1214 | * @brief De-initializes the Digital Filter for Sigma-Delta Modulators interface (DFSDM). |
EricLew | 5:f6afbd3fc47a | 1215 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 1216 | */ |
EricLew | 5:f6afbd3fc47a | 1217 | static uint8_t AUDIO_DFSDMx_DeInit(void) |
EricLew | 5:f6afbd3fc47a | 1218 | { |
EricLew | 5:f6afbd3fc47a | 1219 | /* De-initializes the DFSDM filters to allow access to DFSDM internal registers */ |
EricLew | 5:f6afbd3fc47a | 1220 | if (HAL_DFSDM_FilterDeInit(&BSP_AUDIO_hDfsdmLeftFilter) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1221 | { |
EricLew | 5:f6afbd3fc47a | 1222 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1223 | } |
EricLew | 5:f6afbd3fc47a | 1224 | |
EricLew | 5:f6afbd3fc47a | 1225 | /* De-initializes the DFSDM channels to allow access to DFSDM internal registers */ |
EricLew | 5:f6afbd3fc47a | 1226 | if (HAL_DFSDM_ChannelDeInit(&hAudioIn.hDfsdmLeftChannel) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1227 | { |
EricLew | 5:f6afbd3fc47a | 1228 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1229 | } |
EricLew | 5:f6afbd3fc47a | 1230 | |
EricLew | 5:f6afbd3fc47a | 1231 | /* Disable DFSDM clock */ |
EricLew | 5:f6afbd3fc47a | 1232 | AUDIO_DFSDMx_CLK_DISABLE(); |
EricLew | 5:f6afbd3fc47a | 1233 | |
EricLew | 5:f6afbd3fc47a | 1234 | /* Disable SAIx PLL */ |
EricLew | 5:f6afbd3fc47a | 1235 | if (AUDIO_SAIx_PLL_DISABLE() != AUDIO_OK) |
EricLew | 5:f6afbd3fc47a | 1236 | { |
EricLew | 5:f6afbd3fc47a | 1237 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1238 | } |
EricLew | 5:f6afbd3fc47a | 1239 | |
EricLew | 5:f6afbd3fc47a | 1240 | /* DFSDM reset */ |
EricLew | 5:f6afbd3fc47a | 1241 | __HAL_RCC_DFSDM_FORCE_RESET(); |
EricLew | 5:f6afbd3fc47a | 1242 | __HAL_RCC_DFSDM_RELEASE_RESET(); |
EricLew | 5:f6afbd3fc47a | 1243 | |
EricLew | 5:f6afbd3fc47a | 1244 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 1245 | } |
EricLew | 5:f6afbd3fc47a | 1246 | |
EricLew | 5:f6afbd3fc47a | 1247 | /** |
EricLew | 5:f6afbd3fc47a | 1248 | * @brief Initializes the DFSDM channel MSP. |
EricLew | 5:f6afbd3fc47a | 1249 | * @param hdfsdm_channel : DFSDM channel handle. |
EricLew | 5:f6afbd3fc47a | 1250 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1251 | */ |
EricLew | 5:f6afbd3fc47a | 1252 | void HAL_DFSDM_ChannelMspInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel) |
EricLew | 5:f6afbd3fc47a | 1253 | { |
EricLew | 5:f6afbd3fc47a | 1254 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 5:f6afbd3fc47a | 1255 | |
EricLew | 5:f6afbd3fc47a | 1256 | /* Enable DFSDM clock */ |
EricLew | 5:f6afbd3fc47a | 1257 | AUDIO_DFSDMx_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1258 | |
EricLew | 5:f6afbd3fc47a | 1259 | /* Enable GPIO clock */ |
EricLew | 5:f6afbd3fc47a | 1260 | AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1261 | |
EricLew | 5:f6afbd3fc47a | 1262 | /* DFSDM pins configuration: DFSDM_CKOUT, DMIC_DATIN pins ------------------*/ |
EricLew | 5:f6afbd3fc47a | 1263 | GPIO_InitStruct.Pin = AUDIO_DFSDMx_CKOUT_PIN | AUDIO_DFSDMx_DMIC_DATIN_PIN; |
EricLew | 5:f6afbd3fc47a | 1264 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
EricLew | 5:f6afbd3fc47a | 1265 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 5:f6afbd3fc47a | 1266 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 5:f6afbd3fc47a | 1267 | GPIO_InitStruct.Alternate = AUDIO_DFSDMx_CKOUT_DMIC_DATIN_AF; |
EricLew | 5:f6afbd3fc47a | 1268 | HAL_GPIO_Init(AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 5:f6afbd3fc47a | 1269 | } |
EricLew | 5:f6afbd3fc47a | 1270 | |
EricLew | 5:f6afbd3fc47a | 1271 | /** |
EricLew | 5:f6afbd3fc47a | 1272 | * @brief De-initializes the DFSDM channel MSP. |
EricLew | 5:f6afbd3fc47a | 1273 | * @param hdfsdm_channel : DFSDM channel handle. |
EricLew | 5:f6afbd3fc47a | 1274 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1275 | */ |
EricLew | 5:f6afbd3fc47a | 1276 | void HAL_DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel) |
EricLew | 5:f6afbd3fc47a | 1277 | { |
EricLew | 5:f6afbd3fc47a | 1278 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 5:f6afbd3fc47a | 1279 | |
EricLew | 5:f6afbd3fc47a | 1280 | /* Enable GPIO clock */ |
EricLew | 5:f6afbd3fc47a | 1281 | AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1282 | |
EricLew | 5:f6afbd3fc47a | 1283 | /* DFSDM pins configuration: DFSDM_CKOUT */ |
EricLew | 5:f6afbd3fc47a | 1284 | GPIO_InitStruct.Pin = AUDIO_DFSDMx_CKOUT_PIN; |
EricLew | 5:f6afbd3fc47a | 1285 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 5:f6afbd3fc47a | 1286 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 5:f6afbd3fc47a | 1287 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
EricLew | 5:f6afbd3fc47a | 1288 | HAL_GPIO_Init(AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 5:f6afbd3fc47a | 1289 | HAL_GPIO_WritePin(AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_PORT, AUDIO_DFSDMx_CKOUT_PIN, GPIO_PIN_RESET); |
EricLew | 5:f6afbd3fc47a | 1290 | |
EricLew | 5:f6afbd3fc47a | 1291 | |
EricLew | 5:f6afbd3fc47a | 1292 | /* De-initialize DMIC_DATIN pin */ |
EricLew | 5:f6afbd3fc47a | 1293 | HAL_GPIO_DeInit(AUDIO_DFSDMx_CKOUT_DMIC_DATIN_GPIO_PORT, AUDIO_DFSDMx_DMIC_DATIN_PIN); |
EricLew | 5:f6afbd3fc47a | 1294 | } |
EricLew | 5:f6afbd3fc47a | 1295 | |
EricLew | 5:f6afbd3fc47a | 1296 | /** |
EricLew | 5:f6afbd3fc47a | 1297 | * @brief Initializes the DFSDM filter MSP. |
EricLew | 5:f6afbd3fc47a | 1298 | * @param hdfsdm_filter : DFSDM filter handle. |
EricLew | 5:f6afbd3fc47a | 1299 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1300 | */ |
EricLew | 5:f6afbd3fc47a | 1301 | void HAL_DFSDM_FilterMspInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) |
EricLew | 5:f6afbd3fc47a | 1302 | { |
EricLew | 5:f6afbd3fc47a | 1303 | /* Enable DFSDM clock */ |
EricLew | 5:f6afbd3fc47a | 1304 | AUDIO_DFSDMx_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1305 | |
EricLew | 5:f6afbd3fc47a | 1306 | /* Enable the DMA clock */ |
EricLew | 5:f6afbd3fc47a | 1307 | AUDIO_DFSDMx_DMAx_CLK_ENABLE(); |
EricLew | 5:f6afbd3fc47a | 1308 | |
EricLew | 5:f6afbd3fc47a | 1309 | /* Configure the hAudioIn.hDmaDfsdmLeft handle parameters */ |
EricLew | 5:f6afbd3fc47a | 1310 | hAudioIn.hDmaDfsdmLeft.Init.Request = DMA_REQUEST_0; |
EricLew | 5:f6afbd3fc47a | 1311 | hAudioIn.hDmaDfsdmLeft.Init.Direction = DMA_PERIPH_TO_MEMORY; |
EricLew | 5:f6afbd3fc47a | 1312 | hAudioIn.hDmaDfsdmLeft.Init.PeriphInc = DMA_PINC_DISABLE; |
EricLew | 5:f6afbd3fc47a | 1313 | hAudioIn.hDmaDfsdmLeft.Init.MemInc = DMA_MINC_ENABLE; |
EricLew | 5:f6afbd3fc47a | 1314 | hAudioIn.hDmaDfsdmLeft.Init.PeriphDataAlignment = AUDIO_DFSDMx_DMAx_PERIPH_DATA_SIZE; |
EricLew | 5:f6afbd3fc47a | 1315 | hAudioIn.hDmaDfsdmLeft.Init.MemDataAlignment = AUDIO_DFSDMx_DMAx_MEM_DATA_SIZE; |
EricLew | 5:f6afbd3fc47a | 1316 | hAudioIn.hDmaDfsdmLeft.Init.Mode = DMA_CIRCULAR; |
EricLew | 5:f6afbd3fc47a | 1317 | hAudioIn.hDmaDfsdmLeft.Init.Priority = DMA_PRIORITY_HIGH; |
EricLew | 5:f6afbd3fc47a | 1318 | |
EricLew | 5:f6afbd3fc47a | 1319 | hAudioIn.hDmaDfsdmLeft.Instance = AUDIO_DFSDMx_DMAx_LEFT_CHANNEL; |
EricLew | 5:f6afbd3fc47a | 1320 | |
EricLew | 5:f6afbd3fc47a | 1321 | /* Associate the DMA handle */ |
EricLew | 5:f6afbd3fc47a | 1322 | __HAL_LINKDMA(hdfsdm_filter, hdmaReg, hAudioIn.hDmaDfsdmLeft); |
EricLew | 5:f6afbd3fc47a | 1323 | |
EricLew | 5:f6afbd3fc47a | 1324 | /* Reset DMA handle state */ |
EricLew | 5:f6afbd3fc47a | 1325 | __HAL_DMA_RESET_HANDLE_STATE(&hAudioIn.hDmaDfsdmLeft); |
EricLew | 5:f6afbd3fc47a | 1326 | |
EricLew | 5:f6afbd3fc47a | 1327 | /* Configure the DMA Channel */ |
EricLew | 5:f6afbd3fc47a | 1328 | HAL_DMA_Init(&hAudioIn.hDmaDfsdmLeft); |
EricLew | 5:f6afbd3fc47a | 1329 | |
EricLew | 5:f6afbd3fc47a | 1330 | /* DMA IRQ Channel configuration */ |
EricLew | 5:f6afbd3fc47a | 1331 | HAL_NVIC_SetPriority(AUDIO_DFSDMx_DMAx_LEFT_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0); |
EricLew | 5:f6afbd3fc47a | 1332 | HAL_NVIC_EnableIRQ(AUDIO_DFSDMx_DMAx_LEFT_IRQ); |
EricLew | 5:f6afbd3fc47a | 1333 | } |
EricLew | 5:f6afbd3fc47a | 1334 | |
EricLew | 5:f6afbd3fc47a | 1335 | /** |
EricLew | 5:f6afbd3fc47a | 1336 | * @brief De-initializes the DFSDM filter MSP. |
EricLew | 5:f6afbd3fc47a | 1337 | * @param hdfsdm_filter : DFSDM filter handle. |
EricLew | 5:f6afbd3fc47a | 1338 | * @retval None |
EricLew | 5:f6afbd3fc47a | 1339 | */ |
EricLew | 5:f6afbd3fc47a | 1340 | void HAL_DFSDM_FilterMspDeInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) |
EricLew | 5:f6afbd3fc47a | 1341 | { |
EricLew | 5:f6afbd3fc47a | 1342 | /* Disable DMA Channel IRQ */ |
EricLew | 5:f6afbd3fc47a | 1343 | HAL_NVIC_DisableIRQ(AUDIO_DFSDMx_DMAx_LEFT_IRQ); |
EricLew | 5:f6afbd3fc47a | 1344 | |
EricLew | 5:f6afbd3fc47a | 1345 | /* De-initialize the DMA Channel */ |
EricLew | 5:f6afbd3fc47a | 1346 | HAL_DMA_DeInit(&hAudioIn.hDmaDfsdmLeft); |
EricLew | 5:f6afbd3fc47a | 1347 | |
EricLew | 5:f6afbd3fc47a | 1348 | /* Disable the DMA clock */ |
EricLew | 5:f6afbd3fc47a | 1349 | AUDIO_DFSDMx_DMAx_CLK_DISABLE(); |
EricLew | 5:f6afbd3fc47a | 1350 | } |
EricLew | 5:f6afbd3fc47a | 1351 | |
EricLew | 5:f6afbd3fc47a | 1352 | /** |
EricLew | 5:f6afbd3fc47a | 1353 | * @brief Configures the SAI PLL clock according to the required audio frequency. |
EricLew | 5:f6afbd3fc47a | 1354 | * @param Frequency: Audio frequency. |
EricLew | 5:f6afbd3fc47a | 1355 | * @retval BSP AUDIO status |
EricLew | 5:f6afbd3fc47a | 1356 | * @note The SAI PLL input clock must be configured in the user application. |
EricLew | 5:f6afbd3fc47a | 1357 | * The SAI PLL configuration done within this function assumes that |
EricLew | 5:f6afbd3fc47a | 1358 | * the SAI PLL input clock runs at 8 MHz. |
EricLew | 5:f6afbd3fc47a | 1359 | */ |
EricLew | 5:f6afbd3fc47a | 1360 | static uint8_t AUDIO_SAIPLLConfig(uint32_t Frequency) |
EricLew | 5:f6afbd3fc47a | 1361 | { |
EricLew | 5:f6afbd3fc47a | 1362 | RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct; |
EricLew | 5:f6afbd3fc47a | 1363 | |
EricLew | 5:f6afbd3fc47a | 1364 | /* Retreive actual RCC configuration */ |
EricLew | 5:f6afbd3fc47a | 1365 | HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct); |
EricLew | 5:f6afbd3fc47a | 1366 | |
EricLew | 5:f6afbd3fc47a | 1367 | if ( (Frequency == AUDIO_FREQUENCY_11K) |
EricLew | 5:f6afbd3fc47a | 1368 | || (Frequency == AUDIO_FREQUENCY_22K) |
EricLew | 5:f6afbd3fc47a | 1369 | || (Frequency == AUDIO_FREQUENCY_44K) ) |
EricLew | 5:f6afbd3fc47a | 1370 | { |
EricLew | 5:f6afbd3fc47a | 1371 | /* Configure PLLSAI prescalers */ |
EricLew | 5:f6afbd3fc47a | 1372 | /* SAI clock config |
EricLew | 5:f6afbd3fc47a | 1373 | PLLSAI1_VCO= 8 Mhz * PLLSAI1N = 8 * 24 = VCO_192M |
EricLew | 5:f6afbd3fc47a | 1374 | SAI_CK_x = PLLSAI1_VCO/PLLSAI1P = 192/17 = 11.294 Mhz */ |
EricLew | 5:f6afbd3fc47a | 1375 | RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI1; |
EricLew | 5:f6afbd3fc47a | 1376 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1N = 24; |
EricLew | 5:f6afbd3fc47a | 1377 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1P = 17; |
EricLew | 5:f6afbd3fc47a | 1378 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK; |
EricLew | 5:f6afbd3fc47a | 1379 | RCC_ExCLKInitStruct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1; |
EricLew | 5:f6afbd3fc47a | 1380 | } |
EricLew | 5:f6afbd3fc47a | 1381 | else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_48K, AUDIO_FREQUENCY_96K */ |
EricLew | 5:f6afbd3fc47a | 1382 | { |
EricLew | 5:f6afbd3fc47a | 1383 | /* SAI clock config |
EricLew | 5:f6afbd3fc47a | 1384 | PLLSAI1_VCO= 8 Mhz * PLLSAI1N = 8 * 43 = VCO_344M |
EricLew | 5:f6afbd3fc47a | 1385 | SAI_CK_x = PLLSAI1_VCO/PLLSAI1P = 344/7 = 49.142 Mhz */ |
EricLew | 5:f6afbd3fc47a | 1386 | RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI1; |
EricLew | 5:f6afbd3fc47a | 1387 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1N = 43; |
EricLew | 5:f6afbd3fc47a | 1388 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1P = 7; |
EricLew | 5:f6afbd3fc47a | 1389 | RCC_ExCLKInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK; |
EricLew | 5:f6afbd3fc47a | 1390 | RCC_ExCLKInitStruct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1; |
EricLew | 5:f6afbd3fc47a | 1391 | } |
EricLew | 5:f6afbd3fc47a | 1392 | |
EricLew | 5:f6afbd3fc47a | 1393 | if (HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct) != HAL_OK) |
EricLew | 5:f6afbd3fc47a | 1394 | { |
EricLew | 5:f6afbd3fc47a | 1395 | return AUDIO_ERROR; |
EricLew | 5:f6afbd3fc47a | 1396 | } |
EricLew | 5:f6afbd3fc47a | 1397 | |
EricLew | 5:f6afbd3fc47a | 1398 | return AUDIO_OK; |
EricLew | 5:f6afbd3fc47a | 1399 | } |
EricLew | 5:f6afbd3fc47a | 1400 | |
EricLew | 5:f6afbd3fc47a | 1401 | /** |
EricLew | 5:f6afbd3fc47a | 1402 | * @} |
EricLew | 5:f6afbd3fc47a | 1403 | */ |
EricLew | 5:f6afbd3fc47a | 1404 | |
EricLew | 5:f6afbd3fc47a | 1405 | /** |
EricLew | 5:f6afbd3fc47a | 1406 | * @} |
EricLew | 5:f6afbd3fc47a | 1407 | */ |
EricLew | 5:f6afbd3fc47a | 1408 | |
EricLew | 5:f6afbd3fc47a | 1409 | /** |
EricLew | 5:f6afbd3fc47a | 1410 | * @} |
EricLew | 5:f6afbd3fc47a | 1411 | */ |
EricLew | 5:f6afbd3fc47a | 1412 | |
EricLew | 5:f6afbd3fc47a | 1413 | /** |
EricLew | 5:f6afbd3fc47a | 1414 | * @} |
EricLew | 5:f6afbd3fc47a | 1415 | */ |
EricLew | 5:f6afbd3fc47a | 1416 | |
EricLew | 5:f6afbd3fc47a | 1417 | /** |
EricLew | 5:f6afbd3fc47a | 1418 | * @} |
EricLew | 5:f6afbd3fc47a | 1419 | */ |
EricLew | 5:f6afbd3fc47a | 1420 | |
EricLew | 5:f6afbd3fc47a | 1421 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
EricLew | 5:f6afbd3fc47a | 1422 |