From f763b8ceaf9411e1c5e999bc4423cdc8accb9a47 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 31 Jan 2021 23:17:24 +0100 Subject: [PATCH] stm32-f4discovery-usb: enable audio support --- .../Core/Inc/stm32f4xx_hal_conf.h | 6 +- port/stm32-f4discovery-usb/Makefile | 18 +- port/stm32-f4discovery-usb/bsp/audio.c | 27 + port/stm32-f4discovery-usb/bsp/audio.h | 122 ++ port/stm32-f4discovery-usb/bsp/cs43l22.c | 494 +++++++ port/stm32-f4discovery-usb/bsp/cs43l22.h | 228 ++++ .../bsp/stm32f4_discovery.c | 703 ++++++++++ .../bsp/stm32f4_discovery.h | 303 +++++ .../bsp/stm32f4_discovery_audio.c | 1154 +++++++++++++++++ .../bsp/stm32f4_discovery_audio.h | 275 ++++ .../pdm/Release_Notes.html | 217 ++++ .../pdm/libPDMFilter_CM4_GCC.a | Bin 0 -> 12694 bytes port/stm32-f4discovery-usb/pdm/pdm_filter.h | 76 ++ .../port/hal_audio_f4discovery.c | 333 +++++ port/stm32-f4discovery-usb/port/port.c | 4 +- port/stm32-f4discovery-usb/port/port.h | 4 + 16 files changed, 3954 insertions(+), 10 deletions(-) create mode 100644 port/stm32-f4discovery-usb/bsp/audio.c create mode 100644 port/stm32-f4discovery-usb/bsp/audio.h create mode 100644 port/stm32-f4discovery-usb/bsp/cs43l22.c create mode 100644 port/stm32-f4discovery-usb/bsp/cs43l22.h create mode 100644 port/stm32-f4discovery-usb/bsp/stm32f4_discovery.c create mode 100644 port/stm32-f4discovery-usb/bsp/stm32f4_discovery.h create mode 100644 port/stm32-f4discovery-usb/bsp/stm32f4_discovery_audio.c create mode 100644 port/stm32-f4discovery-usb/bsp/stm32f4_discovery_audio.h create mode 100755 port/stm32-f4discovery-usb/pdm/Release_Notes.html create mode 100755 port/stm32-f4discovery-usb/pdm/libPDMFilter_CM4_GCC.a create mode 100755 port/stm32-f4discovery-usb/pdm/pdm_filter.h create mode 100644 port/stm32-f4discovery-usb/port/hal_audio_f4discovery.c create mode 100644 port/stm32-f4discovery-usb/port/port.h diff --git a/port/stm32-f4discovery-usb/Core/Inc/stm32f4xx_hal_conf.h b/port/stm32-f4discovery-usb/Core/Inc/stm32f4xx_hal_conf.h index 39465ed9f..4bf7158b6 100644 --- a/port/stm32-f4discovery-usb/Core/Inc/stm32f4xx_hal_conf.h +++ b/port/stm32-f4discovery-usb/Core/Inc/stm32f4xx_hal_conf.h @@ -52,8 +52,8 @@ /* #define HAL_SRAM_MODULE_ENABLED */ /* #define HAL_SDRAM_MODULE_ENABLED */ /* #define HAL_HASH_MODULE_ENABLED */ -/* #define HAL_I2C_MODULE_ENABLED */ -/* #define HAL_I2S_MODULE_ENABLED */ +#define HAL_I2C_MODULE_ENABLED +#define HAL_I2S_MODULE_ENABLED /* #define HAL_IWDG_MODULE_ENABLED */ /* #define HAL_LTDC_MODULE_ENABLED */ /* #define HAL_RNG_MODULE_ENABLED */ @@ -61,7 +61,7 @@ /* #define HAL_SAI_MODULE_ENABLED */ /* #define HAL_SD_MODULE_ENABLED */ /* #define HAL_MMC_MODULE_ENABLED */ -/* #define HAL_SPI_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED /* #define HAL_TIM_MODULE_ENABLED */ /* #define HAL_UART_MODULE_ENABLED */ /* #define HAL_USART_MODULE_ENABLED */ diff --git a/port/stm32-f4discovery-usb/Makefile b/port/stm32-f4discovery-usb/Makefile index 9fe7bc812..b0bab97d3 100644 --- a/port/stm32-f4discovery-usb/Makefile +++ b/port/stm32-f4discovery-usb/Makefile @@ -149,7 +149,12 @@ ${BTSTACK_ROOT}/3rd-party/lwip/core/src/apps/http/httpd.c \ ${BTSTACK_ROOT}/3rd-party/lwip/dhcp-server/dhserver.c \ ${BTSTACK_ROOT}/platform/lwip/port/sys_arch.c \ ${BTSTACK_ROOT}/platform/lwip/bnep_lwip.c \ +bsp/audio.c \ +bsp/cs43l22.c \ +bsp/stm32f4_discovery.c \ +bsp/stm32f4_discovery_audio.c \ port/port.c \ +port/hal_audio_f4discovery.c \ port/usbh_bluetooth.c \ port/hci_transport_h2_stm32.c \ port/hal_flash_bank_stm32.c \ @@ -171,13 +176,19 @@ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \ +Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \ +Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \ +Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c \ +Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c \ +Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \ +Src/system_stm32f4xx.c \ Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_core.c \ Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c \ Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ioreq.c \ @@ -218,7 +229,7 @@ CPU = -mcpu=cortex-m4 FPU = -mfpu=fpv4-sp-d16 # float-abi -FLOAT-ABI = -mfloat-abi=hard +FLOAT-ABI = -mfloat-abi=softfp # mcu MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) @@ -292,9 +303,8 @@ CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" # link script LDSCRIPT = STM32F407VGTx_FLASH.ld -# libraries -LIBS = -lc -lm -lnosys -LIBDIR = +LIBS = -lPDMFilter_CM4_GCC -lc -lm -lnosys +LIBDIR = -Lpdm LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections # default action: build all diff --git a/port/stm32-f4discovery-usb/bsp/audio.c b/port/stm32-f4discovery-usb/bsp/audio.c new file mode 100644 index 000000000..65488689b --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/audio.c @@ -0,0 +1,27 @@ +#include "stm32f4_discovery_audio.h" + +// support for I2S/I2C CS43L22 codec is provided by bsp/stm32f4_discovery_audio.c and not by STM32CubeMX +// we need to provide the missing IRQ handler somewhere + +extern I2S_HandleTypeDef hAudioOutI2s; +extern I2S_HandleTypeDef hAudioInI2s; + +/** + * @brief This function handles main I2S interrupt. + * @param None + * @retval 0 if correct communication, else wrong communication + */ +void DMA1_Stream7_IRQHandler(void) +{ + HAL_DMA_IRQHandler(hAudioOutI2s.hdmatx); +} + +/** + * @brief This function handles main I2S interrupt. + * @param None + * @retval 0 if correct communication, else wrong communication + */ +void DMA1_Stream3_IRQHandler(void) +{ + HAL_DMA_IRQHandler(hAudioInI2s.hdmarx); +} diff --git a/port/stm32-f4discovery-usb/bsp/audio.h b/port/stm32-f4discovery-usb/bsp/audio.h new file mode 100644 index 000000000..8b93673ff --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/audio.h @@ -0,0 +1,122 @@ +/** + ****************************************************************************** + * @file audio.h + * @author MCD Application Team + * @version V4.0.1 + * @date 21-July-2015 + * @brief This header file contains the common defines and functions prototypes + * for the Audio driver. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2015 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __AUDIO_H +#define __AUDIO_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup Components + * @{ + */ + +/** @addtogroup AUDIO + * @{ + */ + +/** @defgroup AUDIO_Exported_Constants + * @{ + */ + +/* Codec audio Standards */ +#define CODEC_STANDARD 0x04 +#define I2S_STANDARD I2S_STANDARD_PHILIPS + +/** + * @} + */ + +/** @defgroup AUDIO_Exported_Types + * @{ + */ + +/** @defgroup AUDIO_Driver_structure Audio Driver structure + * @{ + */ +typedef struct +{ + uint32_t (*Init)(uint16_t, uint16_t, uint8_t, uint32_t); + void (*DeInit)(void); + uint32_t (*ReadID)(uint16_t); + uint32_t (*Play)(uint16_t, uint16_t*, uint16_t); + uint32_t (*Pause)(uint16_t); + uint32_t (*Resume)(uint16_t); + uint32_t (*Stop)(uint16_t, uint32_t); + uint32_t (*SetFrequency)(uint16_t, uint32_t); + uint32_t (*SetVolume)(uint16_t, uint8_t); + uint32_t (*SetMute)(uint16_t, uint32_t); + uint32_t (*SetOutputMode)(uint16_t, uint8_t); + uint32_t (*Reset)(uint16_t); +}AUDIO_DrvTypeDef; +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __AUDIO_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/bsp/cs43l22.c b/port/stm32-f4discovery-usb/bsp/cs43l22.c new file mode 100644 index 000000000..c54ad847f --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/cs43l22.c @@ -0,0 +1,494 @@ +/** + ****************************************************************************** + * @file cs43l22.c + * @author MCD Application Team + * @version V2.0.2 + * @date 06-October-2015 + * @brief This file provides the CS43L22 Audio Codec driver. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2015 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "cs43l22.h" + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup Components + * @{ + */ + +/** @addtogroup CS43L22 + * @brief This file provides a set of functions needed to drive the + * CS43L22 audio codec. + * @{ + */ + +/** @defgroup CS43L22_Private_Types + * @{ + */ + +/** + * @} + */ + +/** @defgroup CS43L22_Private_Defines + * @{ + */ +#define VOLUME_CONVERT(Volume) (((Volume) > 100)? 100:((uint8_t)(((Volume) * 255) / 100))) +/* Uncomment this line to enable verifying data sent to codec after each write + operation (for debug purpose) */ +#if !defined (VERIFY_WRITTENDATA) +/* #define VERIFY_WRITTENDATA */ +#endif /* VERIFY_WRITTENDATA */ +/** + * @} + */ + +/** @defgroup CS43L22_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup CS43L22_Private_Variables + * @{ + */ + +/* Audio codec driver structure initialization */ +AUDIO_DrvTypeDef cs43l22_drv = +{ + cs43l22_Init, + cs43l22_DeInit, + cs43l22_ReadID, + + cs43l22_Play, + cs43l22_Pause, + cs43l22_Resume, + cs43l22_Stop, + + cs43l22_SetFrequency, + cs43l22_SetVolume, + cs43l22_SetMute, + cs43l22_SetOutputMode, + cs43l22_Reset, +}; + +static uint8_t Is_cs43l22_Stop = 1; + +volatile uint8_t OutputDev = 0; + +/** + * @} + */ + +/** @defgroup CS43L22_Function_Prototypes + * @{ + */ +static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); +/** + * @} + */ + +/** @defgroup CS43L22_Private_Functions + * @{ + */ + +/** + * @brief Initializes the audio codec and the control interface. + * @param DeviceAddr: Device address on communication Bus. + * @param OutputDevice: can be OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, + * OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO . + * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max)) + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq) +{ + uint32_t counter = 0; + + /* Initialize the Control interface of the Audio Codec */ + AUDIO_IO_Init(); + + /* Keep Codec powered OFF */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01); + + /*Save Output device for mute ON/OFF procedure*/ + switch (OutputDevice) + { + case OUTPUT_DEVICE_SPEAKER: + OutputDev = 0xFA; + break; + + case OUTPUT_DEVICE_HEADPHONE: + OutputDev = 0xAF; + break; + + case OUTPUT_DEVICE_BOTH: + OutputDev = 0xAA; + break; + + case OUTPUT_DEVICE_AUTO: + OutputDev = 0x05; + break; + + default: + OutputDev = 0x05; + break; + } + + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev); + + /* Clock configuration: Auto detection */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_CLOCKING_CTL, 0x81); + + /* Set the Slave Mode and the audio Standard */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_INTERFACE_CTL1, CODEC_STANDARD); + + /* Set the Master volume */ + counter += cs43l22_SetVolume(DeviceAddr, Volume); + + /* If the Speaker is enabled, set the Mono mode and volume attenuation level */ + if(OutputDevice != OUTPUT_DEVICE_HEADPHONE) + { + /* Set the Speaker Mono mode */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PLAYBACK_CTL2, 0x06); + + /* Set the Speaker attenuation level */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_A_VOL, 0x00); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_B_VOL, 0x00); + } + + /* Additional configuration for the CODEC. These configurations are done to reduce + the time needed for the Codec to power off. If these configurations are removed, + then a long delay should be added between powering off the Codec and switching + off the I2S peripheral MCLK clock (which is the operating clock for Codec). + If this delay is not inserted, then the codec will not shut down properly and + it results in high noise after shut down. */ + + /* Disable the analog soft ramp */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_ANALOG_ZC_SR_SETT, 0x00); + /* Disable the digital soft ramp */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04); + /* Disable the limiter attack level */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_LIMIT_CTL1, 0x00); + /* Adjust Bass and Treble levels */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_TONE_CTL, 0x0F); + /* Adjust PCM volume level */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMA_VOL, 0x0A); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMB_VOL, 0x0A); + + /* Return communication control value */ + return counter; +} + +/** + * @brief Deinitializes the audio codec. + * @param None + * @retval None + */ +void cs43l22_DeInit(void) +{ + /* Deinitialize Audio Codec interface */ + AUDIO_IO_DeInit(); +} + +/** + * @brief Get the CS43L22 ID. + * @param DeviceAddr: Device address on communication Bus. + * @retval The CS43L22 ID + */ +uint32_t cs43l22_ReadID(uint16_t DeviceAddr) +{ + uint8_t Value; + /* Initialize the Control interface of the Audio Codec */ + AUDIO_IO_Init(); + + Value = AUDIO_IO_Read(DeviceAddr, CS43L22_CHIPID_ADDR); + Value = (Value & CS43L22_ID_MASK); + + return((uint32_t) Value); +} + +/** + * @brief Start the audio Codec play feature. + * @note For this codec no Play options are required. + * @param DeviceAddr: Device address on communication Bus. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Play(uint16_t DeviceAddr, uint16_t* pBuffer, uint16_t Size) +{ + uint32_t counter = 0; + + if(Is_cs43l22_Stop == 1) + { + /* Enable the digital soft ramp */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x06); + + /* Enable Output device */ + counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF); + + /* Power on the Codec */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E); + Is_cs43l22_Stop = 0; + } + + /* Return communication control value */ + return counter; +} + +/** + * @brief Pauses playing on the audio codec. + * @param DeviceAddr: Device address on communication Bus. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Pause(uint16_t DeviceAddr) +{ + uint32_t counter = 0; + + /* Pause the audio file playing */ + /* Mute the output first */ + counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON); + + /* Put the Codec in Power save mode */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01); + + return counter; +} + +/** + * @brief Resumes playing on the audio codec. + * @param DeviceAddr: Device address on communication Bus. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Resume(uint16_t DeviceAddr) +{ + uint32_t counter = 0; + volatile uint32_t index = 0x00; + /* Resumes the audio file playing */ + /* Unmute the output first */ + counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF); + + for(index = 0x00; index < 0xFF; index++); + + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev); + + /* Exit the Power save mode */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E); + + return counter; +} + +/** + * @brief Stops audio Codec playing. It powers down the codec. + * @param DeviceAddr: Device address on communication Bus. + * @param CodecPdwnMode: selects the power down mode. + * - CODEC_PDWN_HW: Physically power down the codec. When resuming from this + * mode, the codec is set to default configuration + * (user should re-Initialize the codec in order to + * play again the audio stream). + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Stop(uint16_t DeviceAddr, uint32_t CodecPdwnMode) +{ + uint32_t counter = 0; + + /* Mute the output first */ + counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON); + + /* Disable the digital soft ramp */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04); + + /* Power down the DAC and the speaker (PMDAC and PMSPK bits)*/ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9F); + + Is_cs43l22_Stop = 1; + return counter; +} + +/** + * @brief Sets higher or lower the codec volume level. + * @param DeviceAddr: Device address on communication Bus. + * @param Volume: a byte value from 0 to 255 (refer to codec registers + * description for more details). + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_SetVolume(uint16_t DeviceAddr, uint8_t Volume) +{ + uint32_t counter = 0; + uint8_t convertedvol = VOLUME_CONVERT(Volume); + + if(Volume > 0xE6) + { + /* Set the Master volume */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol - 0xE7); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol - 0xE7); + } + else + { + /* Set the Master volume */ + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol + 0x19); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol + 0x19); + } + + return counter; +} + +/** + * @brief Sets new frequency. + * @param DeviceAddr: Device address on communication Bus. + * @param AudioFreq: Audio frequency used to play the audio stream. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_SetFrequency(uint16_t DeviceAddr, uint32_t AudioFreq) +{ + return 0; +} + +/** + * @brief Enables or disables the mute feature on the audio codec. + * @param DeviceAddr: Device address on communication Bus. + * @param Cmd: AUDIO_MUTE_ON to enable the mute or AUDIO_MUTE_OFF to disable the + * mute mode. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_SetMute(uint16_t DeviceAddr, uint32_t Cmd) +{ + uint32_t counter = 0; + + /* Set the Mute mode */ + if(Cmd == AUDIO_MUTE_ON) + { + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFF); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x01); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x01); + } + else /* AUDIO_MUTE_OFF Disable the Mute */ + { + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x00); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x00); + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev); + } + return counter; +} + +/** + * @brief Switch dynamically (while audio file is played) the output target + * (speaker or headphone). + * @note This function modifies a global variable of the audio codec driver: OutputDev. + * @param DeviceAddr: Device address on communication Bus. + * @param Output: specifies the audio output target: OUTPUT_DEVICE_SPEAKER, + * OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_SetOutputMode(uint16_t DeviceAddr, uint8_t Output) +{ + uint32_t counter = 0; + + switch (Output) + { + case OUTPUT_DEVICE_SPEAKER: + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFA); /* SPK always ON & HP always OFF */ + OutputDev = 0xFA; + break; + + case OUTPUT_DEVICE_HEADPHONE: + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAF); /* SPK always OFF & HP always ON */ + OutputDev = 0xAF; + break; + + case OUTPUT_DEVICE_BOTH: + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAA); /* SPK always ON & HP always ON */ + OutputDev = 0xAA; + break; + + case OUTPUT_DEVICE_AUTO: + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05); /* Detect the HP or the SPK automatically */ + OutputDev = 0x05; + break; + + default: + counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05); /* Detect the HP or the SPK automatically */ + OutputDev = 0x05; + break; + } + return counter; +} + +/** + * @brief Resets cs43l22 registers. + * @param DeviceAddr: Device address on communication Bus. + * @retval 0 if correct communication, else wrong communication + */ +uint32_t cs43l22_Reset(uint16_t DeviceAddr) +{ + return 0; +} + +/** + * @brief Writes/Read a single data. + * @param Addr: I2C address + * @param Reg: Reg address + * @param Value: Data to be written + * @retval None + */ +static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) +{ + uint32_t result = 0; + + AUDIO_IO_Write(Addr, Reg, Value); + +#ifdef VERIFY_WRITTENDATA + /* Verify that the data has been correctly written */ + result = (AUDIO_IO_Read(Addr, Reg) == Value)? 0:1; +#endif /* VERIFY_WRITTENDATA */ + + return result; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/bsp/cs43l22.h b/port/stm32-f4discovery-usb/bsp/cs43l22.h new file mode 100644 index 000000000..d66ce8572 --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/cs43l22.h @@ -0,0 +1,228 @@ +/** + ****************************************************************************** + * @file cs43l22.h + * @author MCD Application Team + * @version V2.0.2 + * @date 06-October-2015 + * @brief This file contains all the functions prototypes for the cs43l22.c driver. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2015 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __CS43L22_H +#define __CS43L22_H + +/* Includes ------------------------------------------------------------------*/ +#include "audio.h" + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup Component + * @{ + */ + +/** @addtogroup CS43L22 + * @{ + */ + +/** @defgroup CS43L22_Exported_Types + * @{ + */ + +/** + * @} + */ + +/** @defgroup CS43L22_Exported_Constants + * @{ + */ + +/******************************************************************************/ +/*************************** Codec User defines ******************************/ +/******************************************************************************/ +/* Codec output DEVICE */ +#define OUTPUT_DEVICE_SPEAKER 1 +#define OUTPUT_DEVICE_HEADPHONE 2 +#define OUTPUT_DEVICE_BOTH 3 +#define OUTPUT_DEVICE_AUTO 4 + +/* Volume Levels values */ +#define DEFAULT_VOLMIN 0x00 +#define DEFAULT_VOLMAX 0xFF +#define DEFAULT_VOLSTEP 0x04 + +#define AUDIO_PAUSE 0 +#define AUDIO_RESUME 1 + +/* Codec POWER DOWN modes */ +#define CODEC_PDWN_HW 1 +#define CODEC_PDWN_SW 2 + +/* MUTE commands */ +#define AUDIO_MUTE_ON 1 +#define AUDIO_MUTE_OFF 0 + +/* AUDIO FREQUENCY */ +#define AUDIO_FREQUENCY_192K ((uint32_t)192000) +#define AUDIO_FREQUENCY_96K ((uint32_t)96000) +#define AUDIO_FREQUENCY_48K ((uint32_t)48000) +#define AUDIO_FREQUENCY_44K ((uint32_t)44100) +#define AUDIO_FREQUENCY_32K ((uint32_t)32000) +#define AUDIO_FREQUENCY_22K ((uint32_t)22050) +#define AUDIO_FREQUENCY_16K ((uint32_t)16000) +#define AUDIO_FREQUENCY_11K ((uint32_t)11025) +#define AUDIO_FREQUENCY_8K ((uint32_t)8000) + +/** CS43l22 Registers ***/ +#define CS43L22_REG_ID 0x01 +#define CS43L22_REG_POWER_CTL1 0x02 +#define CS43L22_REG_POWER_CTL2 0x04 +#define CS43L22_REG_CLOCKING_CTL 0x05 +#define CS43L22_REG_INTERFACE_CTL1 0x06 +#define CS43L22_REG_INTERFACE_CTL2 0x07 +#define CS43L22_REG_PASSTHR_A_SELECT 0x08 +#define CS43L22_REG_PASSTHR_B_SELECT 0x09 +#define CS43L22_REG_ANALOG_ZC_SR_SETT 0x0A +#define CS43L22_REG_PASSTHR_GANG_CTL 0x0C +#define CS43L22_REG_PLAYBACK_CTL1 0x0D +#define CS43L22_REG_MISC_CTL 0x0E +#define CS43L22_REG_PLAYBACK_CTL2 0x0F +#define CS43L22_REG_PASSTHR_A_VOL 0x14 +#define CS43L22_REG_PASSTHR_B_VOL 0x15 +#define CS43L22_REG_PCMA_VOL 0x1A +#define CS43L22_REG_PCMB_VOL 0x1B +#define CS43L22_REG_BEEP_FREQ_ON_TIME 0x1C +#define CS43L22_REG_BEEP_VOL_OFF_TIME 0x1D +#define CS43L22_REG_BEEP_TONE_CFG 0x1E +#define CS43L22_REG_TONE_CTL 0x1F +#define CS43L22_REG_MASTER_A_VOL 0x20 +#define CS43L22_REG_MASTER_B_VOL 0x21 +#define CS43L22_REG_HEADPHONE_A_VOL 0x22 +#define CS43L22_REG_HEADPHONE_B_VOL 0x23 +#define CS43L22_REG_SPEAKER_A_VOL 0x24 +#define CS43L22_REG_SPEAKER_B_VOL 0x25 +#define CS43L22_REG_CH_MIXER_SWAP 0x26 +#define CS43L22_REG_LIMIT_CTL1 0x27 +#define CS43L22_REG_LIMIT_CTL2 0x28 +#define CS43L22_REG_LIMIT_ATTACK_RATE 0x29 +#define CS43L22_REG_OVF_CLK_STATUS 0x2E +#define CS43L22_REG_BATT_COMPENSATION 0x2F +#define CS43L22_REG_VP_BATTERY_LEVEL 0x30 +#define CS43L22_REG_SPEAKER_STATUS 0x31 +#define CS43L22_REG_TEMPMONITOR_CTL 0x32 +#define CS43L22_REG_THERMAL_FOLDBACK 0x33 +#define CS43L22_REG_CHARGE_PUMP_FREQ 0x34 + +/******************************************************************************/ +/****************************** REGISTER MAPPING ******************************/ +/******************************************************************************/ +/** + * @brief CS43L22 ID + */ +#define CS43L22_ID 0xE0 +#define CS43L22_ID_MASK 0xF8 +/** + * @brief Chip ID Register: Chip I.D. and Revision Register + * Read only register + * Default value: 0x01 + * [7:3] CHIPID[4:0]: I.D. code for the CS43L22. + * Default value: 11100b + * [2:0] REVID[2:0]: CS43L22 revision level. + * Default value: + * 000 - Rev A0 + * 001 - Rev A1 + * 010 - Rev B0 + * 011 - Rev B1 + */ +#define CS43L22_CHIPID_ADDR 0x01 + +/** + * @} + */ + +/** @defgroup CS43L22_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup CS43L22_Exported_Functions + * @{ + */ + +/*------------------------------------------------------------------------------ + Audio Codec functions +------------------------------------------------------------------------------*/ +/* High Layer codec functions */ +uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq); +void cs43l22_DeInit(void); +uint32_t cs43l22_ReadID(uint16_t DeviceAddr); +uint32_t cs43l22_Play(uint16_t DeviceAddr, uint16_t* pBuffer, uint16_t Size); +uint32_t cs43l22_Pause(uint16_t DeviceAddr); +uint32_t cs43l22_Resume(uint16_t DeviceAddr); +uint32_t cs43l22_Stop(uint16_t DeviceAddr, uint32_t Cmd); +uint32_t cs43l22_SetVolume(uint16_t DeviceAddr, uint8_t Volume); +uint32_t cs43l22_SetFrequency(uint16_t DeviceAddr, uint32_t AudioFreq); +uint32_t cs43l22_SetMute(uint16_t DeviceAddr, uint32_t Cmd); +uint32_t cs43l22_SetOutputMode(uint16_t DeviceAddr, uint8_t Output); +uint32_t cs43l22_Reset(uint16_t DeviceAddr); + +/* AUDIO IO functions */ +void AUDIO_IO_Init(void); +void AUDIO_IO_DeInit(void); +void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); +uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg); + +/* Audio driver structure */ +extern AUDIO_DrvTypeDef cs43l22_drv; + +#endif /* __CS43L22_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.c b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.c new file mode 100644 index 000000000..c26f5d142 --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.c @@ -0,0 +1,703 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery.c + * @author MCD Application Team + * @version V2.1.2 + * @date 27-January-2017 + * @brief This file provides set of firmware functions to manage Leds and + * push-button available on STM32F4-Discovery Kit from STMicroelectronics. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4_discovery.h" + +/** @defgroup BSP BSP + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY STM32F4 DISCOVERY + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL STM32F4 DISCOVERY LOW LEVEL + * @brief This file provides set of firmware functions to manage Leds and push-button + * available on STM32F4-Discovery Kit from STMicroelectronics. + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32F4 DISCOVERY LOW LEVEL Private TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines STM32F4 DISCOVERY LOW LEVEL Private Defines + * @{ + */ + + /** + * @brief STM32F4 DISCO BSP Driver version number V2.1.2 + */ +#define __STM32F4_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */ +#define __STM32F4_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ +#define __STM32F4_DISCO_BSP_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */ +#define __STM32F4_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ +#define __STM32F4_DISCO_BSP_VERSION ((__STM32F4_DISCO_BSP_VERSION_MAIN << 24)\ + |(__STM32F4_DISCO_BSP_VERSION_SUB1 << 16)\ + |(__STM32F4_DISCO_BSP_VERSION_SUB2 << 8 )\ + |(__STM32F4_DISCO_BSP_VERSION_RC)) +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros STM32F4 DISCOVERY LOW LEVEL Private Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables STM32F4 DISCOVERY LOW LEVEL Private Variables + * @{ + */ +GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT, + LED3_GPIO_PORT, + LED5_GPIO_PORT, + LED6_GPIO_PORT}; +const uint16_t GPIO_PIN[LEDn] = {LED4_PIN, + LED3_PIN, + LED5_PIN, + LED6_PIN}; + +GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT}; +const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN}; +const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn}; + +uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /* 0x01) + { + WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; + } + /* Set chip select Low at the start of the transmission */ + ACCELERO_CS_LOW(); + + /* Send the Address of the indexed register */ + SPIx_WriteRead(WriteAddr); + + /* Send the data that will be written into the device (MSB First) */ + while(NumByteToWrite >= 0x01) + { + SPIx_WriteRead(*pBuffer); + NumByteToWrite--; + pBuffer++; + } + + /* Set chip select High at the end of the transmission */ + ACCELERO_CS_HIGH(); +} + +/** + * @brief Reads a block of data from the Accelerometer. + * @param pBuffer: pointer to the buffer that receives the data read from the Accelerometer. + * @param ReadAddr: Accelerometer's internal address to read from. + * @param NumByteToRead: number of bytes to read from the Accelerometer. + */ +void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) +{ + if(NumByteToRead > 0x01) + { + ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); + } + else + { + ReadAddr |= (uint8_t)READWRITE_CMD; + } + /* Set chip select Low at the start of the transmission */ + ACCELERO_CS_LOW(); + + /* Send the Address of the indexed register */ + SPIx_WriteRead(ReadAddr); + + /* Receive the data that will be read from the device (MSB First) */ + while(NumByteToRead > 0x00) + { + /* Send dummy byte (0x00) to generate the SPI clock to ACCELEROMETER (Slave device) */ + *pBuffer = SPIx_WriteRead(DUMMY_BYTE); + NumByteToRead--; + pBuffer++; + } + + /* Set chip select High at the end of the transmission */ + ACCELERO_CS_HIGH(); +} + +/********************************* LINK AUDIO *********************************/ + +/** + * @brief Initializes Audio low level. + */ +void AUDIO_IO_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + /* Enable Reset GPIO Clock */ + AUDIO_RESET_GPIO_CLK_ENABLE(); + + /* Audio reset pin configuration */ + GPIO_InitStruct.Pin = AUDIO_RESET_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FAST; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct); + + I2Cx_Init(); + + /* Power Down the codec */ + HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET); + + /* Wait for a delay to insure registers erasing */ + HAL_Delay(5); + + /* Power on the codec */ + HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_SET); + + /* Wait for a delay to insure registers erasing */ + HAL_Delay(5); +} + +/** + * @brief DeInitializes Audio low level. + */ +void AUDIO_IO_DeInit(void) +{ + +} + +/** + * @brief Writes a single data. + * @param Addr: I2C address + * @param Reg: Reg address + * @param Value: Data to be written + */ +void AUDIO_IO_Write (uint8_t Addr, uint8_t Reg, uint8_t Value) +{ + I2Cx_WriteData(Addr, Reg, Value); +} + +/** + * @brief Reads a single data. + * @param Addr: I2C address + * @param Reg: Reg address + * @retval Data to be read + */ +uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg) +{ + return I2Cx_ReadData(Addr, Reg); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.h b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.h new file mode 100644 index 000000000..6e010eacc --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery.h @@ -0,0 +1,303 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery.h + * @author MCD Application Team + * @version V2.1.2 + * @date 27-January-2017 + * @brief This file contains definitions for STM32F4-Discovery Kit's Leds and + * push-button hardware resources. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4_DISCOVERY_H +#define __STM32F4_DISCOVERY_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4xx_hal.h" + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Types STM32F4 DISCOVERY LOW LEVEL_Exported_Types + * @{ + */ +typedef enum +{ + LED4 = 0, + LED3 = 1, + LED5 = 2, + LED6 = 3 +} Led_TypeDef; + +typedef enum +{ + BUTTON_KEY = 0, +} Button_TypeDef; + +typedef enum +{ + BUTTON_MODE_GPIO = 0, + BUTTON_MODE_EXTI = 1 +} ButtonMode_TypeDef; +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Constants STM32F4 DISCOVERY LOW LEVEL Exported Constants + * @{ + */ + +/** +* @brief Define for STM32F4_DISCOVERY board +*/ +#if !defined (USE_STM32F4_DISCO) + #define USE_STM32F4_DISCO +#endif + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_LED STM32F4 DISCOVERY LOW LEVEL LED + * @{ + */ +#define LEDn 4 + +#define LED4_PIN GPIO_PIN_12 +#define LED4_GPIO_PORT GPIOD +#define LED4_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() +#define LED4_GPIO_CLK_DISABLE() __HAL_RCC_GPIOD_CLK_DISABLE() + +#define LED3_PIN GPIO_PIN_13 +#define LED3_GPIO_PORT GPIOD +#define LED3_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() +#define LED3_GPIO_CLK_DISABLE() __HAL_RCC_GPIOD_CLK_DISABLE() + +#define LED5_PIN GPIO_PIN_14 +#define LED5_GPIO_PORT GPIOD +#define LED5_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() +#define LED5_GPIO_CLK_DISABLE() __HAL_RCC_GPIOD_CLK_DISABLE() + +#define LED6_PIN GPIO_PIN_15 +#define LED6_GPIO_PORT GPIOD +#define LED6_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() +#define LED6_GPIO_CLK_DISABLE() __HAL_RCC_GPIOD_CLK_DISABLE() + +#define LEDx_GPIO_CLK_ENABLE(__INDEX__) do{if((__INDEX__) == 0) LED4_GPIO_CLK_ENABLE(); else \ + if((__INDEX__) == 1) LED3_GPIO_CLK_ENABLE(); else \ + if((__INDEX__) == 2) LED5_GPIO_CLK_ENABLE(); else \ + if((__INDEX__) == 3) LED6_GPIO_CLK_ENABLE(); \ + }while(0) + +#define LEDx_GPIO_CLK_DISABLE(__INDEX__) do{if((__INDEX__) == 0) LED4_GPIO_CLK_DISABLE(); else \ + if((__INDEX__) == 1) LED3_GPIO_CLK_DISABLE(); else \ + if((__INDEX__) == 2) LED5_GPIO_CLK_DISABLE(); else \ + if((__INDEX__) == 3) LED6_GPIO_CLK_DISABLE(); \ + }while(0) +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUTTON STM32F4 DISCOVERY LOW LEVEL BUTTON + * @{ + */ +#define BUTTONn 1 + +/** + * @brief Wakeup push-button + */ +#define KEY_BUTTON_PIN GPIO_PIN_0 +#define KEY_BUTTON_GPIO_PORT GPIOA +#define KEY_BUTTON_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() +#define KEY_BUTTON_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE() +#define KEY_BUTTON_EXTI_IRQn EXTI0_IRQn + +#define BUTTONx_GPIO_CLK_ENABLE(__INDEX__) do{if((__INDEX__) == 0) KEY_BUTTON_GPIO_CLK_ENABLE(); \ + }while(0) + +#define BUTTONx_GPIO_CLK_DISABLE(__INDEX__) do{if((__INDEX__) == 0) KEY_BUTTON_GPIO_CLK_DISABLE(); \ + }while(0) +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUS STM32F4 DISCOVERY LOW LEVEL BUS + * @{ + */ + +/*############################### SPI1 #######################################*/ +#define DISCOVERY_SPIx SPI1 +#define DISCOVERY_SPIx_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE() +#define DISCOVERY_SPIx_GPIO_PORT GPIOA /* GPIOA */ +#define DISCOVERY_SPIx_AF GPIO_AF5_SPI1 +#define DISCOVERY_SPIx_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() +#define DISCOVERY_SPIx_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE() +#define DISCOVERY_SPIx_SCK_PIN GPIO_PIN_5 /* PA.05 */ +#define DISCOVERY_SPIx_MISO_PIN GPIO_PIN_6 /* PA.06 */ +#define DISCOVERY_SPIx_MOSI_PIN GPIO_PIN_7 /* PA.07 */ + +/* Maximum Timeout values for flags waiting loops. These timeouts are not based + on accurate values, they just guarantee that the application will not remain + stuck if the SPI communication is corrupted. + You may modify these timeout values depending on CPU frequency and application + conditions (interrupts routines ...). */ +#define SPIx_TIMEOUT_MAX 0x1000 /*
© COPYRIGHT(c) 2017 STMicroelectronics
+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/*============================================================================== + User NOTES +1. How To use this driver: +-------------------------- + - This driver supports STM32F4xx devices on STM32F4-Discovery Kit: + a) to play an audio file (all functions names start by BSP_AUDIO_OUT_xxx) + b) to record an audio file through MP45DT02, ST MEMS (all functions names start by AUDIO_IN_xxx) + +a) PLAY A FILE: +============== + + Call the function BSP_AUDIO_OUT_Init( + OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER, + OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_AUTO or + OUTPUT_DEVICE_BOTH) + Volume: initial volume to be set (0 is min (mute), 100 is max (100%) + AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...) + this parameter is relative to the audio file/stream type. + ) + This function configures all the hardware required for the audio application (codec, I2C, I2S, + GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK. + If the returned value is different from 0 or the function is stuck then the communication with + the codec (try to un-plug the power or reset device in this case). + - OUTPUT_DEVICE_SPEAKER: only speaker will be set as output for the audio stream. + - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream. + - OUTPUT_DEVICE_AUTO: Selection of output device is made through external switch (implemented + into the audio jack on the discovery board). When the Headphone is connected it is used + as output. When the headphone is disconnected from the audio jack, the output is + automatically switched to Speaker. + - OUTPUT_DEVICE_BOTH: both Speaker and Headphone are used as outputs for the audio stream + at the same time. + + Call the function BSP_AUDIO_OUT_Play( + pBuffer: pointer to the audio data file address + Size: size of the buffer to be sent in Bytes + ) + to start playing (for the first time) from the audio file/stream. + + Call the function BSP_AUDIO_OUT_Pause() to pause playing + + Call the function BSP_AUDIO_OUT_Resume() to resume playing. + Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called + for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case). + Note. This function should be called only when the audio file is played or paused (not stopped). + + For each mode, you may need to implement the relative callback functions into your code. + The Callback functions are named BSP_AUDIO_OUT_XXXCallBack() and only their prototypes are declared in + the stm32f4_discovery_audio.h file. (refer to the example for more details on the callbacks implementations) + + To Stop playing, to modify the volume level, the frequency or to mute, use the functions + BSP_AUDIO_OUT_Stop(), BSP_AUDIO_OUT_SetVolume(), AUDIO_OUT_SetFrequency() BSP_AUDIO_OUT_SetOutputMode and BSP_AUDIO_OUT_SetMute(). + + The driver API and the callback functions are at the end of the stm32f4_discovery_audio.h file. + +Driver architecture: +-------------------- + + This driver provide the High Audio Layer: consists of the function API exported in the stm32f4_discovery_audio.h file + (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...) + + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/ + providing the audio file/stream. These functions are also included as local functions into + the stm32f4_discovery_audio.c file (I2S3_Init()...) + +Known Limitations: +------------------- + 1- When using the Speaker, if the audio file quality is not high enough, the speaker output + may produce high and uncomfortable noise level. To avoid this issue, to use speaker + output properly, try to increase audio file sampling rate (typically higher than 48KHz). + This operation will lead to larger file size. + 2- Communication with the audio codec (through I2C) may be corrupted if it is interrupted by some + user interrupt routines (in this case, interrupts could be disabled just before the start of + communication then re-enabled when it is over). Note that this communication is only done at + the configuration phase (BSP_AUDIO_OUT_Init() or BSP_AUDIO_OUT_Stop()) and when Volume control modification is + performed (BSP_AUDIO_OUT_SetVolume() or BSP_AUDIO_OUT_SetMute()or BSP_AUDIO_OUT_SetOutputMode()). + When the audio data is played, no communication is required with the audio codec. + 3- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size, + File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file. + 4- Supports only Stereo audio streaming. To play mono audio streams, each data should be sent twice + on the I2S or should be duplicated on the source buffer. Or convert the stream in stereo before playing. + 5- Supports only 16-bits audio data size. + +b) RECORD A FILE: +================ + + Call the function BSP_AUDIO_IN_Init( + AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...) + ) + This function configures all the hardware required for the audio application (I2S, + GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK. + + + Call the function BSP_AUDIO_IN_Record( + pbuf Main buffer pointer for the recorded data storing + size Current size of the recorded buffer + ) + to start recording from the microphone. + + + User needs to implement user callbacks to retrieve data saved in the record buffer + (AUDIO_IN_RxHalfCpltCallback/BSP_AUDIO_IN_ReceiveComplete_CallBack) + + + Call the function AUDIO_IN_STOP() to stop recording + +==============================================================================*/ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4_discovery_audio.h" +#include "pdm_filter.h" +#include "btstack_config.h" + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO STM32F4 DISCOVERY AUDIO + * @brief This file includes the low layer audio driver available on STM32F4-Discovery + * discovery board. + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Types STM32F4 DISCOVERY AUDIO Private Types + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Defines STM32F4 DISCOVERY AUDIO Private Defines + * @{ + */ +/* These PLL parameters are valid when the f(VCO clock) = 1Mhz */ +static const uint32_t I2SFreq[8] = {8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000}; +static const uint32_t I2SPLLN[8] = {256, 429, 213, 429, 426, 271, 258, 344}; +static const uint32_t I2SPLLR[8] = {5, 4, 4, 4, 4, 6, 3, 1}; +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Macros STM32F4 DISCOVERY AUDIO Private Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Variables STM32F4 DISCOVERY AUDIO Private Variables + * @{ + */ +/*##### PLAY #####*/ +static AUDIO_DrvTypeDef *pAudioDrv; +I2S_HandleTypeDef hAudioOutI2s; + +/*### RECORDER ###*/ +I2S_HandleTypeDef hAudioInI2s; + +static PDMFilter_InitStruct Filter[DEFAULT_AUDIO_IN_CHANNEL_NBR]; +__IO uint16_t AudioInVolume = DEFAULT_AUDIO_IN_VOLUME; +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Function_Prototypes STM32F4 DISCOVERY AUDIO Private Function Prototypes + * @{ + */ +static uint8_t I2S2_Init(uint32_t AudioFreq); +static uint8_t I2S3_Init(uint32_t AudioFreq); +static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbr); + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Private_Functions STM32F4 DISCOVERY AUDIO OUT Private Functions + * @{ + */ + +/** + * @brief Configures the audio peripherals. + * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, + * OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO . + * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max)) + * @param AudioFreq: Audio frequency used to play the audio stream. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq) +{ + uint8_t ret = AUDIO_OK; + + /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */ + BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL); + + /* I2S data transfer preparation: + Prepare the Media to be used for the audio transfer from memory to I2S peripheral */ + hAudioOutI2s.Instance = I2S3; + if(HAL_I2S_GetState(&hAudioOutI2s) == HAL_I2S_STATE_RESET) + { + /* Init the I2S MSP: this __weak function can be redefined by the application*/ + BSP_AUDIO_OUT_MspInit(&hAudioOutI2s, NULL); + } + + /* I2S data transfer preparation: + Prepare the Media to be used for the audio transfer from memory to I2S peripheral */ + /* Configure the I2S peripheral */ + if(I2S3_Init(AudioFreq) != AUDIO_OK) + { + ret = AUDIO_ERROR; + } + + if(ret == AUDIO_OK) + { + /* Retieve audio codec identifier */ + if(((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID) + { + /* Initialize the audio driver structure */ + pAudioDrv = &cs43l22_drv; + } + else + { + ret = AUDIO_ERROR; + } + } + + if(ret == AUDIO_OK) + { + pAudioDrv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq); + } + + return ret; +} + +/** + * @brief Starts playing audio stream from a data buffer for a determined size. + * @param pBuffer: Pointer to the buffer + * @param Size: Number of audio data BYTES. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size) +{ + /* Call the audio Codec Play function */ + if(pAudioDrv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Update the Media layer and enable it for play */ + HAL_I2S_Transmit_DMA(&hAudioOutI2s, pBuffer, DMA_MAX(Size/AUDIODATA_SIZE)); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Sends n-Bytes on the I2S interface. + * @param pData: Pointer to data address + * @param Size: Number of data to be written + */ +void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size) +{ + HAL_I2S_Transmit_DMA(&hAudioOutI2s, pData, Size); +} + +/** + * @brief Pauses the audio file stream. In case of using DMA, the DMA Pause + * feature is used. + * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only the + * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() + * function for resume could lead to unexpected behavior). + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_Pause(void) +{ + /* Call the Audio Codec Pause/Resume function */ + if(pAudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Call the Media layer pause function */ + HAL_I2S_DMAPause(&hAudioOutI2s); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Resumes the audio file streaming. + * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only + * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() + * function for resume could lead to unexpected behavior). + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_Resume(void) +{ + /* Call the Audio Codec Pause/Resume function */ + if(pAudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Call the Media layer resume function */ + HAL_I2S_DMAResume(&hAudioOutI2s); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Stops audio playing and Power down the Audio Codec. + * @param Option: could be one of the following parameters + * - CODEC_PDWN_HW: completely shut down the codec (physically). + * Then need to reconfigure the Codec after power on. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option) +{ + /* Call DMA Stop to disable DMA stream before stopping codec */ + HAL_I2S_DMAStop(&hAudioOutI2s); + + /* Call Audio Codec Stop function */ + if(pAudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) + { + return AUDIO_ERROR; + } + else + { + if(Option == CODEC_PDWN_HW) + { + /* Wait at least 1ms */ + HAL_Delay(1); + + /* Reset the pin */ + HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET); + } + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Controls the current audio volume level. + * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for + * Mute and 100 for Max volume level). + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume) +{ + /* Call the codec volume control function with converted volume value */ + if(pAudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Enables or disables the MUTE mode by software + * @param Cmd: could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to + * unmute the codec and restore previous volume level. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd) +{ + /* Call the Codec Mute function */ + if(pAudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Switch dynamically (while audio file is played) the output target + * (speaker or headphone). + * @note This function modifies a global variable of the audio codec driver: OutputDev. + * @param Output: specifies the audio output target: OUTPUT_DEVICE_SPEAKER, + * OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output) +{ + /* Call the Codec output Device function */ + if(pAudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) + { + return AUDIO_ERROR; + } + else + { + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; + } +} + +/** + * @brief Update the audio frequency. + * @param AudioFreq: Audio frequency used to play the audio stream. + * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the + * audio frequency. + */ +void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq) +{ + /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */ + BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL); + + /* Update the I2S audio frequency configuration */ + I2S3_Init(AudioFreq); +} + +/** + * @brief Tx Transfer completed callbacks. + * @param hi2s: I2S handle + */ +void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) +{ + if(hi2s->Instance == I2S3) + { + /* Call the user function which will manage directly transfer complete */ + BSP_AUDIO_OUT_TransferComplete_CallBack(); + } +} + +/** + * @brief Tx Half Transfer completed callbacks. + * @param hi2s: I2S handle + */ +void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) +{ + if(hi2s->Instance == I2S3) + { + /* Manage the remaining file size and new address offset: This function should + be coded by user (its prototype is already declared in stm32f4_discovery_audio.h) */ + BSP_AUDIO_OUT_HalfTransfer_CallBack(); + } +} + +/** + * @brief Clock Config. + * @param hi2s: might be required to set audio peripheral predivider if any. + * @param AudioFreq: Audio frequency used to play the audio stream. + * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency() + * Being __weak it can be overwritten by the application + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params) +{ + RCC_PeriphCLKInitTypeDef rccclkinit; + uint8_t index = 0, freqindex = 0xFF; + + for(index = 0; index < 8; index++) + { + if(I2SFreq[index] == AudioFreq) + { + freqindex = index; + } + } + /* Enable PLLI2S clock */ + HAL_RCCEx_GetPeriphCLKConfig(&rccclkinit); + /* PLLI2S_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */ + + // BK use table if frequency found in table, otherwise use same settings as for 48 kHz + if (freqindex != 0xFF) + { + /* I2S clock config + PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) � (PLLI2SN/PLLM) + I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */ + rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S; + rccclkinit.PLLI2S.PLLI2SN = I2SPLLN[freqindex]; + rccclkinit.PLLI2S.PLLI2SR = I2SPLLR[freqindex]; + HAL_RCCEx_PeriphCLKConfig(&rccclkinit); + } + else + { + /* I2S clock config + PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) � (PLLI2SN/PLLM) + I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */ + rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S; + rccclkinit.PLLI2S.PLLI2SN = 258; + rccclkinit.PLLI2S.PLLI2SR = 3; + HAL_RCCEx_PeriphCLKConfig(&rccclkinit); + } +} + +/** + * @brief AUDIO OUT I2S MSP Init. + * @param hi2s: might be required to set audio peripheral predivider if any. + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params) +{ + static DMA_HandleTypeDef hdma_i2sTx; + GPIO_InitTypeDef GPIO_InitStruct; + + /* Enable I2S3 clock */ + I2S3_CLK_ENABLE(); + + /*** Configure the GPIOs ***/ + /* Enable I2S GPIO clocks */ + I2S3_SCK_SD_CLK_ENABLE(); + I2S3_WS_CLK_ENABLE(); + + /* I2S3 pins configuration: WS, SCK and SD pins ----------------------------*/ + GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FAST; + GPIO_InitStruct.Alternate = I2S3_SCK_SD_WS_AF; + HAL_GPIO_Init(I2S3_SCK_SD_GPIO_PORT, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = I2S3_WS_PIN ; + HAL_GPIO_Init(I2S3_WS_GPIO_PORT, &GPIO_InitStruct); + + /* I2S3 pins configuration: MCK pin */ + I2S3_MCK_CLK_ENABLE(); + GPIO_InitStruct.Pin = I2S3_MCK_PIN; + HAL_GPIO_Init(I2S3_MCK_GPIO_PORT, &GPIO_InitStruct); + + /* Enable the I2S DMA clock */ + I2S3_DMAx_CLK_ENABLE(); + + if(hi2s->Instance == I2S3) + { + /* Configure the hdma_i2sTx handle parameters */ + hdma_i2sTx.Init.Channel = I2S3_DMAx_CHANNEL; + hdma_i2sTx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_i2sTx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_i2sTx.Init.MemInc = DMA_MINC_ENABLE; + hdma_i2sTx.Init.PeriphDataAlignment = I2S3_DMAx_PERIPH_DATA_SIZE; + hdma_i2sTx.Init.MemDataAlignment = I2S3_DMAx_MEM_DATA_SIZE; + hdma_i2sTx.Init.Mode = DMA_NORMAL; + + // BK: use circular DMA for hal_audio.h +#ifdef HAVE_HAL_AUDIO + hdma_i2sTx.Init.Mode = DMA_CIRCULAR; +#endif + // BK: use circular DMA (end) + + hdma_i2sTx.Init.Priority = DMA_PRIORITY_HIGH; + hdma_i2sTx.Init.FIFOMode = DMA_FIFOMODE_ENABLE; + hdma_i2sTx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; + hdma_i2sTx.Init.MemBurst = DMA_MBURST_SINGLE; + hdma_i2sTx.Init.PeriphBurst = DMA_PBURST_SINGLE; + + hdma_i2sTx.Instance = I2S3_DMAx_STREAM; + + /* Associate the DMA handle */ + __HAL_LINKDMA(hi2s, hdmatx, hdma_i2sTx); + + /* Deinitialize the Stream for new transfer */ + HAL_DMA_DeInit(&hdma_i2sTx); + + /* Configure the DMA Stream */ + HAL_DMA_Init(&hdma_i2sTx); + } + + /* I2S DMA IRQ Channel configuration */ + HAL_NVIC_SetPriority(I2S3_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0); + HAL_NVIC_EnableIRQ(I2S3_DMAx_IRQ); +} + +/** + * @brief De-Initializes BSP_AUDIO_OUT MSP. + * @param hi2s: might be required to set audio peripheral predivider if any. + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + /* I2S DMA IRQ Channel deactivation */ + HAL_NVIC_DisableIRQ(I2S3_DMAx_IRQ); + + if(hi2s->Instance == I2S3) + { + /* Deinitialize the Stream for new transfer */ + HAL_DMA_DeInit(hi2s->hdmatx); + } + + /* Disable I2S block */ + __HAL_I2S_DISABLE(hi2s); + + /* CODEC_I2S pins configuration: SCK and SD pins */ + GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN; + HAL_GPIO_DeInit(I2S3_SCK_SD_GPIO_PORT, GPIO_InitStruct.Pin); + + /* CODEC_I2S pins configuration: WS pin */ + GPIO_InitStruct.Pin = I2S3_WS_PIN; + HAL_GPIO_DeInit(I2S3_WS_GPIO_PORT, GPIO_InitStruct.Pin); + + /* CODEC_I2S pins configuration: MCK pin */ + GPIO_InitStruct.Pin = I2S3_MCK_PIN; + HAL_GPIO_DeInit(I2S3_MCK_GPIO_PORT, GPIO_InitStruct.Pin); + + /* Disable I2S clock */ + I2S3_CLK_DISABLE(); + + /* GPIO pins clock and DMA clock can be shut down in the applic + by surcgarging this __weak function */ +} + +/** + * @brief Manages the DMA full Transfer complete event. + */ +__weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void) +{ +} + +/** + * @brief Manages the DMA Half Transfer complete event. + */ +__weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) +{ +} + +/** + * @brief Manages the DMA FIFO error event. + */ +__weak void BSP_AUDIO_OUT_Error_CallBack(void) +{ +} + +/******************************************************************************* + Static Functions +*******************************************************************************/ + +/** + * @brief Initializes the Audio Codec audio interface (I2S). + * @param AudioFreq: Audio frequency to be configured for the I2S peripheral. + */ +static uint8_t I2S3_Init(uint32_t AudioFreq) +{ + /* Initialize the hAudioOutI2s Instance parameter */ + hAudioOutI2s.Instance = I2S3; + + /* Disable I2S block */ + __HAL_I2S_DISABLE(&hAudioOutI2s); + + /* I2S3 peripheral configuration */ + hAudioOutI2s.Init.AudioFreq = AudioFreq; + hAudioOutI2s.Init.ClockSource = I2S_CLOCK_PLL; + hAudioOutI2s.Init.CPOL = I2S_CPOL_LOW; + hAudioOutI2s.Init.DataFormat = I2S_DATAFORMAT_16B; + hAudioOutI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE; + hAudioOutI2s.Init.Mode = I2S_MODE_MASTER_TX; + hAudioOutI2s.Init.Standard = I2S_STANDARD; + /* Initialize the I2S peripheral with the structure above */ + if(HAL_I2S_Init(&hAudioOutI2s) != HAL_OK) + { + return AUDIO_ERROR; + } + else + { + return AUDIO_OK; + } +} + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_IN_Private_Functions STM32F4 DISCOVERY AUDIO IN Private Functions + * @{ + */ + +/** + * @brief Initializes wave recording. + * @param AudioFreq: Audio frequency to be configured for the I2S peripheral. + * @param BitRes: Audio frequency to be configured for the I2S peripheral. + * @param ChnlNbr: Audio frequency to be configured for the I2S peripheral. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr) +{ + +#if 0 + // BK: only do PLL clock configuration in sink + /* Configure PLL clock */ + BSP_AUDIO_IN_ClockConfig(&hAudioInI2s, AudioFreq, NULL); +#endif + + /* Configure the PDM library */ + PDMDecoder_Init(AudioFreq, ChnlNbr); + + /* Configure the I2S peripheral */ + hAudioInI2s.Instance = I2S2; + if(HAL_I2S_GetState(&hAudioInI2s) == HAL_I2S_STATE_RESET) + { + /* Initialize the I2S Msp: this __weak function can be rewritten by the application */ + BSP_AUDIO_IN_MspInit(&hAudioInI2s, NULL); + } + + /* Configure the I2S2 */ + I2S2_Init(AudioFreq); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; +} + +/** + * @brief Starts audio recording. + * @param pbuf: Main buffer pointer for the recorded data storing + * @param size: Current size of the recorded buffer + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size) +{ + uint32_t ret = AUDIO_ERROR; + + /* Start the process receive DMA */ + HAL_I2S_Receive_DMA(&hAudioInI2s, pbuf, size); + + /* Return AUDIO_OK when all operations are correctly done */ + ret = AUDIO_OK; + + return ret; +} + +/** + * @brief Stops audio recording. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_Stop(void) +{ + uint32_t ret = AUDIO_ERROR; + + /* Call the Media layer pause function */ + HAL_I2S_DMAStop(&hAudioInI2s); + + /* Return AUDIO_OK when all operations are correctly done */ + ret = AUDIO_OK; + + return ret; +} + +/** + * @brief Pauses the audio file stream. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_Pause(void) +{ + /* Call the Media layer pause function */ + HAL_I2S_DMAPause(&hAudioInI2s); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; +} + +/** + * @brief Resumes the audio file stream. + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_Resume(void) +{ + /* Call the Media layer pause/resume function */ + HAL_I2S_DMAResume(&hAudioInI2s); + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; +} + +/** + * @brief Controls the audio in volume level. + * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for + * Mute and 100 for Max volume level). + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume) +{ + /* Set the Global variable AudioInVolume */ + AudioInVolume = Volume; + + /* Return AUDIO_OK when all operations are correctly done */ + return AUDIO_OK; +} + +/** + * @brief Converts audio format from PDM to PCM. + * @param PDMBuf: Pointer to data PDM buffer + * @param PCMBuf: Pointer to data PCM buffer + * @retval AUDIO_OK if correct communication, else wrong communication + */ +uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t *PDMBuf, uint16_t *PCMBuf) +{ + uint16_t AppPDM[INTERNAL_BUFF_SIZE/2]; + uint32_t index = 0; + + /* PDM Demux */ + for(index = 0; indexInstance == I2S2) + { + /* Configure the hdma_i2sRx handle parameters */ + hdma_i2sRx.Init.Channel = I2S2_DMAx_CHANNEL; + hdma_i2sRx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_i2sRx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_i2sRx.Init.MemInc = DMA_MINC_ENABLE; + hdma_i2sRx.Init.PeriphDataAlignment = I2S2_DMAx_PERIPH_DATA_SIZE; + hdma_i2sRx.Init.MemDataAlignment = I2S2_DMAx_MEM_DATA_SIZE; + hdma_i2sRx.Init.Mode = DMA_CIRCULAR; + hdma_i2sRx.Init.Priority = DMA_PRIORITY_HIGH; + hdma_i2sRx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; + hdma_i2sRx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; + hdma_i2sRx.Init.MemBurst = DMA_MBURST_SINGLE; + hdma_i2sRx.Init.PeriphBurst = DMA_MBURST_SINGLE; + + hdma_i2sRx.Instance = I2S2_DMAx_STREAM; + + /* Associate the DMA handle */ + __HAL_LINKDMA(hi2s, hdmarx, hdma_i2sRx); + + /* Deinitialize the Stream for new transfer */ + HAL_DMA_DeInit(&hdma_i2sRx); + + /* Configure the DMA Stream */ + HAL_DMA_Init(&hdma_i2sRx); + } + + /* I2S DMA IRQ Channel configuration */ + HAL_NVIC_SetPriority(I2S2_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0); + HAL_NVIC_EnableIRQ(I2S2_DMAx_IRQ); +} + +/** + * @brief DeInitializes BSP_AUDIO_IN MSP. + * @param hi2s: I2S handle + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params) +{ + GPIO_InitTypeDef gpio_init_structure; + + /* I2S DMA IRQ Channel deactivation */ + HAL_NVIC_DisableIRQ(I2S2_DMAx_IRQ); + + if(hi2s->Instance == I2S2) + { + /* Deinitialize the Stream for new transfer */ + HAL_DMA_DeInit(hi2s->hdmarx); + } + + /* Disable I2S block */ + __HAL_I2S_DISABLE(hi2s); + + /* Disable pins: SCK and SD pins */ + gpio_init_structure.Pin = I2S2_SCK_PIN; + HAL_GPIO_DeInit(I2S2_SCK_GPIO_PORT, gpio_init_structure.Pin); + gpio_init_structure.Pin = I2S2_MOSI_PIN; + HAL_GPIO_DeInit(I2S2_MOSI_GPIO_PORT, gpio_init_structure.Pin); + + /* Disable I2S clock */ + I2S2_CLK_DISABLE(); + + /* GPIO pins clock and DMA clock can be shut down in the applic + by surcgarging this __weak function */ +} + +/** + * @brief User callback when record buffer is filled. + */ +__weak void BSP_AUDIO_IN_TransferComplete_CallBack(void) +{ + /* This function should be implemented by the user application. + It is called into this driver when the current buffer is filled + to prepare the next buffer pointer and its size. */ +} + +/** + * @brief Manages the DMA Half Transfer complete event. + */ +__weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void) +{ + /* This function should be implemented by the user application. + It is called into this driver when the current buffer is filled + to prepare the next buffer pointer and its size. */ +} + +/** + * @brief Audio IN Error callback function. + */ +__weak void BSP_AUDIO_IN_Error_Callback(void) +{ + /* This function is called when an Interrupt due to transfer error on or peripheral + error occurs. */ +} + +/******************************************************************************* + Static Functions +*******************************************************************************/ + +/** + * @brief Initialize the PDM library. + * @param AudioFreq: Audio sampling frequency + * @param ChnlNbr: Number of audio channels (1: mono; 2: stereo) + */ +static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbr) +{ +#if 1 + uint32_t i = 0; + + /* Enable CRC peripheral to unlock the PDM library */ + __CRC_CLK_ENABLE(); + + for(i = 0; i < ChnlNbr; i++) + { + /* Filter LP and HP Init */ + Filter[i].LP_HZ = AudioFreq / 2; + Filter[i].HP_HZ = 10; + Filter[i].Fs = AudioFreq; + /* On STM32F4-Discovery a single microphone is mounted, samples are duplicated + to make stereo audio streams */ + // BK - generate mono output + // Filter[i].Out_MicChannels = 2; + Filter[i].Out_MicChannels = 1; + Filter[i].In_MicChannels = ChnlNbr; + PDM_Filter_Init((PDMFilter_InitStruct *)&Filter[i]); + } +#endif +} + +/** + * @brief Initializes the Audio Codec audio interface (I2S) + * @note This function assumes that the I2S input clock (through PLL_R in + * Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y) + * is already configured and ready to be used. + * @param AudioFreq: Audio frequency to be configured for the I2S peripheral. + */ +static uint8_t I2S2_Init(uint32_t AudioFreq) +{ + /* Initialize the hAudioInI2s Instance parameter */ + hAudioInI2s.Instance = I2S2; + + /* Disable I2S block */ + __HAL_I2S_DISABLE(&hAudioInI2s); + + /* I2S2 peripheral configuration */ + hAudioInI2s.Init.AudioFreq = 2 * AudioFreq; + hAudioInI2s.Init.ClockSource = I2S_CLOCK_PLL; + hAudioInI2s.Init.CPOL = I2S_CPOL_HIGH; + hAudioInI2s.Init.DataFormat = I2S_DATAFORMAT_16B; + hAudioInI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE; + hAudioInI2s.Init.Mode = I2S_MODE_MASTER_RX; + hAudioInI2s.Init.Standard = I2S_STANDARD_LSB; + + /* Initialize the I2S peripheral with the structure above */ + if(HAL_I2S_Init(&hAudioInI2s) != HAL_OK) + { + return AUDIO_ERROR; + } + else + { + return AUDIO_OK; + } +} + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_IN_OUT_Private_Functions STM32F4 DISCOVERY AUDIO IN OUT Private Functions + * @{ + */ + +/** + * @brief I2S error callbacks. + * @param hi2s: I2S handle + */ +void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) +{ + /* Manage the error generated on DMA FIFO: This function + should be coded by user (its prototype is already declared in stm32f4_discovery_audio.h) */ + if(hi2s->Instance == I2S3) + { + BSP_AUDIO_OUT_Error_CallBack(); + } + if(hi2s->Instance == I2S2) + { + BSP_AUDIO_IN_Error_Callback(); + } + } + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/bsp/stm32f4_discovery_audio.h b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery_audio.h new file mode 100644 index 000000000..0cb4178e8 --- /dev/null +++ b/port/stm32-f4discovery-usb/bsp/stm32f4_discovery_audio.h @@ -0,0 +1,275 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery_audio.h + * @author MCD Application Team + * @version V2.1.2 + * @date 27-January-2017 + * @brief This file contains the common defines and functions prototypes for + * stm32f4_discovery_audio.c driver. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4_DISCOVERY_AUDIO_H +#define __STM32F4_DISCOVERY_AUDIO_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +/* Include audio component Driver */ +#include "cs43l22.h" + +#include "stm32f4_discovery.h" +//#include "../../../Middlewares/ST/STM32_Audio/Addons/PDM/pdm_filter.h" + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_AUDIO + * @{ + */ + + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Types STM32F4 DISCOVERY AUDIO Exported Types + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Exported_Constants STM32F4 DISCOVERY AUDIO OUT Exported Constants + * @{ + */ + +/*------------------------------------------------------------------------------ + AUDIO OUT CONFIGURATION +------------------------------------------------------------------------------*/ + +/* I2S peripheral configuration defines */ +#define I2S3 SPI3 +#define I2S3_CLK_ENABLE() __HAL_RCC_SPI3_CLK_ENABLE() +#define I2S3_CLK_DISABLE() __HAL_RCC_SPI3_CLK_DISABLE() +#define I2S3_SCK_SD_WS_AF GPIO_AF6_SPI3 +#define I2S3_SCK_SD_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() +#define I2S3_MCK_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() +#define I2S3_WS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() +#define I2S3_WS_PIN GPIO_PIN_4 +#define I2S3_SCK_PIN GPIO_PIN_10 +#define I2S3_SD_PIN GPIO_PIN_12 +#define I2S3_MCK_PIN GPIO_PIN_7 +#define I2S3_SCK_SD_GPIO_PORT GPIOC +#define I2S3_WS_GPIO_PORT GPIOA +#define I2S3_MCK_GPIO_PORT GPIOC + +/* I2S DMA Stream definitions */ +#define I2S3_DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE() +#define I2S3_DMAx_CLK_DISABLE() __HAL_RCC_DMA1_CLK_DISABLE() +#define I2S3_DMAx_STREAM DMA1_Stream7 +#define I2S3_DMAx_CHANNEL DMA_CHANNEL_0 +#define I2S3_DMAx_IRQ DMA1_Stream7_IRQn +#define I2S3_DMAx_PERIPH_DATA_SIZE DMA_PDATAALIGN_HALFWORD +#define I2S3_DMAx_MEM_DATA_SIZE DMA_MDATAALIGN_HALFWORD +#define DMA_MAX_SZE 0xFFFF + +#define I2S3_IRQHandler DMA1_Stream7_IRQHandler + +/* Select the interrupt preemption priority and subpriority for the DMA interrupt */ +#define AUDIO_OUT_IRQ_PREPRIO 0x0E /* Select the preemption priority level(0 is the highest) */ + +/*------------------------------------------------------------------------------ + AUDIO IN CONFIGURATION +------------------------------------------------------------------------------*/ +/* SPI Configuration defines */ +#define I2S2 SPI2 +#define I2S2_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE() +#define I2S2_CLK_DISABLE() __HAL_RCC_SPI2_CLK_DISABLE() +#define I2S2_SCK_PIN GPIO_PIN_10 +#define I2S2_SCK_GPIO_PORT GPIOB +#define I2S2_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define I2S2_SCK_AF GPIO_AF5_SPI2 + +#define I2S2_MOSI_PIN GPIO_PIN_3 +#define I2S2_MOSI_GPIO_PORT GPIOC +#define I2S2_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() +#define I2S2_MOSI_AF GPIO_AF5_SPI2 + +/* I2S DMA Stream Rx definitions */ +#define I2S2_DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE() +#define I2S2_DMAx_CLK_DISABLE() __HAL_RCC_DMA1_CLK_DISABLE() +#define I2S2_DMAx_STREAM DMA1_Stream3 +#define I2S2_DMAx_CHANNEL DMA_CHANNEL_0 +#define I2S2_DMAx_IRQ DMA1_Stream3_IRQn +#define I2S2_DMAx_PERIPH_DATA_SIZE DMA_PDATAALIGN_HALFWORD +#define I2S2_DMAx_MEM_DATA_SIZE DMA_MDATAALIGN_HALFWORD + +#define I2S2_IRQHandler DMA1_Stream3_IRQHandler + +/* Select the interrupt preemption priority and subpriority for the IT/DMA interrupt */ +#define AUDIO_IN_IRQ_PREPRIO 0x0F /* Select the preemption priority level(0 is the highest) */ + +/*------------------------------------------------------------------------------ + CONFIGURATION: Audio Driver Configuration parameters +------------------------------------------------------------------------------*/ + +#define AUDIODATA_SIZE 2 /* 16-bits audio data size */ + +/* Audio status definition */ +#define AUDIO_OK 0 +#define AUDIO_ERROR 1 +#define AUDIO_TIMEOUT 2 + +/* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */ +#define DEFAULT_AUDIO_IN_FREQ I2S_AUDIOFREQ_16K +#define DEFAULT_AUDIO_IN_BIT_RESOLUTION 16 +#define DEFAULT_AUDIO_IN_CHANNEL_NBR 1 /* Mono = 1, Stereo = 2 */ +#define DEFAULT_AUDIO_IN_VOLUME 64 + +/* PDM buffer input size */ +#define INTERNAL_BUFF_SIZE 128*DEFAULT_AUDIO_IN_FREQ/16000*DEFAULT_AUDIO_IN_CHANNEL_NBR +/* PCM buffer output size */ +#define PCM_OUT_SIZE DEFAULT_AUDIO_IN_FREQ/1000 +#define CHANNEL_DEMUX_MASK 0x55 + +/*------------------------------------------------------------------------------ + OPTIONAL Configuration defines parameters +------------------------------------------------------------------------------*/ + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Variables STM32F4 DISCOVERY AUDIO Exported Variables + * @{ + */ +extern __IO uint16_t AudioInVolume; +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_Exported_Macros STM32F4 DISCOVERY AUDIO Exported Macros + * @{ + */ +#define DMA_MAX(_X_) (((_X_) <= DMA_MAX_SZE)? (_X_):DMA_MAX_SZE) +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Exported_Functions STM32F4 DISCOVERY AUDIO OUT Exported Functions + * @{ + */ +uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq); +uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size); +void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size); +uint8_t BSP_AUDIO_OUT_Pause(void); +uint8_t BSP_AUDIO_OUT_Resume(void); +uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option); +uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume); +void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq); +uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd); +uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output); + +/* User Callbacks: user has to implement these functions in his code if they are needed. */ +/* This function is called when the requested data has been completely transferred. */ +void BSP_AUDIO_OUT_TransferComplete_CallBack(void); + +/* This function is called when half of the requested buffer has been transferred. */ +void BSP_AUDIO_OUT_HalfTransfer_CallBack(void); + +/* This function is called when an Interrupt due to transfer error on or peripheral + error occurs. */ +void BSP_AUDIO_OUT_Error_CallBack(void); + +/* These function can be modified in case the current settings (e.g. DMA stream) + need to be changed for specific application needs */ +void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params); +void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params); +void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params); + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_AUDIO_IN_Exported_Functions STM32F4 DISCOVERY AUDIO IN Exported Functions + * @{ + */ +uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr); +uint8_t BSP_AUDIO_IN_Record(uint16_t *pData, uint32_t Size); +uint8_t BSP_AUDIO_IN_Stop(void); +uint8_t BSP_AUDIO_IN_Pause(void); +uint8_t BSP_AUDIO_IN_Resume(void); +uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume); +uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t *PDMBuf, uint16_t *PCMBuf); +/* User Callbacks: user has to implement these functions in his code if they are needed. */ +/* This function should be implemented by the user application. + It is called into this driver when the current buffer is filled to prepare the next + buffer pointer and its size. */ +void BSP_AUDIO_IN_TransferComplete_CallBack(void); +void BSP_AUDIO_IN_HalfTransfer_CallBack(void); + +/* This function is called when an Interrupt due to transfer error on or peripheral + error occurs. */ +void BSP_AUDIO_IN_Error_Callback(void); + +/* These function can be modified in case the current settings (e.g. DMA stream) + need to be changed for specific application needs */ +void BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params); +void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params); +void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4_DISCOVERY_AUDIO_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/port/stm32-f4discovery-usb/pdm/Release_Notes.html b/port/stm32-f4discovery-usb/pdm/Release_Notes.html new file mode 100755 index 000000000..a45f214c7 --- /dev/null +++ b/port/stm32-f4discovery-usb/pdm/Release_Notes.html @@ -0,0 +1,217 @@ + + + + + + + + + + + + + + + + + + + + Release Notes for PDM audio software decoding Library + + + + + + + + + +
+


+

+
+ + + + + + +
+ + + + + + + + + +
+ +
+
+

Release +Notes for PDM audio software decoding Library

+

Copyright +2015 STMicroelectronics

+

+
+

 

+ + + + + + +

+
The PDM audio software decoding Library +is an optimized software implementation for PDM signal decoding and +audio signal reconstruction when connecting an ST MEMS +microphone (MP45Dxxx, MP34Dxxx) with an STM32 microcontroller. +It can directly take the Pulse Density Modulated (PDM) data output from +the microphone and convert it to 16-bit pulse-code modulation (PCM) +format.
+ +
+ + This Library includes one header file pdm_filter.h and binary/object codes running on all STM32 devices. The naming format is libPDMFilter_CMx_C.a/.lib, where:
+
+
    +
  • CMx: CortexMx core class (CM0, CM3, CM4 and CM7)
  • C: compiler (IAR, Keil and GCC)
  • +
+
For more information about this Library, please refer to the document "AN3998 PDM audio software decoding on STM32 microcontrollers" available for download from STMicroelectronics website at www.st.com.
+
    + + +
+ + + +

Update History

+

V2.1.0 / 31-March-2015

+ + + + +

Main +Changes

+ + + + + +
    +
  • Add Binaries for CortexM7 (CM7) core
    +
  • +
  • PDM_Filter_128_MSB() and PDM_Filter_128_LSB() functions: enhance volume setting, it was lower than expected
  • +
  • PDM_Filter_64_MSB() function: fix issue when using more than 2 microphones
    +
  • +
+ +

V2.0.1 / 17-December-2013

+ + +

Main +Changes

+ + + +
    +
  • In HTONS() macro, change u16 type by uint16_t
    +
  • +
+ +

V2.0.0 / 07-August-2013

+

Main +Changes

+ +
    +
  • Update Library binary name format
  • +
  • Add Library version optimized for CM0, CM3 and CM4F cores
    +
  • +
  • Add functions for decimation factor 128
  • +
+

V1.1.2 / 17-April-2012

+

Main +Changes

+
  • All source files: license disclaimer text update and add link to the License file on ST Internet.

V1.1.1 / 30-December-2011

Main +Changes

+
  • PDM Library moved from Utilities\STM32F4-Discovery to Utilities\STM32_Audio\Addons\PDM folder
  • Add new license agreement "MCD-ST Image SW License Agreement V2.pdf"

V1.1.0 / 28-October-2011

+

Main +Changes

+ +
  • Add PDM audio software decoding Library drivers, used to decode and reconstruct the audio signal produced by MP45DT02 MEMS microphone from STMicroelectronics. For more details about this Library, please refer to document "PDM audio software decoding on STM32 microcontrollers (AN3998)".
  • stm32f4_discovery_audio_codec.c/.h: update to use DMA instead of Interrupt in I2S communication with the external codec.

V1.0.0 / 19-September-2011

+

Main +Changes

+ +
  • First official version of the STM32F4-Discovery Board Drivers

License

Licensed under MCD-ST Image SW License Agreement V2, (the "License"); You may not use this package except in compliance with the License. You may obtain a copy of the License at:


Unless +required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See +the License for the specific language governing permissions and +limitations under the License.
+ +
+
+

For + complete documentation on STM32 + Microcontrollers visit www.st.com/STM32

+
+

+
+
+

 

+
+ + \ No newline at end of file diff --git a/port/stm32-f4discovery-usb/pdm/libPDMFilter_CM4_GCC.a b/port/stm32-f4discovery-usb/pdm/libPDMFilter_CM4_GCC.a new file mode 100755 index 0000000000000000000000000000000000000000..ed5e8986c561992bac8fa3483b59da4d82d3aede GIT binary patch literal 12694 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 literal 0 HcmV?d00001 diff --git a/port/stm32-f4discovery-usb/pdm/pdm_filter.h b/port/stm32-f4discovery-usb/pdm/pdm_filter.h new file mode 100755 index 000000000..c039ee92a --- /dev/null +++ b/port/stm32-f4discovery-usb/pdm/pdm_filter.h @@ -0,0 +1,76 @@ +/** + ****************************************************************************** + * @file pdm_filter.h + * @author MCD Application Team + * @version V2.1.0 + * @date 31-March-2015 + * @brief Header file for PDM audio software decoding Library. + * This Library is used to decode and reconstruct the audio signal + * produced by ST MEMS microphone (MP45Dxxx, MP34Dxxx). + * For more details about this Library, please refer to document + * "PDM audio software decoding on STM32 microcontrollers (AN3998)". + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2015 STMicroelectronics

+ * + * Licensed under MCD-ST Image SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_image_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __PDM_FILTER_H +#define __PDM_FILTER_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include + + +/* Exported types ------------------------------------------------------------*/ +typedef struct { + uint16_t Fs; + float LP_HZ; + float HP_HZ; + uint16_t In_MicChannels; + uint16_t Out_MicChannels; + char InternalFilter[34]; +} PDMFilter_InitStruct; + +/* Exported constants --------------------------------------------------------*/ +/* Exported macros -----------------------------------------------------------*/ +#define HTONS(A) ((((uint16_t)(A) & 0xff00) >> 8) | \ + (((uint16_t)(A) & 0x00ff) << 8)) + +/* Exported functions ------------------------------------------------------- */ +void PDM_Filter_Init(PDMFilter_InitStruct * Filter); + +int32_t PDM_Filter_64_MSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); +int32_t PDM_Filter_80_MSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); +int32_t PDM_Filter_128_MSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); +int32_t PDM_Filter_64_LSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); +int32_t PDM_Filter_80_LSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); +int32_t PDM_Filter_128_LSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); + +#ifdef __cplusplus +} +#endif + +#endif /* __PDM_FILTER_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/port/stm32-f4discovery-usb/port/hal_audio_f4discovery.c b/port/stm32-f4discovery-usb/port/hal_audio_f4discovery.c new file mode 100644 index 000000000..f6c175968 --- /dev/null +++ b/port/stm32-f4discovery-usb/port/hal_audio_f4discovery.c @@ -0,0 +1,333 @@ +/* + * Copyright (C) 2017 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +#define __BTSTACK_FILE__ "hal_audio_f4_discovery.c" + +#include "hal_audio.h" +#include "btstack_debug.h" +#include "stm32f4_discovery_audio.h" + +// output +#define OUTPUT_BUFFER_NUM_SAMPLES 512 +#define NUM_OUTPUT_BUFFERS 2 + +// #define MEASURE_SAMPLE_RATE + +static void (*audio_played_handler)(uint8_t buffer_index); +static int playback_started; +static uint32_t sink_sample_rate; + +// our storage +static int16_t output_buffer[NUM_OUTPUT_BUFFERS * OUTPUT_BUFFER_NUM_SAMPLES * 2]; // stereo + +#ifdef MEASURE_SAMPLE_RATE +static uint32_t stream_start_ms; +static uint32_t stream_samples; +#endif + +// input - irq every 16 ms currently +#define INPUT_BUFFER_NUM_SAMPLES 256 + +static int recording_started; +static int32_t recording_sample_rate; + +static void (*audio_recorded_callback)(const int16_t * buffer, uint16_t num_samples); + +static int16_t input_buffer[INPUT_BUFFER_NUM_SAMPLES]; // single mono buffer +static uint16_t pdm_buffer[INPUT_BUFFER_NUM_SAMPLES*8]; + +static uint32_t source_sample_rate; +static int source_pcm_samples_per_ms; +static int source_pdm_bytes_per_ms; +static int source_pcm_samples_per_irq; +static int source_pdm_samples_total; + +void BSP_AUDIO_OUT_HalfTransfer_CallBack(void){ + +#ifdef MEASURE_SAMPLE_RATE + if (stream_start_ms == 0){ + stream_start_ms = btstack_run_loop_get_time_ms(); + } else { + stream_samples++; + } +#endif + + (*audio_played_handler)(0); +} + +void BSP_AUDIO_OUT_TransferComplete_CallBack(void){ + +#ifdef MEASURE_SAMPLE_RATE + if (stream_samples == 500){ + uint32_t now = btstack_run_loop_get_time_ms(); + uint32_t delta = now - stream_start_ms; + log_info("Samples per second: %u", stream_samples * OUTPUT_BUFFER_NUM_SAMPLES * 1000 / delta); + stream_start_ms = now; + stream_samples = 0; + } + stream_samples++; +#endif + + (*audio_played_handler)(1); +} + +/** + * @brief Setup audio codec for specified samplerate and number channels + * @param Channels + * @param Sample rate + * @param Buffer played callback + * @param Buffer recorded callback (use NULL if no recording) + */ +void hal_audio_sink_init(uint8_t channels, + uint32_t sample_rate, + void (*buffer_played_callback) (uint8_t buffer_index)){ + + // F4 Discovery Audio BSP only supports stereo playback + if (channels == 1){ + log_error("F4 Discovery Audio BSP only supports stereo playback. HFP/HSP demos using sco_demo_util, please #define ENABLE_SCO_STEREO_PLAYBACK"); + return; + } + + audio_played_handler = buffer_played_callback; + sink_sample_rate = sample_rate; +} + +/** + * @brief Get number of output buffers in HAL + * @returns num buffers + */ +uint16_t hal_audio_sink_get_num_output_buffers(void){ + return NUM_OUTPUT_BUFFERS; +} + +/** + * @brief Get size of single output buffer in HAL + * @returns buffer size + */ +uint16_t hal_audio_sink_get_num_output_buffer_samples(void){ + return OUTPUT_BUFFER_NUM_SAMPLES; +} + +/** + * @brief Reserve output buffer + * @returns buffer + */ +int16_t * hal_audio_sink_get_output_buffer(uint8_t buffer_index){ + switch (buffer_index){ + case 0: + return output_buffer; + case 1: + return &output_buffer[OUTPUT_BUFFER_NUM_SAMPLES * 2]; + default: + return NULL; + } +} + +/** + * @brief Start stream + */ +void hal_audio_sink_start(void){ + playback_started = 1; + + BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, 80, sink_sample_rate); + + // BSP_AUDIO_OUT_Play gets number bytes -> 1 frame - 16 bit/stereo = 4 bytes + BSP_AUDIO_OUT_Play( (uint16_t*) output_buffer, NUM_OUTPUT_BUFFERS * OUTPUT_BUFFER_NUM_SAMPLES * 4); +} + +/** + * @brief Stop stream + */ +void hal_audio_sink_stop(void){ + playback_started = 0; + BSP_AUDIO_OUT_Stop(CODEC_PDWN_HW); +} + +/** + * @brief Close audio codec + */ +void hal_audio_sink_close(void){ + if (playback_started){ + hal_audio_sink_stop(); + } +} + +#ifdef SIMULATE_SINE + +// temp sine simulator +// input signal: pre-computed sine wave, 266 Hz at 16000 kHz +static const int16_t sine_int16_at_16000hz[] = { + 0, 3135, 6237, 9270, 12202, 14999, 17633, 20073, 22294, 24270, + 25980, 27406, 28531, 29344, 29835, 30000, 29835, 29344, 28531, 27406, + 25980, 24270, 22294, 20073, 17633, 14999, 12202, 9270, 6237, 3135, + 0, -3135, -6237, -9270, -12202, -14999, -17633, -20073, -22294, -24270, +-25980, -27406, -28531, -29344, -29835, -30000, -29835, -29344, -28531, -27406, +-25980, -24270, -22294, -20073, -17633, -14999, -12202, -9270, -6237, -3135, +}; +static unsigned int phase; + +// 8 kHz samples in host endianess +static void sco_demo_sine_wave_int16_at_8000_hz_host_endian(unsigned int num_samples, int16_t * data){ + unsigned int i; + for (i=0; i < num_samples; i++){ + data[i] = sine_int16_at_16000hz[phase++]; + // ony use every second sample from 16khz table to get 8khz + phase += 2; + if (phase >= (sizeof(sine_int16_at_16000hz) / sizeof(int16_t))){ + phase = 0; + } + } +} + + +// 16 kHz samples in host endianess +static void sco_demo_sine_wave_int16_at_16000_hz_host_endian(unsigned int num_samples, int16_t * data){ + unsigned int i; + for (i=0; i < num_samples; i++){ + data[i] = sine_int16_at_16000hz[phase++]; + if (phase >= (sizeof(sine_int16_at_16000hz) / sizeof(int16_t))){ + phase = 0; + } + } +} + +static void generate_sine(void){ + if (recording_sample_rate == 8000){ + sco_demo_sine_wave_int16_at_8000_hz_host_endian(INPUT_BUFFER_NUM_SAMPLES, input_buffer); + } else { + sco_demo_sine_wave_int16_at_16000_hz_host_endian(INPUT_BUFFER_NUM_SAMPLES, input_buffer); + } + // notify + (*audio_recorded_callback)(input_buffer, INPUT_BUFFER_NUM_SAMPLES); +} +#else + +static void process_pdm(uint16_t * pdm_half_buffer){ + + int samples_needed = source_pcm_samples_per_irq; + int16_t * pcm_buffer = input_buffer; + + while (samples_needed){ + // TODO: use int16_t for pcm samples + BSP_AUDIO_IN_PDMToPCM(pdm_half_buffer, (uint16_t *) pcm_buffer); + pdm_half_buffer += source_pdm_bytes_per_ms / 2; + pcm_buffer += source_pcm_samples_per_ms; + samples_needed -= source_pcm_samples_per_ms; + } + + // notify + (*audio_recorded_callback)(input_buffer, source_pcm_samples_per_irq); +} + +#endif + +void BSP_AUDIO_IN_HalfTransfer_CallBack(void){ +#ifdef SIMULATE_SINE + generate_sine(); +#else + process_pdm(&pdm_buffer[0]); +#endif +} + +void BSP_AUDIO_IN_TransferComplete_CallBack(void){ +#ifdef SIMULATE_SINE + generate_sine(); +#else + process_pdm(&pdm_buffer[source_pdm_samples_total/2]); +#endif +} + +/** + * @brief Setup audio codec for recording using specified samplerate and number of channels + * @param Channels + * @param Sample rate + * @param Buffer recorded callback + */ +void hal_audio_source_init(uint8_t channels, + uint32_t sample_rate, + void (*buffer_recorded_callback)(const int16_t * buffer, uint16_t num_samples)){ + + source_sample_rate = sample_rate; + + // Driver only supports mono recording + if (channels != 1){ + log_error("F4 Discovery only has single microphone, stereo recording not supported"); + return; + } + + int decimation = 64; + + // size of input & output of PDM filter depend on output frequency and decimation + source_pcm_samples_per_irq = sample_rate / 1000 * 16; // 256@16 kHz, 128@8 kHz + + source_pcm_samples_per_ms = sample_rate / 1000; + source_pdm_bytes_per_ms = source_pcm_samples_per_ms * decimation / 8; + + source_pdm_samples_total = INPUT_BUFFER_NUM_SAMPLES * 8 * sample_rate / 16000; + + log_info("Source: PDM bytes per ms %u, PDM samples total %u - PCM samples per ms %u", source_pdm_bytes_per_ms, source_pdm_samples_total, source_pcm_samples_per_ms); + + audio_recorded_callback = buffer_recorded_callback; + recording_sample_rate = sample_rate; +} + +/** + * @brief Start stream + */ +void hal_audio_source_start(void){ + BSP_AUDIO_IN_Init(source_sample_rate, 16, 1); + BSP_AUDIO_IN_Record(pdm_buffer, source_pdm_samples_total); + recording_started = 1; +} + +/** + * @brief Stop stream + */ +void hal_audio_source_stop(void){ + if (!recording_started) return; + BSP_AUDIO_IN_Stop(); + recording_started = 0; +} + +/** + * @brief Close audio codec + */ +void hal_audio_source_close(void){ + if (recording_started) { + hal_audio_source_stop(); + } +} diff --git a/port/stm32-f4discovery-usb/port/port.c b/port/stm32-f4discovery-usb/port/port.c index 0924dc510..e0d8698ad 100644 --- a/port/stm32-f4discovery-usb/port/port.c +++ b/port/stm32-f4discovery-usb/port/port.c @@ -131,7 +131,7 @@ void port_main(void){ btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); // uncomment for packet log - hci_dump_open( NULL, HCI_DUMP_STDOUT ); + // hci_dump_open( NULL, HCI_DUMP_STDOUT ); // init HCI hci_init(hci_transport_h2_stm32_instance(), NULL); @@ -159,12 +159,10 @@ void port_main(void){ // setup LE Device DB using TLV le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); -#if 0 #ifdef HAVE_HAL_AUDIO // setup audio btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance()); btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance()); -#endif #endif // inform about BTstack state diff --git a/port/stm32-f4discovery-usb/port/port.h b/port/stm32-f4discovery-usb/port/port.h new file mode 100644 index 000000000..2c067fd3f --- /dev/null +++ b/port/stm32-f4discovery-usb/port/port.h @@ -0,0 +1,4 @@ +#ifndef __PORT_H +#define __PORT_H +void port_main(void); +#endif