cancel
Showing results for 
Search instead for 
Did you mean: 

Random glitch on I2S recordings

frnt
Senior

Hello,

I'm using an STM32 M4 for recording short audio signals through the I2S interface and I write the PCM to an SD card. To check that everything is fine I have recorded pure tones (1kHz or 2kHz). By doing that I noticed in some recordings (not all of them) some glitches in the audio signal. Can someone help understand why those glitches appear?

The reference manual reports that I2SxCLK frequency should be higher than the APB bus. In my case the APB is 16MHz and the I2SxCLK is 48MHz. Here is attached one of the 2s audio tracks recorded. Sometimes the glitch appears at the beginning of the track, some others later.

What else can generate this issue?

12 REPLIES 12

SD card can't store data fast enough?

JW

AScha.3
Chief III

obviously a dropout.

can't find my crystal ball right now....

sample rate ? DMA ? circular? block size ? buffers ? size? card interface spi/sd ? SDIO data rate ? filesystem? file size? SDcard size?

If you feel a post has answered your question, please click "Accept as Solution".
frnt
Senior

Sorry,

The I2S has:

  • sampling frequency 16kHz
  • MSB first
  • Data frame 24bits on 32 bits frame
  • clock polarity low
  • The DMA is configured in circular mode with a priority very high, preemption 2

The SPI configuration for the SD card is:

  • clock prescaler 2
  • baud rate 2 Mbits/s
  • clock polarity low
  • clock phase 1 edge
  • CRC calculation disabled
  • NSSP mode enabled
  • NSS signal type software
  • GPIO maximum output speed -> very high

The SD card size is 4GB SDHC (2.2 MB/s max speed) and I write 512 bytes at a time. The file system is fat32.

Do you need more information?

AScha.3
Chief III

buffers ? size? how long are dropouts ?

  • because i found: sometimes SDcards have some delay , depends on card and "age" (fill/using times)
  • so need buffer for min. 2ms to avoid dropouts on reading (writing could be more ! i didnnt test.)

If you feel a post has answered your question, please click "Accept as Solution".

I write 512 bytes at a time

That’s likely to be a problem.

You need to write multiple blocks in one hit with SD-Write-Multi in order to approach the rated speed of an SD card.

This is because internally the page size of an SD card is much larger than 512 bytes, so to write 512 it actually has to erase (say) 16k then copy over all-but-the-512-you’re-changing.

frnt
Senior

Dear all,

thanks for the answers. I report also the code snippet I use:

for(uint16_t i=0; i < N_DURATION; i++){
            while(dma_state == DMA_WAIT);
            if(dma_state == DMA_HALF){
                PDM_Filter(&pdm_data[0], &pcm_data[0], &PDM1_filter_handler);
            }
            else if(dma_state == DMA_FULL){
                PDM_Filter(&pdm_data[2048], &pcm_data[0], &PDM1_filter_handler);
            }
            f_state = f_write(&USERFile, &pcm_data[0], 512, &wb);
            dma_state = DMA_WAIT;
}

As you can see I'm only writing a single block (512) at a time. What I could try to do is increase the DMA buffer in order to write 1024 or 2048 bytes at a time. What do you think? I cannot increase it too much because the RAM is limited.

I have tried measuring the tick count for the PDM filter and the SD write. The SD write sometimes takes longer than usual... Instead, the PDM filter takes almost identical time at every hit.

AScha.3
Chief III

just guessing:

  • try 4k buffer size, because 4GB SDHC should have 4096 bytes clustersize
  • try 1..2k buffer , maybe buffer time long enough to run without dropouts

If you feel a post has answered your question, please click "Accept as Solution".
frnt
Senior

Dear @AScha.3​ ,

unfortunately, I cannot write directly 4k into the SD card because I would need a very large amount of RAM for the DMA to store PDM data and this is not available on the STM32 I'm using.

However, measuring the execution time of the PDM filter and SD write I noticed that sometimes the SD card takes more than 100ms to write 512bytes (usually it takes about 8/10).

Any suggestions?

AScha.3
Chief III

delay of 100ms or more is a problem...i had some big delays with cheap chinese SDcard.

so try new/other card from "good" source , i use only SanDisk ;

( Samsung, Kingston and Toshiba SDcards also good in my tests.)

If you feel a post has answered your question, please click "Accept as Solution".