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