3.2 SPI硬件時序方式
上面的3.1小節(jié)是采用SPI模擬時序驅(qū)動SD NAND,STM32本身集成有SPI硬件模塊,可以直接利用STM32硬件SPI接口讀寫。
下面貼出底層的適配代碼。 上面貼出的驅(qū)動代碼里,已經(jīng)將驅(qū)動接口部分和協(xié)議邏輯部分區(qū)分開了,替換底層的SIP讀寫代碼非常方便。
(1)主要替換的代碼
/*
函數(shù)功能:SPI初始化(模擬SPI)
硬件連接:
MISO--->PB14
MOSI--->PB15
SCLK--->PB13
*/
void SPI_Init(void)
{
/*開啟時鐘*/
RCC->APB1ENR|=1<<14;? ?//開啟SPI2時鐘
RCC->APB2ENR|=1<<3;? ? //PB
GPIOB->CRH&=0X000FFFFF; //清除寄存器
GPIOB->CRH|=0XB8B00000;
GPIOB->ODR|=0X7<<13;? ? //PB13/14/15上拉--輸出高電平
/*SPI2基本配置*/
SPI2->CR1=0X0; //清空寄存器
SPI2->CR1|=0<<15; //選擇“雙線雙向”模式
SPI2->CR1|=0<<11; //使用8位數(shù)據(jù)幀格式進行發(fā)送/接收;
SPI2->CR1|=0<<10; //全雙工(發(fā)送和接收);
SPI2->CR1|=1<<9;? //啟用軟件從設(shè)備管理
SPI2->CR1|=1<<8;? //NSS
SPI2->CR1|=0<<7;? //幀格式,先發(fā)送高位
SPI2->CR1|=0x0<<3;//當(dāng)總線頻率為36MHZ時,SPI速度為18MHZ,高速。
SPI2->CR1|=1<<2;? //配置為主設(shè)備
SPI2->CR1|=1<<1;? //空閑狀態(tài)時, SCK保持高電平。
SPI2->CR1|=1<<0;? //數(shù)據(jù)采樣從第二個時鐘邊沿開始。
SPI2->CR1|=1<<6;? //開啟SPI設(shè)備。
}
/*
函數(shù)功能:SPI讀寫一個字節(jié)
*/
u8 SPI_ReadWriteOneByte(u8 data_tx)
{
u16 cnt=0;
while((SPI2->SR&1<<1)==0) //等待發(fā)送區(qū)空--等待發(fā)送緩沖為空
{
cnt++;
if(cnt>=65530)return 0; //超時退出 u16=2個字節(jié)
}
SPI2->DR=data_tx; //發(fā)送一個byte
cnt=0;
while((SPI2->SR&1<<0)==0) //等待接收完一個byte? ?
{
cnt++;
if(cnt>=65530)return 0; //超時退出
}
return SPI2->DR; //返回收到的數(shù)據(jù)
}
函數(shù)功能:SD卡底層接口,通過SPI時序向SD卡讀寫一個字節(jié)
函數(shù)參數(shù):data是要寫入的數(shù)據(jù)
返 回 值:讀到的數(shù)據(jù)
*/
u8 SDCardReadWriteOneByte(u8 DataTx)
{
return SPI_ReadWriteOneByte(DataTx);
}
(2)運行效果
3.3 SDIO方式
如果想提高SD NAND的讀寫速度,可以采用SDIO協(xié)議,STM32本身有SDIO的硬件支持,配置好SDIO的寄存器即可完成SD NAND的操作。 SDIO的數(shù)據(jù)線都比SPI多,讀寫速度自然沒法比的。
下面貼出STM32F103ZE上面編寫的SDIO協(xié)議讀寫SD NAND的驅(qū)動代碼。
(1)整體工程代碼
(2)sdio.c
#include "sdio_sdcard.h"
#include "string.h"
#include "sys.h"
#include "usart.h"
static u8 CardType=SDIO_STD_CAPACITY_SD_CARD_V1_1; //SD卡類型(默認(rèn)為1.x卡)
static u32 CSD_Tab[4],CID_Tab[4],RCA=0; //SD卡CSD,CID以及相對地址(RCA)數(shù)據(jù)
static u8 DeviceMode=SD_DMA_MODE; //工作模式,注意,工作模式必須通過SDIO_SdCardSetDeviceMode,后才算數(shù).這里只是定義一個默認(rèn)的模式(SD_DMA_MODE)
static u8 StopCondition=0; //是否發(fā)送停止傳輸標(biāo)志位,DMA多塊讀寫的時候用到
volatile SDIO_SD_ERROR_INFO TransferError=SD_OK; //數(shù)據(jù)傳輸錯誤標(biāo)志,DMA讀寫時使用
volatile u8 TransferEnd=0; //傳輸結(jié)束標(biāo)志,DMA讀寫時使用
SD_CardInfo SDCardInfo; //SD卡信息
//SDIO_SdCardReadDiskSector/SDIO_SdCardWriteDiskSector函數(shù)專用buf,當(dāng)這兩個函數(shù)的數(shù)據(jù)緩存區(qū)地址不是4字節(jié)對齊的時候,
//需要用到該數(shù)組,確保數(shù)據(jù)緩存區(qū)地址是4字節(jié)對齊的.
__align(4) u8 SDIO_DATA_BUFFER[512];
/*
SD卡與開發(fā)板的SDIO方式接線關(guān)系如下:
DATA0---PC8
DATA1---PC9
DATA2---PC10
DATA3---PC11
CLK-----PC1
CMD-----PD2
*/
/*
函數(shù)功能:SDIO方式初始化SD卡
返回值 :錯誤代碼;(0,無錯誤)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardInit(void)
{
u8 clkdiv=0;
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
//SDIO IO口初始化
RCC->APB2ENR|=1<<4;? ? //使能PORTC時鐘 ? ? ?
RCC->APB2ENR|=1<<5;? ? //使能PORTD時鐘
RCC->AHBENR|=1<<10;? ? //使能SDIO時鐘 ? ? ?
RCC->AHBENR|=1<<1;? ? //使能DMA2時鐘
GPIOC->CRH&=0XFFF00000;
GPIOC->CRH|=0X000BBBBB; //PC.8~12 復(fù)用輸出
GPIOD->CRL&=0XFFFFF0FF;
GPIOD->CRL|=0X00000B00; //PD2復(fù)用輸出,PD7 上拉輸入
//SDIO外設(shè)寄存器設(shè)置為默認(rèn)值
SDIO->POWER=0x00000000;
SDIO->CLKCR=0x00000000;
SDIO->ARG=0x00000000;
SDIO->CMD=0x00000000;
SDIO->DTIMER=0x00000000;
SDIO->DLEN=0x00000000;
SDIO->DCTRL=0x00000000;
SDIO->ICR=0x00C007FF;
SDIO->MASK=0x00000000;
STM32_NVIC_SetPriority(SDIO_IRQn,0,0); //SDIO中斷配置
errorstatus=SDIO_SdPowerON(); //SD卡上電
SDIO_SdCardInitializeCards(); //初始化SD卡
SDIO_SdCardGetInfo(&SDCardInfo); //獲取卡信息
SDIO_SdCardSelectAddr((u32)(SDCardInfo.RCA<<16));//選中SD卡? ?
SDIO_SdCardEnableWideBusOperation(1); //4位寬度,如果是MMC卡,則不能用4位模式
if((errorstatus==SD_OK)||(SDIO_MULTIMEDIA_CARD==CardType))
{
if(SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1||SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0)
{
clkdiv=SDIO_TRANSFER_CLK_DIV+6; //V1.1/V2.0卡,設(shè)置最高72/12=6Mhz
}else clkdiv=SDIO_TRANSFER_CLK_DIV; //SDHC等其他卡,設(shè)置最高72/6=12Mhz
SDIO_ClockSet(clkdiv); //設(shè)置時鐘頻率,SDIO時鐘計算公式:SDIO_CK時鐘=SDIOCLK/[clkdiv+2];其中,SDIOCLK固定為48Mhz
errorstatus=SDIO_SdCardSetDeviceMode(SD_POLLING_MODE); //設(shè)置為查詢模式
}
return errorstatus;
}
/*
函數(shù)功能: SDIO時鐘初始化設(shè)置
函數(shù)參數(shù):
clkdiv:時鐘分頻系數(shù)
CK時鐘=SDIOCLK/[clkdiv+2];(SDIOCLK時鐘固定為48Mhz)
*/
void SDIO_ClockSet(u8 clkdiv)
{
u32 tmpreg=SDIO->CLKCR;
tmpreg&=0XFFFFFF00;
tmpreg|=clkdiv;
SDIO->CLKCR=tmpreg;
}
/*
函數(shù)功能: SDIO發(fā)送命令函數(shù)
函數(shù)參數(shù):
cmdindex:命令索引,低六位有效
waitrsp:期待的相應(yīng).00/10,無響應(yīng);01,短響應(yīng);11,長響應(yīng)
arg:參數(shù)
*/
void SDIO_SendCmd(u8 cmdindex,u8 waitrsp,u32 arg)
{
u32 tmpreg;
SDIO->ARG=arg;
tmpreg=SDIO->CMD;
tmpreg&=0XFFFFF800; //清除index和waitrsp
tmpreg|=cmdindex&0X3F; //設(shè)置新的index
tmpreg|=waitrsp<<6; //設(shè)置新的wait rsp?
tmpreg|=0<<8; //無等待
tmpreg|=1<<10; //命令通道狀態(tài)機使能
SDIO->CMD=tmpreg;
}
/*
函數(shù)功能: SDIO發(fā)送數(shù)據(jù)配置函數(shù)
函數(shù)參數(shù):
datatimeout:超時時間設(shè)置
datalen:傳輸數(shù)據(jù)長度,低25位有效,必須為塊大小的整數(shù)倍
blksize:塊大小.實際大小為:2^blksize字節(jié)
dir:數(shù)據(jù)傳輸方向:0,控制器到卡;1,卡到控制器;
*/
void SDIO_SendDataConfig(u32 datatimeout,u32 datalen,u8 blksize,u8 dir)
{
u32 tmpreg;
SDIO->DTIMER=datatimeout;
SDIO->DLEN=datalen&0X1FFFFFF; //低25位有效
tmpreg=SDIO->DCTRL;
tmpreg&=0xFFFFFF08; //清除之前的設(shè)置.
tmpreg|=blksize<<4; //設(shè)置塊大小
tmpreg|=0<<2; //塊數(shù)據(jù)傳輸
tmpreg|=(dir&0X01)<<1; //方向控制
tmpreg|=1<<0; //數(shù)據(jù)傳輸使能,DPSM狀態(tài)機
SDIO->DCTRL=tmpreg;
}
/*
函數(shù)功能:卡上電
查詢所有SDIO接口上的卡設(shè)備,并查詢其電壓和配置時鐘
返回值:錯誤代碼;(0,無錯誤)
*/
SDIO_SD_ERROR_INFO SDIO_SdPowerON(void)
{
u8 i=0;
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u32 response=0,count=0,validvoltage=0;
u32 SDType=SD_STD_CAPACITY;
//配置CLKCR寄存器
SDIO->CLKCR=0; //清空CLKCR之前的設(shè)置
SDIO->CLKCR|=0<<9; //非省電模式
SDIO->CLKCR|=0<<10; //關(guān)閉旁路,CK根據(jù)分頻設(shè)置輸出
SDIO->CLKCR|=0<<11; //1位數(shù)據(jù)寬度
SDIO->CLKCR|=0<<13; //SDIOCLK上升沿產(chǎn)生SDIOCK
SDIO->CLKCR|=0<<14; //關(guān)閉硬件流控制? ??
SDIO_ClockSet(SDIO_INIT_CLK_DIV);//設(shè)置時鐘頻率(初始化的時候,不能超過400Khz)
SDIO->POWER=0X03; //上電狀態(tài),開啟卡時鐘
SDIO->CLKCR|=1<<8; //SDIOCK使能? ?
for(i=0;i<74;i++)
{
SDIO_SendCmd(SD_CMD_GO_IDLE_STATE,0,0);//發(fā)送CMD0進入IDLE STAGE模式命令.
errorstatus=SDIO_CmdErrorCheck();
if(errorstatus==SD_OK)break;
}
if(errorstatus)return errorstatus;//返回錯誤狀態(tài)
SDIO_SendCmd(SDIO_SEND_IF_COND,1,SD_CHECK_PATTERN);//發(fā)送CMD8,短響應(yīng),檢查SD卡接口特性.
//arg[11:8]:01,支持電壓范圍,2.7~3.6V
//arg[7:0]:默認(rèn)0XAA
//返回響應(yīng)7
errorstatus=SDIO_CmdResp7Error(); //等待R7響應(yīng)
if(errorstatus==SD_OK) //R7響應(yīng)正常
{
CardType=SDIO_STD_CAPACITY_SD_CARD_V2_0; //SD 2.0卡
SDType=SD_HIGH_CAPACITY; //高容量卡
}
SDIO_SendCmd(SD_CMD_APP_CMD,1,0); //發(fā)送CMD55,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_CMD); //等待R1響應(yīng)
if(errorstatus==SD_OK)//SD2.0/SD 1.1
{
//SD卡,發(fā)送ACMD41 SD_APP_OP_COND,參數(shù)為:0x80100000
while((!validvoltage)&&(count
{
SDIO_SendCmd(SD_CMD_APP_CMD,1,0); //發(fā)送CMD55,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_CMD); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
SDIO_SendCmd(SD_CMD_SD_APP_OP_COND,1,SD_VOLTAGE_WINDOW_SD|SDType);//發(fā)送ACMD41,短響應(yīng)
errorstatus=SDIO_CmdResp3Error(); //等待R3響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
response=SDIO->RESP1;; //得到響應(yīng)
validvoltage=(((response>>31)==1)?1:0); //判斷SD卡上電是否完成
count++;
}
if(count>=SD_MAX_VOLT_TRIAL)
{
errorstatus=SD_INVALID_VOLTRANGE;
return errorstatus;
}
if(response&=SD_HIGH_CAPACITY)
{
CardType=SDIO_HIGH_CAPACITY_SD_CARD;
}
}
return(errorstatus);
}
/*
函數(shù)功能: SD卡斷電
返回值:錯誤代碼;(0,無錯誤)
*/
SDIO_SD_ERROR_INFO SD_PowerOFF(void)
{
SDIO->POWER&=~(3<<0);//SDIO電源關(guān)閉,時鐘停止
return SD_OK;
}
/*
函數(shù)功能:初始化所有的卡,并讓卡進入就緒狀態(tài)
返回值:錯誤代碼
*/
SDIO_SD_ERROR_INFO SDIO_SdCardInitializeCards(void)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u16 rca = 0x01;
if((SDIO->POWER&0X03)==0)return SD_REQUEST_NOT_APPLICABLE;//檢查電源狀態(tài),確保為上電狀態(tài)
if(SDIO_SECURE_DIGITAL_IO_CARD!=CardType) //非SECURE_DIGITAL_IO_CARD
{
SDIO_SendCmd(SD_CMD_ALL_SEND_CID,3,0); //發(fā)送CMD2,取得CID,長響應(yīng)
errorstatus=SDIO_CmdResp2Error(); //等待R2響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
CID_Tab[0]=SDIO->RESP1;
CID_Tab[1]=SDIO->RESP2;
CID_Tab[2]=SDIO->RESP3;
CID_Tab[3]=SDIO->RESP4;
}
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_SECURE_DIGITAL_IO_COMBO_CARD==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))//判斷卡類型
{
SDIO_SendCmd(SD_CMD_SET_REL_ADDR,1,0); //發(fā)送CMD3,短響應(yīng)
errorstatus=SDIO_CmdResp6Error(SD_CMD_SET_REL_ADDR,&rca);//等待R6響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}
if(SDIO_MULTIMEDIA_CARD==CardType)
{
SDIO_SendCmd(SD_CMD_SET_REL_ADDR,1,(u32)(rca<<16));//發(fā)送CMD3,短響應(yīng) ? ?
errorstatus=SDIO_CmdResp2Error(); //等待R2響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}
if(SDIO_SECURE_DIGITAL_IO_CARD!=CardType) //非SECURE_DIGITAL_IO_CARD
{
RCA = rca;
SDIO_SendCmd(SD_CMD_SEND_CSD,3,(u32)(rca<<16));//發(fā)送CMD9+卡RCA,取得CSD,長響應(yīng) ? ?
errorstatus=SDIO_CmdResp2Error(); //等待R2響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
CSD_Tab[0]=SDIO->RESP1;
CSD_Tab[1]=SDIO->RESP2;
CSD_Tab[2]=SDIO->RESP3;
CSD_Tab[3]=SDIO->RESP4;
}
return SD_OK;//卡初始化成功
}
/*
函數(shù)功能:得到卡信息
函數(shù)參數(shù):
cardinfo:卡信息存儲區(qū)
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardGetInfo(SD_CardInfo *cardinfo)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u8 tmp=0;
cardinfo->CardType=(u8)CardType; //卡類型
cardinfo->RCA=(u16)RCA; //卡RCA值
tmp=(u8)((CSD_Tab[0]&0xFF000000)>>24);
cardinfo->SD_csd.CSDStruct=(tmp&0xC0)>>6; //CSD結(jié)構(gòu)
cardinfo->SD_csd.SysSpecVersion=(tmp&0x3C)>>2; //2.0協(xié)議還沒定義這部分(為保留),應(yīng)該是后續(xù)協(xié)議定義的
cardinfo->SD_csd.Reserved1=tmp&0x03; //2個保留位
tmp=(u8)((CSD_Tab[0]&0x00FF0000)>>16); //第1個字節(jié)
cardinfo->SD_csd.TAAC=tmp; //數(shù)據(jù)讀時間1
tmp=(u8)((CSD_Tab[0]&0x0000FF00)>>8); //第2個字節(jié)
cardinfo->SD_csd.NSAC=tmp; //數(shù)據(jù)讀時間2
tmp=(u8)(CSD_Tab[0]&0x000000FF); //第3個字節(jié)
cardinfo->SD_csd.MaxBusClkFrec=tmp; //傳輸速度
tmp=(u8)((CSD_Tab[1]&0xFF000000)>>24); //第4個字節(jié)
cardinfo->SD_csd.CardComdClasses=tmp<<4;? ? //卡指令類高四位
tmp=(u8)((CSD_Tab[1]&0x00FF0000)>>16); //第5個字節(jié)
cardinfo->SD_csd.CardComdClasses|=(tmp&0xF0)>>4;//卡指令類低四位
cardinfo->SD_csd.RdBlockLen=tmp&0x0F; //最大讀取數(shù)據(jù)長度
tmp=(u8)((CSD_Tab[1]&0x0000FF00)>>8); //第6個字節(jié)
cardinfo->SD_csd.PartBlockRead=(tmp&0x80)>>7; //允許分塊讀
cardinfo->SD_csd.WrBlockMisalign=(tmp&0x40)>>6; //寫塊錯位
cardinfo->SD_csd.RdBlockMisalign=(tmp&0x20)>>5; //讀塊錯位
cardinfo->SD_csd.DSRImpl=(tmp&0x10)>>4;
cardinfo->SD_csd.Reserved2=0; //保留
if((CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1)||(CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0)||(SDIO_MULTIMEDIA_CARD==CardType))//標(biāo)準(zhǔn)1.1/2.0卡/MMC卡
{
cardinfo->SD_csd.DeviceSize=(tmp&0x03)<<10; //C_SIZE(12位)
tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7個字節(jié)
cardinfo->SD_csd.DeviceSize|=(tmp)<<2;
tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8個字節(jié)
cardinfo->SD_csd.DeviceSize|=(tmp&0xC0)>>6;
cardinfo->SD_csd.MaxRdCurrentVDDMin=(tmp&0x38)>>3;
cardinfo->SD_csd.MaxRdCurrentVDDMax=(tmp&0x07);
tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9個字節(jié)
cardinfo->SD_csd.MaxWrCurrentVDDMin=(tmp&0xE0)>>5;
cardinfo->SD_csd.MaxWrCurrentVDDMax=(tmp&0x1C)>>2;
cardinfo->SD_csd.DeviceSizeMul=(tmp&0x03)<<1;//C_SIZE_MULT
tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10個字節(jié)
cardinfo->SD_csd.DeviceSizeMul|=(tmp&0x80)>>7;
cardinfo->CardCapacity=(cardinfo->SD_csd.DeviceSize+1);//計算卡容量
cardinfo->CardCapacity*=(1<<(cardinfo->SD_csd.DeviceSizeMul+2));
cardinfo->CardBlockSize=1<<(cardinfo->SD_csd.RdBlockLen);//塊大小
cardinfo->CardCapacity*=cardinfo->CardBlockSize;
}else if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //高容量卡
{
tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7個字節(jié)
cardinfo->SD_csd.DeviceSize=(tmp&0x3F)<<16;//C_SIZE
tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8個字節(jié)
cardinfo->SD_csd.DeviceSize|=(tmp<<8);
tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9個字節(jié)
cardinfo->SD_csd.DeviceSize|=(tmp);
tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10個字節(jié)
cardinfo->CardCapacity=(long long)(cardinfo->SD_csd.DeviceSize+1)*512*1024;//計算卡容量
cardinfo->CardBlockSize=512; //塊大小固定為512字節(jié)
}
cardinfo->SD_csd.EraseGrSize=(tmp&0x40)>>6;
cardinfo->SD_csd.EraseGrMul=(tmp&0x3F)<<1; ? ?
tmp=(u8)(CSD_Tab[2]&0x000000FF); //第11個字節(jié)
cardinfo->SD_csd.EraseGrMul|=(tmp&0x80)>>7;
cardinfo->SD_csd.WrProtectGrSize=(tmp&0x7F);
tmp=(u8)((CSD_Tab[3]&0xFF000000)>>24); //第12個字節(jié)
cardinfo->SD_csd.WrProtectGrEnable=(tmp&0x80)>>7;
cardinfo->SD_csd.ManDeflECC=(tmp&0x60)>>5;
cardinfo->SD_csd.WrSpeedFact=(tmp&0x1C)>>2;
cardinfo->SD_csd.MaxWrBlockLen=(tmp&0x03)<<2; ?
tmp=(u8)((CSD_Tab[3]&0x00FF0000)>>16); //第13個字節(jié)
cardinfo->SD_csd.MaxWrBlockLen|=(tmp&0xC0)>>6;
cardinfo->SD_csd.WriteBlockPaPartial=(tmp&0x20)>>5;
cardinfo->SD_csd.Reserved3=0;
cardinfo->SD_csd.ContentProtectAppli=(tmp&0x01);
tmp=(u8)((CSD_Tab[3]&0x0000FF00)>>8); //第14個字節(jié)
cardinfo->SD_csd.FileFormatGrouop=(tmp&0x80)>>7;
cardinfo->SD_csd.CopyFlag=(tmp&0x40)>>6;
cardinfo->SD_csd.PermWrProtect=(tmp&0x20)>>5;
cardinfo->SD_csd.TempWrProtect=(tmp&0x10)>>4;
cardinfo->SD_csd.FileFormat=(tmp&0x0C)>>2;
cardinfo->SD_csd.ECC=(tmp&0x03);
tmp=(u8)(CSD_Tab[3]&0x000000FF); //第15個字節(jié)
cardinfo->SD_csd.CSD_CRC=(tmp&0xFE)>>1;
cardinfo->SD_csd.Reserved4=1;
tmp=(u8)((CID_Tab[0]&0xFF000000)>>24); //第0個字節(jié)
cardinfo->SD_cid.ManufacturerID=tmp;
tmp=(u8)((CID_Tab[0]&0x00FF0000)>>16); //第1個字節(jié)
cardinfo->SD_cid.OEM_AppliID=tmp<<8; ??
tmp=(u8)((CID_Tab[0]&0x000000FF00)>>8); //第2個字節(jié)
cardinfo->SD_cid.OEM_AppliID|=tmp;
tmp=(u8)(CID_Tab[0]&0x000000FF); //第3個字節(jié)
cardinfo->SD_cid.ProdName1=tmp<<24; ??
tmp=(u8)((CID_Tab[1]&0xFF000000)>>24); //第4個字節(jié)
cardinfo->SD_cid.ProdName1|=tmp<<16; ??
tmp=(u8)((CID_Tab[1]&0x00FF0000)>>16); //第5個字節(jié)
cardinfo->SD_cid.ProdName1|=tmp<<8; ?
tmp=(u8)((CID_Tab[1]&0x0000FF00)>>8); //第6個字節(jié)
cardinfo->SD_cid.ProdName1|=tmp;
tmp=(u8)(CID_Tab[1]&0x000000FF); //第7個字節(jié)
cardinfo->SD_cid.ProdName2=tmp;
tmp=(u8)((CID_Tab[2]&0xFF000000)>>24); //第8個字節(jié)
cardinfo->SD_cid.ProdRev=tmp;
tmp=(u8)((CID_Tab[2]&0x00FF0000)>>16); //第9個字節(jié)
cardinfo->SD_cid.ProdSN=tmp<<24; ? ?
tmp=(u8)((CID_Tab[2]&0x0000FF00)>>8); //第10個字節(jié)
cardinfo->SD_cid.ProdSN|=tmp<<16; ? ?
tmp=(u8)(CID_Tab[2]&0x000000FF); //第11個字節(jié)
cardinfo->SD_cid.ProdSN|=tmp<<8; ? ?
tmp=(u8)((CID_Tab[3]&0xFF000000)>>24); //第12個字節(jié)
cardinfo->SD_cid.ProdSN|=tmp;
tmp=(u8)((CID_Tab[3]&0x00FF0000)>>16); //第13個字節(jié)
cardinfo->SD_cid.Reserved1|=(tmp&0xF0)>>4;
cardinfo->SD_cid.ManufactDate=(tmp&0x0F)<<8;? ??
tmp=(u8)((CID_Tab[3]&0x0000FF00)>>8); //第14個字節(jié)
cardinfo->SD_cid.ManufactDate|=tmp;
tmp=(u8)(CID_Tab[3]&0x000000FF); //第15個字節(jié)
cardinfo->SD_cid.CID_CRC=(tmp&0xFE)>>1;
cardinfo->SD_cid.Reserved2=1;
return errorstatus;
}
/*
函數(shù)功能: 設(shè)置SDIO總線寬度
函數(shù)參數(shù):
wmode:位寬模式.0,1位數(shù)據(jù)寬度;1,4位數(shù)據(jù)寬度;2,8位數(shù)據(jù)寬度
返回值:SD卡錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardEnableWideBusOperation(u32 wmode)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
if(wmode>=2)return SD_UNSUPPORTED_FEATURE;//不支持8位模式
else
{
errorstatus=SDIO_SdCardEnWideBus(wmode);
if(SD_OK==errorstatus)
{
SDIO->CLKCR&=~(3<<11); //清除之前的位寬設(shè)置? ??
SDIO->CLKCR|=(u16)wmode<<11;//1位/4位總線寬度?
SDIO->CLKCR|=0<<14; //不開啟硬件流控制
}
}
}
return errorstatus;
}
/*
函數(shù)功能:設(shè)置SD卡工作模式
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardSetDeviceMode(u32 Mode)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
if((Mode==SD_DMA_MODE)||(Mode==SD_POLLING_MODE))DeviceMode=Mode;
else errorstatus=SD_INVALID_PARAMETER;
return errorstatus;
}
/*
函數(shù)功能:選卡,發(fā)送CMD7,選擇相對地址(rca)為addr的卡,取消其他卡.如果為0,則都不選擇.
函數(shù)參數(shù):
addr:卡的RCA地址
*/
SDIO_SD_ERROR_INFO SDIO_SdCardSelectAddr(u32 addr)
{
SDIO_SendCmd(SD_CMD_SEL_DESEL_CARD,1,addr); //發(fā)送CMD7,選擇卡,短響應(yīng)
return SDIO_CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
}
/*
函數(shù)功能: SD卡讀取一個塊
函數(shù)參數(shù):
buf:讀數(shù)據(jù)緩存區(qū)(必須4字節(jié)對齊!!)
addr:讀取地址
blksize:塊大小
*/
SDIO_SD_ERROR_INFO SDIO_SdCardReadBlock(u8 *buf,long long addr,u16 blksize)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u8 power;
u32 count=0,*tempbuff=(u32*)buf;//轉(zhuǎn)換為u32指針
u32 timeout=SDIO_DATATIMEOUT;
if(NULL==buf)return SD_INVALID_PARAMETER;
SDIO->DCTRL=0x0; //數(shù)據(jù)控制寄存器清零(關(guān)DMA)
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
{
blksize=512;
addr>>=9;
}
SDIO_SendDataConfig(SD_DATATIMEOUT,0,0,0); //清除DPSM狀態(tài)機配置
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡鎖了
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_SendCmd(SD_CMD_SET_BLOCKLEN,1,blksize); //發(fā)送CMD16+設(shè)置數(shù)據(jù)長度為blksize,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}else return SD_INVALID_PARAMETER;
SDIO_SendDataConfig(SD_DATATIMEOUT,blksize,power,1); //blksize,卡到控制器
SDIO_SendCmd(SD_CMD_READ_SINGLE_BLOCK,1,addr); //發(fā)送CMD17+從addr地址出讀取數(shù)據(jù),短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
if(DeviceMode==SD_POLLING_MODE) //查詢模式,輪詢數(shù)據(jù)
{
// INTX_DISABLE();//關(guān)閉總中斷(POLLING模式,嚴(yán)禁中斷打斷SDIO讀寫操作!!!)
while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<10)|(1<<9))))//無上溢/CRC/超時/完成(標(biāo)志)/起始位錯誤
{
if(SDIO->STA&(1<<15)) //接收區(qū)半滿,表示至少存了8個字
{
for(count=0;count<8;count++) //循環(huán)讀取數(shù)據(jù)
{
*(tempbuff+count)=SDIO->FIFO;
}
tempbuff+=8;
timeout=0X7FFFFF; //讀數(shù)據(jù)溢出時間
}else //處理超時
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO->STA&(1<<3)) //數(shù)據(jù)超時錯誤
{
SDIO->ICR|=1<<3; //清錯誤標(biāo)志
return SD_DATA_TIMEOUT;
}else if(SDIO->STA&(1<<1)) //數(shù)據(jù)塊CRC錯誤
{
SDIO->ICR|=1<<1; //清錯誤標(biāo)志
return SD_DATA_CRC_FAIL;
}else if(SDIO->STA&(1<<5)) //接收fifo上溢錯誤
{
SDIO->ICR|=1<<5; //清錯誤標(biāo)志
return SD_RX_OVERRUN;
}else if(SDIO->STA&(1<<9)) //接收起始位錯誤
{
SDIO->ICR|=1<<9; //清錯誤標(biāo)志
return SD_START_BIT_ERR;
}
while(SDIO->STA&(1<<21)) //FIFO里面,還存在可用數(shù)據(jù)
{
*tempbuff=SDIO->FIFO; //循環(huán)讀取數(shù)據(jù)
tempbuff++;
}
// INTX_ENABLE();//開啟總中斷
SDIO->ICR=0X5FF; //清除所有標(biāo)記
}else if(DeviceMode==SD_DMA_MODE)
{
SDIO_SdCard_DMAConfig((u32*)buf,blksize,0);
TransferError=SD_OK;
StopCondition=0; //單塊讀,不需要發(fā)送停止傳輸指令
TransferEnd=0; //傳輸結(jié)束標(biāo)置位,在中斷服務(wù)置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中斷?
SDIO->DCTRL|=1<<3; //SDIO DMA使能?
while(((DMA2->ISR&0X2000)==RESET)&&(TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;//等待傳輸完成
if(timeout==0)return SD_DATA_TIMEOUT;//超時
if(TransferError!=SD_OK)errorstatus=TransferError;
}
return errorstatus;
}
/*
函數(shù)功能: SD卡讀取多個塊
函數(shù)參數(shù):
buf:讀數(shù)據(jù)緩存區(qū)
addr:讀取地址
blksize:塊大小
nblks:要讀取的塊數(shù)
返回值:錯誤狀態(tài)
*/
__align(4) u32 *tempbuff;
SDIO_SD_ERROR_INFO SDIO_SdCardReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u8 power;
u32 count=0;
u32 timeout=SDIO_DATATIMEOUT;
tempbuff=(u32*)buf; //轉(zhuǎn)換為u32指針
SDIO->DCTRL=0x0; //數(shù)據(jù)控制寄存器清零(關(guān)DMA)
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
{
blksize=512;
addr>>=9;
}
SDIO_SendDataConfig(SD_DATATIMEOUT,0,0,0); //清除DPSM狀態(tài)機配置
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡鎖了
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_SendCmd(SD_CMD_SET_BLOCKLEN,1,blksize); //發(fā)送CMD16+設(shè)置數(shù)據(jù)長度為blksize,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}else return SD_INVALID_PARAMETER;
if(nblks>1) //多塊讀
{
if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;//判斷是否超過最大接收長度
SDIO_SendDataConfig(SD_DATATIMEOUT,nblks*blksize,power,1);//nblks*blksize,512塊大小,卡到控制器
SDIO_SendCmd(SD_CMD_READ_MULT_BLOCK,1,addr); //發(fā)送CMD18+從addr地址出讀取數(shù)據(jù),短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_READ_MULT_BLOCK);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
if(DeviceMode==SD_POLLING_MODE)
{
// INTX_DISABLE();//關(guān)閉總中斷(POLLING模式,嚴(yán)禁中斷打斷SDIO讀寫操作!!!)
while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<8)|(1<<9))))//無上溢/CRC/超時/完成(標(biāo)志)/起始位錯誤
{
if(SDIO->STA&(1<<15)) //接收區(qū)半滿,表示至少存了8個字
{
for(count=0;count<8;count++) //循環(huán)讀取數(shù)據(jù)
{
*(tempbuff+count)=SDIO->FIFO;
}
tempbuff+=8;
timeout=0X7FFFFF; //讀數(shù)據(jù)溢出時間
}else //處理超時
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO->STA&(1<<3)) //數(shù)據(jù)超時錯誤
{
SDIO->ICR|=1<<3; //清錯誤標(biāo)志
return SD_DATA_TIMEOUT;
}else if(SDIO->STA&(1<<1)) //數(shù)據(jù)塊CRC錯誤
{
SDIO->ICR|=1<<1; //清錯誤標(biāo)志
return SD_DATA_CRC_FAIL;
}else if(SDIO->STA&(1<<5)) //接收fifo上溢錯誤
{
SDIO->ICR|=1<<5; //清錯誤標(biāo)志
return SD_RX_OVERRUN;
}else if(SDIO->STA&(1<<9)) //接收起始位錯誤
{
SDIO->ICR|=1<<9; //清錯誤標(biāo)志
return SD_START_BIT_ERR;
}
while(SDIO->STA&(1<<21)) //FIFO里面,還存在可用數(shù)據(jù)
{
*tempbuff=SDIO->FIFO; //循環(huán)讀取數(shù)據(jù)
tempbuff++;
}
if(SDIO->STA&(1<<8)) //接收結(jié)束
{
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
SDIO_SendCmd(SD_CMD_STOP_TRANSMISSION,1,0); //發(fā)送CMD12+結(jié)束傳輸
errorstatus=SDIO_CmdResp1Error(SD_CMD_STOP_TRANSMISSION);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
}
}
// INTX_ENABLE();//開啟總中斷
SDIO->ICR=0X5FF; //清除所有標(biāo)記
}
else if(DeviceMode==SD_DMA_MODE)
{
SDIO_SdCard_DMAConfig((u32*)buf,nblks*blksize,0);
TransferError=SD_OK;
StopCondition=1; //多塊讀,需要發(fā)送停止傳輸指令
TransferEnd=0; //傳輸結(jié)束標(biāo)置位,在中斷服務(wù)置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中斷?
SDIO->DCTRL|=1<<3; //SDIO DMA使能?
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待傳輸完成
if(timeout==0)return SD_DATA_TIMEOUT;//超時
while((TransferEnd==0)&&(TransferError==SD_OK));
if(TransferError!=SD_OK)errorstatus=TransferError;
}
}
return errorstatus;
}
/*
函數(shù)功能:SD卡寫1個塊
函數(shù)參數(shù):
buf:數(shù)據(jù)緩存區(qū)
addr:寫地址
blksize:塊大小
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardWriteBlock(u8 *buf,long long addr, u16 blksize)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
u8 power=0,cardstate=0;
u32 timeout=0,bytestransferred=0;
u32 cardstatus=0,count=0,restwords=0;
u32 tlen=blksize; //總長度(字節(jié))
u32*tempbuff=(u32*)buf;
if(buf==NULL)return SD_INVALID_PARAMETER;//參數(shù)錯誤
SDIO->DCTRL=0x0; //數(shù)據(jù)控制寄存器清零(關(guān)DMA)
SDIO_SendDataConfig(SD_DATATIMEOUT,0,0,0); //清除DPSM狀態(tài)機配置
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡鎖了
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //大容量卡
{
blksize=512;
addr>>=9;
}
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_SendCmd(SD_CMD_SET_BLOCKLEN,1,blksize); //發(fā)送CMD16+設(shè)置數(shù)據(jù)長度為blksize,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}else return SD_INVALID_PARAMETER;
SDIO_SendCmd(SD_CMD_SEND_STATUS,1,(u32)RCA<<16); //發(fā)送CMD13,查詢卡的狀態(tài),短響應(yīng) ? ?
errorstatus=SDIO_CmdResp1Error(SD_CMD_SEND_STATUS); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
cardstatus=SDIO->RESP1;
timeout=SD_DATATIMEOUT;
while(((cardstatus&0x00000100)==0)&&(timeout>0)) //檢查READY_FOR_DATA位是否置位
{
timeout--;
SDIO_SendCmd(SD_CMD_SEND_STATUS,1,(u32)RCA<<16);//發(fā)送CMD13,查詢卡的狀態(tài),短響應(yīng) ? ?
errorstatus=SDIO_CmdResp1Error(SD_CMD_SEND_STATUS); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
cardstatus=SDIO->RESP1;
}
if(timeout==0)return SD_ERROR;
SDIO_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK,1,addr); //發(fā)送CMD24,寫單塊指令,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
StopCondition=0; //單塊寫,不需要發(fā)送停止傳輸指令
SDIO_SendDataConfig(SD_DATATIMEOUT,blksize,power,0); //blksize, 控制器到卡
timeout=SDIO_DATATIMEOUT;
if(DeviceMode == SD_POLLING_MODE)
{
// INTX_DISABLE();//關(guān)閉總中斷(POLLING模式,嚴(yán)禁中斷打斷SDIO讀寫操作!!!)
while(!(SDIO->STA&((1<<10)|(1<<4)|(1<<1)|(1<<3)|(1<<9))))//數(shù)據(jù)塊發(fā)送成功/下溢/CRC/超時/起始位錯誤
{
if(SDIO->STA&(1<<14)) //發(fā)送區(qū)半空,表示至少存了8個字
{
if((tlen-bytestransferred)
{
restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
for(count=0;count
{
SDIO->FIFO=*tempbuff;
}
}else
{
for(count=0;count<8;count++)
{
SDIO->FIFO=*(tempbuff+count);
}
tempbuff+=8;
bytestransferred+=32;
}
timeout=0X3FFFFFFF; //寫數(shù)據(jù)溢出時間
}else
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO->STA&(1<<3)) //數(shù)據(jù)超時錯誤
{
SDIO->ICR|=1<<3; //清錯誤標(biāo)志
return SD_DATA_TIMEOUT;
}else if(SDIO->STA&(1<<1)) //數(shù)據(jù)塊CRC錯誤
{
SDIO->ICR|=1<<1; //清錯誤標(biāo)志
return SD_DATA_CRC_FAIL;
}else if(SDIO->STA&(1<<4)) //接收fifo下溢錯誤
{
SDIO->ICR|=1<<4; //清錯誤標(biāo)志
return SD_TX_UNDERRUN;
}else if(SDIO->STA&(1<<9)) //接收起始位錯誤
{
SDIO->ICR|=1<<9; //清錯誤標(biāo)志
return SD_START_BIT_ERR;
}
// INTX_ENABLE();//開啟總中斷
SDIO->ICR=0X5FF; //清除所有標(biāo)記
}else if(DeviceMode==SD_DMA_MODE)
{
SDIO_SdCard_DMAConfig((u32*)buf,blksize,1);//SDIO DMA配置
TransferError=SD_OK;
StopCondition=0; //單塊寫,不需要發(fā)送停止傳輸指令
TransferEnd=0; //傳輸結(jié)束標(biāo)置位,在中斷服務(wù)置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置產(chǎn)生數(shù)據(jù)接收完成中斷
SDIO->DCTRL|=1<<3; //SDIO DMA使能.??
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待傳輸完成
if(timeout==0)
{
SDIO_SdCardInit(); //重新初始化SD卡,可以解決寫入死機的問題
return SD_DATA_TIMEOUT; //超時
}
timeout=SDIO_DATATIMEOUT;
while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
if(timeout==0)return SD_DATA_TIMEOUT; //超時
if(TransferError!=SD_OK)return TransferError;
}
SDIO->ICR=0X5FF; //清除所有標(biāo)記
errorstatus=SDIO_SdCardProgrammingState(&cardstate);
while((errorstatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
{
errorstatus=SDIO_SdCardProgrammingState(&cardstate);
}
return errorstatus;
}
/*
函數(shù)功能:SD卡寫多個塊
函數(shù)參數(shù):
buf:數(shù)據(jù)緩存區(qū)
addr:寫地址
blksize:塊大小
nblks:要寫入的塊數(shù)
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardWriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
u8 power = 0, cardstate = 0;
u32 timeout=0,bytestransferred=0;
u32 count = 0, restwords = 0;
u32 tlen=nblks*blksize; //總長度(字節(jié))
u32 *tempbuff = (u32*)buf;
if(buf==NULL)return SD_INVALID_PARAMETER; //參數(shù)錯誤
SDIO->DCTRL=0x0; //數(shù)據(jù)控制寄存器清零(關(guān)DMA)
SDIO_SendDataConfig(SD_DATATIMEOUT,0,0,0); //清除DPSM狀態(tài)機配置
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡鎖了
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
{
blksize=512;
addr>>=9;
}
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_SendCmd(SD_CMD_SET_BLOCKLEN,1,blksize); //發(fā)送CMD16+設(shè)置數(shù)據(jù)長度為blksize,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus; //響應(yīng)錯誤
}else return SD_INVALID_PARAMETER;
if(nblks>1)
{
if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
//提高性能
SDIO_SendCmd(SD_CMD_APP_CMD,1,(u32)RCA<<16); //發(fā)送ACMD55,短響應(yīng) ? ?
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_CMD); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
SDIO_SendCmd(SD_CMD_SET_BLOCK_COUNT,1,nblks); //發(fā)送CMD23,設(shè)置塊數(shù)量,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
}
SDIO_SendCmd(SD_CMD_WRITE_MULT_BLOCK,1,addr); //發(fā)送CMD25,多塊寫指令,短響應(yīng)
errorstatus=SDIO_CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK); //等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
SDIO_SendDataConfig(SD_DATATIMEOUT,nblks*blksize,power,0);//blksize, 控制器到卡
if(DeviceMode==SD_POLLING_MODE)
{
timeout=SDIO_DATATIMEOUT;
while(!(SDIO->STA&((1<<4)|(1<<1)|(1<<8)|(1<<3)|(1<<9))))//下溢/CRC/數(shù)據(jù)結(jié)束/超時/起始位錯誤
{
if(SDIO->STA&(1<<14)) //發(fā)送區(qū)半空,表示至少存了8字(32字節(jié))
{
if((tlen-bytestransferred)
{
restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
for(count=0;count
{
SDIO->FIFO=*tempbuff;
}
}else //發(fā)送區(qū)半空,可以發(fā)送至少8字(32字節(jié))數(shù)據(jù)
{
for(count=0;count
{
SDIO->FIFO=*(tempbuff+count);
}
tempbuff+=SD_HALFFIFO;
bytestransferred+=SD_HALFFIFOBYTES;
}
timeout=0X3FFFFFFF; //寫數(shù)據(jù)溢出時間
}else
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO->STA&(1<<3)) //數(shù)據(jù)超時錯誤
{
SDIO->ICR|=1<<3; //清錯誤標(biāo)志
return SD_DATA_TIMEOUT;
}else if(SDIO->STA&(1<<1)) //數(shù)據(jù)塊CRC錯誤
{
SDIO->ICR|=1<<1; //清錯誤標(biāo)志
return SD_DATA_CRC_FAIL;
}else if(SDIO->STA&(1<<4)) //接收fifo下溢錯誤
{
SDIO->ICR|=1<<4; //清錯誤標(biāo)志
return SD_TX_UNDERRUN;
}else if(SDIO->STA&(1<<9)) //接收起始位錯誤
{
SDIO->ICR|=1<<9; //清錯誤標(biāo)志
return SD_START_BIT_ERR;
}
if(SDIO->STA&(1<<8)) //發(fā)送結(jié)束
{
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
SDIO_SendCmd(SD_CMD_STOP_TRANSMISSION,1,0); //發(fā)送CMD12+結(jié)束傳輸
errorstatus=SDIO_CmdResp1Error(SD_CMD_STOP_TRANSMISSION);//等待R1響應(yīng)
if(errorstatus!=SD_OK)return errorstatus;
}
}
// INTX_ENABLE();//開啟總中斷
SDIO->ICR=0X5FF; //清除所有標(biāo)記
}
else if(DeviceMode==SD_DMA_MODE)
{
SDIO_SdCard_DMAConfig((u32*)buf,nblks*blksize,1);//SDIO DMA配置
TransferError=SD_OK;
StopCondition=1; //多塊寫,需要發(fā)送停止傳輸指令
TransferEnd=0; //傳輸結(jié)束標(biāo)置位,在中斷服務(wù)置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置產(chǎn)生數(shù)據(jù)接收完成中斷
SDIO->DCTRL|=1<<3; //SDIO DMA使能.?
timeout=SDIO_DATATIMEOUT;
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待傳輸完成
if(timeout==0) //超時
{
SDIO_SdCardInit(); //重新初始化SD卡,可以解決寫入死機的問題
return SD_DATA_TIMEOUT; //超時
}
timeout=SDIO_DATATIMEOUT;
while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
if(timeout==0)return SD_DATA_TIMEOUT; //超時
if(TransferError!=SD_OK)return TransferError;
}
}
SDIO->ICR=0X5FF; //清除所有標(biāo)記
errorstatus=SDIO_SdCardProgrammingState(&cardstate);
while((errorstatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
{
errorstatus=SDIO_SdCardProgrammingState(&cardstate);
}
return errorstatus;
}
/*
函數(shù)功能: SDIO中斷服務(wù)函數(shù)
*/
void SDIO_IRQHandler(void)
{
SDIO_SdCardProcessIRQSrc();//處理所有SDIO相關(guān)中斷
}
/*
函數(shù)功能: SDIO中斷處理函數(shù)
函數(shù)參數(shù): 處理SDIO傳輸過程中的各種中斷事務(wù)
返回值:錯誤代碼
*/
SDIO_SD_ERROR_INFO SDIO_SdCardProcessIRQSrc(void)
{
if(SDIO->STA&(1<<8))//接收完成中斷
{
if(StopCondition==1)
{
SDIO_SendCmd(SD_CMD_STOP_TRANSMISSION,1,0); //發(fā)送CMD12,結(jié)束傳輸
TransferError=SDIO_CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
}else TransferError = SD_OK;
SDIO->ICR|=1<<8;//清除完成中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferEnd = 1;
return(TransferError);
}
if(SDIO->STA&(1<<1))//數(shù)據(jù)CRC錯誤
{
SDIO->ICR|=1<<1;//清除中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferError = SD_DATA_CRC_FAIL;
return(SD_DATA_CRC_FAIL);
}
if(SDIO->STA&(1<<3))//數(shù)據(jù)超時錯誤
{
SDIO->ICR|=1<<3;//清除中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferError = SD_DATA_TIMEOUT;
return(SD_DATA_TIMEOUT);
}
if(SDIO->STA&(1<<5))//FIFO上溢錯誤
{
SDIO->ICR|=1<<5;//清除中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferError = SD_RX_OVERRUN;
return(SD_RX_OVERRUN);
}
if(SDIO->STA&(1<<4))//FIFO下溢錯誤
{
SDIO->ICR|=1<<4;//清除中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferError = SD_TX_UNDERRUN;
return(SD_TX_UNDERRUN);
}
if(SDIO->STA&(1<<9))//起始位錯誤
{
SDIO->ICR|=1<<9;//清除中斷標(biāo)記
SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//關(guān)閉相關(guān)中斷
TransferError = SD_START_BIT_ERR;
return(SD_START_BIT_ERR);
}
return(SD_OK);
}
/*
函數(shù)功能: 檢查CMD0的執(zhí)行狀態(tài)
返回值: sd卡錯誤碼
*/
SDIO_SD_ERROR_INFO SDIO_CmdErrorCheck(void)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
u32 timeout=SDIO_CMD0TIMEOUT;
while(timeout--)
{
if(SDIO->STA&(1<<7))break; //命令已發(fā)送(無需響應(yīng)) ?
}
if(timeout==0)return SD_CMD_RSP_TIMEOUT;
SDIO->ICR=0X5FF; //清除標(biāo)記
return errorstatus;
}
/*
函數(shù)功能: 檢查R7響應(yīng)的錯誤狀態(tài)
函數(shù)參數(shù): 返回值:sd卡錯誤碼
*/
SDIO_SD_ERROR_INFO SDIO_CmdResp7Error(void)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u32 status;
u32 timeout=SDIO_CMD0TIMEOUT;
while(timeout--)
{
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC錯誤/命令響應(yīng)超時/已經(jīng)收到響應(yīng)(CRC校驗成功)
}
if((timeout==0)||(status&(1<<2))) //響應(yīng)超時
{
errorstatus=SD_CMD_RSP_TIMEOUT; //當(dāng)前卡不是2.0兼容卡,或者不支持設(shè)定的電壓范圍
SDIO->ICR|=1<<2; //清除命令響應(yīng)超時標(biāo)志
return errorstatus;
}
if(status&1<<6) //成功接收到響應(yīng)
{
errorstatus=SD_OK;
SDIO->ICR|=1<<6; //清除響應(yīng)標(biāo)志
}
return errorstatus;
}
/*
函數(shù)功能:檢查R1響應(yīng)的錯誤狀態(tài)
函數(shù)參數(shù):
cmd:當(dāng)前命令
返回值:sd卡錯誤碼
*/
SDIO_SD_ERROR_INFO SDIO_CmdResp1Error(u8 cmd)
{
u32 status;
while(1)
{
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC錯誤/命令響應(yīng)超時/已經(jīng)收到響應(yīng)(CRC校驗成功)
}
if(status&(1<<2)) //響應(yīng)超時
{
SDIO->ICR=1<<2; //清除命令響應(yīng)超時標(biāo)志
return SD_CMD_RSP_TIMEOUT;
}
if(status&(1<<0)) //CRC錯誤
{
SDIO->ICR=1<<0; //清除標(biāo)志
return SD_CMD_CRC_FAIL;
}
if(SDIO->RESPCMD!=cmd)return SD_ILLEGAL_CMD;//命令不匹配
SDIO->ICR=0X5FF; //清除標(biāo)記
return (SDIO_SD_ERROR_INFO)(SDIO->RESP1&SD_OCR_ERRORBITS);//返回卡響應(yīng)
}
/*
函數(shù)功能: 檢查R3響應(yīng)的錯誤狀態(tài)
返回值: 錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_CmdResp3Error(void)
{
u32 status;
while(1)
{
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC錯誤/命令響應(yīng)超時/已經(jīng)收到響應(yīng)(CRC校驗成功)
}
if(status&(1<<2)) //響應(yīng)超時
{
SDIO->ICR|=1<<2; //清除命令響應(yīng)超時標(biāo)志
return SD_CMD_RSP_TIMEOUT;
}
SDIO->ICR=0X5FF; //清除標(biāo)記
return SD_OK;
}
/*
函數(shù)功能: 檢查R2響應(yīng)的錯誤狀態(tài)
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_CmdResp2Error(void)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u32 status;
u32 timeout=SDIO_CMD0TIMEOUT;
while(timeout--)
{
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC錯誤/命令響應(yīng)超時/已經(jīng)收到響應(yīng)(CRC校驗成功)
}
if((timeout==0)||(status&(1<<2))) //響應(yīng)超時
{
errorstatus=SD_CMD_RSP_TIMEOUT;
SDIO->ICR|=1<<2; //清除命令響應(yīng)超時標(biāo)志
return errorstatus;
}
if(status&1<<0) //CRC錯誤
{
errorstatus=SD_CMD_CRC_FAIL;
SDIO->ICR|=1<<0; //清除響應(yīng)標(biāo)志
}
SDIO->ICR=0X5FF; //清除標(biāo)記
return errorstatus;
}
/*
函數(shù)功能: 檢查R6響應(yīng)的錯誤狀態(tài)
函數(shù)參數(shù):
cmd:之前發(fā)送的命令
prca:卡返回的RCA地址
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_CmdResp6Error(u8 cmd,u16*prca)
{
SDIO_SD_ERROR_INFO errorstatus=SD_OK;
u32 status;
u32 rspr1;
while(1)
{
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC錯誤/命令響應(yīng)超時/已經(jīng)收到響應(yīng)(CRC校驗成功)
}
if(status&(1<<2)) //響應(yīng)超時
{
SDIO->ICR|=1<<2; //清除命令響應(yīng)超時標(biāo)志
return SD_CMD_RSP_TIMEOUT;
}
if(status&1<<0) //CRC錯誤
{
SDIO->ICR|=1<<0; //清除響應(yīng)標(biāo)志
return SD_CMD_CRC_FAIL;
}
if(SDIO->RESPCMD!=cmd) //判斷是否響應(yīng)cmd命令
{
return SD_ILLEGAL_CMD;
}
SDIO->ICR=0X5FF; //清除所有標(biāo)記
rspr1=SDIO->RESP1; //得到響應(yīng)
if(SD_ALLZERO==(rspr1&(SD_R6_GENERAL_UNKNOWN_ERROR|SD_R6_ILLEGAL_CMD|SD_R6_COM_CRC_FAILED)))
{
*prca=(u16)(rspr1>>16); //右移16位得到,rca
return errorstatus;
}
if(rspr1&SD_R6_GENERAL_UNKNOWN_ERROR)return SD_GENERAL_UNKNOWN_ERROR;
if(rspr1&SD_R6_ILLEGAL_CMD)return SD_ILLEGAL_CMD;
if(rspr1&SD_R6_COM_CRC_FAILED)return SD_COM_CRC_FAILED;
return errorstatus;
}
/*
函數(shù)功能:SDIO使能寬總線模式
函數(shù)參數(shù):
enx:0,不使能;1,使能;
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardEnWideBus(u8 enx)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
u32 scr[2]={0,0};
u8 arg=0X00;
if(enx)arg=0X02;
else arg=0X00;
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//SD卡處于LOCKED狀態(tài)
errorstatus=SDIO_SdCardFindSCR(RCA,scr); //得到SCR寄存器數(shù)據(jù)
if(errorstatus!=SD_OK)return errorstatus;
if((scr[1]&SD_WIDE_BUS_SUPPORT)!=SD_ALLZERO) //支持寬總線
{
SDIO_SendCmd(SD_CMD_APP_CMD,1,(u32)RCA<<16); //發(fā)送CMD55+RCA,短響應(yīng) ??
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_CMD);
if(errorstatus!=SD_OK)return errorstatus;
SDIO_SendCmd(SD_CMD_APP_SD_SET_BUSWIDTH,1,arg);//發(fā)送ACMD6,短響應(yīng),參數(shù):10,4位;00,1位.
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
return errorstatus;
}else return SD_REQUEST_NOT_APPLICABLE; //不支持寬總線設(shè)置
}
/*
函數(shù)功能: 檢查卡是否正在執(zhí)行寫操作
函數(shù)參數(shù): pstatus:當(dāng)前狀態(tài)
返回值:錯誤代碼
*/
SDIO_SD_ERROR_INFO SDIO_SdCardProgrammingState(u8 *pstatus)
{
vu32 respR1 = 0, status = 0;
SDIO_SendCmd(SD_CMD_SEND_STATUS,1,(u32)RCA<<16); //發(fā)送CMD13 ? ?
status=SDIO->STA;
while(!(status&((1<<0)|(1<<6)|(1<<2))))status=SDIO->STA;//等待操作完成
if(status&(1<<0)) //CRC檢測失敗
{
SDIO->ICR|=1<<0; //清除錯誤標(biāo)記
return SD_CMD_CRC_FAIL;
}
if(status&(1<<2)) //命令超時?
{
SDIO->ICR|=1<<2; //清除錯誤標(biāo)記
return SD_CMD_RSP_TIMEOUT;
}
if(SDIO->RESPCMD!=SD_CMD_SEND_STATUS)return SD_ILLEGAL_CMD;
SDIO->ICR=0X5FF; //清除所有標(biāo)記
respR1=SDIO->RESP1;
*pstatus=(u8)((respR1>>9)&0x0000000F);
return SD_OK;
}
/*
函數(shù)功能: 讀取當(dāng)前卡狀態(tài)
函數(shù)參數(shù):
pcardstatus:卡狀態(tài)
返回值 :錯誤代碼
*/
SDIO_SD_ERROR_INFO SDIO_SdCardSendStatus(uint32_t *pcardstatus)
{
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
if(pcardstatus==NULL)
{
errorstatus=SD_INVALID_PARAMETER;
return errorstatus;
}
SDIO_SendCmd(SD_CMD_SEND_STATUS,1,RCA<<16); //發(fā)送CMD13,短響應(yīng) ?
errorstatus=SDIO_CmdResp1Error(SD_CMD_SEND_STATUS); //查詢響應(yīng)狀態(tài)
if(errorstatus!=SD_OK)return errorstatus;
*pcardstatus=SDIO->RESP1;//讀取響應(yīng)值
return errorstatus;
}
/*
函數(shù)功能: 返回SD卡的狀態(tài)
返回值 : SD卡狀態(tài)
*/
SDCardState SDIO_SdCardGetState(void)
{
u32 resp1=0;
if(SDIO_SdCardSendStatus(&resp1)!=SD_OK)return SD_CARD_ERROR;
else return (SDCardState)((resp1>>9) & 0x0F);
}
/*
函數(shù)功能:查找SD卡的SCR寄存器值
函數(shù)參數(shù):
rca:卡相對地址
pscr:數(shù)據(jù)緩存區(qū)(存儲SCR內(nèi)容)
返回值:錯誤狀態(tài)
*/
SDIO_SD_ERROR_INFO SDIO_SdCardFindSCR(u16 rca,u32 *pscr)
{
u32 index = 0;
SDIO_SD_ERROR_INFO errorstatus = SD_OK;
u32 tempscr[2]={0,0};
SDIO_SendCmd(SD_CMD_SET_BLOCKLEN,1,8); //發(fā)送CMD16,短響應(yīng),設(shè)置Block Size為8字節(jié)
errorstatus=SDIO_CmdResp1Error(SD_CMD_SET_BLOCKLEN);
if(errorstatus!=SD_OK)return errorstatus;
SDIO_SendCmd(SD_CMD_APP_CMD,1,(u32)rca<<16); //發(fā)送CMD55,短響應(yīng) ??
errorstatus=SDIO_CmdResp1Error(SD_CMD_APP_CMD);
if(errorstatus!=SD_OK)return errorstatus;
SDIO_SendDataConfig(SD_DATATIMEOUT,8,3,1); //8個字節(jié)長度,block為8字節(jié),SD卡到SDIO.
SDIO_SendCmd(SD_CMD_SD_APP_SEND_SCR,1,0); //發(fā)送ACMD51,短響應(yīng),參數(shù)為0
errorstatus=SDIO_CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
if(errorstatus!=SD_OK)return errorstatus;
while(!(SDIO->STA&(SDIO_FLAG_RXOVERR|SDIO_FLAG_DCRCFAIL|SDIO_FLAG_DTIMEOUT|SDIO_FLAG_DBCKEND|SDIO_FLAG_STBITERR)))
{
if(SDIO->STA&(1<<21))//接收FIFO數(shù)據(jù)可用
{
*(tempscr+index)=SDIO->FIFO; //讀取FIFO內(nèi)容
index++;
if(index>=2)break;
}
}
if(SDIO->STA&(1<<3)) //接收數(shù)據(jù)超時
{
SDIO->ICR|=1<<3; //清除標(biāo)記
return SD_DATA_TIMEOUT;
}
else if(SDIO->STA&(1<<1)) //已發(fā)送/接收的數(shù)據(jù)塊CRC校驗錯誤
{
SDIO->ICR|=1<<1; //清除標(biāo)記
return SD_DATA_CRC_FAIL;
}
else if(SDIO->STA&(1<<5)) //接收FIFO溢出
{
SDIO->ICR|=1<<5; //清除標(biāo)記
return SD_RX_OVERRUN;
}
else if(SDIO->STA&(1<<9)) //起始位檢測錯誤
{
SDIO->ICR|=1<<9; //清除標(biāo)記
return SD_START_BIT_ERR;
}
SDIO->ICR=0X5FF; //清除標(biāo)記
//把數(shù)據(jù)順序按8位為單位倒過來.
*(pscr+1)=((tempscr[0]&SD_0TO7BITS)<<24)|((tempscr[0]&SD_8TO15BITS)<<8)|((tempscr[0]&SD_16TO23BITS)>>8)|((tempscr[0]&SD_24TO31BITS)>>24);
*(pscr)=((tempscr[1]&SD_0TO7BITS)<<24)|((tempscr[1]&SD_8TO15BITS)<<8)|((tempscr[1]&SD_16TO23BITS)>>8)|((tempscr[1]&SD_24TO31BITS)>>24);
return errorstatus;
}
/*
函數(shù)功能: 得到NumberOfBytes以2為底的指數(shù)
函數(shù)參數(shù): NumberOfBytes:字節(jié)數(shù)
返回值:以2為底的指數(shù)值
*/
u8 convert_from_bytes_to_power_of_two(u16 NumberOfBytes)
{
u8 count=0;
while(NumberOfBytes!=1)
{
NumberOfBytes>>=1;
count++;
}
return count;
}
/*
函數(shù)功能: 配置SDIO DMA
函數(shù)參數(shù):
mbuf:存儲器地址
bufsize:傳輸數(shù)據(jù)量
dir:方向;1,存儲器-->SDIO(寫數(shù)據(jù));0,SDIO-->存儲器(讀數(shù)據(jù));
*/
void SDIO_SdCard_DMAConfig(u32*mbuf,u32 bufsize,u8 dir)
{
DMA2->IFCR|=(0XF<<12); //清除DMA2通道4的各種標(biāo)記
DMA2_Channel4->CCR&=~(1<<0); //關(guān)閉DMA 通道4
DMA2_Channel4->CCR&=~(0X7FF<<4); //清除之前的設(shè)置,DIR,CIRC,PINC,MINC,PSIZE,MSIZE,PL,MEM2MEM
DMA2_Channel4->CCR|=dir<<4;? //從存儲器讀? ?
DMA2_Channel4->CCR|=0<<5;? //普通模式
DMA2_Channel4->CCR|=0<<6; //外設(shè)地址非增量模式
DMA2_Channel4->CCR|=1<<7;? //存儲器增量模式
DMA2_Channel4->CCR|=2<<8;? //外設(shè)數(shù)據(jù)寬度為32位
DMA2_Channel4->CCR|=2<<10; //存儲器數(shù)據(jù)寬度32位
DMA2_Channel4->CCR|=2<<12; //高優(yōu)先級 ??
DMA2_Channel4->CNDTR=bufsize/4; //DMA2,傳輸數(shù)據(jù)量
DMA2_Channel4->CPAR=(u32)&SDIO->FIFO;//DMA2 外設(shè)地址
DMA2_Channel4->CMAR=(u32)mbuf; //DMA2,存儲器地址
DMA2_Channel4->CCR|=1<<0; //開啟DMA通道
}
/*
函數(shù)功能: 讀SD卡
函數(shù)參數(shù):
buf:讀數(shù)據(jù)緩存區(qū)
sector:扇區(qū)地址
cnt:扇區(qū)個數(shù)
返回值:錯誤狀態(tài);0,正常;其他,錯誤代碼;
*/
u8 SDIO_SdCardReadDiskSector(u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
long long lsector=sector;
u8 n;
lsector<<=9;
if((u32)buf%4!=0)
{
for(n=0;n
{
sta=SDIO_SdCardReadBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//單個sector的讀操作
memcpy(buf,SDIO_DATA_BUFFER,512);
buf+=512;
}
}else
{
if(cnt==1)sta=SDIO_SdCardReadBlock(buf,lsector,512); //單個sector的讀操作
else sta=SDIO_SdCardReadMultiBlocks(buf,lsector,512,cnt);//多個sector
}
return sta;
}
/*
函數(shù)功能:寫SD卡
函數(shù)參數(shù):
buf:寫數(shù)據(jù)緩存區(qū)
sector:扇區(qū)地址
cnt:扇區(qū)個數(shù)
返回值:錯誤狀態(tài);0,正常;其他,錯誤代碼;
*/
u8 SDIO_SdCardWriteDiskSector(u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
u8 n;
long long lsector=sector;
lsector<<=9;
if((u32)buf%4!=0)
{
for(n=0;n
{
memcpy(SDIO_DATA_BUFFER,buf,512);
sta=SDIO_SdCardWriteBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//單個sector的寫操作
buf+=512;
}
}else
{
if(cnt==1)sta=SDIO_SdCardWriteBlock(buf,lsector,512); //單個sector的寫操作
else sta=SDIO_SdCardWriteMultiBlocks(buf,lsector,512,cnt); //多個sector
}
return sta;
}
(3)sdio.h
#ifndef __SDIO_SDCARD_H
#define __SDIO_SDCARD_H
#include "stm32f10x.h"
//SDIO相關(guān)標(biāo)志位
#define SDIO_FLAG_CCRCFAIL ((uint32_t)0x00000001)
#define SDIO_FLAG_DCRCFAIL ((uint32_t)0x00000002)
#define SDIO_FLAG_CTIMEOUT ((uint32_t)0x00000004)
#define SDIO_FLAG_DTIMEOUT ((uint32_t)0x00000008)
#define SDIO_FLAG_TXUNDERR ((uint32_t)0x00000010)
#define SDIO_FLAG_RXOVERR ((uint32_t)0x00000020)
#define SDIO_FLAG_CMDREND ((uint32_t)0x00000040)
#define SDIO_FLAG_CMDSENT ((uint32_t)0x00000080)
#define SDIO_FLAG_DATAEND ((uint32_t)0x00000100)
#define SDIO_FLAG_STBITERR ((uint32_t)0x00000200)
#define SDIO_FLAG_DBCKEND ((uint32_t)0x00000400)
#define SDIO_FLAG_CMDACT ((uint32_t)0x00000800)
#define SDIO_FLAG_TXACT ((uint32_t)0x00001000)
#define SDIO_FLAG_RXACT ((uint32_t)0x00002000)
#define SDIO_FLAG_TXFIFOHE ((uint32_t)0x00004000)
#define SDIO_FLAG_RXFIFOHF ((uint32_t)0x00008000)
#define SDIO_FLAG_TXFIFOF ((uint32_t)0x00010000)
#define SDIO_FLAG_RXFIFOF ((uint32_t)0x00020000)
#define SDIO_FLAG_TXFIFOE ((uint32_t)0x00040000)
#define SDIO_FLAG_RXFIFOE ((uint32_t)0x00080000)
#define SDIO_FLAG_TXDAVL ((uint32_t)0x00100000)
#define SDIO_FLAG_RXDAVL ((uint32_t)0x00200000)
#define SDIO_FLAG_SDIOIT ((uint32_t)0x00400000)
#define SDIO_FLAG_CEATAEND ((uint32_t)0x00800000)
//用戶配置區(qū)
//SDIO時鐘計算公式:SDIO_CK時鐘=SDIOCLK/[clkdiv+2];其中,SDIOCLK一般為72Mhz
//使用DMA模式的時候,傳輸速率可以到24Mhz,不過如果你的卡不是高速卡,可能也會出錯
//出錯就請降低時鐘,使用查詢模式的話,推薦SDIO_TRANSFER_CLK_DIV設(shè)置為3或者更大
#define SDIO_INIT_CLK_DIV 0xB2 //SDIO初始化頻率,最大400Kh
#define SDIO_TRANSFER_CLK_DIV 0x04 //SDIO傳輸頻率,該值太小可能會導(dǎo)致讀寫文件出錯
//SDIO工作模式定義,通過SDIO_SdCardSetDeviceMode函數(shù)設(shè)置.
#define SD_POLLING_MODE 0 //查詢模式,該模式下,如果讀寫有問題,建議增大SDIO_TRANSFER_CLK_DIV的設(shè)置.
#define SD_DMA_MODE 1 //DMA模式,該模式下,如果讀寫有問題,建議增大SDIO_TRANSFER_CLK_DIV的設(shè)置.
//SDIO 各種錯誤枚舉定義
typedef enum
{
//特殊錯誤定義
SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
SD_DATA_TIMEOUT = (4), /*!< Data time out */
SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
SD_CC_ERROR = (18), /*!< Internal card controller error */
SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
SD_INVALID_VOLTRANGE = (27),
SD_ADDR_OUT_OF_RANGE = (28),
SD_SWITCH_ERROR = (29),
SD_SDIO_DISABLED = (30),
SD_SDIO_FUNCTION_BUSY = (31),
SD_SDIO_FUNCTION_FAILED = (32),
SD_SDIO_UNKNOWN_FUNCTION = (33),
//標(biāo)準(zhǔn)錯誤定義
SD_INTERNAL_ERROR,
SD_NOT_CONFIGURED,
SD_REQUEST_PENDING,
SD_REQUEST_NOT_APPLICABLE,
SD_INVALID_PARAMETER,
SD_UNSUPPORTED_FEATURE,
SD_UNSUPPORTED_HW,
SD_ERROR,
SD_OK = 0
} SDIO_SD_ERROR_INFO;
//SD卡CSD寄存器數(shù)據(jù)
typedef struct
{
u8 CSDStruct; /*!< CSD structure */
u8 SysSpecVersion; /*!< System specification version */
u8 Reserved1; /*!< Reserved */
u8 TAAC; /*!< Data read access-time 1 */
u8 NSAC; /*!< Data read access-time 2 in CLK cycles */
u8 MaxBusClkFrec; /*!< Max. bus clock frequency */
u16 CardComdClasses; /*!< Card command classes */
u8 RdBlockLen; /*!< Max. read data block length */
u8 PartBlockRead; /*!< Partial blocks for read allowed */
u8 WrBlockMisalign; /*!< Write block misalignment */
u8 RdBlockMisalign; /*!< Read block misalignment */
u8 DSRImpl; /*!< DSR implemented */
u8 Reserved2; /*!< Reserved */
u32 DeviceSize; /*!< Device Size */
u8 MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
u8 MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
u8 MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
u8 MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
u8 DeviceSizeMul; /*!< Device size multiplier */
u8 EraseGrSize; /*!< Erase group size */
u8 EraseGrMul; /*!< Erase group size multiplier */
u8 WrProtectGrSize; /*!< Write protect group size */
u8 WrProtectGrEnable; /*!< Write protect group enable */
u8 ManDeflECC; /*!< Manufacturer default ECC */
u8 WrSpeedFact; /*!< Write speed factor */
u8 MaxWrBlockLen; /*!< Max. write data block length */
u8 WriteBlockPaPartial; /*!< Partial blocks for write allowed */
u8 Reserved3; /*!< Reserded */
u8 ContentProtectAppli; /*!< Content protection application */
u8 FileFormatGrouop; /*!< File format group */
u8 CopyFlag; /*!< Copy flag (OTP) */
u8 PermWrProtect; /*!< Permanent write protection */
u8 TempWrProtect; /*!< Temporary write protection */
u8 FileFormat; /*!< File Format */
u8 ECC; /*!< ECC code */
u8 CSD_CRC; /*!< CSD CRC */
u8 Reserved4; /*!< always 1*/
} SD_CSD;
//SD卡CID寄存器數(shù)據(jù)
typedef struct
{
u8 ManufacturerID; /*!< ManufacturerID */
u16 OEM_AppliID; /*!< OEM/Application ID */
u32 ProdName1; /*!< Product Name part1 */
u8 ProdName2; /*!< Product Name part2*/
u8 ProdRev; /*!< Product Revision */
u32 ProdSN; /*!< Product Serial Number */
u8 Reserved1; /*!< Reserved1 */
u16 ManufactDate; /*!< Manufacturing Date */
u8 CID_CRC; /*!< CID CRC */
u8 Reserved2; /*!< always 1 */
} SD_CID;
//SD卡狀態(tài)
typedef enum
{
SD_CARD_READY = ((uint32_t)0x00000001),
SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
SD_CARD_STANDBY = ((uint32_t)0x00000003),
SD_CARD_TRANSFER = ((uint32_t)0x00000004),
SD_CARD_SENDING = ((uint32_t)0x00000005),
SD_CARD_RECEIVING = ((uint32_t)0x00000006),
SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
SD_CARD_ERROR = ((uint32_t)0x000000FF)
}SDCardState;
//SD卡信息,包括CSD,CID等數(shù)據(jù)
typedef struct
{
SD_CSD SD_csd;
SD_CID SD_cid;
long long CardCapacity; //SD卡容量,單位:字節(jié),最大支持2^64字節(jié)大小的卡.
u32 CardBlockSize; //SD卡塊大小
u16 RCA; //卡相對地址
u8 CardType; //卡類型
} SD_CardInfo;
extern SD_CardInfo SDCardInfo;//SD卡信息
//SDIO 指令集
#define SD_CMD_GO_IDLE_STATE ((u8)0)
#define SD_CMD_SEND_OP_COND ((u8)1)
#define SD_CMD_ALL_SEND_CID ((u8)2)
#define SD_CMD_SET_REL_ADDR ((u8)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
#define SD_CMD_SET_DSR ((u8)4)
#define SD_CMD_SDIO_SEN_OP_COND ((u8)5)
#define SD_CMD_HS_SWITCH ((u8)6)
#define SD_CMD_SEL_DESEL_CARD ((u8)7)
#define SD_CMD_HS_SEND_EXT_CSD ((u8)8)
#define SD_CMD_SEND_CSD ((u8)9)
#define SD_CMD_SEND_CID ((u8)10)
#define SD_CMD_READ_DAT_UNTIL_STOP ((u8)11) /*!< SD Card doesn't support it */
#define SD_CMD_STOP_TRANSMISSION ((u8)12)
#define SD_CMD_SEND_STATUS ((u8)13)
#define SD_CMD_HS_BUSTEST_READ ((u8)14)
#define SD_CMD_GO_INACTIVE_STATE ((u8)15)
#define SD_CMD_SET_BLOCKLEN ((u8)16)
#define SD_CMD_READ_SINGLE_BLOCK ((u8)17)
#define SD_CMD_READ_MULT_BLOCK ((u8)18)
#define SD_CMD_HS_BUSTEST_WRITE ((u8)19)
#define SD_CMD_WRITE_DAT_UNTIL_STOP ((u8)20)
#define SD_CMD_SET_BLOCK_COUNT ((u8)23)
#define SD_CMD_WRITE_SINGLE_BLOCK ((u8)24)
#define SD_CMD_WRITE_MULT_BLOCK ((u8)25)
#define SD_CMD_PROG_CID ((u8)26)
#define SD_CMD_PROG_CSD ((u8)27)
#define SD_CMD_SET_WRITE_PROT ((u8)28)
#define SD_CMD_CLR_WRITE_PROT ((u8)29)
#define SD_CMD_SEND_WRITE_PROT ((u8)30)
#define SD_CMD_SD_ERASE_GRP_START ((u8)32) /*!< To set the address of the first write
block to be erased. (For SD card only) */
#define SD_CMD_SD_ERASE_GRP_END ((u8)33) /*!< To set the address of the last write block of the
continuous range to be erased. (For SD card only) */
#define SD_CMD_ERASE_GRP_START ((u8)35) /*!< To set the address of the first write block to be erased.
(For MMC card only spec 3.31) */
#define SD_CMD_ERASE_GRP_END ((u8)36) /*!< To set the address of the last write block of the
continuous range to be erased. (For MMC card only spec 3.31) */
#define SD_CMD_ERASE ((u8)38)
#define SD_CMD_FAST_IO ((u8)39) /*!< SD Card doesn't support it */
#define SD_CMD_GO_IRQ_STATE ((u8)40) /*!< SD Card doesn't support it */
#define SD_CMD_LOCK_UNLOCK ((u8)42)
#define SD_CMD_APP_CMD ((u8)55)
#define SD_CMD_GEN_CMD ((u8)56)
#define SD_CMD_NO_CMD ((u8)64)
/**
* @brief Following commands are SD Card Specific commands.
* SDIO_APP_CMD :CMD55 should be sent before sending these commands.
*/
#define SD_CMD_APP_SD_SET_BUSWIDTH ((u8)6) /*!< For SD Card only */
#define SD_CMD_SD_APP_STAUS ((u8)13) /*!< For SD Card only */
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((u8)22) /*!< For SD Card only */
#define SD_CMD_SD_APP_OP_COND ((u8)41) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((u8)42) /*!< For SD Card only */
#define SD_CMD_SD_APP_SEND_SCR ((u8)51) /*!< For SD Card only */
#define SD_CMD_SDIO_RW_DIRECT ((u8)52) /*!< For SD I/O Card only */
#define SD_CMD_SDIO_RW_EXTENDED ((u8)53) /*!< For SD I/O Card only */
/**
* @brief Following commands are SD Card Specific security commands.
* SDIO_APP_CMD should be sent before sending these commands.
*/
#define SD_CMD_SD_APP_GET_MKB ((u8)43) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_MID ((u8)44) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CER_RN1 ((u8)45) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_CER_RN2 ((u8)46) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CER_RES2 ((u8)47) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_CER_RES1 ((u8)48) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((u8)18) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((u8)25) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_ERASE ((u8)38) /*!< For SD Card only */
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((u8)49) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_WRITE_MKB ((u8)48) /*!< For SD Card only */
//支持的SD卡定義
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((u32)0x00000000)
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((u32)0x00000001)
#define SDIO_HIGH_CAPACITY_SD_CARD ((u32)0x00000002)
#define SDIO_MULTIMEDIA_CARD ((u32)0x00000003)
#define SDIO_SECURE_DIGITAL_IO_CARD ((u32)0x00000004)
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((u32)0x00000005)
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((u32)0x00000006)
#define SDIO_HIGH_CAPACITY_MMC_CARD ((u32)0x00000007)
//SDIO相關(guān)參數(shù)定義
#define NULL 0
#define SDIO_STATIC_FLAGS ((u32)0x000005FF)
#define SDIO_CMD0TIMEOUT ((u32)0x00010000)
#define SDIO_DATATIMEOUT ((u32)0xFFFFFFFF)
#define SDIO_FIFO_Address ((u32)0x40018080)
//Mask for errors Card Status R1 (OCR Register)
#define SD_OCR_ADDR_OUT_OF_RANGE ((u32)0x80000000)
#define SD_OCR_ADDR_MISALIGNED ((u32)0x40000000)
#define SD_OCR_BLOCK_LEN_ERR ((u32)0x20000000)
#define SD_OCR_ERASE_SEQ_ERR ((u32)0x10000000)
#define SD_OCR_BAD_ERASE_PARAM ((u32)0x08000000)
#define SD_OCR_WRITE_PROT_VIOLATION ((u32)0x04000000)
#define SD_OCR_LOCK_UNLOCK_FAILED ((u32)0x01000000)
#define SD_OCR_COM_CRC_FAILED ((u32)0x00800000)
#define SD_OCR_ILLEGAL_CMD ((u32)0x00400000)
#define SD_OCR_CARD_ECC_FAILED ((u32)0x00200000)
#define SD_OCR_CC_ERROR ((u32)0x00100000)
#define SD_OCR_GENERAL_UNKNOWN_ERROR ((u32)0x00080000)
#define SD_OCR_STREAM_READ_UNDERRUN ((u32)0x00040000)
#define SD_OCR_STREAM_WRITE_OVERRUN ((u32)0x00020000)
#define SD_OCR_CID_CSD_OVERWRIETE ((u32)0x00010000)
#define SD_OCR_WP_ERASE_SKIP ((u32)0x00008000)
#define SD_OCR_CARD_ECC_DISABLED ((u32)0x00004000)
#define SD_OCR_ERASE_RESET ((u32)0x00002000)
#define SD_OCR_AKE_SEQ_ERROR ((u32)0x00000008)
#define SD_OCR_ERRORBITS ((u32)0xFDFFE008)
//Masks for R6 Response
#define SD_R6_GENERAL_UNKNOWN_ERROR ((u32)0x00002000)
#define SD_R6_ILLEGAL_CMD ((u32)0x00004000)
#define SD_R6_COM_CRC_FAILED ((u32)0x00008000)
#define SD_VOLTAGE_WINDOW_SD ((u32)0x80100000)
#define SD_HIGH_CAPACITY ((u32)0x40000000)
#define SD_STD_CAPACITY ((u32)0x00000000)
#define SD_CHECK_PATTERN ((u32)0x000001AA)
#define SD_VOLTAGE_WINDOW_MMC ((u32)0x80FF8000)
#define SD_MAX_VOLT_TRIAL ((u32)0x0000FFFF)
#define SD_ALLZERO ((u32)0x00000000)
#define SD_WIDE_BUS_SUPPORT ((u32)0x00040000)
#define SD_SINGLE_BUS_SUPPORT ((u32)0x00010000)
#define SD_CARD_LOCKED ((u32)0x02000000)
#define SD_CARD_PROGRAMMING ((u32)0x00000007)
#define SD_CARD_RECEIVING ((u32)0x00000006)
#define SD_DATATIMEOUT ((u32)0xFFFFFFFF)
#define SD_0TO7BITS ((u32)0x000000FF)
#define SD_8TO15BITS ((u32)0x0000FF00)
#define SD_16TO23BITS ((u32)0x00FF0000)
#define SD_24TO31BITS ((u32)0xFF000000)
#define SD_MAX_DATA_LENGTH ((u32)0x01FFFFFF)
#define SD_HALFFIFO ((u32)0x00000008)
#define SD_HALFFIFOBYTES ((u32)0x00000020)
//Command Class Supported
#define SD_CCCC_LOCK_UNLOCK ((u32)0x00000080)
#define SD_CCCC_WRITE_PROT ((u32)0x00000040)
#define SD_CCCC_ERASE ((u32)0x00000020)
//CMD8指令
#define SDIO_SEND_IF_COND ((u32)0x00000008)
//相關(guān)函數(shù)定義
SDIO_SD_ERROR_INFO SDIO_SdCardInit(void);
void SDIO_ClockSet(u8 clkdiv);
void SDIO_SendCmd(u8 cmdindex,u8 waitrsp,u32 arg);
void SDIO_SendDataConfig(u32 datatimeout,u32 datalen,u8 blksize,u8 dir);
SDIO_SD_ERROR_INFO SDIO_SdPowerON(void);
SDIO_SD_ERROR_INFO SD_PowerOFF(void);
SDIO_SD_ERROR_INFO SDIO_SdCardInitializeCards(void);
SDIO_SD_ERROR_INFO SDIO_SdCardGetInfo(SD_CardInfo *cardinfo);
SDIO_SD_ERROR_INFO SDIO_SdCardEnableWideBusOperation(u32 wmode);
SDIO_SD_ERROR_INFO SDIO_SdCardSetDeviceMode(u32 mode);
SDIO_SD_ERROR_INFO SDIO_SdCardSelectAddr(u32 addr);
SDIO_SD_ERROR_INFO SDIO_SdCardSendStatus(uint32_t *pcardstatus);
SDCardState SDIO_SdCardGetState(void);
SDIO_SD_ERROR_INFO SDIO_SdCardReadBlock(u8 *buf,long long addr,u16 blksize);
SDIO_SD_ERROR_INFO SDIO_SdCardReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
SDIO_SD_ERROR_INFO SDIO_SdCardWriteBlock(u8 *buf,long long addr, u16 blksize);
SDIO_SD_ERROR_INFO SDIO_SdCardWriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
SDIO_SD_ERROR_INFO SDIO_SdCardProcessIRQSrc(void);
SDIO_SD_ERROR_INFO SDIO_CmdErrorCheck(void);
SDIO_SD_ERROR_INFO SDIO_CmdResp7Error(void);
SDIO_SD_ERROR_INFO SDIO_CmdResp1Error(u8 cmd);
SDIO_SD_ERROR_INFO SDIO_CmdResp3Error(void);
SDIO_SD_ERROR_INFO SDIO_CmdResp2Error(void);
SDIO_SD_ERROR_INFO SDIO_CmdResp6Error(u8 cmd,u16*prca);
SDIO_SD_ERROR_INFO SDIO_SdCardEnWideBus(u8 enx);
SDIO_SD_ERROR_INFO SDIO_SdCardProgrammingState(u8 *pstatus);
SDIO_SD_ERROR_INFO SDIO_SdCardFindSCR(u16 rca,u32 *pscr);
u8 convert_from_bytes_to_power_of_two(u16 NumberOfBytes);
void SDIO_SdCard_DMAConfig(u32*mbuf,u32 bufsize,u8 dir);
u8 SDIO_SdCardReadDiskSector(u8*buf,u32 sector,u8 cnt); //讀SD卡,fatfs/usb調(diào)用
u8 SDIO_SdCardWriteDiskSector(u8*buf,u32 sector,u8 cnt); //寫SD卡,fatfs/usb調(diào)用
#endif
-
STM32
+關(guān)注
關(guān)注
2270文章
10910瀏覽量
356611 -
SD卡
+關(guān)注
關(guān)注
2文章
566瀏覽量
63973 -
emmc
+關(guān)注
關(guān)注
7文章
216瀏覽量
52784
發(fā)布評論請先 登錄
相關(guān)推薦
評論