5 years, 10 months ago.

Why does my microSDHC card periodically inhibit writing data to the card? (Edit: 3.3V supply issue)

I am using Neil Thiessen's excellent 2016 SD library to record sampled AnalogIn values to a 32GB SanDisk microSDHC card , which is connected on a SPI bus to mbed LPC1768

#include "mbed.h"
#include "SDFileSystem.h"
#define BUFFERSIZE 0x1000

The data is sampled at 8000Hz (by a function attached to a ticker) and stored in a circular buffer:

void sampleInput(void){ 
if (recording == true) {
        circularBuffer[writePointer] = (signalIn.read_u16() - 0x8000);
        writePointer = writePointer + 1;
        queue = queue + 1;
        writePointer = writePointer & (BUFFERSIZE-1);
    }
}

In the main function, the data is transferred in packets of 512 bytes to a SanDisk microSDHC card:

        while (recording == true) {
            if(queue > 512)
                fwrite(&circularBuffer[readPointer], 2, 256, fp);      
                readPointer = readPointer + 256;
                queue = queue - 256;
                readPointer = readPointer & (BUFFERSIZE-1);   
            }
        }

The pointers are wrapping correctly and data is written in an orderly fashion to the microSD card but after a certain recording duration (typically 4 minutes maximum), the circular buffer will suddenly fill up. If I reset the LPC1768 and try again, the same thing will happen, although after a different duration. This will normally be 2-4 minutes although is occasionally less than 20 seconds.

I am wondering if the SD card's internal controller somehow inhibits data transfer periodically due to its internal memory allocation or something else? Does anyone else have any experience of the same phenomenon and a possible solution? Doubling the buffersize hasn't helped. I plan to purchase and experiment with different SD cards types and sizes.

By the way, the beginning of my main function includes the following lines copied from Neil's "Hello World" program:

    //Configure CRC, large frames, and write validation
    sd.crc(true);
    sd.large_frames(true);
    sd.write_validation(true);

Neil's program returns the following information for my SanDisk card:

	Press the button to perform tests: 
	Mounting SD card...success!
	.Card type: SDHC
	.Sectors: 62333952
	.Capacity: 30436.5MB
	Testing 4096B write performance...done!
	.Result: 351.76KB/s
	Testing 4096B read performance...done!
	.Result: 564.64KB/s

Edit 1: I'm powering the LPC1768 from an isolated 6V supply, not the miniUSB connection. The SD card supply comes from the LPC1768 +3.3V output. There is a 100k pull-up resistor on MISO.

Edit 2: I've changed the card to a Kingston 1GB microSD. I've also added a 10uF tantalum capacitor between 3.3V and GND, right next to the SD card breakout board. The data recording now runs for as long as I want with no buffer overruns! However, looking at the 3.3V supply line on a scope, it is very noisy. Every 2.0 seconds, the supply line is pulled down approximately 100mV for a duration of approximately 100ms. Also, every 32ms (256 samples) there is a ringing on the supply line which lasts about 4ms. I plan to experiment with a separate 3.3V supply for the SD card, with an inline inductor to quieten the ringing. Are there any other hardware suggestions that might be worth considering?

Be the first to answer this question.