请写出在软件 Keil uVision5 环境中使用 Hal 库进行开发,编 写 C 程序用于配置时钟、配置 GPIO 引脚以实现 I2S 串口功能,并调用滤波器解码库 libPDMFilter_Keil.lib 实现滤波处理将 PDM 信号转化为 PCM 信号的代码
时间: 2024-03-07 13:50:17 浏览: 368
STM32F4全系列例程
以下是在Keil uVision5环境中使用Hal库进行开发的示例代码,用于配置时钟、配置GPIO引脚以实现I2S串口功能,并调用滤波器解码库libPDMFilter_Keil.lib实现滤波处理将PDM信号转化为PCM信号:
```c
#include "stm32f4xx_hal.h"
#include "libPDMFilter_Keil.h"
#define I2S3_WS_Pin GPIO_PIN_4
#define I2S3_WS_GPIO_Port GPIOA
#define I2S3_CK_Pin GPIO_PIN_5
#define I2S3_CK_GPIO_Port GPIOA
#define I2S3_SD_Pin GPIO_PIN_7
#define I2S3_SD_GPIO_Port GPIOC
I2S_HandleTypeDef hi2s3;
uint16_t pdm_buffer[2][64]; // PDM数据缓存,双缓冲
uint16_t pcm_buffer[128]; // PCM数据缓存
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2S3_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2S3_Init();
PDM_Filter_Handler_t pdm_filter_handler;
pdm_filter_handler.bit_order = PDM_FILTER_BIT_ORDER_LSB;
pdm_filter_handler.decimation_factor = PDM_FILTER_DEC_FACTOR_64;
pdm_filter_handler.high_pass_tap = 2122358088;
pdm_filter_handler.out_ptr_channels = 1;
PDM_Filter_Init(&pdm_filter_handler);
while (1)
{
if (HAL_I2S_Receive_DMA(&hi2s3, (uint16_t *)pdm_buffer[0], 64) != HAL_OK) // 开始I2S接收
{
Error_Handler();
}
while (HAL_DMA_GetState(hi2s3.hdmarx) != HAL_DMA_STATE_READY)
{
// 等待I2S接收完成
}
if (HAL_I2S_Receive_DMA(&hi2s3, (uint16_t *)pdm_buffer[1], 64) != HAL_OK) // 开始I2S接收
{
Error_Handler();
}
PDM_Filter_64_LSB((uint8_t *)&pdm_buffer[0][0], (uint16_t *)&pcm_buffer[0], 1, &pdm_filter_handler); // 解码PDM数据
PDM_Filter_64_LSB((uint8_t *)&pdm_buffer[1][0], (uint16_t *)&pcm_buffer[64], 1, &pdm_filter_handler);
// 处理PCM数据
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t *)pcm_buffer, 128); // 发送PCM数据
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
static void MX_I2S3_Init(void)
{
hi2s3.Instance = SPI3;
hi2s3.Init.Mode = I2S_MODE_MASTER_RX;
hi2s3.Init.Standard = I2S_STANDARD_LSB;
hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_16K;
hi2s3.Init.CPOL = I2S_CPOL_LOW;
hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
if (HAL_I2S_Init(&hi2s3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = I2S3_WS_Pin | I2S3_CK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = I2S3_SD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(I2S3_SD_GPIO_Port, &GPIO_InitStruct);
}
```
需要注意的是,上述示例代码中使用了双缓冲技术,以避免数据丢失。同时,也需要在中断服务函数中及时处理I2S接收和发送完成事件,以避免数据溢出或数据错位。
阅读全文