SPI模式下MCU对SD卡的控制及操作命令
一、前言
??????? SD 卡有兩個可選的通訊協(xié)議:SD 模式和 SPI模式 SD 模式是SD 卡標準的讀寫方式,但是在選用SD 模式時,往往需要選擇帶有SD 卡控制器接口的 MCU,或者必須加入額外的SD卡控制單元以支持SD 卡的讀寫 然而,大多數(shù)MCU都沒有集成SD 卡控制器接口,若選用SD 模式通訊就無形中增加了產(chǎn)品的硬件成本。在SD卡數(shù)據(jù)讀寫時間要求不是很嚴格的情況下, 選用 SPI模式可以說是一種最佳的解決方案 因為在 SPI模式下,通過四條線就可以完成所有的數(shù)據(jù)交換,并且目前市場上很多MCU都集成
有現(xiàn)成的SPI接口電路,采用 SPI模式對 SD卡進行讀寫操作可大大簡化硬件電路的設(shè)計?
二、硬件電路實現(xiàn)
??????? 以NXP的LPC2210 ARM7MCU為例,下圖是周立功開發(fā)的實現(xiàn)板電路
這里,將LPC2210MCU的SPI0用于SD卡的控制和數(shù)據(jù)讀寫。對SPI0的兩個數(shù)據(jù)線加了上拉電阻以便于MMC卡兼容。
卡供電采用了可控方式,通過GPIO口控制MOS管對其進行供電。
卡檢測電路也使用GPIO口實現(xiàn)。通過讀GPIO口數(shù)據(jù),檢查卡是否寫保護和完全插入。
具體內(nèi)容可以參考周立功的說明書,百度文庫里邊有
?
?
三、SD卡物理接口
我們看到的SD卡一包如下所示,包含9個引腳和一個寫保護開關(guān):
其引腳定義如下:
?
注:1.? S:電源;I:輸入;O:推挽輸出;PP:推挽I/O。
?????? 2.? 擴展的DAT線(DAT1 ~ DAT3)在上電后處于輸入狀態(tài)。它們在執(zhí)行SET_BUS_WIDTH命令后作為DAT線操作。當不使用DAT1 ~ DAT3 線時,主機應(yīng)使自己的DAT1~DAT3線處于輸入模式。這樣定義是為了與MMC卡保持兼容。
????? 3.? 上電后,這條線為帶 50K?上拉電阻的輸入線(可以用于檢測卡是否存在或選擇 SPI 模式) 。用戶可以在正常的數(shù)據(jù)傳輸中用 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 卡發(fā)送復位命令CMD0 。如果SD卡在接收復位命令過程中CS低電平有效,則進入SPI模式,否則工作在SD 總線模式。
下邊是插入SD卡,并初始化為SPI模式的流程圖:(至于CMD××究竟是什么樣的命令,本文最后會附上)
在復位成功之后可以通過CMD55和ACMD41 判斷當前電壓是否在工作范圍內(nèi) 主機還可以繼續(xù)通過CMD10讀取SD 卡的CID寄存器,通過CMD16 設(shè)置數(shù)據(jù) Block長度,通過CMD9 讀取卡的 CSD寄存器 從CSD 寄存器中,主機可獲知卡容量,支持的命令集等重要參數(shù)。
?
2、數(shù)據(jù)塊的讀寫
完成SD 卡的初始化之后即可進行它的讀寫操作 SD卡的讀寫操作都是通過發(fā)送 SD 卡命令完成的SPI總線模式支持單塊(CMD24)和多塊(CMD25)寫操作,多塊操作是指從指定位置開始寫下去,直到SD 卡收到一個停止命令CMD12才停止 單塊寫操作的數(shù)據(jù)塊長度只能是512 字節(jié) 單塊寫入時,命令為CMD24,當應(yīng)答為0時說明可以寫入數(shù)據(jù),大小為512 字節(jié) SD 卡對每個發(fā)送給自己的數(shù)據(jù)塊都通過一個應(yīng)答命令確認,它為1個字節(jié)長,當?shù)?5位為00101 時,表明數(shù)據(jù)塊被正確寫入SD 卡?
? 在需要讀取SD 卡中數(shù)據(jù)的時候,讀SD卡的命令字為CMD17,接收正確的第一個響應(yīng)命令字節(jié)為0xFE,隨后是512 個字節(jié)的用戶數(shù)據(jù)塊,最后為2 個字節(jié)的CRC驗證碼??? 可見,讀寫SD 卡的操作都是在初始化后基于 SD 卡命令和響應(yīng)完成操作的,寫、讀 SD 卡的程序流程圖如下所示? :
(1)寫SD卡流程
(2)讀SD卡流程
?
五、SD卡的操作命令集合
對SD卡的操作就靠這些命令來實現(xiàn)的。一下命令來自周立功的SD/MMC中間件。我查了好多地方都只顯示CMD0、CMD1之類的東西,而沒人說這些東西是什么。就貼到這里供參考,這也是我寫這個博客的目的,因為這些命令我找了好久的。關(guān)于SD,我一句操作代碼都沒貼。。。
/* 命令響應(yīng)定義 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/* 停止讀多塊時的數(shù)據(jù)傳輸 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 **************************//* 設(shè)置塊的長度 Set the block length */ #define CMD16 16 #define CMD16_R R1/* 讀單塊 Read a single block */ #define CMD17 17 #define CMD17_R R1/* 讀多塊,直至主機發(fā)送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 *******************************/ /* 設(shè)置擦除塊的起始地址(只用于SD卡) Set the address of the first write block to be erased(only for SD) */ #define CMD32 32 #define CMD32_R R1/* 設(shè)置擦除塊的終止地址(只用于SD卡) Set the address of the last write block to be erased(only for SD) */ #define CMD33 33 #define CMD33_R R1/* 設(shè)置擦除塊的起始地址(只用于MMC卡) Set the address of the first write block to be erased(only for MMC) */ #define CMD35 35 #define CMD35_R R1/* 設(shè)置擦除塊的終止地址(只用于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 ***************************/ /* 設(shè)置/復位密碼或上鎖/解鎖卡 Set/reset the password or lock/unlock the card */ #define CMD42 42 #define CMD42_R R1B /* Commands from 42 to 54, not defined here *//***************************** 應(yīng)用命令 Application-specific commands ****************/ /* 禁止下一個命令為應(yīng)用命令 Flag that the next command is application-specific */ #define CMD55 55 #define CMD55_R R1/* 應(yīng)用命令的通用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/***************************** 應(yīng)用命令 Application-specific commands ***************/ /* 獲取 SD Status寄存器 Get the SD card's status */ #define ACMD13 13 #define ACMD13_R R2/* 得到已寫入卡中的塊的個數(shù) Get the number of written write blocks (Minus errors ) */ #define ACMD22 22 #define ACMD22_R R1/* 在寫之前,設(shè)置預先擦除的塊的個數(shù) 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
?
Technorati 標簽: SD卡,SPI,命令,周立功總結(jié)
以上是生活随笔為你收集整理的SPI模式下MCU对SD卡的控制及操作命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十三个代码注释的小技巧
- 下一篇: iphone NSNotificatio