1 /** 2 ****************************************************************************** 3 * @file stm32f4_discovery_audio.h 4 * @author MCD Application Team 5 * @version V2.1.2 6 * @date 27-January-2017 7 * @brief This file contains the common defines and functions prototypes for 8 * stm32f4_discovery_audio.c driver. 9 ****************************************************************************** 10 * @attention 11 * 12 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 13 * 14 * Redistribution and use in source and binary forms, with or without modification, 15 * are permitted provided that the following conditions are met: 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 ****************************************************************************** 37 */ 38 39 /* Define to prevent recursive inclusion -------------------------------------*/ 40 #ifndef __STM32F4_DISCOVERY_AUDIO_H 41 #define __STM32F4_DISCOVERY_AUDIO_H 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 /* Includes ------------------------------------------------------------------*/ 48 /* Include audio component Driver */ 49 #include "cs43l22.h" 50 51 #include "stm32f4_discovery.h" 52 //#include "../../../Middlewares/ST/STM32_Audio/Addons/PDM/pdm_filter.h" 53 54 /** @addtogroup BSP 55 * @{ 56 */ 57 58 /** @addtogroup STM32F4_DISCOVERY 59 * @{ 60 */ 61 62 /** @addtogroup STM32F4_DISCOVERY_AUDIO 63 * @{ 64 */ 65 66 67 /** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Types STM32F4 DISCOVERY AUDIO Exported Types 68 * @{ 69 */ 70 /** 71 * @} 72 */ 73 74 /** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Exported_Constants STM32F4 DISCOVERY AUDIO OUT Exported Constants 75 * @{ 76 */ 77 78 /*------------------------------------------------------------------------------ 79 AUDIO OUT CONFIGURATION 80 ------------------------------------------------------------------------------*/ 81 82 /* I2S peripheral configuration defines */ 83 #define I2S3 SPI3 84 #define I2S3_CLK_ENABLE() __HAL_RCC_SPI3_CLK_ENABLE() 85 #define I2S3_CLK_DISABLE() __HAL_RCC_SPI3_CLK_DISABLE() 86 #define I2S3_SCK_SD_WS_AF GPIO_AF6_SPI3 87 #define I2S3_SCK_SD_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() 88 #define I2S3_MCK_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() 89 #define I2S3_WS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() 90 #define I2S3_WS_PIN GPIO_PIN_4 91 #define I2S3_SCK_PIN GPIO_PIN_10 92 #define I2S3_SD_PIN GPIO_PIN_12 93 #define I2S3_MCK_PIN GPIO_PIN_7 94 #define I2S3_SCK_SD_GPIO_PORT GPIOC 95 #define I2S3_WS_GPIO_PORT GPIOA 96 #define I2S3_MCK_GPIO_PORT GPIOC 97 98 /* I2S DMA Stream definitions */ 99 #define I2S3_DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE() 100 #define I2S3_DMAx_CLK_DISABLE() __HAL_RCC_DMA1_CLK_DISABLE() 101 #define I2S3_DMAx_STREAM DMA1_Stream7 102 #define I2S3_DMAx_CHANNEL DMA_CHANNEL_0 103 #define I2S3_DMAx_IRQ DMA1_Stream7_IRQn 104 #define I2S3_DMAx_PERIPH_DATA_SIZE DMA_PDATAALIGN_HALFWORD 105 #define I2S3_DMAx_MEM_DATA_SIZE DMA_MDATAALIGN_HALFWORD 106 #define DMA_MAX_SZE 0xFFFF 107 108 #define I2S3_IRQHandler DMA1_Stream7_IRQHandler 109 110 /* Select the interrupt preemption priority and subpriority for the DMA interrupt */ 111 #define AUDIO_OUT_IRQ_PREPRIO 0x0E /* Select the preemption priority level(0 is the highest) */ 112 113 /*------------------------------------------------------------------------------ 114 AUDIO IN CONFIGURATION 115 ------------------------------------------------------------------------------*/ 116 /* SPI Configuration defines */ 117 #define I2S2 SPI2 118 #define I2S2_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE() 119 #define I2S2_CLK_DISABLE() __HAL_RCC_SPI2_CLK_DISABLE() 120 #define I2S2_SCK_PIN GPIO_PIN_10 121 #define I2S2_SCK_GPIO_PORT GPIOB 122 #define I2S2_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() 123 #define I2S2_SCK_AF GPIO_AF5_SPI2 124 125 #define I2S2_MOSI_PIN GPIO_PIN_3 126 #define I2S2_MOSI_GPIO_PORT GPIOC 127 #define I2S2_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() 128 #define I2S2_MOSI_AF GPIO_AF5_SPI2 129 130 /* I2S DMA Stream Rx definitions */ 131 #define I2S2_DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE() 132 #define I2S2_DMAx_CLK_DISABLE() __HAL_RCC_DMA1_CLK_DISABLE() 133 #define I2S2_DMAx_STREAM DMA1_Stream3 134 #define I2S2_DMAx_CHANNEL DMA_CHANNEL_0 135 #define I2S2_DMAx_IRQ DMA1_Stream3_IRQn 136 #define I2S2_DMAx_PERIPH_DATA_SIZE DMA_PDATAALIGN_HALFWORD 137 #define I2S2_DMAx_MEM_DATA_SIZE DMA_MDATAALIGN_HALFWORD 138 139 #define I2S2_IRQHandler DMA1_Stream3_IRQHandler 140 141 /* Select the interrupt preemption priority and subpriority for the IT/DMA interrupt */ 142 #define AUDIO_IN_IRQ_PREPRIO 0x0F /* Select the preemption priority level(0 is the highest) */ 143 144 /*------------------------------------------------------------------------------ 145 CONFIGURATION: Audio Driver Configuration parameters 146 ------------------------------------------------------------------------------*/ 147 148 #define AUDIODATA_SIZE 2 /* 16-bits audio data size */ 149 150 /* Audio status definition */ 151 #define AUDIO_OK 0 152 #define AUDIO_ERROR 1 153 #define AUDIO_TIMEOUT 2 154 155 /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */ 156 #define DEFAULT_AUDIO_IN_FREQ I2S_AUDIOFREQ_16K 157 #define DEFAULT_AUDIO_IN_BIT_RESOLUTION 16 158 #define DEFAULT_AUDIO_IN_CHANNEL_NBR 1 /* Mono = 1, Stereo = 2 */ 159 #define DEFAULT_AUDIO_IN_VOLUME 64 160 161 /* PDM buffer input size */ 162 #define INTERNAL_BUFF_SIZE 128*DEFAULT_AUDIO_IN_FREQ/16000*DEFAULT_AUDIO_IN_CHANNEL_NBR 163 /* PCM buffer output size */ 164 #define PCM_OUT_SIZE DEFAULT_AUDIO_IN_FREQ/1000 165 #define CHANNEL_DEMUX_MASK 0x55 166 167 /*------------------------------------------------------------------------------ 168 OPTIONAL Configuration defines parameters 169 ------------------------------------------------------------------------------*/ 170 171 /** 172 * @} 173 */ 174 175 /** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Variables STM32F4 DISCOVERY AUDIO Exported Variables 176 * @{ 177 */ 178 extern __IO uint16_t AudioInVolume; 179 /** 180 * @} 181 */ 182 183 /** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Macros STM32F4 DISCOVERY AUDIO Exported Macros 184 * @{ 185 */ 186 #define DMA_MAX(_X_) (((_X_) <= DMA_MAX_SZE)? (_X_):DMA_MAX_SZE) 187 /** 188 * @} 189 */ 190 191 /** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Exported_Functions STM32F4 DISCOVERY AUDIO OUT Exported Functions 192 * @{ 193 */ 194 uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq); 195 uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size); 196 void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size); 197 uint8_t BSP_AUDIO_OUT_Pause(void); 198 uint8_t BSP_AUDIO_OUT_Resume(void); 199 uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option); 200 uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume); 201 void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq); 202 uint32_t BSP_AUDIO_OUT_GetFrequency(); 203 uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd); 204 uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output); 205 206 /* User Callbacks: user has to implement these functions in his code if they are needed. */ 207 /* This function is called when the requested data has been completely transferred. */ 208 void BSP_AUDIO_OUT_TransferComplete_CallBack(void); 209 210 /* This function is called when half of the requested buffer has been transferred. */ 211 void BSP_AUDIO_OUT_HalfTransfer_CallBack(void); 212 213 /* This function is called when an Interrupt due to transfer error on or peripheral 214 error occurs. */ 215 void BSP_AUDIO_OUT_Error_CallBack(void); 216 217 /* These function can be modified in case the current settings (e.g. DMA stream) 218 need to be changed for specific application needs */ 219 void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params); 220 void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params); 221 void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params); 222 223 /** 224 * @} 225 */ 226 227 /** @defgroup STM32F4_DISCOVERY_AUDIO_IN_Exported_Functions STM32F4 DISCOVERY AUDIO IN Exported Functions 228 * @{ 229 */ 230 uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr); 231 uint8_t BSP_AUDIO_IN_Record(uint16_t *pData, uint32_t Size); 232 uint8_t BSP_AUDIO_IN_Stop(void); 233 uint8_t BSP_AUDIO_IN_Pause(void); 234 uint8_t BSP_AUDIO_IN_Resume(void); 235 uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume); 236 uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t *PDMBuf, uint16_t *PCMBuf); 237 /* User Callbacks: user has to implement these functions in his code if they are needed. */ 238 /* This function should be implemented by the user application. 239 It is called into this driver when the current buffer is filled to prepare the next 240 buffer pointer and its size. */ 241 void BSP_AUDIO_IN_TransferComplete_CallBack(void); 242 void BSP_AUDIO_IN_HalfTransfer_CallBack(void); 243 244 /* This function is called when an Interrupt due to transfer error on or peripheral 245 error occurs. */ 246 void BSP_AUDIO_IN_Error_Callback(void); 247 248 /* These function can be modified in case the current settings (e.g. DMA stream) 249 need to be changed for specific application needs */ 250 void BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params); 251 void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params); 252 void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params); 253 254 /** 255 * @} 256 */ 257 258 /** 259 * @} 260 */ 261 262 /** 263 * @} 264 */ 265 266 /** 267 * @} 268 */ 269 270 #ifdef __cplusplus 271 } 272 #endif 273 274 #endif /* __STM32F4_DISCOVERY_AUDIO_H */ 275 276 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 277