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

Parents
  • 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

Comment
  • 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

Children
No Data