Not understanding PDM 2 PCM buffer sized
Hello,
I posted this in another area but it may be better placed here. I have a PDM MEMS microphone that I am reading in I2S master receive mode. What I would really like to do is store the PCM data into a ping pong buffer and then transmit the first half while the second half is being filled by the PDM2PCM function. I know my code is wrong here, and was hoping someone could steer me in the right direction.
What I am seeing is 64 bytes of data (supposedly) followed by 448 bytes of all zeros. I don't know if my buffer sized are wrong or if I am misunderstanding something else. I would assume 128 bytes of data continuously.
uint16_t pdmRxBuf[128];
uint16_t audio_out[64];
uint16_t MidBuffer[16];
void FifoWrite(uint16_t data)
{
fifoBuf[fifo_w_ptr] = data;
fifo_w_ptr++;
}
uint16_t FifoRead()
{
uint16_t val = fifoBuf[fifo_r_ptr];
fifo_r_ptr++;
return val;
}
HAL_I2S_Receive_DMA(&hi2s2, &pdmRxBuf[0],64);
HAL_UART_Transmit_DMA(&huart2, (uint8_t *) audio_out, 128);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (rxstate==1)
{
PDM_Filter(&pdmRxBuf[0],&MidBuffer[0], &PDM1_filter_handler);
for (int i=0; i<16;i++)
{ FifoWrite(MidBuffer[i]);
}
if (fifo_w_ptr-fifo_r_ptr > 128)
{
fifo_read_enabled=1;
}
rxstate=0;
}
if (rxstate==2)
{
PDM_Filter(&pdmRxBuf[64],&MidBuffer[0], &PDM1_filter_handler);
for (int i=0; i<16;i++)
{
FifoWrite(MidBuffer[i]);
}
rxstate=0;
}
if(half_complete == 1)
{
for (int i=0; i<32;i=i+1)
{
uint16_t data = FifoRead();
audio_out[i] = data;
}
half_complete = 0;
}
if(full_complete == 1)
{
for (int i=32; i<64;i=i+1)
{
uint16_t data = FifoRead();
audio_out[i] = data;
}
full_complete = 0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
{
half_complete = 1;
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
full_complete = 1;
}
void HAL_I2S_RxHalfCpltCallback (I2S_HandleTypeDef *hi2s) {
rxstate = 1;
}
void HAL_I2S_RxCpltCallback (I2S_HandleTypeDef *hi2s)
{
rxstate = 2;
}Thanks for any help, I appreciate it.
