#include <MSP430x14x.h>
//#include "USART.C"
/*sbit SD_CLK = P1^1;
sbit SD_DI = P1^2;
sbit SD_DO = P1^0;
sbit SD_CS = P1^3;*/
#define SDCLK_H P4OUT|=BIT1
#define SDCLK_L P4OUT&=~BIT1
#define SDDI_H P4OUT|=BIT2
#define SDDI_L P4OUT&=~BIT2
#define SDDO P4IN
#define SDCS_H P4OUT|=BIT3
#define SDCS_L P4OUT&=~BIT3
//===========================================================
//写一字节到SD卡,模拟SPI总线方式
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i;i--)
{
SDCLK_H;
SDCLK_L;
if((n&0x80)==0x80){SDDI_H;}
if((n&0x80)==0x00){SDDI_L;}
n<<=1;
}
SDDI_H;
}
//===========================================================
//从SD卡读一字节,模拟SPI总线方式
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
{
SDCLK_L;
SDCLK_H;
n<<=1;
if(SDDO) n|=1;
}
return n;
}
//============================================================
//检测SD卡的响应
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//================================================================
//发命令到SD卡
void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
}
//================================================================
//初始化SD卡
unsigned char SD_Init()
{
//int delay1=0, trials=0;
unsigned char i;
unsigned char response=0x01;
SDCS_H;
for(i=0;i<=9;i++)
SdWrite(0xff);
SDCS_L;
//Send Command 0 to put MMC in SPI mode
SdCommand(0x00,0,0x95);
response=SdResponse();
if(response!=0x01)
{
return 0;
}
while(response==0x01)
{
SDCS_H;
SdWrite(0xff);
SDCS_L;
SdCommand(0x01,0x00ffc000,0xff);
response=SdResponse();
}
SDCS_H;
SdWrite(0xff);
return 1;
}
//================================================================
//往SD卡指定地址写数据,一次最多512字节
unsigned char SD_WriteSector(unsigned long address,unsigned char *Block, int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SDCS_L;
//Then send write command
SdCommand(0x18,address,0xff);
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
/
w send data
for(count=0;count<len;count++) SdWrite(*Block++);
for(;count<512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //两字节CRC校验, 为0XFFFF 表示不考虑CRC
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SDCS_H;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR\n");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token.\n");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC.\n");
return 0;
}
//=======================================================================
//从SD卡指定地址读取数据,一次最多512字节
unsigned char SD_ReadSector( unsigned long address,unsigned char *Block,int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
//printf("MMC_read_block\n");
SDCS_L;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==0)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;count<len;count++) *Block++=SdRead();
for(;count<512;count++) SdRead();
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SDCS_H;
SdRead();
return 1;
}
//printf("Command 0x11 (Read) was not received by the MMC.\n");
return 0;
}