?? mmc_sd.c
字號:
/*********************************************************
+-----------------------------------------+
| SD卡模塊 |
+-----------------------------------------+
使用UART0串口SPI模式控制SD卡存儲 端口選擇 P3.0/STE=1
P3.1/SIMO=2 P3.2/SOMI=7 P3.3/UCLK=5 vcc(3.3)=4
Gnd=3,6
SD卡的底層驅(qū)動程序,主控芯片為msp340-149;
其他的微處理器(帶SPI接口的)只需稍加修改即可適用;
->產(chǎn)品咨詢 QQ:461693690
MSN:ly461693690@hotmail.com
說明:SD卡模塊 通過測試,確保其正確性與穩(wěn)定性,請放心使用,
***********************************************************/
#include "MMC_SD.h"
void open_xt1(void) //設(shè)置時鐘為8M晶振
{
BCSCTL1 &= ~XT2OFF;
char n;
do
{
IFG1 &= ~OFIFG;
for(n=0xee;n>0;n--)
;
}
while((IFG1&OFIFG)!=0);
BCSCTL2 |= SELM_2 + SELS;
}
/* spi low speed, below 400KHz */
void SPI_Low(void)
{ BCSCTL2 |= DIVS_1;
UCTL0=SWRST;//開啟設(shè)置
UTCTL0|=SSEL1+STC;//選擇SMCLK
UCTL0|=CHAR+SYNC+MM;//8為數(shù)據(jù) SPI模式
UBR00=0x03;//波特率設(shè)置
UBR10=0X00;
UMCTL0=0X00;
ME1|=USPIE0;//使能SPI
UCTL0&=~SWRST;//關(guān)閉設(shè)置
}
/* spi high speed, not exceed 25MHz */
void SPI_High(void)
{
open_xt1();
UCTL0=SWRST;//開啟設(shè)置
UTCTL0=SSEL1+STC;//選擇SMCLK
UCTL0|=CHAR+SYNC+MM;//8為數(shù)據(jù) SPI模式
UBR00=0x03;//波特率設(shè)置
UBR10=0X00;
UMCTL0=0X00;
ME1|=USPIE0;//使能SPI
UCTL0&=~SWRST;//關(guān)閉設(shè)置
}
/* read and write one byte , full duplex */
uint8 SPI_WriteByte(uint8 val)
{
while((IFG1&UTXIFG0)==0);//等待...直到發(fā)送中斷標志位置位
TXBUF0=val;
while((IFG1&URXIFG0)==0);//等待...直到接受緩存數(shù)據(jù)滿 再取回數(shù)據(jù)
return (RXBUF0);
}
/* sd send command */
uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
{
uint8 r1;
uint8 retry=0;
SPI_WriteByte(0xff);
SPI_CS_Assert;
SPI_WriteByte(cmd | 0x40); /* send command */
SPI_WriteByte(arg>>24);
SPI_WriteByte(arg>>16);
SPI_WriteByte(arg>>8);
SPI_WriteByte(arg);
SPI_WriteByte(0x95);
while((r1 = SPI_WriteByte(0xff)) == 0xff) /* wait response */
if(retry++ > 0xfe) break; /* time out error */
return r1; /* return state */
}
/* SD card initialization, include reset and configuration */
uint8 MMC_SD_Init(void)
{
uint8 i;
uint8 retry = 0;
uint8 r1 = 0;
P3SEL|=0X0e;//0000 1110 P3.0.1.2.3選擇第二功能
P3DIR|=0X0B;//0000 1011 /* Port Initialize */
SPI_Low(); /* SPI intialize */
do
{
for(i=0;i<10;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//發(fā)idle命令 //send idle command
retry++;
if(retry>0xfe) return 1;//超時退出 //time out
} while(r1 != 0x01);
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0); //發(fā)active命令
retry++;
if(retry>0xfe) return 1; //超時退出
} while(r1);
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommand(59, 0); //關(guān)crc
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
r1 = MMC_SD_SendCommand(16, 512); //設(shè)扇區(qū)大小512
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
return 0; //正常返回
}
//讀一個扇區(qū) //read one sector
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
r1 = MMC_SD_SendCommand(17, sector<<9); //讀命令
if(r1 != 0x00)
return r1;
//等數(shù)據(jù)的開始 //wait to start recieve data
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 0xfffe){SPI_CS_Deassert;return 1;}
for(i=0; i<512; i++) //讀512個數(shù)據(jù)
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff); //偽crc
SPI_WriteByte(0xff);
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
return 0;
}
//寫一個扇區(qū) //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
r1 = MMC_SD_SendCommand(24, sector<<9); //寫命令
if(r1 != 0x00)
return r1;
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xfe); //發(fā)開始符
for(i=0; i<512; i++) //送512字節(jié)數(shù)據(jù)
{
SPI_WriteByte(*buffer++);
}
SPI_WriteByte(0xff); //偽crc
SPI_WriteByte(0xff);
r1 = SPI_WriteByte(0xff);
if( (r1&0x1f) != 0x05) //等待是否成功
{
SPI_CS_Deassert;
return r1;
}
//等待操作完
while(!SPI_WriteByte(0xff))if(retry++ > 0xfffe){SPI_CS_Deassert;return 1;}
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
return 0;
}
uint32 MMC_SD_ReadCapacity()
{
uint8 r1;
uint16 i;
uint16 temp;
uint8 buffer[16];
uint32 Capacity;
uint16 retry =0;
r1 = MMC_SD_SendCommand(9,0); //寫命令
if(r1 != 0x00)
return r1;
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 0xfffe){SPI_CS_Deassert;return 1;}
for(i=0;i<16;i++)
{
buffer[i]=SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert;
SPI_WriteByte(0xff); // extra 8 CLK
/*********************************/
// C_SIZE
i = buffer[6]&0x03;
i<<=8;
i += buffer[7];
i<<=2;
i += ((buffer[8]&0xc0)>>6);
/**********************************/
// C_SIZE_MULT
r1 = buffer[9]&0x03;
r1<<=1;
r1 += ((buffer[10]&0x80)>>7);
/**********************************/
// BLOCKNR
r1+=2;
temp = 1;
while(r1)
{
temp*=2;
r1--;
}
Capacity = ((uint32)(i+1))*((uint32)temp);
/////////////////////////
// READ_BL_LEN
i = buffer[5]&0x0f;
/*************************/
//BLOCK_LEN
temp = 1;
while(i)
{
temp*=2;
i--;
}
/************************/
/************** formula of the capacity ******************/
//
// memory capacity = BLOCKNR * BLOCK_LEN
//
// BLOCKNR = (C_SIZE + 1)* MULT
//
// C_SIZE_MULT+2
// MULT = 2
//
// READ_BL_LEN
// BLOCK_LEN = 2
/**********************************************/
//The final result
Capacity *= (uint32)temp;
return Capacity;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -