2021-02-16 07:54 AM
Hello,
I have a PDM MEMS microphone that I am reading in I2S master receive mode. I am trying to take this data and put it in a FIFO buffer and then output the data over UART using DMA. I must be confusing the buffer sizes because I get mostly zeros except for 64 Bytes of supposed data. Can someone please help me figure out where my issue is. Code is below. Thanks for any help.
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;
}