[TUT] [C] Getting SD/MMC card working painlessly with FatFS

Go To Last Post
356 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I didn't look at your entire code, but one thing catched my eye since I stumbled across it myself not too long ago.

 

 

In the main.c, you commented out the following lines:

#ifdef DRV_MMC
	xprintf(PSTR("MMC ==> %u\n"), DRV_MMC);
#endif

 

What happens if you put that code in your main function (or something equivalent) before calling disk_initialize? What value do you get for DRV_MMC?

 

It looks like you never defined "#define DRV_MMC", at least I couldn't find it.

 

Either disk_initialize itself returns STA_NOINIT if neither cf_disk_initialize nor mmc_disk_initialize got called (in diskio.c) or if it isn't cleared in the mmc_disk_initialize function (in mmc_disk_initialize.c).

 

If you did not define it, do so. I set a "#define DRV_MMC 0" in the diskio.h. Then test again what the lines above output.

 

And if I am not mistaken, you don't have to call disk_initialize yourself, you simply use f_mount.

For example:

FATFS fs;
FIL file1;
...
xprintf(PSTR("mount rc=%d\n"), f_mount(&fs,"",0));
xprintf(PSTR("open1 rc=%d\n"), f_open(&file1,"test.txt",FA_WRITE|FA_OPEN_ALWAYS));
...

 

 

The function f_mount will call find_volume which will then call disk_status and disk_initialize, if you call f_mount with opt == 1. Or it will be called when you use f_open which calls find_volume aswell.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello FileNotFound,

I wanted to see if there were any intialization errors as f_mount () initializes later (not at time of calling).

I came to this realization yesterday: the lack of DRV_MMC, however i did not place it in diskio.h. after doing so, mmc_disk_initialize () hangs. I placed a few printf statements in mmc_disk_initialize () and found that it hangs during the 80 dummy clocks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

quick update after playing around with it some more.  

 

I dug up an old version of ffexample from 2015 project attached below.  i corrected a couple of bugs in the mmc_avr.c file and have gotten SPI working.  When i run disk_initialize(0) i am successfully able to initialize the sd card, however, when i move onto f_mount() and f_open() specifically f_open() it hangs in the disk_initialize() function that is called out of find_volume() which is called from f_open().  

 

probing the SPI lines, MISO is still quite with 0 activity 

 

one weird thing that i noticed is the SD card still initializes even when its not inserted... 

 

this is what i fixed in mmc_avr.c 

 

#define	FCLK_SLOW() SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);  //SPCR = 0x52		// Set slow clock (F_CPU / 128) 
#define	FCLK_FAST()	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);           //SPCR = 0x50		// Set fast clock (F_CPU / 2) 

 

any help would be appreciated.  

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry i had classes getting in the way but i was able to figure it out.  Updated to the newest release, and a few minor typos in setting up SPI and some of the FFCONF settings.  Attached is a working project of the latest FATFS sample code for the ATEMGA 2560.  

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

desai401 wrote:

Sorry i had classes getting in the way but i was able to figure it out.  Updated to the newest release, and a few minor typos in setting up SPI and some of the FFCONF settings.  Attached is a working project of the latest FATFS sample code for the ATEMGA 2560.  

 

Hi Thanks for your code.

 

I have modifed the code for Sam3x8E Ardunio and using SD card shield sourced from china

Read file test and rest works ok only the ERROR: FR_DISK_ERR occurs when i try to write file using f_write function (since if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); line gets true) snippet of my code is as follows


    FATFS Ftfs;
	FILINFO finfo;
	FIL fil;        /* File object */
	FRESULT fr=FR_OK;     /* FatFs return code */
	FATFS *fs;
	char line[120];
    FSIZE_t offset;
    char Buff[4096];

    f_mount(&Ftfs, "", 1);

    printfGlobal("%s", "02 **** Write file test started ****\r\n");
    fr = f_stat("write.txt", &finfo);
    if(fr == FR_OK)
        {
            printfGlobal("write.txt file exists hence Deleting\r\n", fr);
            fr = f_unlink("write.txt");
            delay_ms(250);
        }

    fr = f_open(&fil, "write.txt", FA_WRITE | FA_CREATE_ALWAYS);
    if(fr != FR_OK)
        {error(fr); return;}

    offset = 0;
	printfGlobal("%s", "f_lseek started\r\n");
    fr = f_lseek(&fil, offset);
	if(fr != FR_OK)
		{error(fr); f_close(&fil); f_mount(NULL, "", 0); return;}

	memset(Buff, 0, sizeof(Buff));
	strcpy(Buff, "This data is written by SAM\r\n\0");
	printfGlobal("%s", "f_write started\r\n");
    fr = f_write(&fil, (const void*)Buff, strlen(Buff), &bw);
	if(fr != FR_OK)
		{error(fr); f_close(&fil); f_mount(NULL, "", 0); return;}

    f_sync(&fil);

    f_close(&fil);

 

 

Last Edited: Mon. Dec 25, 2017 - 03:35 PM

Pages