+ All Categories
Home > Documents > David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From...

David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From...

Date post: 31-Jan-2018
Category:
Upload: dodan
View: 224 times
Download: 3 times
Share this document with a friend
28
David Harrison, CEO/Design Engineer for Model Sounds Inc. 1
Transcript
Page 1: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

David Harrison,

CEO/Design Engineer for Model Sounds Inc.

1

Page 2: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Overview The Secure Digital standard was introduced in August 1999 as an

improvement over MultiMediaCards (MMC). The Secure Digital standard is maintained by the Secure Digital Association (SDA).

The Secure Digital format includes four card families available in three different form factors. The four families are the original Standard-Capacity (SDSC), the High-Capacity (SDHC), the eXtended-Capacity (SDXC), and the SDIO, which combines input/output functions with data storage.

The three form factors are the original size, the mini size, and the micro size. Electrically passive adapters allow a smaller card to fit and function in a device built for a larger card.

There are many combinations of form factors and device families, although the prevailing formats are full or micro-SDHC and full or micro-SDXC.

David Harrison – June 17, 2015 2

Page 3: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Physical Formats

David Harrison – June 17, 2015 3

ORIGINAL FULL SIZE SD CARD MINI-SD (rarely used now) Micro-SD (ubiquitous)

Page 4: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Capacities

David Harrison – June 17, 2015 4

• SD STANDARD CAPACITY – SDSC : UP TO 4GB The internal 32 bit address is byte oriented – hence 4GB limit. Default format is FAT16.

• SD HIGH CAPACITY – SDHC : 4GB – 32GB

The internal 32 bit address is sector (512 Bytes) oriented. Default format is FAT32.

• SD EXTENDED CAPACITY – SDXC : 32GB – 2TB

The internal 32 bit address is sector (512 Bytes) oriented. Default format is exFAT.

Page 5: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SDIO Cards - 1

David Harrison – June 17, 2015 5

• SDIO cards use the SD card interface to provide high speed I/O access to various peripherals such as : 1. Bluetooth® adapters. 2. GPS receivers. 3. Television tuners. 4. Voice recorders. 5. Fingerprint readers. 6. Wi-Fi adaptors.

• Do not confuse SDIO with the SD card interface.

• Some uC manufacturers call their SD card interface,

an SDIO interface.

Page 6: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SDIO Cards - 2

David Harrison – June 17, 2015 6

Page 7: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Logos

David Harrison – June 17, 2015 7

Little Known Trivia: The trademarked SD logo was originally developed for the Super Density Disc, which was the unsuccessful Toshiba entry in the DVD format war. For this reason the D within the logo resembles an optical disc.

Page 8: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Ultra High Speed (UHS)

David Harrison – June 17, 2015 8

UHS-I Specified in SD version 3.01, supports a clock frequency of 100 MHz (a quadrupling of the original "Default Speed"), which, in four-bit transfer mode could transfer 50 MB/s (SDR50). UHS-I cards declared as UHS104 (SDR104) also support a clock frequency of 208 MHz, which could transfer 104 MB/s. Double data rate operation at 50 MHz (DDR50) is also specified in Version 3.01, and is mandatory for micro-SDHC and micro-SDXC cards labeled as UHS-I. In this mode, four bits are transferred when the clock signal rises and another four bits when it falls, transferring an entire byte on each full clock cycle.

UHS-II Specified in version 4.0, further raises the data transfer rate to a theoretical maximum of 156 MB/s (half duplex) or 312 MB/s (full duplex) using an additional row of pins (a total of 17 pins for full-size and 16 pins for micro-size cards).

Cards that comply with UHS show Roman numerals 'I' or 'II' next to the SD card logo, and report this capability to the host device. Use of UHS-I requires that the host device command the card to drop from 3.3-volt to 1.8-volt operation and select the four-bit transfer mode, while UHS-II requires 0.4-volt operation.

Page 9: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Association – SDCARD.ORG

Can download many “Simplified” specifications. The full spec. versions have to be purchased and are very expensive.

SDA membership dues are USD$2000 per year.

Recommended reading : “Physical Layer Simplified Specification – 4.10” at https://www.sdcard.org/downloads/pls/part1_410.pdf

SD Card Formatter https://www.sdcard.org/downloads/formatter_4/index.html

David Harrison – June 17, 2015

9

Page 10: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

Recommended Further Reading

“SDCARD.ORG – Speed Class Explanations” at https://www.sdcard.org/developers/overview/speed_class

SparkFun Tutorial : SD Cards and Writing Disk Images https://www.sparkfun.com/news/1849

David Harrison – June 17, 2015 10

Page 11: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Physical Connections – SPI Mode

David Harrison – June 17, 2015 11

SD Pin Micro SD Pin

Name I/O Description

1 2 nCS I Not Chip Select

2 3 SDI I Serial Data In

3 - VSS P Ground

4 4 VDD P +3.3V Power

5 5 SCLK I Serial Clock In

6 6 VSS P Ground

7 7 SDO O Serial Data Out

8 8 NC - No Connection

9 1 NC - No Connection

Page 12: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Physical Connections – SD Card Mode

David Harrison – June 17, 2015 12

SD Pin Micro SD Pin

Name I/O Description

1 2 DAT3 I/O SD Serial Data 3

2 3 CMD I/O Command/Response

3 - VSS P Ground

4 4 VDD P +3.3V Power

5 5 SCLK I Serial Clock In

6 6 VSS P Ground

7 7 DAT0 I/O SD Serial Data 0

8 8 DAT1 I/O SD Serial Data 1

9 1 DAT2 I/O SD Serial Data 2

Page 13: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Physical Connections – Development

David Harrison – June 17, 2015 13

• The Full Size SD card connector pads are conveniently 0.1” pitch so :-

• For “Breadboard” development, can use full size SD card connectors soldered to a 0.1” pitch copper strip board.

• Or can use a micro-SD to SD card adaptor and solder wires directly to the adaptor pads.

• Keep wires relatively short e.g. less than 80mm.

• Clock and signal speed is 24MHz.

• Wire a 0.1uF ceramic capacitor across the VDD/VSS Lines as close as possible to the connector or adaptor. Wire BOTH VSS Lines to system GND.

Page 14: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SPI BUS Review - 1

David Harrison – June 17, 2015

e.g. microcontroller e.g. SD Card Nomenclature should really be SDO and SDI.

Can have multiple slaves on the same bus, but each one must have its own SS line (Slave Select). Slaves must tri-state their SDO (MISO) lines when not selected. For SD cards in SPI Mode, all transactions are byte oriented, but some SPI masters can also use 16 bit or 32 bit transfers.

14

Page 15: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card SPI Transaction Overview

David Harrison – June 17, 2015 15

All SD card SPI mode commands are sent in on the DataIn line and responses

(if any) are returned on the DataOut line.

Note: Above DataIn/DataOut communication protocol is for SPI mode ONLY.

SD Card Mode has bidirectional CMD and DATA lines and the communication

protocol is quite different.

Nearly all commands return a response. There is one (GO_IDLE_STATE)

i.e. software reset, which does not.

Page 16: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card SPI Command Format

David Harrison – June 17, 2015 16

All SD Memory Card commands are 6 bytes (48 bits) long. The command transmission always starts with the left most bit 47 MSB first. All commands are protected by a CRC, unless CRC is disabled (SPI mode allowed only, not in SD Card mode).

Note: The start bit is a “zero” so there must be pull-ups on the SD Card CMD line (and the nSS line) and the MOSI pin of the microcontroller must be initialized to a logic high. Responses vary with commands, but fall into six types designated : R1, R1b, R2, R3, R6, R7 – explained later. Response types R4, R5 are reserved for SDIO card use.

Page 17: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Internal Architecture

David Harrison – June 17, 2015 17

SD Card Internal Registers

Name Bit Width

Description

OCR 32 Operation Conditions Register

CID 128 Card Identification Register

RCA 16 Relative Call Address

DSR 16 Driver Stage Register (Optional)

CSD 128 Card Specific Data

SCR 64 SDCard Configuration Register

SSR 512 SDCard Status Register

Page 18: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Commands - 1

David Harrison – June 17, 2015 18

CMD INDEX RESP. ABBREVIATION Description

CMD0 None GO_IDLE_STATE Resets card to idle state.

CMD2 R2 ALL_SEND_CID Asks any card to send the CID register.

CMD3 R6 SEND_RELATIVE_ADDR Asks Card to publish a new RCA address.

CMD6 R1 SWITCH_FUNC Switches Card function

CMD8 R7 SEND_IF_COND Asks card to send interface condition

CMD9 R1 SEND_CSD Asks card to send CSD register

CMD10 R1 SEND_CID Asks card to send CID register

CMD12 R1b STOP_TRANSMISSION Forces Stop Transmission in BLOCK_READ

CMD13 R2 SEND_STATUS Asks card to send SSR register

CMD16 R1 SET_BLOCKLEN Set BLOCK LENGTH for SDSC cards

Page 19: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Commands - 2

David Harrison – June 17, 2015 19

CMD INDEX RESP. ABBREVIATION Description

CMD17 R1 READ_SINGLE_BLOCK Reads single block of data.

CMD18 R1 READ_MULTI_BLOCKS Reads multiple blocks of data.

CMD24 R1 WRITE_SINGLE_BLOCK Writes single block of data.

CMD25 R1 WRITE_MULTI_BLOCKS Writes multiple blocks of data.

CMD32 R1 ERASE_WR_BLK_START_ADDRESS

Sets address of first write block to be erased.

CMD33 R1 ERASE_WR_BLK_END_ ADDRESS

Sets address of last write block to be erased.

CMD38 R1b ERASE Erases all previously selected blocks.

CMD55 R1 APP_CMD Says the next command is an APP cmd rather than a standard cmd.

CMD58 R3 READ_OCR Asks card to send OCR register

CMD59 R1 CRC_ON_OFF Turns CRC option on or off.

Page 20: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Application Specific Commands

David Harrison – June 17, 2015 20

CMD INDEX RESP. ABBREVIATION Description

ACMD13 R1 SEND_STATUS Asks card to send SSR register

ACMD22 R1 SEND_NUM_WR_BLOCKS Asks any card to return the number of well written (without errors) blocks.

ACMD23 R1 SET_WR_BLK_ERASE_ COUNT

Sets number of write blocks to be pre-erased before writing.

ACMD41 R1 SEND_OP_COND Sends host capacity support information and activates the card's initialization process.

ACMD42 R1 SET_CLR_CARD_ DETECT

Connect[1]/Disconnect[0] the 50KOhm pull-up resistor on CS (pin 1)of the card. The pull-up may be used for card detection.

ACMD51 R1 SEND_SCR Asks card to send SCR register

Page 21: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 1

David Harrison – June 17, 2015 21

1. Initialize the SPI GPIO pins.

2. Initialize the SPI module.

3. Write low level routines to put a byte to the SPI bus and get a byte from the SPI bus.

4. Write routines to send SD card commands and return SD card responses.

5. Implement the SD Card Initialization flow.

6. Write routines to Read/Write single and multi-blocks from/to the card.

7. Start accessing the card.

Page 22: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 2

David Harrison – June 17, 2015 22

Typical SPI initialization code.

void SDCARDSPIInit(void)

{

// Initialize SDCARD SPI - SPI3

SDCard.CardStatus = false;

// Enable SPI3 clock.

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

// Configure PC10 SPI3 SCLK & PC12 SPI3 DOUT as alternate function outputs

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

// Connect PC10 as AF SPI3 SCLK OUT

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_6);

// Connect PC12 as AF SPI3 DOUT

GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_6);

Page 23: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 3

David Harrison – June 17, 2015 23

// Configure PC11 SPI3 DIN as alternate function input

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // pull=up

GPIO_Init(GPIOC, &GPIO_InitStructure);

// Connect PC11 as AF SPI3 DIN (MISO)

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_6);

// Init SPI3 module

SPI_I2S_DeInit(SPI3);

SPI_InitTypeDef SPI_InitStructure;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI3, &SPI_InitStructure);

// Configure the RX FIFO Threshold

// This is necessary for 8bit data transfers!!

SPI_RxFIFOThresholdConfig(SPI3, SPI_RxFIFOThreshold_QF);

SPI_Cmd(SPI3, ENABLE);

} // End SDCARDSPIInit(void)

Page 24: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 4

David Harrison – June 17, 2015 24

static U8 SpiPutByte(U8 data) { // Wait until DR register is empty while (!(SPI3->SR & SPI_I2S_FLAG_TXE)); // Send the Data Byte through the SPI peripheral SPI3->DR = data; // Wait for any received data to be complete while (!(SPI3->SR & SPI_I2S_FLAG_RXNE)); // Return the Byte read from the SPI bus return SPI3->DR ; }

static U8 SpiGetByte() { // Wait until DR register is empty while (!(SPI3->SR & SPI_I2S_FLAG_TXE)); // Send a dummy Byte through the SPI peripheral SPI3->DR = 0xFF; // Wait for any received data to be complete while (!(SPI3->SR & SPI_I2S_FLAG_RXNE)); // Return the Byte read from the SPI bus return SPI3->DR ; }

Low level routines to put a byte to the SPI bus and get a byte from the SPI bus.

Page 25: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 5

David Harrison – June 17, 2015 25

U8 spi_send_and_read_sd_command_WithOut_CS(U8 command, U32 arg) { SpiGetByte(); // Dummy write – send 0xFF, sets the CMD line to a known logic high state. SpiPutByte(command); SpiPutByte((arg & 0xFF000000)>>24); // MSB of arg SpiPutByte((arg & 0x00FF0000)>>16); SpiPutByte((arg & 0x0000FF00)>>8); SpiPutByte((arg & 0x000000FF)); // LSB of arg SpiPutByte(0x95); // Checksum for CMD0 GO_IDLE_STATE and dummy checksum for other commands // End command - wait for response // If more than 8 retries, card has timed-out and returned the received 0xFF U8 retry = 0; U8 r1 = 0xFF; while ((r1 = SpiGetByte()) == 0xFF) { retry++; if (retry > 8) break; } return r1; }

Page 26: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 6

David Harrison – June 17, 2015 26

static U8 spi_send_and_read_sd_command_With_CS(U8 command, U32 arg) { SelectSDCard(); U8 r1 = spi_send_and_read_sd_command_WithOut_CS(command, arg); DeSelectSDCard(); return r1; }

Page 27: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 7

David Harrison – June 17, 2015 27

static Bool rcv_datablock(U8 *buff, U32 numBytes) { U8 token; do { // Wait for data packet token = SpiGetByte(); } while (token == 0xFF); if (token != SDC_STARTBLOCK_READ) return false; // If not valid data token, return with error do { // Receive the data block into buffer *buff++ = SpiGetByte(); *buff++ = SpiGetByte(); *buff++ = SpiGetByte(); *buff++ = SpiGetByte(); } while (numBytes -= 4); SpiGetByte(); // Discard CRC SpiGetByte(); return true; }

Page 28: David Harrison, CEO/Design Engineer for Model Sounds Inc. · PDF fileSD Card Access From Microcontroller - 2 David Harrison – June 17, 2015 22 Typical SPI initialization code. void

SD Card Access From Microcontroller - 8

David Harrison – June 17, 2015 28

U8 disk_read(U8 *buff, U32 startSector, U8 numSectors) { // Convert to byte address if needed for standard cards // SDHC uses sector addressing, not byte addressing if (SDCard.CardType != SDHC_CARD) startSector *= DEFAULT_SECTOR_SIZE; SelectSDCard(); // Pull nSS line low if (numSectors == 1) { // Single block read if ((spi_send_and_read_sd_command_WithOut_CS(READ_SINGLE_BLOCK, startSector) == 0) && rcv_datablock(buff, DEFAULT_SECTOR_SIZE)) } else { // Multiple block read if (spi_send_and_read_sd_command_WithOut_CS(READ_MULTI_BLOCK, startSector) == 0) { do { if (!rcv_datablock(buff, DEFAULT_SECTOR_SIZE)) break; buff += DEFAULT_SECTOR_SIZE; } while (--numSectors); spi_send_and_read_sd_command_WithOut_CS(STOP_TRANSMISSION, 0); } } DeSelectSDCard(); // Pull nSS line high return numSectors ? RES_ERROR : RES_OK; }


Recommended