SPI模式下MCU對SD卡的控制及操作命令(轉)


源:SPI模式下MCU對SD卡的控制及操作命令

一、前言

        SD 卡有兩個可選的通訊協議:SD 模式和 SPI模式 SD 模式是SD 卡標准的讀寫方式,但是在選用SD 模式時,往往需要選擇帶有SD 卡控制器接口的 MCU,或者必須加入額外的SD卡控制單元以支持SD 卡的讀寫 然而,大多數MCU都沒有集成SD 卡控制器接口,若選用SD 模式通訊就無形中增加了產品的硬件成本。在SD卡數據讀寫時間要求不是很嚴格的情況下, 選用 SPI模式可以說是一種最佳的解決方案 因為在 SPI模式下,通過四條線就可以完成所有的數據交換,並且目前市場上很多MCU都集成 有現成的SPI接口電路,采用 SPI模式對 SD卡進行讀寫操作可大大簡化硬件電路的設計 

二、硬件電路實現

        以NXP的LPC2210 ARM7MCU為例,下圖是周立功開發的實現板電路

這里,將LPC2210MCU的SPI0用於SD卡的控制和數據讀寫。對SPI0的兩個數據線加了上拉電阻以便於MMC卡兼容。

卡供電采用了可控方式,通過GPIO口控制MOS管對其進行供電。

卡檢測電路也使用GPIO口實現。通過讀GPIO口數據,檢查卡是否寫保護和完全插入。

具體內容可以參考周立功的說明書,百度文庫里邊有

 

image

 

三、SD卡物理接口

我們看到的SD卡一包如下所示,包含9個引腳和一個寫保護開關:

image

其引腳定義如下:

 image

注:1.  S:電源;I:輸入;O:推挽輸出;PP:推挽I/O。         2.  擴展的DAT線(DAT1 ~ DAT3)在上電后處於輸入狀態。它們在執行SET_BUS_WIDTH命令后作為DAT線操作。當不使用DAT1 ~ DAT3 線時,主機應使自己的DAT1~DAT3線處於輸入模式。這樣定義是為了與MMC卡保持兼容。

      3.  上電后,這條線為帶 50KΩ上拉電阻的輸入線(可以用於檢測卡是否存在或選擇 SPI 模式) 。用戶可以在正常的數據傳輸中用 SET_CLR_CARD_DETECT(ACMD42)命令斷開上拉電阻的連接。MMC卡的該引腳在SD模式下為保留引腳,在SD模式下無任何作用。  4.  MMC卡在SD模式下為:I/O/PP/OD。  5.  MMC卡在SPI模式下為:I/PP。

      

 

 

 

四、對SD卡的控制流程

1、SD卡的SPI工作模式

SD 卡在上電初期自動進入SD 總線模式,在此模式下向 SD 卡發送復位命令CMD0 。如果SD卡在接收復位命令過程中CS低電平有效,則進入SPI模式,否則工作在SD 總線模式。

下邊是插入SD卡,並初始化為SPI模式的流程圖:(至於CMD××究竟是什么樣的命令,本文最后會附上

image

在復位成功之后可以通過CMD55和ACMD41 判斷當前電壓是否在工作范圍內 主機還可以繼續通過CMD10讀取SD 卡的CID寄存器,通過CMD16 設置數據 Block長度,通過CMD9 讀取卡的 CSD寄存器 從CSD 寄存器中,主機可獲知卡容量,支持的命令集等重要參數。

 

2、數據塊的讀寫

完成SD 卡的初始化之后即可進行它的讀寫操作 SD卡的讀寫操作都是通過發送 SD 卡命令完成的SPI總線模式支持單塊(CMD24)和多塊(CMD25)寫操作,多塊操作是指從指定位置開始寫下去,直到SD 卡收到一個停止命令CMD12才停止 單塊寫操作的數據塊長度只能是512 字節 單塊寫入時,命令為CMD24,當應答為0時說明可以寫入數據,大小為512 字節 SD 卡對每個發送給自己的數據塊都通過一個應答命令確認,它為1個字節長,當低 5位為00101 時,表明數據塊被正確寫入SD 卡     在需要讀取SD 卡中數據的時候,讀SD卡的命令字為CMD17,接收正確的第一個響應命令字節為0xFE,隨后是512 個字節的用戶數據塊,最后為2 個字節的CRC驗證碼    可見,讀寫SD 卡的操作都是在初始化后基於 SD 卡命令和響應完成操作的,寫、讀 SD 卡的程序流程圖如下所示  :

(1)寫SD卡流程

image

(2)讀SD卡流程

image

 

五、SD卡的操作命令集合

對SD卡的操作就靠這些命令來實現的。一下命令來自周立功的SD/MMC中間件。我查了好多地方都只顯示CMD0、CMD1之類的東西,而沒人說這些東西是什么。就貼到這里供參考,這也是我寫這個博客的目的,因為這些命令我找了好久的。關於SD,我一句操作代碼都沒貼。。。

/* 命令響應定義 define command's response */
#define R1 1
#define R1B 2
#define R2 3
#define R3 4

/**********************************************

     SD卡SPI模式下命令集

**********************************************/

/******************************** 基本命令集 Basic command set **************************/
/* 復位SD 卡 Reset cards to idle state */
#define CMD0 0
#define CMD0_R R1

/* 讀OCR寄存器 Read the OCR (MMC mode, do not use for SD cards) */
#define CMD1 1
#define CMD1_R R1

/* 讀CSD寄存器 Card sends the CSD */
#define CMD9 9
#define CMD9_R R1

/* 讀CID寄存器 Card sends CID */
#define CMD10 10
#define CMD10_R R1

/* 停止讀多塊時的數據傳輸 Stop a multiple block (stream) read/write operation */
#define CMD12 12
#define CMD12_R R1B

/* 讀 Card_Status 寄存器 Get the addressed card's status register */
#define CMD13 13
#define CMD13_R R2

/***************************** 塊讀命令集 Block read commands **************************/

/* 設置塊的長度 Set the block length */
#define CMD16 16
#define CMD16_R R1

/* 讀單塊 Read a single block */
#define CMD17 17
#define CMD17_R R1

/* 讀多塊,直至主機發送CMD12為止 Read multiple blocks until a CMD12 */
#define CMD18 18
#define CMD18_R R1

/***************************** 塊寫命令集 Block write commands *************************/
/* 寫單塊 Write a block of the size selected with CMD16 */
#define CMD24 24
#define CMD24_R R1

/* 寫多塊 Multiple block write until a CMD12 */
#define CMD25 25
#define CMD25_R R1

/* 寫CSD寄存器 Program the programmable bits of the CSD */
#define CMD27 27
#define CMD27_R R1

/***************************** 寫保護 Write protection *****************************/
/* Set the write protection bit of the addressed group */
#define CMD28 28
#define CMD28_R R1B

/* Clear the write protection bit of the addressed group */
#define CMD29 29
#define CMD29_R R1B

/* Ask the card for the status of the write protection bits */
#define CMD30 30
#define CMD30_R R1

/***************************** 擦除命令 Erase commands *******************************/
/* 設置擦除塊的起始地址(只用於SD卡) Set the address of the first write block to be erased(only for SD) */
#define CMD32 32
#define CMD32_R R1

/* 設置擦除塊的終止地址(只用於SD卡) Set the address of the last write block to be erased(only for SD) */
#define CMD33 33
#define CMD33_R R1

/* 設置擦除塊的起始地址(只用於MMC卡) Set the address of the first write block to be erased(only for MMC) */
#define CMD35 35
#define CMD35_R R1

/* 設置擦除塊的終止地址(只用於MMC卡) Set the address of the last write block to be erased(only for MMC) */
#define CMD36 36
#define CMD36_R R1

/* 擦除所選擇的塊 Erase the selected write blocks */
#define CMD38 38
#define CMD38_R R1B

/***************************** 鎖卡命令 Lock Card commands ***************************/
/* 設置/復位密碼或上鎖/解鎖卡 Set/reset the password or lock/unlock the card */
#define CMD42 42
#define CMD42_R    R1B
/* Commands from 42 to 54, not defined here */

/***************************** 應用命令 Application-specific commands ****************/
/* 禁止下一個命令為應用命令  Flag that the next command is application-specific */
#define CMD55 55
#define CMD55_R R1

/* 應用命令的通用I/O  General purpose I/O for application-specific commands */
#define CMD56 56
#define CMD56_R R1

/* 讀OCR寄存器  Read the OCR (SPI mode only) */
#define CMD58 58
#define CMD58_R R3

/* 使能或禁止 CRC Turn CRC on or off */
#define CMD59 59
#define CMD59_R R1

/***************************** 應用命令 Application-specific commands ***************/
/* 獲取 SD Status寄存器 Get the SD card's status */
#define ACMD13 13
#define ACMD13_R R2

/* 得到已寫入卡中的塊的個數 Get the number of written write blocks (Minus errors ) */
#define ACMD22 22
#define ACMD22_R R1

/* 在寫之前,設置預先擦除的塊的個數 Set the number of write blocks to be pre-erased before writing */
#define ACMD23 23
#define ACMD23_R R1

/* 讀取OCR寄存器 Get the card's OCR (SD mode) */
#define ACMD41 41
#define ACMD41_R R1

/* 連接/斷開CD/DATA[3]引腳上的上拉電阻 Connect or disconnect the 50kOhm internal pull-up on CD/DAT[3] */
#define ACMD42 42
#define ACMD42_R R1

/* 讀取SCR寄存器 Get the SD configuration register */
#define ACMD51 51
#define ACMD51_R R1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM