📄 mmc_sd.c
字号:
}
//等待操作完 //wait no busy
while(!SPI_WriteByte(0xff))if(retry++ > ACCESS_TIMEOUT){SPI_CS_Deassert;return 1;}
SPI_CS_Deassert;
SPI_WriteByte(0xff);// extra 8 CLK
return 0;
}
/* return number of max block, in case of 512bytes per unit */
uint32 MMC_SD_ReadCapacity()
{
uint8 r1;
uint16 i;
uint16 temp;
uint8 buffer[16];
uint32 Capacity;
uint32 retry =0;
//uint8 retry=0;
//if(address_mode==BLOCK_MODE) return 0; /*currently I do not have the spec. so do not kown how to calculate the capacity*/
//SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommandCRC_NoDeassert(9, 0,0);//写命令 //send command //READ CSD
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > ACCESS_TIMEOUT){SPI_CS_Deassert;return 1;}
for(i=0;i<16;i++)
{
buffer[i]=SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert;
SPI_WriteByte(0xff);// extra 8 CLK
if((buffer[0]&0xc0)==0x40)
{
Capacity = (((uint32)buffer[8])<<8 + (uint32)buffer[9] +1)*(uint32)1024;
return Capacity;
}
/*********************************/
// 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/512;
}
#if 0
/* SD card initialization, include reset and configuration */
uint8 MMC_SD_Init(void)
{
uint8 i;
uint8 retry = 0;
uint8 r1 = 0;
#if 0
AT91PS_SPI pSPI = AT91C_BASE_SPI;
AT91PS_PMC pPMC = AT91C_BASE_PMC;
MMC_SD_PORT_INI; /* Port Initialize */
/* disable PIO from controlling MOSI, MISO, SCK (=hand over to SPI) */
pPIO->PIO_PDR = AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA14_SPCK;
/* set pin-functions in PIO Controller */
pPIO->PIO_ASR = AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA14_SPCK;
/* enable peripheral clock for SPI ( PID Bit 5 ) */
pPMC->PMC_PCER = ( 1 << AT91C_ID_SPI ); /* n.b. IDs are just bit-numbers */
/* SPI enable and reset */
pSPI->SPI_CR = AT91C_SPI_SPIEN | AT91C_SPI_SWRST;
/* SPI mode: master, fixed periph. sel., FDIV=0, fault detection disabled */
pSPI->SPI_MR = AT91C_SPI_MSTR | AT91C_SPI_PS_FIXED | AT91C_SPI_MODFDIS;
/* set PCS for fixed select, in this program we do not use select of the SPI */
/* We control the select ourself, so the default cs is 0, but we never use it */
pSPI->SPI_MR &= 0xFFF0FFFF; /* clear old PCS - redundant (AT91lib) */
pSPI->SPI_MR |= ( (0<<16) & AT91C_SPI_PCS ); /* set PCS */
pSPI->SPI_CSR[0] = AT91C_SPI_NCPHA | AT91C_SPI_BITS_8;
#endif
SPI1_CS_GPIO();
SPI1_CS_OUT();
SPI_CS_Deassert;
PINSEL1 &= ~((0x03 << 2) + (0x03 << 4) + (0x03 << 6));
PINSEL1 |= (0x02 << 2) + (0x02 << 4) + (0x02 << 6);
SSPCR0 = (0x00 << 8) | // SCR=0
(0x00 << 7) | // CPHA =0时钟输出相位,仅SPI模式有效
(0x00 << 6) | // CPOL =0时钟输出极性,仅SPI模式有效
(0x00 << 4) | // FRF =00 帧格式 00=SPI,01=SSI,10=Microwire,11=保留
(0x07 << 0); // DSS 数据长度,0000-0010=保留,0011=4位,0111=8位,1111=16位
SSPCR1 = (0x00 << 3) | // SOD 从机输出禁能,1=禁止,0=允许
(0x00 << 2) | // MS 主从选择,0=主机,1=从机
(0x01 << 1) | // SSE SSP使能,1=允许SSP与其它设备通信
(0x00 << 0); // LBM 回写模式
/* set SPI clock speed */
SPI_Low();
#if 0
/* Enable SPI interface */
pSPI->SPI_CR = AT91C_SPI_SPIEN;
#endif
do
{
for(i=0;i<10;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//发idle命令 //send idle command
retry++;
if(retry>0xfe) return 1;//超时退出 //time out
} while(r1 != 0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0);//发active命令 //send active command
retry++;
if(retry>0xfe) return 1;//超时退出 //time out
} while(r1);
SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommand(59, 0);//关crc //disable CRC
r1 = MMC_SD_SendCommand(16, 512);//设扇区大小512 //set sector size to 512
return 0;//正常返回 //normal return
}
//读一个扇区 //read one sector
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommand(17, sector<<9);//读命令 //read command
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
//等数据的开始 //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个数据 //read 512 bytes
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//伪crc //dummy crc
SPI_WriteByte(0xff);
SPI_CS_Deassert;
SPI_WriteByte(0xff);// extra 8 CLK
return 0;
}
//写一个扇区 //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommand(24, sector<<9);//写命令 //send command
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xfe);//发开始符 //send start byte "token"
for(i=0; i<512; i++)//送512字节数据 //send 512 bytes data
{
SPI_WriteByte(*buffer++);
}
SPI_WriteByte(0xff); //dummy crc
SPI_WriteByte(0xff);
r1 = SPI_WriteByte(0xff);
if( (r1&0x1f) != 0x05)//等待是否成功 //judge if it successful
{
SPI_CS_Deassert;
return r1;
}
//等待操作完 //wait no busy
while(SPI_WriteByte(0xff) != 0x00)
{
if(retry++ > 0xfffe)
{
SPI_CS_Deassert;
return 1;
}
}
SPI_CS_Deassert;
SPI_WriteByte(0xff);// extra 8 CLK
return 0;
}
uint32 MMC_SD_ReadCapacity(void)
{
uint8 r1;
uint16 i;
uint16 temp;
uint8 buffer[16];
uint32 Capacity;
uint16 retry =0;
//uint8 retry=0;
SPI_High(); /* Use High Speed SPI*/
r1 = MMC_SD_SendCommand(9, 0);//写命令 //send command //READ CSD
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
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_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;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -