1.环境: stm32f407vet6 SPI2作为PDM麦克数据的输入, 单路数字mic,MIC的L/R管脚接地, SPI2的时钟为1.28MHz。 2.配置:SPI2的配置: /* SPI2 init function */ static void MX_SPI2_Init(void) { /* SPI2 parameter configuration*/ hspi2.Instance = SPI2; hspi2.Init.Mode = SPI_MODE_MASTER; hspi2.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; hspi2.Init.DataSize = SPI_DATASIZE_16BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLE; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi2.Init.CRCPolynomial = 10; if (HAL_SPI_Init(&hspi2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } PDM2PCM配置: void MX_PDM2PCM_Init(void) { PDM1_filter_handler.bit_order = PDM_FILTER_BIT_ORDER_LSB; PDM1_filter_handler.endianness = PDM_FILTER_ENDIANNESS_LE; PDM1_filter_handler.high_pass_tap = 1932735282; PDM1_filter_handler.in_ptr_channels = 1; PDM1_filter_handler.out_ptr_channels = 1; PDM_Filter_Init(&PDM1_filter_handler); PDM1_filter_config.decimation_factor = PDM_FILTER_DEC_FACTOR_80; PDM1_filter_config.output_samples_number = 16; PDM1_filter_config.mic_gain = 0; PDM_Filter_setConfig(&PDM1_filter_handler, &PDM1_filter_config); } PDM2PCM转换 uint8_t MX_PDM2PCM_Process(uint16_t *PDMBuf, uint16_t *PCMBuf) { uint32_t retuVal; retuVal= PDM_Filter(PDMBuf,PCMBuf,&PDM1_filter_handler); return retuVal; } 3.数据处理: PDM2PCM #define PDMBUFSIZE 160 //16bit #define PCMBUFSIZE 16 //16bit #define PCM2DACBUFSIZE 60 //16bit void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) { uint8_t i; uint16_t tmpPDM[PDMBUFSIZE/2]; if(hspi==&hspi2) { for(i=0;i<PDMBUFSIZE/2;i++) tmpPDM=HTONS(PDMDataBuf[i+PDMBUFSIZE/2]); PDM2PCMState=MX_PDM2PCM_Process(tmpPDM,PCMdataBuf); for(i=0;i<PCMBUFSIZE;i++) { if(PCM2DACBuf->isFull(PCM2DACBuf)==true) PCM2DACBuf->add(PCM2DACBuf, (PCMdataBuf+i)); ////写入DAC转换 } } } void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi) { uint8_t i; uint16_t tmpPDM[PDMBUFSIZE/2]; if(hspi==&hspi2) { for(i=0;i<PDMBUFSIZE/2;i++) tmpPDM=HTONS(PDMDataBuf); PDM2PCMState=MX_PDM2PCM_Process(tmpPDM,PCMdataBuf); for(i=0;i<PCMBUFSIZE;i++) { if(PCM2DACBuf->isFull(PCM2DACBuf)==true) PCMBufFullFlag++; PCM2DACBuf->add(PCM2DACBuf, (PCMdataBuf+i)); //写入DAC转换 } } } 结果: 转换后的PCM数据只有两种情况:要么是接近0xffff,要么接近ox0000.也就是说,转换的输出成了类似PWM的脉冲,而不是正常音频的PCM数据。 |
不懂,帮顶~~
评分
查看全部评分
直接对照头文件以及结构体参数来调用即可