?? mmc_sd.c
字號:
{
u8 csd[16];
u32 Capacity;
u8 r1;
u16 i;
u16 temp;
//取CSD信息,如果期間出錯,返回0
if(SD_GetCSD(csd)!=0) return 0;
//如果為SDHC卡,按照下面方式計算
if((csd[0]&0xC0)==0x40)
{
Capacity=((u32)csd[8])<<8;
Capacity+=(u32)csd[9]+1;
Capacity = (Capacity)*1024;//得到扇區(qū)數(shù)
Capacity*=512;//得到字節(jié)數(shù)
}
else
{
i = csd[6]&0x03;
i<<=8;
i += csd[7];
i<<=2;
i += ((csd[8]&0xc0)>>6);
//C_SIZE_MULT
r1 = csd[9]&0x03;
r1<<=1;
r1 += ((csd[10]&0x80)>>7);
r1+=2;//BLOCKNR
temp = 1;
while(r1)
{
temp*=2;
r1--;
}
Capacity = ((u32)(i+1))*((u32)temp);
// READ_BL_LEN
i = csd[5]&0x0f;
//BLOCK_LEN
temp = 1;
while(i)
{
temp*=2;
i--;
}
//The final result
Capacity *= (u32)temp;//字節(jié)為單位
}
return (u32)Capacity;
}
//讀SD卡的一個block
//輸入:u32 sector 取地址(sector值,非物理地址)
// u8 *buffer 數(shù)據(jù)存儲地址(大小至少512byte)
//返回值:0: 成功
// other:失敗
u8 SD_ReadSingleBlock(u32 sector, u8 *buffer)
{
u8 r1;
//設(shè)置為高速模式
SPIx_SetSpeed(SPI_SPEED_4);
//如果不是SDHC,給定的是sector地址,將其轉(zhuǎn)換成byte地址
if(SD_Type!=SD_TYPE_V2HC)
{
sector = sector<<9;
}
r1 = SD_SendCommand(CMD17, sector, 0);//讀命令
if(r1 != 0x00)return r1;
r1 = SD_ReceiveData(buffer, 512, RELEASE);
if(r1 != 0)return r1; //讀數(shù)據(jù)出錯!
else return 0;
}
////////////////////////////下面2個函數(shù)為USB讀寫所需要的/////////////////////////
//定義SD卡的塊大小
#define BLOCK_SIZE 512
//寫入MSD/SD數(shù)據(jù)
//pBuffer:數(shù)據(jù)存放區(qū)
//ReadAddr:寫入的首地址
//NumByteToRead:要寫入的字節(jié)數(shù)
//返回值:0,寫入完成
// 其他,寫入失敗
u8 MSD_WriteBuffer(u8* pBuffer, u32 WriteAddr, u32 NumByteToWrite)
{
u32 i,NbrOfBlock = 0, Offset = 0;
u32 sector;
u8 r1;
NbrOfBlock = NumByteToWrite / BLOCK_SIZE;//得到要寫入的塊的數(shù)目
SD_CS=0;
while (NbrOfBlock--)//寫入一個扇區(qū)
{
sector=WriteAddr+Offset;
if(SD_Type==SD_TYPE_V2HC)sector>>=9;//執(zhí)行與普通操作相反的操作
r1=SD_SendCommand_NoDeassert(CMD24,sector,0xff);//寫命令
if(r1)
{
SD_CS=1;
return 1;//應(yīng)答不正確,直接返回
}
SPIx_ReadWriteByte(0xFE);//放起始令牌0xFE
//放一個sector的數(shù)據(jù)
for(i=0;i<512;i++)SPIx_ReadWriteByte(*pBuffer++);
//發(fā)2個Byte的dummy CRC
SPIx_ReadWriteByte(0xff);
SPIx_ReadWriteByte(0xff);
if(SD_WaitDataReady())//等待SD卡數(shù)據(jù)寫入完成
{
SD_CS=1;
return 2;
}
Offset += 512;
}
//寫入完成,片選置1
SD_CS=1;
SPIx_ReadWriteByte(0xff);
return 0;
}
//讀取MSD/SD數(shù)據(jù)
//pBuffer:數(shù)據(jù)存放區(qū)
//ReadAddr:讀取的首地址
//NumByteToRead:要讀出的字節(jié)數(shù)
//返回值:0,讀出完成
// 其他,讀出失敗
u8 MSD_ReadBuffer(u8* pBuffer, u32 ReadAddr, u32 NumByteToRead)
{
u32 NbrOfBlock=0,Offset=0;
u32 sector=0;
u8 r1=0;
NbrOfBlock=NumByteToRead/BLOCK_SIZE;
SD_CS=0;
while (NbrOfBlock --)
{
sector=ReadAddr+Offset;
if(SD_Type==SD_TYPE_V2HC)sector>>=9;//執(zhí)行與普通操作相反的操作
r1=SD_SendCommand_NoDeassert(CMD17,sector,0xff);//讀命令
if(r1)//命令發(fā)送錯誤
{
SD_CS=1;
return r1;
}
r1=SD_ReceiveData(pBuffer,512,RELEASE);
if(r1)//讀數(shù)錯誤
{
SD_CS=1;
return r1;
}
pBuffer+=512;
Offset+=512;
}
SD_CS=1;
SPIx_ReadWriteByte(0xff);
return 0;
}
//////////////////////////////////////////////////////////////////////////
//寫入SD卡的一個block(未實(shí)際測試過)
//輸入:u32 sector 扇區(qū)地址(sector值,非物理地址)
// u8 *buffer 數(shù)據(jù)存儲地址(大小至少512byte)
//返回值:0: 成功
// other:失敗
u8 SD_WriteSingleBlock(u32 sector, const u8 *data)
{
u8 r1;
u16 i;
u16 retry;
//設(shè)置為高速模式
//SPIx_SetSpeed(SPI_SPEED_HIGH);
//如果不是SDHC,給定的是sector地址,將其轉(zhuǎn)換成byte地址
if(SD_Type!=SD_TYPE_V2HC)
{
sector = sector<<9;
}
r1 = SD_SendCommand(CMD24, sector, 0x00);
if(r1 != 0x00)
{
return r1; //應(yīng)答不正確,直接返回
}
//開始準(zhǔn)備數(shù)據(jù)傳輸
SD_CS=0;
//先放3個空數(shù)據(jù),等待SD卡準(zhǔn)備好
SPIx_ReadWriteByte(0xff);
SPIx_ReadWriteByte(0xff);
SPIx_ReadWriteByte(0xff);
//放起始令牌0xFE
SPIx_ReadWriteByte(0xFE);
//放一個sector的數(shù)據(jù)
for(i=0;i<512;i++)
{
SPIx_ReadWriteByte(*data++);
}
//發(fā)2個Byte的dummy CRC
SPIx_ReadWriteByte(0xff);
SPIx_ReadWriteByte(0xff);
//等待SD卡應(yīng)答
r1 = SPIx_ReadWriteByte(0xff);
if((r1&0x1F)!=0x05)
{
SD_CS=1;
return r1;
}
//等待操作完成
retry = 0;
while(!SPIx_ReadWriteByte(0xff))
{
retry++;
if(retry>0xfffe) //如果長時間寫入沒有完成,報錯退出
{
SD_CS=1;
return 1; //寫入超時返回1
}
}
//寫入完成,片選置1
SD_CS=1;
SPIx_ReadWriteByte(0xff);
return 0;
}
//讀SD卡的多個block(實(shí)際測試過)
//輸入:u32 sector 扇區(qū)地址(sector值,非物理地址)
// u8 *buffer 數(shù)據(jù)存儲地址(大小至少512byte)
// u8 count 連續(xù)讀count個block
//返回值:0: 成功
// other:失敗
u8 SD_ReadMultiBlock(u32 sector, u8 *buffer, u8 count)
{
u8 r1;
//SPIx_SetSpeed(SPI_SPEED_HIGH);//設(shè)置為高速模式
//如果不是SDHC,將sector地址轉(zhuǎn)成byte地址
if(SD_Type!=SD_TYPE_V2HC)sector = sector<<9;
//SD_WaitDataReady();
//發(fā)讀多塊命令
r1 = SD_SendCommand(CMD18, sector, 0);//讀命令
if(r1 != 0x00)return r1;
do//開始接收數(shù)據(jù)
{
if(SD_ReceiveData(buffer, 512, NO_RELEASE) != 0x00)break;
buffer += 512;
} while(--count);
//全部傳輸完畢,發(fā)送停止命令
SD_SendCommand(CMD12, 0, 0);
//釋放總線
SD_CS=1;
SPIx_ReadWriteByte(0xFF);
if(count != 0)return count; //如果沒有傳完,返回剩余個數(shù)
else return 0;
}
//寫入SD卡的N個block(未實(shí)際測試過)
//輸入:u32 sector 扇區(qū)地址(sector值,非物理地址)
// u8 *buffer 數(shù)據(jù)存儲地址(大小至少512byte)
// u8 count 寫入的block數(shù)目
//返回值:0: 成功
// other:失敗
u8 SD_WriteMultiBlock(u32 sector, const u8 *data, u8 count)
{
u8 r1;
u16 i;
//SPIx_SetSpeed(SPI_SPEED_HIGH);//設(shè)置為高速模式
if(SD_Type != SD_TYPE_V2HC)sector = sector<<9;//如果不是SDHC,給定的是sector地址,將其轉(zhuǎn)換成byte地址
if(SD_Type != SD_TYPE_MMC) r1 = SD_SendCommand(ACMD23, count, 0x00);//如果目標(biāo)卡不是MMC卡,啟用ACMD23指令使能預(yù)擦除
r1 = SD_SendCommand(CMD25, sector, 0x00);//發(fā)多塊寫入指令
if(r1 != 0x00)return r1; //應(yīng)答不正確,直接返回
SD_CS=0;//開始準(zhǔn)備數(shù)據(jù)傳輸
SPIx_ReadWriteByte(0xff);//先放3個空數(shù)據(jù),等待SD卡準(zhǔn)備好
SPIx_ReadWriteByte(0xff);
//--------下面是N個sector寫入的循環(huán)部分
do
{
//放起始令牌0xFC 表明是多塊寫入
SPIx_ReadWriteByte(0xFC);
//放一個sector的數(shù)據(jù)
for(i=0;i<512;i++)
{
SPIx_ReadWriteByte(*data++);
}
//發(fā)2個Byte的dummy CRC
SPIx_ReadWriteByte(0xff);
SPIx_ReadWriteByte(0xff);
//等待SD卡應(yīng)答
r1 = SPIx_ReadWriteByte(0xff);
if((r1&0x1F)!=0x05)
{
SD_CS=1; //如果應(yīng)答為報錯,則帶錯誤代碼直接退出
return r1;
}
//等待SD卡寫入完成
if(SD_WaitDataReady()==1)
{
SD_CS=1; //等待SD卡寫入完成超時,直接退出報錯
return 1;
}
}while(--count);//本sector數(shù)據(jù)傳輸完成
//發(fā)結(jié)束傳輸令牌0xFD
r1 = SPIx_ReadWriteByte(0xFD);
if(r1==0x00)
{
count = 0xfe;
}
if(SD_WaitDataReady()) //等待準(zhǔn)備好
{
SD_CS=1;
return 1;
}
//寫入完成,片選置1
SD_CS=1;
SPIx_ReadWriteByte(0xff);
return count; //返回count值,如果寫完則count=0,否則count=1
}
//在指定扇區(qū),從offset開始讀出bytes個字節(jié)
//輸入:u32 sector 扇區(qū)地址(sector值,非物理地址)
// u8 *buf 數(shù)據(jù)存儲地址(大小<=512byte)
// u16 offset 在扇區(qū)里面的偏移量
// u16 bytes 要讀出的字節(jié)數(shù)
//返回值:0: 成功
// other:失敗
u8 SD_Read_Bytes(unsigned long address,unsigned char *buf,unsigned int offset,unsigned int bytes)
{
u8 r1;u16 i=0;
r1=SD_SendCommand(CMD17,address<<9,0);//發(fā)送讀扇區(qū)命令
if(r1)return r1; //應(yīng)答不正確,直接返回
SD_CS=0;//選中SD卡
if(SD_GetResponse(0xFE))//等待SD卡發(fā)回數(shù)據(jù)起始令牌0xFE
{
SD_CS=1; //關(guān)閉SD卡
return 1;//讀取失敗
}
for(i=0;i<offset;i++)SPIx_ReadWriteByte(0xff);//跳過offset位
for(;i<offset+bytes;i++)*buf++=SPIx_ReadWriteByte(0xff);//讀取有用數(shù)據(jù)
for(;i<512;i++) SPIx_ReadWriteByte(0xff); //讀出剩余字節(jié)
SPIx_ReadWriteByte(0xff);//發(fā)送偽CRC碼
SPIx_ReadWriteByte(0xff);
SD_CS=1;//關(guān)閉SD卡
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -