Reading/Writing to SD card using Petit FatFs?

fouadalnoor

Member
Hello guys,

I am currently trying to read/write to my sd card using the Petit FatFs System found here:



I am using MPLAB with a PIC18F14K50 to talk to the 8GB MicroSD card using SPI.

I am using the sample code for the pic24 that is given at the bottom of the Petit FatFs website (found under "Resources" on the website ). From what I understand I don't really need to change anything other than the platform dependent macros in the code.

That means, I only need to edit the files: pff.h and and diskio.c to fit my pic18.
Having done that, I still can't write anything to the SD card!

The program seems to be stuck somewhere inside the "disk_initialize" function. I dont use UART and since I am using a PIC18F14K50 I can't debug it using the MPLAB debugger. I have been trying to debug it visually, by turning on an LED on my low pin count development board after a function executes (it enters an infinate while loop).

Can you guys see anything wrong in the code that I have written, because at this point I am out of ideas!

The PIC24 Sample code is attached and my code is attached as well.

Hope you can help!
 

Attachments

Last edited:
Ok, so I have now tried to debug the code and found out that in the disk_initilize function, the program is stuck inside xmit_spi (inside the while loop). This can be found inside diskio.c

It seems like the buffer Full "BF" bit inside SSPSTAT is always 0 and hence the program never exits...

Any idea's how to fix this?
 
Remember the SD is 3 volts so the SDI into the micro may not be high enough.... Can you put a scope on and check the levels....ALSO... You need a GOOD decoupling cap on the SD card connections.

I used to use a 0.1uf right across the Vcc and ground pins..

SD cards can be a bit noisy..
 

Thanks for the response, I will be checking that now. But did you see anything obviusly wrong with my code?
 
Quite difficult to follow... I came to the same conclusion that you did that the only place it can hang is the two "while()" statements in the SPI read / write routines...

BUT!!! if you can get your head round the SPI transmission... you'll stand a good chance..

I believe that some SD cards are a bit finicky ... some work whereas others don't...
 

I see, but if you notice, the spi ports arent even initilized at this point.... The program goes,

main->disk_initilize -> disk_writep(0,0)-> then goes to the point that says "Finalize sector write process" xmit_spi(0) -> then get's stuck at while(!SSPSTATbits.BF) ;

The SPI ports would be initilized after executing disk_writep(0,0); (where it says init_spi(); )

I dont get why the buffer would even work when the SPI ports are not initialized...

I think its more of a coding problem then a hardware problem though..
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…