单片机读写SD卡最简略最基本的程序
/>
/> 2010-11-01 21:14
转载自 刀禁凯森
终究修正 zlulu2008
处理器:s3c44b0 (arm7)
SD卡与处理器的引脚衔接:MISO –>SIORxD MOSI –>SIOTxD CLK –>SCLK CS –>PE5
包含四个文件:sd_drive.c :用户API函数,移植时不需修正
sd_cmd.c:中间层函数,移植时不需修正
sd_hard.c:硬件层函数,移植时需修正
sd_config.h:一些功用的宏界说,移植时需修正
第一次读写SD卡时,需调用SD_Init(void),然后就可以条用 Read_Single_Block或许Write_Single_Block进行读写操作
留意:进行写操作时,最好不要写前700个扇区,应为这些扇区都是FAT文件体系的重要扇区,一旦误写则可能会导致SD无法被电脑辨认,需格式化。
U8 Read_Single_Block(U32 blk_addr, U8 *rx_buf)
{
U16 rsp = 1;
U8 i = 0;
SD_sel(); //使能SD卡
while(rsp && (i < 100)) { write_cmd(CMD17, blk_addr << 9); //写指令CMD17 rsp = Get_rsp(R1); //获取容许 send_clk(); } if(i > 99) //假如指令超时,则履行超时处理
{
SD_desel();
Uart_Printf(“fail in writing CMD17”);
return WR_SGL_BLK_ERR;
}
spi_ro_mode();
send_clk(); //发送8个clk
read_data(rx_buf); //读取512字节
SD_desel();
Uart_Printf(“succeed in reading the %dst block!!!”, blk_addr);
return NO_ERR;
}
U8 Write_Single_Block(U32 blk_addr, U8 *tx_buf)
{
U16 rsp = 1;
U8 i = 0;
SD_sel(); //使能SD卡
while(rsp && (i < 100)) { write_cmd(CMD24, blk_addr << 9); //写指令CMD24 rsp = Get_rsp(R1); //获取容许 send_clk(); } if(i > 99) //假如指令超时,则履行超时处理
{
SD_desel();
Uart_Printf(“fail in writing CMD17”);
return WR_SGL_BLK_ERR;
}
spi_ro_mode();
send_clk(); //发送8个clk
write_data(tx_buf); //读取512字节
SD_desel();
Uart_Printf(“succeed in writing a block!!!”);
return NO_ERR;
}
U8 SD_Init(void)
{
U16 rsp = 1;
U8 i = 0;
spi_port_init(); //初始化spi端口
spi_low_speed(); //初始化时SPI的速度有必要低于400khz
spi_ro_mode(); //只读形式
SD_sel(); //挑选SD卡
for (i = 0;i < 10; i++) //发送至少75个clk send_clk(); while(rsp && (i++ < 100)) { write_cmd(CMD0, 0); //写指令CMD0 rsp = Get_rsp(R1); //获取容许 if (rsp == 1) //rsp为0则初始化成功http://www.tea176.com,为1则持续写CMD0 break; send_clk(); } SD_desel(); if (i > 99) //初始化超时处理
{
Uart_Printf(“fail in writing CMD0!!!”);
return INIT_FAIL;
}
i=0;
SD_sel();
while(rsp && (i++ < 100)) { write_cmd(CMD1, 0); //写CMD1 rsp = Get_rsp(R1); //获取容许 send_clk(); } SD_desel(); if (i > 99)
{
Uart_Printf(“fail in writing CMD1!!!”);
return INIT_FAIL;
}
Uart_Printf(“SD card init OK!!!”);
spi_high_speed(); //初始化作业悉数结束,SPI进入形式形式
spi_rt_mode();
return NO_ERR;
}
void SD_info()
{
U8 rsp=0;
U8 csd[16];
SD_sel();
write_cmd(CMD9, 0);
rsp = Get_rsp(R1);
if (rsp != 0)
{
SD_desel();
Uart_Printf(“error in getting SD info!!!”);
return ;//GET_INFO_ERR;
}
if (read_register(16, csd) != NO_ERR)
{
SD_desel();
return ;
}
SD_desel();
Uart_Printf(“SD information :”);
if (csd[0] & 0x40 ==0x40)
{
Uart_Printf(“version 2.0”);
Uart_Printf(“size is : %d”,1024 * (csd[8]<<8 + csd[9])); } else { Uart_Printf(“version 1.x “); Uart_Printf(“size is : %d MByte”, ((((csd[6]&0x03)<<10) | (csd[7]<<2) | ((csd[8]&0xC0)>>6) + 1) * (1 << ((((csd[9]&0x03)<<1) | ((csd[10]&0x80)>>7)) + 2)))>>11);
}
Uart_Printf(“max block lenght is : %d”,1<<(csd[5]&0x0f)); } void write_cmd(U8 cmd, U32 addr) { U8 i = 0; U8 temp[4]; spi_rt_mode(); //spi发送与接纳形式 if (cmd <= 13) //前13个指令与地址无关 { spi_write_byte((cmd & 0x3F) | 0x40); //指令最高两位有必要是01 for(i = 0; i < 4; i++) //发送4个0,协议规则的 spi_write_byte(0); if (cmd == 0) spi_write_byte(0x95); //假如是CMD0,则要发送CRC校对 else spi_write_byte(0xff); //非CMD0,则无需CRC校对,默以为0xFF } else { for(i = 0; i < 4; i++) //将32位的地址分割成4个字节,预备发送 temp=(char)(addr >> (24 – 8 * i));
spi_write_byte((cmd & 0x3F) | 0x40); //指令最高两位有必要是01
for(i =0; i < 4; i++)
spi_write_byte(temp); //发送地址,共4个字节
spi_write_byte(0xff); //非CMD0,则无需CRC校对,默以为0xFF
}
}
U16 Get_rsp(U8 type)
{
U16 rsp, temp;
spi_ro_mode(); //spi只读形式
send_clk(); //先发送8个clk
rsp = spi_read_byte(); //用spi读取容许字节
if (rsp & 0x8)
rsp = spi_read_byte();
if (type == R2) //假如是R2类型,则容许为两个字节,须再次读取
{
temp = rsp << 8;
rsp = spi_read_byte();
rsp = temp | rsp;
}
return rsp;
}