This is a two-part series tutorial about the SD protocol used by SD flash memory cards.
Personally I learn better using practical examples instead of abstract data,
and for this reason I have constructed this tutorial as a step by step with
practical code examples written in C language, that can be followed by anyone
with basic programming skills and knowledge on how to use a microcontroller.
Although I am using an ATmega328P in this tutorial, the concepts extend to any
microcontroller.
In case you just need a library to read/write a memory card, you can find one here: https://www.programming-electronics-diy.xyz/2022/07/sd-memory-card-library-for-avr.html. This tutorial will help in understanding the low-level interactions between a microcontroller and a memory card but it will not be enough to use an SD card in a practical way. Apart from an SD card driver, that is the code described in this article, you will also need a file system driver such as FAT16 or FAT32 in order to read or write files. Writing and reading data to a memory card without a file system, is like using a large EEPROM.
Contents
1. General Description
1.1 Bus Mode and Clock Speed
1.2 Read/Write Mode Selection
2. SD Card Hardware Interface
2.1 microSD Card Schematic SPI Interface
3. SPI Setup
4. Card Initialization
4.1 Power Up Sequence
4.2 Sending Commands
4.3 Initialization Flow
4.4 CMD0
4.5 Response R1
4.6 CMD8
4.7 Response R7
4.9 CMD58
4.10 Response R3
4.11 ACMD41 & CMD55
5. Reading/Writing Data Blocks
5.1 Block Length
5.2 CMD17 – Reading a Single Block
5.2.1 Read Errors
5.3 CMD24 – Writing a Single Block
6. Links
1. General Description
The Secure Digital (SD) Card was developed by the
SD Association (SDA)
as an improvement over MMCs. The SD Card specifications were originally
defined by MEI (Matsushita Electric Company), Toshiba Corporation and SanDisk
Corporation. Currently, the specifications are controlled by the Secure
Digital Association (SDA).
In addition to the mass storage specific flash memory chip, the SD Card
includes an on-card intelligent controller which manages interface protocols,
security algorithms for copyright protection, data storage and retrieval, as
well as Error Correction Code (ECC) algorithms, defect handling and
diagnostics, power management and clock control.