SD card interfacing

Hi,

I have written SD & SDHC card routine for reading, writing and erasing data from SD card. It used the hardware SPI of nRF51822.

NOTE:

1)Define the PINs of SD card first before initialing.

2)Define MOSI, MISO and SCK pins only, define the CS pin to some other pin that is not being used( these pin are define in spi_master_config.h )

3)CS pin should be separately define in SD_Routines.c by the name #define MMCSD_PIN_SELECT 29

4)If you are facing problem in using my library then first test you SPI separately.

SD_Routines.c SD_Routines.h

  • Hi. Which SDK is this for interfacing SD card? I tried the one with SDK 10, but nothing worked out, I am getting error in the file nrf_spim.h where it says NRF_SPIM_TYPE is undefined. Please help fix this?

  • Hi All,

    I just seen this post and it seems to be useful to me. I tried the earlier approach specified in above two library files using SPI. I interfaced NRF52 with SD card.

    In first approach, I have tried with SD card and SDHC card. In both cases initialization failed massage came. I am using library files specified by Stanislav Silnitskiy .

    I tried FATFS example to interface SD card with nrf52. It works fine . But for writing at high rate based on SAADC interrupt, I am getting some issues specified at this link

    My code with fatfs is specified at above mentioned link.

    Does any one has any idea why I am getting these issues and how Can resolve them?

  • Thanks a lot, the example is very helpful, everything works with the new libraies from SDK12.2.0.

  • Just mentioning that the newest SDK (12.2.0) has support for SD cards. I advise you to take a look at the SD card example here. If you have questions to the SD card library/example in the SDK, you can post a question on the forum and we will help you. The SD card library in the SDK is also based on FATFS.

  • Hi,

    thanks a lot Kumar for sharing your code. I try to use the FatFs with your library on an nRF52 developer board to communicate over SPI with a SD Card.

    But I'm facing some problems trying to write something in a text file on the SD Card. Initialzing the Card and create, open or read a text file works. But when I want to write in my text file the file stays empty. There is no error message in the return value of the f_write function but the f_close function returns FR_DISK_ERR. While debugging I see that in my code the Bytes I want to write in the File are even in the Buffer of the file variable. But when I read out the Card on my PC (with a text editor) the text file is still empty.

    When I write something in my file from the PC and save it, the next time I try to write over SPI on the Card the Bytes that already exists are overwritten by the new values I want to write. But only the ones who already exist are overwritten and no additional Bytes. In this case the f_open function returns FR_OK.

    Here's my code (I use the state variable only for debugging):

    int main(void)
    {
    
    	BYTE data[512];
    	FATFS FatFs;       /* FatFs work area needed for each volume */
    	FIL Fil;           /* File object needed for each open file */
    
    
    	UINT data_len = 64;
    	UINT bw;
    	uint8_t state=0;
    
    	bleStackInit();
    	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    
    
    	memset(data, 97, sizeof data);
            sd_spi_init();
    
    	while(1)
    	{
    		
    		f_mount(&FatFs, "", 0);  /* Give a work area to the default drive */
    
    		/* Opens an existing file. If not exist, creates a new file. */
    		if(f_open(&Fil, "logfile.txt", FA_WRITE| FA_READ | FA_OPEN_ALWAYS)!= FR_OK)
    		{
    			state= 4;
    		}
    
    
    		if(f_write(&Fil,data,data_len,&bw)!= FR_OK) //f_printf(&Fil, "%d", u)
    		{
    			state=2;
    		}
    
    		/* Close the file */
    		 if(f_close(&Fil)!= FR_OK)
    		 {
    			 state=3;
    		 }
    		nrf_delay_ms(1000);
    
    		if(state)
    		{
    
    		}
    	}
    
    }
    

    I'm using a 32 GB Sd Card. I have no other slaves for this SPI than the Card that's why my CS (SS) Pin is set on ground.

    I'm thankful for any advice.

    Regards Lea