请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 PDM转PCM库的使用问题

[复制链接]
技术宅男250 提问时间:2024-4-30 09:58 / 未解决

我在使用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,也同样是一块板子。不知道为何会有这样的差异

收藏 评论2 发布时间:2024-4-30 09:58

举报

2个回答
奇迹 回答时间:2024-5-9 14:48:58

1.试试用官方例程的文件去一个个替换你原本工程的文件对比,有可能库有一定修改。

2.观察编译输出的各个关键变量的地址,看看是否存在4字节对齐问题

zhoucancan 回答时间:2024-9-12 14:21:48

题主解决这个问题了吗?我也遇到了全是0的问题。

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版