unsigned char CS_EN,DI_EN ;
void delay(int i)
{
while(i>0)
{
i--;
}
}
unsigned char Write_Cmd(unsigned char *cmd,unsigned char lres)
{
unsigned char rddata[1],time=0;
do
{
time=0;
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,0);
IOWR_ALTERA_AVALON_PIO_DATA(DI_EN_BASE,0);
alt_avalon_spi_command(SPI_BASE,0,6,cmd,0,NULL,1);
IOWR_ALTERA_AVALON_PIO_DATA(DI_EN_BASE,1);
alt_avalon_spi_command(SPI_BASE,0,1,0xff,0,NULL,1); //provide 8 more clock for internal process of SD card
do
{
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
time++;
}while((rddata[0]==0xff)&&(time<50));
}while(rddata[0]==0xff);
if(!lres)
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,1);
alt_avalon_spi_command(SPI_BASE,0,1,0Xff,0,NULL,0); //provide 8 more clock after receive response of SD card
return(rddata[0]);
}
unsigned char SD_Reset()
{
unsigned char i ;
unsigned char rddata[1],wrdata[1];
unsigned char cmd0[]={0x40,0x00,0x00,0x00,0x00,0x95};
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,1);
IOWR_ALTERA_AVALON_PIO_DATA(DI_EN_BASE,0);
for(i=0; i<0x10;i++)
{
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,0);
}
do
{
rddata[0]=Write_Cmd(cmd0,0) ;
}while(rddata[0]!=0x01);
return 0 ;
}
unsigned char SD_write_sector(unsigned long address,unsigned char *Buffer)
{
unsigned char rddata[1],wrdata[1];
unsigned int i;
unsigned char cmd24[]={0x58,0x00,0x00,0x00,0x00,0x00,0xff};
address <<=9;
cmd24[1]=((address&0xff000000)>>24);
cmd24[2]=((address&0x00ff0000)>>16);
cmd24[3]=((address&0x0000ff00)>>8);
CS_EN =0 ;
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,CS_EN);
do
{
do
{
rddata[0]=Write_Cmd(cmd24,1);
}while(rddata[0]!=0);
for(i=0;i<5;i++)
{
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,1);
}
DI_EN = 0 ;
IOWR_ALTERA_AVALON_PIO_DATA(DI_EN_BASE,DI_EN);
wrdata[0]=0xfe;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,1);
alt_avalon_spi_command(SPI_BASE,0,512,Buffer,0,NULL,1);
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,1);
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,1);
DI_EN=1;
IOWR_ALTERA_AVALON_PIO_DATA(SPI_BASE,DI_EN);
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
}while((rddata[0]&0x1f)!=0x05);
do
{
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
}while(rddata[0]!=0xff);
CS_EN =1 ;
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,CS_EN);
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,0);
return(0);
}
unsigned char SD_read_sector(unsigned long address,unsigned char *buffer)
{
unsigned char rddata[1],wrdata[1];
unsigned char cmd17[]={0x51,0x00,0x00,0x00,0x00,0xff};
address<<=9;
cmd17[1]=((address&0xff000000)>>24);
cmd17[2]=((address&0x00ff0000)>>16);
cmd17[3]=((address&0x0000ff00)>>8);
CS_EN = 0;
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,CS_EN);
do
{
rddata[0]=Write_Cmd(cmd17,1);
}while(rddata[0]!=0);
do
{
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
}while(rddata[0]!=0xfe);
alt_avalon_spi_command(SPI_BASE,0,0,NULL,512,buffer,1);
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
alt_avalon_spi_command(SPI_BASE,0,0,NULL,1,rddata,1);
CS_EN = 1;
IOWR_ALTERA_AVALON_PIO_DATA(CS_EN_BASE,CS_EN);
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,0);
return 0;
}
unsigned char SD_Init()
{
unsigned char rddata_a[1],rddata_b[1],wrdata[1];
unsigned char time;
unsigned char cmd55[]={0x77,0x00,0x00,0x00,0x00,0xff};
unsigned char acmd41[]={0x69,0x00,0x00,0x00,0x00,0xff};
do
{
rddata_a[0]=Write_Cmd(cmd55,0);
rddata_b[0]=Write_Cmd(acmd41,0);
}while(rddata_b[0]!=0);
wrdata[0]=0xff;
alt_avalon_spi_command(SPI_BASE,0,1,wrdata,0,NULL,0);
return(0);
}