微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 我正在用NIOSII 写SD卡驱动程序,写到初始化成功,但CMD24 和 CMD17 死活返回值不对,求大神解答,或者给个思路,给个源码

我正在用NIOSII 写SD卡驱动程序,写到初始化成功,但CMD24 和 CMD17 死活返回值不对,求大神解答,或者给个思路,给个源码

时间:10-02 整理:3721RD 点击:
int sd_read_sector(alt_u32 sd_base, int sdhc, alt_u32 sect, alt_u8 *buf)
{
  int i;
  alt_u8 rcode, token;
  alt_u32 addr, ocr;
  /* byte addr for SD card; sector # for SDHC card */
  if (sdhc==0)
    addr = (sect<<9);
  else
    addr = sect;
  /* send sd card read single block commend */
  rcode = sd_wr_cmd(sd_base, SD_CMD17_READ, addr, &ocr);
  ////if (rcode != 0x00){
    printf("CMD17 command fails: R1=0x%02x \n", rcode);
    return (-1);
  ////}
  /* wait for data start token up to 0.1 sec */
  for(i=0; i<SD_READ_TIME_OUT; i++){
    token = sd_spi_rd_byte(sd_base);
    if (token==SD_TOKEN_DATA_START)   // correct response received
      break;
  }
  if (token!=SD_TOKEN_DATA_START){   //time-out
    printf("No data start token: last token=0x%02x\n", token);
    return (-1);
  }
  /* read one sector (512 bytes) */
  for (i=0; i<512; i++)
    buf[i] = sd_spi_rd_byte(sd_base);
  /* read and discard two crc bytes */
  sd_spi_rd_byte(sd_base);
  sd_spi_rd_byte(sd_base);
  return (0);
}
/***********************************************************************
* function: sd_write_sector()
* purpose:  write one sector (512 (2^9) bytes) from a buffer to sd card
* argument:
*   sd_base: base address of sd card interface
*   sdhc: 0 for SD card; 1 for SDHC card
*   sect: sector number
*   buf: pointer to buffer
* return: 0 for success; -1 otherwise
* note:
*   - procedure:
*     1. send write-one-sector command
*     2. send data_start token
*     3. send 512 bytes
*     4. send 2 dummy crc bytes
*     5  check data accept token
*     5. wait for completion
*   - CMD24 argument
*     - standard capacity SD card: starting byte address (sect<<9)
*     - high capacity SDHC card: starting sector address (sect)
*   - 10 MHz sclk used to calculate # timeout cycles
***********************************************************************/
int sd_write_sector(alt_u32 sd_base, int sdhc, alt_u32 sect, alt_u8 *buf)
{
  int i;
  alt_u8 rcode, token;
  alt_u32 addr, dummy;
  /* byte addr for SD card; sector # for SDHC card */
  if (sdhc==0)
    addr = (sect<<9);
  else
    addr = sect;
  /* send sd card write single block commend */
  rcode = sd_wr_cmd(sd_base, SD_CMD24_WRITE, addr, &dummy);        // error
  ////if (rcode != 0x00){
    printf("\nCMD24 command fails: R1=0x%02x \n", rcode);
    return (-1);
  ////}
  /* initiate transfer by send data start token */
  sd_spi_wr_byte(sd_base, SD_TOKEN_DATA_START);
  /* send 512 bytes */
  for (i=0; i<512; i++){
    sd_spi_wr_byte(sd_base, buf[i]);
  }
  /* send two dummy crc bytes */
  sd_spi_wr_byte(sd_base, 0xff);
  sd_spi_wr_byte(sd_base, 0xff);
  /* wait for data acceptance token up to 0.25 sec */
  for(i=0; i<SD_WRITE_TIME_OUT; i++){
    token = sd_spi_rd_byte(sd_base);
    token = token & 0x1f;              // only 5 LSBs used
    if (token==SD_TOKEN_DATA_ACCEPT)   // correct response received
      break;
  }
  if (token!=SD_TOKEN_DATA_ACCEPT){
    printf("No data accept token: last token=0x%02x\n", token);
    return (-1);
  }
  /* wait for write completion */
  for(i=0; i<SD_WRITE_TIME_OUT; i++){
    token = sd_spi_rd_byte(sd_base);
    if (token==0xff)                 // correct response received
      break;
  }
  if (token != 0xff){
    printf("Write completion timeout: last token=0x%02x\n", token);
     return (-1);
  }
  return (0);          // ok
}


已退回1积分

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top