nguyen nam
/
BSP-DISCO_F746NG
Nam
Drivers/BSP/STM32746G-Discovery/stm32746g_discovery_audio.c@6:e1d9da7fe856, 2016-11-10 (annotated)
- Committer:
- bcostm
- Date:
- Thu Nov 10 09:57:11 2016 +0100
- Revision:
- 6:e1d9da7fe856
- Child:
- 8:56384bddaba5
Update all files present in STM32CubeF7 V1.5.1. Keep original folders structure.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 6:e1d9da7fe856 | 1 | /** |
bcostm | 6:e1d9da7fe856 | 2 | ****************************************************************************** |
bcostm | 6:e1d9da7fe856 | 3 | * @file stm32746g_discovery_audio.c |
bcostm | 6:e1d9da7fe856 | 4 | * @author MCD Application Team |
bcostm | 6:e1d9da7fe856 | 5 | * @version V1.1.1 |
bcostm | 6:e1d9da7fe856 | 6 | * @date 02-June-2016 |
bcostm | 6:e1d9da7fe856 | 7 | * @brief This file provides the Audio driver for the STM32746G-Discovery board. |
bcostm | 6:e1d9da7fe856 | 8 | @verbatim |
bcostm | 6:e1d9da7fe856 | 9 | How To use this driver: |
bcostm | 6:e1d9da7fe856 | 10 | ----------------------- |
bcostm | 6:e1d9da7fe856 | 11 | + This driver supports STM32F7xx devices on STM32746G-Discovery (MB1191) board. |
bcostm | 6:e1d9da7fe856 | 12 | + Call the function BSP_AUDIO_OUT_Init( |
bcostm | 6:e1d9da7fe856 | 13 | OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER, |
bcostm | 6:e1d9da7fe856 | 14 | OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH) |
bcostm | 6:e1d9da7fe856 | 15 | Volume : Initial volume to be set (0 is min (mute), 100 is max (100%) |
bcostm | 6:e1d9da7fe856 | 16 | AudioFreq : Audio frequency in Hz (8000, 16000, 22500, 32000...) |
bcostm | 6:e1d9da7fe856 | 17 | this parameter is relative to the audio file/stream type. |
bcostm | 6:e1d9da7fe856 | 18 | ) |
bcostm | 6:e1d9da7fe856 | 19 | This function configures all the hardware required for the audio application (codec, I2C, SAI, |
bcostm | 6:e1d9da7fe856 | 20 | GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if configuration is OK. |
bcostm | 6:e1d9da7fe856 | 21 | If the returned value is different from AUDIO_OK or the function is stuck then the communication with |
bcostm | 6:e1d9da7fe856 | 22 | the codec or the MFX has failed (try to un-plug the power or reset device in this case). |
bcostm | 6:e1d9da7fe856 | 23 | - OUTPUT_DEVICE_SPEAKER : only speaker will be set as output for the audio stream. |
bcostm | 6:e1d9da7fe856 | 24 | - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream. |
bcostm | 6:e1d9da7fe856 | 25 | - OUTPUT_DEVICE_BOTH : both Speaker and Headphone are used as outputs for the audio stream |
bcostm | 6:e1d9da7fe856 | 26 | at the same time. |
bcostm | 6:e1d9da7fe856 | 27 | Note. On STM32746G-Discovery SAI_DMA is configured in CIRCULAR mode. Due to this the application |
bcostm | 6:e1d9da7fe856 | 28 | does NOT need to call BSP_AUDIO_OUT_ChangeBuffer() to assure streaming. |
bcostm | 6:e1d9da7fe856 | 29 | + Call the function BSP_DISCOVERY_AUDIO_OUT_Play( |
bcostm | 6:e1d9da7fe856 | 30 | pBuffer: pointer to the audio data file address |
bcostm | 6:e1d9da7fe856 | 31 | Size : size of the buffer to be sent in Bytes |
bcostm | 6:e1d9da7fe856 | 32 | ) |
bcostm | 6:e1d9da7fe856 | 33 | to start playing (for the first time) from the audio file/stream. |
bcostm | 6:e1d9da7fe856 | 34 | + Call the function BSP_AUDIO_OUT_Pause() to pause playing |
bcostm | 6:e1d9da7fe856 | 35 | + Call the function BSP_AUDIO_OUT_Resume() to resume playing. |
bcostm | 6:e1d9da7fe856 | 36 | Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called |
bcostm | 6:e1d9da7fe856 | 37 | for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case). |
bcostm | 6:e1d9da7fe856 | 38 | Note. This function should be called only when the audio file is played or paused (not stopped). |
bcostm | 6:e1d9da7fe856 | 39 | + For each mode, you may need to implement the relative callback functions into your code. |
bcostm | 6:e1d9da7fe856 | 40 | The Callback functions are named AUDIO_OUT_XXX_CallBack() and only their prototypes are declared in |
bcostm | 6:e1d9da7fe856 | 41 | the stm32746g_discovery_audio.h file. (refer to the example for more details on the callbacks implementations) |
bcostm | 6:e1d9da7fe856 | 42 | + To Stop playing, to modify the volume level, the frequency, the audio frame slot, |
bcostm | 6:e1d9da7fe856 | 43 | the device output mode the mute or the stop, use the functions: BSP_AUDIO_OUT_SetVolume(), |
bcostm | 6:e1d9da7fe856 | 44 | AUDIO_OUT_SetFrequency(), BSP_AUDIO_OUT_SetAudioFrameSlot(), BSP_AUDIO_OUT_SetOutputMode(), |
bcostm | 6:e1d9da7fe856 | 45 | BSP_AUDIO_OUT_SetMute() and BSP_AUDIO_OUT_Stop(). |
bcostm | 6:e1d9da7fe856 | 46 | + The driver API and the callback functions are at the end of the stm32746g_discovery_audio.h file. |
bcostm | 6:e1d9da7fe856 | 47 | |
bcostm | 6:e1d9da7fe856 | 48 | Driver architecture: |
bcostm | 6:e1d9da7fe856 | 49 | -------------------- |
bcostm | 6:e1d9da7fe856 | 50 | + This driver provides the High Audio Layer: consists of the function API exported in the stm32746g_discovery_audio.h file |
bcostm | 6:e1d9da7fe856 | 51 | (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...) |
bcostm | 6:e1d9da7fe856 | 52 | + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/ |
bcostm | 6:e1d9da7fe856 | 53 | providing the audio file/stream. These functions are also included as local functions into |
bcostm | 6:e1d9da7fe856 | 54 | the stm32746g_discovery_audio_codec.c file (SAIx_Out_Init() and SAIx_Out_DeInit(), SAIx_In_Init() and SAIx_In_DeInit()) |
bcostm | 6:e1d9da7fe856 | 55 | |
bcostm | 6:e1d9da7fe856 | 56 | Known Limitations: |
bcostm | 6:e1d9da7fe856 | 57 | ------------------ |
bcostm | 6:e1d9da7fe856 | 58 | 1- If the TDM Format used to play in parallel 2 audio Stream (the first Stream is configured in codec SLOT0 and second |
bcostm | 6:e1d9da7fe856 | 59 | Stream in SLOT1) the Pause/Resume, volume and mute feature will control the both streams. |
bcostm | 6:e1d9da7fe856 | 60 | 2- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size, |
bcostm | 6:e1d9da7fe856 | 61 | File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file. |
bcostm | 6:e1d9da7fe856 | 62 | 3- Supports only Stereo audio streaming. |
bcostm | 6:e1d9da7fe856 | 63 | 4- Supports only 16-bits audio data size. |
bcostm | 6:e1d9da7fe856 | 64 | @endverbatim |
bcostm | 6:e1d9da7fe856 | 65 | ****************************************************************************** |
bcostm | 6:e1d9da7fe856 | 66 | * @attention |
bcostm | 6:e1d9da7fe856 | 67 | * |
bcostm | 6:e1d9da7fe856 | 68 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
bcostm | 6:e1d9da7fe856 | 69 | * |
bcostm | 6:e1d9da7fe856 | 70 | * Redistribution and use in source and binary forms, with or without modification, |
bcostm | 6:e1d9da7fe856 | 71 | * are permitted provided that the following conditions are met: |
bcostm | 6:e1d9da7fe856 | 72 | * 1. Redistributions of source code must retain the above copyright notice, |
bcostm | 6:e1d9da7fe856 | 73 | * this list of conditions and the following disclaimer. |
bcostm | 6:e1d9da7fe856 | 74 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
bcostm | 6:e1d9da7fe856 | 75 | * this list of conditions and the following disclaimer in the documentation |
bcostm | 6:e1d9da7fe856 | 76 | * and/or other materials provided with the distribution. |
bcostm | 6:e1d9da7fe856 | 77 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
bcostm | 6:e1d9da7fe856 | 78 | * may be used to endorse or promote products derived from this software |
bcostm | 6:e1d9da7fe856 | 79 | * without specific prior written permission. |
bcostm | 6:e1d9da7fe856 | 80 | * |
bcostm | 6:e1d9da7fe856 | 81 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bcostm | 6:e1d9da7fe856 | 82 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bcostm | 6:e1d9da7fe856 | 83 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
bcostm | 6:e1d9da7fe856 | 84 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
bcostm | 6:e1d9da7fe856 | 85 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
bcostm | 6:e1d9da7fe856 | 86 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
bcostm | 6:e1d9da7fe856 | 87 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
bcostm | 6:e1d9da7fe856 | 88 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
bcostm | 6:e1d9da7fe856 | 89 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
bcostm | 6:e1d9da7fe856 | 90 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bcostm | 6:e1d9da7fe856 | 91 | * |
bcostm | 6:e1d9da7fe856 | 92 | ****************************************************************************** |
bcostm | 6:e1d9da7fe856 | 93 | */ |
bcostm | 6:e1d9da7fe856 | 94 | |
bcostm | 6:e1d9da7fe856 | 95 | /* Includes ------------------------------------------------------------------*/ |
bcostm | 6:e1d9da7fe856 | 96 | #include "stm32746g_discovery_audio.h" |
bcostm | 6:e1d9da7fe856 | 97 | |
bcostm | 6:e1d9da7fe856 | 98 | /** @addtogroup BSP |
bcostm | 6:e1d9da7fe856 | 99 | * @{ |
bcostm | 6:e1d9da7fe856 | 100 | */ |
bcostm | 6:e1d9da7fe856 | 101 | |
bcostm | 6:e1d9da7fe856 | 102 | /** @addtogroup STM32746G_DISCOVERY |
bcostm | 6:e1d9da7fe856 | 103 | * @{ |
bcostm | 6:e1d9da7fe856 | 104 | */ |
bcostm | 6:e1d9da7fe856 | 105 | |
bcostm | 6:e1d9da7fe856 | 106 | /** @defgroup STM32746G_DISCOVERY_AUDIO STM32746G_DISCOVERY AUDIO |
bcostm | 6:e1d9da7fe856 | 107 | * @brief This file includes the low layer driver for wm8994 Audio Codec |
bcostm | 6:e1d9da7fe856 | 108 | * available on STM32746G-Discovery board(MB1191). |
bcostm | 6:e1d9da7fe856 | 109 | * @{ |
bcostm | 6:e1d9da7fe856 | 110 | */ |
bcostm | 6:e1d9da7fe856 | 111 | |
bcostm | 6:e1d9da7fe856 | 112 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Private_Types STM32746G_DISCOVERY AUDIO Private Types |
bcostm | 6:e1d9da7fe856 | 113 | * @{ |
bcostm | 6:e1d9da7fe856 | 114 | */ |
bcostm | 6:e1d9da7fe856 | 115 | /** |
bcostm | 6:e1d9da7fe856 | 116 | * @} |
bcostm | 6:e1d9da7fe856 | 117 | */ |
bcostm | 6:e1d9da7fe856 | 118 | |
bcostm | 6:e1d9da7fe856 | 119 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Private_Defines STM32746G_DISCOVERY AUDIO Private Defines |
bcostm | 6:e1d9da7fe856 | 120 | * @{ |
bcostm | 6:e1d9da7fe856 | 121 | */ |
bcostm | 6:e1d9da7fe856 | 122 | /** |
bcostm | 6:e1d9da7fe856 | 123 | * @} |
bcostm | 6:e1d9da7fe856 | 124 | */ |
bcostm | 6:e1d9da7fe856 | 125 | |
bcostm | 6:e1d9da7fe856 | 126 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Private_Macros STM32746G_DISCOVERY AUDIO Private Macros |
bcostm | 6:e1d9da7fe856 | 127 | * @{ |
bcostm | 6:e1d9da7fe856 | 128 | */ |
bcostm | 6:e1d9da7fe856 | 129 | /** |
bcostm | 6:e1d9da7fe856 | 130 | * @} |
bcostm | 6:e1d9da7fe856 | 131 | */ |
bcostm | 6:e1d9da7fe856 | 132 | |
bcostm | 6:e1d9da7fe856 | 133 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Private_Variables STM32746G_DISCOVERY AUDIO Private Variables |
bcostm | 6:e1d9da7fe856 | 134 | * @{ |
bcostm | 6:e1d9da7fe856 | 135 | */ |
bcostm | 6:e1d9da7fe856 | 136 | AUDIO_DrvTypeDef *audio_drv; |
bcostm | 6:e1d9da7fe856 | 137 | SAI_HandleTypeDef haudio_out_sai={0}; |
bcostm | 6:e1d9da7fe856 | 138 | SAI_HandleTypeDef haudio_in_sai={0}; |
bcostm | 6:e1d9da7fe856 | 139 | TIM_HandleTypeDef haudio_tim; |
bcostm | 6:e1d9da7fe856 | 140 | |
bcostm | 6:e1d9da7fe856 | 141 | uint16_t __IO AudioInVolume = DEFAULT_AUDIO_IN_VOLUME; |
bcostm | 6:e1d9da7fe856 | 142 | |
bcostm | 6:e1d9da7fe856 | 143 | /** |
bcostm | 6:e1d9da7fe856 | 144 | * @} |
bcostm | 6:e1d9da7fe856 | 145 | */ |
bcostm | 6:e1d9da7fe856 | 146 | |
bcostm | 6:e1d9da7fe856 | 147 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Private_Function_Prototypes STM32746G_DISCOVERY AUDIO Private Function Prototypes |
bcostm | 6:e1d9da7fe856 | 148 | * @{ |
bcostm | 6:e1d9da7fe856 | 149 | */ |
bcostm | 6:e1d9da7fe856 | 150 | static void AUDIO_IN_INT_IRQHandler(void); // MBED |
bcostm | 6:e1d9da7fe856 | 151 | static void AUDIO_IN_SAIx_DMAx_IRQHandler(void); // MBED |
bcostm | 6:e1d9da7fe856 | 152 | static void AUDIO_OUT_SAIx_DMAx_IRQHandler(void); // MBED |
bcostm | 6:e1d9da7fe856 | 153 | static void SAIx_Out_Init(uint32_t AudioFreq); |
bcostm | 6:e1d9da7fe856 | 154 | static void SAIx_Out_DeInit(void); |
bcostm | 6:e1d9da7fe856 | 155 | static void SAIx_In_Init(uint32_t SaiOutMode, uint32_t SlotActive, uint32_t AudioFreq); |
bcostm | 6:e1d9da7fe856 | 156 | static void SAIx_In_DeInit(void); |
bcostm | 6:e1d9da7fe856 | 157 | /** |
bcostm | 6:e1d9da7fe856 | 158 | * @} |
bcostm | 6:e1d9da7fe856 | 159 | */ |
bcostm | 6:e1d9da7fe856 | 160 | |
bcostm | 6:e1d9da7fe856 | 161 | /** @defgroup STM32746G_DISCOVERY_AUDIO_OUT_Exported_Functions STM32746G_DISCOVERY AUDIO Out Exported Functions |
bcostm | 6:e1d9da7fe856 | 162 | * @{ |
bcostm | 6:e1d9da7fe856 | 163 | */ |
bcostm | 6:e1d9da7fe856 | 164 | |
bcostm | 6:e1d9da7fe856 | 165 | /** |
bcostm | 6:e1d9da7fe856 | 166 | * @brief Configures the audio peripherals. |
bcostm | 6:e1d9da7fe856 | 167 | * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, |
bcostm | 6:e1d9da7fe856 | 168 | * or OUTPUT_DEVICE_BOTH. |
bcostm | 6:e1d9da7fe856 | 169 | * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max)) |
bcostm | 6:e1d9da7fe856 | 170 | * @param AudioFreq: Audio frequency used to play the audio stream. |
bcostm | 6:e1d9da7fe856 | 171 | * @note The I2S PLL input clock must be done in the user application. |
bcostm | 6:e1d9da7fe856 | 172 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 173 | */ |
bcostm | 6:e1d9da7fe856 | 174 | uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 175 | { |
bcostm | 6:e1d9da7fe856 | 176 | uint8_t ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 177 | uint32_t deviceid = 0x00; |
bcostm | 6:e1d9da7fe856 | 178 | |
bcostm | 6:e1d9da7fe856 | 179 | /* Disable SAI */ |
bcostm | 6:e1d9da7fe856 | 180 | SAIx_Out_DeInit(); |
bcostm | 6:e1d9da7fe856 | 181 | |
bcostm | 6:e1d9da7fe856 | 182 | /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */ |
bcostm | 6:e1d9da7fe856 | 183 | BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL); |
bcostm | 6:e1d9da7fe856 | 184 | |
bcostm | 6:e1d9da7fe856 | 185 | /* SAI data transfer preparation: |
bcostm | 6:e1d9da7fe856 | 186 | Prepare the Media to be used for the audio transfer from memory to SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 187 | haudio_out_sai.Instance = AUDIO_OUT_SAIx; |
bcostm | 6:e1d9da7fe856 | 188 | if(HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET) |
bcostm | 6:e1d9da7fe856 | 189 | { |
bcostm | 6:e1d9da7fe856 | 190 | /* Init the SAI MSP: this __weak function can be redefined by the application*/ |
bcostm | 6:e1d9da7fe856 | 191 | BSP_AUDIO_OUT_MspInit(&haudio_out_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 192 | } |
bcostm | 6:e1d9da7fe856 | 193 | SAIx_Out_Init(AudioFreq); |
bcostm | 6:e1d9da7fe856 | 194 | |
bcostm | 6:e1d9da7fe856 | 195 | /* wm8994 codec initialization */ |
bcostm | 6:e1d9da7fe856 | 196 | deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 197 | |
bcostm | 6:e1d9da7fe856 | 198 | if((deviceid) == WM8994_ID) |
bcostm | 6:e1d9da7fe856 | 199 | { |
bcostm | 6:e1d9da7fe856 | 200 | /* Reset the Codec Registers */ |
bcostm | 6:e1d9da7fe856 | 201 | wm8994_drv.Reset(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 202 | /* Initialize the audio driver structure */ |
bcostm | 6:e1d9da7fe856 | 203 | audio_drv = &wm8994_drv; |
bcostm | 6:e1d9da7fe856 | 204 | ret = AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 205 | } |
bcostm | 6:e1d9da7fe856 | 206 | else |
bcostm | 6:e1d9da7fe856 | 207 | { |
bcostm | 6:e1d9da7fe856 | 208 | ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 209 | } |
bcostm | 6:e1d9da7fe856 | 210 | |
bcostm | 6:e1d9da7fe856 | 211 | if(ret == AUDIO_OK) |
bcostm | 6:e1d9da7fe856 | 212 | { |
bcostm | 6:e1d9da7fe856 | 213 | /* Initialize the codec internal registers */ |
bcostm | 6:e1d9da7fe856 | 214 | audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq); |
bcostm | 6:e1d9da7fe856 | 215 | } |
bcostm | 6:e1d9da7fe856 | 216 | |
bcostm | 6:e1d9da7fe856 | 217 | return ret; |
bcostm | 6:e1d9da7fe856 | 218 | } |
bcostm | 6:e1d9da7fe856 | 219 | |
bcostm | 6:e1d9da7fe856 | 220 | /** |
bcostm | 6:e1d9da7fe856 | 221 | * @brief Starts playing audio stream from a data buffer for a determined size. |
bcostm | 6:e1d9da7fe856 | 222 | * @param pBuffer: Pointer to the buffer |
bcostm | 6:e1d9da7fe856 | 223 | * @param Size: Number of audio data in BYTES unit. |
bcostm | 6:e1d9da7fe856 | 224 | * In memory, first element is for left channel, second element is for right channel |
bcostm | 6:e1d9da7fe856 | 225 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 226 | */ |
bcostm | 6:e1d9da7fe856 | 227 | uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size) |
bcostm | 6:e1d9da7fe856 | 228 | { |
bcostm | 6:e1d9da7fe856 | 229 | /* Call the audio Codec Play function */ |
bcostm | 6:e1d9da7fe856 | 230 | if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) |
bcostm | 6:e1d9da7fe856 | 231 | { |
bcostm | 6:e1d9da7fe856 | 232 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 233 | } |
bcostm | 6:e1d9da7fe856 | 234 | else |
bcostm | 6:e1d9da7fe856 | 235 | { |
bcostm | 6:e1d9da7fe856 | 236 | /* Update the Media layer and enable it for play */ |
bcostm | 6:e1d9da7fe856 | 237 | HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*) pBuffer, DMA_MAX(Size / AUDIODATA_SIZE)); |
bcostm | 6:e1d9da7fe856 | 238 | |
bcostm | 6:e1d9da7fe856 | 239 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 240 | } |
bcostm | 6:e1d9da7fe856 | 241 | } |
bcostm | 6:e1d9da7fe856 | 242 | |
bcostm | 6:e1d9da7fe856 | 243 | /** |
bcostm | 6:e1d9da7fe856 | 244 | * @brief Sends n-Bytes on the SAI interface. |
bcostm | 6:e1d9da7fe856 | 245 | * @param pData: pointer on data address |
bcostm | 6:e1d9da7fe856 | 246 | * @param Size: number of data to be written |
bcostm | 6:e1d9da7fe856 | 247 | * @retval None |
bcostm | 6:e1d9da7fe856 | 248 | */ |
bcostm | 6:e1d9da7fe856 | 249 | void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size) |
bcostm | 6:e1d9da7fe856 | 250 | { |
bcostm | 6:e1d9da7fe856 | 251 | HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*) pData, Size); |
bcostm | 6:e1d9da7fe856 | 252 | } |
bcostm | 6:e1d9da7fe856 | 253 | |
bcostm | 6:e1d9da7fe856 | 254 | /** |
bcostm | 6:e1d9da7fe856 | 255 | * @brief This function Pauses the audio file stream. In case |
bcostm | 6:e1d9da7fe856 | 256 | * of using DMA, the DMA Pause feature is used. |
bcostm | 6:e1d9da7fe856 | 257 | * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only |
bcostm | 6:e1d9da7fe856 | 258 | * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() |
bcostm | 6:e1d9da7fe856 | 259 | * function for resume could lead to unexpected behaviour). |
bcostm | 6:e1d9da7fe856 | 260 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 261 | */ |
bcostm | 6:e1d9da7fe856 | 262 | uint8_t BSP_AUDIO_OUT_Pause(void) |
bcostm | 6:e1d9da7fe856 | 263 | { |
bcostm | 6:e1d9da7fe856 | 264 | /* Call the Audio Codec Pause/Resume function */ |
bcostm | 6:e1d9da7fe856 | 265 | if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) |
bcostm | 6:e1d9da7fe856 | 266 | { |
bcostm | 6:e1d9da7fe856 | 267 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 268 | } |
bcostm | 6:e1d9da7fe856 | 269 | else |
bcostm | 6:e1d9da7fe856 | 270 | { |
bcostm | 6:e1d9da7fe856 | 271 | /* Call the Media layer pause function */ |
bcostm | 6:e1d9da7fe856 | 272 | HAL_SAI_DMAPause(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 273 | |
bcostm | 6:e1d9da7fe856 | 274 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 275 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 276 | } |
bcostm | 6:e1d9da7fe856 | 277 | } |
bcostm | 6:e1d9da7fe856 | 278 | |
bcostm | 6:e1d9da7fe856 | 279 | /** |
bcostm | 6:e1d9da7fe856 | 280 | * @brief This function Resumes the audio file stream. |
bcostm | 6:e1d9da7fe856 | 281 | * @note When calling BSP_AUDIO_OUT_Pause() function for pause, only |
bcostm | 6:e1d9da7fe856 | 282 | * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() |
bcostm | 6:e1d9da7fe856 | 283 | * function for resume could lead to unexpected behaviour). |
bcostm | 6:e1d9da7fe856 | 284 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 285 | */ |
bcostm | 6:e1d9da7fe856 | 286 | uint8_t BSP_AUDIO_OUT_Resume(void) |
bcostm | 6:e1d9da7fe856 | 287 | { |
bcostm | 6:e1d9da7fe856 | 288 | /* Call the Audio Codec Pause/Resume function */ |
bcostm | 6:e1d9da7fe856 | 289 | if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0) |
bcostm | 6:e1d9da7fe856 | 290 | { |
bcostm | 6:e1d9da7fe856 | 291 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 292 | } |
bcostm | 6:e1d9da7fe856 | 293 | else |
bcostm | 6:e1d9da7fe856 | 294 | { |
bcostm | 6:e1d9da7fe856 | 295 | /* Call the Media layer pause/resume function */ |
bcostm | 6:e1d9da7fe856 | 296 | HAL_SAI_DMAResume(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 297 | |
bcostm | 6:e1d9da7fe856 | 298 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 299 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 300 | } |
bcostm | 6:e1d9da7fe856 | 301 | } |
bcostm | 6:e1d9da7fe856 | 302 | |
bcostm | 6:e1d9da7fe856 | 303 | /** |
bcostm | 6:e1d9da7fe856 | 304 | * @brief Stops audio playing and Power down the Audio Codec. |
bcostm | 6:e1d9da7fe856 | 305 | * @param Option: could be one of the following parameters |
bcostm | 6:e1d9da7fe856 | 306 | * - CODEC_PDWN_SW: for software power off (by writing registers). |
bcostm | 6:e1d9da7fe856 | 307 | * Then no need to reconfigure the Codec after power on. |
bcostm | 6:e1d9da7fe856 | 308 | * - CODEC_PDWN_HW: completely shut down the codec (physically). |
bcostm | 6:e1d9da7fe856 | 309 | * Then need to reconfigure the Codec after power on. |
bcostm | 6:e1d9da7fe856 | 310 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 311 | */ |
bcostm | 6:e1d9da7fe856 | 312 | uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option) |
bcostm | 6:e1d9da7fe856 | 313 | { |
bcostm | 6:e1d9da7fe856 | 314 | /* Call the Media layer stop function */ |
bcostm | 6:e1d9da7fe856 | 315 | HAL_SAI_DMAStop(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 316 | |
bcostm | 6:e1d9da7fe856 | 317 | /* Call Audio Codec Stop function */ |
bcostm | 6:e1d9da7fe856 | 318 | if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) |
bcostm | 6:e1d9da7fe856 | 319 | { |
bcostm | 6:e1d9da7fe856 | 320 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 321 | } |
bcostm | 6:e1d9da7fe856 | 322 | else |
bcostm | 6:e1d9da7fe856 | 323 | { |
bcostm | 6:e1d9da7fe856 | 324 | if(Option == CODEC_PDWN_HW) |
bcostm | 6:e1d9da7fe856 | 325 | { |
bcostm | 6:e1d9da7fe856 | 326 | /* Wait at least 100us */ |
bcostm | 6:e1d9da7fe856 | 327 | HAL_Delay(1); |
bcostm | 6:e1d9da7fe856 | 328 | } |
bcostm | 6:e1d9da7fe856 | 329 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 330 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 331 | } |
bcostm | 6:e1d9da7fe856 | 332 | } |
bcostm | 6:e1d9da7fe856 | 333 | |
bcostm | 6:e1d9da7fe856 | 334 | /** |
bcostm | 6:e1d9da7fe856 | 335 | * @brief Controls the current audio volume level. |
bcostm | 6:e1d9da7fe856 | 336 | * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for |
bcostm | 6:e1d9da7fe856 | 337 | * Mute and 100 for Max volume level). |
bcostm | 6:e1d9da7fe856 | 338 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 339 | */ |
bcostm | 6:e1d9da7fe856 | 340 | uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume) |
bcostm | 6:e1d9da7fe856 | 341 | { |
bcostm | 6:e1d9da7fe856 | 342 | /* Call the codec volume control function with converted volume value */ |
bcostm | 6:e1d9da7fe856 | 343 | if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) |
bcostm | 6:e1d9da7fe856 | 344 | { |
bcostm | 6:e1d9da7fe856 | 345 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 346 | } |
bcostm | 6:e1d9da7fe856 | 347 | else |
bcostm | 6:e1d9da7fe856 | 348 | { |
bcostm | 6:e1d9da7fe856 | 349 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 350 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 351 | } |
bcostm | 6:e1d9da7fe856 | 352 | } |
bcostm | 6:e1d9da7fe856 | 353 | |
bcostm | 6:e1d9da7fe856 | 354 | /** |
bcostm | 6:e1d9da7fe856 | 355 | * @brief Enables or disables the MUTE mode by software |
bcostm | 6:e1d9da7fe856 | 356 | * @param Cmd: Could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to |
bcostm | 6:e1d9da7fe856 | 357 | * unmute the codec and restore previous volume level. |
bcostm | 6:e1d9da7fe856 | 358 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 359 | */ |
bcostm | 6:e1d9da7fe856 | 360 | uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd) |
bcostm | 6:e1d9da7fe856 | 361 | { |
bcostm | 6:e1d9da7fe856 | 362 | /* Call the Codec Mute function */ |
bcostm | 6:e1d9da7fe856 | 363 | if(audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) |
bcostm | 6:e1d9da7fe856 | 364 | { |
bcostm | 6:e1d9da7fe856 | 365 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 366 | } |
bcostm | 6:e1d9da7fe856 | 367 | else |
bcostm | 6:e1d9da7fe856 | 368 | { |
bcostm | 6:e1d9da7fe856 | 369 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 370 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 371 | } |
bcostm | 6:e1d9da7fe856 | 372 | } |
bcostm | 6:e1d9da7fe856 | 373 | |
bcostm | 6:e1d9da7fe856 | 374 | /** |
bcostm | 6:e1d9da7fe856 | 375 | * @brief Switch dynamically (while audio file is played) the output target |
bcostm | 6:e1d9da7fe856 | 376 | * (speaker or headphone). |
bcostm | 6:e1d9da7fe856 | 377 | * @param Output: The audio output target: OUTPUT_DEVICE_SPEAKER, |
bcostm | 6:e1d9da7fe856 | 378 | * OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH |
bcostm | 6:e1d9da7fe856 | 379 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 380 | */ |
bcostm | 6:e1d9da7fe856 | 381 | uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output) |
bcostm | 6:e1d9da7fe856 | 382 | { |
bcostm | 6:e1d9da7fe856 | 383 | /* Call the Codec output device function */ |
bcostm | 6:e1d9da7fe856 | 384 | if(audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) |
bcostm | 6:e1d9da7fe856 | 385 | { |
bcostm | 6:e1d9da7fe856 | 386 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 387 | } |
bcostm | 6:e1d9da7fe856 | 388 | else |
bcostm | 6:e1d9da7fe856 | 389 | { |
bcostm | 6:e1d9da7fe856 | 390 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 391 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 392 | } |
bcostm | 6:e1d9da7fe856 | 393 | } |
bcostm | 6:e1d9da7fe856 | 394 | |
bcostm | 6:e1d9da7fe856 | 395 | /** |
bcostm | 6:e1d9da7fe856 | 396 | * @brief Updates the audio frequency. |
bcostm | 6:e1d9da7fe856 | 397 | * @param AudioFreq: Audio frequency used to play the audio stream. |
bcostm | 6:e1d9da7fe856 | 398 | * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the |
bcostm | 6:e1d9da7fe856 | 399 | * audio frequency. |
bcostm | 6:e1d9da7fe856 | 400 | * @retval None |
bcostm | 6:e1d9da7fe856 | 401 | */ |
bcostm | 6:e1d9da7fe856 | 402 | void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 403 | { |
bcostm | 6:e1d9da7fe856 | 404 | /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */ |
bcostm | 6:e1d9da7fe856 | 405 | BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL); |
bcostm | 6:e1d9da7fe856 | 406 | |
bcostm | 6:e1d9da7fe856 | 407 | /* Disable SAI peripheral to allow access to SAI internal registers */ |
bcostm | 6:e1d9da7fe856 | 408 | __HAL_SAI_DISABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 409 | |
bcostm | 6:e1d9da7fe856 | 410 | /* Update the SAI audio frequency configuration */ |
bcostm | 6:e1d9da7fe856 | 411 | haudio_out_sai.Init.AudioFrequency = AudioFreq; |
bcostm | 6:e1d9da7fe856 | 412 | HAL_SAI_Init(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 413 | |
bcostm | 6:e1d9da7fe856 | 414 | /* Enable SAI peripheral to generate MCLK */ |
bcostm | 6:e1d9da7fe856 | 415 | __HAL_SAI_ENABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 416 | } |
bcostm | 6:e1d9da7fe856 | 417 | |
bcostm | 6:e1d9da7fe856 | 418 | /** |
bcostm | 6:e1d9da7fe856 | 419 | * @brief Updates the Audio frame slot configuration. |
bcostm | 6:e1d9da7fe856 | 420 | * @param AudioFrameSlot: specifies the audio Frame slot |
bcostm | 6:e1d9da7fe856 | 421 | * This parameter can be one of the following values |
bcostm | 6:e1d9da7fe856 | 422 | * @arg CODEC_AUDIOFRAME_SLOT_0123 |
bcostm | 6:e1d9da7fe856 | 423 | * @arg CODEC_AUDIOFRAME_SLOT_02 |
bcostm | 6:e1d9da7fe856 | 424 | * @arg CODEC_AUDIOFRAME_SLOT_13 |
bcostm | 6:e1d9da7fe856 | 425 | * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the |
bcostm | 6:e1d9da7fe856 | 426 | * audio frame slot. |
bcostm | 6:e1d9da7fe856 | 427 | * @retval None |
bcostm | 6:e1d9da7fe856 | 428 | */ |
bcostm | 6:e1d9da7fe856 | 429 | void BSP_AUDIO_OUT_SetAudioFrameSlot(uint32_t AudioFrameSlot) |
bcostm | 6:e1d9da7fe856 | 430 | { |
bcostm | 6:e1d9da7fe856 | 431 | /* Disable SAI peripheral to allow access to SAI internal registers */ |
bcostm | 6:e1d9da7fe856 | 432 | __HAL_SAI_DISABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 433 | |
bcostm | 6:e1d9da7fe856 | 434 | /* Update the SAI audio frame slot configuration */ |
bcostm | 6:e1d9da7fe856 | 435 | haudio_out_sai.SlotInit.SlotActive = AudioFrameSlot; |
bcostm | 6:e1d9da7fe856 | 436 | HAL_SAI_Init(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 437 | |
bcostm | 6:e1d9da7fe856 | 438 | /* Enable SAI peripheral to generate MCLK */ |
bcostm | 6:e1d9da7fe856 | 439 | __HAL_SAI_ENABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 440 | } |
bcostm | 6:e1d9da7fe856 | 441 | |
bcostm | 6:e1d9da7fe856 | 442 | /** |
bcostm | 6:e1d9da7fe856 | 443 | * @brief Deinit the audio peripherals. |
bcostm | 6:e1d9da7fe856 | 444 | * @retval None |
bcostm | 6:e1d9da7fe856 | 445 | */ |
bcostm | 6:e1d9da7fe856 | 446 | void BSP_AUDIO_OUT_DeInit(void) |
bcostm | 6:e1d9da7fe856 | 447 | { |
bcostm | 6:e1d9da7fe856 | 448 | SAIx_Out_DeInit(); |
bcostm | 6:e1d9da7fe856 | 449 | /* DeInit the SAI MSP : this __weak function can be rewritten by the application */ |
bcostm | 6:e1d9da7fe856 | 450 | BSP_AUDIO_OUT_MspDeInit(&haudio_out_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 451 | } |
bcostm | 6:e1d9da7fe856 | 452 | |
bcostm | 6:e1d9da7fe856 | 453 | /** |
bcostm | 6:e1d9da7fe856 | 454 | * @brief Tx Transfer completed callbacks. |
bcostm | 6:e1d9da7fe856 | 455 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 456 | * @retval None |
bcostm | 6:e1d9da7fe856 | 457 | */ |
bcostm | 6:e1d9da7fe856 | 458 | void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai) |
bcostm | 6:e1d9da7fe856 | 459 | { |
bcostm | 6:e1d9da7fe856 | 460 | /* Manage the remaining file size and new address offset: This function |
bcostm | 6:e1d9da7fe856 | 461 | should be coded by user (its prototype is already declared in stm32746g_discovery_audio.h) */ |
bcostm | 6:e1d9da7fe856 | 462 | BSP_AUDIO_OUT_TransferComplete_CallBack(); |
bcostm | 6:e1d9da7fe856 | 463 | } |
bcostm | 6:e1d9da7fe856 | 464 | |
bcostm | 6:e1d9da7fe856 | 465 | /** |
bcostm | 6:e1d9da7fe856 | 466 | * @brief Tx Half Transfer completed callbacks. |
bcostm | 6:e1d9da7fe856 | 467 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 468 | * @retval None |
bcostm | 6:e1d9da7fe856 | 469 | */ |
bcostm | 6:e1d9da7fe856 | 470 | void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai) |
bcostm | 6:e1d9da7fe856 | 471 | { |
bcostm | 6:e1d9da7fe856 | 472 | /* Manage the remaining file size and new address offset: This function |
bcostm | 6:e1d9da7fe856 | 473 | should be coded by user (its prototype is already declared in stm32746g_discovery_audio.h) */ |
bcostm | 6:e1d9da7fe856 | 474 | BSP_AUDIO_OUT_HalfTransfer_CallBack(); |
bcostm | 6:e1d9da7fe856 | 475 | } |
bcostm | 6:e1d9da7fe856 | 476 | |
bcostm | 6:e1d9da7fe856 | 477 | /** |
bcostm | 6:e1d9da7fe856 | 478 | * @brief SAI error callbacks. |
bcostm | 6:e1d9da7fe856 | 479 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 480 | * @retval None |
bcostm | 6:e1d9da7fe856 | 481 | */ |
bcostm | 6:e1d9da7fe856 | 482 | void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai) |
bcostm | 6:e1d9da7fe856 | 483 | { |
bcostm | 6:e1d9da7fe856 | 484 | HAL_SAI_StateTypeDef audio_out_state; |
bcostm | 6:e1d9da7fe856 | 485 | HAL_SAI_StateTypeDef audio_in_state; |
bcostm | 6:e1d9da7fe856 | 486 | |
bcostm | 6:e1d9da7fe856 | 487 | audio_out_state = HAL_SAI_GetState(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 488 | audio_in_state = HAL_SAI_GetState(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 489 | |
bcostm | 6:e1d9da7fe856 | 490 | /* Determines if it is an audio out or audio in error */ |
bcostm | 6:e1d9da7fe856 | 491 | if ((audio_out_state == HAL_SAI_STATE_BUSY) || (audio_out_state == HAL_SAI_STATE_BUSY_TX)) |
bcostm | 6:e1d9da7fe856 | 492 | { |
bcostm | 6:e1d9da7fe856 | 493 | BSP_AUDIO_OUT_Error_CallBack(); |
bcostm | 6:e1d9da7fe856 | 494 | } |
bcostm | 6:e1d9da7fe856 | 495 | |
bcostm | 6:e1d9da7fe856 | 496 | if ((audio_in_state == HAL_SAI_STATE_BUSY) || (audio_in_state == HAL_SAI_STATE_BUSY_RX)) |
bcostm | 6:e1d9da7fe856 | 497 | { |
bcostm | 6:e1d9da7fe856 | 498 | BSP_AUDIO_IN_Error_CallBack(); |
bcostm | 6:e1d9da7fe856 | 499 | } |
bcostm | 6:e1d9da7fe856 | 500 | } |
bcostm | 6:e1d9da7fe856 | 501 | |
bcostm | 6:e1d9da7fe856 | 502 | /** |
bcostm | 6:e1d9da7fe856 | 503 | * @brief Manages the DMA full Transfer complete event. |
bcostm | 6:e1d9da7fe856 | 504 | * @retval None |
bcostm | 6:e1d9da7fe856 | 505 | */ |
bcostm | 6:e1d9da7fe856 | 506 | __weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 507 | { |
bcostm | 6:e1d9da7fe856 | 508 | } |
bcostm | 6:e1d9da7fe856 | 509 | |
bcostm | 6:e1d9da7fe856 | 510 | /** |
bcostm | 6:e1d9da7fe856 | 511 | * @brief Manages the DMA Half Transfer complete event. |
bcostm | 6:e1d9da7fe856 | 512 | * @retval None |
bcostm | 6:e1d9da7fe856 | 513 | */ |
bcostm | 6:e1d9da7fe856 | 514 | __weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 515 | { |
bcostm | 6:e1d9da7fe856 | 516 | } |
bcostm | 6:e1d9da7fe856 | 517 | |
bcostm | 6:e1d9da7fe856 | 518 | /** |
bcostm | 6:e1d9da7fe856 | 519 | * @brief Manages the DMA FIFO error event. |
bcostm | 6:e1d9da7fe856 | 520 | * @retval None |
bcostm | 6:e1d9da7fe856 | 521 | */ |
bcostm | 6:e1d9da7fe856 | 522 | __weak void BSP_AUDIO_OUT_Error_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 523 | { |
bcostm | 6:e1d9da7fe856 | 524 | } |
bcostm | 6:e1d9da7fe856 | 525 | |
bcostm | 6:e1d9da7fe856 | 526 | /** |
bcostm | 6:e1d9da7fe856 | 527 | * @brief Initializes BSP_AUDIO_OUT MSP. |
bcostm | 6:e1d9da7fe856 | 528 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 529 | * @param Params |
bcostm | 6:e1d9da7fe856 | 530 | * @retval None |
bcostm | 6:e1d9da7fe856 | 531 | */ |
bcostm | 6:e1d9da7fe856 | 532 | __weak void BSP_AUDIO_OUT_MspInit(SAI_HandleTypeDef *hsai, void *Params) |
bcostm | 6:e1d9da7fe856 | 533 | { |
bcostm | 6:e1d9da7fe856 | 534 | static DMA_HandleTypeDef hdma_sai_tx; |
bcostm | 6:e1d9da7fe856 | 535 | GPIO_InitTypeDef gpio_init_structure; |
bcostm | 6:e1d9da7fe856 | 536 | |
bcostm | 6:e1d9da7fe856 | 537 | /* Enable SAI clock */ |
bcostm | 6:e1d9da7fe856 | 538 | AUDIO_OUT_SAIx_CLK_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 539 | |
bcostm | 6:e1d9da7fe856 | 540 | /* Enable GPIO clock */ |
bcostm | 6:e1d9da7fe856 | 541 | AUDIO_OUT_SAIx_MCLK_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 542 | AUDIO_OUT_SAIx_SCK_SD_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 543 | AUDIO_OUT_SAIx_FS_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 544 | /* CODEC_SAI pins configuration: FS, SCK, MCK and SD pins ------------------*/ |
bcostm | 6:e1d9da7fe856 | 545 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_FS_PIN; |
bcostm | 6:e1d9da7fe856 | 546 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
bcostm | 6:e1d9da7fe856 | 547 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 548 | gpio_init_structure.Speed = GPIO_SPEED_HIGH; |
bcostm | 6:e1d9da7fe856 | 549 | gpio_init_structure.Alternate = AUDIO_OUT_SAIx_FS_SD_MCLK_AF; |
bcostm | 6:e1d9da7fe856 | 550 | HAL_GPIO_Init(AUDIO_OUT_SAIx_FS_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 551 | |
bcostm | 6:e1d9da7fe856 | 552 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_SCK_PIN; |
bcostm | 6:e1d9da7fe856 | 553 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
bcostm | 6:e1d9da7fe856 | 554 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 555 | gpio_init_structure.Speed = GPIO_SPEED_HIGH; |
bcostm | 6:e1d9da7fe856 | 556 | gpio_init_structure.Alternate = AUDIO_OUT_SAIx_SCK_AF; |
bcostm | 6:e1d9da7fe856 | 557 | HAL_GPIO_Init(AUDIO_OUT_SAIx_SCK_SD_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 558 | |
bcostm | 6:e1d9da7fe856 | 559 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_SD_PIN; |
bcostm | 6:e1d9da7fe856 | 560 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
bcostm | 6:e1d9da7fe856 | 561 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 562 | gpio_init_structure.Speed = GPIO_SPEED_HIGH; |
bcostm | 6:e1d9da7fe856 | 563 | gpio_init_structure.Alternate = AUDIO_OUT_SAIx_FS_SD_MCLK_AF; |
bcostm | 6:e1d9da7fe856 | 564 | HAL_GPIO_Init(AUDIO_OUT_SAIx_SCK_SD_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 565 | |
bcostm | 6:e1d9da7fe856 | 566 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_MCLK_PIN; |
bcostm | 6:e1d9da7fe856 | 567 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
bcostm | 6:e1d9da7fe856 | 568 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 569 | gpio_init_structure.Speed = GPIO_SPEED_HIGH; |
bcostm | 6:e1d9da7fe856 | 570 | gpio_init_structure.Alternate = AUDIO_OUT_SAIx_FS_SD_MCLK_AF; |
bcostm | 6:e1d9da7fe856 | 571 | HAL_GPIO_Init(AUDIO_OUT_SAIx_MCLK_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 572 | |
bcostm | 6:e1d9da7fe856 | 573 | /* Enable the DMA clock */ |
bcostm | 6:e1d9da7fe856 | 574 | AUDIO_OUT_SAIx_DMAx_CLK_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 575 | |
bcostm | 6:e1d9da7fe856 | 576 | if(hsai->Instance == AUDIO_OUT_SAIx) |
bcostm | 6:e1d9da7fe856 | 577 | { |
bcostm | 6:e1d9da7fe856 | 578 | /* Configure the hdma_saiTx handle parameters */ |
bcostm | 6:e1d9da7fe856 | 579 | hdma_sai_tx.Init.Channel = AUDIO_OUT_SAIx_DMAx_CHANNEL; |
bcostm | 6:e1d9da7fe856 | 580 | hdma_sai_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; |
bcostm | 6:e1d9da7fe856 | 581 | hdma_sai_tx.Init.PeriphInc = DMA_PINC_DISABLE; |
bcostm | 6:e1d9da7fe856 | 582 | hdma_sai_tx.Init.MemInc = DMA_MINC_ENABLE; |
bcostm | 6:e1d9da7fe856 | 583 | hdma_sai_tx.Init.PeriphDataAlignment = AUDIO_OUT_SAIx_DMAx_PERIPH_DATA_SIZE; |
bcostm | 6:e1d9da7fe856 | 584 | hdma_sai_tx.Init.MemDataAlignment = AUDIO_OUT_SAIx_DMAx_MEM_DATA_SIZE; |
bcostm | 6:e1d9da7fe856 | 585 | hdma_sai_tx.Init.Mode = DMA_CIRCULAR; |
bcostm | 6:e1d9da7fe856 | 586 | hdma_sai_tx.Init.Priority = DMA_PRIORITY_HIGH; |
bcostm | 6:e1d9da7fe856 | 587 | hdma_sai_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE; |
bcostm | 6:e1d9da7fe856 | 588 | hdma_sai_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
bcostm | 6:e1d9da7fe856 | 589 | hdma_sai_tx.Init.MemBurst = DMA_MBURST_SINGLE; |
bcostm | 6:e1d9da7fe856 | 590 | hdma_sai_tx.Init.PeriphBurst = DMA_PBURST_SINGLE; |
bcostm | 6:e1d9da7fe856 | 591 | |
bcostm | 6:e1d9da7fe856 | 592 | hdma_sai_tx.Instance = AUDIO_OUT_SAIx_DMAx_STREAM; |
bcostm | 6:e1d9da7fe856 | 593 | |
bcostm | 6:e1d9da7fe856 | 594 | /* Associate the DMA handle */ |
bcostm | 6:e1d9da7fe856 | 595 | __HAL_LINKDMA(hsai, hdmatx, hdma_sai_tx); |
bcostm | 6:e1d9da7fe856 | 596 | |
bcostm | 6:e1d9da7fe856 | 597 | /* Deinitialize the Stream for new transfer */ |
bcostm | 6:e1d9da7fe856 | 598 | HAL_DMA_DeInit(&hdma_sai_tx); |
bcostm | 6:e1d9da7fe856 | 599 | |
bcostm | 6:e1d9da7fe856 | 600 | /* Configure the DMA Stream */ |
bcostm | 6:e1d9da7fe856 | 601 | HAL_DMA_Init(&hdma_sai_tx); |
bcostm | 6:e1d9da7fe856 | 602 | } |
bcostm | 6:e1d9da7fe856 | 603 | #if ( __MBED__ == 1) |
bcostm | 6:e1d9da7fe856 | 604 | // Enable interrupt |
bcostm | 6:e1d9da7fe856 | 605 | IRQn_Type irqn = (IRQn_Type)(AUDIO_OUT_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 606 | NVIC_ClearPendingIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 607 | NVIC_DisableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 608 | NVIC_SetPriority(irqn, AUDIO_OUT_IRQ_PREPRIO); |
bcostm | 6:e1d9da7fe856 | 609 | NVIC_SetVector(irqn, (uint32_t)AUDIO_OUT_SAIx_DMAx_IRQHandler); |
bcostm | 6:e1d9da7fe856 | 610 | NVIC_EnableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 611 | |
bcostm | 6:e1d9da7fe856 | 612 | #else |
bcostm | 6:e1d9da7fe856 | 613 | /* SAI DMA IRQ Channel configuration */ |
bcostm | 6:e1d9da7fe856 | 614 | HAL_NVIC_SetPriority(AUDIO_OUT_SAIx_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0); |
bcostm | 6:e1d9da7fe856 | 615 | HAL_NVIC_EnableIRQ(AUDIO_OUT_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 616 | #endif |
bcostm | 6:e1d9da7fe856 | 617 | } |
bcostm | 6:e1d9da7fe856 | 618 | |
bcostm | 6:e1d9da7fe856 | 619 | /** |
bcostm | 6:e1d9da7fe856 | 620 | * @brief Deinitializes SAI MSP. |
bcostm | 6:e1d9da7fe856 | 621 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 622 | * @param Params |
bcostm | 6:e1d9da7fe856 | 623 | * @retval None |
bcostm | 6:e1d9da7fe856 | 624 | */ |
bcostm | 6:e1d9da7fe856 | 625 | __weak void BSP_AUDIO_OUT_MspDeInit(SAI_HandleTypeDef *hsai, void *Params) |
bcostm | 6:e1d9da7fe856 | 626 | { |
bcostm | 6:e1d9da7fe856 | 627 | GPIO_InitTypeDef gpio_init_structure; |
bcostm | 6:e1d9da7fe856 | 628 | |
bcostm | 6:e1d9da7fe856 | 629 | /* SAI DMA IRQ Channel deactivation */ |
bcostm | 6:e1d9da7fe856 | 630 | HAL_NVIC_DisableIRQ(AUDIO_OUT_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 631 | |
bcostm | 6:e1d9da7fe856 | 632 | if(hsai->Instance == AUDIO_OUT_SAIx) |
bcostm | 6:e1d9da7fe856 | 633 | { |
bcostm | 6:e1d9da7fe856 | 634 | /* Deinitialize the DMA stream */ |
bcostm | 6:e1d9da7fe856 | 635 | HAL_DMA_DeInit(hsai->hdmatx); |
bcostm | 6:e1d9da7fe856 | 636 | } |
bcostm | 6:e1d9da7fe856 | 637 | |
bcostm | 6:e1d9da7fe856 | 638 | /* Disable SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 639 | __HAL_SAI_DISABLE(hsai); |
bcostm | 6:e1d9da7fe856 | 640 | |
bcostm | 6:e1d9da7fe856 | 641 | /* Deactives CODEC_SAI pins FS, SCK, MCK and SD by putting them in input mode */ |
bcostm | 6:e1d9da7fe856 | 642 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_FS_PIN; |
bcostm | 6:e1d9da7fe856 | 643 | HAL_GPIO_DeInit(AUDIO_OUT_SAIx_FS_GPIO_PORT, gpio_init_structure.Pin); |
bcostm | 6:e1d9da7fe856 | 644 | |
bcostm | 6:e1d9da7fe856 | 645 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_SCK_PIN; |
bcostm | 6:e1d9da7fe856 | 646 | HAL_GPIO_DeInit(AUDIO_OUT_SAIx_SCK_SD_GPIO_PORT, gpio_init_structure.Pin); |
bcostm | 6:e1d9da7fe856 | 647 | |
bcostm | 6:e1d9da7fe856 | 648 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_SD_PIN; |
bcostm | 6:e1d9da7fe856 | 649 | HAL_GPIO_DeInit(AUDIO_OUT_SAIx_SCK_SD_GPIO_PORT, gpio_init_structure.Pin); |
bcostm | 6:e1d9da7fe856 | 650 | |
bcostm | 6:e1d9da7fe856 | 651 | gpio_init_structure.Pin = AUDIO_OUT_SAIx_MCLK_PIN; |
bcostm | 6:e1d9da7fe856 | 652 | HAL_GPIO_DeInit(AUDIO_OUT_SAIx_MCLK_GPIO_PORT, gpio_init_structure.Pin); |
bcostm | 6:e1d9da7fe856 | 653 | |
bcostm | 6:e1d9da7fe856 | 654 | /* Disable SAI clock */ |
bcostm | 6:e1d9da7fe856 | 655 | AUDIO_OUT_SAIx_CLK_DISABLE(); |
bcostm | 6:e1d9da7fe856 | 656 | |
bcostm | 6:e1d9da7fe856 | 657 | /* GPIO pins clock and DMA clock can be shut down in the application |
bcostm | 6:e1d9da7fe856 | 658 | by surcharging this __weak function */ |
bcostm | 6:e1d9da7fe856 | 659 | } |
bcostm | 6:e1d9da7fe856 | 660 | |
bcostm | 6:e1d9da7fe856 | 661 | /** |
bcostm | 6:e1d9da7fe856 | 662 | * @brief Clock Config. |
bcostm | 6:e1d9da7fe856 | 663 | * @param hsai: might be required to set audio peripheral predivider if any. |
bcostm | 6:e1d9da7fe856 | 664 | * @param AudioFreq: Audio frequency used to play the audio stream. |
bcostm | 6:e1d9da7fe856 | 665 | * @param Params |
bcostm | 6:e1d9da7fe856 | 666 | * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency() |
bcostm | 6:e1d9da7fe856 | 667 | * Being __weak it can be overwritten by the application |
bcostm | 6:e1d9da7fe856 | 668 | * @retval None |
bcostm | 6:e1d9da7fe856 | 669 | */ |
bcostm | 6:e1d9da7fe856 | 670 | __weak void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t AudioFreq, void *Params) |
bcostm | 6:e1d9da7fe856 | 671 | { |
bcostm | 6:e1d9da7fe856 | 672 | RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct; |
bcostm | 6:e1d9da7fe856 | 673 | |
bcostm | 6:e1d9da7fe856 | 674 | HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct); |
bcostm | 6:e1d9da7fe856 | 675 | |
bcostm | 6:e1d9da7fe856 | 676 | /* Set the PLL configuration according to the audio frequency */ |
bcostm | 6:e1d9da7fe856 | 677 | if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K)) |
bcostm | 6:e1d9da7fe856 | 678 | { |
bcostm | 6:e1d9da7fe856 | 679 | /* Configure PLLI2S prescalers */ |
bcostm | 6:e1d9da7fe856 | 680 | /* PLLI2S_VCO: VCO_429M |
bcostm | 6:e1d9da7fe856 | 681 | I2S_CLK(first level) = PLLI2S_VCO/PLLI2SQ = 429/2 = 214.5 Mhz |
bcostm | 6:e1d9da7fe856 | 682 | I2S_CLK_x = I2S_CLK(first level)/PLLI2SDIVQ = 214.5/19 = 11.289 Mhz */ |
bcostm | 6:e1d9da7fe856 | 683 | rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI2; |
bcostm | 6:e1d9da7fe856 | 684 | rcc_ex_clk_init_struct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLI2S; |
bcostm | 6:e1d9da7fe856 | 685 | rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 429; |
bcostm | 6:e1d9da7fe856 | 686 | rcc_ex_clk_init_struct.PLLI2S.PLLI2SQ = 2; |
bcostm | 6:e1d9da7fe856 | 687 | rcc_ex_clk_init_struct.PLLI2SDivQ = 19; |
bcostm | 6:e1d9da7fe856 | 688 | |
bcostm | 6:e1d9da7fe856 | 689 | HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct); |
bcostm | 6:e1d9da7fe856 | 690 | |
bcostm | 6:e1d9da7fe856 | 691 | } |
bcostm | 6:e1d9da7fe856 | 692 | else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_48K), AUDIO_FREQUENCY_96K */ |
bcostm | 6:e1d9da7fe856 | 693 | { |
bcostm | 6:e1d9da7fe856 | 694 | /* I2S clock config |
bcostm | 6:e1d9da7fe856 | 695 | PLLI2S_VCO: VCO_344M |
bcostm | 6:e1d9da7fe856 | 696 | I2S_CLK(first level) = PLLI2S_VCO/PLLI2SQ = 344/7 = 49.142 Mhz |
bcostm | 6:e1d9da7fe856 | 697 | I2S_CLK_x = I2S_CLK(first level)/PLLI2SDIVQ = 49.142/1 = 49.142 Mhz */ |
bcostm | 6:e1d9da7fe856 | 698 | rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI2; |
bcostm | 6:e1d9da7fe856 | 699 | rcc_ex_clk_init_struct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLI2S; |
bcostm | 6:e1d9da7fe856 | 700 | rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344; |
bcostm | 6:e1d9da7fe856 | 701 | rcc_ex_clk_init_struct.PLLI2S.PLLI2SQ = 7; |
bcostm | 6:e1d9da7fe856 | 702 | rcc_ex_clk_init_struct.PLLI2SDivQ = 1; |
bcostm | 6:e1d9da7fe856 | 703 | |
bcostm | 6:e1d9da7fe856 | 704 | HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct); |
bcostm | 6:e1d9da7fe856 | 705 | } |
bcostm | 6:e1d9da7fe856 | 706 | } |
bcostm | 6:e1d9da7fe856 | 707 | |
bcostm | 6:e1d9da7fe856 | 708 | /******************************************************************************* |
bcostm | 6:e1d9da7fe856 | 709 | Static Functions |
bcostm | 6:e1d9da7fe856 | 710 | *******************************************************************************/ |
bcostm | 6:e1d9da7fe856 | 711 | |
bcostm | 6:e1d9da7fe856 | 712 | /** |
bcostm | 6:e1d9da7fe856 | 713 | * @brief Initializes the output Audio Codec audio interface (SAI). |
bcostm | 6:e1d9da7fe856 | 714 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
bcostm | 6:e1d9da7fe856 | 715 | * @note The default SlotActive configuration is set to CODEC_AUDIOFRAME_SLOT_0123 |
bcostm | 6:e1d9da7fe856 | 716 | * and user can update this configuration using |
bcostm | 6:e1d9da7fe856 | 717 | * @retval None |
bcostm | 6:e1d9da7fe856 | 718 | */ |
bcostm | 6:e1d9da7fe856 | 719 | static void SAIx_Out_Init(uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 720 | { |
bcostm | 6:e1d9da7fe856 | 721 | /* Initialize the haudio_out_sai Instance parameter */ |
bcostm | 6:e1d9da7fe856 | 722 | haudio_out_sai.Instance = AUDIO_OUT_SAIx; |
bcostm | 6:e1d9da7fe856 | 723 | |
bcostm | 6:e1d9da7fe856 | 724 | /* Disable SAI peripheral to allow access to SAI internal registers */ |
bcostm | 6:e1d9da7fe856 | 725 | __HAL_SAI_DISABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 726 | |
bcostm | 6:e1d9da7fe856 | 727 | /* Configure SAI_Block_x |
bcostm | 6:e1d9da7fe856 | 728 | LSBFirst: Disabled |
bcostm | 6:e1d9da7fe856 | 729 | DataSize: 16 */ |
bcostm | 6:e1d9da7fe856 | 730 | haudio_out_sai.Init.AudioFrequency = AudioFreq; |
bcostm | 6:e1d9da7fe856 | 731 | haudio_out_sai.Init.AudioMode = SAI_MODEMASTER_TX; |
bcostm | 6:e1d9da7fe856 | 732 | haudio_out_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLED; |
bcostm | 6:e1d9da7fe856 | 733 | haudio_out_sai.Init.Protocol = SAI_FREE_PROTOCOL; |
bcostm | 6:e1d9da7fe856 | 734 | haudio_out_sai.Init.DataSize = SAI_DATASIZE_16; |
bcostm | 6:e1d9da7fe856 | 735 | haudio_out_sai.Init.FirstBit = SAI_FIRSTBIT_MSB; |
bcostm | 6:e1d9da7fe856 | 736 | haudio_out_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE; |
bcostm | 6:e1d9da7fe856 | 737 | haudio_out_sai.Init.Synchro = SAI_ASYNCHRONOUS; |
bcostm | 6:e1d9da7fe856 | 738 | haudio_out_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLED; |
bcostm | 6:e1d9da7fe856 | 739 | haudio_out_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF; |
bcostm | 6:e1d9da7fe856 | 740 | |
bcostm | 6:e1d9da7fe856 | 741 | /* Configure SAI_Block_x Frame |
bcostm | 6:e1d9da7fe856 | 742 | Frame Length: 64 |
bcostm | 6:e1d9da7fe856 | 743 | Frame active Length: 32 |
bcostm | 6:e1d9da7fe856 | 744 | FS Definition: Start frame + Channel Side identification |
bcostm | 6:e1d9da7fe856 | 745 | FS Polarity: FS active Low |
bcostm | 6:e1d9da7fe856 | 746 | FS Offset: FS asserted one bit before the first bit of slot 0 */ |
bcostm | 6:e1d9da7fe856 | 747 | haudio_out_sai.FrameInit.FrameLength = 64; |
bcostm | 6:e1d9da7fe856 | 748 | haudio_out_sai.FrameInit.ActiveFrameLength = 32; |
bcostm | 6:e1d9da7fe856 | 749 | haudio_out_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION; |
bcostm | 6:e1d9da7fe856 | 750 | haudio_out_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW; |
bcostm | 6:e1d9da7fe856 | 751 | haudio_out_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT; |
bcostm | 6:e1d9da7fe856 | 752 | |
bcostm | 6:e1d9da7fe856 | 753 | /* Configure SAI Block_x Slot |
bcostm | 6:e1d9da7fe856 | 754 | Slot First Bit Offset: 0 |
bcostm | 6:e1d9da7fe856 | 755 | Slot Size : 16 |
bcostm | 6:e1d9da7fe856 | 756 | Slot Number: 4 |
bcostm | 6:e1d9da7fe856 | 757 | Slot Active: All slot actives */ |
bcostm | 6:e1d9da7fe856 | 758 | haudio_out_sai.SlotInit.FirstBitOffset = 0; |
bcostm | 6:e1d9da7fe856 | 759 | haudio_out_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE; |
bcostm | 6:e1d9da7fe856 | 760 | haudio_out_sai.SlotInit.SlotNumber = 4; |
bcostm | 6:e1d9da7fe856 | 761 | haudio_out_sai.SlotInit.SlotActive = CODEC_AUDIOFRAME_SLOT_0123; |
bcostm | 6:e1d9da7fe856 | 762 | |
bcostm | 6:e1d9da7fe856 | 763 | HAL_SAI_Init(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 764 | |
bcostm | 6:e1d9da7fe856 | 765 | /* Enable SAI peripheral to generate MCLK */ |
bcostm | 6:e1d9da7fe856 | 766 | __HAL_SAI_ENABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 767 | } |
bcostm | 6:e1d9da7fe856 | 768 | |
bcostm | 6:e1d9da7fe856 | 769 | |
bcostm | 6:e1d9da7fe856 | 770 | |
bcostm | 6:e1d9da7fe856 | 771 | /** |
bcostm | 6:e1d9da7fe856 | 772 | * @brief Deinitializes the output Audio Codec audio interface (SAI). |
bcostm | 6:e1d9da7fe856 | 773 | * @retval None |
bcostm | 6:e1d9da7fe856 | 774 | */ |
bcostm | 6:e1d9da7fe856 | 775 | static void SAIx_Out_DeInit(void) |
bcostm | 6:e1d9da7fe856 | 776 | { |
bcostm | 6:e1d9da7fe856 | 777 | /* Initialize the haudio_out_sai Instance parameter */ |
bcostm | 6:e1d9da7fe856 | 778 | haudio_out_sai.Instance = AUDIO_OUT_SAIx; |
bcostm | 6:e1d9da7fe856 | 779 | |
bcostm | 6:e1d9da7fe856 | 780 | /* Disable SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 781 | __HAL_SAI_DISABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 782 | |
bcostm | 6:e1d9da7fe856 | 783 | HAL_SAI_DeInit(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 784 | } |
bcostm | 6:e1d9da7fe856 | 785 | |
bcostm | 6:e1d9da7fe856 | 786 | /** |
bcostm | 6:e1d9da7fe856 | 787 | * @} |
bcostm | 6:e1d9da7fe856 | 788 | */ |
bcostm | 6:e1d9da7fe856 | 789 | |
bcostm | 6:e1d9da7fe856 | 790 | /** @defgroup STM32746G_DISCOVERY_AUDIO_Out_Private_Functions STM32746G_DISCOVERY_AUDIO Out Private Functions |
bcostm | 6:e1d9da7fe856 | 791 | * @{ |
bcostm | 6:e1d9da7fe856 | 792 | */ |
bcostm | 6:e1d9da7fe856 | 793 | |
bcostm | 6:e1d9da7fe856 | 794 | /** |
bcostm | 6:e1d9da7fe856 | 795 | * @brief Initializes wave recording. |
bcostm | 6:e1d9da7fe856 | 796 | * @param InputDevice: INPUT_DEVICE_DIGITAL_MICROPHONE_2 or INPUT_DEVICE_INPUT_LINE_1 |
bcostm | 6:e1d9da7fe856 | 797 | * @param Volume: Initial volume level (in range 0(Mute)..80(+0dB)..100(+17.625dB)) |
bcostm | 6:e1d9da7fe856 | 798 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
bcostm | 6:e1d9da7fe856 | 799 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 800 | */ |
bcostm | 6:e1d9da7fe856 | 801 | uint8_t BSP_AUDIO_IN_Init(uint16_t InputDevice, uint8_t Volume, uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 802 | { |
bcostm | 6:e1d9da7fe856 | 803 | uint8_t ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 804 | uint32_t deviceid = 0x00; |
bcostm | 6:e1d9da7fe856 | 805 | uint32_t slot_active; |
bcostm | 6:e1d9da7fe856 | 806 | |
bcostm | 6:e1d9da7fe856 | 807 | if ((InputDevice != INPUT_DEVICE_INPUT_LINE_1) && /* Only INPUT_LINE_1 and MICROPHONE_2 inputs supported */ |
bcostm | 6:e1d9da7fe856 | 808 | (InputDevice != INPUT_DEVICE_DIGITAL_MICROPHONE_2)) |
bcostm | 6:e1d9da7fe856 | 809 | { |
bcostm | 6:e1d9da7fe856 | 810 | ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 811 | } |
bcostm | 6:e1d9da7fe856 | 812 | else |
bcostm | 6:e1d9da7fe856 | 813 | { |
bcostm | 6:e1d9da7fe856 | 814 | /* Disable SAI */ |
bcostm | 6:e1d9da7fe856 | 815 | SAIx_In_DeInit(); |
bcostm | 6:e1d9da7fe856 | 816 | |
bcostm | 6:e1d9da7fe856 | 817 | /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */ |
bcostm | 6:e1d9da7fe856 | 818 | BSP_AUDIO_OUT_ClockConfig(&haudio_in_sai, AudioFreq, NULL); /* Clock config is shared between AUDIO IN and OUT */ |
bcostm | 6:e1d9da7fe856 | 819 | |
bcostm | 6:e1d9da7fe856 | 820 | /* SAI data transfer preparation: |
bcostm | 6:e1d9da7fe856 | 821 | Prepare the Media to be used for the audio transfer from SAI peripheral to memory */ |
bcostm | 6:e1d9da7fe856 | 822 | haudio_in_sai.Instance = AUDIO_IN_SAIx; |
bcostm | 6:e1d9da7fe856 | 823 | if(HAL_SAI_GetState(&haudio_in_sai) == HAL_SAI_STATE_RESET) |
bcostm | 6:e1d9da7fe856 | 824 | { |
bcostm | 6:e1d9da7fe856 | 825 | /* Init the SAI MSP: this __weak function can be redefined by the application*/ |
bcostm | 6:e1d9da7fe856 | 826 | BSP_AUDIO_OUT_MspInit(&haudio_in_sai, NULL); /* Initialize GPIOs for SAI2 block A Master signals */ |
bcostm | 6:e1d9da7fe856 | 827 | BSP_AUDIO_IN_MspInit(&haudio_in_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 828 | } |
bcostm | 6:e1d9da7fe856 | 829 | |
bcostm | 6:e1d9da7fe856 | 830 | /* Configure SAI in master RX mode : |
bcostm | 6:e1d9da7fe856 | 831 | * - SAI2_block_A in master RX mode |
bcostm | 6:e1d9da7fe856 | 832 | * - SAI2_block_B in slave RX mode synchronous from SAI2_block_A |
bcostm | 6:e1d9da7fe856 | 833 | */ |
bcostm | 6:e1d9da7fe856 | 834 | if (InputDevice == INPUT_DEVICE_DIGITAL_MICROPHONE_2) |
bcostm | 6:e1d9da7fe856 | 835 | { |
bcostm | 6:e1d9da7fe856 | 836 | slot_active = CODEC_AUDIOFRAME_SLOT_13; |
bcostm | 6:e1d9da7fe856 | 837 | } |
bcostm | 6:e1d9da7fe856 | 838 | else |
bcostm | 6:e1d9da7fe856 | 839 | { |
bcostm | 6:e1d9da7fe856 | 840 | slot_active = CODEC_AUDIOFRAME_SLOT_02; |
bcostm | 6:e1d9da7fe856 | 841 | } |
bcostm | 6:e1d9da7fe856 | 842 | SAIx_In_Init(SAI_MODEMASTER_RX, slot_active, AudioFreq); |
bcostm | 6:e1d9da7fe856 | 843 | |
bcostm | 6:e1d9da7fe856 | 844 | /* wm8994 codec initialization */ |
bcostm | 6:e1d9da7fe856 | 845 | deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 846 | |
bcostm | 6:e1d9da7fe856 | 847 | if((deviceid) == WM8994_ID) |
bcostm | 6:e1d9da7fe856 | 848 | { |
bcostm | 6:e1d9da7fe856 | 849 | /* Reset the Codec Registers */ |
bcostm | 6:e1d9da7fe856 | 850 | wm8994_drv.Reset(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 851 | /* Initialize the audio driver structure */ |
bcostm | 6:e1d9da7fe856 | 852 | audio_drv = &wm8994_drv; |
bcostm | 6:e1d9da7fe856 | 853 | ret = AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 854 | } |
bcostm | 6:e1d9da7fe856 | 855 | else |
bcostm | 6:e1d9da7fe856 | 856 | { |
bcostm | 6:e1d9da7fe856 | 857 | ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 858 | } |
bcostm | 6:e1d9da7fe856 | 859 | |
bcostm | 6:e1d9da7fe856 | 860 | if(ret == AUDIO_OK) |
bcostm | 6:e1d9da7fe856 | 861 | { |
bcostm | 6:e1d9da7fe856 | 862 | /* Initialize the codec internal registers */ |
bcostm | 6:e1d9da7fe856 | 863 | audio_drv->Init(AUDIO_I2C_ADDRESS, InputDevice, Volume, AudioFreq); |
bcostm | 6:e1d9da7fe856 | 864 | } |
bcostm | 6:e1d9da7fe856 | 865 | } |
bcostm | 6:e1d9da7fe856 | 866 | return ret; |
bcostm | 6:e1d9da7fe856 | 867 | } |
bcostm | 6:e1d9da7fe856 | 868 | |
bcostm | 6:e1d9da7fe856 | 869 | /** |
bcostm | 6:e1d9da7fe856 | 870 | * @brief Initializes wave recording and playback in parallel. |
bcostm | 6:e1d9da7fe856 | 871 | * @param InputDevice: INPUT_DEVICE_DIGITAL_MICROPHONE_2 |
bcostm | 6:e1d9da7fe856 | 872 | * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, |
bcostm | 6:e1d9da7fe856 | 873 | * or OUTPUT_DEVICE_BOTH. |
bcostm | 6:e1d9da7fe856 | 874 | * @param Volume: Initial volume level (in range 0(Mute)..80(+0dB)..100(+17.625dB)) |
bcostm | 6:e1d9da7fe856 | 875 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
bcostm | 6:e1d9da7fe856 | 876 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 877 | */ |
bcostm | 6:e1d9da7fe856 | 878 | uint8_t BSP_AUDIO_IN_OUT_Init(uint16_t InputDevice, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 879 | { |
bcostm | 6:e1d9da7fe856 | 880 | uint8_t ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 881 | uint32_t deviceid = 0x00; |
bcostm | 6:e1d9da7fe856 | 882 | uint32_t slot_active; |
bcostm | 6:e1d9da7fe856 | 883 | |
bcostm | 6:e1d9da7fe856 | 884 | if (InputDevice != INPUT_DEVICE_DIGITAL_MICROPHONE_2) /* Only MICROPHONE_2 input supported */ |
bcostm | 6:e1d9da7fe856 | 885 | { |
bcostm | 6:e1d9da7fe856 | 886 | ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 887 | } |
bcostm | 6:e1d9da7fe856 | 888 | else |
bcostm | 6:e1d9da7fe856 | 889 | { |
bcostm | 6:e1d9da7fe856 | 890 | /* Disable SAI */ |
bcostm | 6:e1d9da7fe856 | 891 | SAIx_In_DeInit(); |
bcostm | 6:e1d9da7fe856 | 892 | SAIx_Out_DeInit(); |
bcostm | 6:e1d9da7fe856 | 893 | |
bcostm | 6:e1d9da7fe856 | 894 | /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */ |
bcostm | 6:e1d9da7fe856 | 895 | BSP_AUDIO_OUT_ClockConfig(&haudio_in_sai, AudioFreq, NULL); /* Clock config is shared between AUDIO IN and OUT */ |
bcostm | 6:e1d9da7fe856 | 896 | |
bcostm | 6:e1d9da7fe856 | 897 | /* SAI data transfer preparation: |
bcostm | 6:e1d9da7fe856 | 898 | Prepare the Media to be used for the audio transfer from SAI peripheral to memory */ |
bcostm | 6:e1d9da7fe856 | 899 | haudio_in_sai.Instance = AUDIO_IN_SAIx; |
bcostm | 6:e1d9da7fe856 | 900 | if(HAL_SAI_GetState(&haudio_in_sai) == HAL_SAI_STATE_RESET) |
bcostm | 6:e1d9da7fe856 | 901 | { |
bcostm | 6:e1d9da7fe856 | 902 | /* Init the SAI MSP: this __weak function can be redefined by the application*/ |
bcostm | 6:e1d9da7fe856 | 903 | BSP_AUDIO_IN_MspInit(&haudio_in_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 904 | } |
bcostm | 6:e1d9da7fe856 | 905 | |
bcostm | 6:e1d9da7fe856 | 906 | /* SAI data transfer preparation: |
bcostm | 6:e1d9da7fe856 | 907 | Prepare the Media to be used for the audio transfer from memory to SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 908 | haudio_out_sai.Instance = AUDIO_OUT_SAIx; |
bcostm | 6:e1d9da7fe856 | 909 | if(HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET) |
bcostm | 6:e1d9da7fe856 | 910 | { |
bcostm | 6:e1d9da7fe856 | 911 | /* Init the SAI MSP: this __weak function can be redefined by the application*/ |
bcostm | 6:e1d9da7fe856 | 912 | BSP_AUDIO_OUT_MspInit(&haudio_out_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 913 | } |
bcostm | 6:e1d9da7fe856 | 914 | |
bcostm | 6:e1d9da7fe856 | 915 | /* Configure SAI in master mode : |
bcostm | 6:e1d9da7fe856 | 916 | * - SAI2_block_A in master TX mode |
bcostm | 6:e1d9da7fe856 | 917 | * - SAI2_block_B in slave RX mode synchronous from SAI2_block_A |
bcostm | 6:e1d9da7fe856 | 918 | */ |
bcostm | 6:e1d9da7fe856 | 919 | if (InputDevice == INPUT_DEVICE_DIGITAL_MICROPHONE_2) |
bcostm | 6:e1d9da7fe856 | 920 | { |
bcostm | 6:e1d9da7fe856 | 921 | slot_active = CODEC_AUDIOFRAME_SLOT_13; |
bcostm | 6:e1d9da7fe856 | 922 | } |
bcostm | 6:e1d9da7fe856 | 923 | else |
bcostm | 6:e1d9da7fe856 | 924 | { |
bcostm | 6:e1d9da7fe856 | 925 | slot_active = CODEC_AUDIOFRAME_SLOT_02; |
bcostm | 6:e1d9da7fe856 | 926 | } |
bcostm | 6:e1d9da7fe856 | 927 | SAIx_In_Init(SAI_MODEMASTER_TX, slot_active, AudioFreq); |
bcostm | 6:e1d9da7fe856 | 928 | |
bcostm | 6:e1d9da7fe856 | 929 | /* wm8994 codec initialization */ |
bcostm | 6:e1d9da7fe856 | 930 | deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 931 | |
bcostm | 6:e1d9da7fe856 | 932 | if((deviceid) == WM8994_ID) |
bcostm | 6:e1d9da7fe856 | 933 | { |
bcostm | 6:e1d9da7fe856 | 934 | /* Reset the Codec Registers */ |
bcostm | 6:e1d9da7fe856 | 935 | wm8994_drv.Reset(AUDIO_I2C_ADDRESS); |
bcostm | 6:e1d9da7fe856 | 936 | /* Initialize the audio driver structure */ |
bcostm | 6:e1d9da7fe856 | 937 | audio_drv = &wm8994_drv; |
bcostm | 6:e1d9da7fe856 | 938 | ret = AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 939 | } |
bcostm | 6:e1d9da7fe856 | 940 | else |
bcostm | 6:e1d9da7fe856 | 941 | { |
bcostm | 6:e1d9da7fe856 | 942 | ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 943 | } |
bcostm | 6:e1d9da7fe856 | 944 | |
bcostm | 6:e1d9da7fe856 | 945 | if(ret == AUDIO_OK) |
bcostm | 6:e1d9da7fe856 | 946 | { |
bcostm | 6:e1d9da7fe856 | 947 | /* Initialize the codec internal registers */ |
bcostm | 6:e1d9da7fe856 | 948 | audio_drv->Init(AUDIO_I2C_ADDRESS, InputDevice | OutputDevice, Volume, AudioFreq); |
bcostm | 6:e1d9da7fe856 | 949 | } |
bcostm | 6:e1d9da7fe856 | 950 | } |
bcostm | 6:e1d9da7fe856 | 951 | return ret; |
bcostm | 6:e1d9da7fe856 | 952 | } |
bcostm | 6:e1d9da7fe856 | 953 | |
bcostm | 6:e1d9da7fe856 | 954 | |
bcostm | 6:e1d9da7fe856 | 955 | /** |
bcostm | 6:e1d9da7fe856 | 956 | * @brief Starts audio recording. |
bcostm | 6:e1d9da7fe856 | 957 | * @param pbuf: Main buffer pointer for the recorded data storing |
bcostm | 6:e1d9da7fe856 | 958 | * @param size: size of the recorded buffer in number of elements (typically number of half-words) |
bcostm | 6:e1d9da7fe856 | 959 | * Be careful that it is not the same unit than BSP_AUDIO_OUT_Play function |
bcostm | 6:e1d9da7fe856 | 960 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 961 | */ |
bcostm | 6:e1d9da7fe856 | 962 | uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size) |
bcostm | 6:e1d9da7fe856 | 963 | { |
bcostm | 6:e1d9da7fe856 | 964 | uint32_t ret = AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 965 | |
bcostm | 6:e1d9da7fe856 | 966 | /* Start the process receive DMA */ |
bcostm | 6:e1d9da7fe856 | 967 | HAL_SAI_Receive_DMA(&haudio_in_sai, (uint8_t*)pbuf, size); |
bcostm | 6:e1d9da7fe856 | 968 | |
bcostm | 6:e1d9da7fe856 | 969 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 970 | ret = AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 971 | |
bcostm | 6:e1d9da7fe856 | 972 | return ret; |
bcostm | 6:e1d9da7fe856 | 973 | } |
bcostm | 6:e1d9da7fe856 | 974 | |
bcostm | 6:e1d9da7fe856 | 975 | /** |
bcostm | 6:e1d9da7fe856 | 976 | * @brief Stops audio recording. |
bcostm | 6:e1d9da7fe856 | 977 | * @param Option: could be one of the following parameters |
bcostm | 6:e1d9da7fe856 | 978 | * - CODEC_PDWN_SW: for software power off (by writing registers). |
bcostm | 6:e1d9da7fe856 | 979 | * Then no need to reconfigure the Codec after power on. |
bcostm | 6:e1d9da7fe856 | 980 | * - CODEC_PDWN_HW: completely shut down the codec (physically). |
bcostm | 6:e1d9da7fe856 | 981 | * Then need to reconfigure the Codec after power on. |
bcostm | 6:e1d9da7fe856 | 982 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 983 | */ |
bcostm | 6:e1d9da7fe856 | 984 | uint8_t BSP_AUDIO_IN_Stop(uint32_t Option) |
bcostm | 6:e1d9da7fe856 | 985 | { |
bcostm | 6:e1d9da7fe856 | 986 | /* Call the Media layer stop function */ |
bcostm | 6:e1d9da7fe856 | 987 | HAL_SAI_DMAStop(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 988 | |
bcostm | 6:e1d9da7fe856 | 989 | /* Call Audio Codec Stop function */ |
bcostm | 6:e1d9da7fe856 | 990 | if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) |
bcostm | 6:e1d9da7fe856 | 991 | { |
bcostm | 6:e1d9da7fe856 | 992 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 993 | } |
bcostm | 6:e1d9da7fe856 | 994 | else |
bcostm | 6:e1d9da7fe856 | 995 | { |
bcostm | 6:e1d9da7fe856 | 996 | if(Option == CODEC_PDWN_HW) |
bcostm | 6:e1d9da7fe856 | 997 | { |
bcostm | 6:e1d9da7fe856 | 998 | /* Wait at least 100us */ |
bcostm | 6:e1d9da7fe856 | 999 | HAL_Delay(1); |
bcostm | 6:e1d9da7fe856 | 1000 | } |
bcostm | 6:e1d9da7fe856 | 1001 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 1002 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 1003 | } |
bcostm | 6:e1d9da7fe856 | 1004 | } |
bcostm | 6:e1d9da7fe856 | 1005 | |
bcostm | 6:e1d9da7fe856 | 1006 | /** |
bcostm | 6:e1d9da7fe856 | 1007 | * @brief Pauses the audio file stream. |
bcostm | 6:e1d9da7fe856 | 1008 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 1009 | */ |
bcostm | 6:e1d9da7fe856 | 1010 | uint8_t BSP_AUDIO_IN_Pause(void) |
bcostm | 6:e1d9da7fe856 | 1011 | { |
bcostm | 6:e1d9da7fe856 | 1012 | /* Call the Media layer pause function */ |
bcostm | 6:e1d9da7fe856 | 1013 | HAL_SAI_DMAPause(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1014 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 1015 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 1016 | } |
bcostm | 6:e1d9da7fe856 | 1017 | |
bcostm | 6:e1d9da7fe856 | 1018 | /** |
bcostm | 6:e1d9da7fe856 | 1019 | * @brief Resumes the audio file stream. |
bcostm | 6:e1d9da7fe856 | 1020 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 1021 | */ |
bcostm | 6:e1d9da7fe856 | 1022 | uint8_t BSP_AUDIO_IN_Resume(void) |
bcostm | 6:e1d9da7fe856 | 1023 | { |
bcostm | 6:e1d9da7fe856 | 1024 | /* Call the Media layer pause/resume function */ |
bcostm | 6:e1d9da7fe856 | 1025 | HAL_SAI_DMAResume(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1026 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 1027 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 1028 | } |
bcostm | 6:e1d9da7fe856 | 1029 | |
bcostm | 6:e1d9da7fe856 | 1030 | /** |
bcostm | 6:e1d9da7fe856 | 1031 | * @brief Controls the audio in volume level. |
bcostm | 6:e1d9da7fe856 | 1032 | * @param Volume: Volume level in range 0(Mute)..80(+0dB)..100(+17.625dB) |
bcostm | 6:e1d9da7fe856 | 1033 | * @retval AUDIO_OK if correct communication, else wrong communication |
bcostm | 6:e1d9da7fe856 | 1034 | */ |
bcostm | 6:e1d9da7fe856 | 1035 | uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume) |
bcostm | 6:e1d9da7fe856 | 1036 | { |
bcostm | 6:e1d9da7fe856 | 1037 | /* Call the codec volume control function with converted volume value */ |
bcostm | 6:e1d9da7fe856 | 1038 | if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) |
bcostm | 6:e1d9da7fe856 | 1039 | { |
bcostm | 6:e1d9da7fe856 | 1040 | return AUDIO_ERROR; |
bcostm | 6:e1d9da7fe856 | 1041 | } |
bcostm | 6:e1d9da7fe856 | 1042 | else |
bcostm | 6:e1d9da7fe856 | 1043 | { |
bcostm | 6:e1d9da7fe856 | 1044 | /* Set the Global variable AudioInVolume */ |
bcostm | 6:e1d9da7fe856 | 1045 | AudioInVolume = Volume; |
bcostm | 6:e1d9da7fe856 | 1046 | /* Return AUDIO_OK when all operations are correctly done */ |
bcostm | 6:e1d9da7fe856 | 1047 | return AUDIO_OK; |
bcostm | 6:e1d9da7fe856 | 1048 | } |
bcostm | 6:e1d9da7fe856 | 1049 | } |
bcostm | 6:e1d9da7fe856 | 1050 | |
bcostm | 6:e1d9da7fe856 | 1051 | /** |
bcostm | 6:e1d9da7fe856 | 1052 | * @brief Deinit the audio IN peripherals. |
bcostm | 6:e1d9da7fe856 | 1053 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1054 | */ |
bcostm | 6:e1d9da7fe856 | 1055 | void BSP_AUDIO_IN_DeInit(void) |
bcostm | 6:e1d9da7fe856 | 1056 | { |
bcostm | 6:e1d9da7fe856 | 1057 | SAIx_In_DeInit(); |
bcostm | 6:e1d9da7fe856 | 1058 | /* DeInit the SAI MSP : this __weak function can be rewritten by the application */ |
bcostm | 6:e1d9da7fe856 | 1059 | BSP_AUDIO_IN_MspDeInit(&haudio_in_sai, NULL); |
bcostm | 6:e1d9da7fe856 | 1060 | } |
bcostm | 6:e1d9da7fe856 | 1061 | |
bcostm | 6:e1d9da7fe856 | 1062 | /** |
bcostm | 6:e1d9da7fe856 | 1063 | * @brief Rx Transfer completed callbacks. |
bcostm | 6:e1d9da7fe856 | 1064 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 1065 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1066 | */ |
bcostm | 6:e1d9da7fe856 | 1067 | void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai) |
bcostm | 6:e1d9da7fe856 | 1068 | { |
bcostm | 6:e1d9da7fe856 | 1069 | /* Call the record update function to get the next buffer to fill and its size (size is ignored) */ |
bcostm | 6:e1d9da7fe856 | 1070 | BSP_AUDIO_IN_TransferComplete_CallBack(); |
bcostm | 6:e1d9da7fe856 | 1071 | } |
bcostm | 6:e1d9da7fe856 | 1072 | |
bcostm | 6:e1d9da7fe856 | 1073 | /** |
bcostm | 6:e1d9da7fe856 | 1074 | * @brief Rx Half Transfer completed callbacks. |
bcostm | 6:e1d9da7fe856 | 1075 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 1076 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1077 | */ |
bcostm | 6:e1d9da7fe856 | 1078 | void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai) |
bcostm | 6:e1d9da7fe856 | 1079 | { |
bcostm | 6:e1d9da7fe856 | 1080 | /* Manage the remaining file size and new address offset: This function |
bcostm | 6:e1d9da7fe856 | 1081 | should be coded by user (its prototype is already declared in stm32746g_discovery_audio.h) */ |
bcostm | 6:e1d9da7fe856 | 1082 | BSP_AUDIO_IN_HalfTransfer_CallBack(); |
bcostm | 6:e1d9da7fe856 | 1083 | } |
bcostm | 6:e1d9da7fe856 | 1084 | |
bcostm | 6:e1d9da7fe856 | 1085 | /** |
bcostm | 6:e1d9da7fe856 | 1086 | * @brief User callback when record buffer is filled. |
bcostm | 6:e1d9da7fe856 | 1087 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1088 | */ |
bcostm | 6:e1d9da7fe856 | 1089 | __weak void BSP_AUDIO_IN_TransferComplete_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 1090 | { |
bcostm | 6:e1d9da7fe856 | 1091 | /* This function should be implemented by the user application. |
bcostm | 6:e1d9da7fe856 | 1092 | It is called into this driver when the current buffer is filled |
bcostm | 6:e1d9da7fe856 | 1093 | to prepare the next buffer pointer and its size. */ |
bcostm | 6:e1d9da7fe856 | 1094 | } |
bcostm | 6:e1d9da7fe856 | 1095 | |
bcostm | 6:e1d9da7fe856 | 1096 | /** |
bcostm | 6:e1d9da7fe856 | 1097 | * @brief Manages the DMA Half Transfer complete event. |
bcostm | 6:e1d9da7fe856 | 1098 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1099 | */ |
bcostm | 6:e1d9da7fe856 | 1100 | __weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 1101 | { |
bcostm | 6:e1d9da7fe856 | 1102 | /* This function should be implemented by the user application. |
bcostm | 6:e1d9da7fe856 | 1103 | It is called into this driver when the current buffer is filled |
bcostm | 6:e1d9da7fe856 | 1104 | to prepare the next buffer pointer and its size. */ |
bcostm | 6:e1d9da7fe856 | 1105 | } |
bcostm | 6:e1d9da7fe856 | 1106 | |
bcostm | 6:e1d9da7fe856 | 1107 | /** |
bcostm | 6:e1d9da7fe856 | 1108 | * @brief Audio IN Error callback function. |
bcostm | 6:e1d9da7fe856 | 1109 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1110 | */ |
bcostm | 6:e1d9da7fe856 | 1111 | __weak void BSP_AUDIO_IN_Error_CallBack(void) |
bcostm | 6:e1d9da7fe856 | 1112 | { |
bcostm | 6:e1d9da7fe856 | 1113 | /* This function is called when an Interrupt due to transfer error on or peripheral |
bcostm | 6:e1d9da7fe856 | 1114 | error occurs. */ |
bcostm | 6:e1d9da7fe856 | 1115 | } |
bcostm | 6:e1d9da7fe856 | 1116 | |
bcostm | 6:e1d9da7fe856 | 1117 | /** |
bcostm | 6:e1d9da7fe856 | 1118 | * @brief Initializes BSP_AUDIO_IN MSP. |
bcostm | 6:e1d9da7fe856 | 1119 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 1120 | * @param Params |
bcostm | 6:e1d9da7fe856 | 1121 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1122 | */ |
bcostm | 6:e1d9da7fe856 | 1123 | __weak void BSP_AUDIO_IN_MspInit(SAI_HandleTypeDef *hsai, void *Params) |
bcostm | 6:e1d9da7fe856 | 1124 | { |
bcostm | 6:e1d9da7fe856 | 1125 | static DMA_HandleTypeDef hdma_sai_rx; |
bcostm | 6:e1d9da7fe856 | 1126 | GPIO_InitTypeDef gpio_init_structure; |
bcostm | 6:e1d9da7fe856 | 1127 | |
bcostm | 6:e1d9da7fe856 | 1128 | /* Enable SAI clock */ |
bcostm | 6:e1d9da7fe856 | 1129 | AUDIO_IN_SAIx_CLK_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 1130 | |
bcostm | 6:e1d9da7fe856 | 1131 | /* Enable SD GPIO clock */ |
bcostm | 6:e1d9da7fe856 | 1132 | AUDIO_IN_SAIx_SD_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 1133 | /* CODEC_SAI pin configuration: SD pin */ |
bcostm | 6:e1d9da7fe856 | 1134 | gpio_init_structure.Pin = AUDIO_IN_SAIx_SD_PIN; |
bcostm | 6:e1d9da7fe856 | 1135 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
bcostm | 6:e1d9da7fe856 | 1136 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 1137 | gpio_init_structure.Speed = GPIO_SPEED_FAST; |
bcostm | 6:e1d9da7fe856 | 1138 | gpio_init_structure.Alternate = AUDIO_IN_SAIx_SD_AF; |
bcostm | 6:e1d9da7fe856 | 1139 | HAL_GPIO_Init(AUDIO_IN_SAIx_SD_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 1140 | |
bcostm | 6:e1d9da7fe856 | 1141 | /* Enable Audio INT GPIO clock */ |
bcostm | 6:e1d9da7fe856 | 1142 | AUDIO_IN_INT_GPIO_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 1143 | /* Audio INT pin configuration: input */ |
bcostm | 6:e1d9da7fe856 | 1144 | gpio_init_structure.Pin = AUDIO_IN_INT_GPIO_PIN; |
bcostm | 6:e1d9da7fe856 | 1145 | gpio_init_structure.Mode = GPIO_MODE_INPUT; |
bcostm | 6:e1d9da7fe856 | 1146 | gpio_init_structure.Pull = GPIO_NOPULL; |
bcostm | 6:e1d9da7fe856 | 1147 | gpio_init_structure.Speed = GPIO_SPEED_FAST; |
bcostm | 6:e1d9da7fe856 | 1148 | HAL_GPIO_Init(AUDIO_IN_INT_GPIO_PORT, &gpio_init_structure); |
bcostm | 6:e1d9da7fe856 | 1149 | |
bcostm | 6:e1d9da7fe856 | 1150 | /* Enable the DMA clock */ |
bcostm | 6:e1d9da7fe856 | 1151 | AUDIO_IN_SAIx_DMAx_CLK_ENABLE(); |
bcostm | 6:e1d9da7fe856 | 1152 | |
bcostm | 6:e1d9da7fe856 | 1153 | if(hsai->Instance == AUDIO_IN_SAIx) |
bcostm | 6:e1d9da7fe856 | 1154 | { |
bcostm | 6:e1d9da7fe856 | 1155 | /* Configure the hdma_sai_rx handle parameters */ |
bcostm | 6:e1d9da7fe856 | 1156 | hdma_sai_rx.Init.Channel = AUDIO_IN_SAIx_DMAx_CHANNEL; |
bcostm | 6:e1d9da7fe856 | 1157 | hdma_sai_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; |
bcostm | 6:e1d9da7fe856 | 1158 | hdma_sai_rx.Init.PeriphInc = DMA_PINC_DISABLE; |
bcostm | 6:e1d9da7fe856 | 1159 | hdma_sai_rx.Init.MemInc = DMA_MINC_ENABLE; |
bcostm | 6:e1d9da7fe856 | 1160 | hdma_sai_rx.Init.PeriphDataAlignment = AUDIO_IN_SAIx_DMAx_PERIPH_DATA_SIZE; |
bcostm | 6:e1d9da7fe856 | 1161 | hdma_sai_rx.Init.MemDataAlignment = AUDIO_IN_SAIx_DMAx_MEM_DATA_SIZE; |
bcostm | 6:e1d9da7fe856 | 1162 | hdma_sai_rx.Init.Mode = DMA_CIRCULAR; |
bcostm | 6:e1d9da7fe856 | 1163 | hdma_sai_rx.Init.Priority = DMA_PRIORITY_HIGH; |
bcostm | 6:e1d9da7fe856 | 1164 | hdma_sai_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
bcostm | 6:e1d9da7fe856 | 1165 | hdma_sai_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
bcostm | 6:e1d9da7fe856 | 1166 | hdma_sai_rx.Init.MemBurst = DMA_MBURST_SINGLE; |
bcostm | 6:e1d9da7fe856 | 1167 | hdma_sai_rx.Init.PeriphBurst = DMA_MBURST_SINGLE; |
bcostm | 6:e1d9da7fe856 | 1168 | |
bcostm | 6:e1d9da7fe856 | 1169 | hdma_sai_rx.Instance = AUDIO_IN_SAIx_DMAx_STREAM; |
bcostm | 6:e1d9da7fe856 | 1170 | |
bcostm | 6:e1d9da7fe856 | 1171 | /* Associate the DMA handle */ |
bcostm | 6:e1d9da7fe856 | 1172 | __HAL_LINKDMA(hsai, hdmarx, hdma_sai_rx); |
bcostm | 6:e1d9da7fe856 | 1173 | |
bcostm | 6:e1d9da7fe856 | 1174 | /* Deinitialize the Stream for new transfer */ |
bcostm | 6:e1d9da7fe856 | 1175 | HAL_DMA_DeInit(&hdma_sai_rx); |
bcostm | 6:e1d9da7fe856 | 1176 | |
bcostm | 6:e1d9da7fe856 | 1177 | /* Configure the DMA Stream */ |
bcostm | 6:e1d9da7fe856 | 1178 | HAL_DMA_Init(&hdma_sai_rx); |
bcostm | 6:e1d9da7fe856 | 1179 | } |
bcostm | 6:e1d9da7fe856 | 1180 | |
bcostm | 6:e1d9da7fe856 | 1181 | /* SAI DMA IRQ Channel configuration */ |
bcostm | 6:e1d9da7fe856 | 1182 | #if ( __MBED__ == 1) |
bcostm | 6:e1d9da7fe856 | 1183 | IRQn_Type irqn = (IRQn_Type)(AUDIO_IN_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 1184 | NVIC_ClearPendingIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1185 | NVIC_DisableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1186 | NVIC_SetPriority(irqn, AUDIO_IN_IRQ_PREPRIO); |
bcostm | 6:e1d9da7fe856 | 1187 | NVIC_SetVector(irqn, (uint32_t)AUDIO_IN_SAIx_DMAx_IRQHandler); |
bcostm | 6:e1d9da7fe856 | 1188 | NVIC_EnableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1189 | #else |
bcostm | 6:e1d9da7fe856 | 1190 | HAL_NVIC_SetPriority(AUDIO_IN_SAIx_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0); |
bcostm | 6:e1d9da7fe856 | 1191 | HAL_NVIC_EnableIRQ(AUDIO_IN_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 1192 | #endif |
bcostm | 6:e1d9da7fe856 | 1193 | |
bcostm | 6:e1d9da7fe856 | 1194 | /* Audio INT IRQ Channel configuration */ |
bcostm | 6:e1d9da7fe856 | 1195 | #if ( __MBED__ == 1) |
bcostm | 6:e1d9da7fe856 | 1196 | irqn = (IRQn_Type)(AUDIO_IN_INT_IRQ); |
bcostm | 6:e1d9da7fe856 | 1197 | NVIC_ClearPendingIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1198 | NVIC_DisableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1199 | NVIC_SetPriority(irqn, AUDIO_IN_IRQ_PREPRIO); |
bcostm | 6:e1d9da7fe856 | 1200 | NVIC_SetVector(irqn, (uint32_t)AUDIO_IN_INT_IRQHandler); |
bcostm | 6:e1d9da7fe856 | 1201 | NVIC_EnableIRQ(irqn); |
bcostm | 6:e1d9da7fe856 | 1202 | #else |
bcostm | 6:e1d9da7fe856 | 1203 | HAL_NVIC_SetPriority(AUDIO_IN_INT_IRQ, AUDIO_IN_IRQ_PREPRIO, 0); |
bcostm | 6:e1d9da7fe856 | 1204 | HAL_NVIC_EnableIRQ(AUDIO_IN_INT_IRQ); |
bcostm | 6:e1d9da7fe856 | 1205 | #endif |
bcostm | 6:e1d9da7fe856 | 1206 | } |
bcostm | 6:e1d9da7fe856 | 1207 | |
bcostm | 6:e1d9da7fe856 | 1208 | /** |
bcostm | 6:e1d9da7fe856 | 1209 | * @brief DeInitializes BSP_AUDIO_IN MSP. |
bcostm | 6:e1d9da7fe856 | 1210 | * @param hsai: SAI handle |
bcostm | 6:e1d9da7fe856 | 1211 | * @param Params |
bcostm | 6:e1d9da7fe856 | 1212 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1213 | */ |
bcostm | 6:e1d9da7fe856 | 1214 | __weak void BSP_AUDIO_IN_MspDeInit(SAI_HandleTypeDef *hsai, void *Params) |
bcostm | 6:e1d9da7fe856 | 1215 | { |
bcostm | 6:e1d9da7fe856 | 1216 | GPIO_InitTypeDef gpio_init_structure; |
bcostm | 6:e1d9da7fe856 | 1217 | |
bcostm | 6:e1d9da7fe856 | 1218 | static DMA_HandleTypeDef hdma_sai_rx; |
bcostm | 6:e1d9da7fe856 | 1219 | |
bcostm | 6:e1d9da7fe856 | 1220 | /* SAI IN DMA IRQ Channel deactivation */ |
bcostm | 6:e1d9da7fe856 | 1221 | HAL_NVIC_DisableIRQ(AUDIO_IN_SAIx_DMAx_IRQ); |
bcostm | 6:e1d9da7fe856 | 1222 | |
bcostm | 6:e1d9da7fe856 | 1223 | if(hsai->Instance == AUDIO_IN_SAIx) |
bcostm | 6:e1d9da7fe856 | 1224 | { |
bcostm | 6:e1d9da7fe856 | 1225 | /* Deinitialize the Stream for new transfer */ |
bcostm | 6:e1d9da7fe856 | 1226 | HAL_DMA_DeInit(&hdma_sai_rx); |
bcostm | 6:e1d9da7fe856 | 1227 | } |
bcostm | 6:e1d9da7fe856 | 1228 | |
bcostm | 6:e1d9da7fe856 | 1229 | /* Disable SAI block */ |
bcostm | 6:e1d9da7fe856 | 1230 | __HAL_SAI_DISABLE(hsai); |
bcostm | 6:e1d9da7fe856 | 1231 | |
bcostm | 6:e1d9da7fe856 | 1232 | /* Disable pin: SD pin */ |
bcostm | 6:e1d9da7fe856 | 1233 | gpio_init_structure.Pin = AUDIO_IN_SAIx_SD_PIN; |
bcostm | 6:e1d9da7fe856 | 1234 | HAL_GPIO_DeInit(AUDIO_IN_SAIx_SD_GPIO_PORT, gpio_init_structure.Pin); |
bcostm | 6:e1d9da7fe856 | 1235 | |
bcostm | 6:e1d9da7fe856 | 1236 | /* Disable SAI clock */ |
bcostm | 6:e1d9da7fe856 | 1237 | AUDIO_IN_SAIx_CLK_DISABLE(); |
bcostm | 6:e1d9da7fe856 | 1238 | |
bcostm | 6:e1d9da7fe856 | 1239 | /* GPIO pins clock and DMA clock can be shut down in the application |
bcostm | 6:e1d9da7fe856 | 1240 | by surcharging this __weak function */ |
bcostm | 6:e1d9da7fe856 | 1241 | } |
bcostm | 6:e1d9da7fe856 | 1242 | |
bcostm | 6:e1d9da7fe856 | 1243 | |
bcostm | 6:e1d9da7fe856 | 1244 | /******************************************************************************* |
bcostm | 6:e1d9da7fe856 | 1245 | Static Functions |
bcostm | 6:e1d9da7fe856 | 1246 | *******************************************************************************/ |
bcostm | 6:e1d9da7fe856 | 1247 | |
bcostm | 6:e1d9da7fe856 | 1248 | /** |
bcostm | 6:e1d9da7fe856 | 1249 | * @brief Initializes the input Audio Codec audio interface (SAI). |
bcostm | 6:e1d9da7fe856 | 1250 | * @param SaiOutMode: SAI_MODEMASTER_TX (for record and playback in parallel) |
bcostm | 6:e1d9da7fe856 | 1251 | * or SAI_MODEMASTER_RX (for record only). |
bcostm | 6:e1d9da7fe856 | 1252 | * @param SlotActive: CODEC_AUDIOFRAME_SLOT_02 or CODEC_AUDIOFRAME_SLOT_13 |
bcostm | 6:e1d9da7fe856 | 1253 | * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. |
bcostm | 6:e1d9da7fe856 | 1254 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1255 | */ |
bcostm | 6:e1d9da7fe856 | 1256 | static void SAIx_In_Init(uint32_t SaiOutMode, uint32_t SlotActive, uint32_t AudioFreq) |
bcostm | 6:e1d9da7fe856 | 1257 | { |
bcostm | 6:e1d9da7fe856 | 1258 | /* Initialize SAI2 block A in MASTER RX */ |
bcostm | 6:e1d9da7fe856 | 1259 | /* Initialize the haudio_out_sai Instance parameter */ |
bcostm | 6:e1d9da7fe856 | 1260 | haudio_out_sai.Instance = AUDIO_OUT_SAIx; |
bcostm | 6:e1d9da7fe856 | 1261 | |
bcostm | 6:e1d9da7fe856 | 1262 | /* Disable SAI peripheral to allow access to SAI internal registers */ |
bcostm | 6:e1d9da7fe856 | 1263 | __HAL_SAI_DISABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 1264 | |
bcostm | 6:e1d9da7fe856 | 1265 | /* Configure SAI_Block_x |
bcostm | 6:e1d9da7fe856 | 1266 | LSBFirst: Disabled |
bcostm | 6:e1d9da7fe856 | 1267 | DataSize: 16 */ |
bcostm | 6:e1d9da7fe856 | 1268 | haudio_out_sai.Init.AudioFrequency = AudioFreq; |
bcostm | 6:e1d9da7fe856 | 1269 | haudio_out_sai.Init.AudioMode = SaiOutMode; |
bcostm | 6:e1d9da7fe856 | 1270 | haudio_out_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLED; |
bcostm | 6:e1d9da7fe856 | 1271 | haudio_out_sai.Init.Protocol = SAI_FREE_PROTOCOL; |
bcostm | 6:e1d9da7fe856 | 1272 | haudio_out_sai.Init.DataSize = SAI_DATASIZE_16; |
bcostm | 6:e1d9da7fe856 | 1273 | haudio_out_sai.Init.FirstBit = SAI_FIRSTBIT_MSB; |
bcostm | 6:e1d9da7fe856 | 1274 | haudio_out_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE; |
bcostm | 6:e1d9da7fe856 | 1275 | haudio_out_sai.Init.Synchro = SAI_ASYNCHRONOUS; |
bcostm | 6:e1d9da7fe856 | 1276 | haudio_out_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLED; |
bcostm | 6:e1d9da7fe856 | 1277 | haudio_out_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF; |
bcostm | 6:e1d9da7fe856 | 1278 | |
bcostm | 6:e1d9da7fe856 | 1279 | /* Configure SAI_Block_x Frame |
bcostm | 6:e1d9da7fe856 | 1280 | Frame Length: 64 |
bcostm | 6:e1d9da7fe856 | 1281 | Frame active Length: 32 |
bcostm | 6:e1d9da7fe856 | 1282 | FS Definition: Start frame + Channel Side identification |
bcostm | 6:e1d9da7fe856 | 1283 | FS Polarity: FS active Low |
bcostm | 6:e1d9da7fe856 | 1284 | FS Offset: FS asserted one bit before the first bit of slot 0 */ |
bcostm | 6:e1d9da7fe856 | 1285 | haudio_out_sai.FrameInit.FrameLength = 64; |
bcostm | 6:e1d9da7fe856 | 1286 | haudio_out_sai.FrameInit.ActiveFrameLength = 32; |
bcostm | 6:e1d9da7fe856 | 1287 | haudio_out_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION; |
bcostm | 6:e1d9da7fe856 | 1288 | haudio_out_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW; |
bcostm | 6:e1d9da7fe856 | 1289 | haudio_out_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT; |
bcostm | 6:e1d9da7fe856 | 1290 | |
bcostm | 6:e1d9da7fe856 | 1291 | /* Configure SAI Block_x Slot |
bcostm | 6:e1d9da7fe856 | 1292 | Slot First Bit Offset: 0 |
bcostm | 6:e1d9da7fe856 | 1293 | Slot Size : 16 |
bcostm | 6:e1d9da7fe856 | 1294 | Slot Number: 4 |
bcostm | 6:e1d9da7fe856 | 1295 | Slot Active: All slot actives */ |
bcostm | 6:e1d9da7fe856 | 1296 | haudio_out_sai.SlotInit.FirstBitOffset = 0; |
bcostm | 6:e1d9da7fe856 | 1297 | haudio_out_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE; |
bcostm | 6:e1d9da7fe856 | 1298 | haudio_out_sai.SlotInit.SlotNumber = 4; |
bcostm | 6:e1d9da7fe856 | 1299 | haudio_out_sai.SlotInit.SlotActive = SlotActive; |
bcostm | 6:e1d9da7fe856 | 1300 | |
bcostm | 6:e1d9da7fe856 | 1301 | HAL_SAI_Init(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 1302 | |
bcostm | 6:e1d9da7fe856 | 1303 | /* Initialize SAI2 block B in SLAVE RX synchronous from SAI2 block A */ |
bcostm | 6:e1d9da7fe856 | 1304 | /* Initialize the haudio_in_sai Instance parameter */ |
bcostm | 6:e1d9da7fe856 | 1305 | haudio_in_sai.Instance = AUDIO_IN_SAIx; |
bcostm | 6:e1d9da7fe856 | 1306 | |
bcostm | 6:e1d9da7fe856 | 1307 | /* Disable SAI peripheral to allow access to SAI internal registers */ |
bcostm | 6:e1d9da7fe856 | 1308 | __HAL_SAI_DISABLE(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1309 | |
bcostm | 6:e1d9da7fe856 | 1310 | /* Configure SAI_Block_x |
bcostm | 6:e1d9da7fe856 | 1311 | LSBFirst: Disabled |
bcostm | 6:e1d9da7fe856 | 1312 | DataSize: 16 */ |
bcostm | 6:e1d9da7fe856 | 1313 | haudio_in_sai.Init.AudioFrequency = AudioFreq; |
bcostm | 6:e1d9da7fe856 | 1314 | haudio_in_sai.Init.AudioMode = SAI_MODESLAVE_RX; |
bcostm | 6:e1d9da7fe856 | 1315 | haudio_in_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLED; |
bcostm | 6:e1d9da7fe856 | 1316 | haudio_in_sai.Init.Protocol = SAI_FREE_PROTOCOL; |
bcostm | 6:e1d9da7fe856 | 1317 | haudio_in_sai.Init.DataSize = SAI_DATASIZE_16; |
bcostm | 6:e1d9da7fe856 | 1318 | haudio_in_sai.Init.FirstBit = SAI_FIRSTBIT_MSB; |
bcostm | 6:e1d9da7fe856 | 1319 | haudio_in_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE; |
bcostm | 6:e1d9da7fe856 | 1320 | haudio_in_sai.Init.Synchro = SAI_SYNCHRONOUS; |
bcostm | 6:e1d9da7fe856 | 1321 | haudio_in_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLED; |
bcostm | 6:e1d9da7fe856 | 1322 | haudio_in_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF; |
bcostm | 6:e1d9da7fe856 | 1323 | |
bcostm | 6:e1d9da7fe856 | 1324 | /* Configure SAI_Block_x Frame |
bcostm | 6:e1d9da7fe856 | 1325 | Frame Length: 64 |
bcostm | 6:e1d9da7fe856 | 1326 | Frame active Length: 32 |
bcostm | 6:e1d9da7fe856 | 1327 | FS Definition: Start frame + Channel Side identification |
bcostm | 6:e1d9da7fe856 | 1328 | FS Polarity: FS active Low |
bcostm | 6:e1d9da7fe856 | 1329 | FS Offset: FS asserted one bit before the first bit of slot 0 */ |
bcostm | 6:e1d9da7fe856 | 1330 | haudio_in_sai.FrameInit.FrameLength = 64; |
bcostm | 6:e1d9da7fe856 | 1331 | haudio_in_sai.FrameInit.ActiveFrameLength = 32; |
bcostm | 6:e1d9da7fe856 | 1332 | haudio_in_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION; |
bcostm | 6:e1d9da7fe856 | 1333 | haudio_in_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW; |
bcostm | 6:e1d9da7fe856 | 1334 | haudio_in_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT; |
bcostm | 6:e1d9da7fe856 | 1335 | |
bcostm | 6:e1d9da7fe856 | 1336 | /* Configure SAI Block_x Slot |
bcostm | 6:e1d9da7fe856 | 1337 | Slot First Bit Offset: 0 |
bcostm | 6:e1d9da7fe856 | 1338 | Slot Size : 16 |
bcostm | 6:e1d9da7fe856 | 1339 | Slot Number: 4 |
bcostm | 6:e1d9da7fe856 | 1340 | Slot Active: All slot active */ |
bcostm | 6:e1d9da7fe856 | 1341 | haudio_in_sai.SlotInit.FirstBitOffset = 0; |
bcostm | 6:e1d9da7fe856 | 1342 | haudio_in_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE; |
bcostm | 6:e1d9da7fe856 | 1343 | haudio_in_sai.SlotInit.SlotNumber = 4; |
bcostm | 6:e1d9da7fe856 | 1344 | haudio_in_sai.SlotInit.SlotActive = SlotActive; |
bcostm | 6:e1d9da7fe856 | 1345 | |
bcostm | 6:e1d9da7fe856 | 1346 | HAL_SAI_Init(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1347 | |
bcostm | 6:e1d9da7fe856 | 1348 | /* Enable SAI peripheral to generate MCLK */ |
bcostm | 6:e1d9da7fe856 | 1349 | __HAL_SAI_ENABLE(&haudio_out_sai); |
bcostm | 6:e1d9da7fe856 | 1350 | |
bcostm | 6:e1d9da7fe856 | 1351 | /* Enable SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 1352 | __HAL_SAI_ENABLE(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1353 | } |
bcostm | 6:e1d9da7fe856 | 1354 | |
bcostm | 6:e1d9da7fe856 | 1355 | |
bcostm | 6:e1d9da7fe856 | 1356 | |
bcostm | 6:e1d9da7fe856 | 1357 | /** |
bcostm | 6:e1d9da7fe856 | 1358 | * @brief Deinitializes the output Audio Codec audio interface (SAI). |
bcostm | 6:e1d9da7fe856 | 1359 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1360 | */ |
bcostm | 6:e1d9da7fe856 | 1361 | static void SAIx_In_DeInit(void) |
bcostm | 6:e1d9da7fe856 | 1362 | { |
bcostm | 6:e1d9da7fe856 | 1363 | /* Initialize the haudio_in_sai Instance parameter */ |
bcostm | 6:e1d9da7fe856 | 1364 | haudio_in_sai.Instance = AUDIO_IN_SAIx; |
bcostm | 6:e1d9da7fe856 | 1365 | |
bcostm | 6:e1d9da7fe856 | 1366 | /* Disable SAI peripheral */ |
bcostm | 6:e1d9da7fe856 | 1367 | __HAL_SAI_DISABLE(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1368 | |
bcostm | 6:e1d9da7fe856 | 1369 | HAL_SAI_DeInit(&haudio_in_sai); |
bcostm | 6:e1d9da7fe856 | 1370 | } |
bcostm | 6:e1d9da7fe856 | 1371 | |
bcostm | 6:e1d9da7fe856 | 1372 | #if ( __MBED__ == 1) |
bcostm | 6:e1d9da7fe856 | 1373 | /** |
bcostm | 6:e1d9da7fe856 | 1374 | * @brief This function handles External line 15_10 interrupt request. |
bcostm | 6:e1d9da7fe856 | 1375 | * @param None |
bcostm | 6:e1d9da7fe856 | 1376 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1377 | */ |
bcostm | 6:e1d9da7fe856 | 1378 | static void AUDIO_IN_INT_IRQHandler(void) |
bcostm | 6:e1d9da7fe856 | 1379 | { |
bcostm | 6:e1d9da7fe856 | 1380 | /* Interrupt handler shared between SD_DETECT pin, USER_KEY button and touch screen interrupt */ |
bcostm | 6:e1d9da7fe856 | 1381 | if (__HAL_GPIO_EXTI_GET_IT(AUDIO_IN_INT_GPIO_PIN) != RESET) |
bcostm | 6:e1d9da7fe856 | 1382 | { |
bcostm | 6:e1d9da7fe856 | 1383 | HAL_GPIO_EXTI_IRQHandler(AUDIO_IN_INT_GPIO_PIN); /* Audio Interrupt */ |
bcostm | 6:e1d9da7fe856 | 1384 | } |
bcostm | 6:e1d9da7fe856 | 1385 | } |
bcostm | 6:e1d9da7fe856 | 1386 | |
bcostm | 6:e1d9da7fe856 | 1387 | /** |
bcostm | 6:e1d9da7fe856 | 1388 | * @brief This function handles DMA2 Stream 7 interrupt request. |
bcostm | 6:e1d9da7fe856 | 1389 | * @param None |
bcostm | 6:e1d9da7fe856 | 1390 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1391 | */ |
bcostm | 6:e1d9da7fe856 | 1392 | static void AUDIO_IN_SAIx_DMAx_IRQHandler(void) |
bcostm | 6:e1d9da7fe856 | 1393 | { |
bcostm | 6:e1d9da7fe856 | 1394 | HAL_DMA_IRQHandler(haudio_in_sai.hdmarx); |
bcostm | 6:e1d9da7fe856 | 1395 | } |
bcostm | 6:e1d9da7fe856 | 1396 | |
bcostm | 6:e1d9da7fe856 | 1397 | /** |
bcostm | 6:e1d9da7fe856 | 1398 | * @brief This function handles DMA2 Stream 6 interrupt request. |
bcostm | 6:e1d9da7fe856 | 1399 | * @param None |
bcostm | 6:e1d9da7fe856 | 1400 | * @retval None |
bcostm | 6:e1d9da7fe856 | 1401 | */ |
bcostm | 6:e1d9da7fe856 | 1402 | static void AUDIO_OUT_SAIx_DMAx_IRQHandler(void) |
bcostm | 6:e1d9da7fe856 | 1403 | { |
bcostm | 6:e1d9da7fe856 | 1404 | HAL_DMA_IRQHandler(haudio_out_sai.hdmatx); |
bcostm | 6:e1d9da7fe856 | 1405 | } |
bcostm | 6:e1d9da7fe856 | 1406 | #endif |
bcostm | 6:e1d9da7fe856 | 1407 | |
bcostm | 6:e1d9da7fe856 | 1408 | /** |
bcostm | 6:e1d9da7fe856 | 1409 | * @} |
bcostm | 6:e1d9da7fe856 | 1410 | */ |
bcostm | 6:e1d9da7fe856 | 1411 | |
bcostm | 6:e1d9da7fe856 | 1412 | /** |
bcostm | 6:e1d9da7fe856 | 1413 | * @} |
bcostm | 6:e1d9da7fe856 | 1414 | */ |
bcostm | 6:e1d9da7fe856 | 1415 | |
bcostm | 6:e1d9da7fe856 | 1416 | /** |
bcostm | 6:e1d9da7fe856 | 1417 | * @} |
bcostm | 6:e1d9da7fe856 | 1418 | */ |
bcostm | 6:e1d9da7fe856 | 1419 | |
bcostm | 6:e1d9da7fe856 | 1420 | /** |
bcostm | 6:e1d9da7fe856 | 1421 | * @} |
bcostm | 6:e1d9da7fe856 | 1422 | */ |
bcostm | 6:e1d9da7fe856 | 1423 | |
bcostm | 6:e1d9da7fe856 | 1424 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |