OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2611|回复: 9

fatfs SD驱动打开报错

[复制链接]

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
发表于 2015-10-23 18:23:44 | 显示全部楼层 |阅读模式
5金钱

SD卡调用f_mkfs时里面会调用初始化函数,正常通过的,但是会卡在ff.c4166行,写有问题,命令响应值不是0x00


 ST_WORD(tbl+BS_55AA, 0xAA55);   /* Signature (Offset is fixed here regardless of sector size) */
 if (disk_write(pdrv, tbl, b_vol, 1)) /* Write it to the VBR sector */    ---有时会成功,在执行的话有问题
  return FR_DISK_ERR;
 if (fmt == FS_FAT32)     /* Write backup VBR if needed (VBR+6) */
  disk_write(pdrv, tbl, b_vol + 6, 1);


如果以上执行成功后,问题会出现在ff.c的4189行,n_fat这个值>30000,执行几次就不行,挂了

 for (n = 1; n < n_fat; n++)
  {  /* This loop may take a time on FAT32 volume due to many single sector writes */
   if (disk_write(pdrv, tbl, wsect++, 1))
    return FR_DISK_ERR;
  }
 

后来在f_mount后不再调用f_mkfs(格式化已经在电脑执行过了,就想不再调用,故注释掉),直接调用f_open打开文件

/*##-4- Create and Open a new text file object with write access #####*/
       nRet = f_open(&file, "0:/STM.txt", FA_CREATE_ALWAYS | FA_WRITE);



执行到find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));里的

fs->fs_type = 0;     /* Clear the file system object */
 fs->drv = LD2PD(vol);    /* Bind the logical drive and a physical drive */
 stat = disk_initialize(fs->drv); /* Initialize the physical drive */    ---按理说正常初始化啊,在验证ACMD41时反而一直没响应值
 if (stat & STA_NOINIT)    /* Check if the initialization succeeded */
  return FR_NOT_READY;   /* Failed to initialize due to no medium or hard error *


初始化函数:

uint8_t MicroSD_init(void)
{

 uint8_t counter;
 uint32_t timeout = 0XFFFE;
 uint8_t szBuf[4];

 /* SD chip select high */
 uint16_t i;
 // MicroSD_SPIx_SetSpeed(MicroSD_SPI_SPEED_4);
 MICROSD_CS_HIGH();

 for (counter = 0; counter < 0x0F; counter++) // 发送至少74个脉冲
 {
  /* Send dummy byte 0xFF */
  MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
 }

 /*SD initialized and set to SPI mode properly */
 /* Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode and wait for In Idle State Response (R1 Format) equal to 0x01 */

 if (MicroSD_SendCmd(MICROSD_CMD_GO_IDLE_STATE, 0, 0x95, SD_IN_IDLE_STATE) != MSD_OK)
 {
  /* No Idle State Response: return response failure */
  return MSD_ERROR;
 }

 /*----------Activates the card initialization process-----------*/
 /* Send CMD1 (Activates the card process) until response equal to 0x0 and Wait for no error Response (R1 Format) equal to 0x00 */
 //while (MicroSD_SendCmd(MICROSD_CMD_SEND_OP_COND, 0, 0xFF, SD_RESPONSE_NO_ERROR) != MSD_OK);
 //Sends SD Memory Card interface condition that includes host supply voltage information and asks  accessed card whether card can
 // operate in supplied voltage range.Reserved bits shall be set to '0'.
 if (MicroSD_SendCmd(MICROSD_CMD_SEND_IF_COND, 0x1AA, 0x87, 0x01) == MSD_OK)             //SD V2.0
 {
  for (i = 0; i < 4; i++)
  {
   szBuf = MicroSD_IO_ReadByte();
  }

  //Get trailing return value of R7 resp
  //CMD55efines to the card that the next command is an application specific command rather than a standard command
  //CMD41:Sends host capacity support information and activates the card's initialization process. Reserved bits shall be set to '0'

  //HAL_Delay(3000);
  do
  {
   MicroSD_SendCmd(MICROSD_CMD_APP, 0, 0X01, 0x01);
  }
  while ((MicroSD_SendCmd(MICROSD_CMD_41, 0x40000000, 0X01, 0x00) != MSD_OK) && timeout--);

  if (timeout == 0)
  {
   return MSD_ERROR;
  }

 }
 //MicroSD_SPIx_SetSpeed(MicroSD_SPI_SPEED_2);
 return MSD_OK;
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-23 18:24:37 | 显示全部楼层
原子兄帮忙看看,是不是我的配置文件哪里不对啊?急需高手指点
回复

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2015-10-23 22:30:24 | 显示全部楼层
建议你将SPI驱动SD代码贴出来,你这样不知道怎么分析
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2015-10-25 22:47:51 | 显示全部楼层
可以参考我们的例程.
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-26 08:54:11 | 显示全部楼层
#include <stdio.h>
#include "stm32f1xx_hal_sd.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_conf.h"
#include "MicroCard.h"
#include "Spi.h"

uint8_t SdStatus = SD_PRESENT;
SD_CardInfo SDCardInfo;
void SD_Delay(uint32_t nTime)
{
while (nTime)
{
nTime--;
}
}
#define EVAL_SPIx_TIMEOUT_MAX                   1000

#ifdef HAL_SPI_MODULE_ENABLED
uint32_t SpixTimeout = EVAL_SPIx_TIMEOUT_MAX;        /*<! Value of Timeout when SPI communication fails */

SPI_HandleTypeDef heval_Spi;
#endif /* HAL_SPI_MODULE_ENABLED */

void GetSPI_Handle(SPI_HandleTypeDef pSpiDef)
{
heval_Spi = pSpiDef;
}
void MicroSD_SPIx_SetSpeed(uint8_t SpeedSet)
{
SpeedSet &= 0X07; //限制范围
SPI2->CR1 &= 0XFFC7;
SPI2->CR1 |= SpeedSet << 3; //设置SPI1速度
SPI2->CR1 |= 1 << 6;  //SPI设备使能
}

uint8_t MicroSD_init(void)
{

uint8_t counter;
uint32_t timeout = 0XFFFE;
uint8_t szBuf[4];

/* SD chip select high */
uint16_t i;
// MicroSD_SPIx_SetSpeed(MicroSD_SPI_SPEED_4);
MICROSD_CS_HIGH();

for (counter = 0; counter < 0x0F; counter++) // 发送至少74个脉冲
{
/* Send dummy byte 0xFF */
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
}

/*SD initialized and set to SPI mode properly */
/* Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode and wait for In Idle State Response (R1 Format) equal to 0x01 */

if (MicroSD_SendCmd(MICROSD_CMD_GO_IDLE_STATE, 0, 0x95, SD_IN_IDLE_STATE) != MSD_OK)
{
/* No Idle State Response: return response failure */
return MSD_ERROR;
}

/*----------Activates the card initialization process-----------*/
/* Send CMD1 (Activates the card process) until response equal to 0x0 and Wait for no error Response (R1 Format) equal to 0x00 */
//while (MicroSD_SendCmd(MICROSD_CMD_SEND_OP_COND, 0, 0xFF, SD_RESPONSE_NO_ERROR) != MSD_OK);
//Sends SD Memory Card interface condition that includes host supply voltage information and asks  accessed card whether card can
// operate in supplied voltage range.Reserved bits shall be set to '0'.
if (MicroSD_SendCmd(MICROSD_CMD_SEND_IF_COND, 0x1AA, 0x87, 0x01) == MSD_OK)             //SD V2.0
{
for (i = 0; i < 4; i++)
{
szBuf = MicroSD_IO_ReadByte();
}

//Get trailing return value of R7 resp
//CMD55efines to the card that the next command is an application specific command rather than a standard command
//CMD41:Sends host capacity support information and activates the card's initialization process. Reserved bits shall be set to '0'

//HAL_Delay(3000);
do
{
MicroSD_SendCmd(MICROSD_CMD_APP, 0, 0X01, 0x01);
}
while ((MicroSD_SendCmd(MICROSD_CMD_41, 0x40000000, 0X01, 0x00) != MSD_OK) && timeout--);

if (timeout == 0)
{
return MSD_ERROR;
}

}
//MicroSD_SPIx_SetSpeed(MicroSD_SPI_SPEED_2);
return MSD_OK;
}

uint8_t MicroSD_IsDetected(void)
{
__IO uint8_t status = SD_PRESENT;

/* Check SD card detect pin */
if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET)
{
status = SD_NOT_PRESENT;
}

return status;
}

uint8_t MicroSD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
{
uint8_t status = MSD_ERROR;

if (MicroSD_IO_WriteCmd(Cmd, Arg, Crc, Response) == HAL_OK)
{
status = MSD_OK;
}

/* Send Dummy Byte */
MicroSD_IO_WriteDummy();
//printf("MicroSD_SendCmd end\r\n");
return status;
}

void MicroSD_IO_WriteDummy(void)
{
/* SD chip select high */
MICROSD_CS_HIGH();

/* Send Dummy byte 0xFF */
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
}

HAL_StatusTypeDef  MicroSD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
{
uint8_t frame[6];
uint32_t counter = 0x00;
//uint32_t timeout = 0xfffe;
//uint8_t R1;

/* repare Frame to send */
frame[0] = Cmd | 0x40;        /* Construct byte 1 */
frame[1] = Arg >> 24;         /* Construct byte 2 */
frame[2] = Arg >> 16;         /* Construct byte 3 */
frame[3] = Arg >> 8;          /* Construct byte 4 */
frame[4] = Arg;               /* Construct byte 5 */
frame[5] = Crc;               /* Construct CRC: byte 6 */

/* SD chip select low */
MICROSD_CS_LOW();

/* Send Frame */
for (counter = 0; counter < 6; counter++)
{
MicroSD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
}

if (Response != SD_NO_RESPONSE_EXPECTED)
{
return MicroSD_IO_WaitResponse(Response);
}

return HAL_OK;
}

void MicroSD_IO_WriteByte(uint8_t Data)
{
/* Send the byte */
HAL_StatusTypeDef status = HAL_OK;
status = HAL_SPI_Transmit(&heval_Spi, (uint8_t *) &Data, 1, 400);
if (status != HAL_OK)
{
/* Execute user timeout callback */
printf("status=%d", status);
}
}

HAL_StatusTypeDef MicroSD_IO_WaitResponse(uint8_t Response)
{
uint32_t timeout = 0xFFFF;
uint8_t resp = 0;
/* Check if response is got or a timeout is happen */
do
{
resp = MicroSD_IO_ReadByte();
timeout--;
}
while ((resp != Response) && timeout);

if (timeout == 0)
{
/* After time out */
return HAL_TIMEOUT;
}
else
{
/* Right response got */
return HAL_OK;
}
}

uint8_t MicroSD_IO_ReadByte(void)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t          rdval = 0;
uint8_t          wrval = 0xFF;

status = HAL_SPI_TransmitReceive(&heval_Spi, &wrval, &rdval, 1, 100);
/* Check the communication status */
if (status != HAL_OK)
{
/* Execute user timeout callback */
status = HAL_ERROR;
}
return rdval;
}

/**
  * @brief SPI Read 4 bytes from device
  * @retval Read data
*/


uint8_t MicroSD_Read_Disk(uint8_t *pRdbuf, uint32_t sector, uint8_t cnt)
{
uint8_t R1 = MSD_ERROR;

/* Send CMD16 (MICROMICROSD_CMD_SET_BLOCKLEN) to set the size of the block and
   Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_SET_BLOCKLEN, 512, 0xFF, SD_RESPONSE_NO_ERROR) != HAL_OK)
{
return MSD_ERROR;
}

/* Data transfer */
if (cnt)
{
/* Send dummy byte: 8 Clock pulses of delay */
MicroSD_IO_WriteDummy();

/* Send CMD17 (MICROMICROSD_CMD_READ_SINGLE_BLOCK) to read one block */
/* Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_READ_SINGLE_BLOCK, sector, 0x01, SD_RESPONSE_NO_ERROR) == HAL_OK)
{
R1 = MicroSD_Receive_Block(pRdbuf, 512);
}
}
else
{
if (MicroSD_IO_WriteCmd(MICROSD_CMD_READ_MULT_BLOCK, sector, 0x01, SD_RESPONSE_NO_ERROR) == HAL_OK)
{
do
{
R1 = MicroSD_Receive_Block(pRdbuf, 512);
pRdbuf += 512;

}
while (--cnt && R1 == 0);
MicroSD_IO_WriteCmd(SD_CMD_STOP_TRANSMISSION, 0, 0X01,0X00); //发送停止命令
}
}

/* Send dummy byte: 8 Clock pulses of delay */
MicroSD_IO_WriteDummy();
    
/* Returns the reponse */
return R1;
}

uint8_t MicroSD_Write_Disk(uint8_t *pWrBuf, uint32_t sector, uint8_t cnt)
{
uint8_t R1 = MSD_ERROR;
    
    HAL_Delay(1);
/* Data transfer */
if (cnt == 1)
{
/* Send CMD24 (MICROMICROSD_CMD_WRITE_SINGLE_BLOCK) to write blocks  and Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_WRITE_SINGLE_BLOCK, sector, 0xff, 0x00) ==  HAL_OK)
{
R1 = MicroSD_Send_Block(pWrBuf, SD_START_DATA_SINGLE_BLOCK_WRITE);
}

}
else    //MULTI BLOCK
{
/* Send CMD25 (MICROSD_CMD_WRITE_MULT_BLOCK) to write blocks  and Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_WRITE_MULT_BLOCK, sector, 0x01, SD_RESPONSE_NO_ERROR) != HAL_OK)
{
do
{
R1 = MicroSD_Send_Block(pWrBuf, SD_START_DATA_MULTIPLE_BLOCK_WRITE);
pWrBuf += 512;
}
while (--cnt && R1 == 0);
R1 = MicroSD_Send_Block(0, SD_STOP_DATA_MULTIPLE_BLOCK_WRITE); //接收512个字节
}
}

/* Send dummy byte: 8 Clock pulses of delay */
MicroSD_IO_WriteDummy();

/* Returns the reponse */
return R1;
}
uint8_t MicroSD_WaitReady(void)
{
    uint32_t t = 0;
do
{
if (MicroSD_IO_ReadByte() == 0XFF) { return 0; } //OK
t++;
}
while (t < 0XFFFFFF); //等待
return 1;
}
uint8_t MicroSD_Send_Block(uint8_t *buf, uint8_t cmd)
{
uint32_t counter = 0;
uint8_t rvalue;

/* Send dummy byte */
//MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
    if (MicroSD_WaitReady()) 
    {
        return 1;
    }
    
/* Send the data token to signify the start of the data */
MicroSD_IO_WriteByte(cmd);

/* Write the block data to SD: write count data by block */
for (counter = 0; counter < 512; counter++)
{
/* Send the pointed byte */
MicroSD_IO_WriteByte(buf[counter]);
}

/* Set next write address */

/* ut CRC bytes (not really needed by us, but required by SD) */
MicroSD_IO_ReadByte();
MicroSD_IO_ReadByte();

/* Read data response */
if (MicroSD_GetDataResponse() == SD_DATA_OK)
{
/* Set response value to success */
rvalue = MSD_OK;
}
else
{
/* Set response value to failure */
rvalue = MSD_ERROR;
}
return rvalue;
}
uint8_t MicroSD_Receive_Block(uint8_t *pRdbuf, uint16_t len)
{
uint32_t counter = 0;
uint8_t rvalue = MSD_ERROR;

/* Now look for the data token to signify the start of the data */
if (MicroSD_IO_WaitResponse(SD_START_DATA_SINGLE_BLOCK_READ) == HAL_OK)
{
/* Read the SD block data: read NumByteToRead data */
for (counter = 0; counter < len; counter++)
{
/* Read the pointed data */
pRdbuf[counter] = MicroSD_IO_ReadByte();
pRdbuf++;
}

/* get CRC bytes (not really needed by us, but required by SD) */
MicroSD_IO_ReadByte();
MicroSD_IO_ReadByte();
/* Set response value to success */
rvalue = MSD_OK;
}
else
{
/* Set response value to failure */
rvalue = MSD_ERROR;
}

return rvalue;
}
SD_Info MicroSD_GetDataResponse(void)
{
uint32_t counter = 0;
SD_Info response, rvalue;

while (counter <= 0xff)
{
/* Read response */
response = (SD_Info)MicroSD_IO_ReadByte();

/* Mask unused bits */
response &= 0x1F;
switch (response)
{
case SD_DATA_OK:
{
rvalue = SD_DATA_OK;
break;
}
case SD_DATA_CRC_ERROR:
return SD_DATA_CRC_ERROR;
case SD_DATA_WRITE_ERROR:
return SD_DATA_WRITE_ERROR;
default:
{
rvalue = SD_DATA_OTHER_ERROR;
break;
}
}
/* Exit loop in case of data ok */
if (rvalue == SD_DATA_OK)
{
break;
}
/* Increment loop counter */
counter++;
}

/* Wait null data */
while (MicroSD_IO_ReadByte() != 0);

/* Return response */
return response;
}


uint8_t MicroSD_GetCardInfo(SD_CardInfo *pCardInfo)
{
uint8_t status = MSD_ERROR;

MicroSD_GetCSDRegister(&(pCardInfo->Csd));
status = MicroSD_GetCIDRegister(&(pCardInfo->Cid));
pCardInfo->CardCapacity = (pCardInfo->Csd.DeviceSize + 1) ;
pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.DeviceSizeMul + 2));
pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;

/* Returns the reponse */
return status;
}

uint8_t MicroSD_GetCSDRegister(SD_CSD *Csd)
{
uint32_t counter = 0;
uint8_t rvalue = MSD_ERROR;
uint8_t szCSDBuf[16];

/* Send CMD9 (CSD register) or CMD10(CSD register) and Wait for response in the R1 format (0x00 is no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_SEND_CSD, 0, 0xFF, SD_RESPONSE_NO_ERROR) == HAL_OK)
{
if (MicroSD_IO_WaitResponse(SD_START_DATA_SINGLE_BLOCK_READ) == HAL_OK)
{
for (counter = 0; counter < 16; counter++)
{
/* Store CSD register value on szCSDBuf */
szCSDBuf[counter] = MicroSD_IO_ReadByte();
//SD_Delay(5);
//printf("%d--------%x",counter,szCSDBuf[counter]);
}

/* Get CRC bytes (not really needed by us, but required by SD) */
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);

/* Set response value to success */
rvalue = MSD_OK;
}
}
/* Send dummy byte: 8 Clock pulses of delay */
MicroSD_IO_WriteDummy();

if (rvalue == SD_RESPONSE_NO_ERROR)
{
/* Byte 0 */
Csd->CSDStruct = (szCSDBuf[0] & 0xC0) >> 6;
Csd->SysSpecVersion = (szCSDBuf[0] & 0x3C) >> 2;
Csd->Reserved1 = szCSDBuf[0] & 0x03;

/* Byte 1 */
Csd->TAAC = szCSDBuf[1];

/* Byte 2 */
Csd->NSAC = szCSDBuf[2];

/* Byte 3 */
Csd->MaxBusClkFrec = szCSDBuf[3];

/* Byte 4 */
Csd->CardComdClasses = szCSDBuf[4] << 4;

/* Byte 5 */
Csd->CardComdClasses |= (szCSDBuf[5] & 0xF0) >> 4;
Csd->RdBlockLen = szCSDBuf[5] & 0x0F;

/* Byte 6 */
Csd->artBlockRead = (szCSDBuf[6] & 0x80) >> 7;
Csd->WrBlockMisalign = (szCSDBuf[6] & 0x40) >> 6;
Csd->RdBlockMisalign = (szCSDBuf[6] & 0x20) >> 5;
Csd->DSRImpl = (szCSDBuf[6] & 0x10) >> 4;
Csd->Reserved2 = 0; /*!< Reserved */

Csd->DeviceSize = (szCSDBuf[6] & 0x03) << 10;

/* Byte 7 */
Csd->DeviceSize |= (szCSDBuf[7]) << 2;

/* Byte 8 */
Csd->DeviceSize |= (szCSDBuf[8] & 0xC0) >> 6;

Csd->MaxRdCurrentVDDMin = (szCSDBuf[8] & 0x38) >> 3;
Csd->MaxRdCurrentVDDMax = (szCSDBuf[8] & 0x07);

/* Byte 9 */
Csd->MaxWrCurrentVDDMin = (szCSDBuf[9] & 0xE0) >> 5;
Csd->MaxWrCurrentVDDMax = (szCSDBuf[9] & 0x1C) >> 2;
Csd->DeviceSizeMul = (szCSDBuf[9] & 0x03) << 1;
/* Byte 10 */
Csd->DeviceSizeMul |= (szCSDBuf[10] & 0x80) >> 7;

Csd->EraseGrSize = (szCSDBuf[10] & 0x40) >> 6;
Csd->EraseGrMul = (szCSDBuf[10] & 0x3F) << 1;

/* Byte 11 */
Csd->EraseGrMul |= (szCSDBuf[11] & 0x80) >> 7;
Csd->WrProtectGrSize = (szCSDBuf[11] & 0x7F);

/* Byte 12 */
Csd->WrProtectGrEnable = (szCSDBuf[12] & 0x80) >> 7;
Csd->ManDeflECC = (szCSDBuf[12] & 0x60) >> 5;
Csd->WrSpeedFact = (szCSDBuf[12] & 0x1C) >> 2;
Csd->MaxWrBlockLen = (szCSDBuf[12] & 0x03) << 2;

/* Byte 13 */
Csd->MaxWrBlockLen |= (szCSDBuf[13] & 0xC0) >> 6;
Csd->WriteBlockPaPartial = (szCSDBuf[13] & 0x20) >> 5;
Csd->Reserved3 = 0;
Csd->ContentProtectAppli = (szCSDBuf[13] & 0x01);

/* Byte 14 */
Csd->FileFormatGrouop = (szCSDBuf[14] & 0x80) >> 7;
Csd->CopyFlag = (szCSDBuf[14] & 0x40) >> 6;
Csd->ermWrProtect = (szCSDBuf[14] & 0x20) >> 5;
Csd->TempWrProtect = (szCSDBuf[14] & 0x10) >> 4;
Csd->FileFormat = (szCSDBuf[14] & 0x0C) >> 2;
Csd->ECC = (szCSDBuf[14] & 0x03);

/* Byte 15 */
Csd->CSD_CRC = (szCSDBuf[15] & 0xFE) >> 1;
Csd->Reserved4 = 1;
}
/* Return the reponse */
return rvalue;
}

uint8_t MicroSD_GetCIDRegister(SD_CID *Cid)
{
uint32_t counter = 0;
uint8_t rvalue = MSD_ERROR;
uint8_t szCIDBuf[16];

/* Send CMD10 (CID register) and Wait for response in the R1 format (0x00
is no errors) */
if (MicroSD_IO_WriteCmd(MICROSD_CMD_SEND_CID, 0, 0xFF, SD_RESPONSE_NO_ERROR) == HAL_OK)
{
if (MicroSD_IO_WaitResponse(SD_START_DATA_SINGLE_BLOCK_READ) == HAL_OK)
{
/* Store CID register value on szCIDBuf */
for (counter = 0; counter < 16; counter++)
{
szCIDBuf[counter] = MicroSD_IO_ReadByte();
}

/* Get CRC bytes (not really needed by us, but required by SD) */
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);
MicroSD_IO_WriteByte(SD_DUMMY_BYTE);

/* Set response value to success */
rvalue = MSD_OK;
}
}

/* Send dummy byte: 8 Clock pulses of delay */
MicroSD_IO_WriteDummy();

if (rvalue == MSD_OK)
{
/* Byte 0 */
Cid->ManufacturerID = szCIDBuf[0];

/* Byte 1 */
Cid->OEM_AppliID = szCIDBuf[1] << 8;

/* Byte 2 */
Cid->OEM_AppliID |= szCIDBuf[2];

/* Byte 3 */
Cid->rodName1 = szCIDBuf[3] << 24;

/* Byte 4 */
Cid->rodName1 |= szCIDBuf[4] << 16;

/* Byte 5 */
Cid->rodName1 |= szCIDBuf[5] << 8;

/* Byte 6 */
Cid->rodName1 |= szCIDBuf[6];

/* Byte 7 */
Cid->rodName2 = szCIDBuf[7];

/* Byte 8 */
Cid->rodRev = szCIDBuf[8];

/* Byte 9 */
Cid->ProdSN = szCIDBuf[9] << 24;

/* Byte 10 */
Cid->ProdSN |= szCIDBuf[10] << 16;

/* Byte 11 */
Cid->ProdSN |= szCIDBuf[11] << 8;

/* Byte 12 */
Cid->ProdSN |= szCIDBuf[12];

/* Byte 13 */
Cid->Reserved1 |= (szCIDBuf[13] & 0xF0) >> 4;
Cid->ManufactDate = (szCIDBuf[13] & 0x0F) << 8;

/* Byte 14 */
Cid->ManufactDate |= szCIDBuf[14];

/* Byte 15 */
Cid->CID_CRC = (szCIDBuf[15] & 0xFE) >> 1;
Cid->Reserved2 = 1;
}
/* Return the reponse */
return rvalue;
}
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-26 08:55:38 | 显示全部楼层
回复【4楼】正点原子:
----------------------------
这个就是参考原子例程的,逻辑是一样的啊,只是这个是新版本的封装后的修改
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-26 08:55:56 | 显示全部楼层
回复【3楼】八度空间:
---------------------------------
见下面回帖
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-26 08:56:40 | 显示全部楼层
回复【7楼】AlexXue:
---------------------------------
我怀疑是不是我的延时有问题,可以加我QQ:2354431375嘛
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2015-10-13
在线时间
0 小时
 楼主| 发表于 2015-10-26 09:11:07 | 显示全部楼层
这个是我的整个工程,请帮忙看下

play - 1014micro_1026.rar

9 MB, 下载次数: 22

回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2015-11-1 23:23:27 | 显示全部楼层
帮顶....
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-20 19:56

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表