我在使用STM32WBA55G-DK1板子调试SAI接口,SAI连接了一个数字麦克风,数字麦克风提供的是PDM信号,ST提供了一个PDM2PCM库进行转换,但是我的工程发现转换出来的PCM数据都是0,为了验证是不是我的PDM数据有问题,我从这个官方提供这个板子的Demo工程里获取SAI获取的PDM数据保存在一个数组中, 在我自己的工程中对这个数组进行PDM转PCM,结果还是为0。
为了对比我的工程与官方工程的差异,不断精简官方代码和我的工程代码,最后只剩下在main函数中初始化串口1作为log输出,直接调用PDM2PCM库进行初始化以及转换,结果仍然有差异,我都不知道为何会造成这种情况。
下面是我的代码
#define PDM_SAMPLES_AT_EACH_CALL 20
#define AUDIO_REC_BUFFER_SIZE (PDM_SAMPLES_AT_EACH_CALL * 8 * 2)
#define AUDIO_PLAY_BUFFER_SIZE (PDM_SAMPLES_AT_EACH_CALL * 4)
#define AUDIO_HIGH_PASS_TAP 2122358088
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
uint8_t RecordBuff[AUDIO_REC_BUFFER_SIZE] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x96, 0x65, 0x9A, 0x5A, 0x36, 0xCA, 0x95, 0xA5, 0x69, 0x66, 0x0D, 0x73, 0x4E, 0x99, 0x6A, 0x9A, 0x93, 0xA9, 0xA6, 0x95,
0x23, 0x67, 0x96, 0x99, 0xA5, 0x99, 0x69, 0x96, 0x65, 0xA6, 0xA6, 0x55, 0x66, 0x26, 0x8F, 0x99, 0x59, 0xA6, 0x69, 0x5A, 0xE5, 0x30, 0x67, 0x93, 0xA9, 0x66, 0x66, 0x23, 0x67, 0x96, 0x65, 0x9A,
0xA6, 0x8D, 0xC9, 0x95, 0x59, 0xA6, 0xA6, 0x35, 0x9C, 0x69, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x96, 0x59, 0xA6, 0x69, 0x5A, 0xA3, 0x5C, 0x59, 0x9A, 0x69, 0x96, 0x65, 0xC9, 0x59,
};
uint16_t PlaybackBuff[AUDIO_PLAY_BUFFER_SIZE];
static PDM_Filter_Handler_t PDM_FilterHandler;
static PDM_Filter_Config_t PDM_FilterConfig;
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t ret;
uint16_t loop;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("Hello World\r\n");
__HAL_RCC_CRC_CLK_ENABLE();
/* Init PDM filters */
PDM_FilterHandler.bit_order = PDM_FILTER_BIT_ORDER_MSB;
PDM_FilterHandler.endianness = PDM_FILTER_ENDIANNESS_LE;
PDM_FilterHandler.high_pass_tap = AUDIO_HIGH_PASS_TAP;
PDM_FilterHandler.out_ptr_channels = 1;
PDM_FilterHandler.in_ptr_channels = 1;
ret = PDM_Filter_Init((PDM_Filter_Handler_t *)(&PDM_FilterHandler));
if (ret != 0) {
printf("PDM_Filter_Init ret:%d\r\n", ret);
}
/* PDM lib config phase */
PDM_FilterConfig.mic_gain = 24;
PDM_FilterConfig.output_samples_number = PDM_SAMPLES_AT_EACH_CALL;
PDM_FilterConfig.decimation_factor = PDM_FILTER_DEC_FACTOR_64;
ret = PDM_Filter_setConfig((PDM_Filter_Handler_t *)&PDM_FilterHandler, &PDM_FilterConfig);
if (ret != 0) {
printf("PDM_Filter_setConfig ret:%d\r\n", ret);
}
for (loop = 0; loop < AUDIO_REC_BUFFER_SIZE / 2; loop++) {
printf("%02X ", RecordBuff[loop]);
if (loop % 32 == 31) {
printf("\n");
}
}
if (loop % 32 != 0) {
printf("\n");
}
memset(PlaybackBuff, 0, sizeof(PlaybackBuff));
//hcrc.Instance->CR = CRC_CR_RESET;
ret = PDM_Filter(RecordBuff, PlaybackBuff, &PDM_FilterHandler);
if (ret != 0) {
printf("PDM_Filter Fail, ret:%x\n", ret);
}
for (loop = 0; loop < PDM_SAMPLES_AT_EACH_CALL; loop++) {
printf("%04X ", RecordBuff[loop]);
if (loop % 32 == 31) {
printf("\n");
}
}
if (loop % 32 != 0) {
printf("\n");
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
以下是官方的工程中精简后的代码
#define PDM_SAMPLES_AT_EACH_CALL 20
#define AUDIO_REC_BUFFER_SIZE (PDM_SAMPLES_AT_EACH_CALL * 8 * 2)
#define AUDIO_PLAY_BUFFER_SIZE (PDM_SAMPLES_AT_EACH_CALL * 4)
#define AUDIO_HIGH_PASS_TAP 2122358088
static uint8_t RecordBuff[AUDIO_REC_BUFFER_SIZE] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x96, 0x65, 0x9A, 0x5A, 0x36, 0xCA, 0x95, 0xA5, 0x69, 0x66, 0x0D, 0x73, 0x4E, 0x99, 0x6A, 0x9A, 0x93, 0xA9, 0xA6, 0x95,
0x23, 0x67, 0x96, 0x99, 0xA5, 0x99, 0x69, 0x96, 0x65, 0xA6, 0xA6, 0x55, 0x66, 0x26, 0x8F, 0x99, 0x59, 0xA6, 0x69, 0x5A, 0xE5, 0x30, 0x67, 0x93, 0xA9, 0x66, 0x66, 0x23, 0x67, 0x96, 0x65, 0x9A,
0xA6, 0x8D, 0xC9, 0x95, 0x59, 0xA6, 0xA6, 0x35, 0x9C, 0x69, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x96, 0x59, 0xA6, 0x69, 0x5A, 0xA3, 0x5C, 0x59, 0x9A, 0x69, 0x96, 0x65, 0xC9, 0x59,
};
static uint16_t PlaybackBuff[AUDIO_PLAY_BUFFER_SIZE];
static PDM_Filter_Handler_t PDM_FilterHandler;
static PDM_Filter_Config_t PDM_FilterConfig;
int main(void)
{
uint32_t ret, loop;
/* STM32WBAxx HAL library initialization:
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure the System clock to have a frequency of 100 MHz */
SystemClock_Config();
/* Add your application code here
*/
MX_USART1_UART_Init();
printf("Hello World\r\n");
__HAL_RCC_CRC_CLK_ENABLE();
/* Init PDM filters */
PDM_FilterHandler.bit_order = PDM_FILTER_BIT_ORDER_MSB;
PDM_FilterHandler.endianness = PDM_FILTER_ENDIANNESS_LE;
PDM_FilterHandler.high_pass_tap = AUDIO_HIGH_PASS_TAP;
PDM_FilterHandler.out_ptr_channels = 1;
PDM_FilterHandler.in_ptr_channels = 1;
ret = PDM_Filter_Init((PDM_Filter_Handler_t *)(&PDM_FilterHandler));
if (ret != 0) {
printf("PDM_Filter_Init ret:%d\r\n", ret);
}
/* PDM lib config phase */
PDM_FilterConfig.mic_gain = 24;
PDM_FilterConfig.output_samples_number = PDM_SAMPLES_AT_EACH_CALL;
PDM_FilterConfig.decimation_factor = PDM_FILTER_DEC_FACTOR_64;
ret = PDM_Filter_setConfig((PDM_Filter_Handler_t *)&PDM_FilterHandler, &PDM_FilterConfig);
if (ret != 0) {
printf("PDM_Filter_Init ret:%d\r\n", ret);
}
printf("PDM\r\n");
for (loop = 0; loop < AUDIO_REC_BUFFER_SIZE / 2; loop++) {
printf("0x%02X, ", RecordBuff[loop]);
if (loop % 32 == 31) {
printf("\r\n");
}
}
if (loop % 32 != 0) {
printf("\r\n");
}
/* Store values on Play buff */
ret = PDM_Filter(&RecordBuff[0], &PlaybackBuff[0], &PDM_FilterHandler);
printf("PCM ret:%d\r\n", ret);
for (loop = 0; loop < PDM_SAMPLES_AT_EACH_CALL; loop++) {
printf("0x%04X, ", PlaybackBuff[loop]);
if (loop % 32 == 31) {
printf("\r\n");
}
}
if (loop % 32 != 0) {
printf("\r\n");
}
/* Infinite loop */
while (1)
{
}
}
数据源都是数组,且数据相同。PDM_Filter的配置也相同。使用的PDM2PCM库也都是libPDMFilter_CM33_GCC_wc32.a,也同样是一块板子。不知道为何会有这样的差异
1.试试用官方例程的文件去一个个替换你原本工程的文件对比,有可能库有一定修改。
2.观察编译输出的各个关键变量的地址,看看是否存在4字节对齐问题
题主解决这个问题了吗?我也遇到了全是0的问题。