.

Fork of BSP_DISCO_F746NG by ST

Committer:
bcostm
Date:
Mon Jan 04 15:19:15 2016 +0000
Revision:
1:ee089790cdbb
Child:
2:458ab1edf6b2
Add missing files

Who changed what in which revision?

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