I want to communicate with a microSD card with an ATmega32 using SPI. I make a simple SPI loopback, and it works fine.

Now, how can I communicate with the microSD card to write and read simple bytes or strings?

I am using a custom board, ATmega32 on a breadboard and an SD card module from eBay. I was looking for if ATmegas are compatible with FatFs and if they can open, read, and write text files with ASCII characters. How can I use FatFs?

  • 1
    \$\begingroup\$ Are you using Arduino with the SD shield? If not - specify the hardware. If yes - you have the SD library for arduino. \$\endgroup\$ – Eugene Sh. Dec 15 '14 at 22:10
  • \$\begingroup\$ i am using custom board, atmega32 on breadboard and sd module from ebay. i was looking that atmega are compatible with fatfs and they can open, read, and write txt files with ascii characters. how can i use fatfs? \$\endgroup\$ – MrBit Dec 15 '14 at 22:18
  • \$\begingroup\$ Before you are getting into higher level stuff, like filesystems, make sure you are able to communicate and initialize the card as described in the protocol, for example here :alumni.cs.ucr.edu/~amitra/sdcard/Additional/… \$\endgroup\$ – Eugene Sh. Dec 15 '14 at 22:25
  • \$\begingroup\$ Have you tried using any of the SD or MultiMediaCard libraries? \$\endgroup\$ – davidcary Dec 17 '14 at 4:10

However you intent to proceed from there, your first step is clear: You need to make a function that reads one block (512 bytes) of data from the SD card. To do that, you will need to issue SD (or MMC) commands to initialize and select the card. These involve commands such as GO TO IDLE STATE, GO TO NOT IDLE STATE, SEND OPERATING CONDITIONS. Then read the OCR register to see if it's a High Capacity card or not (these are addressed differently, for old card an address is a byte address; for HC card an address is a block number (LBA)), and set block length of 512 bytes. Then you can give the READ_BLOCK command to get actual disk data. This looks complicated (and is) but you can google with the command names to find lots of code that initializes SD cards.

First read the block 0 (first 512 bytes) from the SD card; it ends with 0x55aa. Then go on from there. I've been told that a library of code called fatfs can do the job of handling FAT disks quite well, but just for reading files, the code to find and read files from a FAT filesystem is not that complex. But do check out fatfs - I'm told that you just need to supply the read block + write block functions to it and it will take care of the rest.

| improve this answer | |
  • \$\begingroup\$ i found a FATFS library but it works with GPIO pins. I want to work with SPI module of atmega32 \$\endgroup\$ – MrBit Dec 17 '14 at 18:38
  • \$\begingroup\$ Then you should implement f_read and f_write functions that use the SPI module, and have fatFS use those functions to access the card. Hmm, I would think someone would have implemented those functions already. I wouldn't know, I've written my own fat filesystem handler from scratch. Sorry, I cannot be of any more assistance. \$\endgroup\$ – PkP Dec 17 '14 at 20:07
  • \$\begingroup\$ i'm searching for fatfs which use spi functions to access the card. any example? \$\endgroup\$ – MrBit Dec 17 '14 at 20:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.