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
//CMD55 efines 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;
}
|