×
嵌入式 > 技术百科 > 详情

单片机读写SD卡最简单最基本的程序

发布时间:2020-06-20 发布时间:
|
  单片机读写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<
  }

  

  

  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;

  }
[page]
  

  void read_data(U8 *buffer)

  {

  U32 i;

  U8 rsp = 0;

  while(!(rsp == 0xfe)) //答应字节的最低为0则代表起始位

  rsp = spi_read_byte();

  for(i = 0;i < BLOCK_LEN; i++) //读一个block的内容,一般为512字节

  buffer = spi_read_byte();

  for(i = 0; i < 2; i++) //读两个CRC校正码

  send_clk();

  send_clk(); //读结束字节

  }

  

  U8 write_data(U8 *buffer)

  {

  U16 rsp = 0, tmp = 0, busy = 0, i = 6;

  spi_rt_mode();

  spi_write_byte(0xfe); //起始位

  for(i = 0; i < 512; i++) //发送512个字节

  spi_write_byte(buffer);

  for(i = 0; i < 2; i++) //发送16位的CRC校正

  spi_write_byte(0xff);

  spi_ro_mode(); //等待答应

  while(!(rsp == 0x1))

  {

  rsp =(U16)spi_read_byte();

  tmp = rsp;

  rsp &= 0x11;

  }

  while(!(busy == 0xff)) //判忙

  {

  busy = spi_read_byte();

  }

  tmp &= 0xe;

  if (tmp == 4)

  return NO_ERR;

  else

  {

  Uart_Printf("writing error!!! ");

  return WR_SGL_BLK_ERR;

  }

  }

  

  U8 read_register(U8 len, U8 *buffer)

  {

  U8 rsp = 0xff, i = 0;

  spi_ro_mode();

  while((rsp == 0xff) && (i < 100))

  {

  rsp=spi_read_byte();

  }

  if (i > 99)

  {

  Uart_Printf("ERR in readding register!!! ");

  return rsp;

  }

  if (rsp != 0xfe)

  {

  buffer[0] = rsp;

  i = 1;

  }

  else

  i = 0;

  for( ; i < len; i++)

  buffer = spi_read_byte();

  for(i = 0; i < 2; i++ )

  send_clk();

  send_clk();

  return NO_ERR;

  }

  

  

  void send_clk()

  {

  rSIOCON |= (1 << 3); //使能SPI

  while (!(rINTPND & BIT_SIO)); //等待发送完毕

  rI_ISPC|=BIT_SIO; //清除中断标志

  }

  

  void spi_write_byte(U8 dat)

  {

  rSIODAT = dat;

  send_clk(); //SPI发送

  }

  

  U8 spi_read_byte(void)

  {

  send_clk(); //SPI发送

  return rSIODAT;

  }

  

  void spi_port_init()

  {

  rIVTCNT = 0;

  rPCONF = (rPCONF & 0xe3ff) | 0x1B0C00; //除了CLK,茶叶MISO,MOSI外,不改变其他位

  rPUPF |= 0x160; //使能MISO的上拉电阻

  }

  

  #ifndef _SD_CONG

  #define _SD_CONG

  #define BLOCK_LEN (512) //一个block的长度

  #define CMD0 0

  #define CMD1 1 // 读OCR寄存器

  #define CMD9 9 // 读CSD寄存器

  #define CMD10 10 // 读CID寄存器

  #define CMD12 12 // 停止读多块时的数据传输

  #define CMD13 13 // 读 Card_Status 寄存器

  #define CMD16 16 // 设置块的长度

  #define CMD17 17 // 读单块

  #define CMD18 18 // 读多块,直至主机发送CMD12

  #define CMD24 24 // 写单块

  #define CMD25 25 // 写多块

  #define CMD27 27 // 写CSD寄存器

  #define CMD28 28 // Set the write protection bit of the addressed group

  #define CMD29 29 // Clear the write protection bit of the addressed group

  #define CMD30 30 // Ask the card for the status of the write protection bits

  #define CMD32 32 // 设置擦除块的起始地址

  #define CMD33 33 // 设置擦除块的终止地址

  #define CMD38 38 //擦除所选择的块

  #define CMD42 42 // 设置/复位密码或上锁/解锁卡

  #define CMD55 55 // 禁止下一个命令为应用命令

  #define CMD56 56 // 应用命令的通用I/O

  #define CMD58 58 // 读OCR寄存器

  #define CMD59 59 // 使能或禁止

  //错误返回

  #define INIT_FAIL 0

  #define NO_ERR 1

  #define WR_SGL_BLK_ERR 2

  #define GET_INFO_ERR 3

  #define R1 1 //SD卡答应类型,表示一个字节

  #define R2 2 //SD卡答应类型,表示两个字节

  //一下是移植时需修改的内容

  #define SD_desel() rPDATE=0x20; //使能SD卡

  #define SD_sel() rPDATE=0x00; //放开SD卡

  #define spi_high_speed() rSBRDR = 5; //spi高速模式

  #define spi_low_speed() rSBRDR = 99; //spi低速模式

  #define spi_ro_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x0 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //只读模式

  #define spi_rt_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x1 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //读写模式

  #endif 



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
Keil5(MDK5)在调试(debug)过程中遇到的问题