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

Go To Last Post
354 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): 

Pages