SD card initialization in SD mode

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

blue_z
Location: USA
Posts: 1906
Joined: Thu Apr 19, 2007 10:15 pm

Re: SD card initialization in SD mode

Mon Sep 15, 2014 9:23 pm

Owais wrote:My main goal at this point was to just write a raw data into an already formatted SD card and then read that data back
That's IMO putting the cart before the horse.
It's prudent to get the (non-destructive) read operational first, especially when a non-removable medium is used.
Once read capability is reliable, then attempt (destructive) write operations.
Owais wrote:... the read write operation given in the datasheet,
The microcontroller datasheet only provides you with a description of the host controller.
The MCI interface is actually a bus interface to intelligent peripherals.
You need to also reference the SDcard spec to which jharley has provided a link.

The SDcard spec describes the modes of the host and the states of the SDcard that can be used to create state machines in the device driver for the host side and for each socket/card.
Owais wrote: its just that if I give a data lets say a byte and try to write it randomly anywhere in the SD card would this work?
No, you cannot write just "a byte".
As I already wrote, the SDcard is a block device.

BTW although data transfers are always for a block, the address of that block depends on the SDcard capacity.
SDSC (i.e. 2GB card or smaller) uses a 32-bit byte address that should be block aligned.
SDHC and SDXC use a 32-bit block address.
Owais wrote: Also the SD card publishes a RCA(Relative Card Address) what is the purpose of that is it somehow used in writing or reading the very first data to the SD card?
Card addressing is implemented using a session address, assigned to the card during the initialization phase.
This is a mechanism for the host to reliably handle removable media.

A newly inserted SDcard will be in the "uninitialized" state and have a RCA of zero, which is otherwise invalid.
During the "card identification mode" the SDcard will generate its RCA. The host needs to verify that this SDcard's RCA is unique compared to any other (when there are multiple sockets).
The RDA is then used to select a SDcard when issuing commands during the "data transfer mode".
If the SDcard is removed or replaced, then there will no longer be a response to that RCA and a newly inserted SDcard will not be inadvertently written or read.

Regards
Sam++
Posts: 16
Joined: Tue Jul 01, 2014 9:05 pm

Re: SD card initialization in SD mode

Tue Sep 16, 2014 11:30 am

As an example, in the attached file there is a class implementing HSMCI SD card (read only for now).

I's by far perfect (had to figure out how to know if the card can operate in 4-bits mode and how to switch to that mode ;) ) but it is operational.

It can be used with FatFs.

Hope this helps,

Paul
Attachments
SPg_HSMCard.zip
(4.39 KiB) Downloaded 103 times
Owais
Posts: 57
Joined: Wed Jul 23, 2014 3:25 pm

Re: SD card initialization in SD mode

Tue Sep 16, 2014 5:13 pm

Thanks everyone for your detailed response and support. I have implemented the read function as attached, now I am looking into write function but I am not sure if it is correct. moreover I am just looking for reading and writing a single block of data that is 512 bytes. I have included the read function into FreeRTOS+FAT SL and it seems to be working fine without any errors. However the write function is not working at all please have a look in the attachment for both functions. If there is something wrong please point it out.

Regards,
Owais
Attachments
SD_READ_WRITE.zip
(1.19 KiB) Downloaded 116 times

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests